selector.c 38.2 KB
Newer Older
1 2
/* selector */
/* vim: set sw=2 et: */
3 4 5 6
/*
 * Copyright (C) 2003 Sun Microsystems, Inc.
 * Copyright (C) 2001 Free Software Foundation, Inc.
 * Copyright (C) 2000 Helix Code, Inc.
7
 * Copyright (C) 2005-2007 Vincent Untz
8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
20
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 22 23 24 25 26 27
 *
 * Authors:
 *      Mark McLoughlin <mark@skynet.ie>
 *      George Lebl <jirka@5z.com>
 *      Jacob Berkman <jacob@helixcode.com>
 */

Vincent Untz's avatar
Vincent Untz committed
28 29
#include <config.h>

30 31
#include <gtk/gtk.h>

Vincent Untz's avatar
Vincent Untz committed
32
#include <glib/gi18n-lib.h>
33 34 35
#include "selector.h"
#include "libwnck.h"
#include "screen.h"
36
#include "wnck-image-menu-item-private.h"
37
#include "private.h"
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/**
 * SECTION:selector
 * @short_description: a window selector widget, showing the list of windows as
 * a menu.
 * @see_also: #WnckTasklist
 * @stability: Unstable
 *
 * The #WnckSelector represents client windows on a screen as a menu, where
 * menu items are labelled with the window titles and icons. Activating a menu
 * item activates the represented window.
 *
 * The #WnckSelector will automatically detect the screen it is on, and will
 * represent windows of this screen only.
 */

54
struct _WnckSelectorPrivate {
55
  GtkWidget  *image;
56
  WnckWindow *icon_window;
57 58 59 60

  /* those have the same lifecycle as the menu */
  GtkWidget  *menu;
  GtkWidget  *no_windows_item;
61 62 63
  GHashTable *window_hash;
};

64
G_DEFINE_TYPE_WITH_PRIVATE (WnckSelector, wnck_selector, GTK_TYPE_MENU_BAR);
65

66 67 68
static GObject *wnck_selector_constructor (GType                  type,
                                           guint                  n_construct_properties,
                                           GObjectConstructParam *construct_properties);
69
static void wnck_selector_dispose           (GObject           *object);
70
static void wnck_selector_finalize          (GObject           *object);
71 72
static void wnck_selector_realize           (GtkWidget *widget);
static void wnck_selector_unrealize         (GtkWidget *widget);
73 74
static gboolean wnck_selector_scroll_event  (GtkWidget        *widget,
                                             GdkEventScroll   *event);
75 76 77
static void wnck_selector_connect_to_window (WnckSelector      *selector,
                                             WnckWindow        *window);

78 79 80 81 82
static void wnck_selector_insert_window (WnckSelector *selector,
                                         WnckWindow   *window);
static void wnck_selector_append_window (WnckSelector *selector,
                                         WnckWindow   *window);

83 84 85 86 87 88 89 90 91 92 93 94 95
static gint
wnck_selector_windows_compare (gconstpointer  a,
                               gconstpointer  b)
{
  int posa;
  int posb;

  posa = wnck_window_get_sort_order (WNCK_WINDOW (a));
  posb = wnck_window_get_sort_order (WNCK_WINDOW (b));

  return (posa - posb);
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static void
wncklet_connect_while_alive (gpointer object,
                             const char *signal,
                             GCallback func,
                             gpointer func_data, gpointer alive_object)
{
  GClosure *closure;

  closure = g_cclosure_new (func, func_data, NULL);
  g_object_watch_closure (G_OBJECT (alive_object), closure);
  g_signal_connect_closure_by_id (object,
                                  g_signal_lookup (signal,
                                                   G_OBJECT_TYPE (object)), 0,
                                  closure, FALSE);
}

112 113 114 115 116
static WnckScreen *
wnck_selector_get_screen (WnckSelector *selector)
{
  GdkScreen *screen;

117
  g_assert (gtk_widget_has_screen (GTK_WIDGET (selector)));
118 119 120

  screen = gtk_widget_get_screen (GTK_WIDGET (selector));

121
  return wnck_screen_get (gdk_x11_screen_get_screen_number (screen));
122 123 124 125 126 127 128 129 130 131
}

static GdkPixbuf *
wnck_selector_get_default_window_icon (void)
{
  static GdkPixbuf *retval = NULL;

  if (retval)
    return retval;

132
  retval = gdk_pixbuf_new_from_resource ("/org/gnome/libwnck/default_icon.png", NULL);
133 134 135 136 137 138

  g_assert (retval);

  return retval;
}

139
static GdkPixbuf *
140 141 142 143 144
wnck_selector_dimm_icon (GdkPixbuf *pixbuf)
{
  int x, y, pixel_stride, row_stride;
  guchar *row, *pixels;
  int w, h;
145
  GdkPixbuf *dimmed;
146 147 148 149

  w = gdk_pixbuf_get_width (pixbuf);
  h = gdk_pixbuf_get_height (pixbuf);

Vincent Untz's avatar
Vincent Untz committed
150
  if (gdk_pixbuf_get_has_alpha (pixbuf))
151 152 153
    dimmed = gdk_pixbuf_copy (pixbuf);
  else
    dimmed = gdk_pixbuf_add_alpha (pixbuf, FALSE, 0, 0, 0);
154 155 156

  pixel_stride = 4;

157 158
  row = gdk_pixbuf_get_pixels (dimmed);
  row_stride = gdk_pixbuf_get_rowstride (dimmed);
159 160 161 162 163 164 165 166 167 168 169

  for (y = 0; y < h; y++)
    {
      pixels = row;
      for (x = 0; x < w; x++)
        {
          pixels[3] /= 2;
          pixels += pixel_stride;
        }
      row += row_stride;
    }
170 171

  return dimmed;
172 173
}

174 175 176
void
_wnck_selector_set_window_icon (GtkWidget  *image,
                                WnckWindow *window)
177
{
178
  GdkPixbuf *pixbuf, *freeme, *freeme2;
179 180 181 182 183
  int width, height;
  int icon_size = -1;

  pixbuf = NULL;
  freeme = NULL;
184
  freeme2 = NULL;
185 186

  if (window)
187
    pixbuf = wnck_window_get_mini_icon (window);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

  if (!pixbuf)
    pixbuf = wnck_selector_get_default_window_icon ();

  if (icon_size == -1)
    gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, NULL, &icon_size);

  width = gdk_pixbuf_get_width (pixbuf);
  height = gdk_pixbuf_get_height (pixbuf);

  if (icon_size != -1 && (width > icon_size || height > icon_size))
    {
      double scale;

      scale = ((double) icon_size) / MAX (width, height);

      pixbuf = gdk_pixbuf_scale_simple (pixbuf, width * scale,
                                        height * scale, GDK_INTERP_BILINEAR);
      freeme = pixbuf;
    }

  if (window && wnck_window_is_minimized (window))
Vincent Untz's avatar
Vincent Untz committed
210
    {
211 212 213
      pixbuf = wnck_selector_dimm_icon (pixbuf);
      freeme2 = pixbuf;
    }
214 215 216 217 218

  gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);

  if (freeme)
    g_object_unref (freeme);
