sources/interface.cpp

branch
protocol5
changeset 84
3bd32eec3d57
parent 80
f992b027374b
parent 83
08bfc3d9d2ae
child 103
b78c0ca832a9
--- a/sources/interface.cpp	Sun May 17 22:07:48 2015 +0300
+++ b/sources/interface.cpp	Wed May 27 21:15:52 2015 +0300
@@ -41,7 +41,7 @@
 //
 int Interface::color_pair (Color fg, Color bg)
 {
-	return COLOR_PAIR ((int (fg) * NUM_COLORS) + int (bg));
+	return COLOR_PAIR (1 + (int (fg) * NUM_COLORS) + int (bg));
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -143,25 +143,46 @@
 Interface::Interface() :
 	Session (this)
 {
-	::initscr();
-	::start_color();
-	::raw();
+#ifdef XCURSES
+    ::Xinitscr(argc, argv);
+#else
+    ::initscr();
+#endif
+
+	::cbreak();
 	::keypad (stdscr, true);
 	::noecho();
 	::refresh();
 	::timeout (0);
-	::use_default_colors();
 	InputHistory.clear();
 	InputHistory << "";
 	OutputLines.clear();
 	OutputLines << ColoredLine();
+	Title.sprintf (APPNAME " %s (%d)", full_version_string(), changeset_date_string());
 
-	for (int i = 0; i < NUM_COLORS; ++i)
-	for (int j = 0; j < NUM_COLORS; ++j)
+	if (::has_colors())
 	{
-		init_pair ((i * NUM_COLORS + j),
-			(i == DEFAULT) ? -1 : i,
-			(j == DEFAULT) ? -1 : j);
+		::start_color();
+		::use_default_colors();
+
+		int defaultFg = (use_default_colors() == OK) ? -1 : COLOR_WHITE;
+		int defaultBg = (use_default_colors() == OK) ? -1 : COLOR_BLACK;
+
+		// Initialize color pairs
+		for (int i = 0; i < NUM_COLORS; ++i)
+		for (int j = 0; j < NUM_COLORS; ++j)
+		{
+			int pairnum = 1 + (i * NUM_COLORS + j);
+			int fg = (i == DEFAULT) ? defaultFg : i;
+			int bg = (j == DEFAULT) ? defaultBg : j;
+
+			if (::init_pair (pairnum, fg, bg) == ERR)
+				print ("Unable to initialize color pair %d (%d, %d)\n", pairnum, fg, bg);
+		}
+	}
+	else
+	{
+		print ("This terminal does not appear to support colors.\n");
 	}
 
 	render_full();
@@ -173,18 +194,13 @@
 //
 void Interface::render_titlebar()
 {
-	String message = Title;
-
-	if (Title.is_empty())
-		message = format (APPNAME " %1 (%2)", full_version_string(), changeset_date_string());
-
-	if (message.length() <= COLS)
+	if (Title.length() <= COLS)
 	{
 		int pair = color_pair (WHITE, BLUE);
-		int startx = (COLS - message.length()) / 2;
-		int endx = startx + message.length();
+		int startx = (COLS - Title.length()) / 2;
+		int endx = startx + Title.length();
 		attron (pair);
-		mvprintw (0, startx, "%s", message.chars());
+		mvprintw (0, startx, "%s", Title.chars());
 		mvhline (0, 0, ' ', startx);
 		mvhline (0, endx, ' ', COLS - endx);
 		attroff (pair);
@@ -382,7 +398,17 @@
 		mvhline (y, x, ' ', width);
 
 		if (i < PlayerNames.size())
-			render_colorline (y, x, width, PlayerNames[i], false);
+		{
+			String displaynick = PlayerNames[i];
+
+			if (displaynick.length() > width)
+			{
+				displaynick = displaynick.mid (0, width - 3);
+				displaynick += "...";
+			}
+
+			mvprintw (y, x, "%s", displaynick.chars());
+		}
 
 		y++;
 	}
@@ -488,12 +514,17 @@
 			String adminText;
 
 			if (Session.num_admins() == 0)
+			{
 				adminText = "No other admins";
+			}
 			else
-				adminText = format ("%1 other admin%s1", Session.num_admins());
+			{
+				adminText.sprintf ("%d other admin%s", Session.num_admins(),
+					Session.num_admins() != 1 ? "s" : "");
+			}
 
-			text = format ("%1 | %2 | %3", Session.address().to_string (IP_WITH_PORT),
-				Session.level(), adminText);
+			text.sprintf ("%s | %s | %s", Session.address().to_string (IP_WITH_PORT).chars(),
+				Session.level().chars(), adminText.chars());
 		}
 		break;
 	}
@@ -501,7 +532,7 @@
 	if (not text.is_empty())
 		text += " | ";
 
-	text += "^N to connect, ^Q to quit";
+	text += "Ctrl+N to connect, Ctrl+Q to quit";
 
 	if (text != StatusBarText)
 	{
@@ -596,6 +627,9 @@
 {
 	int ch = ::getch();
 
+	if (ch < 0)
+		return;
+
 	if (ch == KEY_RESIZE)
 	{
 		::clear();
@@ -696,6 +730,7 @@
 		break;
 
 	case KEY_BACKSPACE:
+	case '\b':
 		if (CursorPosition > 0)
 		{
 			mutable_current_input().remove_at (--CursorPosition);
@@ -775,7 +810,7 @@
 			}
 			catch (std::exception& e)
 			{
-				print ("%1\n", e.what());
+				print ("%s\n", e.what());
 				return;
 			}
 
@@ -872,6 +907,49 @@
 
 // -------------------------------------------------------------------------------------------------
 //
+void Interface::vprint (const char* fmtstr, va_list args)
+{
+	String message;
+	message.vsprintf (fmtstr, args);
+	print_to_console (message);
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+void __cdecl Interface::print (const char* fmtstr, ...)
+{
+	va_list args;
+	va_start (args, fmtstr);
+	vprint (fmtstr, args);
+	va_end (args);
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+void __cdecl Interface::print_warning (const char* fmtstr, ...)
+{
+	va_list args;
+	va_start (args, fmtstr);
+	print_to_console (TEXTCOLOR_BrightYellow "-!- ");
+	vprint (fmtstr, args);
+	print_to_console (TEXTCOLOR_Reset);
+	va_end (args);
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+void __cdecl Interface::print_error (const char* fmtstr, ...)
+{
+	va_list args;
+	va_start (args, fmtstr);
+	print_to_console (TEXTCOLOR_BrightRed "!!! ");
+	vprint (fmtstr, args);
+	print_to_console (TEXTCOLOR_Reset);
+	va_end (args);
+}
+
+// -------------------------------------------------------------------------------------------------
+//
 void Interface::print_to_console (String a)
 {
 	// Zandronum sometimes sends color codes as "\\c" and sometimes as "\x1C".
@@ -914,7 +992,7 @@
 	}
 	catch (std::exception& e)
 	{
-		print ("%1\n", e.what());
+		print ("%s\n", e.what());
 		return;
 	}
 
@@ -930,16 +1008,7 @@
 //
 void Interface::set_player_names (const StringList& names)
 {
-	PlayerNames.clear();
-
-	for (const String& name : names)
-	{
-		ColoredLine coloredname;
-		coloredname.add_string (name);
-		coloredname.finalize();
-		PlayerNames.append (coloredname);
-	}
-
+	PlayerNames = names;
 	NeedNicklistRender = true;
 }
 
@@ -959,12 +1028,3 @@
 		NeedInputRender = true;
 	}
 }
-
-// -------------------------------------------------------------------------------------------------
-//
-void Interface::disconnected()
-{
-	Title = "";
-	update_statusbar();
-	render_titlebar();
-}

mercurial