~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to panels/display/xrandr-capplet.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Monitor Settings. A preference panel for configuring monitors
 
2
 *
 
3
 * Copyright (C) 2007, 2008  Red Hat, Inc.
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 * Author: Soren Sandmann <sandmann@redhat.com>
 
20
 */
 
21
 
 
22
#include <config.h>
 
23
#include <string.h>
 
24
#include <stdlib.h>
 
25
#include <sys/wait.h>
 
26
 
 
27
#include <gtk/gtk.h>
 
28
#include "scrollarea.h"
 
29
#define GNOME_DESKTOP_USE_UNSTABLE_API
 
30
#include <libgnome-desktop/gnome-rr.h>
 
31
#include <libgnome-desktop/gnome-rr-config.h>
 
32
#include <libgnome-desktop/gnome-rr-labeler.h>
 
33
#include <gdk/gdkx.h>
 
34
#include <X11/Xlib.h>
 
35
#include <glib/gi18n.h>
 
36
#include <dbus/dbus-glib.h>
 
37
#include <dbus/dbus-glib-bindings.h>
 
38
#include <gdesktop-enums.h>
 
39
 
 
40
#include "xrandr-capplet.h"
 
41
 
 
42
#define TOP_BAR_HEIGHT 10
 
43
 
 
44
#define CLOCK_SCHEMA "org.gnome.desktop.interface"
 
45
#define CLOCK_FORMAT_KEY "clock-format"
 
46
 
 
47
/* The minimum supported size for the panel, see:
 
48
 * http://live.gnome.org/Design/SystemSettings */
 
49
#define MINIMUM_WIDTH 675
 
50
#define MINIMUM_HEIGHT 530
 
51
 
 
52
typedef struct App App;
 
53
typedef struct GrabInfo GrabInfo;
 
54
 
 
55
struct App
 
56
{
 
57
  GnomeRRScreen       *screen;
 
58
  GnomeRRConfig  *current_configuration;
 
59
  GnomeRRLabeler *labeler;
 
60
  GnomeRROutputInfo         *current_output;
 
61
 
 
62
  GSettings      *clock_settings;
 
63
  GtkBuilder     *builder;
 
64
 
 
65
  GtkWidget      *panel;
 
66
  GtkWidget      *current_monitor_event_box;
 
67
  GtkWidget      *current_monitor_label;
 
68
  GtkWidget      *monitor_switch;
 
69
  GtkListStore   *resolution_store;
 
70
  GtkWidget      *resolution_combo;
 
71
  GtkWidget      *rotation_combo;
 
72
  GtkWidget      *clone_checkbox;
 
73
  GtkWidget      *clone_label;
 
74
  GtkWidget      *show_icon_checkbox;
 
75
 
 
76
  /* We store the event timestamp when the Apply button is clicked */
 
77
  guint32         apply_button_clicked_timestamp;
 
78
 
 
79
  GtkWidget      *area;
 
80
  gboolean        ignore_gui_changes;
 
81
  gboolean        dragging_top_bar;
 
82
 
 
83
  /* These are used while we are waiting for the ApplyConfiguration method to be executed over D-bus */
 
84
  DBusGConnection *connection;
 
85
  DBusGProxy *proxy;
 
86
  DBusGProxyCall *proxy_call;
 
87
};
 
88
 
 
89
static void rebuild_gui (App *app);
 
90
static void on_clone_changed (GtkWidget *box, gpointer data);
 
91
static gboolean output_overlaps (GnomeRROutputInfo *output, GnomeRRConfig *config);
 
92
static void select_current_output_from_dialog_position (App *app);
 
93
static void monitor_switch_active_cb (GObject *object, GParamSpec *pspec, gpointer data);
 
94
static void get_geometry (GnomeRROutputInfo *output, int *w, int *h);
 
95
static void apply_configuration_returned_cb (DBusGProxy *proxy, DBusGProxyCall *call_id, void *data);
 
96
static gboolean get_clone_size (GnomeRRScreen *screen, int *width, int *height);
 
97
static gboolean output_info_supports_mode (App *app, GnomeRROutputInfo *info, int width, int height);
 
98
 
 
99
static void
 
100
error_message (App *app, const char *primary_text, const char *secondary_text)
 
101
{
 
102
  GtkWidget *toplevel;
 
103
  GtkWidget *dialog;
 
104
 
 
105
  if (app && app->panel)
 
106
    toplevel = gtk_widget_get_toplevel (app->panel);
 
107
  else
 
108
    toplevel = NULL;
 
109
 
 
110
  dialog = gtk_message_dialog_new (GTK_WINDOW (toplevel),
 
111
                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
 
112
                                   GTK_MESSAGE_ERROR,
 
113
                                   GTK_BUTTONS_CLOSE,
 
114
                                   "%s", primary_text);
 
115
 
 
116
  if (secondary_text)
 
117
    gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", secondary_text);
 
118
 
 
119
  gtk_dialog_run (GTK_DIALOG (dialog));
 
120
  gtk_widget_destroy (dialog);
 
121
}
 
122
 
 
123
static gboolean
 
124
do_free (gpointer data)
 
125
{
 
126
  g_free (data);
 
127
  return FALSE;
 
128
}
 
129
 
 
130
static gchar *
 
131
idle_free (gchar *s)
 
132
{
 
133
  g_idle_add (do_free, s);
 
134
 
 
135
  return s;
 
136
}
 
137
 
 
138
static gboolean
 
139
should_show_resolution (gint output_width,
 
140
                        gint output_height,
 
141
                        gint width,
 
142
                        gint height)
 
143
{
 
144
  if (width >= MIN (output_width, MINIMUM_WIDTH) &&
 
145
      height >= MIN (output_height, MINIMUM_HEIGHT))
 
146
    {
 
147
      return TRUE;
 
148
    }
 
149
  return FALSE;
 
150
}
 
151
 
 
152
static void
 
153
on_screen_changed (GnomeRRScreen *scr,
 
154
                   gpointer data)
 
155
{
 
156
  GnomeRRConfig *current;
 
157
  App *app = data;
 
158
 
 
159
  current = gnome_rr_config_new_current (app->screen, NULL);
 
160
  gnome_rr_config_ensure_primary (current);
 
161
 
 
162
  if (app->current_configuration)
 
163
    g_object_unref (app->current_configuration);
 
164
 
 
165
  app->current_configuration = current;
 
166
  app->current_output = NULL;
 
167
 
 
168
  if (app->labeler) {
 
169
    gnome_rr_labeler_hide (app->labeler);
 
170
    g_object_unref (app->labeler);
 
171
  }
 
172
 
 
173
  app->labeler = gnome_rr_labeler_new (app->current_configuration);
 
174
 
 
175
  select_current_output_from_dialog_position (app);
 
176
}
 
177
 
 
178
static void
 
179
on_viewport_changed (FooScrollArea *scroll_area,
 
180
                     GdkRectangle  *old_viewport,
 
181
                     GdkRectangle  *new_viewport)
 
182
{
 
183
  foo_scroll_area_set_size (scroll_area,
 
184
                            new_viewport->width,
 
185
                            new_viewport->height);
 
186
 
 
187
  foo_scroll_area_invalidate (scroll_area);
 
188
}
 
189
 
 
190
static void
 
191
layout_set_font (PangoLayout *layout, const char *font)
 
192
{
 
193
  PangoFontDescription *desc =
 
194
    pango_font_description_from_string (font);
 
195
 
 
196
  if (desc)
 
197
    {
 
198
      pango_layout_set_font_description (layout, desc);
 
199
 
 
200
      pango_font_description_free (desc);
 
201
    }
 
202
}
 
203
 
 
204
static void
 
205
clear_combo (GtkWidget *widget)
 
206
{
 
207
  GtkComboBox *box = GTK_COMBO_BOX (widget);
 
208
  GtkTreeModel *model = gtk_combo_box_get_model (box);
 
209
  GtkListStore *store = GTK_LIST_STORE (model);
 
210
 
 
211
  gtk_list_store_clear (store);
 
212
}
 
213
 
 
214
typedef struct
 
215
{
 
216
  const char *text;
 
217
  gboolean found;
 
218
  GtkTreeIter iter;
 
219
} ForeachInfo;
 
220
 
 
221
static gboolean
 
222
foreach (GtkTreeModel *model,
 
223
         GtkTreePath *path,
 
224
         GtkTreeIter *iter,
 
225
         gpointer data)
 
226
{
 
227
  ForeachInfo *info = data;
 
228
  char *text = NULL;
 
229
 
 
230
  gtk_tree_model_get (model, iter, 0, &text, -1);
 
231
 
 
232
  g_assert (text != NULL);
 
233
 
 
234
  if (strcmp (info->text, text) == 0)
 
235
    {
 
236
      info->found = TRUE;
 
237
      info->iter = *iter;
 
238
      return TRUE;
 
239
    }
 
240
 
 
241
  return FALSE;
 
242
}
 
243
 
 
244
static void
 
245
add_key (GtkWidget *widget,
 
246
         const char *text,
 
247
         int width, int height, int rate,
 
248
         GnomeRRRotation rotation)
 
249
{
 
250
  ForeachInfo info;
 
251
  GtkComboBox *box = GTK_COMBO_BOX (widget);
 
252
  GtkTreeModel *model = gtk_combo_box_get_model (box);
 
253
  GtkListStore *store = GTK_LIST_STORE (model);
 
254
 
 
255
  info.text = text;
 
256
  info.found = FALSE;
 
257
 
 
258
  gtk_tree_model_foreach (model, foreach, &info);
 
259
 
 
260
  if (!info.found)
 
261
    {
 
262
      GtkTreeIter iter;
 
263
      gtk_list_store_insert_with_values (store, &iter, -1,
 
264
                                         0, text,
 
265
                                         1, width,
 
266
                                         2, height,
 
267
                                         3, rate,
 
268
                                         4, width * height,
 
269
                                         5, rotation,
 
270
                                         -1);
 
271
 
 
272
    }
 
273
}
 
274
 
 
275
static gboolean
 
276
combo_select (GtkWidget *widget, const char *text)
 
277
{
 
278
  GtkComboBox *box = GTK_COMBO_BOX (widget);
 
279
  GtkTreeModel *model = gtk_combo_box_get_model (box);
 
280
  ForeachInfo info;
 
281
 
 
282
  info.text = text;
 
283
  info.found = FALSE;
 
284
 
 
285
  gtk_tree_model_foreach (model, foreach, &info);
 
286
 
 
287
  if (!info.found)
 
288
    return FALSE;
 
289
 
 
290
  gtk_combo_box_set_active_iter (box, &info.iter);
 
291
  return TRUE;
 
292
}
 
293
 
 
294
static GnomeRRMode **
 
295
get_current_modes (App *app)
 
296
{
 
297
  GnomeRROutput *output;
 
298
 
 
299
  if (gnome_rr_config_get_clone (app->current_configuration))
 
300
    {
 
301
      return gnome_rr_screen_list_clone_modes (app->screen);
 
302
    }
 
303
  else
 
304
    {
 
305
      if (!app->current_output)
 
306
        return NULL;
 
307
 
 
308
      output = gnome_rr_screen_get_output_by_name (app->screen,
 
309
                                                   gnome_rr_output_info_get_name (app->current_output));
 
310
 
 
311
      if (!output)
 
312
        return NULL;
 
313
 
 
314
      return gnome_rr_output_list_modes (output);
 
315
    }
 
316
}
 
317
 
 
318
static void
 
319
rebuild_rotation_combo (App *app)
 
320
{
 
321
  typedef struct
 
322
  {
 
323
    GnomeRRRotation     rotation;
 
324
    const char *        name;
 
325
  } RotationInfo;
 
326
  static const RotationInfo rotations[] = {
 
327
    { GNOME_RR_ROTATION_0, N_("Normal") },
 
328
    { GNOME_RR_ROTATION_90, N_("Anti-Clockwise") },
 
329
    { GNOME_RR_ROTATION_270, N_("Clockwise") },
 
330
    { GNOME_RR_ROTATION_180, N_("180 Degrees") },
 
331
  };
 
332
  const char *selection;
 
333
  GnomeRRRotation current;
 
334
  int i;
 
335
 
 
336
  clear_combo (app->rotation_combo);
 
337
 
 
338
  gtk_widget_set_sensitive (app->rotation_combo,
 
339
                            app->current_output && gnome_rr_output_info_is_active (app->current_output));
 
340
 
 
341
  if (!app->current_output)
 
342
    return;
 
343
 
 
344
  current = gnome_rr_output_info_get_rotation (app->current_output);
 
345
 
 
346
  selection = NULL;
 
347
  for (i = 0; i < G_N_ELEMENTS (rotations); ++i)
 
348
    {
 
349
      const RotationInfo *info = &(rotations[i]);
 
350
 
 
351
      gnome_rr_output_info_set_rotation (app->current_output, info->rotation);
 
352
 
 
353
      /* NULL-GError --- FIXME: we should say why this rotation is not available! */
 
354
      if (gnome_rr_config_applicable (app->current_configuration, app->screen, NULL))
 
355
        {
 
356
          add_key (app->rotation_combo, _(info->name), 0, 0, 0, info->rotation);
 
357
 
 
358
          if (info->rotation == current)
 
359
            selection = _(info->name);
 
360
        }
 
361
    }
 
362
 
 
363
  gnome_rr_output_info_set_rotation (app->current_output, current);
 
364
 
 
365
  if (!(selection && combo_select (app->rotation_combo, selection)))
 
366
    combo_select (app->rotation_combo, _("Normal"));
 
367
}
 
