Patch to movehandle widget to make window snap to corners and edge centers

Jasper Huijsmans jasper at moongroup.com
Fri Mar 7 16:24:20 CET 2003


Hi all,

I would like to get your opinion on the following patch to libxfcegui4.
It patches the move handler widget so that the associated window will
snap to corners and edge centers.

It is probably not the most efficient way to do this, but it works very
well for me.

My question is basically if this is a good way to handle panel (and
iconbox) positioning and if it is alright to make a non-configurable
part of the widget.

	Jasper

Index: xfce_movehandler.c
===================================================================
RCS file:
/cvsroot/xfce/xfce-devel/libxfcegui4/libxfcegui4/xfce_movehandler.c,v
retrieving revision 1.5
diff -u -r1.5 xfce_movehandler.c
--- xfce_movehandler.c	18 Oct 2002 13:00:52 -0000	1.5
+++ xfce_movehandler.c	7 Mar 2003 15:08:40 -0000
@@ -344,6 +344,75 @@
     return event_handled;
 }
 
+#define SNAP_DISTANCE 20
+
+static void snap_to_corners(GtkWidget *widget, int *x, int *y)
+{
+    static int screen_width = 0;
+    static int screen_height = 0;
+    int i;
+    int snapx[8], snapy[8];
+
+    if (screen_width == 0)
+    {
+	screen_width = gdk_screen_width();
+	screen_height = gdk_screen_height();
+    }
+
+    /* top left */
+    snapx[0] = 0;
+    snapy[0] = 0;
+
+    /* top right */
+    snapx[1] = screen_width - widget->allocation.width;
+    snapy[1] = 0;
+    
+    /* bottom left */
+    snapx[2] = 0;
+    snapy[2] = screen_height - widget->allocation.height;
+
+    /* bottom right */
+    snapx[3] = snapx[1];
+    snapy[3] = snapy[2];
+    
+    /* top center */
+    snapx[4] = screen_width / 2 - widget->allocation.width / 2;
+    snapy[4] = 0;
+
+    /* bottom center */
+    snapx[5] = snapx[4];
+    snapy[5] = snapy[2];
+
+    /* left center */
+    snapx[6] = 0;
+    snapy[6] = screen_height / 2 - widget->allocation.height / 2;
+
+    /* right center */
+    snapx[7] = snapx[1];
+    snapy[7] = snapy[6];
+    
+    /* snap to corners or edge centers (8 snap positions) */
+    for (i = 0; i < 8; i++)
+    {
+	int minx, maxx, miny, maxy;
+
+	minx = snapx[i] - SNAP_DISTANCE;
+	maxx = snapx[i] + SNAP_DISTANCE;
+	miny = snapy[i] - SNAP_DISTANCE;
+	maxy = snapy[i] + SNAP_DISTANCE;
+
+	if (*x > minx && *x < maxx && *y > miny && *y < maxy)
+	{
+	    *x = snapx[i];
+	    *y = snapy[i];
+
+	    DBG("snap: (x,y) = (%d,%d)\n", *x, *y);
+
+	    break;
+	}
+    }
+}
+
 static gint xfce_movehandler_motion (GtkWidget *widget, GdkEventMotion
*event)
 {
     XfceMovehandler *movehandler;
@@ -367,6 +436,8 @@
     new_x += movehandler->float_allocation.x + movehandler->deskoff_x;
     new_y += movehandler->float_allocation.y + movehandler->deskoff_y;
 
+    snap_to_corners(movehandler->gtk_window, &new_x, &new_y);
+    
     gdk_window_move (movehandler->float_window, new_x, new_y);
     gdk_window_raise (movehandler->float_window);
 



More information about the Xfce4-dev mailing list