219 220
  if (freeme2)
    g_object_unref (freeme2);
221 222 223 224 225
}

static void
wnck_selector_set_active_window (WnckSelector *selector, WnckWindow *window)
{
226
  _wnck_selector_set_window_icon (selector->priv->image, window);
227
  selector->priv->icon_window = window;
228 229
}

230 231 232
static void
wnck_selector_make_menu_consistent (WnckSelector *selector)
{
233
  GList     *l, *children;
234 235
  int        workspace_n;
  GtkWidget *workspace_item;
236 237 238 239 240
  GtkWidget *separator;
  gboolean   separator_is_first;
  gboolean   separator_is_last;
  gboolean   visible_window;

241 242 243 244
  workspace_n = -1;
  workspace_item = NULL;

  separator = NULL;
245 246
  separator_is_first = FALSE;
  separator_is_last = FALSE;
247

248 249
  visible_window = FALSE;

250 251 252
  children = gtk_container_get_children (GTK_CONTAINER (selector->priv->menu));

  for (l = children; l; l = l->next)
253
    {
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      int i;

      i = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (l->data),
                                              "wnck-selector-workspace-n"));

      if (i > 0)
        {
          workspace_n = i - 1;

          /* we have two consecutive workspace items => hide the first */
          if (workspace_item)
            gtk_widget_hide (workspace_item);

          workspace_item = GTK_WIDGET (l->data);
        }
      else if (GTK_IS_SEPARATOR_MENU_ITEM (l->data))
270 271 272 273 274 275
        {
          if (!visible_window)
            separator_is_first = TRUE;
          separator_is_last = TRUE;
          separator = GTK_WIDGET (l->data);
        }
276
      else if (gtk_widget_get_visible (l->data) &&
277
               l->data != selector->priv->no_windows_item)
278 279 280
        {
          separator_is_last = FALSE;
          visible_window = TRUE;
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

          /* if we know of a workspace item that was not shown */
          if (workspace_item)
            {
              WnckWindow    *window;
              WnckWorkspace *workspace;

              window = g_object_get_data (G_OBJECT (l->data),
                                          "wnck-selector-window");

              if (window)
                {
                  workspace = wnck_window_get_workspace (window);
                  if (workspace &&
                      workspace_n == wnck_workspace_get_number (workspace))
                    {
                      gtk_widget_show (workspace_item);
                      workspace_n = -1;
                      workspace_item = NULL;
                    }
                }
            }
        } /* end if (normal item) */
304 305
    }

306 307
  g_list_free (children);

308 309 310 311 312 313 314 315 316 317 318
  /* do we have a trailing workspace item to be hidden? */
  if (workspace_item)
    gtk_widget_hide (workspace_item);

  if (separator)
    {
      if (separator_is_first || separator_is_last)
        gtk_widget_hide (separator);
      else
        gtk_widget_show (separator);
    }
319 320 321 322 323 324 325

  if (visible_window)
    gtk_widget_hide (selector->priv->no_windows_item);
  else
    gtk_widget_show (selector->priv->no_windows_item);
}

326 327 328 329
static void
wnck_selector_window_icon_changed (WnckWindow *window,
                                   WnckSelector *selector)
{
330
  GtkWidget *item;
331

332
  if (selector->priv->icon_window == window)
333 334
    wnck_selector_set_active_window (selector, window);

335
  if (!selector->priv->window_hash)
336 337
	  return;

338
  item = g_hash_table_lookup (selector->priv->window_hash, window);
339 340
  if (item != NULL)
    {
341 342
      wnck_image_menu_item_set_image_from_window (WNCK_IMAGE_MENU_ITEM (item),
                                                  window);
343 344 345 346 347 348 349
    }
}