368
 
 
369
static int
 
370
count_active_outputs (App *app)
 
371
{
 
372
  int i, count = 0;
 
373
  GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (app->current_configuration);
 
374
 
 
375
  for (i = 0; outputs[i] != NULL; ++i)
 
376
    {
 
377
      if (gnome_rr_output_info_is_active (outputs[i]))
 
378
        count++;
 
379
    }
 
380
 
 
381
  return count;
 
382
}
 
383
 
 
384
#if 0
 
385
static int
 
386
count_all_outputs (GnomeRRConfig *config)
 
387
{
 
388
  int i;
 
389
  GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (config);
 
390
 
 
391
  for (i = 0; outputs[i] != NULL; i++)
 
392
    ;
 
393
 
 
394
  return i;
 
395
}
 
396
#endif
 
397
 
 
398
/* Computes whether "Mirror displays" (clone mode) is supported based on these criteria:
 
399
 *
 
400
 * 1. There is an available size for cloning.
 
401
 *
 
402
 * 2. There are 2 or more connected outputs that support that size.
 
403
 */
 
404
static gboolean
 
405
mirror_screens_is_supported (App *app)
 
406
{
 
407
  int clone_width, clone_height;
 
408
  gboolean have_clone_size;
 
409
  gboolean mirror_is_supported;
 
410
 
 
411
  mirror_is_supported = FALSE;
 
412
 
 
413
  have_clone_size = get_clone_size (app->screen, &clone_width, &clone_height);
 
414
 
 
415
  if (have_clone_size) {
 
416
    int i;
 
417
    int num_outputs_with_clone_size;
 
418
    GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (app->current_configuration);
 
419
 
 
420
    num_outputs_with_clone_size = 0;
 
421
 
 
422
    for (i = 0; outputs[i] != NULL; i++)
 
423
      {
 
424
         /* We count the connected outputs that support the clone size.  It
 
425
          * doesn't matter if those outputs aren't actually On currently; we
 
426
          * will turn them on in on_clone_changed().
 
427
          */
 
428
         if (gnome_rr_output_info_is_connected (outputs[i]) && output_info_supports_mode (app, outputs[i], clone_width, clone_height))
 
429
           num_outputs_with_clone_size++;
 
430
      }
 
431
 
 
432
    if (num_outputs_with_clone_size >= 2)
 
433
      mirror_is_supported = TRUE;
 
434
  }
 
435
 
 
436
  return mirror_is_supported;
 
437
}
 
438
 
 
439
static void
 
440
rebuild_mirror_screens (App *app)
 
441
{
 
442
  gboolean mirror_is_active;
 
443
  gboolean mirror_is_supported;
 
444
 
 
445
  g_signal_handlers_block_by_func (app->clone_checkbox, G_CALLBACK (on_clone_changed), app);
 
446
 
 
447
  mirror_is_active = app->current_configuration && gnome_rr_config_get_clone (app->current_configuration);
 
448
 
 
449
  /* If mirror_is_active, then it *must* be possible to turn mirroring off */
 
450
  mirror_is_supported = mirror_is_active || mirror_screens_is_supported (app);
 
451
 
 
452
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->clone_checkbox), mirror_is_active);
 
453
  gtk_widget_set_sensitive (app->clone_checkbox, mirror_is_supported);
 
454
  gtk_widget_set_sensitive (app->clone_label, mirror_is_supported);
 
455
 
 
456
  g_signal_handlers_unblock_by_func (app->clone_checkbox, G_CALLBACK (on_clone_changed), app);
 
457
}
 
458
 
 
459
static char *
 
460
mirror_monitor_name (void)
 
461
{
 
462
  /* Translators:  this is the feature where what you see on your laptop's
 
463
   * screen is the same as your external monitor.  Here, "Mirror" is being
 
464
   * used as an adjective, not as a verb.  For example, the Spanish
 
465
   * translation could be "Pantallas en Espejo", *not* "Espejar Pantallas".
 
466
   */
 
467
  return g_strdup (_("Mirror Displays"));
 
468
}
 
469
 
 
470
static void
 
471
rebuild_current_monitor_label (App *app)
 
472
{
 
473
  char *str, *tmp;
 
474
  GdkColor color;
 
475
  gboolean use_color;
 
476
 
 
477
  if (app->current_output)
 
478
    {
 
479
      if (gnome_rr_config_get_clone (app->current_configuration))
 
480
        tmp = mirror_monitor_name ();
 
481
      else
 
482
        tmp = g_strdup (gnome_rr_output_info_get_display_name (app->current_output));
 
483
 
 
484
      str = g_strdup_printf ("<b>%s</b>", tmp);
 
485
      gnome_rr_labeler_get_color_for_output (app->labeler, app->current_output, &color);
 
486
      use_color = TRUE;
 
487
      g_free (tmp);
 
488
    }
 
489
  else
 
490
    {
 
491
      str = g_strdup_printf ("<b>%s</b>", _("Monitor"));
 
492
      use_color = FALSE;
 
493
    }
 
494
 
 
495
  gtk_label_set_markup (GTK_LABEL (app->current_monitor_label), str);
 
496
  g_free (str);
 
497
 
 
498
  if (use_color)
 
499
    {
 
500
      GdkRGBA black = { 0, 0, 0, 1.0 };
 
501
      GdkRGBA light;
 
502
 
 
503
      light.red = color.red / 65535.0;
 
504
      light.green = color.green / 65535.0;
 
505
      light.blue = color.blue / 65535.0;
 
506
      light.alpha = 1.0;
 
507
      gtk_widget_override_background_color (app->current_monitor_event_box,
 
508
                                            gtk_widget_get_state_flags (app->current_monitor_event_box),
 
509
                                            &light);
 
510
 
 
511
      /* Make the label explicitly black.  We don't want it to follow the
 
512
       * theme's colors, since the label is always shown against a light
 
513
       * pastel background.  See bgo#556050
 
514
       */
 
515
      gtk_widget_override_color (app->current_monitor_label,
 
516
                                 gtk_widget_get_state_flags (app->current_monitor_label),
 
517
                                 &black);
 
518
    }
 
519
  else
 
520
    {
 
521
      /* Remove any modifications we did on the label's color */
 
522
      gtk_widget_override_color (app->current_monitor_label,
 
523
                                 gtk_widget_get_state_flags (app->current_monitor_label),
 
524
                                 NULL);
 
525
    }
 
526
 
 
527
    gtk_event_box_set_visible_window (GTK_EVENT_BOX (app->current_monitor_event_box), use_color);
 
528
}
 
529
 
 
530
static void
 
531
rebuild_on_off_radios (App *app)
 
532
{
 
533
  gboolean sensitive;
 
534
  gboolean on_active;
 
535
 
 
536
  g_signal_handlers_block_by_func (app->monitor_switch, G_CALLBACK (monitor_switch_active_cb), app);
 
537
 
 
538
  sensitive = FALSE;
 
539
  on_active = FALSE;
 
540
 
 
541
  if (!gnome_rr_config_get_clone (app->current_configuration) && app->current_output)
 
542
    {
 
543
      if (count_active_outputs (app) > 1 || !gnome_rr_output_info_is_active (app->current_output))
 
544
        sensitive = TRUE;
 
545
      else
 
546
        sensitive = FALSE;
 
547
 
 
548
      on_active = gnome_rr_output_info_is_active (app->current_output);
 
549
    }
 
550
 
 
551
  gtk_widget_set_sensitive (app->monitor_switch, sensitive);
 
552
 
 
553
  gtk_switch_set_active (GTK_SWITCH (app->monitor_switch), on_active);
 
554
 
 
555
  g_signal_handlers_unblock_by_func (app->monitor_switch, G_CALLBACK (monitor_switch_active_cb), app);
 
556
}
 
557
 
 
558
static char *
 
559
make_resolution_string (int width, int height)
 
560
{
 
561
  int ratio;
 
562
  const char *aspect = NULL;
 
563
 
 
564
  if (width && height) {
 
565
    if (width > height)
 
566
      ratio = width * 10 / height;
 
567
    else
 
568
      ratio = height * 10 / width;
 
569
 
 
570
    switch (ratio) {
 
571
    case 13:
 
572
      aspect = "4:3";
 
573
      break;
 
574
    case 16:
 
575
      aspect = "16:10";
 
576
      break;
 
577
    case 17:
 
578
      aspect = "16:9";
 
579
      break;
 
580
    case 12:
 
581
      aspect = "5:4";
 
582
      break;
 
583
      /* This catches 1.5625 as well (1600x1024) when maybe it shouldn't. */
 
584
    case 15:
 
585
      aspect = "3:2";
 
586
      break;
 
587
    case 18:
 
588
      aspect = "9:5";
 
589
      break;
 
590
    case 10:
 
591
      aspect = "1:1";
 
592
      break;
 
593
    }
 
594
  }
 
595
 
 
596
  if (aspect != NULL)
 
597
    return g_strdup_printf (_("%d x %d (%s)"), width, height, aspect);
 
598
  else
 
599
    return g_strdup_printf (_("%d x %d"), width, height);
 
600
}
 
601
 
 
602
static void
 
603
find_best_mode (GnomeRRMode **modes, int *out_width, int *out_height)
 
604
{
 
605
  int i;
 
606
 
 
607
  *out_width = 0;
 
608
  *out_height = 0;
 
609
 
 
610
  for (i = 0; modes[i] != NULL; i++)
 
611
    {
 
612
      int w, h;
 
613
 
 
614
      w = gnome_rr_mode_get_width (modes[i]);
 
615
      h = gnome_rr_mode_get_height (modes[i]);
 
616
 
 
617
      if (w * h > *out_width * *out_height)
 
618
        {
 
619
          *out_width = w;
 
620
          *out_height = h;
 
621
        }
 
622
    }
 
623
}
 
624
 
 
625
static void
 
626
rebuild_resolution_combo (App *app)
 
627
{
 
628
  int i;
 
629
  GnomeRRMode **modes;
 
630
  const char *current;
 
631
  int output_width, output_height;
 
632
 
 
633
  clear_combo (app->resolution_combo);
 
634
 
 
635
  if (!(modes = get_current_modes (app))
 
636
      || !app->current_output
 
637
      || !gnome_rr_output_info_is_active (app->current_output))
 
638
    {
 
639
      gtk_widget_set_sensitive (app->resolution_combo, FALSE);
 
640
      return;
 
641
    }
 
642
 
 
643
  g_assert (app->current_output != NULL);
 
644
 
 
645
  gnome_rr_output_info_get_geometry (app->current_output, NULL, NULL, &output_width, &output_height);
 
646
  g_assert (output_width != 0 && output_height != 0);
 
647
 
 
648
  gtk_widget_set_sensitive (app->resolution_combo, TRUE);
 
649
 
 
650
  for (i = 0; modes[i] != NULL; ++i)
 
651
    {
 
652
      int width, height;
 
653
 
 
654
      width = gnome_rr_mode_get_width (modes[i]);
 
655
      height = gnome_rr_mode_get_height (modes[i]);
 
656
 
 
657
      if (should_show_resolution (output_width, output_height, width, height))
 
658
        {
 
659
          add_key (app->resolution_combo,
 
660
                   idle_free (make_resolution_string (width, height)),
 
661
                   width, height, 0, -1);
 
662
        }
 
663
    }
 
664
 
 
665
  current = idle_free (make_resolution_string (output_width, output_height));
 
666
 
 
667
  if (!combo_select (app->resolution_combo, current))
 
668
    {
 
669
      int best_w, best_h;
 
670
 
 
671
      find_best_mode (modes, &best_w, &best_h);
 
672
      combo_select (app->resolution_combo, idle_free (make_resolution_string (best_w, best_h)));
 
673
    }
 
674
}
 
675
 
 
676
static void
 
677
rebuild_gui (App *app)
 
678
{
 
679
  /* We would break spectacularly if we recursed, so
 
680
   * just assert if that happens
 
681
   */
 
682
  g_assert (app->ignore_gui_changes == FALSE);
 
683
 
 
684
  app->ignore_gui_changes = TRUE;
 
685
 
 
686
  rebuild_mirror_screens (app);
 
687
  rebuild_current_monitor_label (app);
 
688
  rebuild_on_off_radios (app);
 
689
  rebuild_resolution_combo (app);
 
690
  rebuild_rotation_combo (app);
 
691
 
 
692
  app->ignore_gui_changes = FALSE;
 
693
}
 
