~ubuntu-branches/ubuntu/precise/xfwm4/precise-updates

« back to all changes in this revision

Viewing changes to settings-dialogs/workspace-settings.c

  • Committer: Bazaar Package Importer
  • Author(s): Jérôme Guelfucci, Jérôme Guelfucci, Lionel Le Folgoc
  • Date: 2009-01-30 18:28:59 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20090130182859-1tci3n1f1hhppvc2
Tags: 4.5.99.1-0ubuntu1
[ Jérôme Guelfucci ]
* Merge with Debian Xfce UNRELEASED, remaining Ubuntu changes:
  - debian/xfwm4.1: update bug reporting address (LP instead of Debian BTS).

[ Lionel Le Folgoc ]
* debian/control: use our Vcs-* fields.
* Bugs fixed by this new release:
  - "User interface of focused application is covered by another application's
    new window in Xfce" (LP: #250101)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (c) 2008 Brian Tarricone <bjt23@cornell.edu>
 
3
 *  Copyright (c) 2008 Stephan Arts <stephan@xfce.org>
 
4
 *  Copyright (c) 2008 Jannis Pohlmann <jannis@xfce.org>
 
5
 *  Copyright (c) 2008 Mike Massonnet <mmassonnet@xfce.org>
 
6
 *  Copyright (c) 2008 Olivier Fourdan <olivier@xfce.org>
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 2 of the License, or
 
11
 *  (at your option) any later version.
 
12
 *
 
13
 *  This program is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *  GNU Library General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program; if not, write to the Free Software
 
20
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
 
 
24
#include <config.h>
 
25
#include <string.h>
 
26
 
 
27
#include <glib.h>
 
28
#include <gtk/gtk.h>
 
29
#include <glade/glade.h>
 
30
#include <dbus/dbus-glib.h>
 
31
#include <libwnck/libwnck.h>
 
32
 
 
33
#include <libxfce4util/libxfce4util.h>
 
34
#include <libxfcegui4/libxfcegui4.h>
 
35
#include <xfconf/xfconf.h>
 
36
#include "xfwm4-workspace-dialog_glade.h"
 
37
#include "monitor-icon.h"
 
38
 
 
39
#define WORKSPACES_CHANNEL         "xfwm4"
 
40
 
 
41
#define WORKSPACE_NAMES_PROP       "/general/workspace_names"
 
42
 
 
43
static GdkNativeWindow opt_socket_id = 0;
 
44
static gboolean opt_version = FALSE;
 
45
 
 
46
 
 
47
enum
 
48
{
 
49
    COL_NUMBER = 0,
 
50
    COL_NAME,
 
51
    N_COLS,
 
52
};
 
53
 
 
54
static void
 
55
workspace_names_update_xfconf(gint workspace,
 
56
                              const gchar *new_name)
 
