* configure.ac: 0.12.0
[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 #include <gtk/gtk.h>
26 #include "hildon-defines.h"
27
28 HildonIconSizes *hildoniconsizes = NULL; /* FIXME: could be const */
29 HildonIconSizes hildoninternaliconsizes; /* FIXME: should be static */
30
31 /**
32  * hildon_icon_sizes_init:
33  * 
34  * Initializes the icon sizes. This is automatically
35  * called when the icon sizes have not been initialized
36  * and one is requested.
37  **/
38 void hildon_icon_sizes_init (void)
39 {
40   if (hildoniconsizes != NULL)
41     return;
42   
43   hildoniconsizes = &hildoninternaliconsizes;
44   
45   hildoniconsizes->icon_size_list = gtk_icon_size_register ("hildon_icon_size_list", 64, 64);
46   hildoniconsizes->icon_size_small = gtk_icon_size_register ("*icon_size_small", 26, 26);
47   hildoniconsizes->icon_size_toolbar = gtk_icon_size_register ("icon_size_toolbar", 26, 26);
48   hildoniconsizes->icon_size_widg = gtk_icon_size_register ("icon_size_widg", 26, 26);
49   hildoniconsizes->icon_size_widg_wizard = gtk_icon_size_register ("icon_size_widg_wizard", 50, 50);
50   hildoniconsizes->icon_size_grid = gtk_icon_size_register ("icon_size_grid", 64, 64);
51   hildoniconsizes->icon_size_big_note = gtk_icon_size_register ("icon_size_big_note", 50, 50);
52   hildoniconsizes->icon_size_note = gtk_icon_size_register ("icon_size_note", 26, 26);
53   hildoniconsizes->icon_size_statusbar = gtk_icon_size_register ("icon_size_statusbar", 40, 40);
54   hildoniconsizes->icon_size_indi_video_player_pre_roll = gtk_icon_size_register ("icon_size_indi_video_player_pre_roll", 64, 64);
55   hildoniconsizes->icon_size_indi_key_pad_lock = gtk_icon_size_register ("icon_size_indi_key_pad_lock", 50, 50);
56   hildoniconsizes->icon_size_indi_copy = gtk_icon_size_register ("icon_size_indi_copy", 64, 64);
57   hildoniconsizes->icon_size_indi_delete = gtk_icon_size_register ("icon_size_indi_delete", 64, 64);
58   hildoniconsizes->icon_size_indi_process = gtk_icon_size_register ("icon_size_indi_process", 64, 64);
59   hildoniconsizes->icon_size_indi_progressball = gtk_icon_size_register ("icon_size_indi_progressball", 64, 64);
60   hildoniconsizes->icon_size_indi_send = gtk_icon_size_register ("icon_size_indi_send", 64, 64);
61   hildoniconsizes->icon_size_indi_offmode_charging = gtk_icon_size_register ("icon_size_indi_offmode_charging", 50, 50);
62   hildoniconsizes->icon_size_indi_tap_and_hold = gtk_icon_size_register ("icon_size_indi_tap_and_hold", 34, 34);
63   hildoniconsizes->icon_size_indi_send_receive = gtk_icon_size_register ("icon_size_indi_send_receive", 64, 64);
64   hildoniconsizes->icon_size_indi_wlan_strength = gtk_icon_size_register ("icon_size_indi_wlan_strength", 64, 64);
65   
66   hildoniconsizes->image_size_indi_nokia_logo = gtk_icon_size_register ("image_size_indi_nokia_logo", 64, 64);
67   hildoniconsizes->image_size_indi_startup_failed = gtk_icon_size_register ("image_size_indi_startup_failed", 64, 64);
68   hildoniconsizes->image_size_indi_startup_nokia_logo = gtk_icon_size_register ("image_size_indi_startup_nokia_logo", 64, 64);
69   hildoniconsizes->image_size_indi_nokia_hands = gtk_icon_size_register ("image_size_indi_nokia_hands", 64, 64);
70 }
71
72 typedef struct _HildonLogicalData HildonLogicalData;
73
74 struct _HildonLogicalData
75 {
76   GtkRcFlags rcflags;
77   GtkStateType state;
78   gchar *logicalcolorstring;
79   gchar *logicalfontstring;
80 };
81
82
83 static void hildon_change_style_recursive_from_ld (GtkWidget *widget, GtkStyle *prev_style, HildonLogicalData *ld)
84 {
85   /* Change the style for child widgets */
86   if (GTK_IS_CONTAINER (widget))
87     gtk_container_forall (GTK_CONTAINER (widget), (GtkCallback) (hildon_change_style_recursive_from_ld), ld);
88
89   /* gtk_widget_modify_*() emit "style_set" signals, so if we got here from
90      "style_set" signal, we need to block this function from being called
91      again or we get into inifinite loop.
92
93      Compiling with gcc > 3.3 and -pedantic won't allow conversion between
94      function and object pointers. GLib API however requires an object pointer
95      for a function, so we have to work around this. See
96      see http://bugzilla.gnome.org/show_bug.cgi?id=310175
97
98      FIXME: Use G_GNUC_EXTENSION instead of our own ifdef
99   */
100 #ifdef __GNUC__
101   __extension__
102 #endif
103   g_signal_handlers_block_matched (G_OBJECT (widget), G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC,
104                                    g_signal_lookup ("style_set", G_TYPE_FROM_INSTANCE (widget)),
105                                    0, NULL,
106                                    (gpointer) hildon_change_style_recursive_from_ld,
107                                    NULL);
108    
109   if (ld->logicalcolorstring != NULL)
110     {
111       /* Changing logical color */
112       GdkColor color;
113       if (gtk_style_lookup_logical_color (widget->style, ld->logicalcolorstring, &color) == TRUE)
114         switch (ld->rcflags)
115           {
116             case GTK_RC_FG:
117               gtk_widget_modify_fg (widget, ld->state, &color);
118               break;
119             case GTK_RC_BG:
120               gtk_widget_modify_bg (widget, ld->state, &color);
121               break;
122             case GTK_RC_TEXT:
123               gtk_widget_modify_text (widget, ld->state, &color);
124               break;
125             case GTK_RC_BASE:
126               gtk_widget_modify_base (widget, ld->state, &color);
127               break;
128          }
129     }
130
131   if (ld->logicalfontstring != NULL)
132     {
133       /* Changing logical font */
134       GtkStyle *fontstyle = gtk_rc_get_style_by_paths (gtk_settings_get_default (), ld->logicalfontstring, NULL, G_TYPE_NONE);
135       if (fontstyle != NULL)
136         {
137           PangoFontDescription *fontdesc = fontstyle->font_desc;
138           
139           if (fontdesc != NULL)
140             gtk_widget_modify_font (widget, fontdesc);
141         }
142     }
143    
144
145   /* Compilation workaround for gcc > 3.3 + -pedantic again */
146 #ifdef __GNUC__
147   __extension__
148 #endif
149   g_signal_handlers_unblock_matched (G_OBJECT (widget), G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC,
150                                    g_signal_lookup ("style_set", G_TYPE_FROM_INSTANCE (widget)),
151                                    0, NULL,
152                                    (gpointer) hildon_change_style_recursive_from_ld,
153                                    NULL);
154 }
155
156 /**
157  * hildon_gtk_widget_set_logical_font:
158  * @widget : A @GtkWidget to assign this logical font for.
159  * @logicalfontname : A gchar* with the logical font name to assign to the widget with an "osso-" -prefix.
160  * 
161  * This function assigns a defined logical font to the @widget and all its child widgets.
162  * 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.
163  * The returned signal id can be used to disconnect the signal.
164  * 
165  * Return value : The signal id that is triggered every time theme is changed. 0 if font set failed.
166  **/
167 gulong hildon_gtk_widget_set_logical_font (GtkWidget *widget, gchar *logicalfontname)
168 {
169   HildonLogicalData *ld;
170   gulong signum = 0;
171    
172   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
173   g_return_val_if_fail (logicalfontname != NULL, 0);
174    
175   ld = g_malloc (sizeof (HildonLogicalData));
176
177   ld->rcflags = 0;
178   ld->state = 0;
179   ld->logicalcolorstring = NULL;
180   ld->logicalfontstring = logicalfontname; /* FIXME: g_strdup() */
181
182   /* Change the font now */
183   hildon_change_style_recursive_from_ld (widget, NULL, ld);
184
185   /* Connect to "style_set" so that the font gets changed whenever theme
186      changes. FIXME: if this function is called multiple times, the old signal
187      handler should be disconnected */
188   signum = g_signal_connect_data (G_OBJECT (widget), "style_set", G_CALLBACK (hildon_change_style_recursive_from_ld), ld, (GClosureNotify) g_free, 0);
189
190   return signum;
191 }
192
193 /**
194  * hildon_gtk_widget_set_logical_color:
195  * @widget : A @GtkWidget to assign this logical font for.
196  * @rcflags : @GtkRcFlags enumeration defining whether to assign to FG, BG, TEXT or BASE style.
197  * @state : @GtkStateType indicating to which state to assign the logical color
198  * @logicalcolorname : A gchar* with the logical font name to assign to the widget.
199  * 
200  * This function assigns a defined logical color to the @widget and all it's child widgets.
201  * 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.
202  * The returned signal id can be used to disconnect the signal.
203  * 
204  * Example : If the style you want to modify is bg[NORMAL] then set rcflags to GTK_RC_BG and state to GTK_STATE_NORMAL.
205  * 
206  * Return value : The signal id that is triggered every time theme is changed. 0 if color set failed.
207  **/
208 gulong hildon_gtk_widget_set_logical_color (GtkWidget *widget, GtkRcFlags rcflags,
209                                     GtkStateType state, gchar *logicalcolorname)
210 {
211   HildonLogicalData *ld;
212   gulong signum = 0;
213   
214   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
215   g_return_val_if_fail (logicalcolorname != NULL, 0);
216   
217   ld = g_malloc (sizeof (HildonLogicalData));
218
219   ld->rcflags = rcflags;
220   ld->state = state;
221   ld->logicalcolorstring = logicalcolorname; /* FIXME: g_strdup() */
222   ld->logicalfontstring = NULL;
223   
224   /* Change the colors now */
225   hildon_change_style_recursive_from_ld (widget, NULL, ld);
226
227   /* Connect to "style_set" so that the colors gets changed whenever theme
228      changes. FIXME: if this function is called multiple times, the old signal
229      handler should be disconnected */
230   signum = g_signal_connect_data (G_OBJECT (widget), "style_set", G_CALLBACK (hildon_change_style_recursive_from_ld), ld, (GClosureNotify) g_free, 0);
231
232   return signum;
233 }