694
 
 
695
static gboolean
 
696
get_mode (GtkWidget *widget, int *width, int *height, int *freq, GnomeRRRotation *rot)
 
697
{
 
698
  GtkTreeIter iter;
 
699
  GtkTreeModel *model;
 
700
  GtkComboBox *box = GTK_COMBO_BOX (widget);
 
701
  int dummy;
 
702
 
 
703
  if (!gtk_combo_box_get_active_iter (box, &iter))
 
704
    return FALSE;
 
705
 
 
706
  if (!width)
 
707
    width = &dummy;
 
708
 
 
709
  if (!height)
 
710
    height = &dummy;
 
711
 
 
712
  if (!freq)
 
713
    freq = &dummy;
 
714
 
 
715
  if (!rot)
 
716
    rot = (GnomeRRRotation *)&dummy;
 
717
 
 
718
  model = gtk_combo_box_get_model (box);
 
719
  gtk_tree_model_get (model, &iter,
 
720
                      1, width,
 
721
                      2, height,
 
722
                      3, freq,
 
723
                      5, rot,
 
724
                      -1);
 
725
 
 
726
  return TRUE;
 
727
 
 
728
}
 
729
 
 
730
static void
 
731
on_rotation_changed (GtkComboBox *box, gpointer data)
 
732
{
 
733
  App *app = data;
 
734
  GnomeRRRotation rotation;
 
735
 
 
736
  if (!app->current_output)
 
737
    return;
 
738
 
 
739
  if (get_mode (app->rotation_combo, NULL, NULL, NULL, &rotation))
 
740
    gnome_rr_output_info_set_rotation (app->current_output, rotation);
 
741
 
 
742
  foo_scroll_area_invalidate (FOO_SCROLL_AREA (app->area));
 
743
}
 
744
 
 
745
static void
 
746
select_resolution_for_current_output (App *app)
 
747
{
 
748
  GnomeRRMode **modes;
 
749
  int width, height;
 
750
  int x,y;
 
751
  gnome_rr_output_info_get_geometry (app->current_output, &x, &y, NULL, NULL);
 
752
 
 
753
  width = gnome_rr_output_info_get_preferred_width (app->current_output);
 
754
  height = gnome_rr_output_info_get_preferred_height (app->current_output);
 
755
 
 
756
  if (width != 0 && height != 0)
 
757
    {
 
758
      gnome_rr_output_info_set_geometry (app->current_output, x, y, width, height);
 
759
      return;
 
760
    }
 
761
 
 
762
  modes = get_current_modes (app);
 
763
  if (!modes)
 
764
    return;
 
765
 
 
766
  find_best_mode (modes, &width, &height);
 
767
 
 
768
  gnome_rr_output_info_set_geometry (app->current_output, x, y, width, height);
 
769
}
 
770
 
 
771
static void
 
772
monitor_switch_active_cb (GObject    *object,
 
773
                          GParamSpec *pspec,
 
774
                          gpointer    data)
 
775
{
 
776
  App *app = data;
 
777
  gboolean value;
 
778
 
 
779
  if (!app->current_output)
 
780
    return;
 
781
 
 
782
  value = gtk_switch_get_active (GTK_SWITCH (object));
 
783
 
 
784
  if (value)
 
785
    {
 
786
      gnome_rr_output_info_set_active (app->current_output, TRUE);
 
787
      select_resolution_for_current_output (app);
 
788
    }
 
789
  else
 
790
    {
 
791
      gnome_rr_output_info_set_active (app->current_output, FALSE);
 
792
      gnome_rr_config_ensure_primary (app->current_configuration);
 
793
    }
 
794
 
 
795
  rebuild_gui (app);
 
796
  foo_scroll_area_invalidate (FOO_SCROLL_AREA (app->area));
 
797
}
 
798
 
 
799
static void
 
800
realign_outputs_after_resolution_change (App *app, GnomeRROutputInfo *output_that_changed, int old_width, int old_height)
 
801
{
 
802
  /* We find the outputs that were below or to the right of the output that
 
803
   * changed, and realign them; we also do that for outputs that shared the
 
804
   * right/bottom edges with the output that changed.  The outputs that are
 
805
   * above or to the left of that output don't need to change.
 
806
   */
 
807
 
 
808
  int i;
 
809
  int old_right_edge, old_bottom_edge;
 
810
  int dx, dy;
 
811
  int x, y, width, height;
 
812
  GnomeRROutputInfo **outputs;
 
813
 
 
814
  g_assert (app->current_configuration != NULL);
 
815
 
 
816
  gnome_rr_output_info_get_geometry (output_that_changed, &x, &y, &width, &height); 
 
817
 
 
818
  if (width == old_width && height == old_height)
 
819
    return;
 
820
 
 
821
  old_right_edge = x + old_width;
 
822
  old_bottom_edge = y + old_height;
 
823
 
 
824
  dx = width - old_width;
 
825
  dy = height - old_height;
 
826
 
 
827
  outputs = gnome_rr_config_get_outputs (app->current_configuration);
 
828
 
 
829
  for (i = 0; outputs[i] != NULL; i++)
 
830
    {
 
831
      int output_x, output_y;
 
832
      int output_width, output_height;
 
833
 
 
834
      if (outputs[i] == output_that_changed || !gnome_rr_output_info_is_connected (outputs[i]))
 
835
        continue;
 
836
 
 
837
      gnome_rr_output_info_get_geometry (outputs[i], &output_x, &output_y, &output_width, &output_height);
 
838
 
 
839
      if (output_x >= old_right_edge)
 
840
         output_x += dx;
 
841
      else if (output_x + output_width == old_right_edge)
 
842
         output_x = x + width - output_width;
 
843
 
 
844
      if (output_y >= old_bottom_edge)
 
845
         output_y += dy;
 
846
      else if (output_y + output_height == old_bottom_edge)
 
847
         output_y = y + height - output_height;
 
848
 
 
849
      gnome_rr_output_info_set_geometry (outputs[i], output_x, output_y, output_width, output_height);
 
850
    }
 
851
}
 
852
 
 
853
static void
 
854
on_resolution_changed (GtkComboBox *box, gpointer data)
 
855
{
 
856
  App *app = data;
 
857
  int old_width, old_height;
 
858
  int x,y;
 
859
  int width;
 
860
  int height;
 
861
 
 
862
  if (!app->current_output)
 
863
    return;
 
864
 
 
865
  gnome_rr_output_info_get_geometry (app->current_output, &x, &y, &old_width, &old_height);
 
866
 
 
867
  if (get_mode (app->resolution_combo, &width, &height, NULL, NULL))
 
868
    {
 
869
      gnome_rr_output_info_set_geometry (app->current_output, x, y, width, height);
 
870
 
 
871
      if (width == 0 || height == 0)
 
872
        gnome_rr_output_info_set_active (app->current_output, FALSE);
 
873
      else
 
874
        gnome_rr_output_info_set_active (app->current_output, TRUE);
 
875
    }
 
876
 
 
877
  realign_outputs_after_resolution_change (app, app->current_output, old_width, old_height);
 
878
 
 
879
  rebuild_rotation_combo (app);
 
880
 
 
881
  foo_scroll_area_invalidate (FOO_SCROLL_AREA (app->area));
 
882
}
 
883
 
 
884
static void
 
885
lay_out_outputs_horizontally (App *app)
 
886
{
 
887
  int i;
 
888
  int x;
 
889
  GnomeRROutputInfo **outputs;
 
890
 
 
891
  /* Lay out all the monitors horizontally when "mirror screens" is turned
 
892
   * off, to avoid having all of them overlapped initially.  We put the
 
893
   * outputs turned off on the right-hand side.
 
894
   */
 
895
 
 
896
  x = 0;
 
897
 
 
898
  /* First pass, all "on" outputs */
 
899
  outputs = gnome_rr_config_get_outputs (app->current_configuration);
 
900
 
 
901
  for (i = 0; outputs[i]; ++i)
 
902
    {
 
903
      int width, height;
 
904
      if (gnome_rr_output_info_is_connected (outputs[i]) && gnome_rr_output_info_is_active (outputs[i]))
 
905
        {
 
906
          gnome_rr_output_info_get_geometry (outputs[i], NULL, NULL, &width, &height);
 
907
          gnome_rr_output_info_set_geometry (outputs[i], x, 0, width, height);
 
908
          x += width;
 
909
        }
 
910
    }
 
911
 
 
912
  /* Second pass, all the black screens */
 
913
 
 
914
    for (i = 0; outputs[i]; ++i)
 
915
    {
 
916
      int width, height;
 
917
      if (!(gnome_rr_output_info_is_connected (outputs[i]) && gnome_rr_output_info_is_active (outputs[i])))
 
918
        {
 
919
          gnome_rr_output_info_get_geometry (outputs[i], NULL, NULL, &width, &height);
 
920
          gnome_rr_output_info_set_geometry (outputs[i], x, 0, width, height);
 
921
          x += width;
 
922
        }
 
923
    }
 
924
 
 
925
}
 
926
 
 
927
/* FIXME: this function is copied from gnome-settings-daemon/plugins/xrandr/gsd-xrandr-manager.c.
 
928
 * Do we need to put this function in gnome-desktop for public use?
 
929
 */
 
930
static gboolean
 
931
get_clone_size (GnomeRRScreen *screen, int *width, int *height)
 
932
{
 
933
  GnomeRRMode **modes = gnome_rr_screen_list_clone_modes (screen);
 
934
  int best_w, best_h;
 
935
  int i;
 
936
 
 
937
  best_w = 0;
 
938
  best_h = 0;
 
939
 
 
940
  for (i = 0; modes[i] != NULL; ++i) {
 
941
    GnomeRRMode *mode = modes[i];
 
942
    int w, h;
 
943
 
 
944
    w = gnome_rr_mode_get_width (mode);
 
945
    h = gnome_rr_mode_get_height (mode);
 
946
 
 
947
    if (w * h > best_w * best_h) {
 
948
      best_w = w;
 
949
      best_h = h;
 
950
    }
 
951
  }
 
952
 
 
953
  if (best_w > 0 && best_h > 0) {
 
954
    if (width)
 
955
      *width = best_w;
 
956
    if (height)
 
957
      *height = best_h;
 
958
 
 
959
    return TRUE;
 
960
  }
 
961
 
 
962
  return FALSE;
 
963
}
 
964
 
 
965
static gboolean
 
966
output_info_supports_mode (App *app, GnomeRROutputInfo *info, int width, int height)
 
967
{
 
968
  GnomeRROutput *output;
 
969
  GnomeRRMode **modes;
 
970
  int i;
 
971
 
 
972
  if (!gnome_rr_output_info_is_connected (info))
 
973
    return FALSE;
 
974
 
 
975
  output = gnome_rr_screen_get_output_by_name (app->screen, gnome_rr_output_info_get_name (info));
 
976
  if (!output)
 
977
    return FALSE;
 
978
 
 
979
  modes = gnome_rr_output_list_modes (output);
 
980
 
 
981
  for (i = 0; modes[i]; i++) {
 
982
    if (gnome_rr_mode_get_width (modes[i]) == width
 
983
        && gnome_rr_mode_get_height (modes[i]) == height)
 
984
      return TRUE;
 
985
  }
 
986
 
 
987
  return FALSE;
 
988
}
 
989
 
 
990
static void
 
991
on_clone_changed (GtkWidget *box, gpointer data)
 
992
{
 
993
  App *app = data;
 
994
 
 
995
  gnome_rr_config_set_clone (app->current_configuration, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (app->clone_checkbox)));
 
996
 
 
997
  if (gnome_rr_config_get_clone (app->current_configuration))
 
998
    {
 
999
      int i;
 
1000
      int width, height;
 
1001
      GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (app->current_configuration);
 
1002
 
 
1003
      for (i = 0; outputs[i]; ++i)
 
1004
        {
 
1005
          if (gnome_rr_output_info_is_connected (outputs[i]))
 
1006
            {
 
1007
              app->current_output = outputs[i];
 
1008
              break;
 
1009
            }
 
1010
        }
 
1011
 
 
1012
      /* Turn on all the connected screens that support the best clone mode.
 
1013
       * The user may hit "Mirror displays", but he shouldn't have to turn on
 
1014
       * all the required outputs as well.
 
1015
       */
 
1016
 
 
1017
      get_clone_size (app->screen, &width, &height);
 
1018
 
 
1019
      for (i = 0; outputs[i]; i++) {
 
1020
        int x, y;
 
1021
        if (output_info_supports_mode (app, outputs[i], width, height)) {
 
1022
          gnome_rr_output_info_set_active (outputs[i], TRUE);
 
1023
          gnome_rr_output_info_get_geometry (outputs[i], &x, &y, NULL, NULL);
 
1024
          gnome_rr_output_info_set_geometry (outputs[i], x, y, width, height);
 
1025
        }
 
1026
      }
 
1027
    }
 
