most of the 2.X->3.0 commit (up to rev 4299) except for gtk2_ardour/editor_canvas...
[ardour.git] / libs / clearlooks-newer / widget-information.c
1 #include <gtk/gtk.h>
2
3 #include "general-support.h"
4 #include "widget-information.h"
5 #include <math.h>
6 #include <string.h>
7
8 /* Widget Type Lookups/Macros
9    
10    Based on/modified from functions in
11    Smooth-Engine.
12 */ 
13 gboolean
14 ge_object_is_a (const GObject * object, const gchar * type_name)
15 {
16   gboolean result = FALSE;
17  
18   if ((object))
19     {
20       GType tmp = g_type_from_name (type_name);
21
22       if (tmp)
23         result = g_type_check_instance_is_a ((GTypeInstance *) object, tmp);
24     }
25  
26   return result;
27 }
28  
29 gboolean
30 ge_is_combo_box_entry (GtkWidget * widget)
31 {
32   gboolean result = FALSE;
33  
34   if ((widget) && (widget->parent))
35     {
36       if (GE_IS_COMBO_BOX_ENTRY (widget->parent))
37         result = TRUE;
38       else
39         result = ge_is_combo_box_entry (widget->parent);
40     }
41   return result;
42 }
43  
44 static gboolean
45 ge_combo_box_is_using_list (GtkWidget * widget)
46 {
47   gboolean result = FALSE;
48  
49   if (GE_IS_COMBO_BOX (widget))
50     {
51       gboolean *tmp = NULL;
52  
53       gtk_widget_style_get (widget, "appears-as-list", &result, NULL);
54
55       if (tmp)
56         result = *tmp;
57     }
58  
59   return result;
60 }
61  
62 gboolean
63 ge_is_combo_box (GtkWidget * widget, gboolean as_list)
64 {
65   gboolean result = FALSE;
66  
67   if ((widget) && (widget->parent))
68     {
69       if (GE_IS_COMBO_BOX (widget->parent))
70         {
71           if (as_list)
72             result = (ge_combo_box_is_using_list(widget->parent));
73           else
74             result = (!ge_combo_box_is_using_list(widget->parent));
75         }
76       else
77         result = ge_is_combo_box (widget->parent, as_list);
78     }
79   return result;
80 }
81  
82 gboolean
83 ge_is_combo (GtkWidget * widget)
84 {
85   gboolean result = FALSE;
86  
87   if ((widget) && (widget->parent))
88     {
89       if (GE_IS_COMBO (widget->parent))
90         result = TRUE;
91       else
92         result = ge_is_combo (widget->parent);
93     }
94   return result;
95 }
96  
97 gboolean
98 ge_is_in_combo_box (GtkWidget * widget)
99 {
100   return ((ge_is_combo (widget) || ge_is_combo_box (widget, TRUE) || ge_is_combo_box_entry (widget)));
101 }
102  
103 gboolean
104 ge_is_toolbar_item (GtkWidget * widget)
105 {
106   gboolean result = FALSE;
107  
108   if ((widget) && (widget->parent)) {
109     if ((GE_IS_BONOBO_TOOLBAR (widget->parent))
110         || (GE_IS_BONOBO_DOCK_ITEM (widget->parent))
111         || (GE_IS_EGG_TOOLBAR (widget->parent))
112         || (GE_IS_TOOLBAR (widget->parent))
113         || (GE_IS_HANDLE_BOX (widget->parent)))
114       result = TRUE;
115     else
116       result = ge_is_toolbar_item (widget->parent);
117   }
118   return result;
119 }
120  
121 gboolean
122 ge_is_panel_widget_item (GtkWidget * widget)
123 {
124   gboolean result = FALSE;
125  
126   if ((widget) && (widget->parent))
127     {
128       if (GE_IS_PANEL_WIDGET (widget->parent))
129         result = TRUE;
130       else
131         result = ge_is_panel_widget_item (widget->parent);
132     }
133   return result;
134 }
135  
136 gboolean 
137 ge_is_bonobo_dock_item (GtkWidget * widget)
138 {
139   gboolean result = FALSE;
140  
141   if ((widget))
142     {
143       if (GE_IS_BONOBO_DOCK_ITEM(widget) || GE_IS_BONOBO_DOCK_ITEM (widget->parent))
144         result = TRUE;
145       else if (GE_IS_BOX(widget) || GE_IS_BOX(widget->parent))
146         {
147           GtkContainer *box = GE_IS_BOX(widget)?GTK_CONTAINER(widget):GTK_CONTAINER(widget->parent);
148           GList *children = NULL, *child = NULL;
149  
150           children = gtk_container_get_children(box);
151               
152           for (child = g_list_first(children); child; child = g_list_next(child))
153             {
154               if (GE_IS_BONOBO_DOCK_ITEM_GRIP(child->data))
155                 {
156                   result = TRUE;
157                   child = NULL;
158                 }
159             }               
160          
161           if (children)   
162             g_list_free(children);
163         }
164     }
165   return result;
166 }
167
168 static GtkWidget *
169 ge_find_combo_box_entry_widget (GtkWidget * widget)
170 {
171   GtkWidget *result = NULL;
172
173   if (widget)
174     {
175       if (GE_IS_COMBO_BOX_ENTRY (widget))
176         result = widget;
177       else
178         result = ge_find_combo_box_entry_widget (widget->parent);
179     }
180
181   return result;
182 }
183
184 static GtkWidget *
185 ge_find_combo_box_widget (GtkWidget * widget, gboolean as_list)
186 {
187   GtkWidget *result = NULL;
188  
189   if (widget)
190     {
191       if (GE_IS_COMBO_BOX (widget))
192         {
193           if (as_list)
194             result = (ge_combo_box_is_using_list(widget))?widget:NULL;
195           else
196             result = (!ge_combo_box_is_using_list(widget))?widget:NULL;
197         }
198       else
199         result = ge_find_combo_box_widget (widget->parent, as_list);
200     }
201   return result;
202 }
203  
204 static GtkWidget *
205 ge_find_combo_widget (GtkWidget * widget)
206 {
207   GtkWidget *result = NULL;
208  
209   if (widget)
210     {
211       if (GE_IS_COMBO (widget))
212         result = widget;
213       else
214         result = ge_find_combo_widget(widget->parent);
215     }
216   return result;
217 }
218
219 GtkWidget*
220 ge_find_combo_box_widget_parent (GtkWidget * widget)
221 {
222    GtkWidget *result = NULL;
223    
224    if (!result)
225      result = ge_find_combo_widget(widget);
226   
227    if (!result)
228      result = ge_find_combo_box_widget(widget, TRUE);
229
230    if (!result)
231      result = ge_find_combo_box_entry_widget(widget);
232
233   return result;
234 }
235
236 /***********************************************
237  * option_menu_get_props -
238  *  
239  *   Find Option Menu Size and Spacing
240  *
241  *   Taken from Smooth
242  ***********************************************/ 
243 void
244 ge_option_menu_get_props (GtkWidget * widget,
245                        GtkRequisition * indicator_size,
246                        GtkBorder * indicator_spacing)
247 {
248   GtkRequisition default_size = { 9, 5 };
249   GtkBorder default_spacing = { 7, 5, 2, 2 };
250   GtkRequisition *tmp_size = NULL;
251   GtkBorder *tmp_spacing = NULL;
252  
253   if ((widget) && GE_IS_OPTION_MENU(widget))
254     gtk_widget_style_get (widget,
255                           "indicator_size", &tmp_size,
256                           "indicator_spacing", &tmp_spacing, NULL);
257  
258   if (tmp_size)
259     {
260       *indicator_size = *tmp_size;
261       gtk_requisition_free (tmp_size);
262     }
263   else
264     *indicator_size = default_size;
265  
266   if (tmp_spacing)
267     {
268       *indicator_spacing = *tmp_spacing;
269       gtk_border_free (tmp_spacing);
270     }
271   else
272     *indicator_spacing = default_spacing;
273 }
274
275 void
276 ge_button_get_default_border (GtkWidget *widget, 
277                               GtkBorder *border)
278 {
279         GtkBorder default_border = {1, 1, 1, 1};
280         GtkBorder *tmp_border = NULL;
281         
282         if (widget && GE_IS_BUTTON (widget))
283                 gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
284
285         if (tmp_border)
286         {
287                 *border = *tmp_border;
288                 gtk_border_free (tmp_border);
289         }
290         else
291         {
292                 *border = default_border;
293         }
294 }
295
296
297 gboolean
298 ge_widget_is_ltr (GtkWidget *widget)
299 {
300         GtkTextDirection dir = GTK_TEXT_DIR_NONE;
301         
302         if (GE_IS_WIDGET (widget))
303                 dir = gtk_widget_get_direction (widget);
304
305         if (dir == GTK_TEXT_DIR_NONE)
306                 dir = gtk_widget_get_default_direction ();
307
308         if (dir == GTK_TEXT_DIR_RTL)
309                 return FALSE;
310         else
311                 return TRUE;
312 }