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>
25 #include "gtkprivate.h"
26 #include "gtkwindow.h"
28 #include "gtkwindow-decorate.h"
32 #ifdef DECORATE_WINDOWS
36 GTK_WINDOW_REGION_TITLE,
37 GTK_WINDOW_REGION_MAXIMIZE,
38 GTK_WINDOW_REGION_CLOSE,
39 GTK_WINDOW_REGION_BR_RESIZE
40 } GtkWindowRegionType;
42 typedef struct _GtkWindowRegion GtkWindowRegion;
43 typedef struct _GtkWindowDecoration GtkWindowDecoration;
45 struct _GtkWindowRegion
48 GtkWindowRegionType type;
62 } GtkWindowResizeType;
64 struct _GtkWindowDecoration
67 GtkWindowRegion *regions;
72 PangoLayout *title_layout;
74 GtkWindowResizeType resize;
80 guint maximizable : 1;
82 guint real_inner_move : 1;
86 #define DECORATION_BORDER_TOP 15
87 #define DECORATION_BORDER_LEFT 3
88 #define DECORATION_BORDER_RIGHT 3
89 #define DECORATION_BORDER_BOTTOM 3
90 #define DECORATION_BORDER_TOT_X (DECORATION_BORDER_LEFT + DECORATION_BORDER_RIGHT)
91 #define DECORATION_BORDER_TOT_Y (DECORATION_BORDER_TOP + DECORATION_BORDER_BOTTOM)
92 #define DECORATION_BUTTON_SIZE 9
93 #define DECORATION_BUTTON_Y_OFFSET 2
94 #define DECORATION_TITLE_FONT "Sans 9"
96 static void gtk_decorated_window_recalculate_regions (GtkWindow *window);
97 static GtkWindowRegionType gtk_decorated_window_region_type (GtkWindow *window,
100 static gint gtk_decorated_window_frame_event (GtkWindow *window,
102 static gint gtk_decorated_window_button_press (GtkWidget *widget,
103 GdkEventButton *event);
104 static gint gtk_decorated_window_button_release (GtkWidget *widget,
105 GdkEventButton *event);
106 static gint gtk_decorated_window_motion_notify (GtkWidget *widget,
107 GdkEventMotion *event);
108 static gint gtk_decorated_window_window_state (GtkWidget *widget,
109 GdkEventWindowState *event);
110 static void gtk_decorated_window_paint (GtkWidget *widget,
112 static gint gtk_decorated_window_focus_change (GtkWidget *widget,
113 GdkEventFocus *event);
114 static void gtk_decorated_window_realize (GtkWindow *window);
115 static void gtk_decorated_window_unrealize (GtkWindow *window);
118 gtk_decoration_free (GtkWindowDecoration *deco)
120 g_free (deco->regions);
121 deco->regions = NULL;
128 gtk_decorated_window_init (GtkWindow *window)
130 GtkWindowDecoration *deco;
132 deco = g_new (GtkWindowDecoration, 1);
135 deco->regions = NULL;
136 deco->title_layout = NULL;
137 deco->resize = RESIZE_NONE;
138 deco->moving = FALSE;
139 deco->decorated = TRUE;
140 deco->closing = FALSE;
141 deco->maximizing = FALSE;
142 deco->maximized = FALSE;
143 deco->maximizable = FALSE;
144 deco->real_inner_move = FALSE;
146 g_object_set_data_full (G_OBJECT (window), I_("gtk-window-decoration"), deco,
147 (GDestroyNotify) gtk_decoration_free);
149 gtk_window_set_has_frame (window, TRUE);
151 g_signal_connect (window,
153 G_CALLBACK (gtk_decorated_window_frame_event),
155 g_signal_connect (window,
157 G_CALLBACK (gtk_decorated_window_focus_change),
159 g_signal_connect (window,
161 G_CALLBACK (gtk_decorated_window_focus_change),
163 g_signal_connect (window,
165 G_CALLBACK (gtk_decorated_window_realize),
167 g_signal_connect (window,
169 G_CALLBACK (gtk_decorated_window_unrealize),
173 static inline GtkWindowDecoration *
174 get_decoration (GtkWindow *window)
176 return (GtkWindowDecoration *)g_object_get_data (G_OBJECT (window), "gtk-window-decoration");
180 gtk_decorated_window_set_title (GtkWindow *window,
183 GtkWindowDecoration *deco = get_decoration (window);
185 if (deco->title_layout)
186 pango_layout_set_text (deco->title_layout, title, -1);
190 gtk_decorated_window_calculate_frame_size (GtkWindow *window)
192 GdkWMDecoration decorations;
193 GtkWindowDecoration *deco = get_decoration (window);
195 if (gdk_window_get_decorations (GTK_WIDGET (window)->window,
198 if ((decorations & GDK_DECOR_BORDER) &&
199 (decorations & GDK_DECOR_TITLE))
201 deco->decorated = TRUE;
202 if ((decorations & GDK_DECOR_MAXIMIZE) &&
203 (gtk_window_get_type_hint (window) == GDK_WINDOW_TYPE_HINT_NORMAL))
204 deco->maximizable = TRUE;
207 deco->decorated = FALSE;
211 deco->decorated = (window->type != GTK_WINDOW_POPUP);
212 deco->maximizable = (gtk_window_get_type_hint (window) == GDK_WINDOW_TYPE_HINT_NORMAL);
216 gtk_window_set_frame_dimensions (window,
217 DECORATION_BORDER_LEFT,
218 DECORATION_BORDER_TOP,
219 DECORATION_BORDER_RIGHT,
220 DECORATION_BORDER_BOTTOM);
222 gtk_window_set_frame_dimensions (window, 0, 0, 0, 0);
224 gtk_decorated_window_recalculate_regions (window);
228 gtk_decorated_window_inner_change (GdkWindow *win,
230 gint width, gint height,
233 GtkWindow *window = (GtkWindow *)user_data;
234 GtkWidget *widget = GTK_WIDGET (window);
235 GtkWindowDecoration *deco = get_decoration (window);
237 if (deco->real_inner_move)
239 deco->real_inner_move = FALSE;
243 deco->real_inner_move = TRUE;
244 gdk_window_move_resize (widget->window,
245 window->frame_left, window->frame_top,
248 gdk_window_move_resize (window->frame,
249 x - window->frame_left, y - window->frame_top,
250 width + window->frame_left + window->frame_right,
251 height + window->frame_top + window->frame_bottom);
256 gtk_decorated_window_inner_get_pos (GdkWindow *win,
260 GtkWindow *window = (GtkWindow *)user_data;
262 gdk_window_get_position (window->frame, x, y);
264 *x += window->frame_left;
265 *y += window->frame_top;
269 gtk_decorated_window_realize (GtkWindow *window)
271 GtkWindowDecoration *deco = get_decoration (window);
272 GtkWidget *widget = GTK_WIDGET (window);
273 PangoFontDescription *font_desc;
275 deco->title_layout = gtk_widget_create_pango_layout (widget,
276 (window->title)?window->title:"");
278 font_desc = pango_font_description_from_string(DECORATION_TITLE_FONT);
279 pango_layout_set_font_description (deco->title_layout, font_desc);
280 pango_font_description_free (font_desc);
285 gtk_decorated_window_unrealize (GtkWindow *window)
287 GtkWindowDecoration *deco = get_decoration (window);
289 if (deco->title_layout)
291 g_object_unref (deco->title_layout);
292 deco->title_layout = NULL;
297 gtk_decorated_window_frame_event (GtkWindow *window, GdkEvent *event)
299 GtkWindowDecoration *deco = get_decoration (window);
300 GtkWidget *widget = GTK_WIDGET (window);
301 GdkEventExpose *expose_event;
306 expose_event = (GdkEventExpose *)event;
308 gtk_decorated_window_paint (widget, &expose_event->area);
312 gtk_decorated_window_recalculate_regions (window);
314 case GDK_MOTION_NOTIFY:
315 return gtk_decorated_window_motion_notify (widget, (GdkEventMotion *)event);
317 case GDK_BUTTON_PRESS:
318 return gtk_decorated_window_button_press (widget, (GdkEventButton *)event);
320 case GDK_BUTTON_RELEASE:
321 return gtk_decorated_window_button_release (widget, (GdkEventButton *)event);
322 case GDK_WINDOW_STATE:
323 return gtk_decorated_window_window_state (widget, (GdkEventWindowState *)event);
331 gtk_decorated_window_focus_change (GtkWidget *widget,
332 GdkEventFocus *event)
334 GtkWindow *window = GTK_WINDOW(widget);
335 GtkWindowDecoration *deco = get_decoration (window);
336 deco->focused = event->in;
337 gdk_window_invalidate_rect (window->frame, NULL, FALSE);
342 gtk_decorated_window_motion_notify (GtkWidget *widget,
343 GdkEventMotion *event)
346 GtkWindowDecoration *deco;
347 GdkModifierType mask;
350 gint win_x, win_y, win_w, win_h;
352 window = GTK_WINDOW (widget);
353 deco = get_decoration (window);
355 if (!deco->decorated)
358 win = widget->window;
359 gdk_window_get_pointer (window->frame, &x, &y, &mask);
361 gdk_window_get_position (window->frame, &win_x, &win_y);
362 win_x += DECORATION_BORDER_LEFT;
363 win_y += DECORATION_BORDER_TOP;
365 gdk_window_get_geometry (win, NULL, NULL, &win_w, &win_h, NULL);
370 dx = x - deco->last_x;
371 dy = y - deco->last_y;
373 gtk_window_move (window, win_x + dx, win_y + dy);
376 if (deco->resize != RESIZE_NONE)
383 switch(deco->resize) {
384 case RESIZE_BOTTOM_RIGHT:
385 w = x - DECORATION_BORDER_TOT_X;
386 h = y - DECORATION_BORDER_TOT_Y;
389 w = x - DECORATION_BORDER_TOT_X;
392 h = y - DECORATION_BORDER_TOT_Y;
394 case RESIZE_TOP_LEFT:
396 case RESIZE_TOP_RIGHT:
397 case RESIZE_BOTTOM_LEFT:
400 g_warning ("Resize mode %d not handled yet.\n", deco->resize);
404 if ((w > 0) && (h > 0))
406 _gtk_window_constrain_size (window, w,h, &w, &h);
408 if ((w != win_w) || (h != win_h))
409 gdk_window_resize (widget->window, w, h);
416 static GtkWindowRegionType
417 gtk_decorated_window_region_type (GtkWindow *window, gint x, gint y)
419 GtkWindowDecoration *deco = get_decoration (window);
422 for (i=0;i<deco->n_regions;i++)
424 if ((x > deco->regions[i].rect.x) &&
425 (x - deco->regions[i].rect.x < deco->regions[i].rect.width) &&
426 (y > deco->regions[i].rect.y) &&
427 (y - deco->regions[i].rect.y < deco->regions[i].rect.height))
428 return deco->regions[i].type;
434 gtk_decorated_window_button_press (GtkWidget *widget,
435 GdkEventButton *event)
438 GtkWindowRegionType type;
439 GtkWindowDecoration *deco;
442 window = GTK_WINDOW (widget);
443 deco = get_decoration (window);
445 if (!deco->decorated)
451 type = gtk_decorated_window_region_type (window, x, y);
455 case GTK_WINDOW_REGION_TITLE:
456 if (!deco->maximized && event->state & GDK_BUTTON1_MASK)
463 case GTK_WINDOW_REGION_MAXIMIZE:
464 if (event->state & GDK_BUTTON1_MASK)
465 deco->maximizing = TRUE;
467 case GTK_WINDOW_REGION_CLOSE:
468 if (event->state & GDK_BUTTON1_MASK)
469 deco->closing = TRUE;
471 case GTK_WINDOW_REGION_BR_RESIZE:
472 if (!deco->maximized)
474 if (event->state & GDK_BUTTON1_MASK)
475 deco->resize = RESIZE_BOTTOM_RIGHT;
488 gtk_decorated_window_button_release (GtkWidget *widget,
489 GdkEventButton *event)
492 GtkWindowRegionType type;
493 GtkWindowDecoration *deco;
495 window = GTK_WINDOW (widget);
496 deco = get_decoration (window);
500 type = gtk_decorated_window_region_type (window, event->x, event->y);
501 if (type == GTK_WINDOW_REGION_CLOSE)
503 GdkEvent *event = gdk_event_new (GDK_DELETE);
505 event->any.type = GDK_DELETE;
506 event->any.window = g_object_ref (widget->window);
507 event->any.send_event = TRUE;
509 gtk_main_do_event (event);
510 gdk_event_free (event);
513 else if (deco->maximizing)
515 type = gtk_decorated_window_region_type (window, event->x, event->y);
516 if (type == GTK_WINDOW_REGION_MAXIMIZE)
519 gtk_window_unmaximize (window);
521 gtk_window_maximize (window);
525 deco->closing = FALSE;
526 deco->maximizing = FALSE;
527 deco->moving = FALSE;
528 deco->resize = RESIZE_NONE;
533 gtk_decorated_window_window_state (GtkWidget *widget,
534 GdkEventWindowState *event)
537 GtkWindowDecoration *deco;
538 GdkWindowObject *priv;
540 window = GTK_WINDOW (widget);
541 deco = get_decoration (window);
542 priv = GDK_WINDOW_OBJECT (window->frame);
544 if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
546 if (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED)
549 gdk_window_get_geometry (widget->window, NULL, NULL,
550 &deco->last_w, &deco->last_h, NULL);
551 gdk_window_get_origin (widget->window, &deco->last_x, &deco->last_y);
552 w = gdk_screen_get_width(gdk_screen_get_default()) - DECORATION_BORDER_TOT_X;
553 h = gdk_screen_get_height(gdk_screen_get_default()) - DECORATION_BORDER_TOT_Y;
554 _gtk_window_constrain_size (window, w, h, &w, &h);
555 if (w != deco->last_w || h != deco->last_h)
557 gtk_window_move (window, DECORATION_BORDER_LEFT, DECORATION_BORDER_TOP);
558 gdk_window_resize (widget->window, w, h);
559 deco->maximized = TRUE;
564 gtk_window_move (window, deco->last_x, deco->last_y);
565 _gtk_window_constrain_size (window, deco->last_w, deco->last_h,
566 &deco->last_w, &deco->last_h);
567 gdk_window_resize (widget->window, deco->last_w, deco->last_h);
568 deco->maximized = FALSE;
575 gtk_decorated_window_paint (GtkWidget *widget,
578 GtkWindow *window = GTK_WINDOW (widget);
579 GtkWindowDecoration *deco = get_decoration (window);
581 GtkStateType border_state;
589 frame = window->frame;
590 gdk_drawable_get_size (frame, &width, &height);
593 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
594 GTK_SHADOW_NONE, area, widget, "base",
596 width, DECORATION_BORDER_TOP);
598 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
599 GTK_SHADOW_NONE, area, widget, "base",
600 0, height - DECORATION_BORDER_BOTTOM,
601 width, DECORATION_BORDER_BOTTOM);
603 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
604 GTK_SHADOW_NONE, area, widget, "base",
605 0, DECORATION_BORDER_TOP,
606 DECORATION_BORDER_LEFT, height - DECORATION_BORDER_TOT_Y);
608 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
609 GTK_SHADOW_NONE, area, widget, "base",
610 width - DECORATION_BORDER_RIGHT, DECORATION_BORDER_TOP,
611 DECORATION_BORDER_RIGHT, height - DECORATION_BORDER_TOT_Y);
615 border_state = GTK_STATE_SELECTED;
617 border_state = GTK_STATE_PRELIGHT;
619 gtk_paint_box (widget->style, frame, border_state,
620 GTK_SHADOW_OUT, area, widget, "base",
621 0, 0, width, height);
623 gtk_paint_box (widget->style, frame, border_state,
624 GTK_SHADOW_IN, area, widget, "base",
625 DECORATION_BORDER_LEFT - 2, DECORATION_BORDER_TOP - 2,
626 width - (DECORATION_BORDER_LEFT + DECORATION_BORDER_RIGHT) + 3,
627 height - (DECORATION_BORDER_TOP + DECORATION_BORDER_BOTTOM) + 3);
629 cr = gdk_cairo_create (frame);
630 cairo_set_line_width (cr, 1.0);
631 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
635 gdk_cairo_rectangle (cr, &area);
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;
648 gdk_cairo_set_source_color (cr, &widget->style->bg[widget->state]);
649 cairo_rectangle (cr, x1, y1, x2 - x1, y2 - y1);
652 gdk_cairo_set_source_rgb (cr, 0, 0, 0);
653 cairo_rectangle (cr, x1 + 1, y1 + 1, x2 - x1 - 3, 1);
654 cairo_move_to (cr, x1 + 1.5, y1 + 1.5);
655 cairo_line_to (cr, x2 - 1.5, y1 + 1.5);
656 cairo_rectangle (cr, x1 + 1.5, y1 + 2.5, DECORATION_BUTTON_SIZE - 3, DECORATION_BUTTON_SIZE - 4);
662 x1 = width - DECORATION_BORDER_LEFT - DECORATION_BUTTON_SIZE;
663 y1 = DECORATION_BUTTON_Y_OFFSET;
664 x2 = width - DECORATION_BORDER_LEFT;
665 y2 = DECORATION_BUTTON_Y_OFFSET + DECORATION_BUTTON_SIZE;
667 gdk_cairo_set_source_color (cr, &widget->style->bg[widget->state]);
668 cairo_rectangle (cr, x1, y1, x2 - x1, y2 - y1);
672 cairo_move_to (cr, x1 + 0.5, y1 + 0.5);
673 cairo_line_to (cr, x2 - 0.5, y2 - 0.5);
674 cairo_move_to (cr, x1 + 0.5, y2 - 0.5);
675 cairo_line_to (cr, x2 - 0.5, y1 + 0.5);
679 if (deco->title_layout)
681 gdk_cairo_set_source_color (cr, widget->style->fg [border_state]);
682 pango_cairo_show_layout (cr, deco->title_layout);
691 gtk_decorated_window_recalculate_regions (GtkWindow *window)
695 GtkWindowRegion *region;
696 GtkWindowDecoration *deco = get_decoration (window);
700 if (!deco->decorated)
703 n_regions += 2; /* close, Title */
704 if (deco->maximizable)
706 if (gtk_window_get_resizable (window))
709 if (deco->n_regions != n_regions)
711 g_free (deco->regions);
712 deco->regions = g_new (GtkWindowRegion, n_regions);
713 deco->n_regions = n_regions;
716 width = GTK_WIDGET (window)->allocation.width + DECORATION_BORDER_TOT_X;
717 height = GTK_WIDGET (window)->allocation.height + DECORATION_BORDER_TOT_Y;
719 region = deco->regions;
721 /* Maximize button */
722 if (deco->maximizable)
724 region->rect.x = width - (DECORATION_BORDER_LEFT * 2) - (DECORATION_BUTTON_SIZE * 2);
725 region->rect.y = DECORATION_BUTTON_Y_OFFSET;
726 region->rect.width = DECORATION_BUTTON_SIZE;
727 region->rect.height = DECORATION_BUTTON_SIZE;
728 region->type = GTK_WINDOW_REGION_MAXIMIZE;
733 region->rect.x = width - DECORATION_BORDER_LEFT - DECORATION_BUTTON_SIZE;
734 region->rect.y = DECORATION_BUTTON_Y_OFFSET;
735 region->rect.width = DECORATION_BUTTON_SIZE;
736 region->rect.height = DECORATION_BUTTON_SIZE;
737 region->type = GTK_WINDOW_REGION_CLOSE;
743 region->rect.width = width;
744 region->rect.height = DECORATION_BORDER_TOP;
745 region->type = GTK_WINDOW_REGION_TITLE;
748 if (gtk_window_get_resizable (window))
750 region->rect.x = width - (DECORATION_BORDER_RIGHT + 10);
751 region->rect.y = height - DECORATION_BORDER_BOTTOM;
752 region->rect.width = DECORATION_BORDER_RIGHT + 10;
753 region->rect.height = DECORATION_BORDER_BOTTOM;
754 region->type = GTK_WINDOW_REGION_BR_RESIZE;
757 region->rect.x = width - DECORATION_BORDER_RIGHT;
758 region->rect.y = height - (DECORATION_BORDER_BOTTOM + 10);
759 region->rect.width = DECORATION_BORDER_RIGHT;
760 region->rect.height = DECORATION_BORDER_BOTTOM + 10;
761 region->type = GTK_WINDOW_REGION_BR_RESIZE;
767 gtk_decorated_window_move_resize_window (GtkWindow *window,
773 GtkWidget *widget = GTK_WIDGET (window);
774 GtkWindowDecoration *deco = get_decoration (window);
776 deco->real_inner_move = TRUE;
777 gdk_window_move_resize (widget->window,
778 x, y, width, height);
783 gtk_decorated_window_init (GtkWindow *window)
788 gtk_decorated_window_calculate_frame_size (GtkWindow *window)
793 gtk_decorated_window_set_title (GtkWindow *window,
799 gtk_decorated_window_move_resize_window (GtkWindow *window,
805 gdk_window_move_resize (gtk_widget_get_window (GTK_WIDGET (window)),
806 x, y, width, height);