comparison gtk/dw.c @ 1468:b2235efd9914

Rewrite of window position code for GTK2. DW_BORDER_WIDTH and DW_BORDER_HEIGHT are no longer needed, window decorations are now detected automatically. Initial implementation of dw_window_set_gravity().
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 19 Dec 2011 13:16:34 +0000
parents 77f530f95826
children b9efb744cfbd
comparison
equal deleted inserted replaced
1467:91bdd9840b9f 1468:b2235efd9914
140 #if GTK_MAJOR_VERSION < 2 140 #if GTK_MAJOR_VERSION < 2
141 static int _dw_file_active = 0; 141 static int _dw_file_active = 0;
142 #endif 142 #endif
143 static int _dw_ignore_click = 0, _dw_ignore_expand = 0, _dw_color_active = 0; 143 static int _dw_ignore_click = 0, _dw_ignore_expand = 0, _dw_color_active = 0;
144 static pthread_t _dw_thread = (pthread_t)-1; 144 static pthread_t _dw_thread = (pthread_t)-1;
145 /* Use default border size for the default enlightenment theme */
146 static int _dw_border_width = 12, _dw_border_height = 28;
147 145
148 #define DW_MUTEX_LOCK { if(pthread_self() != _dw_thread && !pthread_getspecific(_dw_mutex_key)) { gdk_threads_enter(); pthread_setspecific(_dw_mutex_key, (void *)1); _locked_by_me = TRUE; } } 146 #define DW_MUTEX_LOCK { if(pthread_self() != _dw_thread && !pthread_getspecific(_dw_mutex_key)) { gdk_threads_enter(); pthread_setspecific(_dw_mutex_key, (void *)1); _locked_by_me = TRUE; } }
149 #define DW_MUTEX_UNLOCK { if(pthread_self() != _dw_thread && _locked_by_me == TRUE) { gdk_threads_leave(); pthread_setspecific(_dw_mutex_key, NULL); _locked_by_me = FALSE; } } 147 #define DW_MUTEX_UNLOCK { if(pthread_self() != _dw_thread && _locked_by_me == TRUE) { gdk_threads_leave(); pthread_setspecific(_dw_mutex_key, NULL); _locked_by_me = FALSE; } }
150 148
151 #define DEFAULT_SIZE_WIDTH 12 149 #define DEFAULT_SIZE_WIDTH 12
344 GtkWidget *child; 342 GtkWidget *child;
345 GtkMdi *mdi; 343 GtkMdi *mdi;
346 344
347 gint x; 345 gint x;
348 gint y; 346 gint y;
349 gint width; 347 gint width;
350 gint height; 348 gint height;
351 349
352 GtkMdiChildState state; 350 GtkMdiChildState state;
353 }; 351 };
354 352
2086 * False if there is already a message loop running. 2084 * False if there is already a message loop running.
2087 */ 2085 */
2088 int dw_int_init(DWResources *res, int newthread, int *argc, char **argv[]) 2086 int dw_int_init(DWResources *res, int newthread, int *argc, char **argv[])
2089 { 2087 {
2090 int z; 2088 int z;
2091 char *tmp;
2092 char *fname; 2089 char *fname;
2093 static char * test_xpm[] = { 2090 static char * test_xpm[] = {
2094 "1 1 1 1", 2091 "1 1 1 1",
2095 " c None", 2092 " c None",
2096 " "}; 2093 " "};
2146 #endif 2143 #endif
2147 /* Add colors to the system colormap */ 2144 /* Add colors to the system colormap */
2148 _dw_cmap = gdk_colormap_get_system(); 2145 _dw_cmap = gdk_colormap_get_system();
2149 for(z=0;z<16;z++) 2146 for(z=0;z<16;z++)
2150 gdk_color_alloc(_dw_cmap, &_colors[z]); 2147 gdk_color_alloc(_dw_cmap, &_colors[z]);
2151
2152 tmp = getenv("DW_BORDER_WIDTH");
2153 if(tmp)
2154 _dw_border_width = atoi(tmp);
2155 tmp = getenv("DW_BORDER_HEIGHT");
2156 if(tmp)
2157 _dw_border_height = atoi(tmp);
2158 2148
2159 pthread_key_create(&_dw_fg_color_key, NULL); 2149 pthread_key_create(&_dw_fg_color_key, NULL);
2160 pthread_key_create(&_dw_bg_color_key, NULL); 2150 pthread_key_create(&_dw_bg_color_key, NULL);
2161 pthread_key_create(&_dw_mutex_key, NULL); 2151 pthread_key_create(&_dw_mutex_key, NULL);
2162 2152
2690 if (GTK_WIDGET(handle)->window) 2680 if (GTK_WIDGET(handle)->window)
2691 { 2681 {
2692 int width = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(handle), "_dw_width")); 2682 int width = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(handle), "_dw_width"));
2693 int height = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(handle), "_dw_height")); 2683 int height = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(handle), "_dw_height"));
2694 2684
2695 if (width && height) 2685 /* If we had a size request before shown */
2686 if (width || height)
2696 { 2687 {
2697 gtk_widget_set_usize(handle, width, height); 2688 /* Call the size function again now that we are realized */
2698 gtk_object_set_data(GTK_OBJECT(handle), "_dw_width", GINT_TO_POINTER(0)); 2689 dw_window_set_size(handle, width, height);
2699 gtk_object_set_data(GTK_OBJECT(handle), "_dw_height", GINT_TO_POINTER(0)); 2690 /* Clear out the data so we don't do it again */
2691 gtk_object_set_data(GTK_OBJECT(handle), "_dw_width", NULL);
2692 gtk_object_set_data(GTK_OBJECT(handle), "_dw_height", NULL);
2700 } 2693 }
2701 2694
2695 /* If we had a position request before shown */
2696 if (gtk_object_get_data(GTK_OBJECT(handle), "_dw_pos"))
2697 {
2698 int x = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(handle), "_dw_x"));
2699 int y = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(handle), "_dw_y"));
2700
2701 /* Call the position function again now that we are realized */
2702 dw_window_set_pos(handle, x, y);
2703 /* Clear out the data so we don't do it again */
2704 gtk_object_set_data(GTK_OBJECT(handle), "_dw_pos", NULL);
2705 }
2706
2707 /* Make sure it is the topmost window */
2702 gdk_window_raise(GTK_WIDGET(handle)->window); 2708 gdk_window_raise(GTK_WIDGET(handle)->window);
2703 gdk_flush(); 2709 gdk_flush();
2710 /* And visible */
2704 gdk_window_show(GTK_WIDGET(handle)->window); 2711 gdk_window_show(GTK_WIDGET(handle)->window);
2705 gdk_flush(); 2712 gdk_flush();
2706 } 2713 }
2707 defaultitem = (GtkWidget *)gtk_object_get_data(GTK_OBJECT(handle), "_dw_defaultitem"); 2714 defaultitem = (GtkWidget *)gtk_object_get_data(GTK_OBJECT(handle), "_dw_defaultitem");
2708 if (defaultitem) 2715 if (defaultitem)
3415 gtk_widget_show_all(table); 3422 gtk_widget_show_all(table);
3416 3423
3417 last_window = tmp = gtk_window_new(GTK_WINDOW_TOPLEVEL); 3424 last_window = tmp = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3418 3425
3419 gtk_window_set_title(GTK_WINDOW(tmp), title); 3426 gtk_window_set_title(GTK_WINDOW(tmp), title);
3420 if(!(flStyle & DW_FCF_SIZEBORDER)) 3427 gtk_window_set_resizable(GTK_WINDOW(tmp), (flStyle & DW_FCF_SIZEBORDER) ? TRUE : FALSE);
3421 gtk_window_set_policy(GTK_WINDOW(tmp), FALSE, FALSE, TRUE);
3422 3428
3423 gtk_widget_realize(tmp); 3429 gtk_widget_realize(tmp);
3424 3430
3425 if(flStyle & DW_FCF_TITLEBAR) 3431 if(flStyle & DW_FCF_TITLEBAR)
3426 flags |= GDK_DECOR_TITLE; 3432 flags |= GDK_DECOR_TITLE;
3457 gdk_window_set_decorations(tmp->window, flags); 3463 gdk_window_set_decorations(tmp->window, flags);
3458 3464
3459 if(hwndOwner) 3465 if(hwndOwner)
3460 gdk_window_reparent(GTK_WIDGET(tmp)->window, GTK_WIDGET(hwndOwner)->window, 0, 0); 3466 gdk_window_reparent(GTK_WIDGET(tmp)->window, GTK_WIDGET(hwndOwner)->window, 0, 0);
3461 3467
3462 if(flStyle & DW_FCF_SIZEBORDER)
3463 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_size", GINT_TO_POINTER(1));
3464
3465 gtk_table_attach(GTK_TABLE(table), box, 0, 1, 1, 2, GTK_EXPAND | GTK_FILL | GTK_SHRINK, GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 0); 3468 gtk_table_attach(GTK_TABLE(table), box, 0, 1, 1, 2, GTK_EXPAND | GTK_FILL | GTK_SHRINK, GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 0);
3466 gtk_container_add(GTK_CONTAINER(tmp), table); 3469 gtk_container_add(GTK_CONTAINER(tmp), table);
3467 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_boxhandle", (gpointer)box); 3470 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_boxhandle", (gpointer)box);
3468 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_table", (gpointer)table); 3471 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_table", (gpointer)table);
3469 } 3472 }
10110 * height: New height in pixels. 10113 * height: New height in pixels.
10111 */ 10114 */
10112 void dw_window_set_size(HWND handle, unsigned long width, unsigned long height) 10115 void dw_window_set_size(HWND handle, unsigned long width, unsigned long height)
10113 { 10116 {
10114 int _locked_by_me = FALSE; 10117 int _locked_by_me = FALSE;
10115 long default_width = width - _dw_border_width; 10118 int cx = 0, cy = 0;
10116 long default_height = height - _dw_border_height;
10117 10119
10118 if(!handle) 10120 if(!handle)
10119 return; 10121 return;
10120 10122
10121 DW_MUTEX_LOCK; 10123 DW_MUTEX_LOCK;
10122 if(GTK_IS_WINDOW(handle)) 10124 if(GTK_IS_WINDOW(handle))
10123 { 10125 {
10124 if ( width == 0 )
10125 default_width = -1;
10126 if ( height == 0 )
10127 default_height = -1;
10128 #ifdef GDK_WINDOWING_X11 10126 #ifdef GDK_WINDOWING_X11
10129 _size_allocate(GTK_WINDOW(handle)); 10127 _size_allocate(GTK_WINDOW(handle));
10130 #endif 10128 #endif
10131 if(handle->window) 10129 /* If the window is realized */
10132 gdk_window_resize(handle->window, default_width , default_height ); 10130 if(handle->window && gdk_window_is_visible(handle->window))
10133 gtk_window_set_default_size(GTK_WINDOW(handle), default_width , default_height ); 10131 {
10134 if(!gtk_object_get_data(GTK_OBJECT(handle), "_dw_size")) 10132 #if GTK_MAJOR_VERSION > 1
10135 { 10133 GdkRectangle frame;
10136 gtk_object_set_data(GTK_OBJECT(handle), "_dw_width", GINT_TO_POINTER(default_width) ); 10134 gint gwidth, gheight;
10137 gtk_object_set_data(GTK_OBJECT(handle), "_dw_height", GINT_TO_POINTER(default_height) ); 10135
10138 } 10136 /* Calculate the border size */
10137 gdk_window_get_frame_extents(handle->window, &frame);
10138 gdk_window_get_geometry(handle->window, NULL, NULL, &gwidth, &gheight, NULL);
10139
10140 cx = frame.width - gwidth;
10141 if(cx < 0)
10142 cx = 0;
10143 cy = frame.height - gheight;
10144 if(cy < 0)
10145 cy = 0;
10146 #endif
10147 /* Resize minus the border size */
10148 gdk_window_resize(handle->window, width - cx , height - cy );
10149 }
10150 if(!cx && !cy)
10151 {
10152 /* Save the size for when it is shown */
10153 gtk_object_set_data(GTK_OBJECT(handle), "_dw_width", GINT_TO_POINTER(width));
10154 gtk_object_set_data(GTK_OBJECT(handle), "_dw_height", GINT_TO_POINTER(height));
10155 }
10156 gtk_window_set_default_size(GTK_WINDOW(handle), width - cx, height - cy );
10139 } 10157 }
10140 else 10158 else
10141 gtk_widget_set_usize(handle, width, height); 10159 gtk_widget_set_usize(handle, width, height);
10142 DW_MUTEX_UNLOCK; 10160 DW_MUTEX_UNLOCK;
10143 } 10161 }
10204 DW_MUTEX_UNLOCK; 10222 DW_MUTEX_UNLOCK;
10205 return retval; 10223 return retval;
10206 } 10224 }
10207 10225
10208 /* 10226 /*
10227 * Sets the gravity of a given window (widget).
10228 * Gravity controls which corner of the screen and window the position is relative to.
10229 * Parameters:
10230 * handle: Window (widget) handle.
10231 * horz: DW_GRAV_LEFT (default), DW_GRAV_RIGHT or DW_GRAV_CENTER.
10232 * vert: DW_GRAV_TOP (default), DW_GRAV_BOTTOM or DW_GRAV_CENTER.
10233 */
10234 void API dw_window_set_gravity(HWND handle, int horz, int vert)
10235 {
10236 dw_window_set_data(handle, "_dw_grav_horz", DW_INT_TO_POINTER(horz));
10237 dw_window_set_data(handle, "_dw_grav_vert", DW_INT_TO_POINTER(vert));
10238 }
10239
10240 /*
10209 * Sets the position of a given window (widget). 10241 * Sets the position of a given window (widget).
10210 * Parameters: 10242 * Parameters:
10211 * handle: Window (widget) handle. 10243 * handle: Window (widget) handle.
10212 * x: X location from the bottom left. 10244 * x: X location from the bottom left.
10213 * y: Y location from the bottom left. 10245 * y: Y location from the bottom left.
10234 GdkWindow *window = NULL; 10266 GdkWindow *window = NULL;
10235 10267
10236 if(GTK_IS_WINDOW(handle)) 10268 if(GTK_IS_WINDOW(handle))
10237 { 10269 {
10238 #if GTK_MAJOR_VERSION > 1 10270 #if GTK_MAJOR_VERSION > 1
10239 gtk_window_move(GTK_WINDOW(handle), x, y); 10271 /* If the window is visible */
10272 if(handle->window && gdk_window_is_visible(handle->window))
10273 {
10274 int horz = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(handle), "_dw_grav_horz"));
10275 int vert = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(handle), "_dw_grav_vert"));
10276 int newx = x, newy = y;
10277
10278 if(horz || vert)
10279 {
10280 GdkRectangle frame;
10281
10282 /* Get the frame size */
10283 gdk_window_get_frame_extents(handle->window, &frame);
10284
10285 /* Handle horizontal center gravity */
10286 if((horz & 0xf) == DW_GRAV_CENTER)
10287 newx += ((gdk_screen_width() / 2) - (frame.width / 2));
10288 /* Handle right gravity */
10289 else if((horz & 0xf) == DW_GRAV_RIGHT)
10290 newx = gdk_screen_width() - frame.width - x;
10291 /* Handle vertical center gravity */
10292 if((vert & 0xf) == DW_GRAV_CENTER)
10293 newy += ((gdk_screen_height() / 2) - (frame.height / 2));
10294 else if((vert & 0xf) == DW_GRAV_BOTTOM)
10295 newy = gdk_screen_height() - frame.height - x;
10296 }
10297 gtk_window_move(GTK_WINDOW(handle), newx, newy);
10298 }
10299 else
10300 {
10301 /* Save the positions for when it is shown */
10302 gtk_object_set_data(GTK_OBJECT(handle), "_dw_x", GINT_TO_POINTER(x));
10303 gtk_object_set_data(GTK_OBJECT(handle), "_dw_y", GINT_TO_POINTER(y));
10304 gtk_object_set_data(GTK_OBJECT(handle), "_dw_pos", GINT_TO_POINTER(1));
10305 }
10240 #else 10306 #else
10241 gtk_widget_set_uposition(handle, x, y); 10307 gtk_widget_set_uposition(handle, x, y);
10242 #endif 10308 #endif
10243 } 10309 }
10244 else if((window = gtk_widget_get_window(handle))) 10310 else if((window = gtk_widget_get_window(handle)))
10256 * width: Width of the widget. 10322 * width: Width of the widget.
10257 * height: Height of the widget. 10323 * height: Height of the widget.
10258 */ 10324 */
10259 void dw_window_set_pos_size(HWND handle, long x, long y, unsigned long width, unsigned long height) 10325 void dw_window_set_pos_size(HWND handle, long x, long y, unsigned long width, unsigned long height)
10260 { 10326 {
10261 int _locked_by_me = FALSE; 10327 dw_window_set_size(handle, width, height);
10262 #if GTK_MAJOR_VERSION > 1 10328 dw_window_set_pos(handle, x, y);
10263 GtkWidget *mdi;
10264 #endif
10265
10266 if(!handle)
10267 return;
10268
10269 DW_MUTEX_LOCK;
10270 #if GTK_MAJOR_VERSION > 1
10271 if((mdi = (GtkWidget *)gtk_object_get_data(GTK_OBJECT(handle), "_dw_mdi")) && GTK_IS_MDI(mdi))
10272 {
10273 gtk_mdi_move(GTK_MDI(mdi), handle, x, y);
10274 }
10275 else
10276 #endif
10277 {
10278 if(GTK_IS_WINDOW(handle))
10279 {
10280 dw_window_set_size(handle, width, height);
10281 #if GTK_MAJOR_VERSION > 1
10282 gtk_window_move(GTK_WINDOW(handle), x, y);
10283 #else
10284 gtk_widget_set_uposition(handle, x, y);
10285 #endif
10286 }
10287 else if(handle->window)
10288 {
10289 gdk_window_resize(handle->window, width, height);
10290 gdk_window_move(handle->window, x, y);
10291 }
10292 }
10293 DW_MUTEX_UNLOCK;
10294 } 10329 }
10295 10330
10296 /* 10331 /*
10297 * Gets the position and size of a given window (widget). 10332 * Gets the position and size of a given window (widget).
10298 * Parameters: 10333 * Parameters:
10303 * height: Height of the widget. 10338 * height: Height of the widget.
10304 */ 10339 */
10305 void dw_window_get_pos_size(HWND handle, long *x, long *y, ULONG *width, ULONG *height) 10340 void dw_window_get_pos_size(HWND handle, long *x, long *y, ULONG *width, ULONG *height)
10306 { 10341 {
10307 int _locked_by_me = FALSE; 10342 int _locked_by_me = FALSE;
10308 gint gx, gy, gwidth, gheight, gdepth; 10343 gint gx = 0, gy = 0, gwidth = 0, gheight = 0;
10309 #if GTK_MAJOR_VERSION > 1 10344 #if GTK_MAJOR_VERSION > 1
10310 GtkWidget *mdi; 10345 GtkWidget *mdi;
10311 #endif 10346 #endif
10312 10347
10313 DW_MUTEX_LOCK; 10348 DW_MUTEX_LOCK;
10314 #if GTK_MAJOR_VERSION > 1 10349 #if GTK_MAJOR_VERSION > 1
10350 /* If it is an MDI window query what we can */
10315 if((mdi = (GtkWidget *)gtk_object_get_data(GTK_OBJECT(handle), "_dw_mdi")) && GTK_IS_MDI(mdi)) 10351 if((mdi = (GtkWidget *)gtk_object_get_data(GTK_OBJECT(handle), "_dw_mdi")) && GTK_IS_MDI(mdi))
10316 { 10352 {
10317 gint myx=0, myy=0; 10353 gtk_mdi_get_pos(GTK_MDI(mdi), handle, &gx, &gy);
10318
10319 gtk_mdi_get_pos(GTK_MDI(mdi), handle, &myx, &myy);
10320 *x = myx;
10321 *y = myy;
10322 } 10354 }
10323 else 10355 else
10324 #endif 10356 #endif
10325 { 10357 {
10358 /* Can only query if the window is realized */
10326 if(handle && handle->window) 10359 if(handle && handle->window)
10327 { 10360 {
10328 10361 /* If it is a toplevel window */
10329 gdk_window_get_geometry(handle->window, &gx, &gy, &gwidth, &gheight, &gdepth);
10330 gdk_window_get_root_origin(handle->window, &gx, &gy);
10331 if(x)
10332 *x = gx;
10333 if(y)
10334 *y = gy;
10335 if(GTK_IS_WINDOW(handle)) 10362 if(GTK_IS_WINDOW(handle))
10336 { 10363 {
10337 if(width) 10364 if(handle->window && gdk_window_is_visible(handle->window))
10338 *width = gwidth + _dw_border_width; 10365 {
10339 if(height) 10366 GdkRectangle frame;
10340 *height = gheight + _dw_border_height; 10367
10368 /* Calculate the border rectangle */
10369 gdk_window_get_frame_extents(handle->window, &frame);
10370 gx = frame.x;
10371 gy = frame.y;
10372 gwidth = frame.width;
10373 gheight = frame.height;
10374 }
10341 } 10375 }
10342 else 10376 else
10343 { 10377 {
10344 if(width) 10378 /* Get an individual widget coordinates */
10345 *width = gwidth; 10379 gdk_window_get_geometry(handle->window, &gx, &gy, &gwidth, &gheight, NULL);
10346 if(height) 10380 gdk_window_get_root_origin(handle->window, &gx, &gy);
10347 *height = gheight;
10348 } 10381 }
10349 } 10382 }
10350 } 10383 }
10384 /* Fill in the requested fields */
10385 if(x)
10386 *x = gx;
10387 if(y)
10388 *y = gy;
10389 if(width)
10390 *width = gwidth;
10391 if(height)
10392 *height = gheight;
10351 DW_MUTEX_UNLOCK; 10393 DW_MUTEX_UNLOCK;
10352 } 10394 }
10353 10395
10354 /* 10396 /*
10355 * Sets the style of a given window (widget). 10397 * Sets the style of a given window (widget).