1028
  else
 
1029
    {
 
1030
      if (output_overlaps (app->current_output, app->current_configuration))
 
1031
        lay_out_outputs_horizontally (app);
 
1032
    }
 
1033
 
 
1034
  rebuild_gui (app);
 
1035
}
 
1036
 
 
1037
static void
 
1038
apply_rotation_to_geometry (GnomeRROutputInfo *output, int *w, int *h)
 
1039
{
 
1040
  GnomeRRRotation rotation;
 
1041
 
 
1042
  rotation = gnome_rr_output_info_get_rotation (output);
 
1043
  if ((rotation & GNOME_RR_ROTATION_90) || (rotation & GNOME_RR_ROTATION_270))
 
1044
    {
 
1045
      int tmp;
 
1046
      tmp = *h;
 
1047
      *h = *w;
 
1048
      *w = tmp;
 
1049
    }
 
1050
}
 
1051
 
 
1052
static void
 
1053
get_geometry (GnomeRROutputInfo *output, int *w, int *h)
 
1054
{
 
1055
  if (gnome_rr_output_info_is_active (output))
 
1056
    {
 
1057
      gnome_rr_output_info_get_geometry (output, NULL, NULL, w, h);
 
1058
    }
 
1059
  else
 
1060
    {
 
1061
      *h = gnome_rr_output_info_get_preferred_height (output);
 
1062
      *w = gnome_rr_output_info_get_preferred_width (output);
 
1063
    }
 
1064
 
 
1065
  apply_rotation_to_geometry (output, w, h);
 
1066
}
 
1067
 
 
1068
#define SPACE 15
 
1069
#define MARGIN  15
 
1070
 
 
1071
static GList *
 
1072
list_connected_outputs (App *app, int *total_w, int *total_h)
 
1073
{
 
1074
  int i, dummy;
 
1075
  GList *result = NULL;
 
1076
  GnomeRROutputInfo **outputs;
 
1077
 
 
1078
  if (!total_w)
 
1079
    total_w = &dummy;
 
1080
  if (!total_h)
 
1081
    total_h = &dummy;
 
1082
 
 
1083
  *total_w = 0;
 
1084
  *total_h = 0;
 
1085
 
 
1086
  outputs = gnome_rr_config_get_outputs (app->current_configuration);
 
1087
  for (i = 0; outputs[i] != NULL; ++i)
 
1088
    {
 
1089
      if (gnome_rr_output_info_is_connected (outputs[i]))
 
1090
        {
 
1091
          int w, h;
 
1092
 
 
1093
          result = g_list_prepend (result, outputs[i]);
 
1094
 
 
1095
          get_geometry (outputs[i], &w, &h);
 
1096
 
 
1097
          *total_w += w;
 
1098
          *total_h += h;
 
1099
        }
 
1100
    }
 
1101
 
 
1102
  return g_list_reverse (result);
 
1103
}
 
1104
 
 
1105
static int
 
1106
get_n_connected (App *app)
 
1107
{
 
1108
  GList *connected_outputs = list_connected_outputs (app, NULL, NULL);
 
1109
  int n = g_list_length (connected_outputs);
 
1110
 
 
1111
  g_list_free (connected_outputs);
 
1112
 
 
1113
  return n;
 
1114
}
 
1115
 
 
1116
static double
 
1117
compute_scale (App *app)
 
1118
{
 
1119
  int available_w, available_h;
 
1120
  int total_w, total_h;
 
1121
  int n_monitors;
 
1122
  GdkRectangle viewport;
 
1123
  GList *connected_outputs;
 
1124
 
 
1125
  foo_scroll_area_get_viewport (FOO_SCROLL_AREA (app->area), &viewport);
 
1126
 
 
1127
  connected_outputs = list_connected_outputs (app, &total_w, &total_h);
 
1128
 
 
1129
  n_monitors = g_list_length (connected_outputs);
 
1130
 
 
1131
  g_list_free (connected_outputs);
 
1132
 
 
1133
  available_w = viewport.width - 2 * MARGIN - (n_monitors - 1) * SPACE;
 
1134
  available_h = viewport.height - 2 * MARGIN - (n_monitors - 1) * SPACE;
 
1135
 
 
1136
  return MIN ((double)available_w / total_w, (double)available_h / total_h);
 
1137
}
 
1138
 
 
1139
typedef struct Edge
 
1140
{
 
1141
  GnomeRROutputInfo *output;
 
1142
  int x1, y1;
 
1143
  int x2, y2;
 
1144
} Edge;
 
1145
 
 
1146
typedef struct Snap
 
1147
{
 
1148
  Edge *snapper;              /* Edge that should be snapped */
 
1149
  Edge *snappee;
 
1150
  int dy, dx;
 
1151
} Snap;
 
1152
 
 
1153
static void
 
1154
add_edge (GnomeRROutputInfo *output, int x1, int y1, int x2, int y2, GArray *edges)
 
1155
{
 
1156
  Edge e;
 
1157
 
 
1158
  e.x1 = x1;
 
1159
  e.x2 = x2;
 
1160
  e.y1 = y1;
 
1161
  e.y2 = y2;
 
1162
  e.output = output;
 
1163
 
 
1164
  g_array_append_val (edges, e);
 
1165
}
 
1166
 
 
1167
static void
 
1168
list_edges_for_output (GnomeRROutputInfo *output, GArray *edges)
 
1169
{
 
1170
  int x, y, w, h;
 
1171
 
 
1172
  gnome_rr_output_info_get_geometry (output, &x, &y, &w, &h);
 
1173
 
 
1174
  apply_rotation_to_geometry (output, &w, &h);
 
1175
 
 
1176
  /* Top, Bottom, Left, Right */
 
1177
  add_edge (output, x, y, x + w, y, edges);
 
1178
  add_edge (output, x, y + h, x + w, y + h, edges);
 
1179
  add_edge (output, x, y, x, y + h, edges);
 
1180
  add_edge (output, x + w, y, x + w, y + h, edges);
 
1181
}
 
1182
 
 
1183
static void
 
1184
list_edges (GnomeRRConfig *config, GArray *edges)
 
1185
{
 
1186
  int i;
 
1187
  GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (config);
 
1188
 
 
1189
  for (i = 0; outputs[i]; ++i)
 
1190
    {
 
1191
      if (gnome_rr_output_info_is_connected (outputs[i]))
 
1192
        list_edges_for_output (outputs[i], edges);
 
1193
    }
 
1194
}
 
1195
 
 
1196
static gboolean
 
1197
overlap (int s1, int e1, int s2, int e2)
 
1198
{
 
1199
  return (!(e1 < s2 || s1 >= e2));
 
1200
}
 
1201
 
 
1202
static gboolean
 
1203
horizontal_overlap (Edge *snapper, Edge *snappee)
 
1204
{
 
1205
  if (snapper->y1 != snapper->y2 || snappee->y1 != snappee->y2)
 
1206
    return FALSE;
 
1207
 
 
1208
  return overlap (snapper->x1, snapper->x2, snappee->x1, snappee->x2);
 
1209
}
 
1210
 
 
1211
static gboolean
 
1212
vertical_overlap (Edge *snapper, Edge *snappee)
 
1213
{
 
1214
  if (snapper->x1 != snapper->x2 || snappee->x1 != snappee->x2)
 
1215
    return FALSE;
 
1216
 
 
1217
  return overlap (snapper->y1, snapper->y2, snappee->y1, snappee->y2);
 
1218
}
 
1219
 
 
1220
static void
 
1221
add_snap (GArray *snaps, Snap snap)
 
1222
{
 
1223
  if (ABS (snap.dx) <= 200 || ABS (snap.dy) <= 200)
 
1224
    g_array_append_val (snaps, snap);
 
1225
}
 
1226
 
 
1227
static void
 
1228
add_edge_snaps (Edge *snapper, Edge *snappee, GArray *snaps)
 
1229
{
 
1230
  Snap snap;
 
1231
 
 
1232
  snap.snapper = snapper;
 
1233
  snap.snappee = snappee;
 
1234
 
 
1235
  if (horizontal_overlap (snapper, snappee))
 
1236
    {
 
1237
      snap.dx = 0;
 
1238
      snap.dy = snappee->y1 - snapper->y1;
 
1239
 
 
1240
      add_snap (snaps, snap);
 
1241
    }
 
1242
  else if (vertical_overlap (snapper, snappee))
 
1243
    {
 
1244
      snap.dy = 0;
 
1245
      snap.dx = snappee->x1 - snapper->x1;
 
1246
 
 
1247
      add_snap (snaps, snap);
 
1248
    }
 
1249
 
 
1250
  /* Corner snaps */
 
1251
  /* 1->1 */
 
1252
  snap.dx = snappee->x1 - snapper->x1;
 
1253
  snap.dy = snappee->y1 - snapper->y1;
 
1254
 
 
1255
  add_snap (snaps, snap);
 
1256
 
 
1257
  /* 1->2 */
 
1258
  snap.dx = snappee->x2 - snapper->x1;
 
1259
  snap.dy = snappee->y2 - snapper->y1;
 
1260
 
 
1261
  add_snap (snaps, snap);
 
1262
 
 
1263
  /* 2->2 */
 
1264
  snap.dx = snappee->x2 - snapper->x2;
 
1265
  snap.dy = snappee->y2 - snapper->y2;
 
1266
 
 
1267
  add_snap (snaps, snap);
 
1268
 
 
1269
  /* 2->1 */
 
1270
  snap.dx = snappee->x1 - snapper->x2;
 
1271
  snap.dy = snappee->y1 - snapper->y2;
 
1272
 
 
1273
  add_snap (snaps, snap);
 
1274
}
 
1275
 
 
1276
static void
 
1277
list_snaps (GnomeRROutputInfo *output, GArray *edges, GArray *snaps)
 
1278
{
 
1279
  int i;
 
1280
 
 
1281
  for (i = 0; i < edges->len; ++i)
 
1282
    {
 
1283
      Edge *output_edge = &(g_array_index (edges, Edge, i));
 
1284
 
 
1285
      if (output_edge->output == output)
 
1286
        {
 
1287
          int j;
 
1288
 
 
1289
          for (j = 0; j < edges->len; ++j)
 
1290
            {
 
1291
              Edge *edge = &(g_array_index (edges, Edge, j));
 
1292
 
 
1293
              if (edge->output != output)
 
1294
                add_edge_snaps (output_edge, edge, snaps);
 
1295
            }
 
1296
        }
 
1297
    }
 
1298
}
 
1299
 
 
1300
#if 0
 
1301
static void
 
1302
print_edge (Edge *edge)
 
1303
{
 
1304
  g_debug ("(%d %d %d %d)", edge->x1, edge->y1, edge->x2, edge->y2);
 
1305
}
 
1306
#endif
 
1307
 
 
1308
static gboolean
 
1309
corner_on_edge (int x, int y, Edge *e)
 
1310
{
 
1311
  if (x == e->x1 && x == e->x2 && y >= e->y1 && y <= e->y2)
 
1312
    return TRUE;
 
1313
 
 
1314
  if (y == e->y1 && y == e->y2 && x >= e->x1 && x <= e->x2)
 
1315
    return TRUE;
 
1316
 
 
1317
  return FALSE;
 
1318
}
 
1319
 
 
1320
static gboolean
 
1321
edges_align (Edge *e1, Edge *e2)
 
1322
{
 
1323
  if (corner_on_edge (e1->x1, e1->y1, e2))
 
1324
    return TRUE;
 
1325
 
 
1326
  if (corner_on_edge (e2->x1, e2->y1, e1))
 
1327
    return TRUE;
 
1328
 
 
1329
  return FALSE;
 
1330
}
 
1331
 
 
1332
static gboolean
 
1333
output_is_aligned (GnomeRROutputInfo *output, GArray *edges)
 
1334
{
 
1335
  gboolean result = FALSE;
 
1336
  int i;
 
1337
 
 
1338
  for (i = 0; i < edges->len; ++i)
 
1339
    {
 
1340
      Edge *output_edge = &(g_array_index (edges, Edge, i));
 
1341
 
 
1342
      if (output_edge->output == output)
 
1343
        {
 
1344
          int j;
 
1345
 
 
1346
          for (j = 0; j < edges->len; ++j)
 
1347
            {
 
1348
              Edge *edge = &(g_array_index (edges, Edge, j));
 
1349
 
 
1350
              /* We are aligned if an output edge matches
 
1351
               * an edge of another output
 
1352
               */
 
1353
              if (edge->output != output_edge->output)
 
1354
                {
 
1355
                  if (edges_align (output_edge, edge))
 
1356
                    {
 
1357
                      result = TRUE;
 
1358
                      goto done;
 
1359
                    }
 
1360
                }
 
1361
            }
 
1362
        }
 
1363
    }
 
1364
 done:
 
1365
 
 
1366
  return result;
 
1367
}
 
