changeset 14362:665b814f8fd7

[gaim-migrate @ 17068] Fix the wm to give focus to the buddylist after you toggle it on with alt+b. Draw a line on the right of the buddylist. this should also show how to add other window-decorations. Draw a [X] at the corner of the windows, clicking which closes the windows. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Mon, 28 Aug 2006 05:39:42 +0000
parents 0c3957362c69
children 4558e71a5a34
files console/libgnt/gntmain.c console/libgnt/gntwm.h console/libgnt/wms/s.c
diffstat 3 files changed, 117 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/console/libgnt/gntmain.c	Sun Aug 27 23:04:55 2006 +0000
+++ b/console/libgnt/gntmain.c	Mon Aug 28 05:39:42 2006 +0000
@@ -77,6 +77,7 @@
 {
 	NULL,   /* new_window */
 	NULL,   /* close_window */
+	NULL,   /* window_resized */
 	NULL,   /* key_pressed */
 	NULL,   /* mouse clicked */
 	bring_on_top, /* give_focus */
@@ -516,12 +517,10 @@
  *  - bring a window on top if you click on its taskbar
  *  - click on the top-bar of the active window and drag+drop to move a window
  *  - click on a window to bring it to focus
+ *   - allow scrolling in tree/textview on wheel-scroll event
+ *   - click to activate button or select a row in tree
  *  wishlist:
  *   - have a little [X] on the windows, and clicking it will close that window.
- *   - allow scrolling in tree/textview on wheel-scroll event
- *   - click to activate button or select a row in tree
- *      - all these can be fulfilled by adding a "clicked" event for GntWidget
- *        which will send the (x,y) to the widget. (look at "key_pressed" for hints)
  */
 static gboolean
 detect_mouse_action(const char *buffer)
@@ -536,6 +535,8 @@
 	static GntWidget *remember = NULL;
 	static int offset = 0;
 	GntMouseEvent event;
+	GntWidget *widget = NULL;
+	GList *iter;
 
 	if (!ordered || buffer[0] != 27)
 		return FALSE;
@@ -551,30 +552,19 @@
 	x -= 33;
 	y -= 33;
 
+	for (iter = ordered; iter; iter = iter->next) {
+		GntWidget *wid = iter->data;
+		if (x >= wid->priv.x && x < wid->priv.x + wid->priv.width) {
+			if (y >= wid->priv.y && y < wid->priv.y + wid->priv.height) {
+				widget = wid;
+				break;
+			}
+		}
+	}
 	if (strncmp(buffer, "[M ", 3) == 0) {
 		/* left button down */
 		/* Bring the window you clicked on to front */
 		/* If you click on the topbar, then you can drag to move the window */
-		GList *iter;
-		for (iter = ordered; iter; iter = iter->next) {
-			GntWidget *wid = iter->data;
-			if (x >= wid->priv.x && x < wid->priv.x + wid->priv.width) {
-				if (y >= wid->priv.y && y < wid->priv.y + wid->priv.height) {
-					if (iter != ordered) {
-						GntWidget *w = ordered->data;
-						ordered = g_list_bring_to_front(ordered, iter->data);
-						wm.give_focus(ordered->data);
-						gnt_widget_set_focus(w, FALSE);
-					}
-					if (y == wid->priv.y) {
-						offset = x - wid->priv.x;
-						remember = wid;
-						button = MOUSE_LEFT;
-					}
-					break;
-				}
-			}
-		}
 		event = GNT_LEFT_MOUSE_DOWN;
 	} else if (strncmp(buffer, "[M\"", 3) == 0) {
 		/* right button down */
@@ -590,7 +580,28 @@
 		event = GNT_MOUSE_SCROLL_DOWN;
 	} else if (strncmp(buffer, "[M#", 3) == 0) {
 		/* button up */
+		event = GNT_MOUSE_UP;
+	} else
+		return FALSE;
+	
+	if (wm.mouse_clicked && wm.mouse_clicked(event, x, y, widget))
+		return TRUE;
+	
+	if (event == GNT_LEFT_MOUSE_DOWN && widget) {
+		if (widget != ordered->data) {
+			GntWidget *w = ordered->data;
+			ordered = g_list_bring_to_front(ordered, widget);
+			wm.give_focus(ordered->data);
+			gnt_widget_set_focus(w, FALSE);
+		}
+		if (y == widget->priv.y) {
+			offset = x - widget->priv.x;
+			remember = widget;
+			button = MOUSE_LEFT;
+		}
+	} else if (event == GNT_MOUSE_UP) {
 		if (button == MOUSE_NONE && y == getmaxy(stdscr) - 1) {
+			/* Clicked on the taskbar */
 			int n = g_list_length(focus_list);
 			if (n) {
 				int width = getmaxx(stdscr) / n;
@@ -606,9 +617,7 @@
 		button = MOUSE_NONE;
 		remember = NULL;
 		offset = 0;
-		event = GNT_MOUSE_UP;
-	} else
-		return FALSE;
+	}
 
 	gnt_widget_clicked(ordered->data, event, x, y);
 	return FALSE; /* XXX: this should be TRUE */
@@ -1141,7 +1150,10 @@
 		hide_panel(node->panel);
 		gnt_widget_set_size(widget, width, height);
 		gnt_widget_draw(widget);
-		replace_panel(node->panel, widget->window);
+		if (wm.window_resized)
+			node->panel = wm.window_resized(node->panel, widget);
+		else
+			replace_panel(node->panel, widget->window);
 		show_panel(node->panel);
 		update_screen(NULL);
 	}
--- a/console/libgnt/gntwm.h	Sun Aug 27 23:04:55 2006 +0000
+++ b/console/libgnt/gntwm.h	Mon Aug 28 05:39:42 2006 +0000
@@ -16,6 +16,10 @@
 	/* This is called when a window is being closed */
 	gboolean (*close_window)(GntWidget *win);
 
+	/* Can del_panel the old panel and return a new_panel.
+	 * Otherwise, this should at least do a replace_panel. */
+	PANEL *(*window_resized)(PANEL *pan, GntWidget *win);
+
 	/* This should usually return NULL if the keys were processed by the WM.
 	 * If not, the WM can simply return the original string, which will be
 	 * processed by the default WM. The custom WM can also return a different
@@ -23,8 +27,7 @@
 	 */
 	const char *(*key_pressed)(const char *key);
 
-	/* Not decided yet */
-	gboolean (*mouse_clicked)(void);
+	gboolean (*mouse_clicked)(GntMouseEvent event, int x, int y, GntWidget *widget);
 
 	/* Whatever the WM wants to do when a window is given focus */
 	void (*give_focus)(GntWidget *widget);
--- a/console/libgnt/wms/s.c	Sun Aug 27 23:04:55 2006 +0000
+++ b/console/libgnt/wms/s.c	Mon Aug 28 05:39:42 2006 +0000
@@ -7,6 +7,43 @@
 
 static GntWM *gwm;
 
+static void
+envelope_buddylist(GntWidget *win)
+{
+	int w, h;
+	gnt_widget_get_size(win, &w, &h);
+	wresize(win->window, h, w + 1);
+	mvwvline(win->window, 0, w, ACS_VLINE | COLOR_PAIR(GNT_COLOR_NORMAL), h);
+}
+
+static void
+envelope_normal_window(GntWidget *win)
+{
+	int w, h;
+
+	if (GNT_WIDGET_IS_FLAG_SET(win, GNT_WIDGET_NO_BORDER))
+		return;
+
+	gnt_widget_get_size(win, &w, &h);
+	wbkgdset(win->window, ' ' | COLOR_PAIR(GNT_COLOR_NORMAL));
+	mvwprintw(win->window, 0, w - 4, "[X]");
+}
+
+static PANEL *
+s_resize_window(PANEL *panel, GntWidget *win)
+{
+	const char *name;
+
+	name = gnt_widget_get_name(win);
+	if (name && strcmp(name, "buddylist") == 0) {
+		envelope_buddylist(win);
+	} else {
+		envelope_normal_window(win);
+	}
+	replace_panel(panel, win->window);
+	return panel;
+}
+
 static PANEL *
 s_new_window(GntWidget *win)
 {
@@ -36,6 +73,7 @@
 
 		gnt_widget_set_size(win, w, h);
 		gnt_widget_draw(win);
+		envelope_buddylist(win);
 	} else if (name && strcmp(name, "conversation-window") == 0) {
 		/* Put the conversation windows to the far-right */
 		x = maxx - w;
@@ -43,6 +81,7 @@
 		gnt_widget_set_position(win, x, y);
 		mvwin(win->window, y, x);
 		gnt_widget_draw(win);
+		envelope_normal_window(win);
 	} else if (!GNT_WIDGET_IS_FLAG_SET(win, GNT_WIDGET_TRANSIENT)) {
 		/* In the middle of the screen */
 		x = (maxx - w) / 2;
@@ -50,6 +89,7 @@
 
 		gnt_widget_set_position(win, x, y);
 		mvwin(win->window, y, x);
+		envelope_normal_window(win);
 	}
 
 	return new_panel(win->window);
@@ -69,6 +109,13 @@
 	return NULL;
 }
 