57
{
 
58
    WnckScreen *screen = wnck_screen_get_default();
 
59
    XfconfChannel *channel;
 
60
    gchar **names;
 
61
    gboolean do_update_xfconf = TRUE;
 
62
 
 
63
    channel = xfconf_channel_get(WORKSPACES_CHANNEL);
 
64
    names = xfconf_channel_get_string_list(channel, WORKSPACE_NAMES_PROP);
 
65
 
 
66
    if(!names) {
 
67
        /* the property doesn't exist; let's build one from scratch */
 
68
        gint i, n_workspaces = wnck_screen_get_workspace_count(screen);
 
69
 
 
70
        names = g_new(gchar *, n_workspaces + 1);
 
71
        for(i = 0; i < n_workspaces; ++i) {
 
72
            if(G_LIKELY(i != workspace))
 
73
                names[i] = g_strdup_printf(_("Workspace %d"), i + 1);
 
74
            else
 
75
                names[i] = g_strdup(new_name);
 
76
        }
 
77
        names[n_workspaces] = NULL;
 
78
    } else {
 
79
        gint i, prop_len = g_strv_length(names);
 
80
        gint n_workspaces = wnck_screen_get_workspace_count(screen);
 
81
 
 
82
        if(prop_len < n_workspaces) {
 
83
            /* the property exists, but it's smaller than the current
 
84
             * actual number of workspaces */
 
85
            names = g_realloc(names, sizeof(gchar *) * (n_workspaces + 1));
 
86
            for(i = prop_len; i < n_workspaces; ++i) {
 
87
                if(i != workspace)
 
88
                    names[i] = g_strdup_printf(_("Workspace %d"), i + 1);
 
89
                else
 
90
                    names[i] = g_strdup(new_name);
 
91
            }
 
92
            names[n_workspaces] = NULL;
 
93
        } else {
 
94
            /* here we may have a |names| array longer than the actual
 
95
             * number of workspaces, but that's fine.  the user might
 
96
             * want to re-add a workspace or whatever, and may appreciate
 
97
             * that we remember the old name. */
 
98
            if(strcmp(names[workspace], new_name)) {
 
99
                g_free(names[workspace]);
 
100
                names[workspace] = g_strdup(new_name);
 
101
            } else {
 
102
                /* nothing's actually changed, so don't update the xfconf
 
103
                 * property.  this saves us some trouble later. */
 
104
                do_update_xfconf = FALSE;
 
105
            }
 
106
        }
 
107
    }
 
108
 
 
109
    if(do_update_xfconf)
 
110
        xfconf_channel_set_string_list(channel, WORKSPACE_NAMES_PROP, (const gchar **)names);
 
111
 
 
112
    g_strfreev(names);
 
113
}
 
114
 
 
115
static void
 
116
treeview_ws_names_cell_edited (GtkCellRendererText *cell,
 
117
                               const gchar         *path_string,
 
118
                               const gchar         *new_name,
 
119
                               gpointer             user_data)
 
120
{
 
121
    GtkTreeView *treeview;
 
122
    GtkTreeModel *model;
 
123
    GtkTreePath *path;
 
124
    GtkTreeIter iter;
 
125
    gchar *old_name = NULL;
 
126
    gint ws_num = 1;
 
127
 
 
128
    treeview = (GtkTreeView *) user_data;
 
129
    model = gtk_tree_view_get_model(treeview);
 
130
    path = gtk_tree_path_new_from_string (path_string);
 
131
    gtk_tree_model_get_iter (model, &iter, path);
 
132
 
 
133
    gtk_tree_model_get(model, &iter, COL_NUMBER, &ws_num, COL_NAME, &old_name, -1);
 
134
    if(strcmp(old_name, new_name)) {
 
135
        gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_NAME, new_name, -1);
 
136
        workspace_names_update_xfconf(ws_num - 1, new_name);
 
137
    }
 
138
 
 
139
    g_free(old_name);
 
140
 
 
141
    gtk_tree_path_free (path);
 
142
}
 
143
 
 
144
 
 
145
static void
 
146
xfconf_workspace_names_changed(XfconfChannel *channel,
 
147
                               const gchar *property,
 
148
                               const GValue *value,
 
149
                               gpointer user_data)
 