1368
 
 
1369
static void
 
1370
get_output_rect (GnomeRROutputInfo *output, GdkRectangle *rect)
 
1371
{
 
1372
  gnome_rr_output_info_get_geometry (output, &rect->x, &rect->y, &rect->width, &rect->height);
 
1373
 
 
1374
  apply_rotation_to_geometry (output, &rect->width, &rect->height);
 
1375
}
 
1376
 
 
1377
static gboolean
 
1378
output_overlaps (GnomeRROutputInfo *output, GnomeRRConfig *config)
 
1379
{
 
1380
  int i;
 
1381
  GdkRectangle output_rect;
 
1382
  GnomeRROutputInfo **outputs;
 
1383
 
 
1384
  g_assert (output != NULL);
 
1385
 
 
1386
  get_output_rect (output, &output_rect);
 
1387
 
 
1388
  outputs = gnome_rr_config_get_outputs (config);
 
1389
  for (i = 0; outputs[i]; ++i)
 
1390
    {
 
1391
      if (outputs[i] != output && gnome_rr_output_info_is_connected (outputs[i]))
 
1392
        {
 
1393
          GdkRectangle other_rect;
 
1394
 
 
1395
          get_output_rect (outputs[i], &other_rect);
 
1396
          if (gdk_rectangle_intersect (&output_rect, &other_rect, NULL))
 
1397
            return TRUE;
 
1398
        }
 
1399
    }
 
1400
 
 
1401
  return FALSE;
 
1402
}
 
1403
 
 
1404
static gboolean
 
1405
gnome_rr_config_is_aligned (GnomeRRConfig *config, GArray *edges)
 
1406
{
 
1407
  int i;
 
1408
  gboolean result = TRUE;
 
1409
  GnomeRROutputInfo **outputs;
 
1410
 
 
1411
  outputs = gnome_rr_config_get_outputs (config);
 
1412
  for (i = 0; outputs[i]; ++i)
 
1413
    {
 
1414
      if (gnome_rr_output_info_is_connected (outputs[i]))
 
1415
        {
 
1416
          if (!output_is_aligned (outputs[i], edges))
 
1417
            return FALSE;
 
1418
 
 
1419
          if (output_overlaps (outputs[i], config))
 
1420
            return FALSE;
 
1421
        }
 
1422
    }
 
1423
 
 
1424
  return result;
 
1425
}
 
1426
 
 
1427
struct GrabInfo
 
1428
{
 
1429
  int grab_x;
 
1430
  int grab_y;
 
1431
  int output_x;
 
1432
  int output_y;
 
1433
};
 
1434
 
 
1435
static gboolean
 
1436
is_corner_snap (const Snap *s)
 
1437
{
 
1438
  return s->dx != 0 && s->dy != 0;
 
1439
}
 
1440
 
 
1441
static int
 
1442
compare_snaps (gconstpointer v1, gconstpointer v2)
 
1443
{
 
1444
  const Snap *s1 = v1;
 
1445
  const Snap *s2 = v2;
 
1446
  int sv1 = MAX (ABS (s1->dx), ABS (s1->dy));
 
1447
  int sv2 = MAX (ABS (s2->dx), ABS (s2->dy));
 
1448
  int d;
 
1449
 
 
1450
  d = sv1 - sv2;
 
1451
 
 
1452
  /* This snapping algorithm is good enough for rock'n'roll, but
 
1453
   * this is probably a better:
 
1454
   *
 
1455
   *    First do a horizontal/vertical snap, then
 
1456
   *    with the new coordinates from that snap,
 
1457
   *    do a corner snap.
 
1458
   *
 
1459
   * Right now, it's confusing that corner snapping
 
1460
   * depends on the distance in an axis that you can't actually see.
 
1461
   *
 
1462
   */
 
1463
  if (d == 0)
 
1464
    {
 
1465
      if (is_corner_snap (s1) && !is_corner_snap (s2))
 
1466
        return -1;
 
1467
      else if (is_corner_snap (s2) && !is_corner_snap (s1))
 
1468
        return 1;
 
1469
      else
 
1470
        return 0;
 
1471
    }
 
1472
  else
 
1473
    {
 
1474
      return d;
 
1475
    }
 
1476
}
 
1477
 
 
1478
/* Sets a mouse cursor for a widget's window.  As a hack, you can pass
 
1479
 * GDK_BLANK_CURSOR to mean "set the cursor to NULL" (i.e. reset the widget's
 
1480
 * window's cursor to its default).
 
1481
 */
 
1482
static void
 
1483
set_cursor (GtkWidget *widget, GdkCursorType type)
 
1484
{
 
1485
  GdkCursor *cursor;
 
1486
  GdkWindow *window;
 
1487
 
 
1488
  if (type == GDK_BLANK_CURSOR)
 
1489
    cursor = NULL;
 
1490
  else
 
1491
    cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), type);
 
1492
 
 
1493
  window = gtk_widget_get_window (widget);
 
1494
 
 
1495
  if (window)
 
1496
    gdk_window_set_cursor (window, cursor);
 
1497
 
 
1498
  if (cursor)
 
1499
    g_object_unref (cursor);
 
1500
}
 
1501
 
 
1502
static void
 
1503
set_top_bar_tooltip (App *app, gboolean is_dragging)
 
1504
{
 
1505
  const char *text;
 
1506
 
 
1507
  if (is_dragging)
 
1508
    text = NULL;
 
1509
  else
 
1510
    text = _("Drag to change primary display.");
 
1511
 
 
1512
  gtk_widget_set_tooltip_text (app->area, text);
 
1513
}
 
1514
 
 
1515
static void
 
1516
on_top_bar_event (FooScrollArea *area,
 
1517
                  FooScrollAreaEvent *event,
 
1518
                  App *app)
 
1519
{
 
1520
  /* Ignore drops */
 
1521
  if (event->type == FOO_DROP)
 
1522
    return;
 
1523
 
 
1524
  /* If the mouse is inside the top bar, set the cursor to "you can move me".  See
 
1525
   * on_canvas_event() for where we reset the cursor to the default if it
 
1526
   * exits the outputs' area.
 
1527
   */
 
1528
  if (!gnome_rr_config_get_clone (app->current_configuration) && get_n_connected (app) > 1)
 
1529
    set_cursor (GTK_WIDGET (area), GDK_HAND1);
 
1530
 
 
1531
  if (event->type == FOO_BUTTON_PRESS)
 
1532
    {
 
1533
      rebuild_gui (app);
 
1534
      set_top_bar_tooltip (app, TRUE);
 
1535
 
 
1536
      if (!gnome_rr_config_get_clone (app->current_configuration) && get_n_connected (app) > 1)
 
1537
        {
 
1538
          app->dragging_top_bar = TRUE;
 
1539
          foo_scroll_area_begin_grab (area, (FooScrollAreaEventFunc) on_top_bar_event, app);
 
1540
        }
 
1541
 
 
1542
      foo_scroll_area_invalidate (area);
 
1543
    }
 
1544
  else
 
1545
    {
 
1546
      if (foo_scroll_area_is_grabbed (area))
 
1547
        {
 
1548
          if (event->type == FOO_BUTTON_RELEASE)
 
1549
            {
 
1550
              foo_scroll_area_end_grab (area, event);
 
1551
              app->dragging_top_bar = FALSE;
 
1552
              set_top_bar_tooltip (app, FALSE);
 
1553
            }
 
1554
 
 
1555
          foo_scroll_area_invalidate (area);
 
1556
        }
 
1557
    }
 
1558
}
 
1559
 
 
1560
static void
 
1561
set_monitors_tooltip (App *app, gboolean is_dragging)
 
1562
{
 
1563
  const char *text;
 
1564
 
 
1565
  if (is_dragging)
 
1566
    text = NULL;
 
1567
  else
 
1568
    text = _("Select a monitor to change its properties; drag it to rearrange its placement.");
 
1569
 
 
1570
  gtk_widget_set_tooltip_text (app->area, text);
 
1571
}
 
1572
 
 
1573
static void
 
1574
set_primary_output (App *app,
 
1575
                    GnomeRROutputInfo *output)
 
1576
{
 
1577
  int i;
 
1578
  GnomeRROutputInfo **outputs;
 
1579
 
 
1580
  outputs = gnome_rr_config_get_outputs (app->current_configuration);
 
1581
  for (i = 0; outputs[i] != NULL; ++i)
 
1582
    gnome_rr_output_info_set_primary (outputs[i], outputs[i] == output);
 
1583
}
 
1584
 
 
1585
static void
 
1586
on_output_event (FooScrollArea *area,
 
1587
                 FooScrollAreaEvent *event,
 
1588
                 gpointer data)
 
1589
{
 
1590
  GnomeRROutputInfo *output = data;
 
1591
  App *app = g_object_get_data (G_OBJECT (area), "app");
 
1592
 
 
1593
  if (event->type == FOO_DRAG_HOVER)
 
1594
    {
 
1595
      if (gnome_rr_output_info_is_active (output) && app->dragging_top_bar)
 
1596
        set_primary_output (app, output);
 
1597
      return;
 
1598
    }
 
1599
  if (event->type == FOO_DROP)
 
1600
    {
 
1601
      /* Activate new primary? */
 
1602
      return;
 
1603
    }
 
1604
 
 
1605
  /* If the mouse is inside the outputs, set the cursor to "you can move me".  See
 
1606
   * on_canvas_event() for where we reset the cursor to the default if it
 
1607
   * exits the outputs' area.
 
1608
   */
 
1609
  if (!gnome_rr_config_get_clone (app->current_configuration) && get_n_connected (app) > 1)
 
1610
    set_cursor (GTK_WIDGET (area), GDK_FLEUR);
 
1611
 
 
1612
  if (event->type == FOO_BUTTON_PRESS)
 
1613
    {
 
1614
      GrabInfo *info;
 
1615
 
 
1616
      app->current_output = output;
 
1617
 
 
1618
      rebuild_gui (app);
 
1619
      set_monitors_tooltip (app, TRUE);
 
1620
 
 
1621
      if (!gnome_rr_config_get_clone (app->current_configuration) && get_n_connected (app) > 1)
 
1622
        {
 
1623
          int output_x, output_y;
 
1624
          gnome_rr_output_info_get_geometry (output, &output_x, &output_y, NULL, NULL);
 
1625
 
 
1626
          foo_scroll_area_begin_grab (area, on_output_event, data);
 
1627
 
 
1628
          info = g_new0 (GrabInfo, 1);
 
1629
          info->grab_x = event->x;
 
1630
          info->grab_y = event->y;
 
1631
          info->output_x = output_x;
 
1632
          info->output_y = output_y;
 
1633
 
 
1634
          g_object_set_data (G_OBJECT (output), "grab-info", info);
 
1635
        }
 
1636
      foo_scroll_area_invalidate (area);
 
1637
    }
 
1638
  else
 
1639
    {
 
1640
      if (foo_scroll_area_is_grabbed (area))
 
1641
        {
 
1642
          GrabInfo *info = g_object_get_data (G_OBJECT (output), "grab-info");
 
1643
          double scale = compute_scale (app);
 
1644
          int old_x, old_y;
 
1645
          int width, height;
 
1646
          int new_x, new_y;
 
1647
          int i;
 
1648
          GArray *edges, *snaps, *new_edges;
 
1649
 
 
1650
          gnome_rr_output_info_get_geometry (output, &old_x, &old_y, &width, &height);
 
1651
          new_x = info->output_x + (event->x - info->grab_x) / scale;
 
1652
          new_y = info->output_y + (event->y - info->grab_y) / scale;
 
1653
 
 
1654
          gnome_rr_output_info_set_geometry (output, new_x, new_y, width, height);
 
1655
 
 
1656
          edges = g_array_new (TRUE, TRUE, sizeof (Edge));
 
1657
          snaps = g_array_new (TRUE, TRUE, sizeof (Snap));
 
1658
          new_edges = g_array_new (TRUE, TRUE, sizeof (Edge));
 
1659
 
 
1660
          list_edges (app->current_configuration, edges);
 
1661
          list_snaps (output, edges, snaps);
 
1662
 
 
1663
          g_array_sort (snaps, compare_snaps);
 
1664
 
 
1665
          gnome_rr_output_info_set_geometry (output, new_x, new_y, width, height);
 
1666
 
 
1667
          for (i = 0; i < snaps->len; ++i)
 
1668
            {
 
1669
              Snap *snap = &(g_array_index (snaps, Snap, i));
 
1670
              GArray *new_edges = g_array_new (TRUE, TRUE, sizeof (Edge));
 
1671
 
 
1672
              gnome_rr_output_info_set_geometry (output, new_x + snap->dx, new_y + snap->dy, width, height);
 
1673
 
 
1674
              g_array_set_size (new_edges, 0);
 
1675
              list_edges (app->current_configuration, new_edges);
 
1676
 
 
1677
              if (gnome_rr_config_is_aligned (app->current_configuration, new_edges))
 
1678
                {
 
1679
                  g_array_free (new_edges, TRUE);
 
1680
                  break;
 
1681
                }
 
1682
              else
 
1683
                {
 
1684
                  gnome_rr_output_info_set_geometry (output, info->output_x, info->output_y, width, height);
 
1685
                }
 
1686
            }
 
1687
 
 
1688
          g_array_free (new_edges, TRUE);
 
1689
          g_array_free (snaps, TRUE);
 
1690
          g_array_free (edges, TRUE);
 
1691
 
 
1692
          if (event->type == FOO_BUTTON_RELEASE)
 
1693
            {
 
1694
              foo_scroll_area_end_grab (area, event);
 
1695
              set_monitors_tooltip (app, FALSE);
 
1696
 
 
1697
              g_free (g_object_get_data (G_OBJECT (output), "grab-info"));
 
1698
              g_object_set_data (G_OBJECT (output), "grab-info", NULL);
 
1699
 
 
1700
#if 0
 
1701
              g_debug ("new position: %d %d %d %d", output->x, output->y, output->width, output->height);
 
1702
#endif
 
1703
            }
 
1704
 
 
1705
          foo_scroll_area_invalidate (area);
 
1706
        }
 
1707
    }
 
1708
}
 
