windows-actions.c 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "actions-types.h"

#include "core/gimp.h"
#include "core/gimpimage.h"
#include "core/gimplist.h"

#include "file/file-utils.h"

#include "widgets/gimpactiongroup.h"
35 36
#include "widgets/gimpdialogfactory.h"
#include "widgets/gimpdock.h"
37 38 39 40
#include "widgets/gimphelp-ids.h"

#include "display/gimpdisplay.h"

41 42
#include "dialogs/dialogs.h"

43 44 45 46 47 48
#include "windows-actions.h"
#include "windows-commands.h"

#include "gimp-intl.h"


49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static void   windows_actions_display_add    (GimpContainer     *container,
                                              GimpDisplay       *display,
                                              GimpActionGroup   *group);
static void   windows_actions_display_remove (GimpContainer     *container,
                                              GimpDisplay       *display,
                                              GimpActionGroup   *group);
static void   windows_actions_image_notify   (GimpDisplay       *display,
                                              const GParamSpec  *unused,
                                              GimpActionGroup   *group);

static void   windows_actions_dock_added     (GimpDialogFactory *factory,
                                              GimpDock          *dock,
                                              GimpActionGroup   *group);
static void   windows_actions_dock_removed   (GimpDialogFactory *factory,
                                              GimpDock          *dock,
                                              GimpActionGroup   *group);
static void   windows_actions_dock_notify    (GimpDock          *dock,
                                              const GParamSpec  *pspec,
                                              GimpActionGroup   *group);
68

69 70 71 72 73 74 75
static void   windows_actions_recent_add     (GimpContainer     *container,
                                              GimpSessionInfo   *info,
                                              GimpActionGroup   *group);
static void   windows_actions_recent_remove  (GimpContainer     *container,
                                              GimpSessionInfo   *info,
                                              GimpActionGroup   *group);

76 77 78

static const GimpActionEntry windows_actions[] =
{
79 80 81
  { "windows-menu",         NULL, N_("_Windows")               },
  { "windows-docks-menu",   NULL, N_("_Recently Closed Docks") },
  { "windows-dialogs-menu", NULL, N_("_Dockable Dialogs")      },
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

  { "windows-show-toolbox", NULL,
    N_("Tool_box"), "<control>B",
    N_("Raise the toolbox"),
    G_CALLBACK (windows_show_toolbox_cmd_callback),
    GIMP_HELP_TOOLBOX }
};


void
windows_actions_setup (GimpActionGroup *group)
{
  GList *list;

  gimp_action_group_add_actions (group,
                                 windows_actions,
                                 G_N_ELEMENTS (windows_actions));

100 101
  gimp_action_group_set_action_hide_empty (group, "windows-docks-menu", FALSE);

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  g_signal_connect_object (group->gimp->displays, "add",
                           G_CALLBACK (windows_actions_display_add),
                           group, 0);
  g_signal_connect_object (group->gimp->displays, "remove",
                           G_CALLBACK (windows_actions_display_remove),
                           group, 0);

  for (list = GIMP_LIST (group->gimp->displays)->list;
       list;
       list = g_list_next (list))
    {
      GimpDisplay *display = list->data;

      windows_actions_display_add (group->gimp->displays, display, group);
    }
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

  g_signal_connect_object (global_dock_factory, "dock-added",
                           G_CALLBACK (windows_actions_dock_added),
                           group, 0);
  g_signal_connect_object (global_dock_factory, "dock-removed",
                           G_CALLBACK (windows_actions_dock_removed),
                           group, 0);

  for (list = global_dock_factory->open_dialogs;
       list;
       list = g_list_next (list))
    {
      GimpDock *dock = list->data;

      if (GIMP_IS_DOCK (dock))
        windows_actions_dock_added (global_dock_factory, dock, group);
    }
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

  g_signal_connect_object (global_recent_docks, "add",
                           G_CALLBACK (windows_actions_recent_add),
                           group, 0);
  g_signal_connect_object (global_recent_docks, "remove",
                           G_CALLBACK (windows_actions_recent_remove),
                           group, 0);

  for (list = GIMP_LIST (global_recent_docks)->list;
       list;
       list = g_list_next (list))
    {
      GimpSessionInfo *info = list->data;

      windows_actions_recent_add (global_recent_docks, info, group);
    }
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
}

