1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 */
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is EGE Output Action.
18 *
19 * The Initial Developer of the Original Code is
20 * Jon A. Cruz.
21 * Portions created by the Initial Developer are Copyright (C) 2007
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
40 /* Note: this file should be kept compilable as both .cpp and .c */
42 #include <string.h>
44 #include <gtk/gtkhbox.h>
45 #include <gtk/gtklabel.h>
46 #include <gtk/gtktoolitem.h>
48 #include "ege-output-action.h"
51 static void ege_output_action_class_init( EgeOutputActionClass* klass );
52 static void ege_output_action_init( EgeOutputAction* action );
53 static void ege_output_action_get_property( GObject* obj, guint propId, GValue* value, GParamSpec * pspec );
54 static void ege_output_action_set_property( GObject* obj, guint propId, const GValue *value, GParamSpec* pspec );
55 static void fixup_labels( GObject *gobject, GParamSpec *arg1, gpointer user_data );
57 /* static GtkWidget* create_menu_item( GtkAction* action ); */
58 static GtkWidget* create_tool_item( GtkAction* action );
60 static GtkActionClass* gParentClass = 0;
63 struct _EgeOutputActionPrivate
64 {
65 gboolean useMarkup;
66 };
68 #define EGE_OUTPUT_ACTION_GET_PRIVATE( o ) ( G_TYPE_INSTANCE_GET_PRIVATE( (o), EGE_OUTPUT_ACTION_TYPE, EgeOutputActionPrivate ) )
70 enum {
71 PROP_USE_MARKUP = 1,
72 };
74 GType ege_output_action_get_type( void )
75 {
76 static GType myType = 0;
77 if ( !myType ) {
78 static const GTypeInfo myInfo = {
79 sizeof( EgeOutputActionClass ),
80 NULL, /* base_init */
81 NULL, /* base_finalize */
82 (GClassInitFunc)ege_output_action_class_init,
83 NULL, /* class_finalize */
84 NULL, /* class_data */
85 sizeof( EgeOutputAction ),
86 0, /* n_preallocs */
87 (GInstanceInitFunc)ege_output_action_init,
88 NULL
89 };
91 myType = g_type_register_static( GTK_TYPE_ACTION, "EgeOutputAction", &myInfo, (GTypeFlags)0 );
92 }
94 return myType;
95 }
97 void ege_output_action_class_init( EgeOutputActionClass* klass )
98 {
99 if ( klass ) {
100 GObjectClass* objClass = G_OBJECT_CLASS( klass );
101 gParentClass = GTK_ACTION_CLASS( g_type_class_peek_parent( klass ) );
103 objClass->get_property = ege_output_action_get_property;
104 objClass->set_property = ege_output_action_set_property;
106 /* klass->parent_class.create_menu_item = create_menu_item; */
107 klass->parent_class.create_tool_item = create_tool_item;
109 g_object_class_install_property( objClass,
110 PROP_USE_MARKUP,
111 g_param_spec_boolean( "use-markup",
112 "UseMarkup",
113 "If markup should be used",
114 FALSE,
115 (GParamFlags)(G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT) ) );
117 g_type_class_add_private( klass, sizeof(EgeOutputActionClass) );
118 }
119 }
122 void ege_output_action_init( EgeOutputAction* action )
123 {
124 action->private_data = EGE_OUTPUT_ACTION_GET_PRIVATE( action );
125 action->private_data->useMarkup = FALSE;
127 g_signal_connect( action, "notify", G_CALLBACK( fixup_labels ), NULL );
128 }
130 EgeOutputAction* ege_output_action_new( const gchar *name,
131 const gchar *label,
132 const gchar *tooltip,
133 const gchar *stock_id )
134 {
135 GObject* obj = (GObject*)g_object_new( EGE_OUTPUT_ACTION_TYPE,
136 "name", name,
137 "label", label,
138 "tooltip", tooltip,
139 "stock_id", stock_id,
140 "use-markup", FALSE,
141 NULL );
143 EgeOutputAction* action = EGE_OUTPUT_ACTION( obj );
145 return action;
146 }
148 gboolean ege_output_action_get_use_markup( EgeOutputAction* action )
149 {
150 g_return_val_if_fail( IS_EGE_OUTPUT_ACTION(action), FALSE );
152 return action->private_data->useMarkup;
153 }
155 void ege_output_action_set_use_markup( EgeOutputAction* action, gboolean setting )
156 {
157 g_object_set( G_OBJECT(action), "use-markup", setting, NULL );
158 }
160 void ege_output_action_get_property( GObject* obj, guint propId, GValue* value, GParamSpec * pspec )
161 {
162 EgeOutputAction* action = EGE_OUTPUT_ACTION( obj );
163 switch ( propId ) {
164 case PROP_USE_MARKUP:
165 g_value_set_boolean( value, action->private_data->useMarkup );
166 break;
168 default:
169 G_OBJECT_WARN_INVALID_PROPERTY_ID( obj, propId, pspec );
170 }
171 }
173 void ege_output_action_set_property( GObject* obj, guint propId, const GValue *value, GParamSpec* pspec )
174 {
175 EgeOutputAction* action = EGE_OUTPUT_ACTION( obj );
176 switch ( propId ) {
177 case PROP_USE_MARKUP:
178 {
179 action->private_data->useMarkup = g_value_get_boolean( value );
180 }
181 break;
183 default:
184 G_OBJECT_WARN_INVALID_PROPERTY_ID( obj, propId, pspec );
185 }
186 }
189 /* static GtkWidget* create_menu_item( GtkAction* action ) */
191 GtkWidget* create_tool_item( GtkAction* action )
192 {
193 GtkWidget* item = 0;
195 if ( IS_EGE_OUTPUT_ACTION(action) )
196 {
197 GValue value;
198 GtkWidget* hb = gtk_hbox_new( FALSE, 5 );
199 GtkWidget* lbl = 0;
200 memset( &value, 0, sizeof(value) );
202 g_value_init( &value, G_TYPE_STRING );
203 g_object_get_property( G_OBJECT(action), "short_label", &value );
204 const gchar* sss = g_value_get_string( &value );
206 item = GTK_WIDGET( gtk_tool_item_new() );
208 lbl = gtk_label_new( " " );
209 gtk_container_add( GTK_CONTAINER(hb), lbl );
211 if ( EGE_OUTPUT_ACTION(action)->private_data->useMarkup ) {
212 lbl = gtk_label_new(NULL);
213 gtk_label_set_markup( GTK_LABEL(lbl), sss ? sss : " " );
214 } else {
215 lbl = gtk_label_new( sss ? sss : " " );
216 }
217 gtk_container_add( GTK_CONTAINER(hb), lbl );
219 lbl = gtk_label_new( " " );
220 gtk_container_add( GTK_CONTAINER(hb), lbl );
222 gtk_container_add( GTK_CONTAINER(item), hb );
224 gtk_widget_show_all( item );
225 } else {
226 item = gParentClass->create_tool_item( action );
227 }
229 return item;
230 }
232 void fixup_labels( GObject *gobject, GParamSpec *arg1, gpointer user_data )
233 {
234 /* TODO: handle 'use-markup' getting changed also */
236 if ( arg1 && arg1->name && (strcmp("label", arg1->name) == 0) ) {
237 GSList* proxies = gtk_action_get_proxies( GTK_ACTION(gobject) );
238 gchar* str = 0;
239 g_object_get( gobject, "label", &str, NULL );
240 (void)user_data;
241 while ( proxies ) {
242 if ( GTK_IS_TOOL_ITEM(proxies->data) ) {
243 /* Search for the things we built up in create_tool_item() */
244 GList* children = gtk_container_get_children( GTK_CONTAINER(proxies->data) );
245 if ( children && children->data ) {
246 if ( GTK_IS_HBOX(children->data) ) {
247 children = gtk_container_get_children( GTK_CONTAINER(children->data) );
248 if ( children && g_list_next(children) ) {
249 GtkWidget* child = GTK_WIDGET( g_list_next(children)->data );
250 if ( GTK_IS_LABEL(child) ) {
251 GtkLabel* lbl = GTK_LABEL(child);
252 if ( EGE_OUTPUT_ACTION(gobject)->private_data->useMarkup ) {
253 gtk_label_set_markup( lbl, str );
254 } else {
255 gtk_label_set_text( lbl, str );
256 }
257 }
258 }
259 }
260 }
261 }
262 proxies = g_slist_next( proxies );
263 }
264 g_free( str );
265 }
266 }