150
{
 
151
    GtkTreeView *treeview = user_data;
 
152
    GtkTreeModel *model = gtk_tree_view_get_model(treeview);
 
153
    WnckScreen *screen = wnck_screen_get_default();
 
154
    gint i, n_workspaces;
 
155
    GPtrArray *names;
 
156
    GtkTreePath *path;
 
157
    GtkTreeIter iter;
 
158
 
 
159
    if(G_VALUE_TYPE(value) !=  dbus_g_type_get_collection("GPtrArray",
 
160
                                                          G_TYPE_VALUE))
 
161
    {
 
162
        g_warning("(workspace names) Expected boxed GPtrArray property, got %s",
 
163
                  G_VALUE_TYPE_NAME(value));
 
164
        return;
 
165
    }
 
166
 
 
167
    names = g_value_get_boxed(value);
 
168
    if(!names)
 
169
        return;
 
170
 
 
171
    wnck_screen_force_update(screen);
 
172
    n_workspaces = wnck_screen_get_workspace_count(screen);
 
173
    for(i = 0; i < n_workspaces && i < names->len; ++i) {
 
174
        GValue *val = g_ptr_array_index(names, i);
 
175
        const gchar *new_name;
 
176
 
 
177
        if(!G_VALUE_HOLDS_STRING(val)) {
 
178
            g_warning("(workspace names) Expected string but got %s for item %d",
 
179
                      G_VALUE_TYPE_NAME(val), i);
 
180
            continue;
 
181
        }
 
182
 
 
183
        new_name = g_value_get_string(val);
 
184
 
 
185
        path = gtk_tree_path_new_from_indices(i, -1);
 
186
        if(gtk_tree_model_get_iter(model, &iter, path)) {
 
187
            gchar *old_name = NULL;
 
188
 
 
189
            gtk_tree_model_get(model, &iter,
 
190
                               COL_NAME, &old_name,
 
191
                               -1);
 
192
            /* only update the names that have actually changed */
 
193
            if(strcmp(old_name, new_name)) {
 
194
                gtk_list_store_set(GTK_LIST_STORE(model), &iter,
 
195
                                   COL_NAME, new_name,
 
196
                                   -1);
 
197
            }
 
198
            g_free(old_name);
 
199
        } else {
 
200
            /* must be a new workspace */
 
201
            gtk_list_store_append(GTK_LIST_STORE(model), &iter);
 
202
            gtk_list_store_set(GTK_LIST_STORE(model), &iter,
 
203
                               COL_NUMBER, i + 1,
 
204
                               COL_NAME, new_name,
 
205
                               -1);
 
206
        }
 
207
 
 
208
        gtk_tree_path_free(path);
 
209
    }
 
210
 
 
211
    /* if workspaces got destroyed, we need to remove them from the treeview */
 
212
    path = gtk_tree_path_new_from_indices(n_workspaces, -1);
 
213
    while(gtk_tree_model_get_iter(model, &iter, path))
 
214
        gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
 
215
    gtk_tree_path_free(path);
 
216
}
 
217
 
 
218
static void
 
219
workspace_dialog_setup_names_treeview(GladeXML *gxml,
 
220
                                      XfconfChannel *channel)
 
221
{
 
222
    GtkWidget *treeview, *dialog;
 
223
    GtkListStore *ls;
 
224
    GtkCellRenderer *render;
 
225
    GtkTreeViewColumn *col;
 
226
    WnckScreen *screen;
 
227
    gint n_workspaces, i;
 
228
    GtkTreeIter iter;
 
229
    gchar **names;
 
230
 
 
231
    dialog = glade_xml_get_widget(gxml, "change_name_dialog");
 
232
    g_object_set_data(G_OBJECT(dialog), "name-entry",
 
233
                      glade_xml_get_widget(gxml, "entry_name"));
 
234
    g_signal_connect(G_OBJECT(dialog), "delete-event",
 
235
                     G_CALLBACK(gtk_true), NULL);
 
236
 
 
237
    treeview = glade_xml_get_widget(gxml, "treeview_ws_names");
 
238
 
 
239
    ls = gtk_list_store_new(N_COLS, G_TYPE_INT, G_TYPE_STRING);
 
240
 
 
241
    render = gtk_cell_renderer_text_new();
 
242
    col = gtk_tree_view_column_new_with_attributes("", render,
 
243
                                                   "text", COL_NUMBER,
 
244
                                                   NULL);
 
245
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col);
 
246
 
 
247
    render = gtk_cell_renderer_text_new();
 
248
    g_object_set(G_OBJECT(render),
 
249
                 "editable", TRUE,
 
250
                 "ellipsize", PANGO_ELLIPSIZE_END,
 
251
                 "ellipsize-set", TRUE,
 
252
                 NULL);
 
253
    col = gtk_tree_view_column_new_with_attributes(_("Workspace Name"),
 
254
                                                   render,
 
255
                                                   "text", COL_NAME,
 
256
                                                   NULL);
 
257
    g_signal_connect (render, "edited", G_CALLBACK (treeview_ws_names_cell_edited), treeview);
 
258
 
 
259
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), col);
 
260
 
 
261
    screen = wnck_screen_get_default();
 