1709
 
 
1710
static void
 
1711
on_canvas_event (FooScrollArea *area,
 
1712
                 FooScrollAreaEvent *event,
 
1713
                 gpointer data)
 
1714
{
 
1715
  /* If the mouse exits the outputs, reset the cursor to the default.  See
 
1716
   * on_output_event() for where we set the cursor to the movement cursor if
 
1717
   * it is over one of the outputs.
 
1718
   */
 
1719
  set_cursor (GTK_WIDGET (area), GDK_BLANK_CURSOR);
 
1720
}
 
1721
 
 
1722
static PangoLayout *
 
1723
get_display_name (App *app,
 
1724
                  GnomeRROutputInfo *output)
 
1725
{
 
1726
  PangoLayout *layout;
 
1727
  char *text;
 
1728
 
 
1729
  if (gnome_rr_config_get_clone (app->current_configuration))
 
1730
    text = mirror_monitor_name ();
 
1731
  else
 
1732
    text = g_strdup (gnome_rr_output_info_get_display_name (output));
 
1733
 
 
1734
  layout = gtk_widget_create_pango_layout (GTK_WIDGET (app->area), text);
 
1735
  g_free (text);
 
1736
  pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
 
1737
 
 
1738
  return layout;
 
1739
}
 
1740
 
 
1741
static void
 
1742
paint_background (FooScrollArea *area,
 
1743
                  cairo_t       *cr)
 
1744
{
 
1745
  GdkRectangle viewport;
 
1746
  GtkWidget *widget;
 
1747
  GtkStyleContext *context;
 
1748
  GdkRGBA fg, bg;
 
1749
 
 
1750
  widget = GTK_WIDGET (area);
 
1751
 
 
1752
  foo_scroll_area_get_viewport (area, &viewport);
 
1753
  context = gtk_widget_get_style_context (widget);
 
1754
  gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &fg);
 
1755
  gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
 
1756
 
 
1757
  cairo_set_source_rgba (cr,
 
1758
                         (fg.red + bg.red) / 2,
 
1759
                         (fg.green + bg.green) / 2,
 
1760
                         (fg.blue + bg.blue) / 2,
 
1761
                         (fg.alpha + bg.alpha) / 2);
 
1762
 
 
1763
  cairo_rectangle (cr,
 
1764
                   viewport.x, viewport.y,
 
1765
                   viewport.width, viewport.height);
 
1766
 
 
1767
  cairo_fill_preserve (cr);
 
1768
 
 
1769
  foo_scroll_area_add_input_from_fill (area, cr, on_canvas_event, NULL);
 
1770
 
 
1771
  cairo_set_source_rgba (cr,
 
1772
                         0.7 * bg.red,
 
1773
                         0.7 * bg.green,
 
1774
                         0.7 * bg.blue,
 
1775
                         0.7 * bg.alpha);
 
1776
 
 
1777
  cairo_stroke (cr);
 
1778
}
 
1779
 
 
1780
static void
 
1781
color_shade (double *r,
 
1782
             double *g,
 
1783
             double *b,
 
1784
             double  k)
 
1785
{
 
1786
  double h, s, v;
 
1787
 
 
1788
  gtk_rgb_to_hsv (*r, *g, *b, &h, &s, &v);
 
1789
 
 
1790
  s *= k;
 
1791
  if (s > 1.0)
 
1792
    s = 1.0;
 
1793
  else if (s < 0.0)
 
1794
    s = 0.0;
 
1795
 
 
1796
  v *= k;
 
1797
  if (v > 1.0)
 
1798
    v = 1.0;
 
1799
  else if (v < 0.0)
 
1800
    v = 0.0;
 
1801
 
 
1802
  gtk_hsv_to_rgb (h, s, v, r, g, b);
 
1803
}
 
1804
 
 
1805
static void
 
1806
paint_output (App *app, cairo_t *cr, int i)
 
1807
{
 
1808
  int w, h;
 
1809
  double scale = compute_scale (app);
 
1810
  double x, y;
 
1811
  int output_x, output_y;
 
1812
  GnomeRRRotation rotation;
 
1813
  int total_w, total_h;
 
1814
  GList *connected_outputs = list_connected_outputs (app, &total_w, &total_h);
 
1815
  GnomeRROutputInfo *output = g_list_nth (connected_outputs, i)->data;
 
1816
  PangoLayout *layout = get_display_name (app, output);
 
1817
  PangoRectangle ink_extent, log_extent;
 
1818
  GdkRectangle viewport;
 
1819
  GdkColor output_color;
 
1820
  double r, g, b;
 
1821
  double available_w;
 
1822
  double factor;
 
1823
 
 
1824
  cairo_save (cr);
 
1825
 
 
1826
  foo_scroll_area_get_viewport (FOO_SCROLL_AREA (app->area), &viewport);
 
1827
  get_geometry (output, &w, &h);
 
1828
 
 
1829
#if 0
 
1830
  g_debug ("%s (%p) geometry %d %d %d primary=%d", output->name, output->name,
 
1831
           w, h, output->rate, output->primary);
 
1832
#endif
 
1833
 
 
1834
  viewport.height -= 2 * MARGIN;
 
1835
  viewport.width -= 2 * MARGIN;
 
1836
 
 
1837
  gnome_rr_output_info_get_geometry (output, &output_x, &output_y, NULL, NULL);
 
1838
  x = output_x * scale + MARGIN + (viewport.width - total_w * scale) / 2.0;
 
1839
  y = output_y * scale + MARGIN + (viewport.height - total_h * scale) / 2.0;
 
1840
 
 
1841
#if 0
 
1842
  g_debug ("scaled: %f %f", x, y);
 
1843
 
 
1844
  g_debug ("scale: %f", scale);
 
1845
 
 
1846
  g_debug ("%f %f %f %f", x, y, w * scale + 0.5, h * scale + 0.5);
 
1847
#endif
 
1848
 
 
1849
  cairo_translate (cr,
 
1850
                   x + (w * scale + 0.5) / 2,
 
1851
                   y + (h * scale + 0.5) / 2);
 
1852
 
 
1853
  /* rotation is already applied in get_geometry */
 
1854
 
 
1855
  rotation = gnome_rr_output_info_get_rotation (output);
 
1856
  if (rotation & GNOME_RR_REFLECT_X)
 
1857
    cairo_scale (cr, -1, 1);
 
1858
 
 
1859
  if (rotation & GNOME_RR_REFLECT_Y)
 
1860
    cairo_scale (cr, 1, -1);
 
1861
 
 
1862
  cairo_translate (cr,
 
1863
                   - x - (w * scale + 0.5) / 2,
 
1864
                   - y - (h * scale + 0.5) / 2);
 
1865
 
 
1866
  if (output == app->current_output)
 
1867
    {
 
1868
      GtkStyleContext *context;
 
1869
      GdkRGBA color;
 
1870
 
 
1871
      context = gtk_widget_get_style_context (app->area);
 
1872
      gtk_style_context_get_background_color (context, GTK_STATE_FLAG_SELECTED, &color);
 
1873
 
 
1874
      cairo_rectangle (cr, x - 2, y - 2, w * scale + 0.5 + 4, h * scale + 0.5 + 4);
 
1875
 
 
1876
      cairo_set_line_width (cr, 4);
 
1877
      cairo_set_source_rgba (cr, color.red, color.green, color.blue, 0.5);
 
1878
      cairo_stroke (cr);
 
1879
    }
 
1880
 
 
1881
  cairo_rectangle (cr, x, y, w * scale + 0.5, h * scale + 0.5);
 
1882
  cairo_clip_preserve (cr);
 
1883
 
 
1884
  gnome_rr_labeler_get_color_for_output (app->labeler, output, &output_color);
 
1885
  r = output_color.red / 65535.0;
 
1886
  g = output_color.green / 65535.0;
 
1887
  b = output_color.blue / 65535.0;
 
1888
 
 
1889
  if (!gnome_rr_output_info_is_active (output))
 
1890
    {
 
1891
      /* If the output is turned off, just darken the selected color */
 
1892
      color_shade (&r, &g, &b, 0.4);
 
1893
    }
 
1894
 
 
1895
  cairo_set_source_rgba (cr, r, g, b, 1.0);
 
1896
 
 
1897
  foo_scroll_area_add_input_from_fill (FOO_SCROLL_AREA (app->area),
 
1898
                                       cr, on_output_event, output);
 
1899
  cairo_fill (cr);
 
1900
 
 
1901
  cairo_rectangle (cr, x + 0.5, y + 0.5, w * scale + 0.5 - 1, h * scale + 0.5 - 1);
 
1902
 
 
1903
  cairo_set_line_width (cr, 1);
 
1904
  cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
 
1905
 
 
1906
  cairo_stroke (cr);
 
1907
  cairo_set_line_width (cr, 2);
 
1908
 
 
1909
  cairo_save (cr);
 
1910
 
 
1911
  layout_set_font (layout, "Sans 10");
 
1912
  pango_layout_get_pixel_extents (layout, &ink_extent, &log_extent);
 
1913
 
 
1914
  available_w = w * scale + 0.5 - 6; /* Same as the inner rectangle's width, minus 1 pixel of padding on each side */
 
1915
  if (available_w < ink_extent.width)
 
1916
    factor = available_w / ink_extent.width;
 
1917
  else
 
1918
    factor = 1.0;
 
1919
 
 
1920
  cairo_move_to (cr,
 
1921
                 x + ((w * scale + 0.5) - factor * log_extent.width) / 2,
 
1922
                 y + ((h * scale + 0.5) - factor * log_extent.height) / 2);
 
1923
 
 
1924
  cairo_scale (cr, factor, factor);
 
1925
  if (gnome_rr_output_info_is_active (output))
 
1926
    cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
 
1927
  else
 
1928
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
 
1929
 
 
1930
  pango_cairo_show_layout (cr, layout);
 
1931
  g_object_unref (layout);
 
1932
  cairo_restore (cr);
 
1933
 
 
1934
  if (gnome_rr_output_info_get_primary (output))
 
1935
    {
 
1936
      const char *clock_format;
 
1937
      char *text;
 
1938
      gboolean use_24;
 
1939
      GDateTime *dt;
 
1940
      GDesktopClockFormat value;
 
1941
 
 
1942
      /* top bar */
 
1943
      cairo_rectangle (cr, x, y, w * scale + 0.5, TOP_BAR_HEIGHT);
 
1944
      cairo_set_source_rgb (cr, 0, 0, 0);
 
1945
      foo_scroll_area_add_input_from_fill (FOO_SCROLL_AREA (app->area),
 
1946
                                           cr,
 
1947
                                           (FooScrollAreaEventFunc) on_top_bar_event,
 
1948
                                           app);
 
1949
 
 
1950
      cairo_fill (cr);
 
1951
 
 
1952
      /* clock */
 
1953
      /* FIXME: set 12/24 hour */
 
1954
      value = g_settings_get_enum (app->clock_settings, CLOCK_FORMAT_KEY);
 
1955
      use_24 = value == G_DESKTOP_CLOCK_FORMAT_24H;
 
1956
      if (use_24)
 
1957
        clock_format = _("%a %R");
 
1958
      else
 
1959
        clock_format = _("%a %l:%M %p");
 
1960
 
 
1961
      dt = g_date_time_new_now_local ();
 
1962
      text = g_date_time_format (dt, clock_format);
 
1963
      g_date_time_unref (dt);
 
1964
 
 
1965
      layout = gtk_widget_create_pango_layout (GTK_WIDGET (app->area), text);
 
1966
      g_free (text);
 
1967
      pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
 
1968
 
 
1969
      layout_set_font (layout, "Sans 4");
 
1970
      pango_layout_get_pixel_extents (layout, &ink_extent, &log_extent);
 
1971
 
 
1972
      if (available_w < ink_extent.width)
 
1973
        factor = available_w / ink_extent.width;
 
1974
      else
 
1975
        factor = 1.0;
 
1976
 
 
1977
      cairo_move_to (cr,
 
1978
                     x + ((w * scale + 0.5) - factor * log_extent.width) / 2,
 
1979
                     y + (TOP_BAR_HEIGHT - factor * log_extent.height) / 2);
 
1980
 
 
1981
      cairo_scale (cr, factor, factor);
 
1982
 
 
1983
      cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
 
1984
 
 
1985
      pango_cairo_show_layout (cr, layout);
 
1986
      g_object_unref (layout);
 
1987
    }
 
1988
 
 
1989
  cairo_restore (cr);
 
1990
}
 