void
windows_actions_update (GimpActionGroup *group,
                        gpointer         data)
{
}


/*  private functions  */

static void
windows_actions_display_add (GimpContainer   *container,
                             GimpDisplay     *display,
                             GimpActionGroup *group)
{
  g_signal_connect_object (display, "notify::image",
                           G_CALLBACK (windows_actions_image_notify),
                           group, 0);

  if (display->image)
    windows_actions_image_notify (display, NULL, group);
}

static void
windows_actions_display_remove (GimpContainer   *container,
                                GimpDisplay     *display,
                                GimpActionGroup *group)
{
  GtkAction *action;
  gchar     *action_name = g_strdup_printf ("windows-display-%04d",
                                            gimp_display_get_ID (display));

  action = gtk_action_group_get_action (GTK_ACTION_GROUP (group), action_name);

  if (action)
    gtk_action_group_remove_action (GTK_ACTION_GROUP (group), action);

  g_free (action_name);
}

static void
windows_actions_image_notify (GimpDisplay      *display,
                              const GParamSpec *unused,
                              GimpActionGroup  *group)
{
  if (display->image)
    {
      GtkAction *action;
      gchar     *action_name = g_strdup_printf ("windows-display-%04d",
                                                gimp_display_get_ID (display));

      action = gtk_action_group_get_action (GTK_ACTION_GROUP (group),
                                            action_name);

      if (! action)
        {
          GimpActionEntry entry;

          entry.name        = action_name;
          entry.stock_id    = GIMP_STOCK_IMAGE;
          entry.label       = "";
          entry.accelerator = NULL;
          entry.tooltip     = NULL;
          entry.callback    = G_CALLBACK (windows_show_display_cmd_callback);
215
          entry.help_id     = GIMP_HELP_WINDOWS_SHOW_IMAGE_WINDOW;
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

          gimp_action_group_add_actions (group, &entry, 1);

          action = gtk_action_group_get_action (GTK_ACTION_GROUP (group),
                                                action_name);

          g_object_set_data (G_OBJECT (action), "display", display);
        }

      {
        const gchar *uri;
        gchar       *filename;
        gchar       *basename;
        gchar       *escaped;
        gchar       *title;

232
        uri = gimp_image_get_uri (display->image);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

        filename = file_utils_uri_display_name (uri);
        basename = file_utils_uri_display_basename (uri);

        escaped = gimp_escape_uline (basename);
        g_free (basename);

        title = g_strdup_printf ("%s-%d.%d", escaped,
                                 gimp_image_get_ID (display->image),
                                 display->instance);
        g_free (escaped);

        g_object_set (action,
                      "label",    title,
                      "tooltip",  filename,
                      "viewable", display->image,
                      "context",  gimp_get_user_context (group->gimp),
                      NULL);

        g_free (filename);
        g_free (title);
      }

      g_free (action_name);
    }
  else
    {
      windows_actions_display_remove (group->gimp->displays, display, group);
    }
}
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

