1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/gtk/gtk_input_event_box.h"
12 #define GTK_INPUT_EVENT_BOX_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
13 GTK_TYPE_INPUT_EVENT_BOX, \
14 GtkInputEventBoxPrivate))
16 typedef struct _GtkInputEventBoxPrivate GtkInputEventBoxPrivate
;
18 struct _GtkInputEventBoxPrivate
{
19 GdkWindow
* event_window
;
22 G_DEFINE_TYPE(GtkInputEventBox
, gtk_input_event_box
, GTK_TYPE_BIN
)
24 static void gtk_input_event_box_realize(GtkWidget
* widget
);
25 static void gtk_input_event_box_unrealize(GtkWidget
* widget
);
26 static void gtk_input_event_box_map(GtkWidget
* widget
);
27 static void gtk_input_event_box_unmap(GtkWidget
* widget
);
28 static void gtk_input_event_box_size_allocate(GtkWidget
* widget
,
29 GtkAllocation
* allocation
);
30 static void gtk_input_event_box_size_request(GtkWidget
* widget
,
31 GtkRequisition
* requisition
);
33 static void gtk_input_event_box_class_init(GtkInputEventBoxClass
* klass
) {
34 GtkWidgetClass
* widget_class
= reinterpret_cast<GtkWidgetClass
*>(klass
);
35 widget_class
->realize
= gtk_input_event_box_realize
;
36 widget_class
->unrealize
= gtk_input_event_box_unrealize
;
37 widget_class
->map
= gtk_input_event_box_map
;
38 widget_class
->unmap
= gtk_input_event_box_unmap
;
39 widget_class
->size_allocate
= gtk_input_event_box_size_allocate
;
40 widget_class
->size_request
= gtk_input_event_box_size_request
;
42 GObjectClass
* gobject_class
= G_OBJECT_CLASS(klass
);
43 g_type_class_add_private(gobject_class
, sizeof(GtkInputEventBoxPrivate
));
46 static void gtk_input_event_box_init(GtkInputEventBox
* widget
) {
47 GtkInputEventBoxPrivate
* priv
= GTK_INPUT_EVENT_BOX_GET_PRIVATE(widget
);
48 priv
->event_window
= NULL
;
51 GtkWidget
* gtk_input_event_box_new() {
52 return GTK_WIDGET(g_object_new(GTK_TYPE_INPUT_EVENT_BOX
, NULL
));
55 static void gtk_input_event_box_realize(GtkWidget
* widget
) {
56 g_return_if_fail(GTK_IS_INPUT_EVENT_BOX(widget
));
58 GtkInputEventBoxPrivate
* priv
= GTK_INPUT_EVENT_BOX_GET_PRIVATE(widget
);
60 GdkWindow
* parent
= gtk_widget_get_parent_window(widget
);
61 GdkWindowAttr attributes
;
62 GtkAllocation allocation
;
63 gint attributes_mask
= GDK_WA_X
| GDK_WA_Y
;
65 gtk_widget_get_allocation(widget
, &allocation
);
67 attributes
.x
= allocation
.x
;
68 attributes
.y
= allocation
.y
;
69 attributes
.width
= allocation
.width
;
70 attributes
.height
= allocation
.height
;
72 attributes
.window_type
= GDK_WINDOW_CHILD
;
73 attributes
.wclass
= GDK_INPUT_ONLY
;
75 attributes
.event_mask
= gtk_widget_get_events(widget
) |
76 GDK_BUTTON_MOTION_MASK
|
77 GDK_BUTTON_PRESS_MASK
|
78 GDK_BUTTON_RELEASE_MASK
|
80 GDK_ENTER_NOTIFY_MASK
|
81 GDK_LEAVE_NOTIFY_MASK
;
83 priv
->event_window
= gdk_window_new(parent
, &attributes
, attributes_mask
);
84 gdk_window_set_user_data(priv
->event_window
, widget
);
86 GTK_WIDGET_CLASS(gtk_input_event_box_parent_class
)->realize(widget
);
89 static void gtk_input_event_box_unrealize(GtkWidget
* widget
) {
90 g_return_if_fail(GTK_IS_INPUT_EVENT_BOX(widget
));
92 GtkInputEventBoxPrivate
* priv
= GTK_INPUT_EVENT_BOX_GET_PRIVATE(widget
);
93 if (priv
->event_window
) {
94 gdk_window_set_user_data(priv
->event_window
, NULL
);
95 gdk_window_destroy(priv
->event_window
);
96 priv
->event_window
= NULL
;
99 GTK_WIDGET_CLASS(gtk_input_event_box_parent_class
)->unrealize(widget
);
102 static void gtk_input_event_box_map(GtkWidget
* widget
) {
103 g_return_if_fail(GTK_IS_INPUT_EVENT_BOX(widget
));
105 GtkInputEventBoxPrivate
* priv
= GTK_INPUT_EVENT_BOX_GET_PRIVATE(widget
);
106 if (priv
->event_window
)
107 gdk_window_show(priv
->event_window
);
109 GTK_WIDGET_CLASS(gtk_input_event_box_parent_class
)->map(widget
);
112 static void gtk_input_event_box_unmap(GtkWidget
* widget
) {
113 g_return_if_fail(GTK_IS_INPUT_EVENT_BOX(widget
));
115 GtkInputEventBoxPrivate
* priv
= GTK_INPUT_EVENT_BOX_GET_PRIVATE(widget
);
116 if (priv
->event_window
)
117 gdk_window_hide(priv
->event_window
);
119 GTK_WIDGET_CLASS(gtk_input_event_box_parent_class
)->unmap(widget
);
122 static void gtk_input_event_box_size_allocate(GtkWidget
* widget
,
123 GtkAllocation
* allocation
) {
124 g_return_if_fail(GTK_IS_INPUT_EVENT_BOX(widget
));
126 gtk_widget_set_allocation(widget
, allocation
);
128 GtkInputEventBoxPrivate
* priv
= GTK_INPUT_EVENT_BOX_GET_PRIVATE(widget
);
129 if (priv
->event_window
) {
130 gdk_window_move_resize(priv
->event_window
,
137 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(widget
));
139 gtk_widget_size_allocate(child
, allocation
);
142 static void gtk_input_event_box_size_request(GtkWidget
* widget
,
143 GtkRequisition
* requisition
) {
145 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(widget
));
147 gtk_widget_size_request(child
, requisition
);
150 GdkWindow
* gtk_input_event_box_get_window(GtkInputEventBox
* widget
) {
151 g_return_val_if_fail(GTK_IS_INPUT_EVENT_BOX(widget
), NULL
);
153 GtkInputEventBoxPrivate
* priv
= GTK_INPUT_EVENT_BOX_GET_PRIVATE(widget
);
154 return priv
->event_window
;