Upgrading the license headers, moving package name to "hildon" etc.
[hildon] / src / hildon-defines.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@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; version 2.1 of
11  * the License.
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 <config.h>
32 #include <gtk/gtk.h>
33 #include "hildon-defines.h"
34
35 const  HildonIconSizes *hildoniconsizes = NULL;
36 static HildonIconSizes iis; /* hildon internal icon sizes */
37
38 /**
39  * hildon_icon_sizes_init:
40  * 
41  * Initializes the icon sizes. This is automatically
42  * called when the icon sizes have not been initialized
43  * and one is requested.
44  **/
45 void hildon_icon_sizes_init (void)
46 {
47   if (hildoniconsizes != NULL)
48     return;
49   
50   hildoniconsizes = &iis;
51   
52   iis.icon_size_list                       = gtk_icon_size_register ("hildon_icon_size_list",                64, 64);
53   iis.icon_size_small                      = gtk_icon_size_register ("*icon_size_small",                     26, 26);
54   iis.icon_size_toolbar                    = gtk_icon_size_register ("icon_size_toolbar",                    26, 26);
55   iis.icon_size_widg                       = gtk_icon_size_register ("icon_size_widg",                       26, 26);
56   iis.icon_size_widg_wizard                = gtk_icon_size_register ("icon_size_widg_wizard",                50, 50);
57   iis.icon_size_grid                       = gtk_icon_size_register ("icon_size_grid",                       64, 64);
58   iis.icon_size_big_note                   = gtk_icon_size_register ("icon_size_big_note",                   50, 50);
59   iis.icon_size_note                       = gtk_icon_size_register ("icon_size_note",                       26, 26);
60   iis.icon_size_statusbar                  = gtk_icon_size_register ("icon_size_statusbar",                  40, 40);
61   iis.icon_size_indi_video_player_pre_roll = gtk_icon_size_register ("icon_size_indi_video_player_pre_roll", 64, 64);
62   iis.icon_size_indi_key_pad_lock          = gtk_icon_size_register ("icon_size_indi_key_pad_lock",          50, 50);
63   iis.icon_size_indi_copy                  = gtk_icon_size_register ("icon_size_indi_copy",                  64, 64);
64   iis.icon_size_indi_delete                = gtk_icon_size_register ("icon_size_indi_delete",                64, 64);
65   iis.icon_size_indi_process               = gtk_icon_size_register ("icon_size_indi_process",               64, 64);
66   iis.icon_size_indi_progressball          = gtk_icon_size_register ("icon_size_indi_progressball",          64, 64);
67   iis.icon_size_indi_send                  = gtk_icon_size_register ("icon_size_indi_send",                  64, 64);
68   iis.icon_size_indi_offmode_charging      = gtk_icon_size_register ("icon_size_indi_offmode_charging",      50, 50);
69   iis.icon_size_indi_tap_and_hold          = gtk_icon_size_register ("icon_size_indi_tap_and_hold",          34, 34);
70   iis.icon_size_indi_send_receive          = gtk_icon_size_register ("icon_size_indi_send_receive",          64, 64);
71   iis.icon_size_indi_wlan_strength         = gtk_icon_size_register ("icon_size_indi_wlan_strength",         64, 64);
72
73   iis.image_size_indi_nokia_logo           = gtk_icon_size_register ("image_size_indi_nokia_logo",           64, 64);
74   iis.image_size_indi_startup_failed       = gtk_icon_size_register ("image_size_indi_startup_failed",       64, 64);
75   iis.image_size_indi_startup_nokia_logo   = gtk_icon_size_register ("image_size_indi_startup_nokia_logo",   64, 64);
76   iis.image_size_indi_nokia_hands          = gtk_icon_size_register ("image_size_indi_nokia_hands",          64, 64);
77 }
78
79 typedef struct _HildonLogicalData HildonLogicalData;
80
81 struct _HildonLogicalData
82 {
83   GtkRcFlags rcflags;
84   GtkStateType state;
85   gchar *logicalcolorstring;
86   gchar *logicalfontstring;
87 };
88
89
90 static void hildon_change_style_recursive_from_ld (GtkWidget *widget, GtkStyle *prev_style, HildonLogicalData *ld)
91 {
92   /* Change the style for child widgets */
93   if (GTK_IS_CONTAINER (widget)) {
94     GList *iterator = gtk_container_get_children (GTK_CONTAINER (widget));
95     for (iterator = iterator; iterator != NULL; iterator = g_list_next (iterator))
96       hildon_change_style_recursive_from_ld (GTK_WIDGET (iterator->data), prev_style, ld);
97   }
98
99   /* gtk_widget_modify_*() emit "style_set" signals, so if we got here from
100      "style_set" signal, we need to block this function from being called
101      again or we get into inifinite loop.
102
103      FIXME: Compiling with gcc > 3.3 and -pedantic won't allow
104      conversion between function and object pointers. GLib API however
105      requires an object pointer for a function, so we have to work
106      around this.
107      See http://bugzilla.gnome.org/show_bug.cgi?id=310175
108   */
109   G_GNUC_EXTENSION
110   g_signal_handlers_block_matched (G_OBJECT (widget), G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC,
111                                    g_signal_lookup ("style_set", G_TYPE_FROM_INSTANCE (widget)),
112                                    0, NULL,
113                                    (gpointer) hildon_change_style_recursive_from_ld,
114                                    NULL);
115   
116   if (ld->logicalcolorstring != NULL)
117     {
118       /* Changing logical color */
119       GdkColor color;
120       gtk_widget_ensure_style (widget);
121       if (gtk_style_lookup_color (widget->style, ld->logicalcolorstring, &color) == TRUE)
122         switch (ld->rcflags)
123           {
124             case GTK_RC_FG:
125               gtk_widget_modify_fg (widget, ld->state, &color);
126               break;
127             case GTK_RC_BG:
128               gtk_widget_modify_bg (widget, ld->state, &color);
129               break;
130             case GTK_RC_TEXT:
131               gtk_widget_modify_text (widget, ld->state, &color);
132               break;
133             case GTK_RC_BASE:
134               gtk_widget_modify_base (widget, ld->state, &color);
135               break;
136          } else 
137          {
138            g_warning ("Failed to lookup '%s' color!", ld->logicalcolorstring);
139          }
140     }
141
142   if (ld->logicalfontstring != NULL)
143     {
144       /* Changing logical font */
145       GtkStyle *fontstyle = gtk_rc_get_style_by_paths (gtk_settings_get_default (), ld->logicalfontstring, NULL, G_TYPE_NONE);
146       if (fontstyle != NULL)
147         {
148           PangoFontDescription *fontdesc = fontstyle->font_desc;
149           
150           if (fontdesc != NULL)
151             gtk_widget_modify_font (widget, fontdesc);
152         }
153     }
154    
155
156   /* FIXME: Compilation workaround for gcc > 3.3 + -pedantic again */
157   G_GNUC_EXTENSION
158   g_signal_handlers_unblock_matched (G_OBJECT (widget), G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC,
159                                    g_signal_lookup ("style_set", G_TYPE_FROM_INSTANCE (widget)),
160                                    0, NULL,
161                                    (gpointer) hildon_change_style_recursive_from_ld,
162                                    NULL);
163 }
164
165 static void hildon_logical_data_free (HildonLogicalData *ld)
166 {
167   g_return_if_fail (ld != NULL);
168   
169   if (ld->logicalcolorstring)
170     g_free(ld->logicalcolorstring);
171   
172   if (ld->logicalfontstring)
173     g_free(ld->logicalfontstring);
174   
175   g_free(ld);
176 }
177
178 /**
179  * hildon_gtk_widget_set_logical_font:
180  * @widget : A @GtkWidget to assign this logical font for.
181  * @logicalfontname : A gchar* with the logical font name to assign to the widget with an "osso-" -prefix.
182  * 
183  * This function assigns a defined logical font to the @widget and all its child widgets.
184  * 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.
185  * The returned signal id can be used to disconnect the signal. 
186  * The previous signal (obtained by calling this function) is disconnected automatically and should not be used. 
187  * 
188  * Return value : The signal id that is triggered every time theme is changed. 0 if font set failed.
189  **/
190 gulong hildon_gtk_widget_set_logical_font (GtkWidget *widget, const gchar *logicalfontname)
191 {
192   HildonLogicalData *ld;
193   gulong signum = 0;
194    
195   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
196   g_return_val_if_fail (logicalfontname != NULL, 0);
197    
198   ld = g_malloc (sizeof (HildonLogicalData));
199
200   ld->rcflags = 0;
201   ld->state = 0;
202   ld->logicalcolorstring = NULL;
203   ld->logicalfontstring = g_strdup(logicalfontname);
204
205   /* Disconnects the previously connected signals. That calls the closure notify
206    * and effectively disposes the allocated data (hildon_logical_data_free) */
207   g_signal_handlers_disconnect_matched (G_OBJECT (widget), G_SIGNAL_MATCH_FUNC, 
208                                         0, 0, NULL, 
209                                         G_CALLBACK (hildon_change_style_recursive_from_ld), NULL);
210
211   /* Change the font now */
212   hildon_change_style_recursive_from_ld (widget, NULL, ld);
213
214   /* Connect to "style_set" so that the font gets changed whenever theme changes. */
215   signum = g_signal_connect_data (G_OBJECT (widget), "style_set",
216                                   G_CALLBACK (hildon_change_style_recursive_from_ld),
217                                   ld, (GClosureNotify) hildon_logical_data_free, 0);
218
219   return signum;
220 }
221
222 /**
223  * hildon_gtk_widget_set_logical_color:
224  * @widget : A @GtkWidget to assign this logical font for.
225  * @rcflags : @GtkRcFlags enumeration defining whether to assign to FG, BG, TEXT or BASE style.
226  * @state : @GtkStateType indicating to which state to assign the logical color
227  * @logicalcolorname : A gchar* with the logical font name to assign to the widget.
228  * 
229  * This function assigns a defined logical color to the @widget and all it's child widgets.
230  * 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.
231  * The returned signal id can be used to disconnect the signal.
232  * The previous signal (obtained by calling this function) is disconnected automatically and should not be used. 
233  * 
234  * Example : If the style you want to modify is bg[NORMAL] then set rcflags to GTK_RC_BG and state to GTK_STATE_NORMAL.
235  * 
236  * Return value : The signal id that is triggered every time theme is changed. 0 if color set failed.
237  **/
238 gulong hildon_gtk_widget_set_logical_color (GtkWidget *widget, GtkRcFlags rcflags,
239                                     GtkStateType state, const gchar *logicalcolorname)
240 {
241   HildonLogicalData *ld;
242   gulong signum = 0;
243   
244   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
245   g_return_val_if_fail (logicalcolorname != NULL, 0);
246  
247   ld = g_malloc (sizeof (HildonLogicalData));
248
249   ld->rcflags = rcflags;
250   ld->state = state;
251   ld->logicalcolorstring = g_strdup(logicalcolorname);
252   ld->logicalfontstring = NULL;
253
254   /* Disconnects the previously connected signals. That calls the closure notify
255    * and effectively disposes the allocated data (hildon_logical_data_free) */
256   g_signal_handlers_disconnect_matched (G_OBJECT (widget), G_SIGNAL_MATCH_FUNC, 
257                                         0, 0, NULL, 
258                                         G_CALLBACK (hildon_change_style_recursive_from_ld), NULL);
259   
260   /* Change the colors now */
261   hildon_change_style_recursive_from_ld (widget, NULL, ld);
262
263   /* Connect to "style_set" so that the colors gets changed whenever theme */
264   signum = g_signal_connect_data (G_OBJECT (widget), "style_set",
265                                   G_CALLBACK (hildon_change_style_recursive_from_ld),
266                                   ld, (GClosureNotify) hildon_logical_data_free, 0);
267
268   return signum;
269 }