static void
wnck_selector_window_name_changed (WnckWindow *window,
                                   WnckSelector *selector)
{
350
  GtkWidget *item;
351 352
  char *window_name;

353
  if (!selector->priv->window_hash)
354 355
	  return;

356
  item = g_hash_table_lookup (selector->priv->window_hash, window);
357 358
  if (item != NULL)
    {
359
      window_name = _wnck_window_get_name_for_display (window, FALSE, TRUE);
360
      gtk_menu_item_set_label (GTK_MENU_ITEM (item), window_name);
361
      g_free (window_name);
362 363 364 365 366 367 368 369 370
    }
}

static void
wnck_selector_window_state_changed (WnckWindow *window,
                                    WnckWindowState changed_mask,
                                    WnckWindowState new_state,
                                    WnckSelector *selector)
{
371
  GtkWidget *item;
372 373 374 375 376 377
  char *window_name;

  if (!
      (changed_mask &
       (WNCK_WINDOW_STATE_MINIMIZED | WNCK_WINDOW_STATE_SHADED |
        WNCK_WINDOW_STATE_SKIP_TASKLIST |
378 379
        WNCK_WINDOW_STATE_DEMANDS_ATTENTION |
        WNCK_WINDOW_STATE_URGENT)))
380 381
    return;

382
  if (!selector->priv->window_hash)
383 384
	  return;

385
  item = g_hash_table_lookup (selector->priv->window_hash, window);
386 387 388 389 390 391
  if (item == NULL)
    return;

  if (changed_mask & WNCK_WINDOW_STATE_SKIP_TASKLIST)
    {
      if (wnck_window_is_skip_tasklist (window))
392
        gtk_widget_hide (item);
393
      else
394
        gtk_widget_show (item);
395 396 397 398

      wnck_selector_make_menu_consistent (selector);

      gtk_menu_reposition (GTK_MENU (selector->priv->menu));
399 400
    }

Vincent Untz's avatar
Vincent Untz committed
401 402
  if (changed_mask &
      (WNCK_WINDOW_STATE_DEMANDS_ATTENTION | WNCK_WINDOW_STATE_URGENT))
403 404
    {
      if (wnck_window_or_transient_needs_attention (window))
405
        wnck_image_menu_item_make_label_bold (WNCK_IMAGE_MENU_ITEM (item));
406
      else
407
        wnck_image_menu_item_make_label_normal (WNCK_IMAGE_MENU_ITEM (item));
408 409
    }

410
  if (changed_mask &
411
      (WNCK_WINDOW_STATE_MINIMIZED | WNCK_WINDOW_STATE_SHADED))
412
    {
413
      window_name = _wnck_window_get_name_for_display (window, FALSE, TRUE);
414
      gtk_menu_item_set_label (GTK_MENU_ITEM (item), window_name);
415
      g_free (window_name);
416 417 418
    }
}

419 420 421 422
static void
wnck_selector_window_workspace_changed (WnckWindow   *window,
                                        WnckSelector *selector)
{
423
  GtkWidget *item;
424

425
  if (!selector->priv->menu || !gtk_widget_get_visible (selector->priv->menu))
426 427
    return;

428 429 430 431 432 433 434 435
  if (!selector->priv->window_hash)
    return;

  item = g_hash_table_lookup (selector->priv->window_hash, window);
  if (!item)
    return;

  /* destroy the item and recreate one so it's at the right position */
436
  gtk_widget_destroy (item);
437 438 439 440 441 442 443 444
  g_hash_table_remove (selector->priv->window_hash, window);

  wnck_selector_insert_window (selector, window);
  wnck_selector_make_menu_consistent (selector);

  gtk_menu_reposition (GTK_MENU (selector->priv->menu));
}

445
static void
446 447
wnck_selector_active_window_changed (WnckScreen   *screen,
                                     WnckWindow   *previous_window,
448 449 450 451 452 453
                                     WnckSelector *selector)
{
  WnckWindow *window;

  window = wnck_screen_get_active_window (screen);

454
  if (selector->priv->icon_window != window)
455 456 457 458 459 460
    wnck_selector_set_active_window (selector, window);
}

static void
wnck_selector_activate_window (WnckWindow *window)
{
461 462 463
  WnckWorkspace *workspace;
  guint32 timestamp;

464
  /* We're in an activate callback, so gtk_get_current_time() works... */
465 466 467 468 469 470 471
  timestamp = gtk_get_current_event_time ();

  /* FIXME: THIS IS SICK AND WRONG AND BUGGY.  See the end of
   * http://mail.gnome.org/archives/wm-spec-list/2005-July/msg00032.html
   * There should only be *one* activate call.
   */
  workspace = wnck_window_get_workspace (window);
472 473
  if (workspace)
    wnck_workspace_activate (workspace, timestamp);
474 475

  wnck_window_activate (window, timestamp);
476 477
}

Vincent Untz's avatar
Vincent Untz committed
478
static void
479 480 481 482
wnck_selector_drag_begin (GtkWidget          *widget,
			  GdkDragContext     *context,
			  WnckWindow         *window)
{
483 484 485 486 487 488 489 490
  while (widget)
    {
      if (WNCK_IS_SELECTOR (widget))
        break;

      if (GTK_IS_MENU (widget))
        widget = gtk_menu_get_attach_widget (GTK_MENU (widget));
      else
491
        widget = gtk_widget_get_parent (widget);
492 493 494 495
    }

  if (widget)
    _wnck_window_set_as_drag_icon (window, context, widget);
496 497
}