static void
windows_actions_dock_added (GimpDialogFactory *factory,
                            GimpDock          *dock,
                            GimpActionGroup   *group)
{
  GtkAction       *action;
  GimpActionEntry  entry;
  gchar           *action_name = g_strdup_printf ("windows-dock-%04d", dock->ID);

  entry.name        = action_name;
  entry.stock_id    = NULL;
  entry.label       = "";
  entry.accelerator = NULL;
  entry.tooltip     = NULL;
  entry.callback    = G_CALLBACK (windows_show_dock_cmd_callback);
279
  entry.help_id     = GIMP_HELP_WINDOWS_SHOW_DOCK;
280 281 282 283 284 285

  gimp_action_group_add_actions (group, &entry, 1);

  action = gtk_action_group_get_action (GTK_ACTION_GROUP (group),
                                        action_name);

286 287 288 289
  g_object_set (action,
                "ellipsize", PANGO_ELLIPSIZE_END,
                NULL);

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
  g_object_set_data (G_OBJECT (action), "dock", dock);

  g_free (action_name);

  g_signal_connect_object (dock, "notify::title",
                           G_CALLBACK (windows_actions_dock_notify),
                           group, 0);

  if (gtk_window_get_title (GTK_WINDOW (dock)))
    windows_actions_dock_notify (dock, NULL, group);
}

static void
windows_actions_dock_removed (GimpDialogFactory *factory,
                              GimpDock          *dock,
                              GimpActionGroup   *group)
{
  GtkAction *action;
  gchar     *action_name = g_strdup_printf ("windows-dock-%04d", dock->ID);

  action = gtk_action_group_get_action (GTK_ACTION_GROUP (group), action_name);

  if (action)
    gtk_action_group_remove_action (GTK_ACTION_GROUP (group), action);

  g_free (action_name);
}

static void
windows_actions_dock_notify (GimpDock         *dock,
                             const GParamSpec *pspec,
                             GimpActionGroup  *group)
{
  GtkAction *action;
324
  gchar     *action_name;
325

326
  action_name = g_strdup_printf ("windows-dock-%04d", dock->ID);
327
  action = gtk_action_group_get_action (GTK_ACTION_GROUP (group), action_name);
328
  g_free (action_name);
329 330 331

  if (action)
    g_object_set (action,
332 333
                  "label",   gtk_window_get_title (GTK_WINDOW (dock)),
                  "tooltip", gtk_window_get_title (GTK_WINDOW (dock)),
334 335
                  NULL);
}
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

static void
windows_actions_recent_add (GimpContainer   *container,
                            GimpSessionInfo *info,
                            GimpActionGroup *group)
{
  GtkAction       *action;
  GimpActionEntry  entry;
  gint             info_id;
  static gint      info_id_counter = 1;
  gchar           *action_name;

  info_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (info),
                                                "recent-action-id"));

  if (! info_id)
    {
      info_id = info_id_counter++;

      g_object_set_data (G_OBJECT (info), "recent-action-id",
                         GINT_TO_POINTER (info_id));
    }

  action_name = g_strdup_printf ("windows-recent-%04d", info_id);

  entry.name        = action_name;
  entry.stock_id    = NULL;
  entry.label       = gimp_object_get_name (GIMP_OBJECT (info));
  entry.accelerator = NULL;
365
  entry.tooltip     = gimp_object_get_name (GIMP_OBJECT (info));
366
  entry.callback    = G_CALLBACK (windows_open_recent_cmd_callback);
367
  entry.help_id     = GIMP_HELP_WINDOWS_OPEN_RECENT_DOCK;
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

  gimp_action_group_add_actions (group, &entry, 1);

  action = gtk_action_group_get_action (GTK_ACTION_GROUP (group),
                                        action_name);

  g_object_set (action,
                "ellipsize",       PANGO_ELLIPSIZE_END,
                "max-width-chars", 30,
                NULL);

  g_object_set_data (G_OBJECT (action), "info", info);

  g_free (action_name);
}

static void
windows_actions_recent_remove (GimpContainer   *container,
                               GimpSessionInfo *info,
                               GimpActionGroup *group)
{
  GtkAction *action;
  gint       info_id;
  gchar     *action_name;

  info_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (info),
                                                "recent-action-id"));

  action_name = g_strdup_printf ("windows-recent-%04d", info_id);

  action = gtk_action_group_get_action (GTK_ACTION_GROUP (group), action_name);

  if (action)
    gtk_action_group_remove_action (GTK_ACTION_GROUP (group), action);

  g_free (action_name);
}