262
    wnck_screen_force_update(screen);
 
263
    names = xfconf_channel_get_string_list(channel, WORKSPACE_NAMES_PROP);
 
264
 
 
265
    n_workspaces = wnck_screen_get_workspace_count(screen);
 
266
    i = 0;
 
267
    if (names) {
 
268
        for(; i < n_workspaces && names[i]; ++i) {
 
269
            gtk_list_store_append(ls, &iter);
 
270
            gtk_list_store_set(ls, &iter,
 
271
                               COL_NUMBER, i + 1,
 
272
                               COL_NAME, names[i],
 
273
                               -1);
 
274
        }
 
275
    }
 
276
    for(; i < n_workspaces; ++i) {
 
277
        WnckWorkspace *space = wnck_screen_get_workspace(screen, i);
 
278
        const char *name = wnck_workspace_get_name(space);
 
279
 
 
280
        gtk_list_store_append(ls, &iter);
 
281
        gtk_list_store_set(ls, &iter,
 
282
                           COL_NUMBER, i + 1,
 
283
                           COL_NAME, name,
 
284
                           -1);
 
285
    }
 
286
    g_strfreev(names);
 
287
 
 
288
    gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(ls));
 
289
 
 
290
    g_signal_connect(G_OBJECT(channel),
 
291
                     "property-changed::" WORKSPACE_NAMES_PROP,
 
292
                     G_CALLBACK(xfconf_workspace_names_changed), treeview);
 
293
}
 
294
 
 
295
static void
 
296
workspace_dialog_configure_widgets (GladeXML *gxml,
 
297
                                    XfconfChannel *channel)
 
298
{
 
299
    GtkWidget *vbox;
 
300
 
 
301
    GdkPixbuf *monitor;
 
302
    GtkWidget *image;
 
303
 
 
304
    gint wmax, hmax;
 
305
 
 
306
    GtkWidget *workspace_count_spinbutton = glade_xml_get_widget (gxml, "workspace_count_spinbutton");
 
307
 
 
308
    GtkWidget *margin_top_spinbutton = glade_xml_get_widget (gxml, "margin_top_spinbutton");
 
309
    GtkWidget *margin_right_spinbutton = glade_xml_get_widget (gxml, "margin_right_spinbutton");
 
310
    GtkWidget *margin_bottom_spinbutton = glade_xml_get_widget (gxml, "margin_bottom_spinbutton");
 
311
    GtkWidget *margin_left_spinbutton = glade_xml_get_widget (gxml, "margin_left_spinbutton");
 
312
 
 
313
    /* Set monitor icon */
 
314
    monitor = xfce_inline_icon_at_size (monitor_icon_data, -1, -1);
 
315
    image = glade_xml_get_widget (gxml, "monitor_icon");
 
316
    gtk_image_set_from_pixbuf (GTK_IMAGE (image), monitor);
 
317
    g_object_unref (monitor);
 
318
 
 
319
    /* Set max margins range */
 
320
    wmax = gdk_screen_width () / 4;
 
321
    hmax = gdk_screen_height () / 4;
 
322
 
 
323
    gtk_spin_button_set_range (GTK_SPIN_BUTTON (margin_top_spinbutton), 0, wmax);
 
324
    gtk_spin_button_set_range (GTK_SPIN_BUTTON (margin_right_spinbutton), 0, hmax);
 
325
    gtk_spin_button_set_range (GTK_SPIN_BUTTON (margin_bottom_spinbutton), 0, wmax);
 
326
    gtk_spin_button_set_range (GTK_SPIN_BUTTON (margin_left_spinbutton), 0, hmax);
 
327
 
 
328
    /* Bind easy properties */
 
329
    xfconf_g_property_bind (channel,
 
330
                            "/general/workspace_count",
 
331
                            G_TYPE_INT,
 
332
                            (GObject *)workspace_count_spinbutton, "value");
 
333
 
 
334
    xfconf_g_property_bind (channel,
 
335
                            "/general/margin_top",
 
336
                            G_TYPE_INT,
 
337
                            (GObject *)margin_top_spinbutton, "value");
 
338
    xfconf_g_property_bind (channel,
 
339
                            "/general/margin_right",
 
340
                            G_TYPE_INT,
 
341
                            (GObject *)margin_right_spinbutton, "value");
 
342
    xfconf_g_property_bind (channel,
 
343
                            "/general/margin_bottom",
 
344
                            G_TYPE_INT,
 
345
                            (GObject *)margin_bottom_spinbutton, "value");
 
346
    xfconf_g_property_bind (channel,
 
347
                            "/general/margin_left",
 
348
                            G_TYPE_INT,
 
349
                            (GObject *)margin_left_spinbutton, "value");
 
350
 
 
351
    workspace_dialog_setup_names_treeview(gxml, channel);
 
352
 
 
353
    vbox = glade_xml_get_widget (gxml, "main-vbox");
 
354
 
 
355
    gtk_widget_show_all(vbox);
 
356
}
 