Vincent Untz's avatar
Vincent Untz committed
498
static void
499 500 501 502 503 504 505
wnck_selector_drag_data_get (GtkWidget          *widget,
			     GdkDragContext     *context,
			     GtkSelectionData   *selection_data,
			     guint               info,
			     guint               time,
			     WnckWindow         *window)
{
Vincent Untz's avatar
Vincent Untz committed
506
  gulong xid;
507 508 509

  xid = wnck_window_get_xid (window);
  gtk_selection_data_set (selection_data,
510
                          gtk_selection_data_get_target (selection_data),
511 512 513
			  8, (guchar *)&xid, sizeof (gulong));
}

514 515 516 517 518
static GtkWidget *
wnck_selector_item_new (WnckSelector *selector,
                        const gchar *label, WnckWindow *window)
{
  GtkWidget *item;
519
  static const GtkTargetEntry targets[] = {
520
    { (gchar *) "application/x-wnck-window-id", 0, 0 }
521
  };
522

523
  item = wnck_image_menu_item_new_with_label (label);
524 525 526

  if (window != NULL)
    {
527 528
      /* if window demands attention, bold the label */
      if (wnck_window_or_transient_needs_attention (window))
529
        wnck_image_menu_item_make_label_bold (WNCK_IMAGE_MENU_ITEM (item));
530

531
      g_hash_table_insert (selector->priv->window_hash, window, item);
532 533
    }

534 535 536 537 538 539 540 541 542 543
  if (window != NULL)
    {
      gtk_drag_source_set (item,
                           GDK_BUTTON1_MASK,
                           targets, 1,
                           GDK_ACTION_MOVE);

      g_signal_connect_object (item, "drag_data_get",
                               G_CALLBACK (wnck_selector_drag_data_get),
                               G_OBJECT (window),
Vincent Untz's avatar
Vincent Untz committed
544
                               0);
545 546 547 548

      g_signal_connect_object (item, "drag_begin",
                               G_CALLBACK (wnck_selector_drag_begin),
                               G_OBJECT (window),
Vincent Untz's avatar
Vincent Untz committed
549
                               0);
550 551 552 553 554 555 556 557 558
    }

  return item;
}

static void
wnck_selector_workspace_name_changed (WnckWorkspace *workspace,
                                      GtkLabel      *label)
{
559 560 561 562
  GtkStyleContext *context;
  GdkRGBA          color;
  char            *name;
  char            *markup;
563

564
  context = gtk_widget_get_style_context (GTK_WIDGET (label));
565 566 567

  gtk_style_context_save (context);
  gtk_style_context_set_state (context, GTK_STATE_FLAG_INSENSITIVE);
568
  gtk_style_context_get_color (context, GTK_STATE_FLAG_INSENSITIVE, &color);
569
  gtk_style_context_restore (context);
570 571 572

  name = g_markup_escape_text (wnck_workspace_get_name (workspace), -1);
  markup = g_strdup_printf ("<span size=\"x-small\" style=\"italic\" foreground=\"#%.2x%.2x%.2x\">%s</span>",
573 574 575
                            (int)(color.red * 65535 + 0.5),
                            (int)(color.green * 65535 + 0.5),
                            (int)(color.blue * 65535 + 0.5), name);
576 577 578 579
  g_free (name);

  gtk_label_set_markup (label, markup);
  g_free (markup);
580 581
}

582
static void
583 584
wnck_selector_workspace_label_style_updated (GtkLabel      *label,
                                             WnckWorkspace *workspace)
585 586 587 588
{
  wnck_selector_workspace_name_changed (workspace, label);
}

589
static void
590 591 592 593 594 595 596 597 598 599
wnck_selector_add_workspace (WnckSelector *selector,
                             WnckScreen   *screen,
                             int           workspace_n)
{
  WnckWorkspace *workspace;
  GtkWidget     *item;
  GtkWidget     *label;

  workspace = wnck_screen_get_workspace (screen, workspace_n);

600 601 602
  /* We use a separator in which we add a label. This makes the menu item not
   * selectable without any hack. */
  item = gtk_separator_menu_item_new ();
603 604

  label = gtk_label_new ("");
605
  gtk_label_set_xalign (GTK_LABEL (label), 1.0);
606
  gtk_widget_show (label);
607 608
  /* the handler will also take care of setting the name for the first time,
   * and we'll be able to adapt to theme changes */
609 610
  g_signal_connect (G_OBJECT (label), "style-updated",
                    G_CALLBACK (wnck_selector_workspace_label_style_updated),
611
		    workspace);
612 613 614 615 616 617 618 619 620 621 622 623 624 625
  wncklet_connect_while_alive (workspace, "name_changed",
                               G_CALLBACK (wnck_selector_workspace_name_changed),
                                label, label);

  gtk_container_add (GTK_CONTAINER (item), label);

  gtk_menu_shell_append (GTK_MENU_SHELL (selector->priv->menu), item);

  g_object_set_data (G_OBJECT (item), "wnck-selector-workspace-n",
                     GINT_TO_POINTER (workspace_n + 1));
}

static GtkWidget *
wnck_selector_create_window (WnckSelector *selector, WnckWindow *window)
626 627 628 629
{
  GtkWidget *item;
  char *name;

630
  name = _wnck_window_get_name_for_display (window, FALSE, TRUE);
631 632

  item = wnck_selector_item_new (selector, name, window);
633
  g_free (name);
634

635 636
  wnck_image_menu_item_set_image_from_window (WNCK_IMAGE_MENU_ITEM (item),
                                              window);
637 638 639 640 641

  g_signal_connect_swapped (item, "activate",
                            G_CALLBACK (wnck_selector_activate_window),
                            window);

642 643
  if (!wnck_window_is_skip_tasklist (window))
    gtk_widget_show (item);
644 645 646 647

  g_object_set_data (G_OBJECT (item), "wnck-selector-window", window);

  return item;
648 649 650
}

