gtkapplication-quartz.c 11 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
/*
 * Copyright © 2010 Codethink Limited
 * Copyright © 2013 Canonical Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#include "gtkapplicationprivate.h"
24
#include "gtkbuilder.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#import <Cocoa/Cocoa.h>

typedef struct
{
  guint cookie;
  GtkApplicationInhibitFlags flags;
  char *reason;
  GtkWindow *window;
} GtkApplicationQuartzInhibitor;

static void
gtk_application_quartz_inhibitor_free (GtkApplicationQuartzInhibitor *inhibitor)
{
  g_free (inhibitor->reason);
  g_clear_object (&inhibitor->window);
  g_slice_free (GtkApplicationQuartzInhibitor, inhibitor);
}

typedef GtkApplicationImplClass GtkApplicationImplQuartzClass;

typedef struct
{
  GtkApplicationImpl impl;

49 50 51
  GtkActionMuxer *muxer;
  GMenu *combined;

52 53 54
  GSList *inhibitors;
  gint quit_inhibit;
  guint next_cookie;
55
  NSObject *delegate;
56 57 58 59
} GtkApplicationImplQuartz;

G_DEFINE_TYPE (GtkApplicationImplQuartz, gtk_application_impl_quartz, GTK_TYPE_APPLICATION_IMPL)

60
@interface GtkApplicationQuartzDelegate : NSObject
61
{
62 63
  GtkApplicationImplQuartz *quartz;
}
64

65 66 67
- (id)initWithImpl:(GtkApplicationImplQuartz*)impl;
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender;
@end
68

69 70 71 72 73 74
@implementation GtkApplicationQuartzDelegate
-(id)initWithImpl:(GtkApplicationImplQuartz*)impl
{
  [super init];
  quartz = impl;
  return self;
75 76
}

77
-(NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender
78
{
79 80 81 82 83
  /* We have no way to give our message other than to pop up a dialog
   * ourselves, which we should not do since the OS will already show
   * one when we return NSTerminateNow.
   *
   * Just let the OS show the generic message...
84
   */
85
  return quartz->quit_inhibit == 0 ? NSTerminateNow : NSTerminateCancel;
86
}
87
@end
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/* these exist only for accel handling */
static void
gtk_application_impl_quartz_hide (GSimpleAction *action,
                                  GVariant      *parameter,
                                  gpointer       user_data)
{
  [NSApp hide:NSApp];
}

static void
gtk_application_impl_quartz_hide_others (GSimpleAction *action,
                                         GVariant      *parameter,
                                         gpointer       user_data)
{
  [NSApp hideOtherApplications:NSApp];
}

static void
gtk_application_impl_quartz_show_all (GSimpleAction *action,
                                      GVariant      *parameter,
                                      gpointer       user_data)
{
  [NSApp unhideAllApplications:NSApp];
}

static GActionEntry gtk_application_impl_quartz_actions[] = {
  { "hide",             gtk_application_impl_quartz_hide        },
  { "hide-others",      gtk_application_impl_quartz_hide_others },
  { "show-all",         gtk_application_impl_quartz_show_all    }
};

