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

Jasper Huijsmans jasper at moongroup.com
Fri Mar 7 17:32:00 CET 2003


On Fri, 7 Mar 2003 16:58:11 +0100
Jasper Huijsmans <jasper at moongroup.com> wrote:
...
> For starters, it's a bit brain-dead to calculate the snap coordinates
> all the time, when it's only necessary to do it once on button-press
> ... 
> I should probably add the snap coordinates to the widget itself.
> 

Updated patch:

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 16:18:35 -0000
@@ -289,8 +289,11 @@
 static gint xfce_movehandler_button_changed (GtkWidget *widget,
GdkEventButton *event) {
     XfceMovehandler *movehandler;
+    GtkWidget *win;
     gboolean event_handled;
     GdkCursor *fleur;
+    static int screen_width = 0;
+    static int screen_height = 0;
 
     g_return_val_if_fail(widget != NULL, FALSE);
     g_return_val_if_fail(XFCE_IS_MOVEHANDLER(widget), FALSE);
@@ -311,6 +314,12 @@
             gint desk_x, desk_y;
             gint root_x, root_y;
 
+	    if (screen_width == 0)
+	    {
+		screen_width = gdk_screen_width();
+		screen_height = gdk_screen_height();
+	    }
+
             gdk_window_get_root_origin (movehandler->float_window,
&desk_x, &desk_y);             gdk_window_get_origin
(movehandler->float_window, &root_x, &root_y); 
@@ -320,6 +329,41 @@
             movehandler->deskoff_x = desk_x - root_x;
             movehandler->deskoff_y = desk_y - root_y;
 
+	    /* Snap coordinates: 4 corners + 4 edge centers */
+	    win = movehandler->gtk_window;
+	    
+	    /* top left */
+	    movehandler->snapx[0] = 0;
+	    movehandler->snapy[0] = 0;
+
+	    /* top right */
+	    movehandler->snapx[1] = screen_width - win->allocation.width;
+	    movehandler->snapy[1] = 0;
+	    
+	    /* bottom left */
+	    movehandler->snapx[2] = 0;
+	    movehandler->snapy[2] = screen_height - win->allocation.height;
+
+	    /* bottom right */
+	    movehandler->snapx[3] = movehandler->snapx[1];
+	    movehandler->snapy[3] = movehandler->snapy[2];
+	    
+	    /* top center */
+	    movehandler->snapx[4] = screen_width / 2 - win->allocation.width /
2;+	    movehandler->snapy[4] = 0;
+
+	    /* bottom center */
+	    movehandler->snapx[5] = movehandler->snapx[4];
+	    movehandler->snapy[5] = movehandler->snapy[2];
+
+	    /* left center */
+	    movehandler->snapx[6] = 0;
+	    movehandler->snapy[6] = screen_height / 2 - win->allocation.height
/ 2;+
+	    /* right center */
+	    movehandler->snapx[7] = movehandler->snapx[1];
+	    movehandler->snapy[7] = movehandler->snapy[6];
+    
             movehandler->in_drag = TRUE;
             fleur = gdk_cursor_new (GDK_FLEUR);
             if (gdk_pointer_grab (widget->window, FALSE,
(GDK_BUTTON1_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
GDK_BUTTON_RELEASE_MASK), NULL, fleur, GDK_CURRENT_TIME) != 0)@@ -344,6
+388,38 @@     return event_handled;
 }
 
+#define SNAP_DISTANCE 20
+
+static void snap_to_corners(XfceMovehandler *movehandler, int *x, int
*y)+{
+    int i;
+    int *snapx, *snapy;
+
+    snapx = movehandler->snapx;
+    snapy = movehandler->snapy;
+    
+    /* 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];
+
+/*	    g_print("snap: (x,y) = (%d,%d)\n", *x, *y);*/
+
+	    break;
+	}
+    }
+}
+
 static gint xfce_movehandler_motion (GtkWidget *widget, GdkEventMotion
*event) {
     XfceMovehandler *movehandler;
@@ -367,6 +443,8 @@
     new_x += movehandler->float_allocation.x + movehandler->deskoff_x;
     new_y += movehandler->float_allocation.y + movehandler->deskoff_y;
 
+    snap_to_corners(movehandler, &new_x, &new_y);
+    
     gdk_window_move (movehandler->float_window, new_x, new_y);
     gdk_window_raise (movehandler->float_window);
 
Index: xfce_movehandler.h
===================================================================
RCS file:
/cvsroot/xfce/xfce-devel/libxfcegui4/libxfcegui4/xfce_movehandler.h,v
retrieving revision 1.4 diff -u -r1.4 xfce_movehandler.h
--- xfce_movehandler.h	8 Oct 2002 19:56:37 -0000	1.4
+++ xfce_movehandler.h	7 Mar 2003 16:18:36 -0000
@@ -51,6 +51,8 @@
         GdkBitmap *dark_bmap;
         GdkBitmap *mid_bmap;
         GdkBitmap *light_bmap;
+
+	gint snapx[8], snapy[8];
     };
 
     struct _XfceMovehandlerClass




More information about the Xfce4-dev mailing list