static void
651
wnck_selector_insert_window (WnckSelector *selector, WnckWindow *window)
652
{
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
  GtkWidget     *item;
  WnckScreen    *screen;
  WnckWorkspace *workspace;
  int            workspace_n;
  int            i;

  screen = wnck_selector_get_screen (selector);
  workspace = wnck_window_get_workspace (window);

  if (!workspace && !wnck_window_is_pinned (window))
    return;

  item = wnck_selector_create_window (selector, window);

  if (!workspace || workspace == wnck_screen_get_active_workspace (screen))
668
    {
669 670
      /* window is pinned or in the current workspace
       * => insert before the separator */
671
      GList *l, *children;
672

673 674
      i = 0;

675 676
      children = gtk_container_get_children (GTK_CONTAINER (selector->priv->menu));
      for (l = children; l; l = l->next)
677 678 679 680 681
        {
          if (GTK_IS_SEPARATOR_MENU_ITEM (l->data))
            break;
          i++;
        }
682
      g_list_free (children);
683 684 685 686

      gtk_menu_shell_insert (GTK_MENU_SHELL (selector->priv->menu),
                             item, i);
    }
Vincent Untz's avatar
Vincent Untz committed
687
  else
688 689 690 691 692 693 694 695 696
    {
      workspace_n = wnck_workspace_get_number (workspace);

      if (workspace_n == wnck_screen_get_workspace_count (screen) - 1)
        /* window is in last workspace => just append */
        gtk_menu_shell_append (GTK_MENU_SHELL (selector->priv->menu), item);
      else
        {
          /* insert just before the next workspace item */
697
          GList *l, *children;
698 699 700

          i = 0;

701 702
          children = gtk_container_get_children (GTK_CONTAINER (selector->priv->menu));
          for (l = children; l; l = l->next)
703 704 705 706 707 708 709 710
            {
              int j;
              j = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (l->data),
                                                      "wnck-selector-workspace-n"));
              if (j - 1 == workspace_n + 1)
                break;
              i++;
            }
711
          g_list_free (children);
712 713 714 715

          gtk_menu_shell_insert (GTK_MENU_SHELL (selector->priv->menu),
                                 item, i);
        }
716
    }
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
}

static void
wnck_selector_append_window (WnckSelector *selector, WnckWindow *window)
{
  GtkWidget *item;

  item = wnck_selector_create_window (selector, window);
  gtk_menu_shell_append (GTK_MENU_SHELL (selector->priv->menu), item);
}

static void
wnck_selector_window_opened (WnckScreen *screen,
                             WnckWindow *window, WnckSelector *selector)
{
732 733
  wnck_selector_connect_to_window (selector, window);

734
  if (!selector->priv->menu || !gtk_widget_get_visible (selector->priv->menu))
735 736 737 738 739 740 741 742 743
    return;

  if (!selector->priv->window_hash)
    return;

  wnck_selector_insert_window (selector, window);
  wnck_selector_make_menu_consistent (selector);

  gtk_menu_reposition (GTK_MENU (selector->priv->menu));
744 745 746 747 748 749
}

static void
wnck_selector_window_closed (WnckScreen *screen,
                             WnckWindow *window, WnckSelector *selector)
{
750
  GtkWidget *item;
751

752
  if (window == selector->priv->icon_window)
753 754
    wnck_selector_set_active_window (selector, NULL);

755
  if (!selector->priv->menu || !gtk_widget_get_visible (selector->priv->menu))
756 757
    return;

758
  if (!selector->priv->window_hash)
759
    return;
760

761
  item = g_hash_table_lookup (selector->priv->window_hash, window);
762 763 764
  if (!item)
    return;

765
  g_object_set_data (G_OBJECT (item), "wnck-selector-window", NULL);
766

767
  gtk_widget_hide (item);
768 769
  wnck_selector_make_menu_consistent (selector);

770
  gtk_menu_reposition (GTK_MENU (selector->priv->menu));
771 772 773
}

static void
774 775 776
wnck_selector_workspace_created (WnckScreen    *screen,
                                 WnckWorkspace *workspace,
                                 WnckSelector  *selector)
777
{
778
  if (!selector->priv->menu || !gtk_widget_get_visible (selector->priv->menu))
779 780
    return;

781 782 783 784 785
  /* this is assuming that the new workspace will have a higher number
   * than all the old workspaces, which is okay since the old workspaces
   * didn't disappear in the meantime */
  wnck_selector_add_workspace (selector, screen,
                               wnck_workspace_get_number (workspace));
786

787 788 789 790 791 792 793 794 795 796
  wnck_selector_make_menu_consistent (selector);

  gtk_menu_reposition (GTK_MENU (selector->priv->menu));
}

static void
wnck_selector_workspace_destroyed (WnckScreen    *screen,
                                   WnckWorkspace *workspace,
                                   WnckSelector  *selector)
{
797
  GList     *l, *children;
798 799 800
  GtkWidget *destroy;
  int        i;

801
  if (!selector->priv->menu || !gtk_widget_get_visible (selector->priv->menu))
802 803
    return;

804 805 806 807 808
  destroy = NULL;

  i = wnck_workspace_get_number (workspace);

  /* search for the item of this workspace so that we destroy it */
809 810 811
  children = gtk_container_get_children (GTK_CONTAINER (selector->priv->menu));

  for (l = children; l; l = l->next)
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    {
      int j;

      j = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (l->data),
                           "wnck-selector-workspace-n"));


      if (j - 1 == i)
        destroy = GTK_WIDGET (l->data);
      else if (j - 1 > i)
        /* shift the following workspaces */
        g_object_set_data (G_OBJECT (l->data), "wnck-selector-workspace-n",
                           GINT_TO_POINTER (j - 1));
    }