1991
 
 
1992
static void
 
1993
on_area_paint (FooScrollArea  *area,
 
1994
               cairo_t        *cr,
 
1995
               gpointer        data)
 
1996
{
 
1997
  App *app = data;
 
1998
  GList *connected_outputs = NULL;
 
1999
  GList *list;
 
2000
 
 
2001
  paint_background (area, cr);
 
2002
 
 
2003
  if (!app->current_configuration)
 
2004
    return;
 
2005
 
 
2006
  connected_outputs = list_connected_outputs (app, NULL, NULL);
 
2007
 
 
2008
  for (list = connected_outputs; list != NULL; list = list->next)
 
2009
    {
 
2010
      paint_output (app, cr, g_list_position (connected_outputs, list));
 
2011
 
 
2012
      if (gnome_rr_config_get_clone (app->current_configuration))
 
2013
        break;
 
2014
    }
 
2015
}
 
2016
 
 
2017
static void
 
2018
make_text_combo (GtkWidget *widget, int sort_column)
 
2019
{
 
2020
  GtkComboBox *box = GTK_COMBO_BOX (widget);
 
2021
  GtkListStore *store = gtk_list_store_new (
 
2022
                                            6,
 
2023
                                            G_TYPE_STRING,          /* Text */
 
2024
                                            G_TYPE_INT,             /* Width */
 
2025
                                            G_TYPE_INT,             /* Height */
 
2026
                                            G_TYPE_INT,             /* Frequency */
 
2027
                                            G_TYPE_INT,             /* Width * Height */
 
2028
                                            G_TYPE_INT);            /* Rotation */
 
2029
 
 
2030
  GtkCellRenderer *cell;
 
2031
 
 
2032
  gtk_cell_layout_clear (GTK_CELL_LAYOUT (widget));
 
2033
 
 
2034
  gtk_combo_box_set_model (box, GTK_TREE_MODEL (store));
 
2035
 
 
2036
  cell = gtk_cell_renderer_text_new ();
 
2037
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (box), cell, TRUE);
 
2038
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (box), cell,
 
2039
                                  "text", 0,
 
2040
                                  NULL);
 
2041
 
 
2042
  if (sort_column != -1)
 
2043
    {
 
2044
      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
 
2045
                                            sort_column,
 
2046
                                            GTK_SORT_DESCENDING);
 
2047
    }
 
2048
}
 
2049
 
 
2050
static void
 
2051
compute_virtual_size_for_configuration (GnomeRRConfig *config, int *ret_width, int *ret_height)
 
2052
{
 
2053
  int i;
 
2054
  int width, height;
 
2055
  int output_x, output_y, output_width, output_height;
 
2056
  GnomeRROutputInfo **outputs;
 
2057
 
 
2058
  width = height = 0;
 
2059
 
 
2060
  outputs = gnome_rr_config_get_outputs (config);
 
2061
  for (i = 0; outputs[i] != NULL; i++)
 
2062
    {
 
2063
      if (gnome_rr_output_info_is_active (outputs[i]))
 
2064
        {
 
2065
          gnome_rr_output_info_get_geometry (outputs[i], &output_x, &output_y, &output_width, &output_height);
 
2066
          width = MAX (width, output_x + output_width);
 
2067
          height = MAX (height, output_y + output_height);
 
2068
        }
 
2069
    }
 
2070
 
 
2071
  *ret_width = width;
 
2072
  *ret_height = height;
 
2073
}
 
2074
 
 
2075
static void
 
2076
check_required_virtual_size (App *app)
 
2077
{
 
2078
  int req_width, req_height;
 
2079
  int min_width, max_width;
 
2080
  int min_height, max_height;
 
2081
 
 
2082
  compute_virtual_size_for_configuration (app->current_configuration, &req_width, &req_height);
 
2083
 
 
2084
  gnome_rr_screen_get_ranges (app->screen, &min_width, &max_width, &min_height, &max_height);
 
2085
 
 
2086
#if 0
 
2087
  g_debug ("X Server supports:");
 
2088
  g_debug ("min_width = %d, max_width = %d", min_width, max_width);
 
2089
  g_debug ("min_height = %d, max_height = %d", min_height, max_height);
 
2090
 
 
2091
  g_debug ("Requesting size of %dx%d", req_width, req_height);
 
2092
#endif
 
2093
 
 
2094
  if (!(min_width <= req_width && req_width <= max_width
 
2095
        && min_height <= req_height && req_height <= max_height))
 
2096
    {
 
2097
      /* FIXME: present a useful dialog, maybe even before the user tries to Apply */
 
2098
#if 0
 
2099
      g_debug ("Your X server needs a larger Virtual size!");
 
2100
#endif
 
2101
    }
 
2102
}
 
2103
 
 
2104
static void
 
2105
begin_version2_apply_configuration (App *app, GdkWindow *parent_window, guint32 timestamp)
 
2106
{
 
2107
  XID parent_window_xid;
 
2108
 
 
2109
  parent_window_xid = GDK_WINDOW_XID (parent_window);
 
2110
 
 
2111
  app->proxy = dbus_g_proxy_new_for_name (app->connection,
 
2112
                                          "org.gnome.SettingsDaemon",
 
2113
                                          "/org/gnome/SettingsDaemon/XRANDR",
 
2114
                                          "org.gnome.SettingsDaemon.XRANDR_2");
 
2115
  g_assert (app->proxy != NULL); /* that call does not fail unless we pass bogus names */
 
2116
 
 
2117
  app->proxy_call = dbus_g_proxy_begin_call (app->proxy, "ApplyConfiguration",
 
2118
                                             apply_configuration_returned_cb, app,
 
2119
                                             NULL,
 
2120
                                             G_TYPE_INT64, (gint64) parent_window_xid,
 
2121
                                             G_TYPE_INT64, (gint64) timestamp,
 
2122
                                             G_TYPE_INVALID,
 
2123
                                             G_TYPE_INVALID);
 
2124
  /* FIXME: we don't check for app->proxy_call == NULL, which could happen if
 
2125
   * the connection was disconnected.  This is left as an exercise for the
 
2126
   * reader.
 
2127
   */
 
2128
}
 
2129
 
 
2130
static void
 
2131
ensure_current_configuration_is_saved (void)
 
2132
{
 
2133
  GnomeRRScreen *rr_screen;
 
2134
  GnomeRRConfig *rr_config;
 
2135
 
 
2136
  /* Normally, gnome_rr_config_save() creates a backup file based on the
 
2137
   * old monitors.xml.  However, if *that* file didn't exist, there is
 
2138
   * nothing from which to create a backup.  So, here we'll save the
 
2139
   * current/unchanged configuration and then let our caller call
 
2140
   * gnome_rr_config_save() again with the new/changed configuration, so
 
2141
   * that there *will* be a backup file in the end.
 
2142
   */
 
2143
 
 
2144
  rr_screen = gnome_rr_screen_new (gdk_screen_get_default (), NULL); /* NULL-GError */
 
2145
  if (!rr_screen)
 
2146
    return;
 
2147
 
 
2148
  rr_config = gnome_rr_config_new_current (rr_screen, NULL);
 
2149
  gnome_rr_config_ensure_primary (rr_config);
 
2150
  gnome_rr_config_save (rr_config, NULL); /* NULL-GError */
 
2151
 
 
2152
  g_object_unref (rr_config);
 
2153
  g_object_unref (rr_screen);
 
2154
}
 
2155
 
 
2156
/* Callback for dbus_g_proxy_begin_call() */
 
2157
static void
 
2158
apply_configuration_returned_cb (DBusGProxy       *proxy,
 
2159
                                 DBusGProxyCall   *call_id,
 
2160
                                 void             *data)
 
2161
{
 
2162
  App *app = data;
 
2163
  gboolean success;
 
2164
  GError *error;
 
2165
 
 
2166
  g_assert (call_id == app->proxy_call);
 
2167
 
 
2168
  error = NULL;
 
2169
  success = dbus_g_proxy_end_call (proxy, call_id, &error, G_TYPE_INVALID);
 
2170
 
 
2171
  if (!success) {
 
2172
    /* We don't pop up an error message; gnome-settings-daemon already does that
 
2173
     * in case the selected RANDR configuration could not be applied.
 
2174
     */
 
2175
    g_error_free (error);
 
2176
  }
 
2177
 
 
2178
  g_object_unref (app->proxy);
 
2179
  app->proxy = NULL;
 
2180
 
 
2181
  dbus_g_connection_unref (app->connection);
 
2182
  app->connection = NULL;
 
2183
  app->proxy_call = NULL;
 
2184
 
 
2185
  gtk_widget_set_sensitive (app->panel, TRUE);
 
2186
}
 
2187
 
 
2188
static gboolean
 
2189
sanitize_and_save_configuration (App *app)
 
2190
{
 
2191
  GError *error;
 
2192
 
 
2193
  gnome_rr_config_sanitize (app->current_configuration);
 
2194
  gnome_rr_config_ensure_primary (app->current_configuration);
 
2195
 
 
2196
  check_required_virtual_size (app);
 
2197
 
 
2198
  foo_scroll_area_invalidate (FOO_SCROLL_AREA (app->area));
 
2199
 
 
2200
  ensure_current_configuration_is_saved ();
 
2201
 
 
2202
  error = NULL;
 
2203
  if (!gnome_rr_config_save (app->current_configuration, &error))
 
2204
    {
 
2205
      error_message (app, _("Could not save the monitor configuration"), error->message);
 
2206
      g_error_free (error);
 
2207
      return FALSE;
 
2208
    }
 
2209
 
 
2210
  return TRUE;
 
2211
}
 
2212
 
 
2213
static void
 
2214
apply (App *app)
 
2215
{
 
2216
  GError *error = NULL;
 
2217
  GdkWindow *window;
 
2218
 
 
2219
  app->apply_button_clicked_timestamp = gtk_get_current_event_time ();
 
2220
 
 
2221
  if (!sanitize_and_save_configuration (app))
 
2222
    return;
 
2223
 
 
2224
  g_assert (app->connection == NULL);
 
2225
  g_assert (app->proxy == NULL);
 
2226
  g_assert (app->proxy_call == NULL);
 
2227
 
 
2228
  app->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 
2229
  if (app->connection == NULL) {
 
2230
    error_message (app, _("Could not get session bus while applying display configuration"), error->message);
 
2231
    g_error_free (error);
 
2232
    return;
 
2233
  }
 
2234
 
 
2235
  gtk_widget_set_sensitive (app->panel, FALSE);
 
2236
 
 
2237
  window = gtk_widget_get_window (gtk_widget_get_toplevel (app->panel));
 
2238
 
 
2239
  begin_version2_apply_configuration (app, window,
 
2240
                                      app->apply_button_clicked_timestamp);
 
2241
}
 
2242
 
 
2243
#if 0
 
2244
/* Returns whether the graphics driver doesn't advertise RANDR 1.2 features, and just 1.0 */
 
2245
static gboolean
 
2246
driver_is_randr_10 (GnomeRRConfig *config)
 
