* doc/tmpl/* * hildon-widgets/* moved widget descriptions to respective source file...
[hildon] / hildon-widgets / hildon-defines.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Luc Pionchon <luc.pionchon@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-defines
27  * @short_description: Not a widget, just a helper
28  *
29  */
30
31 #include <gtk/gtk.h>
32 #include "hildon-defines.h"
33
34 const  HildonIconSizes *hildoniconsizes = NULL;
35 static HildonIconSizes iis; /* hildon internal icon sizes */
36
37 /**
38  * hildon_icon_sizes_init:
39  * 
40  * Initializes the icon sizes. This is automatically
41  * called when the icon sizes have not been initialized
42  * and one is requested.
43  **/
44 void hildon_icon_sizes_init (void)
45 {
46   if (hildoniconsizes != NULL)
47     return;
48   
49   hildoniconsizes = &iis;
50   
51   iis.icon_size_list                       = gtk_icon_size_register ("hildon_icon_size_list",                64, 64);
52   iis.icon_size_small                      = gtk_icon_size_register ("*icon_size_small",                     26, 26);
53   iis.icon_size_toolbar                    = gtk_icon_size_register ("icon_size_toolbar",                    26, 26);
54   iis.icon_size_widg                       = gtk_icon_size_register ("icon_size_widg",                       26, 26);
55   iis.icon_size_widg_wizard                = gtk_icon_size_register ("icon_size_widg_wizard",                50, 50);
56   iis.icon_size_grid                       = gtk_icon_size_register ("icon_size_grid",                       64, 64);
57   iis.icon_size_big_note                   = gtk_icon_size_register ("icon_size_big_note",                   50, 50);
58   iis.icon_size_note                       = gtk_icon_size_register ("icon_size_note",                       26, 26);
59   iis.icon_size_statusbar                  = gtk_icon_size_register ("icon_size_statusbar",                  40, 40);
60   iis.icon_size_indi_video_player_pre_roll = gtk_icon_size_register ("icon_size_indi_video_player_pre_roll", 64, 64);
61   iis.icon_size_indi_key_pad_lock          = gtk_icon_size_register ("icon_size_indi_key_pad_lock",          50, 50);
62   iis.icon_size_indi_copy                  = gtk_icon_size_register ("icon_size_indi_copy",                  64, 64);
63   iis.icon_size_indi_delete                = gtk_icon_size_register ("icon_size_indi_delete",                64, 64);
64   iis.icon_size_indi_process               = gtk_icon_size_register ("icon_size_indi_process",               64, 64);
65   iis.icon_size_indi_progressball          = gtk_icon_size_register ("icon_size_indi_progressball",          64, 64);
66   iis.icon_size_indi_send                  = gtk_icon_size_register ("icon_size_indi_send",                  64, 64);
67   iis.icon_size_indi_offmode_charging      = gtk_icon_size_register ("icon_size_indi_offmode_charging",      50, 50);
68   iis.icon_size_indi_tap_and_hold          = gtk_icon_size_register ("icon_size_indi_tap_and_hold",          34, 34);
69   iis.icon_size_indi_send_receive          = gtk_icon_size_register ("icon_size_indi_send_receive",          64, 64);
70   iis.icon_size_indi_wlan_strength         = gtk_icon_size_register ("icon_size_indi_wlan_strength",         64, 64);
71
72   iis.image_size_indi_nokia_logo           = gtk_icon_size_register ("image_size_indi_nokia_logo",           64, 64);
73   iis.image_size_indi_startup_failed       = gtk_icon_size_register ("image_size_indi_startup_failed",       64, 64);
74   iis.image_size_indi_startup_nokia_logo   = gtk_icon_size_register ("image_size_indi_startup_nokia_logo",   64, 64);
75   iis.image_size_indi_nokia_hands          = gtk_icon_size_register ("image_size_indi_nokia_hands",          64, 64);
76 }
77
78 typedef struct _HildonLogicalData HildonLogicalData;
79
80 struct _HildonLogicalData
81 {
82   GtkRcFlags rcflags;
83   GtkStateType state;
84   gchar *logicalcolorstring;
85   gchar *logicalfontstring;
86 };
87
88
89 static void hildon_change_style_recursive_from_ld (GtkWidget *widget, GtkStyle *prev_style, HildonLogicalData *ld)
90 {
91   /* Change the style for child widgets */
92   if (GTK_IS_CONTAINER (widget))
93     gtk_container_forall (GTK_CONTAINER (widget), (GtkCallback) (hildon_change_style_recursive_from_ld), ld);
94
95   /* gtk_widget_modify_*() emit "style_set" signals, so if we got here from
96      "style_set" signal, we need to block this function from being called
97      again or we get into inifinite loop.
98
99      FIXME: Compiling with gcc > 3.3 and -pedantic won't allow
100      conversion between function and object pointers. GLib API however
101      requires an object pointer for a function, so we have to work
102      around this.
103      See http://bugzilla.gnome.org/show_bug.cgi?id=310175
104   */
105   G_GNUC_EXTENSION
106   g_signal_handlers_block_matched (G_OBJECT (widget), G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC,
107                                    g_signal_lookup ("style_set", G_TYPE_FROM_INSTANCE (widget)),
108                                    0, NULL,
109                                    (gpointer) hildon_change_style_recursive_from_ld,
110                                    NULL);
111    
112   if (ld->logicalcolorstring != NULL)
113     {
114       /* Changing logical color */
115       GdkColor color;
116       if (gtk_style_lookup_logical_color (widget->style, ld->logicalcolorstring, &color) == TRUE)
117         switch (ld->rcflags)
118           {
119             case GTK_RC_FG:
120               gtk_widget_modify_fg (widget, ld->state, &color);
121               break;
122             case GTK_RC_BG:
123               gtk_widget_modify_bg (widget, ld->state, &color);
124               break;
125             case GTK_RC_TEXT:
126               gtk_widget_modify_text (widget, ld->state, &color);
127               break;
128             case GTK_RC_BASE:
129               gtk_widget_modify_base (widget, ld->state, &color);
130               break;
131          }
132     }
133
134   if (ld->logicalfontstring != NULL)
135     {
136       /* Changing logical font */
137       GtkStyle *fontstyle = gtk_rc_get_style_by_paths (gtk_settings_get_default (), ld->logicalfontstring, NULL, G_TYPE_NONE);
138       if (fontstyle != NULL)
139         {
140           PangoFontDescription *fontdesc = fontstyle->font_desc;
141           
142           if (fontdesc != NULL)
143             gtk_widget_modify_font (widget, fontdesc);
144         }
145     }
146    
147
148   /* FIXME: Compilation workaround for gcc > 3.3 + -pedantic again */
149   G_GNUC_EXTENSION
150   g_signal_handlers_unblock_matched (G_OBJECT (widget), G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC,
151                                    g_signal_lookup ("style_set", G_TYPE_FROM_INSTANCE (widget)),
152                                    0, NULL,
153                                    (gpointer) hildon_change_style_recursive_from_ld,
154                                    NULL);
155 }
156
157 static void hildon_logical_data_free (HildonLogicalData *ld)
158 {
159   g_free(ld->logicalcolorstring);
160   g_free(ld->logicalfontstring);
161   g_free(ld);
162 }
163
164 /**
165  * hildon_gtk_widget_set_logical_font:
166  * @widget : A @GtkWidget to assign this logical font for.
167  * @logicalfontname : A gchar* with the logical font name to assign to the widget with an "osso-" -prefix.
168  * 
169  * This function assigns a defined logical font to the @widget and all its child widgets.
170  * It also connects to the "style_set" signal which will retrieve & assign the new font for the given logical name each time the theme is changed.
171  * The returned signal id can be used to disconnect the signal.
172  * 
173  * Return value : The signal id that is triggered every time theme is changed. 0 if font set failed.
174  **/
175 gulong hildon_gtk_widget_set_logical_font (GtkWidget *widget, const gchar *logicalfontname)
176 {
177   HildonLogicalData *ld;
178   gulong signum = 0;
179    
180   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
181   g_return_val_if_fail (logicalfontname != NULL, 0);
182    
183   ld = g_malloc (sizeof (HildonLogicalData));
184
185   ld->rcflags = 0;
186   ld->state = 0;
187   ld->logicalcolorstring = NULL;
188   ld->logicalfontstring = g_strdup(logicalfontname);
189
190   /* Change the font now */
191   hildon_change_style_recursive_from_ld (widget, NULL, ld);
192
193   /* Connect to "style_set" so that the font gets changed whenever theme changes.
194
195      FIXME: if this function is called multiple times, the old signal
196      handler should be disconnected. However since signal ID is
197      returned, this probably can't be done without breaking backwards
198      compatibility. */
199   signum = g_signal_connect_data (G_OBJECT (widget), "style_set",
200                                   G_CALLBACK (hildon_change_style_recursive_from_ld),
201                                   ld, (GClosureNotify) hildon_logical_data_free, 0);
202
203   return signum;
204 }
205
206 /**
207  * hildon_gtk_widget_set_logical_color:
208  * @widget : A @GtkWidget to assign this logical font for.
209  * @rcflags : @GtkRcFlags enumeration defining whether to assign to FG, BG, TEXT or BASE style.
210  * @state : @GtkStateType indicating to which state to assign the logical color
211  * @logicalcolorname : A gchar* with the logical font name to assign to the widget.
212  * 
213  * This function assigns a defined logical color to the @widget and all it's child widgets.
214  * It also connects to the "style_set" signal which will retrieve & assign the new color for the given logical name each time the theme is changed.
215  * The returned signal id can be used to disconnect the signal.
216  * 
217  * Example : If the style you want to modify is bg[NORMAL] then set rcflags to GTK_RC_BG and state to GTK_STATE_NORMAL.
218  * 
219  * Return value : The signal id that is triggered every time theme is changed. 0 if color set failed.
220  **/
221 gulong hildon_gtk_widget_set_logical_color (GtkWidget *widget, GtkRcFlags rcflags,
222                                     GtkStateType state, const gchar *logicalcolorname)
223 {
224   HildonLogicalData *ld;
225   gulong signum = 0;
226   
227   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
228   g_return_val_if_fail (logicalcolorname != NULL, 0);
229   
230   ld = g_malloc (sizeof (HildonLogicalData));
231
232   ld->rcflags = rcflags;
233   ld->state = state;
234   ld->logicalcolorstring = g_strdup(logicalcolorname);
235   ld->logicalfontstring = NULL;
236   
237   /* Change the colors now */
238   hildon_change_style_recursive_from_ld (widget, NULL, ld);
239
240   /* Connect to "style_set" so that the colors gets changed whenever theme
241      changes. FIXME: same as above */
242   signum = g_signal_connect_data (G_OBJECT (widget), "style_set",
243                                   G_CALLBACK (hildon_change_style_recursive_from_ld),
244                                   ld, (GClosureNotify) hildon_logical_data_free, 0);
245
246   return signum;
247 }