827 828
  g_list_free (children);

829 830 831 832 833 834
  if (destroy)
    gtk_widget_destroy (destroy);

  wnck_selector_make_menu_consistent (selector);

  gtk_menu_reposition (GTK_MENU (selector->priv->menu));
835 836 837 838 839 840 841 842 843 844 845 846
}

static void
wnck_selector_connect_to_window (WnckSelector *selector, WnckWindow *window)
{
  wncklet_connect_while_alive (window, "icon_changed",
                               G_CALLBACK (wnck_selector_window_icon_changed),
                               selector, selector);
  wncklet_connect_while_alive (window, "name_changed",
                               G_CALLBACK (wnck_selector_window_name_changed),
                               selector, selector);
  wncklet_connect_while_alive (window, "state_changed",
847 848 849 850 851
                               G_CALLBACK (wnck_selector_window_state_changed),
                               selector, selector);
  wncklet_connect_while_alive (window, "workspace_changed",
                               G_CALLBACK (wnck_selector_window_workspace_changed),
                               selector, selector);
852 853
}

854 855 856 857 858 859 860 861 862 863 864 865 866
static void
wnck_selector_disconnect_from_window (WnckSelector *selector,
                                      WnckWindow   *window)
{
  g_signal_handlers_disconnect_by_func (window,
                                        wnck_selector_window_icon_changed,
                                        selector);
  g_signal_handlers_disconnect_by_func (window,
                                        wnck_selector_window_name_changed,
                                        selector);
  g_signal_handlers_disconnect_by_func (window,
                                        wnck_selector_window_state_changed,
                                        selector);
867 868 869
  g_signal_handlers_disconnect_by_func (window,
                                        wnck_selector_window_workspace_changed,
                                        selector);
870 871
}

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
static void
wnck_selector_connect_to_screen (WnckSelector *selector, WnckScreen *screen)
{
  wncklet_connect_while_alive (screen, "active_window_changed",
                               G_CALLBACK
                               (wnck_selector_active_window_changed),
                               selector, selector);

  wncklet_connect_while_alive (screen, "window_opened",
                               G_CALLBACK (wnck_selector_window_opened),
                               selector, selector);

  wncklet_connect_while_alive (screen, "window_closed",
                               G_CALLBACK (wnck_selector_window_closed),
                               selector, selector);
887 888 889 890 891 892 893 894

  wncklet_connect_while_alive (screen, "workspace_created",
                               G_CALLBACK (wnck_selector_workspace_created),
                               selector, selector);

  wncklet_connect_while_alive (screen, "workspace_destroyed",
                               G_CALLBACK (wnck_selector_workspace_destroyed),
                               selector, selector);
895 896
}

897 898 899 900 901 902 903 904 905 906 907 908 909
static void
wnck_selector_disconnect_from_screen (WnckSelector *selector,
                                      WnckScreen   *screen)
{
  g_signal_handlers_disconnect_by_func (screen,
                                        wnck_selector_active_window_changed,
                                        selector);
  g_signal_handlers_disconnect_by_func (screen,
                                        wnck_selector_window_opened,
                                        selector);
  g_signal_handlers_disconnect_by_func (screen,
                                        wnck_selector_window_closed,
                                        selector);
910 911 912 913 914 915
  g_signal_handlers_disconnect_by_func (screen,
                                        wnck_selector_workspace_created,
                                        selector);
  g_signal_handlers_disconnect_by_func (screen,
                                        wnck_selector_workspace_destroyed,
                                        selector);
916 917
}

918 919 920
static void
wnck_selector_destroy_menu (GtkWidget *widget, WnckSelector *selector)
{
921
  selector->priv->menu = NULL;
922

923 924 925
  if (selector->priv->window_hash)
    g_hash_table_destroy (selector->priv->window_hash);
  selector->priv->window_hash = NULL;
926

927
  selector->priv->no_windows_item = NULL;
928 929
}

930
static gboolean
931 932
wnck_selector_scroll_event (GtkWidget      *widget,
                            GdkEventScroll *event)
933
{
934
  WnckSelector *selector;
935 936 937 938 939 940 941 942
  WnckScreen *screen;
  WnckWorkspace *workspace;
  GList *windows_list;
  GList *l;
  WnckWindow *window;
  WnckWindow *previous_window;
  gboolean should_activate_next_window;

943 944
  selector = WNCK_SELECTOR (widget);

945 946 947
  screen = wnck_selector_get_screen (selector);
  workspace = wnck_screen_get_active_workspace (screen);
  windows_list = wnck_screen_get_windows (screen);
948
  windows_list = g_list_sort (windows_list, wnck_selector_windows_compare);
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963

  /* Walk through the list of windows until we find the active one
   * (considering only those windows on the same workspace).
   * Then, depending on whether we're scrolling up or down, activate the next
   * window in the list (if it exists), or the previous one.
   */
  previous_window = NULL;
  should_activate_next_window = FALSE;
  for (l = windows_list; l; l = l->next)
    {
      window = WNCK_WINDOW (l->data);

      if (wnck_window_is_skip_tasklist (window))
        continue;

964 965
      if (workspace && !wnck_window_is_pinned (window) &&
          wnck_window_get_workspace (window) != workspace)
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
        continue;

      if (should_activate_next_window)
        {
          wnck_window_activate_transient (window, event->time);
          return TRUE;
        }

      if (wnck_window_is_active (window))
        {
          switch (event->direction)
            {
              case GDK_SCROLL_UP:
                if (previous_window != NULL)
                  {
                    wnck_window_activate_transient (previous_window,
                                                    event->time);
                    return TRUE;
                  }
              break;

987 988 989 990
              case GDK_SCROLL_DOWN:
                should_activate_next_window = TRUE;
              break;

991 992 993 994 995
              case GDK_SCROLL_LEFT:
              case GDK_SCROLL_RIGHT:
                /* We ignore LEFT and RIGHT scroll events. */
              break;

996 997 998
              case GDK_SCROLL_SMOOTH:
              break;

999 1000 1001 1002 1003 1004 1005
              default:
                g_assert_not_reached ();
            }
        }

      previous_window = window;
    }
Vincent Untz's avatar
Vincent Untz committed
1006

1007 1008 1009
  return TRUE;
}

