ref: 356554c22998264769a5c516697cbfbd43847ce4
parent: 535b4edd143ceeae870e9c13cdac65ce17e2be48
author: Simon Howard <fraggle@soulsphere.org>
date: Sat Apr 4 17:29:33 EDT 2015
textscreen: Add help URL functionality for windows. This adds the ability to associate a URL with a window that gives some extra information about it and the configuration options found in it.
--- a/textscreen/examples/guitest.c
+++ b/textscreen/examples/guitest.c
@@ -96,6 +96,8 @@
window = TXT_NewWindow("Window test");
+ TXT_SetWindowHelpURL(window, "https://www.youtube.com/watch?v=dQw4w9WgXcQ");
+
TXT_AddWidget(window, TXT_NewSeparator("Main section"));
table = TXT_NewTable(3);
--- a/textscreen/txt_desktop.c
+++ b/textscreen/txt_desktop.c
@@ -25,6 +25,7 @@
#include "txt_separator.h"
#include "txt_window.h"
+#define HELP_KEY KEY_F1
#define MAXWINDOWS 128
static char *desktop_title;
@@ -188,6 +189,37 @@
TXT_Puts(title);
}
+static void DrawHelpIndicator(void)
+{
+ char keybuf[10];
+ int fgcolor;
+ int x, y;
+
+ TXT_GetKeyDescription(HELP_KEY, keybuf, sizeof(keybuf));
+
+ TXT_GetMousePosition(&x, &y);
+
+ if (y == 0 && x >= TXT_SCREEN_W - 9)
+ {
+ fgcolor = TXT_COLOR_GREY;
+ TXT_BGColor(TXT_COLOR_BLACK, 0);
+ }
+ else
+ {
+ fgcolor = TXT_COLOR_BLACK;
+ TXT_BGColor(TXT_COLOR_GREY, 0);
+ }
+
+ TXT_GotoXY(TXT_SCREEN_W - 9, 0);
+
+ TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
+ TXT_DrawString(" ");
+ TXT_DrawString(keybuf);
+
+ TXT_FGColor(fgcolor);
+ TXT_DrawString("=Help ");
+}
+
void TXT_SetDesktopTitle(char *title)
{
free(desktop_title);
@@ -197,8 +229,9 @@
void TXT_DrawDesktop(void)
{
- int i;
+ txt_window_t *active_window;
const char *title;
+ int i;
TXT_InitClipArea();
@@ -209,6 +242,12 @@
DrawDesktopBackground(title);
+ active_window = TXT_GetActiveWindow();
+ if (active_window != NULL && active_window->help_url != NULL)
+ {
+ DrawHelpIndicator();
+ }
+
for (i=0; i<num_windows; ++i)
{
TXT_DrawWindow(all_windows[i]);
@@ -217,17 +256,53 @@
TXT_UpdateScreen();
}
+// Fallback function to handle key/mouse events that are not handled by
+// the active window.
+static void DesktopInputEvent(int c)
+{
+ txt_window_t *active_window;
+ int x, y;
+
+ switch (c)
+ {
+ case TXT_MOUSE_LEFT:
+ TXT_GetMousePosition(&x, &y);
+
+ // Clicking the top-right of the screen is equivalent
+ // to pressing the help key.
+ if (y == 0 && x >= TXT_SCREEN_W - 9)
+ {
+ DesktopInputEvent(HELP_KEY);
+ }
+ break;
+
+ case HELP_KEY:
+ active_window = TXT_GetActiveWindow();
+ if (active_window != NULL)
+ {
+ TXT_OpenWindowHelpURL(active_window);
+ }
+ break;
+
+ default:
+ break;
+ }
+
+
+}
+
void TXT_DispatchEvents(void)
{
+ txt_window_t *active_window;
int c;
while ((c = TXT_GetChar()) > 0)
{
- if (num_windows > 0)
- {
- // Send the keypress to the top window
+ active_window = TXT_GetActiveWindow();
- TXT_WindowKeyPress(all_windows[num_windows - 1], c);
+ if (active_window != NULL && !TXT_WindowKeyPress(active_window, c))
+ {
+ DesktopInputEvent(c);
}
}
}
--- a/textscreen/txt_desktop.h
+++ b/textscreen/txt_desktop.h
@@ -31,7 +31,7 @@
void TXT_DispatchEvents(void);
void TXT_DrawWindow(txt_window_t *window);
void TXT_SetWindowFocus(txt_window_t *window, int focused);
-void TXT_WindowKeyPress(txt_window_t *window, int c);
+int TXT_WindowKeyPress(txt_window_t *window, int c);
/**
* Set the title displayed at the top of the screen.
--- a/textscreen/txt_gui.c
+++ b/textscreen/txt_gui.c
@@ -488,8 +488,8 @@
cliparea = malloc(sizeof(txt_cliparea_t));
cliparea->x1 = 0;
cliparea->x2 = TXT_SCREEN_W;
- cliparea->y1 = 1;
- cliparea->y2 = TXT_SCREEN_H - 1;
+ cliparea->y1 = 0;
+ cliparea->y2 = TXT_SCREEN_H;
cliparea->next = NULL;
}
}
--- a/textscreen/txt_window.c
+++ b/textscreen/txt_window.c
@@ -70,6 +70,7 @@
win->vert_align = TXT_VERT_CENTER;
win->key_listener = NULL;
win->mouse_listener = NULL;
+ win->help_url = NULL;
TXT_AddWidget(win, TXT_NewSeparator(NULL));
@@ -365,7 +366,7 @@
window->y = y;
}
-static void MouseButtonPress(txt_window_t *window, int b)
+static int MouseButtonPress(txt_window_t *window, int b)
{
int x, y;
int i;
@@ -375,7 +376,7 @@
// Lay out the window, set positions and sizes of all widgets
TXT_LayoutWindow(window);
-
+
// Get the current mouse position
TXT_GetMousePosition(&x, &y);
@@ -390,10 +391,10 @@
if (window->mouse_listener(window, x, y, b,
window->mouse_listener_data))
{
- return;
+ return 1;
}
}
-
+
// Is it within the table range?
widgets = (txt_widget_t *) window;
@@ -402,7 +403,7 @@
&& y >= widgets->y && y < (signed) (widgets->y + widgets->h))
{
TXT_WidgetMousePress(window, x, y, b);
- return;
+ return 1;
}
// Was one of the action area buttons pressed?
@@ -429,21 +430,22 @@
// Pass through mouse press.
TXT_WidgetMousePress(widget, x, y, b);
- return;
+ return 1;
}
}
+
+ return 0;
}
-void TXT_WindowKeyPress(txt_window_t *window, int c)
+int TXT_WindowKeyPress(txt_window_t *window, int c)
{
int i;
// Is this a mouse button ?
-
+
if (c >= TXT_MOUSE_BASE && c < TXT_MOUSE_BASE + TXT_MAX_MOUSE_BUTTONS)
{
- MouseButtonPress(window, c);
- return;
+ return MouseButtonPress(window, c);
}
// Try the window key spy
@@ -454,15 +456,15 @@
if (window->key_listener(window, c, window->key_listener_data))
{
- return;
+ return 1;
}
}
- // Send to the currently selected widget
+ // Send to the currently selected widget:
if (TXT_WidgetKeyPress(window, c))
{
- return;
+ return 1;
}
// Try all of the action buttons
@@ -472,9 +474,11 @@
if (window->actions[i] != NULL
&& TXT_WidgetKeyPress(window->actions[i], c))
{
- return;
+ return 1;
}
}
+
+ return 0;
}
void TXT_SetKeyListener(txt_window_t *window, TxtWindowKeyPress key_listener,
@@ -495,6 +499,48 @@
void TXT_SetWindowFocus(txt_window_t *window, int focused)
{
TXT_SetWidgetFocus(window, focused);
+}
+
+void TXT_SetWindowHelpURL(txt_window_t *window, char *help_url)
+{
+ window->help_url = help_url;
+}
+
+void TXT_OpenURL(char *url)
+{
+ char *cmd;
+ size_t cmd_len;
+
+ cmd_len = strlen(url) + 30;
+ cmd = malloc(cmd_len);
+
+#if defined(_WIN32)
+ TXT_snprintf(cmd, cmd_len, "start \"%s\"", url);
+#elif defined(__MACOSX__)
+ TXT_snprintf(cmd, cmd_len, "open \"%s\"", url);
+#else
+ // The Unix situation sucks as usual, but the closest thing to a
+ // standard that exists is the xdg-utils package.
+ if (system("xdg-open --version 2>/dev/null") != 0)
+ {
+ fprintf(stderr,
+ "xdg-utils is not installed. Can't open this URL:\n%s\n", url);
+ return;
+ }
+
+ TXT_snprintf(cmd, cmd_len, "xdg-open \"%s\"", url);
+#endif
+
+ system(cmd);
+ free(cmd);
+}
+
+void TXT_OpenWindowHelpURL(txt_window_t *window)
+{
+ if (window->help_url != NULL)
+ {
+ TXT_OpenURL(window->help_url);
+ }
}
txt_window_t *TXT_MessageBox(char *title, char *message, ...)
--- a/textscreen/txt_window.h
+++ b/textscreen/txt_window.h
@@ -85,6 +85,10 @@
int window_x, window_y;
unsigned int window_w, window_h;
+
+ // URL of a webpage with help about this window. If set, a help key
+ // indicator is shown while this window is active.
+ char *help_url;
};
/**
@@ -193,6 +197,24 @@
*/
txt_window_t *TXT_MessageBox(char *title, char *message, ...);
+
+/**
+ * Set the help URL for the given window.
+ *
+ * @param window The window.
+ * @param help_url String containing URL of the help page for this
+ * window, or NULL to set no help for this window.
+ */
+
+void TXT_SetWindowHelpURL(txt_window_t *window, char *help_url);
+
+/**
+ * Open the help URL for the given window, if one is set.
+ *
+ * @param window The window.
+ */
+
+void TXT_OpenWindowHelpURL(txt_window_t *window);
#endif /* #ifndef TXT_WINDOW_H */