120 121 122 123 124
static void
gtk_application_impl_quartz_startup (GtkApplicationImpl *impl,
                                     gboolean            register_session)
{
  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
125 126
  GSimpleActionGroup *gtkinternal;
  GMenuModel *app_menu;
127 128 129 130
  const gchar *pref_accel[] = {"<Primary>comma", NULL};
  const gchar *hide_others_accel[] = {"<Primary><Alt>h", NULL};
  const gchar *hide_accel[] = {"<Primary>h", NULL};
  const gchar *quit_accel[] = {"<Primary>q", NULL};
131 132

  if (register_session)
133 134 135 136
    {
      quartz->delegate = [[GtkApplicationQuartzDelegate alloc] initWithImpl:quartz];
      [NSApp setDelegate: quartz->delegate];
    }
137

138 139 140
  quartz->muxer = gtk_action_muxer_new ();
  gtk_action_muxer_set_parent (quartz->muxer, gtk_application_get_action_muxer (impl->application));

141
  /* Add the default accels */
142 143 144 145
  gtk_application_set_accels_for_action (impl->application, "app.preferences", pref_accel);
  gtk_application_set_accels_for_action (impl->application, "gtkinternal.hide-others", hide_others_accel);
  gtk_application_set_accels_for_action (impl->application, "gtkinternal.hide", hide_accel);
  gtk_application_set_accels_for_action (impl->application, "app.quit", quit_accel);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

  /* and put code behind the 'special' accels */
  gtkinternal = g_simple_action_group_new ();
  g_action_map_add_action_entries (G_ACTION_MAP (gtkinternal), gtk_application_impl_quartz_actions,
                                   G_N_ELEMENTS (gtk_application_impl_quartz_actions), quartz);
  gtk_application_insert_action_group (impl->application, "gtkinternal", G_ACTION_GROUP (gtkinternal));
  g_object_unref (gtkinternal);

  /* now setup the menu */
  app_menu = gtk_application_get_app_menu (impl->application);
  if (app_menu == NULL)
    {
      GtkBuilder *builder;

      /* If the user didn't fill in their own menu yet, add ours.
       *
       * The fact that we do this here ensures that we will always have the
       * app menu at index 0 in 'combined'.
       */
165
      builder = gtk_builder_new_from_resource ("/org/gtk/libgtk/ui/gtkapplication-quartz.ui");
166 167 168 169 170 171 172
      gtk_application_set_app_menu (impl->application, G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
      g_object_unref (builder);
    }
  else
    gtk_application_impl_set_app_menu (impl, app_menu);

  /* This may or may not add an item to 'combined' */
173 174 175 176
  gtk_application_impl_set_menubar (impl, gtk_application_get_menubar (impl->application));

  /* OK.  Now put it in the menu. */
  gtk_application_impl_quartz_setup_menu (G_MENU_MODEL (quartz->combined), quartz->muxer);
177 178 179 180 181 182 183 184 185

  [NSApp finishLaunching];
}

static void
gtk_application_impl_quartz_shutdown (GtkApplicationImpl *impl)
{
  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;

186 187
  /* destroy our custom menubar */
  [NSApp setMainMenu:[[[NSMenu alloc] init] autorelease]];
188

189 190 191 192 193 194
  if (quartz->delegate)
    {
      [quartz->delegate release];
      quartz->delegate = NULL;
    }

195 196 197 198
  g_slist_free_full (quartz->inhibitors, (GDestroyNotify) gtk_application_quartz_inhibitor_free);
  quartz->inhibitors = NULL;
}

199 200 201 202 203 204 205 206 207 208 209 210
static void
gtk_application_impl_quartz_active_window_changed (GtkApplicationImpl *impl,
                                                   GtkWindow          *window)
{
  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;

  gtk_action_muxer_remove (quartz->muxer, "win");

  if (G_IS_ACTION_GROUP (window))
    gtk_action_muxer_insert (quartz->muxer, "win", G_ACTION_GROUP (window));
}

211 212 213 214 215 216
static void
gtk_application_impl_quartz_set_app_menu (GtkApplicationImpl *impl,
                                          GMenuModel         *app_menu)
{
  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  /* If there are any items at all, then the first one is the app menu */
  if (g_menu_model_get_n_items (G_MENU_MODEL (quartz->combined)))
    g_menu_remove (quartz->combined, 0);

  if (app_menu)
    g_menu_prepend_submenu (quartz->combined, "Application", app_menu);
  else
    {
      GMenu *empty;

      /* We must preserve the rule that index 0 is the app menu */
      empty = g_menu_new ();
      g_menu_prepend_submenu (quartz->combined, "Application", G_MENU_MODEL (empty));
      g_object_unref (empty);
    }
232 233 234 235 236 237 238 239
}

static void
gtk_application_impl_quartz_set_menubar (GtkApplicationImpl *impl,
                                         GMenuModel         *menubar)
{
  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;

240 241 242 243 244 245
  /* If we have the menubar, it is a section at index '1' */
  if (g_menu_model_get_n_items (G_MENU_MODEL (quartz->combined)) > 1)
    g_menu_remove (quartz->combined, 1);

  if (menubar)
    g_menu_append_section (quartz->combined, NULL, menubar);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
}

static guint
gtk_application_impl_quartz_inhibit (GtkApplicationImpl         *impl,
                                     GtkWindow                  *window,
                                     GtkApplicationInhibitFlags  flags,
                                     const gchar                *reason)
{
  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
  GtkApplicationQuartzInhibitor *inhibitor;

  inhibitor = g_slice_new (GtkApplicationQuartzInhibitor);
  inhibitor->cookie = ++quartz->next_cookie;
  inhibitor->flags = flags;
  inhibitor->reason = g_strdup (reason);
  inhibitor->window = window ? g_object_ref (window) : NULL;

  quartz->inhibitors = g_slist_prepend (quartz->inhibitors, inhibitor);

  if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
    quartz->quit_inhibit++;

  return inhibitor->cookie;
}

static void
gtk_application_impl_quartz_uninhibit (GtkApplicationImpl *impl,
                                       guint               cookie)
{
  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;
  GSList *iter;

  for (iter = quartz->inhibitors; iter; iter = iter->next)
    {
      GtkApplicationQuartzInhibitor *inhibitor = iter->data;

      if (inhibitor->cookie == cookie)
        {
          if (inhibitor->flags & GTK_APPLICATION_INHIBIT_LOGOUT)
            quartz->quit_inhibit--;
          gtk_application_quartz_inhibitor_free (inhibitor);
          quartz->inhibitors = g_slist_delete_link (quartz->inhibitors, iter);
          return;
        }
    }

  g_warning ("Invalid inhibitor cookie");
}

static gboolean
gtk_application_impl_quartz_is_inhibited (GtkApplicationImpl         *impl,
                                          GtkApplicationInhibitFlags  flags)
{
  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) impl;

  if (flags & GTK_APPLICATION_INHIBIT_LOGOUT)
    return quartz->quit_inhibit > 0;

  return FALSE;
}

static void
gtk_application_impl_quartz_init (GtkApplicationImplQuartz *quartz)
{
310
  quartz->combined = g_menu_new ();
311 312 313 314 315 316 317
}

static void
gtk_application_impl_quartz_finalize (GObject *object)
{
  GtkApplicationImplQuartz *quartz = (GtkApplicationImplQuartz *) object;

318
  g_clear_object (&quartz->combined);
319 320 321 322 323 324 325 326 327 328 329

  G_OBJECT_CLASS (gtk_application_impl_quartz_parent_class)->finalize (object);
}

static void
gtk_application_impl_quartz_class_init (GtkApplicationImplClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

  class->startup = gtk_application_impl_quartz_startup;
  class->shutdown = gtk_application_impl_quartz_shutdown;
330
  class->active_window_changed = gtk_application_impl_quartz_active_window_changed;
331 332 333 334 335 336 337 338
  class->set_app_menu = gtk_application_impl_quartz_set_app_menu;
  class->set_menubar = gtk_application_impl_quartz_set_menubar;
  class->inhibit = gtk_application_impl_quartz_inhibit;
  class->uninhibit = gtk_application_impl_quartz_uninhibit;
  class->is_inhibited = gtk_application_impl_quartz_is_inhibited;

  gobject_class->finalize = gtk_application_impl_quartz_finalize;
}