1010 1011 1012
static void
wnck_selector_menu_hidden (GtkWidget *menu, WnckSelector *selector)
{
1013
  gtk_widget_set_state_flags (GTK_WIDGET (selector), GTK_STATE_FLAG_NORMAL, TRUE);
1014 1015 1016 1017 1018 1019 1020
}

static void
wnck_selector_on_show (GtkWidget *widget, WnckSelector *selector)
{
  GtkWidget *separator;
  WnckScreen *screen;
1021 1022 1023 1024
  WnckWorkspace *workspace;
  int nb_workspace;
  int i;
  GList **windows_per_workspace;
1025 1026 1027 1028
  GList *windows;
  GList *l, *children;

  /* Remove existing items */
1029
  children = gtk_container_get_children (GTK_CONTAINER (selector->priv->menu));
1030
  for (l = children; l; l = l->next)
1031
    gtk_container_remove (GTK_CONTAINER (selector->priv->menu), l->data);
1032 1033
  g_list_free (children);

1034 1035 1036
  if (selector->priv->window_hash)
    g_hash_table_destroy (selector->priv->window_hash);
  selector->priv->window_hash = g_hash_table_new_full (g_direct_hash,
1037
                                                 g_direct_equal,
1038
                                                 NULL, NULL);
1039

1040 1041 1042 1043 1044 1045 1046
  screen = wnck_selector_get_screen (selector);

  nb_workspace = wnck_screen_get_workspace_count (screen);
  windows_per_workspace = g_malloc0 (nb_workspace * sizeof (GList *));

  /* Get windows ordered by workspaces */
  windows = wnck_screen_get_windows (screen);
1047 1048
  windows = g_list_sort (windows, wnck_selector_windows_compare);

1049
  for (l = windows; l; l = l->next)
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    {
      workspace = wnck_window_get_workspace (l->data);
      if (!workspace && wnck_window_is_pinned (l->data))
        workspace = wnck_screen_get_active_workspace (screen);
      if (!workspace)
        continue;
      i = wnck_workspace_get_number (workspace);
      windows_per_workspace[i] = g_list_prepend (windows_per_workspace[i],
                                                 l->data);
    }

  /* Add windows from the current workspace */
  workspace = wnck_screen_get_active_workspace (screen);
  if (workspace)
    {
      i = wnck_workspace_get_number (workspace);

      windows_per_workspace[i] = g_list_reverse (windows_per_workspace[i]);
      for (l = windows_per_workspace[i]; l; l = l->next)
        wnck_selector_append_window (selector, l->data);
      g_list_free (windows_per_workspace[i]);
      windows_per_workspace[i] = NULL;
    }

  /* Add separator */
  separator = gtk_separator_menu_item_new ();
  gtk_menu_shell_append (GTK_MENU_SHELL (selector->priv->menu), separator);

  /* Add windows from other workspaces */
  for (i = 0; i < nb_workspace; i++)
    {
      wnck_selector_add_workspace (selector, screen, i);
      windows_per_workspace[i] = g_list_reverse (windows_per_workspace[i]);
      for (l = windows_per_workspace[i]; l; l = l->next)
        wnck_selector_append_window (selector, l->data);
      g_list_free (windows_per_workspace[i]);
      windows_per_workspace[i] = NULL;
    }
  g_free (windows_per_workspace);
1089

1090 1091 1092 1093 1094 1095
  selector->priv->no_windows_item = wnck_selector_item_new (selector,
		  					    _("No Windows Open"),
							    NULL);
  gtk_widget_set_sensitive (selector->priv->no_windows_item, FALSE);
  gtk_menu_shell_append (GTK_MENU_SHELL (selector->priv->menu),
                         selector->priv->no_windows_item);
1096

1097
  wnck_selector_make_menu_consistent (selector);
1098 1099 1100 1101 1102
}

static void
wnck_selector_fill (WnckSelector *selector)
{
1103 1104
  GtkWidget      *menu_item;
  GtkCssProvider *provider;
1105

1106 1107 1108
  menu_item = gtk_menu_item_new ();
  gtk_widget_show (menu_item);
  gtk_menu_shell_append (GTK_MENU_SHELL (selector), menu_item);
1109

1110 1111 1112
  selector->priv->image = gtk_image_new ();
  gtk_widget_show (selector->priv->image);
  gtk_container_add (GTK_CONTAINER (menu_item), selector->priv->image);
1113

1114
  selector->priv->menu = gtk_menu_new ();
1115
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item),
1116 1117
                             selector->priv->menu);
  g_signal_connect (selector->priv->menu, "hide",
1118
                    G_CALLBACK (wnck_selector_menu_hidden), selector);
