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 GDK_WINDOWING_FB
33 #define DECORATE_WINDOWS
36 #ifdef DECORATE_WINDOWS
37 #include "linux-fb/gdkfb.h"
41 GTK_WINDOW_REGION_TITLE,
42 GTK_WINDOW_REGION_MAXIMIZE,
43 GTK_WINDOW_REGION_CLOSE,
44 GTK_WINDOW_REGION_BR_RESIZE
45 } GtkWindowRegionType;
47 typedef struct _GtkWindowRegion GtkWindowRegion;
48 typedef struct _GtkWindowDecoration GtkWindowDecoration;
50 struct _GtkWindowRegion
53 GtkWindowRegionType type;
67 } GtkWindowResizeType;
69 struct _GtkWindowDecoration
72 GtkWindowRegion *regions;
77 PangoLayout *title_layout;
79 GtkWindowResizeType resize;
85 guint maximizable : 1;
87 guint real_inner_move : 1;
91 #define DECORATION_BORDER_TOP 15
92 #define DECORATION_BORDER_LEFT 3
93 #define DECORATION_BORDER_RIGHT 3
94 #define DECORATION_BORDER_BOTTOM 3
95 #define DECORATION_BORDER_TOT_X (DECORATION_BORDER_LEFT + DECORATION_BORDER_RIGHT)
96 #define DECORATION_BORDER_TOT_Y (DECORATION_BORDER_TOP + DECORATION_BORDER_BOTTOM)
97 #define DECORATION_BUTTON_SIZE 9
98 #define DECORATION_BUTTON_Y_OFFSET 2
99 #define DECORATION_TITLE_FONT "Sans 9"
101 static void gtk_decorated_window_recalculate_regions (GtkWindow *window);
102 static GtkWindowRegionType gtk_decorated_window_region_type (GtkWindow *window,
105 static gint gtk_decorated_window_frame_event (GtkWindow *window,
107 static gint gtk_decorated_window_button_press (GtkWidget *widget,
108 GdkEventButton *event);
109 static gint gtk_decorated_window_button_release (GtkWidget *widget,
110 GdkEventButton *event);
111 static gint gtk_decorated_window_motion_notify (GtkWidget *widget,
112 GdkEventMotion *event);
113 static gint gtk_decorated_window_window_state (GtkWidget *widget,
114 GdkEventWindowState *event);
115 static void gtk_decorated_window_paint (GtkWidget *widget,
117 static gint gtk_decorated_window_focus_change (GtkWidget *widget,
118 GdkEventFocus *event);
119 static void gtk_decorated_window_realize (GtkWindow *window);
120 static void gtk_decorated_window_unrealize (GtkWindow *window);
123 gtk_decoration_free (GtkWindowDecoration *deco)
125 g_free (deco->regions);
126 deco->regions = NULL;
133 gtk_decorated_window_init (GtkWindow *window)
135 GtkWindowDecoration *deco;
137 deco = g_new (GtkWindowDecoration, 1);
140 deco->regions = NULL;
141 deco->title_layout = NULL;
142 deco->resize = RESIZE_NONE;
143 deco->moving = FALSE;
144 deco->decorated = TRUE;
145 deco->closing = FALSE;
146 deco->maximizing = FALSE;
147 deco->maximized = FALSE;
148 deco->maximizable = FALSE;
149 deco->real_inner_move = FALSE;
151 g_object_set_data_full (G_OBJECT (window), I_("gtk-window-decoration"), deco,
152 (GDestroyNotify) gtk_decoration_free);
154 gtk_window_set_has_frame (window, TRUE);
156 g_signal_connect (window,
158 G_CALLBACK (gtk_decorated_window_frame_event),
160 g_signal_connect (window,
162 G_CALLBACK (gtk_decorated_window_focus_change),
164 g_signal_connect (window,
166 G_CALLBACK (gtk_decorated_window_focus_change),
168 g_signal_connect (window,
170 G_CALLBACK (gtk_decorated_window_realize),
172 g_signal_connect (window,
174 G_CALLBACK (gtk_decorated_window_unrealize),
178 static inline GtkWindowDecoration *
179 get_decoration (GtkWindow *window)
181 return (GtkWindowDecoration *)g_object_get_data (G_OBJECT (window), "gtk-window-decoration");
185 gtk_decorated_window_set_title (GtkWindow *window,
188 GtkWindowDecoration *deco = get_decoration (window);
190 if (deco->title_layout)
191 pango_layout_set_text (deco->title_layout, title, -1);
195 gtk_decorated_window_calculate_frame_size (GtkWindow *window)
197 GdkWMDecoration decorations;
198 GtkWindowDecoration *deco = get_decoration (window);
200 if (gdk_window_get_decorations (GTK_WIDGET (window)->window,
203 if ((decorations & GDK_DECOR_BORDER) &&
204 (decorations & GDK_DECOR_TITLE))
206 deco->decorated = TRUE;
207 if ((decorations & GDK_DECOR_MAXIMIZE) &&
208 (gtk_window_get_type_hint (window) == GDK_WINDOW_TYPE_HINT_NORMAL))
209 deco->maximizable = TRUE;
212 deco->decorated = FALSE;
216 deco->decorated = (window->type != GTK_WINDOW_POPUP);
217 deco->maximizable = (gtk_window_get_type_hint (window) == GDK_WINDOW_TYPE_HINT_NORMAL);
221 gtk_window_set_frame_dimensions (window,
222 DECORATION_BORDER_LEFT,
223 DECORATION_BORDER_TOP,
224 DECORATION_BORDER_RIGHT,
225 DECORATION_BORDER_BOTTOM);
227 gtk_window_set_frame_dimensions (window, 0, 0, 0, 0);
229 gtk_decorated_window_recalculate_regions (window);
233 gtk_decorated_window_inner_change (GdkWindow *win,
235 gint width, gint height,
238 GtkWindow *window = (GtkWindow *)user_data;
239 GtkWidget *widget = GTK_WIDGET (window);
240 GtkWindowDecoration *deco = get_decoration (window);
242 if (deco->real_inner_move)
244 deco->real_inner_move = FALSE;
248 deco->real_inner_move = TRUE;
249 gdk_window_move_resize (widget->window,
250 window->frame_left, window->frame_top,
253 gdk_window_move_resize (window->frame,
254 x - window->frame_left, y - window->frame_top,
255 width + window->frame_left + window->frame_right,
256 height + window->frame_top + window->frame_bottom);
261 gtk_decorated_window_inner_get_pos (GdkWindow *win,
265 GtkWindow *window = (GtkWindow *)user_data;
267 gdk_window_get_position (window->frame, x, y);
269 *x += window->frame_left;
270 *y += window->frame_top;
274 gtk_decorated_window_realize (GtkWindow *window)
276 GtkWindowDecoration *deco = get_decoration (window);
277 GtkWidget *widget = GTK_WIDGET (window);
278 PangoFontDescription *font_desc;
280 deco->title_layout = gtk_widget_create_pango_layout (widget,
281 (window->title)?window->title:"");
283 font_desc = pango_font_description_from_string(DECORATION_TITLE_FONT);
284 pango_layout_set_font_description (deco->title_layout, font_desc);
285 pango_font_description_free (font_desc);
287 gdk_fb_window_set_child_handler (window->frame,
288 gtk_decorated_window_inner_change,
289 gtk_decorated_window_inner_get_pos,
292 /* This is a huge hack to make frames have the same shape as
293 the window they wrap */
294 gdk_window_shape_combine_mask (window->frame, GDK_FB_USE_CHILD_SHAPE, 0, 0);
299 gtk_decorated_window_unrealize (GtkWindow *window)
301 GtkWindowDecoration *deco = get_decoration (window);
303 if (deco->title_layout)
305 g_object_unref (deco->title_layout);
306 deco->title_layout = NULL;
311 gtk_decorated_window_frame_event (GtkWindow *window, GdkEvent *event)
313 GtkWindowDecoration *deco = get_decoration (window);
314 GtkWidget *widget = GTK_WIDGET (window);
315 GdkEventExpose *expose_event;
320 expose_event = (GdkEventExpose *)event;
322 gtk_decorated_window_paint (widget, &expose_event->area);
326 gtk_decorated_window_recalculate_regions (window);
328 case GDK_MOTION_NOTIFY:
329 return gtk_decorated_window_motion_notify (widget, (GdkEventMotion *)event);
331 case GDK_BUTTON_PRESS:
332 return gtk_decorated_window_button_press (widget, (GdkEventButton *)event);
334 case GDK_BUTTON_RELEASE:
335 return gtk_decorated_window_button_release (widget, (GdkEventButton *)event);
336 case GDK_WINDOW_STATE:
337 return gtk_decorated_window_window_state (widget, (GdkEventWindowState *)event);
345 gtk_decorated_window_focus_change (GtkWidget *widget,
346 GdkEventFocus *event)
348 GtkWindow *window = GTK_WINDOW(widget);
349 GtkWindowDecoration *deco = get_decoration (window);
350 deco->focused = event->in;
351 gdk_window_invalidate_rect (window->frame, NULL, FALSE);
356 gtk_decorated_window_motion_notify (GtkWidget *widget,
357 GdkEventMotion *event)
360 GtkWindowDecoration *deco;
361 GdkModifierType mask;
364 gint win_x, win_y, win_w, win_h;
366 window = GTK_WINDOW (widget);
367 deco = get_decoration (window);
369 if (!deco->decorated)
372 win = widget->window;
373 gdk_window_get_pointer (window->frame, &x, &y, &mask);
375 gdk_window_get_position (window->frame, &win_x, &win_y);
376 win_x += DECORATION_BORDER_LEFT;
377 win_y += DECORATION_BORDER_TOP;
379 gdk_window_get_geometry (win, NULL, NULL, &win_w, &win_h, NULL);
384 dx = x - deco->last_x;
385 dy = y - deco->last_y;
387 _gtk_window_reposition (window, win_x + dx, win_y + dy);
390 if (deco->resize != RESIZE_NONE)
397 switch(deco->resize) {
398 case RESIZE_BOTTOM_RIGHT:
399 w = x - DECORATION_BORDER_TOT_X;
400 h = y - DECORATION_BORDER_TOT_Y;
403 w = x - DECORATION_BORDER_TOT_X;
406 h = y - DECORATION_BORDER_TOT_Y;
408 case RESIZE_TOP_LEFT:
410 case RESIZE_TOP_RIGHT:
411 case RESIZE_BOTTOM_LEFT:
414 g_warning ("Resize mode %d not handled yet.\n", deco->resize);
418 if ((w > 0) && (h > 0))
420 _gtk_window_constrain_size (window, w,h, &w, &h);
422 if ((w != win_w) || (h != win_h))
423 gdk_window_resize (widget->window, w, h);
430 static GtkWindowRegionType
431 gtk_decorated_window_region_type (GtkWindow *window, gint x, gint y)
433 GtkWindowDecoration *deco = get_decoration (window);
436 for (i=0;i<deco->n_regions;i++)
438 if ((x > deco->regions[i].rect.x) &&
439 (x - deco->regions[i].rect.x < deco->regions[i].rect.width) &&
440 (y > deco->regions[i].rect.y) &&
441 (y - deco->regions[i].rect.y < deco->regions[i].rect.height))
442 return deco->regions[i].type;
448 gtk_decorated_window_button_press (GtkWidget *widget,
449 GdkEventButton *event)
452 GtkWindowRegionType type;
453 GtkWindowDecoration *deco;
456 window = GTK_WINDOW (widget);
457 deco = get_decoration (window);
459 if (!deco->decorated)
465 type = gtk_decorated_window_region_type (window, x, y);
469 case GTK_WINDOW_REGION_TITLE:
470 if (!deco->maximized && event->state & GDK_BUTTON1_MASK)
477 case GTK_WINDOW_REGION_MAXIMIZE:
478 if (event->state & GDK_BUTTON1_MASK)
479 deco->maximizing = TRUE;
481 case GTK_WINDOW_REGION_CLOSE:
482 if (event->state & GDK_BUTTON1_MASK)
483 deco->closing = TRUE;
485 case GTK_WINDOW_REGION_BR_RESIZE:
486 if (!deco->maximized)
488 if (event->state & GDK_BUTTON1_MASK)
489 deco->resize = RESIZE_BOTTOM_RIGHT;
502 gtk_decorated_window_button_release (GtkWidget *widget,
503 GdkEventButton *event)
506 GtkWindowRegionType type;
507 GtkWindowDecoration *deco;
509 window = GTK_WINDOW (widget);
510 deco = get_decoration (window);
514 type = gtk_decorated_window_region_type (window, event->x, event->y);
515 if (type == GTK_WINDOW_REGION_CLOSE)
517 GdkEvent *event = gdk_event_new (GDK_DELETE);
519 event->any.type = GDK_DELETE;
520 event->any.window = g_object_ref (widget->window);
521 event->any.send_event = TRUE;
523 gtk_main_do_event (event);
524 gdk_event_free (event);
527 else if (deco->maximizing)
529 type = gtk_decorated_window_region_type (window, event->x, event->y);
530 if (type == GTK_WINDOW_REGION_MAXIMIZE)
533 gtk_window_unmaximize (window);
535 gtk_window_maximize (window);
539 deco->closing = FALSE;
540 deco->maximizing = FALSE;
541 deco->moving = FALSE;
542 deco->resize = RESIZE_NONE;
547 gtk_decorated_window_window_state (GtkWidget *widget,
548 GdkEventWindowState *event)
551 GtkWindowDecoration *deco;
552 GdkWindowObject *priv;
554 window = GTK_WINDOW (widget);
555 deco = get_decoration (window);
556 priv = GDK_WINDOW_OBJECT (window->frame);
558 if (event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED)
560 if (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED)
563 gdk_window_get_geometry (widget->window, NULL, NULL,
564 &deco->last_w, &deco->last_h, NULL);
565 gdk_window_get_origin (widget->window, &deco->last_x, &deco->last_y);
566 w = gdk_screen_get_width(gdk_screen_get_default()) - DECORATION_BORDER_TOT_X;
567 h = gdk_screen_get_height(gdk_screen_get_default()) - DECORATION_BORDER_TOT_Y;
568 _gtk_window_constrain_size (window, w, h, &w, &h);
569 if (w != deco->last_w || h != deco->last_h)
571 _gtk_window_reposition (window, DECORATION_BORDER_LEFT, DECORATION_BORDER_TOP);
572 gdk_window_resize (widget->window, w, h);
573 deco->maximized = TRUE;
578 _gtk_window_reposition (window, deco->last_x, deco->last_y);
579 _gtk_window_constrain_size (window, deco->last_w, deco->last_h,
580 &deco->last_w, &deco->last_h);
581 gdk_window_resize (widget->window, deco->last_w, deco->last_h);
582 deco->maximized = FALSE;
589 gtk_decorated_window_paint (GtkWidget *widget,
592 GtkWindow *window = GTK_WINDOW (widget);
593 GtkWindowDecoration *deco = get_decoration (window);
595 GtkStateType border_state;
602 frame = window->frame;
603 gdk_drawable_get_size (frame, &width, &height);
606 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
607 GTK_SHADOW_NONE, area, widget, "base",
609 width, DECORATION_BORDER_TOP);
611 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
612 GTK_SHADOW_NONE, area, widget, "base",
613 0, height - DECORATION_BORDER_BOTTOM,
614 width, DECORATION_BORDER_BOTTOM);
616 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
617 GTK_SHADOW_NONE, area, widget, "base",
618 0, DECORATION_BORDER_TOP,
619 DECORATION_BORDER_LEFT, height - DECORATION_BORDER_TOT_Y);
621 gtk_paint_flat_box (widget->style, frame, GTK_STATE_NORMAL,
622 GTK_SHADOW_NONE, area, widget, "base",
623 width - DECORATION_BORDER_RIGHT, DECORATION_BORDER_TOP,
624 DECORATION_BORDER_RIGHT, height - DECORATION_BORDER_TOT_Y);
628 border_state = GTK_STATE_SELECTED;
630 border_state = GTK_STATE_PRELIGHT;
632 gtk_paint_box (widget->style, frame, border_state,
633 GTK_SHADOW_OUT, area, widget, "base",
634 0, 0, width, height);
636 gtk_paint_box (widget->style, frame, border_state,
637 GTK_SHADOW_IN, area, widget, "base",
638 DECORATION_BORDER_LEFT - 2, DECORATION_BORDER_TOP - 2,
639 width - (DECORATION_BORDER_LEFT + DECORATION_BORDER_RIGHT) + 3,
640 height - (DECORATION_BORDER_TOP + DECORATION_BORDER_BOTTOM) + 3);
642 if (deco->maximizable)
644 /* Maximize button: */
646 x1 = width - (DECORATION_BORDER_LEFT * 2) - (DECORATION_BUTTON_SIZE * 2);
647 y1 = DECORATION_BUTTON_Y_OFFSET;
648 x2 = x1 + DECORATION_BUTTON_SIZE;
649 y2 = y1 + DECORATION_BUTTON_SIZE;
652 gdk_gc_set_clip_rectangle (widget->style->bg_gc[widget->state], area);
654 gdk_draw_rectangle (frame, widget->style->bg_gc[widget->state], TRUE,
655 x1, y1, x2 - x1, y2 - y1);
657 gdk_draw_line (frame, widget->style->black_gc, x1 + 1, y1 + 1, x2 - 2, y1 + 1);
659 gdk_draw_rectangle (frame, widget->style->black_gc, FALSE,
661 DECORATION_BUTTON_SIZE - 3, DECORATION_BUTTON_SIZE - 4);
664 gdk_gc_set_clip_rectangle (widget->style->black_gc, NULL);
669 x1 = width - DECORATION_BORDER_LEFT - DECORATION_BUTTON_SIZE;
670 y1 = DECORATION_BUTTON_Y_OFFSET;
671 x2 = width - DECORATION_BORDER_LEFT;
672 y2 = DECORATION_BUTTON_Y_OFFSET + DECORATION_BUTTON_SIZE;
675 gdk_gc_set_clip_rectangle (widget->style->bg_gc[widget->state], area);
677 gdk_draw_rectangle (frame, widget->style->bg_gc[widget->state], TRUE,
678 x1, y1, x2 - x1, y2 - y1);
681 gdk_gc_set_clip_rectangle (widget->style->bg_gc[widget->state], NULL);
684 gdk_gc_set_clip_rectangle (widget->style->black_gc, area);
686 gdk_draw_line (frame, widget->style->black_gc, x1, y1, x2-1, y2-1);
688 gdk_draw_line (frame, widget->style->black_gc, x1, y2-1, x2-1, y1);
691 gdk_gc_set_clip_rectangle (widget->style->black_gc, NULL);
696 if (deco->title_layout)
699 gdk_gc_set_clip_rectangle (widget->style->fg_gc [border_state], area);
701 gdk_draw_layout (frame,
702 widget->style->fg_gc [border_state],
703 DECORATION_BORDER_LEFT, 1,
706 gdk_gc_set_clip_rectangle (widget->style->fg_gc [border_state], NULL);
714 gtk_decorated_window_recalculate_regions (GtkWindow *window)
718 GtkWindowRegion *region;
719 GtkWindowDecoration *deco = get_decoration (window);
723 if (!deco->decorated)
726 n_regions += 2; /* close, Title */
727 if (deco->maximizable)
729 if (window->allow_shrink || window->allow_grow)
732 if (deco->n_regions != n_regions)
734 g_free (deco->regions);
735 deco->regions = g_new (GtkWindowRegion, n_regions);
736 deco->n_regions = n_regions;
739 width = GTK_WIDGET (window)->allocation.width + DECORATION_BORDER_TOT_X;
740 height = GTK_WIDGET (window)->allocation.height + DECORATION_BORDER_TOT_Y;
742 region = deco->regions;
744 /* Maximize button */
745 if (deco->maximizable)
747 region->rect.x = width - (DECORATION_BORDER_LEFT * 2) - (DECORATION_BUTTON_SIZE * 2);
748 region->rect.y = DECORATION_BUTTON_Y_OFFSET;
749 region->rect.width = DECORATION_BUTTON_SIZE;
750 region->rect.height = DECORATION_BUTTON_SIZE;
751 region->type = GTK_WINDOW_REGION_MAXIMIZE;
756 region->rect.x = width - DECORATION_BORDER_LEFT - DECORATION_BUTTON_SIZE;
757 region->rect.y = DECORATION_BUTTON_Y_OFFSET;
758 region->rect.width = DECORATION_BUTTON_SIZE;
759 region->rect.height = DECORATION_BUTTON_SIZE;
760 region->type = GTK_WINDOW_REGION_CLOSE;
766 region->rect.width = width;
767 region->rect.height = DECORATION_BORDER_TOP;
768 region->type = GTK_WINDOW_REGION_TITLE;
771 if (window->allow_shrink || window->allow_grow)
773 region->rect.x = width - (DECORATION_BORDER_RIGHT + 10);
774 region->rect.y = height - DECORATION_BORDER_BOTTOM;
775 region->rect.width = DECORATION_BORDER_RIGHT + 10;
776 region->rect.height = DECORATION_BORDER_BOTTOM;
777 region->type = GTK_WINDOW_REGION_BR_RESIZE;
780 region->rect.x = width - DECORATION_BORDER_RIGHT;
781 region->rect.y = height - (DECORATION_BORDER_BOTTOM + 10);
782 region->rect.width = DECORATION_BORDER_RIGHT;
783 region->rect.height = DECORATION_BORDER_BOTTOM + 10;
784 region->type = GTK_WINDOW_REGION_BR_RESIZE;
790 gtk_decorated_window_move_resize_window (GtkWindow *window,
796 GtkWidget *widget = GTK_WIDGET (window);
797 GtkWindowDecoration *deco = get_decoration (window);
799 deco->real_inner_move = TRUE;
800 gdk_window_move_resize (widget->window,
801 x, y, width, height);
806 gtk_decorated_window_init (GtkWindow *window)
811 gtk_decorated_window_calculate_frame_size (GtkWindow *window)
816 gtk_decorated_window_set_title (GtkWindow *window,
822 gtk_decorated_window_move_resize_window (GtkWindow *window,
828 gdk_window_move_resize (GTK_WIDGET (window)->window,
829 x, y, width, height);
834 #define __GTK_WINDOW_DECORATE_C__
835 #include "gtkaliasdef.c"