1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Authors: Alexander Larsson <alexl@redhat.com>
24 #include "gtkprivate.h"
25 #include "gtkwindow.h"
29 #ifdef GDK_WINDOWING_FB
30 #define DECORATE_WINDOWS
33 #ifdef DECORATE_WINDOWS
34 #include "linux-fb/gdkfb.h"
38 GTK_WINDOW_REGION_TITLE,
39 GTK_WINDOW_REGION_MAXIMIZE,
40 GTK_WINDOW_REGION_CLOSE,
41 GTK_WINDOW_REGION_BR_RESIZE
42 } GtkWindowRegionType;
44 typedef struct _GtkWindowRegion GtkWindowRegion;
45 typedef struct _GtkWindowDecoration GtkWindowDecoration;
47 struct _GtkWindowRegion
50 GtkWindowRegionType type;
64 } GtkWindowResizeType;
66 struct _GtkWindowDecoration
69 GtkWindowRegion *regions;
74 PangoLayout *title_layout;
76 GtkWindowResizeType resize;
80 gboolean maximizing : 1;
81 gboolean maximized : 1;
82 gboolean maximizable : 1;
83 gboolean decorated : 1;
84 gboolean real_inner_move : 1;
88 #define DECORATION_BORDER_TOP 15
89 #define DECORATION_BORDER_LEFT 3
90 #define DECORATION_BORDER_RIGHT 3
91 #define DECORATION_BORDER_BOTTOM 3
92 #define DECORATION_BORDER_TOT_X (DECORATION_BORDER_LEFT + DECORATION_BORDER_RIGHT)
93 #define DECORATION_BORDER_TOT_Y (DECORATION_BORDER_TOP + DECORATION_BORDER_BOTTOM)
94 #define DECORATION_BUTTON_SIZE 9
95 #define DECORATION_BUTTON_Y_OFFSET 2
96 #define DECORATION_TITLE_FONT "Sans 9"
98 static void gtk_decorated_window_recalculate_regions (GtkWindow *window);
99 static GtkWindowRegionType gtk_decorated_window_region_type (GtkWindow *window,
102 static gint gtk_decorated_window_frame_event (GtkWindow *window,
104 static gint gtk_decorated_window_button_press (GtkWidget *widget,
105 GdkEventButton *event);
106 static gint gtk_decorated_window_button_release (GtkWidget *widget,
107 GdkEventButton *event);
108 static gint gtk_decorated_window_motion_notify (GtkWidget *widget,
109 GdkEventMotion *event);
110 static gint gtk_decorated_window_window_state (GtkWidget *widget,
111 GdkEventWindowState *event);
112 static void gtk_decorated_window_paint (GtkWidget *widget,
114 static gint gtk_decorated_window_focus_change (GtkWidget *widget,
115 GdkEventFocus *event);
116 static void gtk_decorated_window_realize (GtkWindow *window);
117 static void gtk_decorated_window_unrealize (GtkWindow *window);
120 gtk_decoration_free (GtkWindowDecoration *deco)
122 g_free (deco->regions);
123 deco->regions = NULL;
130 gtk_decorated_window_init (GtkWindow *window)
132 GtkWindowDecoration *deco;
134 deco = g_new (GtkWindowDecoration, 1);
137 deco->regions = NULL;
138 deco->title_layout = NULL;
139 deco->resize = RESIZE_NONE;
140 deco->moving = FALSE;
141 deco->decorated = TRUE;
142 deco->closing = FALSE;
143 deco->maximizing = FALSE;
144 deco->maximized = FALSE;
145 deco->maximizable = FALSE;
146 deco->real_inner_move = FALSE;
148 g_object_set_data_full (G_OBJECT (window), "gtk-window-decoration", deco,
149 (GDestroyNotify) gtk_decoration_free);
151 gtk_window_set_has_frame (window, TRUE);
153 g_signal_connect (window,
155 G_CALLBACK (gtk_decorated_window_frame_event),
157 g_signal_connect (window,
159 G_CALLBACK (gtk_decorated_window_focus_change),
161 g_signal_connect (window,
163 G_CALLBACK (gtk_decorated_window_focus_change),
165 g_signal_connect (window,
167 G_CALLBACK (gtk_decorated_window_realize),
169 g_signal_connect (window,
171 G_CALLBACK (gtk_decorated_window_unrealize),
175 static inline GtkWindowDecoration *
176 get_decoration (GtkWindow *window)
178 return (GtkWindowDecoration *)g_object_get_data (G_OBJECT (window), "gtk-window-decoration");
182 gtk_decorated_window_set_title (GtkWindow *window,
185 GtkWindowDecoration *deco = get_decoration (window);
187 if (deco->title_layout)
188 pango_layout_set_text (deco->title_layout, title, -1);
192 gtk_decorated_window_calculate_frame_size (GtkWindow *window)
194 GdkWMDecoration decorations;
195 GtkWindowDecoration *deco = get_decoration (window);
197 if (gdk_window_get_decorations (GTK_WIDGET (window)->window,
200 if ((decorations & GDK_DECOR_BORDER) &&
201 (decorations & GDK_DECOR_TITLE))
203 deco->decorated = TRUE;
204 if ((decorations & GDK_DECOR_MAXIMIZE) &&
205 (gtk_window_get_type_hint (window) == GDK_WINDOW_TYPE_HINT_NORMAL))
206 deco->maximizable = TRUE;
209 deco->decorated = FALSE;
213 deco->decorated = (window->type != GTK_WINDOW_POPUP);
214 deco->maximizable = (gtk_window_get_type_hint (window) == GDK_WINDOW_TYPE_HINT_NORMAL);
218 gtk_window_set_frame_dimensions (window,
219 DECORATION_BORDER_LEFT,
220 DECORATION_BORDER_TOP,
221 DECORATION_BORDER_RIGHT,
222 DECORATION_BORDER_BOTTOM);
224 gtk_window_set_frame_dimensions (window, 0, 0, 0, 0);
226 gtk_decorated_window_recalculate_regions (window);
230 gtk_decorated_window_inner_change (GdkWindow *win,
232 gint width, gint height,
235 GtkWindow *window = (GtkWindow *)user_data;
236 GtkWidget *widget = GTK_WIDGET (window);
237 GtkWindowDecoration *deco = get_decoration (window);
239 if (deco->real_inner_move)
241 deco->real_inner_move = FALSE;
245 deco->real_inner_move = TRUE;
246 gdk_window_move_resize (widget->window,
247 window->frame_left, window->frame_top,
250 gdk_window_move_resize (window->frame,
251 x - window->frame_left, y - window->frame_top,
252 width + window->frame_left + window->frame_right,
253 height + window->frame_top + window->frame_bottom);
258 gtk_decorated_window_inner_get_pos (GdkWindow *win,
262 GtkWindow *window = (GtkWindow *)user_data;
264 gdk_window_get_position (window->frame, x, y);
266 *x += window->frame_left;
267 *y += window->frame_top;
271 gtk_decorated_window_realize (GtkWindow *window)
273 GtkWindowDecoration *deco = get_decoration (window);
274 GtkWidget *widget = GTK_WIDGET (window);
275 PangoFontDescription *font_desc;
277 deco->title_layout = gtk_widget_create_pango_layout (widget,
278 (window->title)?window->title:"");
280 font_desc = pango_font_description_from_string(DECORATION_TITLE_FONT);
281 pango_layout_set_font_description (deco->title_layout, font_desc);
282 pango_font_description_free (font_desc);
284 gdk_fb_window_set_child_handler (window->frame,
285 gtk_decorated_window_inner_change,
286 gtk_decorated_window_inner_get_pos,
289 /* This is a huge hack to make frames have the same shape as
290 the window they wrap */
291 gdk_window_shape_combine_mask (window->frame, GDK_FB_USE_CHILD_SHAPE, 0, 0);
296 gtk_decorated_window_unrealize (GtkWindow *window)
298 GtkWindowDecoration *deco = get_decoration (window);
300 if (deco->title_layout)
302 g_object_unref (deco->title_layout);
303 deco->title_layout = NULL;
308 gtk_decorated_window_frame_event (GtkWindow *window, GdkEvent *event)
310 GtkWindowDecoration *deco = get_decoration (window);
311 GtkWidget *widget = GTK_WIDGET (window);
312 GdkEventExpose *expose_event;
317 expose_event = (GdkEventExpose *)event;
319 gtk_decorated_window_paint (widget, &expose_event->area);
323 gtk_decorated_window_recalculate_regions (window);
325 case GDK_MOTION_NOTIFY:
326 return gtk_decorated_window_motion_notify (widget, (GdkEventMotion *)event);
328 case GDK_BUTTON_PRESS:
329 return gtk_decorated_window_button_press (widget, (GdkEventButton *)event);
331 case GDK_BUTTON_RELEASE:
332 return gtk_decorated_window_button_release (widget, (GdkEventButton *)event);
333 case GDK_WINDOW_STATE:
334 return gtk_decorated_window_window_state (widget, (GdkEventWindowState *)event);
342 gtk_decorated_window_focus_change (GtkWidget *widget,
343 GdkEventFocus *event)
345 GtkWindow *window = GTK_WINDOW(widget);
346 GtkWindowDecoration *deco = get_decoration (window);
347 deco->focused = event->in;
348 gdk_window_invalidate_rect (window->frame, NULL, FALSE);
353 gtk_decorated_window_motion_notify (GtkWidget *widget,
354 GdkEventMotion *event)
357 GtkWindowDecoration *deco;
358 GdkModifierType mask;
361 gint win_x, win_y, win_w, win_h;
363 window = GTK_WINDOW (widget);
364 deco = get_decoration (window);
366 if (!deco->decorated)
369 win = widget->window;
370 gdk_window_get_pointer (window->frame, &x, &y, &mask);
372 gdk_window_get_position (window->frame, &win_x, &win_y);
373 win_x += DECORATION_BORDER_LEFT;
374 win_y += DECORATION_BORDER_TOP;
376 gdk_window_get_geometry (win, NULL, NULL, &win_w, &win_h, NULL);
381 dx = x - deco->last_x;
382 dy = y - deco->last_y;
384 _gtk_window_reposition (window, win_x + dx, win_y + dy);
387 if (deco->resize != RESIZE_NONE)
394 switch(deco->resize) {
395 case RESIZE_BOTTOM_RIGHT:
396 w = x - DECORATION_BORDER_TOT_X;
397 h = y - DECORATION_BORDER_TOT_Y;
400 w = x - DECORATION_BORDER_TOT_X;
403 h = y - DECORATION_BORDER_TOT_Y;
405 case RESIZE_TOP_LEFT:
407 case RESIZE_TOP_RIGHT:
408 case RESIZE_BOTTOM_LEFT:
411 g_warning ("Resize mode %d not handled yet.\n", deco->resize);
415 if ((w > 0) && (h > 0))
417 _gtk_window_constrain_size (window, w,h, &w, &h);
419 if ((w != win_w) || (h != win_h))
420 gdk_window_resize (widget->window, w, h);
427 static GtkWindowRegionType
428 gtk_decorated_window_region_type (GtkWindow *window, gint x, gint y)
430 GtkWindowDecoration *deco = get_decoration (window);
433 for (i=0;i<deco->n_regions;i++)
435 if ((x > deco->regions[i].rect.x) &&
436 (x - deco->regions[i].rect.x < deco->regions[i].rect.width) &&
437 (y > deco->regions[i].rect.y) &&
438 (y - deco->regions[i].rect.y < deco->regions[i].rect.height))
439 return deco->regions[i].type;
445 gtk_decorated_window_button_press (GtkWidget *widget,
446 GdkEventButton *event)
449 GtkWindowRegionType type;
450 GtkWindowDecoration *deco;
453 window = GTK_WINDOW (widget);
454 deco = get_decoration (window);
456 if (!deco->decorated)
462 type = gtk_decorated_window_region_type (window, x, y);
466 case GTK_WINDOW_REGION_TITLE:
467 if (!deco->maximized && event->state & GDK_BUTTON1_MASK)
474 case GTK_WINDOW_REGION_MAXIMIZE:
475 if (event->state & GDK_BUTTON1_MASK)
476 deco->maximizing = TRUE;
478 case GTK_WINDOW_REGION_CLOSE:
479 if (event->state & GDK_BUTTON1_MASK)
480 deco->closing = TRUE;
482 case GTK_WINDOW_REGION_BR_RESIZE:
483 if (!deco->maximized)
485 if (event->state & GDK_BUTTON1_MASK)
486 deco->resize = RESIZE_BOTTOM_RIGHT;
499 gtk_decorated_window_button_release (GtkWidget *widget,
500 GdkEventButton *event)
503 GtkWindowRegionType type;
504 GtkWindowDecoration *deco;
506 window = GTK_WINDOW (widget);
507 deco = get_decoration (window);
511 type = gtk_decorated_window_region_type (window, event->x, event->y);
512 if (type == GTK_WINDOW_REGION_CLOSE)
514 GdkEvent *event = gdk_event_new (GDK_DELETE);
516 event->any.type = GDK_DELETE;
517 event->any.window = g_object_ref (widget->window);
518 event->any.send_event = TRUE;
520 gtk_main_do_event (event);
521 gdk_event_free (event);
524 else if (deco->maximizing)
526 type = gtk_decorated_window_region_type (window, event->x, event->y);
527 if (type == GTK_WINDOW_REGION_MAXIMIZE)
530 gtk_window_unmaximize (window);
532 gtk_window_maximize (window);
536 deco->closing = FALSE;
537 deco->maximizing = FALSE;
538 deco->moving = FALSE;
539 deco->resize = RESIZE_NONE;
544 gtk_decorated_window_window_state (GtkWidget *widget,
545 GdkEventWindowState *event)
548 GtkWindowDecoration *deco;
549 GdkWindowObject *priv;
551 window = GTK_WINDOW (widget);
552 deco = get_decoration (window);
553 priv = GDK_WINDOW_OBJECT (window->frame);
555 if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
557 if (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED)
560 gdk_window_get_geometry (widget->window, NULL, NULL,
561 &deco->last_w, &deco->last_h, NULL);
562 gdk_window_get_origin (widget->window, &deco->last_x, &deco->last_y);
563 w = gdk_screen_get_width(gdk_screen_get_default()) - DECORATION_BORDER_TOT_X;
564 h = gdk_screen_get_height(gdk_screen_get_default()) - DECORATION_BORDER_TOT_Y;
565 _gtk_window_constrain_size (window, w, h, &w, &h);
566 if (w != deco->last_w || h != deco->last_h)
568 _gtk_window_reposition (window, DECORATION_BORDER_LEFT, DECORATION_BORDER_TOP);
569 gdk_window_resize (widget->window, w, h);
570 deco->maximized = TRUE;
575 _gtk_window_reposition (window, deco->last_x, deco->last_y);
576 _gtk_window_constrain_size (window, deco->last_w, deco->last_h,
577 &deco->last_w, &deco->last_h);
578 gdk_window_resize (widget->window, deco->last_w, deco->last_h);
579 deco->maximized = FALSE;
586 gtk_decorated_window_paint (GtkWidget *widget,
589 GtkWindow *window = GTK_WINDOW (widget);
590 GtkWindowDecoration *deco = get_decoration (window);
592 GtkStateType border_state;
599 frame = window->frame;
600 gdk_drawable_get_size (frame, &width, &height);
603 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
604 GTK_SHADOW_NONE, area, widget, "base",
606 width, DECORATION_BORDER_TOP);
608 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
609 GTK_SHADOW_NONE, area, widget, "base",
610 0, height - DECORATION_BORDER_BOTTOM,
611 width, DECORATION_BORDER_BOTTOM);
613 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
614 GTK_SHADOW_NONE, area, widget, "base",
615 0, DECORATION_BORDER_TOP,
616 DECORATION_BORDER_LEFT, height - DECORATION_BORDER_TOT_Y);
618 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
619 GTK_SHADOW_NONE, area, widget, "base",
620 width - DECORATION_BORDER_RIGHT, DECORATION_BORDER_TOP,
621 DECORATION_BORDER_RIGHT, height - DECORATION_BORDER_TOT_Y);
625 border_state = GTK_STATE_SELECTED;
627 border_state = GTK_STATE_PRELIGHT;
629 gtk_paint_box (widget->style, frame, border_state,
630 GTK_SHADOW_OUT, area, widget, "base",
631 0, 0, width, height);
633 gtk_paint_box (widget->style, frame, border_state,
634 GTK_SHADOW_IN, area, widget, "base",
635 DECORATION_BORDER_LEFT - 2, DECORATION_BORDER_TOP - 2,
636 width - (DECORATION_BORDER_LEFT + DECORATION_BORDER_RIGHT) + 3,
637 height - (DECORATION_BORDER_TOP + DECORATION_BORDER_BOTTOM) + 3);
639 if (deco->maximizable)
641 /* Maximize button: */
643 x1 = width - (DECORATION_BORDER_LEFT * 2) - (DECORATION_BUTTON_SIZE * 2);
644 y1 = DECORATION_BUTTON_Y_OFFSET;
645 x2 = x1 + DECORATION_BUTTON_SIZE;
646 y2 = y1 + DECORATION_BUTTON_SIZE;
649 gdk_gc_set_clip_rectangle (widget->style->bg_gc[widget->state], area);
651 gdk_draw_rectangle (frame, widget->style->bg_gc[widget->state], TRUE,
652 x1, y1, x2 - x1, y2 - y1);
654 gdk_draw_line (frame, widget->style->black_gc, x1 + 1, y1 + 1, x2 - 2, y1 + 1);
656 gdk_draw_rectangle (frame, widget->style->black_gc, FALSE,
658 DECORATION_BUTTON_SIZE - 3, DECORATION_BUTTON_SIZE - 4);
661 gdk_gc_set_clip_rectangle (widget->style->black_gc, NULL);
666 x1 = width - DECORATION_BORDER_LEFT - DECORATION_BUTTON_SIZE;
667 y1 = DECORATION_BUTTON_Y_OFFSET;
668 x2 = width - DECORATION_BORDER_LEFT;
669 y2 = DECORATION_BUTTON_Y_OFFSET + DECORATION_BUTTON_SIZE;
672 gdk_gc_set_clip_rectangle (widget->style->bg_gc[widget->state], area);
674 gdk_draw_rectangle (frame, widget->style->bg_gc[widget->state], TRUE,
675 x1, y1, x2 - x1, y2 - y1);
678 gdk_gc_set_clip_rectangle (widget->style->bg_gc[widget->state], NULL);
681 gdk_gc_set_clip_rectangle (widget->style->black_gc, area);
683 gdk_draw_line (frame, widget->style->black_gc, x1, y1, x2-1, y2-1);
685 gdk_draw_line (frame, widget->style->black_gc, x1, y2-1, x2-1, y1);
688 gdk_gc_set_clip_rectangle (widget->style->black_gc, NULL);
693 if (deco->title_layout)
696 gdk_gc_set_clip_rectangle (widget->style->fg_gc [border_state], area);
698 gdk_draw_layout (frame,
699 widget->style->fg_gc [border_state],
700 DECORATION_BORDER_LEFT, 1,
703 gdk_gc_set_clip_rectangle (widget->style->fg_gc [border_state], NULL);
711 gtk_decorated_window_recalculate_regions (GtkWindow *window)
715 GtkWindowRegion *region;
716 GtkWindowDecoration *deco = get_decoration (window);
720 if (!deco->decorated)
723 n_regions += 2; /* close, Title */
724 if (deco->maximizable)
726 if (window->allow_shrink || window->allow_grow)
729 if (deco->n_regions != n_regions)
731 g_free (deco->regions);
732 deco->regions = g_new (GtkWindowRegion, n_regions);
733 deco->n_regions = n_regions;
736 width = GTK_WIDGET (window)->allocation.width + DECORATION_BORDER_TOT_X;
737 height = GTK_WIDGET (window)->allocation.height + DECORATION_BORDER_TOT_Y;
739 region = deco->regions;
741 /* Maximize button */
742 if (deco->maximizable)
744 region->rect.x = width - (DECORATION_BORDER_LEFT * 2) - (DECORATION_BUTTON_SIZE * 2);
745 region->rect.y = DECORATION_BUTTON_Y_OFFSET;
746 region->rect.width = DECORATION_BUTTON_SIZE;
747 region->rect.height = DECORATION_BUTTON_SIZE;
748 region->type = GTK_WINDOW_REGION_MAXIMIZE;
753 region->rect.x = width - DECORATION_BORDER_LEFT - DECORATION_BUTTON_SIZE;
754 region->rect.y = DECORATION_BUTTON_Y_OFFSET;
755 region->rect.width = DECORATION_BUTTON_SIZE;
756 region->rect.height = DECORATION_BUTTON_SIZE;
757 region->type = GTK_WINDOW_REGION_CLOSE;
763 region->rect.width = width;
764 region->rect.height = DECORATION_BORDER_TOP;
765 region->type = GTK_WINDOW_REGION_TITLE;
768 if (window->allow_shrink || window->allow_grow)
770 region->rect.x = width - (DECORATION_BORDER_RIGHT + 10);
771 region->rect.y = height - DECORATION_BORDER_BOTTOM;
772 region->rect.width = DECORATION_BORDER_RIGHT + 10;
773 region->rect.height = DECORATION_BORDER_BOTTOM;
774 region->type = GTK_WINDOW_REGION_BR_RESIZE;
777 region->rect.x = width - DECORATION_BORDER_RIGHT;
778 region->rect.y = height - (DECORATION_BORDER_BOTTOM + 10);
779 region->rect.width = DECORATION_BORDER_RIGHT;
780 region->rect.height = DECORATION_BORDER_BOTTOM + 10;
781 region->type = GTK_WINDOW_REGION_BR_RESIZE;
787 gtk_decorated_window_move_resize_window (GtkWindow *window,
793 GtkWidget *widget = GTK_WIDGET (window);
794 GtkWindowDecoration *deco = get_decoration (window);
796 deco->real_inner_move = TRUE;
797 gdk_window_move_resize (widget->window,
798 x, y, width, height);
803 gtk_decorated_window_init (GtkWindow *window)
808 gtk_decorated_window_calculate_frame_size (GtkWindow *window)
813 gtk_decorated_window_set_title (GtkWindow *window,
819 gtk_decorated_window_move_resize_window (GtkWindow *window,
825 gdk_window_move_resize (GTK_WIDGET (window)->window,
826 x, y, width, height);