1119
  g_signal_connect (selector->priv->menu, "destroy",
1120
                    G_CALLBACK (wnck_selector_destroy_menu), selector);
1121
  g_signal_connect (selector->priv->menu, "show",
1122 1123 1124
                    G_CALLBACK (wnck_selector_on_show), selector);

  gtk_widget_set_name (GTK_WIDGET (selector),
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
                       "gnome-panel-window-menu-menu-bar");

  provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (provider,
                                   "#gnome-panel-window-menu-menu-bar {\n"
                                   " border-width: 0px;\n"
                                   "}",
                                   -1, NULL);
  gtk_style_context_add_provider (gtk_widget_get_style_context (GTK_WIDGET (selector)),
                                  GTK_STYLE_PROVIDER (provider),
                                  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  g_object_unref (provider);
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148

  gtk_widget_show (GTK_WIDGET (selector));
}

static void
wnck_selector_init (WnckSelector *selector)
{
  AtkObject *atk_obj;

  atk_obj = gtk_widget_get_accessible (GTK_WIDGET (selector));
  atk_object_set_name (atk_obj, _("Window Selector"));
  atk_object_set_description (atk_obj, _("Tool to switch between windows"));
1149

1150
  selector->priv = wnck_selector_get_instance_private (selector);
1151

1152
  gtk_widget_add_events (GTK_WIDGET (selector), GDK_SCROLL_MASK);
1153 1154 1155 1156 1157
}

static void
wnck_selector_class_init (WnckSelectorClass *klass)
{
1158 1159
  GObjectClass   *object_class     = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class     = GTK_WIDGET_CLASS (klass);
1160

1161
  object_class->constructor = wnck_selector_constructor;
1162
  object_class->dispose = wnck_selector_dispose;
1163
  object_class->finalize = wnck_selector_finalize;
1164 1165 1166

  widget_class->realize   = wnck_selector_realize;
  widget_class->unrealize = wnck_selector_unrealize;
1167
  widget_class->scroll_event = wnck_selector_scroll_event;
1168 1169

  gtk_widget_class_set_css_name (widget_class, "wnck-selector");
1170 1171
}

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
static GObject *
wnck_selector_constructor (GType                  type,
                           guint                  n_construct_properties,
                           GObjectConstructParam *construct_properties)
{
  GObject *obj;

  obj = G_OBJECT_CLASS (wnck_selector_parent_class)->constructor (
                                                      type,
                                                      n_construct_properties,
                                                      construct_properties);

  wnck_selector_fill (WNCK_SELECTOR (obj));

  return obj;
}

1189 1190 1191 1192 1193 1194 1195
static void
wnck_selector_finalize (GObject *object)
{
  WnckSelector *selector;

  selector = WNCK_SELECTOR (object);

1196 1197 1198
  if (selector->priv->window_hash)
    g_hash_table_destroy (selector->priv->window_hash);
  selector->priv->window_hash = NULL;
1199

1200 1201 1202
  G_OBJECT_CLASS (wnck_selector_parent_class)->finalize (object);
}

1203
static void
1204
wnck_selector_dispose (GObject *object)
1205
{
1206 1207 1208
  WnckSelector *selector;

  selector = WNCK_SELECTOR (object);
1209

1210 1211 1212
  if (selector->priv->menu)
    gtk_widget_destroy (selector->priv->menu);
  selector->priv->menu = NULL;
1213

1214 1215
  selector->priv->image       = NULL;
  selector->priv->icon_window = NULL;
1216

1217
  G_OBJECT_CLASS (wnck_selector_parent_class)->dispose (object);
1218 1219
}

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
static void
wnck_selector_realize (GtkWidget *widget)
{
  WnckSelector *selector;
  WnckScreen   *screen;
  WnckWindow   *window;
  GList        *l;

  GTK_WIDGET_CLASS (wnck_selector_parent_class)->realize (widget);

  selector = WNCK_SELECTOR (widget);
  screen = wnck_selector_get_screen (selector);

  window = wnck_screen_get_active_window (screen);
  wnck_selector_set_active_window (selector, window);

  for (l = wnck_screen_get_windows (screen); l; l = l->next)
    wnck_selector_connect_to_window (selector, l->data);

  wnck_selector_connect_to_screen (selector, screen);
}

static void
wnck_selector_unrealize (GtkWidget *widget)
{
  WnckSelector *selector;
  WnckScreen   *screen;
  GList        *l;

  selector = WNCK_SELECTOR (widget);
  screen = wnck_selector_get_screen (selector);

  wnck_selector_disconnect_from_screen (selector, screen);

  for (l = wnck_screen_get_windows (screen); l; l = l->next)
    wnck_selector_disconnect_from_window (selector, l->data);

  GTK_WIDGET_CLASS (wnck_selector_parent_class)->unrealize (widget);
1258 1259
}

1260 1261 1262 1263 1264 1265 1266
/**
 * wnck_selector_new:
 *
 * Creates a new #WnckSelector. The #WnckSelector will list #WnckWindow of the
 * #WnckScreen it is on.
 *
 * Return value: a newly created #WnckSelector.
Vincent Untz's avatar
Vincent Untz committed
1267 1268
 *
 * Since: 2.10
1269
 */
1270
GtkWidget *
1271
wnck_selector_new (void)
1272 1273
{
  WnckSelector *selector;
1274

1275
  selector = g_object_new (WNCK_TYPE_SELECTOR, NULL);
1276

1277 1278
  return GTK_WIDGET (selector);
}