357
 
 
358
 
 
359
static GOptionEntry entries[] =
 
360
{
 
361
    { "socket-id", 's', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT, &opt_socket_id, N_("Settings manager socket"), N_("SOCKET ID") },
 
362
    { "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &opt_version, N_("Version information"), NULL },
 
363
    { NULL }
 
364
};
 
365
 
 
366
 
 
367
int
 
368
main(int argc, gchar **argv)
 
369
{
 
370
    GladeXML *gxml;
 
371
    GtkWidget *dialog;
 
372
    GtkWidget *plug;
 
373
    GtkWidget *plug_child;
 
374
    XfconfChannel *channel;
 
375
    GError *cli_error = NULL;
 
376
 
 
377
    xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");
 
378
 
 
379
    if(!gtk_init_with_args(&argc, &argv, _("."), entries, PACKAGE, &cli_error))
 
380
    {
 
381
        if (cli_error != NULL)
 
382
        {
 
383
            g_print (_("%s: %s\nTry %s --help to see a full list of available command line options.\n"), PACKAGE, cli_error->message, PACKAGE_NAME);
 
384
            g_error_free (cli_error);
 
385
            return 1;
 
386
        }
 
387
    }
 
388
 
 
389
    if(opt_version)
 
390
    {
 
391
        g_print("%s\n", PACKAGE_STRING);
 
392
        return 0;
 
393
    }
 
394
 
 
395
    if(!xfconf_init (&cli_error)) {
 
396
        g_critical ("Failed to contact xfconfd: %s", cli_error->message);
 
397
        g_error_free (cli_error);
 
398
        return 1;
 
399
    }
 
400
 
 
401
    channel = xfconf_channel_get(WORKSPACES_CHANNEL);
 
402
 
 
403
    gxml = glade_xml_new_from_buffer (workspace_dialog_glade,
 
404
                                      workspace_dialog_glade_length,
 
405
                                      NULL, NULL);
 
406
 
 
407
    if(gxml) {
 
408
        workspace_dialog_configure_widgets (gxml, channel);
 
409
 
 
410
        if(opt_socket_id == 0) {
 
411
            dialog = glade_xml_get_widget (gxml, "main-dialog");
 
412
 
 
413
            while(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_HELP) {
 
414
                /* FIXME: launch help */
 
415
            }
 
416
 
 
417
            gtk_widget_destroy(dialog);
 
418
        } else {
 
419
            /* Create plug widget */
 
420
            plug = gtk_plug_new (opt_socket_id);
 
421
            g_signal_connect (plug, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
 
422
            gtk_widget_show (plug);
 
423
 
 
424
            /* Get plug child widget */
 
425
            plug_child = glade_xml_get_widget (gxml, "plug-child");
 
426
            gtk_widget_reparent (plug_child, plug);
 
427
            gtk_widget_show (plug_child);
 
428
 
 
429
            /* Stop startup notification */
 
430
            gdk_notify_startup_complete ();
 
431
 
 
432
            /* Enter main loop */
 
433
            gtk_main ();
 
434
        }
 
435
    }
 
436
 
 
437
    xfconf_shutdown();
 
438
 
 
439
    return 0;
 
440
}