+static gboolean
+give_the_darned_focus(gpointer w)
+{
+	gwm->give_focus(w);
+	return FALSE;
+}
+
 static const char*
 s_key_pressed(const char *key)
 {
@@ -78,7 +125,7 @@
 		if (w == NULL) {
 			gg_blist_show();
 			w = find_widget("buddylist");
-			gwm->give_focus(w);
+			g_timeout_add(0, give_the_darned_focus, w);
 		} else {
 			gnt_widget_destroy(w);
 		}
@@ -87,10 +134,34 @@
 	return key;
 }
 
+static gboolean
+s_mouse_clicked(GntMouseEvent event, int cx, int cy, GntWidget *widget)
+{
+	int x, y, w, h;
+
+	if (!widget)
+		return FALSE;       /* This might a place to bring up a context menu */
+	
+	if (event != GNT_LEFT_MOUSE_DOWN ||
+			GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
+		return FALSE;       /* For now, just the left-button to close a window */
+	
+	gnt_widget_get_position(widget, &x, &y);
+	gnt_widget_get_size(widget, &w, &h);
+
+	if (cy == y && cx == x + w - 3) {
+		gnt_widget_destroy(widget);
+		return TRUE;
+	}
+	return FALSE;
+}
+
 void gntwm_init(GntWM *wm)
 {
 	gwm = wm;
 	wm->new_window = s_new_window;
+	wm->window_resized = s_resize_window;
 	wm->key_pressed = s_key_pressed;
+	wm->mouse_clicked = s_mouse_clicked;
 }