1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/*
* panel-test-applets.c:
*
* Authors:
* Mark McLoughlin <mark@skynet.ie>
*
* Copyright 2002 Sun Microsystems, Inc.
*/
#include <config.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <matecomponent/matecomponent-exception.h>
#include <matecomponent/matecomponent-main.h>
#include <matecomponent/matecomponent-widget.h>
#include <mateconf/mateconf.h>
#include "mate-panel-applet.h"
G_GNUC_UNUSED void on_execute_button_clicked (GtkButton *button, gpointer dummy);
static GtkWidget *win = NULL;
static GtkWidget *applet_combo = NULL;
static GtkWidget *prefs_dir_entry = NULL;
static GtkWidget *orient_combo = NULL;
static GtkWidget *size_combo = NULL;
static char *cli_iid = NULL;
static char *cli_prefs_dir = NULL;
static char *cli_size = NULL;
static char *cli_orient = NULL;
static const GOptionEntry options [] = {
{ "iid", 0, 0, G_OPTION_ARG_STRING, &cli_iid, N_("Specify an applet IID to load"), NULL},
{ "prefs-dir", 0, 0, G_OPTION_ARG_STRING, &cli_prefs_dir, N_("Specify a mateconf location in which the applet preferences should be stored"), NULL},
{ "size", 0, 0, G_OPTION_ARG_STRING, &cli_size, N_("Specify the initial size of the applet (xx-small, medium, large etc.)"), NULL},
{ "orient", 0, 0, G_OPTION_ARG_STRING, &cli_orient, N_("Specify the initial orientation of the applet (top, bottom, left or right)"), NULL},
{ NULL}
};
enum {
COLUMN_TEXT,
COLUMN_ITEM,
NUMBER_COLUMNS
};
typedef struct {
const char *name;
const char *value;
} ComboItem;
static ComboItem orient_items [] = {
{ NC_("Orientation", "Top"), "top" },
{ NC_("Orientation", "Bottom"), "bottom" },
{ NC_("Orientation", "Left"), "left" },
{ NC_("Orientation", "Right"), "right" }
};
static ComboItem size_items [] = {
{ NC_("Size", "XX Small"), "xx-small" },
{ NC_("Size", "X Small"), "x-small" },
{ NC_("Size", "Small"), "small" },
{ NC_("Size", "Medium"), "medium" },
{ NC_("Size", "Large"), "large" },
{ NC_("Size", "X Large"), "x-large" },
{ NC_("Size", "XX Large"), "xx-large" }
};
static char *
get_combo_value (GtkWidget *combo_box)
{
GtkTreeIter iter;
GtkTreeModel *model;
char *value;
if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
return NULL;
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
gtk_tree_model_get (model, &iter, COLUMN_ITEM, &value, -1);
return value;
}
static char *
construct_moniker (void)
{
const char *prefs_key;
char *iid;
char *size;
char *orient;
char *ret_value;
iid = get_combo_value (applet_combo);
g_assert (iid != NULL);
size = get_combo_value (size_combo);
g_assert (size != NULL);
orient = get_combo_value (orient_combo);
g_assert (orient != NULL);
prefs_key = gtk_entry_get_text (GTK_ENTRY (prefs_dir_entry));
ret_value= g_strdup_printf ("%s!prefs_key=%s;size=%s;orient=%s",
iid, prefs_key, size, orient);
g_free (iid);
g_free (size);
g_free (orient);
return ret_value;
}
static void
load_applet_into_window (const char *moniker,
const char *title)
{
GtkWidget *applet_window;
GtkWidget *applet;
applet = matecomponent_widget_new_control (moniker, NULL);
if (!applet) {
GtkWidget *dialog;
dialog = gtk_message_dialog_new (win ? GTK_WINDOW (win) : NULL,
GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("Failed to load applet %s"),
title);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
return;
}
applet_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (applet);
gtk_container_add (GTK_CONTAINER (applet_window), applet);
//FIXME: we could set the window icon with the applet icon
gtk_window_set_title (GTK_WINDOW (applet_window), title);
gtk_widget_show (applet_window);
}
static void
load_applet_from_command_line (void)
{
GString *str;
g_assert (cli_iid != NULL);
str = g_string_new (cli_iid);
if (cli_prefs_dir || cli_size || cli_orient) {
g_string_append_c (str, '!');
if (cli_prefs_dir)
g_string_append_printf (str, "prefs_key=%s", cli_prefs_dir);
g_string_append_c (str, ';');
if (cli_size)
g_string_append_printf (str, "size=%s", cli_size);
g_string_append_c (str, ';');
if (cli_orient)
g_string_append_printf (str, "orient=%s", cli_orient);
}
g_print ("Loading %s\n", str->str);
load_applet_into_window (str->str, cli_iid);
g_string_free (str, TRUE);
}
G_GNUC_UNUSED void
on_execute_button_clicked (GtkButton *button,
gpointer dummy)
{
char *moniker;
char *title;
moniker = construct_moniker ();
title = get_combo_value (applet_combo);
load_applet_into_window (moniker, title);
g_free (moniker);
g_free (title);
}
static void
setup_combo (GtkWidget *combo_box,
ComboItem *items,
const char *context,
int nb_items,
gboolean dynamic)
{
GtkListStore *model;
GtkTreeIter iter;
GtkCellRenderer *renderer;
int i;
model = gtk_list_store_new (NUMBER_COLUMNS,
G_TYPE_STRING,
G_TYPE_STRING);
gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box),
GTK_TREE_MODEL (model));
for (i = 0; i < nb_items; i++) {
gtk_list_store_append (model, &iter);
gtk_list_store_set (model, &iter,
COLUMN_TEXT, dynamic ? g_strdup (items [i].name) : g_dpgettext2 (NULL, context, items [i].name),
COLUMN_ITEM, dynamic ? g_strdup (items [i].value) : items [i].value,
-1);
}
renderer = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box),
renderer, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
renderer, "text", COLUMN_TEXT, NULL);
gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
}
static void
setup_options (void)
{
MateComponent_ServerInfoList *applets;
CORBA_Environment env;
int i;
char *prefs_dir;
char *unique_key;
ComboItem *applet_items;
int applet_nb;
CORBA_exception_init (&env);
applets = matecomponent_activation_query (
"has (repo_ids, 'IDL:MATE/Vertigo/MatePanelAppletShell:1.0')",
NULL, &env);
if (MATECOMPONENT_EX (&env))
g_error (_("query returned exception %s\n"), MATECOMPONENT_EX_REPOID (&env));
CORBA_exception_free (&env);
applet_nb = applets->_length;
applet_items = g_new0 (ComboItem, applet_nb);
for (i = 0; i < applet_nb; i++) {
MateComponent_ServerInfo *info;
info = &applets->_buffer [i];
applet_items[i].name = info->iid;
applet_items[i].value = info->iid;
}
setup_combo (applet_combo, applet_items, NULL, applet_nb, TRUE);
g_free (applet_items);
CORBA_free (applets);
setup_combo (size_combo, size_items, "Size",
G_N_ELEMENTS (size_items), FALSE);
setup_combo (orient_combo, orient_items, "Orientation",
G_N_ELEMENTS (orient_items), FALSE);
unique_key = mateconf_unique_key ();
prefs_dir = g_strdup_printf ("/tmp/%s", unique_key);
g_free (unique_key);
gtk_entry_set_text (GTK_ENTRY (prefs_dir_entry), prefs_dir);
g_free (prefs_dir);
}
int
main (int argc, char **argv)
{
GtkBuilder *builder;
char *uifile;
GError *error;
bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
error = NULL;
if (!gtk_init_with_args (&argc, &argv,
"", (GOptionEntry *) options, GETTEXT_PACKAGE,
&error)) {
if (error) {
g_printerr ("%s\n", error->message);
g_error_free (error);
} else
g_printerr ("Cannot initiliaze GTK+.\n");
return 1;
}
if (!matecomponent_init (&argc, argv)) {
g_printerr ("Cannot initialize matecomponent.\n");
return 1;
}
if (cli_iid) {
load_applet_from_command_line ();
gtk_main ();
return 0;
}
builder = gtk_builder_new ();
gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
uifile = MATE_PANEL_APPLET_BUILDERDIR "/panel-test-applets.ui";
gtk_builder_add_from_file (builder, uifile, &error);
if (error) {
g_warning ("Error loading \"%s\": %s", uifile, error->message);
g_error_free (error);
return 1;
}
gtk_builder_connect_signals (builder, NULL);
win = GTK_WIDGET (gtk_builder_get_object (builder,
"toplevel"));
applet_combo = GTK_WIDGET (gtk_builder_get_object (builder,
"applet-combo"));
prefs_dir_entry = GTK_WIDGET (gtk_builder_get_object (builder,
"prefs-dir-entry"));
orient_combo = GTK_WIDGET (gtk_builder_get_object (builder,
"orient-combo"));
size_combo = GTK_WIDGET (gtk_builder_get_object (builder,
"size-combo"));
g_object_unref (builder);
setup_options ();
gtk_widget_show (win);
gtk_main ();
return 0;
}
|