2247
{
 
2248
  /* In the Xorg code, see xserver/randr/rrinfo.c:RRScanOldConfig().  It gets
 
2249
   * called when the graphics driver doesn't support RANDR 1.2 yet, just 1.0.
 
2250
   * In that case, the X server's base code (which supports RANDR 1.2) will
 
2251
   * simulate having a single output called "default".  For drivers that *do*
 
2252
   * support RANDR 1.2, the separate outputs will be named differently, we
 
2253
   * hope.
 
2254
   *
 
2255
   * This heuristic is courtesy of Dirk Mueller <dmueller@suse.de>
 
2256
   *
 
2257
   * FIXME: however, we don't even check for XRRQueryVersion() returning 1.2, neither
 
2258
   * here nor in gnome-desktop/libgnomedesktop*.c.  Do we need to check for that,
 
2259
   * or is gnome_rr_screen_new()'s return value sufficient?
 
2260
   */
 
2261
 
 
2262
  return (count_all_outputs (config) == 1 && strcmp (gnome_rr_output_info_get_name (gnome_rr_config_get_outputs (config)[0]), "default") == 0);
 
2263
}
 
2264
#endif
 
2265
 
 
2266
static void
 
2267
on_detect_displays (GtkWidget *widget, gpointer data)
 
2268
{
 
2269
  App *app = data;
 
2270
  GError *error;
 
2271
 
 
2272
  error = NULL;
 
2273
  if (!gnome_rr_screen_refresh (app->screen, &error)) {
 
2274
    if (error) {
 
2275
      error_message (app, _("Could not detect displays"), error->message);
 
2276
      g_error_free (error);
 
2277
    }
 
2278
  }
 
2279
}
 
2280
 
 
2281
static GnomeRROutputInfo *
 
2282
get_nearest_output (GnomeRRConfig *configuration, int x, int y)
 
2283
{
 
2284
  int i;
 
2285
  int nearest_index;
 
2286
  int nearest_dist;
 
2287
  GnomeRROutputInfo **outputs;
 
2288
 
 
2289
  nearest_index = -1;
 
2290
  nearest_dist = G_MAXINT;
 
2291
 
 
2292
  outputs = gnome_rr_config_get_outputs (configuration);
 
2293
  for (i = 0; outputs[i] != NULL; i++)
 
2294
    {
 
2295
      int dist_x, dist_y;
 
2296
      int output_x, output_y, output_width, output_height;
 
2297
 
 
2298
      if (!(gnome_rr_output_info_is_connected (outputs[i]) && gnome_rr_output_info_is_active (outputs[i])))
 
2299
        continue;
 
2300
 
 
2301
      gnome_rr_output_info_get_geometry (outputs[i], &output_x, &output_y, &output_width, &output_height);
 
2302
 
 
2303
      if (x < output_x)
 
2304
        dist_x = output_x - x;
 
2305
      else if (x >= output_x + output_width)
 
2306
        dist_x = x - (output_x + output_width) + 1;
 
2307
      else
 
2308
        dist_x = 0;
 
2309
 
 
2310
      if (y < output_y)
 
2311
        dist_y = output_y - y;
 
2312
      else if (y >= output_y + output_height)
 
2313
        dist_y = y - (output_y + output_height) + 1;
 
2314
      else
 
2315
        dist_y = 0;
 
2316
 
 
2317
      if (MIN (dist_x, dist_y) < nearest_dist)
 
2318
        {
 
2319
          nearest_dist = MIN (dist_x, dist_y);
 
2320
          nearest_index = i;
 
2321
        }
 
2322
    }
 
2323
 
 
2324
  if (nearest_index != -1)
 
2325
    return outputs[nearest_index];
 
2326
  else
 
2327
    return NULL;
 
2328
}
 
2329
 
 
2330
/* Gets the output that contains the largest intersection with the window.
 
2331
 * Logic stolen from gdk_screen_get_monitor_at_window().
 
2332
 */
 
2333
static GnomeRROutputInfo *
 
2334
get_output_for_window (GnomeRRConfig *configuration, GdkWindow *window)
 
2335
{
 
2336
  GdkRectangle win_rect;
 
2337
  int i;
 
2338
  int largest_area;
 
2339
  int largest_index;
 
2340
  GnomeRROutputInfo **outputs;
 
2341
 
 
2342
  gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width, &win_rect.height);
 
2343
  gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
 
2344
 
 
2345
  largest_area = 0;
 
2346
  largest_index = -1;
 
2347
 
 
2348
  outputs = gnome_rr_config_get_outputs (configuration);
 
2349
  for (i = 0; outputs[i] != NULL; i++)
 
2350
    {
 
2351
      GdkRectangle output_rect, intersection;
 
2352
 
 
2353
      gnome_rr_output_info_get_geometry (outputs[i], &output_rect.x, &output_rect.y, &output_rect.width, &output_rect.height);
 
2354
 
 
2355
      if (gnome_rr_output_info_is_connected (outputs[i]) && gdk_rectangle_intersect (&win_rect, &output_rect, &intersection))
 
2356
        {
 
2357
          int area;
 
2358
 
 
2359
          area = intersection.width * intersection.height;
 
2360
          if (area > largest_area)
 
2361
            {
 
2362
              largest_area = area;
 
2363
              largest_index = i;
 
2364
            }
 
2365
        }
 
2366
    }
 
2367
 
 
2368
  if (largest_index != -1)
 
2369
    return outputs[largest_index];
 
2370
  else
 
2371
    return get_nearest_output (configuration,
 
2372
                               win_rect.x + win_rect.width / 2,
 
2373
                               win_rect.y + win_rect.height / 2);
 
2374
}
 
2375
 
 
2376
static void
 
2377
on_toplevel_realized (GtkWidget *widget,
 
2378
                      App       *app)
 
2379
{
 
2380
  app->current_output = get_output_for_window (app->current_configuration,
 
2381
                                               gtk_widget_get_window (widget));
 
2382
  rebuild_gui (app);
 
2383
}
 
2384
 
 
2385
/* We select the current output, i.e. select the one being edited, based on
 
2386
 * which output is showing the configuration dialog.
 
2387
 */
 
2388
static void
 
2389
select_current_output_from_dialog_position (App *app)
 
2390
{
 
2391
  GtkWidget *toplevel;
 
2392
 
 
2393
  toplevel = gtk_widget_get_toplevel (app->panel);
 
2394
 
 
2395
  if (gtk_widget_get_realized (toplevel)) {
 
2396
    app->current_output = get_output_for_window (app->current_configuration,
 
2397
                                                 gtk_widget_get_window (toplevel));
 
2398
    rebuild_gui (app);
 
2399
  } else {
 
2400
    g_signal_connect (toplevel, "realize", G_CALLBACK (on_toplevel_realized), app);
 
2401
    app->current_output = NULL;
 
2402
  }
 
2403
}
 
2404
 
 
2405
/* This is a GtkWidget::map-event handler.  We wait for the display-properties
 
2406
 * dialog to be mapped, and then we select the output which corresponds to the
 
2407
 * monitor on which the dialog is being shown.
 
2408
 */
 
2409
static gboolean
 
2410
dialog_map_event_cb (GtkWidget *widget, GdkEventAny *event, gpointer data)
 
2411
{
 
2412
  App *app = data;
 
2413
 
 
2414
  select_current_output_from_dialog_position (app);
 
2415
  return FALSE;
 
2416
}
 
2417
 
 
2418
 
 
2419
static GtkWidget*
 
2420
_gtk_builder_get_widget (GtkBuilder *builder, const gchar *name)
 
2421
{
 
2422
  return GTK_WIDGET (gtk_builder_get_object (builder, name));
 
2423
}
 
2424
 
 
2425
static void
 
2426
destroy_app (App *app)
 
2427
{
 
2428
  g_object_unref (app->screen);
 
2429
  g_object_unref (app->builder);
 
2430
 
 
2431
  if (app->clock_settings != NULL)
 
2432
    g_object_unref (app->clock_settings);
 
2433
 
 
2434
  gnome_rr_labeler_hide (app->labeler);
 
2435
  g_object_unref (app->labeler);
 
2436
 
 
2437
  g_free (app);
 
2438
}
 
2439
 
 
2440
GtkWidget*
 
2441
run_application (void)
 
2442
{
 
2443
#ifndef UIDIR
 
2444
#define UIDIR "."
 
2445
#endif
 
2446
#define UI_FILE UIDIR "/display-capplet.ui"
 
2447
  GtkBuilder *builder;
 
2448
  GtkWidget *align;
 
2449
  GError *error;
 
2450
  gchar *objects[] = {"display-panel", "rotation-liststore", NULL};
 
2451
  App *app;
 
2452
 
 
2453
  app = g_new0 (App, 1);
 
2454
 
 
2455
  error = NULL;
 
2456
  app->builder = builder = gtk_builder_new ();
 
2457
 
 
2458
  if (!gtk_builder_add_objects_from_file (builder, UI_FILE, objects, &error))
 
2459
    {
 
2460
      g_warning ("Could not parse UI definition: %s", error->message);
 
2461
      g_error_free (error);
 
2462
      g_object_unref (builder);
 
2463
      return NULL;
 
2464
    }
 
2465
 
 
2466
  app->screen = gnome_rr_screen_new (gdk_screen_get_default (), &error);
 
2467
  g_signal_connect (app->screen, "changed", G_CALLBACK (on_screen_changed), app);
 
2468
  if (!app->screen)
 
2469
    {
 
2470
      error_message (NULL, _("Could not get screen information"), error->message);
 
2471
      g_error_free (error);
 
2472
      g_object_unref (builder);
 
2473
      return NULL;
 
2474
    }
 
2475
 
 
2476
  app->clock_settings = g_settings_new (CLOCK_SCHEMA);
 
2477
 
 
2478
  app->panel = _gtk_builder_get_widget (builder, "display-panel");
 
2479
 
 
2480
  if (!app->panel)
 
2481
    g_warning ("Missing display-panel object");
 
2482
  g_signal_connect_after (app->panel, "show",
 
2483
                          G_CALLBACK (dialog_map_event_cb), app);
 
2484
 
 
2485
  app->current_monitor_event_box = _gtk_builder_get_widget (builder,
 
2486
                                                            "current_monitor_event_box");
 
2487
  app->current_monitor_label = _gtk_builder_get_widget (builder,
 
2488
                                                        "current_monitor_label");
 
2489
 
 
2490
  app->monitor_switch = _gtk_builder_get_widget (builder,
 
2491
                                                 "monitor_switch");
 
2492
  g_signal_connect (app->monitor_switch, "notify::active",
 
2493
                    G_CALLBACK (monitor_switch_active_cb), app);
 
2494
 
 
2495
  app->resolution_combo = _gtk_builder_get_widget (builder,
 
2496
                                                   "resolution_combo");
 
2497
  g_signal_connect (app->resolution_combo, "changed",
 
2498
                    G_CALLBACK (on_resolution_changed), app);
 
2499
 
 
2500
  app->rotation_combo = _gtk_builder_get_widget (builder, "rotation_combo");
 
2501
  g_signal_connect (app->rotation_combo, "changed",
 
2502
                    G_CALLBACK (on_rotation_changed), app);
 
2503
 
 
2504
  app->clone_checkbox = _gtk_builder_get_widget (builder, "clone_checkbox");
 
2505
  g_signal_connect (app->clone_checkbox, "toggled",
 
2506
                    G_CALLBACK (on_clone_changed), app);
 
2507
 
 
2508
  app->clone_label    = _gtk_builder_get_widget (builder, "clone_resolution_warning_label");
 
2509
 
 
2510
  g_signal_connect (_gtk_builder_get_widget (builder, "detect_displays_button"),
 
2511
                    "clicked", G_CALLBACK (on_detect_displays), app);
 
2512
 
 
2513
  make_text_combo (app->resolution_combo, 4);
 
2514
  make_text_combo (app->rotation_combo, -1);
 
2515
 
 
2516
  /* Scroll Area */
 
2517
  app->area = (GtkWidget *)foo_scroll_area_new ();
 
2518
 
 
2519
  g_object_set_data (G_OBJECT (app->area), "app", app);
 
2520
 
 
2521
  set_monitors_tooltip (app, FALSE);
 
2522
 
 
2523
  /* FIXME: this should be computed dynamically */
 
2524
  foo_scroll_area_set_min_size (FOO_SCROLL_AREA (app->area), 0, 200);
 
2525
  gtk_widget_show (app->area);
 
2526
  g_signal_connect (app->area, "paint",
 
2527
                    G_CALLBACK (on_area_paint), app);
 
2528
  g_signal_connect (app->area, "viewport_changed",
 
2529
                    G_CALLBACK (on_viewport_changed), app);
 
2530
 
 
2531
  align = _gtk_builder_get_widget (builder, "align");
 
2532
 
 
2533
  gtk_container_add (GTK_CONTAINER (align), app->area);
 
2534
 
 
2535
  on_screen_changed (app->screen, app);
 
2536
 
 
2537
  g_signal_connect_swapped (_gtk_builder_get_widget (builder, "apply_button"),
 
2538
                            "clicked", G_CALLBACK (apply), app);
 
2539
 
 
2540
  g_object_weak_ref (G_OBJECT (app->panel), (GWeakNotify) destroy_app, app);
 
2541
 
 
2542
 
 
2543
  return app->panel;
 
2544
}