| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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. | |
| 4 | |
| 5 #include "chrome/browser/gtk/gtk_expanded_container.h" | |
| 6 | |
| 7 #include <gtk/gtk.h> | |
| 8 #include <algorithm> | |
| 9 | |
| 10 namespace { | |
| 11 | |
| 12 enum { | |
| 13 CHILD_SIZE_REQUEST, | |
| 14 LAST_SIGNAL | |
| 15 }; | |
| 16 | |
| 17 guint expanded_container_signals[LAST_SIGNAL] = { 0 }; | |
| 18 | |
| 19 struct SizeAllocateData { | |
| 20 GtkWidget* container; | |
| 21 GtkAllocation* allocation; | |
| 22 int border_width; | |
| 23 }; | |
| 24 | |
| 25 void GetChildPosition(GtkWidget* container, GtkWidget* child, int* x, int* y) { | |
| 26 GValue v = { 0 }; | |
| 27 g_value_init(&v, G_TYPE_INT); | |
| 28 gtk_container_child_get_property(GTK_CONTAINER(container), child, "x", &v); | |
| 29 *x = g_value_get_int(&v); | |
| 30 gtk_container_child_get_property(GTK_CONTAINER(container), child, "y", &v); | |
| 31 *y = g_value_get_int(&v); | |
| 32 g_value_unset(&v); | |
| 33 } | |
| 34 | |
| 35 void ChildSizeAllocate(GtkWidget* child, gpointer userdata) { | |
| 36 if (!GTK_WIDGET_VISIBLE(child)) | |
| 37 return; | |
| 38 | |
| 39 SizeAllocateData* data = reinterpret_cast<SizeAllocateData*>(userdata); | |
| 40 | |
| 41 GtkRequisition child_requisition; | |
| 42 child_requisition.width = data->allocation->width - data->border_width * 2; | |
| 43 child_requisition.height = data->allocation->height - data->border_width * 2; | |
| 44 | |
| 45 // We need to give whoever is pulling our strings a chance to adjust the | |
| 46 // size of our children. | |
| 47 g_signal_emit(data->container, | |
| 48 expanded_container_signals[CHILD_SIZE_REQUEST], 0, | |
| 49 child, &child_requisition); | |
| 50 | |
| 51 GtkAllocation child_allocation; | |
| 52 child_allocation.width = child_requisition.width; | |
| 53 child_allocation.height = child_requisition.height; | |
| 54 if (child_allocation.width < 0 || child_allocation.height < 0) { | |
| 55 gtk_widget_get_child_requisition(child, &child_requisition); | |
| 56 if (child_allocation.width < 0) | |
| 57 child_allocation.width = child_requisition.width; | |
| 58 if (child_allocation.height < 0) | |
| 59 child_allocation.height = child_requisition.height; | |
| 60 } | |
| 61 | |
| 62 int x, y; | |
| 63 GetChildPosition(data->container, child, &x, &y); | |
| 64 | |
| 65 child_allocation.x = x + data->border_width; | |
| 66 child_allocation.y = y + data->border_width; | |
| 67 | |
| 68 if (GTK_WIDGET_NO_WINDOW(data->container)) { | |
| 69 child_allocation.x += data->allocation->x; | |
| 70 child_allocation.y += data->allocation->y; | |
| 71 } | |
| 72 gtk_widget_size_allocate(child, &child_allocation); | |
| 73 } | |
| 74 | |
| 75 void Marshal_VOID__OBJECT_BOXED(GClosure* closure, | |
| 76 GValue* return_value G_GNUC_UNUSED, | |
| 77 guint n_param_values, | |
| 78 const GValue* param_values, | |
| 79 gpointer invocation_hint G_GNUC_UNUSED, | |
| 80 gpointer marshal_data) { | |
| 81 typedef void (*GMarshalFunc_VOID__OBJECT_BOXED) (gpointer data1, | |
| 82 gpointer arg_1, | |
| 83 gpointer arg_2, | |
| 84 gpointer data2); | |
| 85 register GMarshalFunc_VOID__OBJECT_BOXED callback; | |
| 86 register GCClosure *cc = reinterpret_cast<GCClosure*>(closure); | |
| 87 register gpointer data1, data2; | |
| 88 | |
| 89 g_return_if_fail(n_param_values == 3); | |
| 90 | |
| 91 if (G_CCLOSURE_SWAP_DATA(closure)) { | |
| 92 data1 = closure->data; | |
| 93 data2 = g_value_peek_pointer(param_values + 0); | |
| 94 } else { | |
| 95 data1 = g_value_peek_pointer(param_values + 0); | |
| 96 data2 = closure->data; | |
| 97 } | |
| 98 | |
| 99 callback = reinterpret_cast<GMarshalFunc_VOID__OBJECT_BOXED>( | |
| 100 marshal_data ? marshal_data : cc->callback); | |
| 101 | |
| 102 callback(data1, | |
| 103 g_value_get_object(param_values + 1), | |
| 104 g_value_get_boxed(param_values + 2), | |
| 105 data2); | |
| 106 } | |
| 107 | |
| 108 } // namespace | |
| 109 | |
| 110 G_BEGIN_DECLS | |
| 111 | |
| 112 static void gtk_expanded_container_size_allocate(GtkWidget* widget, | |
| 113 GtkAllocation* allocation); | |
| 114 | |
| 115 G_DEFINE_TYPE(GtkExpandedContainer, gtk_expanded_container, GTK_TYPE_FIXED) | |
| 116 | |
| 117 static void gtk_expanded_container_class_init( | |
| 118 GtkExpandedContainerClass *klass) { | |
| 119 GtkObjectClass* object_class = | |
| 120 reinterpret_cast<GtkObjectClass*>(klass); | |
| 121 | |
| 122 GtkWidgetClass* widget_class = | |
| 123 reinterpret_cast<GtkWidgetClass*>(klass); | |
| 124 widget_class->size_allocate = gtk_expanded_container_size_allocate; | |
| 125 | |
| 126 expanded_container_signals[CHILD_SIZE_REQUEST] = | |
| 127 g_signal_new("child-size-request", | |
| 128 G_OBJECT_CLASS_TYPE(object_class), | |
| 129 static_cast<GSignalFlags>(G_SIGNAL_RUN_FIRST), | |
| 130 0, | |
| 131 NULL, NULL, | |
| 132 Marshal_VOID__OBJECT_BOXED, | |
| 133 G_TYPE_NONE, 2, | |
| 134 GTK_TYPE_WIDGET, | |
| 135 GTK_TYPE_REQUISITION | G_SIGNAL_TYPE_STATIC_SCOPE); | |
| 136 } | |
| 137 | |
| 138 static void gtk_expanded_container_init(GtkExpandedContainer* container) { | |
| 139 } | |
| 140 | |
| 141 static void gtk_expanded_container_size_allocate(GtkWidget* widget, | |
| 142 GtkAllocation* allocation) { | |
| 143 widget->allocation = *allocation; | |
| 144 | |
| 145 if (!GTK_WIDGET_NO_WINDOW(widget) && GTK_WIDGET_REALIZED(widget)) { | |
| 146 gdk_window_move_resize(widget->window, | |
| 147 allocation->x, | |
| 148 allocation->y, | |
| 149 allocation->width, | |
| 150 allocation->height); | |
| 151 } | |
| 152 | |
| 153 SizeAllocateData data; | |
| 154 data.container = widget; | |
| 155 data.allocation = allocation; | |
| 156 data.border_width = gtk_container_get_border_width(GTK_CONTAINER(widget)); | |
| 157 | |
| 158 gtk_container_foreach(GTK_CONTAINER(widget), ChildSizeAllocate, &data); | |
| 159 } | |
| 160 | |
| 161 GtkWidget* gtk_expanded_container_new() { | |
| 162 return GTK_WIDGET(g_object_new(GTK_TYPE_EXPANDED_CONTAINER, NULL)); | |
| 163 } | |
| 164 | |
| 165 void gtk_expanded_container_put(GtkExpandedContainer* container, | |
| 166 GtkWidget* widget, gint x, gint y) { | |
| 167 g_return_if_fail(GTK_IS_EXPANDED_CONTAINER(container)); | |
| 168 g_return_if_fail(GTK_IS_WIDGET(widget)); | |
| 169 gtk_fixed_put(GTK_FIXED(container), widget, x, y); | |
| 170 } | |
| 171 | |
| 172 void gtk_expanded_container_move(GtkExpandedContainer* container, | |
| 173 GtkWidget* widget, gint x, gint y) { | |
| 174 g_return_if_fail(GTK_IS_EXPANDED_CONTAINER(container)); | |
| 175 g_return_if_fail(GTK_IS_WIDGET(widget)); | |
| 176 gtk_fixed_move(GTK_FIXED(container), widget, x, y); | |
| 177 } | |
| 178 | |
| 179 void gtk_expanded_container_set_has_window(GtkExpandedContainer* container, | |
| 180 gboolean has_window) { | |
| 181 g_return_if_fail(GTK_IS_EXPANDED_CONTAINER(container)); | |
| 182 g_return_if_fail(!GTK_WIDGET_REALIZED(container)); | |
| 183 gtk_fixed_set_has_window(GTK_FIXED(container), has_window); | |
| 184 } | |
| 185 | |
| 186 gboolean gtk_expanded_container_get_has_window( | |
| 187 GtkExpandedContainer* container) { | |
| 188 g_return_val_if_fail(GTK_IS_EXPANDED_CONTAINER(container), FALSE); | |
| 189 return gtk_fixed_get_has_window(GTK_FIXED(container)); | |
| 190 } | |
| 191 | |
| 192 G_END_DECLS | |
| OLD | NEW |