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
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * 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 "ctkapplicationprivate.h"
#include "ctkbuilder.h"
#import <Cocoa/Cocoa.h>

typedef struct
{
  guint cookie;
  CtkApplicationInhibitFlags flags;
  char *reason;
  CtkWindow *window;
} CtkApplicationQuartzInhibitor;

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

typedef CtkApplicationImplClass CtkApplicationImplQuartzClass;

typedef struct
{
  CtkApplicationImpl impl;

  CtkActionMuxer *muxer;
  GMenu *combined;

  GSList *inhibitors;
  gint quit_inhibit;
  guint next_cookie;
  NSObject *delegate;
} CtkApplicationImplQuartz;

G_DEFINE_TYPE (CtkApplicationImplQuartz, ctk_application_impl_quartz, CTK_TYPE_APPLICATION_IMPL)
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
@interface CtkApplicationQuartzDelegate : NSObject <NSApplicationDelegate>
#else
@interface CtkApplicationQuartzDelegate : NSObject<--- syntax error
#endif
{
  CtkApplicationImplQuartz *quartz;
}

- (id)initWithImpl:(CtkApplicationImplQuartz*)impl;
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender;
- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)filenames;
@end

@implementation CtkApplicationQuartzDelegate
-(id)initWithImpl:(CtkApplicationImplQuartz*)impl
{
  [super init];
  quartz = impl;
  return self;
}

-(NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender
{
  /* 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...
   */
  return quartz->quit_inhibit == 0 ? NSTerminateNow : NSTerminateCancel;
}

-(void)application:(NSApplication *)theApplication openFiles:(NSArray *)filenames
{
  GFile **files;
  gint i;
  GApplicationFlags flags;

  flags = g_application_get_flags (G_APPLICATION (quartz->impl.application));

  if (~flags & G_APPLICATION_HANDLES_OPEN)
    {
      [theApplication replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
      return;
    }

  files = g_new (GFile *, [filenames count]);

  for (i = 0; i < [filenames count]; i++)
    files[i] = g_file_new_for_path ([(NSString *)[filenames objectAtIndex:i] UTF8String]);

  g_application_open (G_APPLICATION (quartz->impl.application), files, [filenames count], "");

  for (i = 0; i < [filenames count]; i++)
    g_object_unref (files[i]);

  g_free (files);

  [theApplication replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
}
@end

/* these exist only for accel handling */
static void
ctk_application_impl_quartz_hide (GSimpleAction *action,
                                  GVariant      *parameter,
                                  gpointer       user_data)
{
  [NSApp hide:NSApp];
}

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

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

static GActionEntry ctk_application_impl_quartz_actions[] = {
  { "hide",             ctk_application_impl_quartz_hide        },
  { "hide-others",      ctk_application_impl_quartz_hide_others },
  { "show-all",         ctk_application_impl_quartz_show_all    }
};

static void
ctk_application_impl_quartz_startup (CtkApplicationImpl *impl,
                                     gboolean            register_session)
{
  CtkApplicationImplQuartz *quartz = (CtkApplicationImplQuartz *) impl;
  GSimpleActionGroup *ctkinternal;
  GMenuModel *app_menu;
  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};

  if (register_session)
    {
      quartz->delegate = [[CtkApplicationQuartzDelegate alloc] initWithImpl:quartz];
      [NSApp setDelegate: (id)(quartz->delegate)];
    }

  quartz->muxer = ctk_action_muxer_new ();
  ctk_action_muxer_set_parent (quartz->muxer, ctk_application_get_action_muxer (impl->application));

  /* Add the default accels */
  ctk_application_set_accels_for_action (impl->application, "app.preferences", pref_accel);
  ctk_application_set_accels_for_action (impl->application, "ctkinternal.hide-others", hide_others_accel);
  ctk_application_set_accels_for_action (impl->application, "ctkinternal.hide", hide_accel);
  ctk_application_set_accels_for_action (impl->application, "app.quit", quit_accel);

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

  /* now setup the menu */
  app_menu = ctk_application_get_app_menu (impl->application);
  if (app_menu == NULL)
    {
      CtkBuilder *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'.
       */
      builder = ctk_builder_new_from_resource ("/org/ctk/libctk/ui/ctkapplication-quartz.ui");
      ctk_application_set_app_menu (impl->application, G_MENU_MODEL (ctk_builder_get_object (builder, "app-menu")));
      g_object_unref (builder);
    }
  else
    ctk_application_impl_set_app_menu (impl, app_menu);

  /* This may or may not add an item to 'combined' */
  ctk_application_impl_set_menubar (impl, ctk_application_get_menubar (impl->application));

  /* OK.  Now put it in the menu. */
  ctk_application_impl_quartz_setup_menu (G_MENU_MODEL (quartz->combined), quartz->muxer);

  [NSApp finishLaunching];
}

static void
ctk_application_impl_quartz_shutdown (CtkApplicationImpl *impl)
{
  CtkApplicationImplQuartz *quartz = (CtkApplicationImplQuartz *) impl;

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

  if (quartz->delegate)
    {
      [quartz->delegate release];
      quartz->delegate = NULL;
    }

  g_slist_free_full (quartz->inhibitors, (GDestroyNotify) ctk_application_quartz_inhibitor_free);
  quartz->inhibitors = NULL;
}

static void
ctk_application_impl_quartz_active_window_changed (CtkApplicationImpl *impl,
                                                   CtkWindow          *window)
{
  CtkApplicationImplQuartz *quartz = (CtkApplicationImplQuartz *) impl;

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

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

static void
ctk_application_impl_quartz_set_app_menu (CtkApplicationImpl *impl,
                                          GMenuModel         *app_menu)
{
  CtkApplicationImplQuartz *quartz = (CtkApplicationImplQuartz *) impl;

  /* 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);
    }
}

static void
ctk_application_impl_quartz_set_menubar (CtkApplicationImpl *impl,
                                         GMenuModel         *menubar)
{
  CtkApplicationImplQuartz *quartz = (CtkApplicationImplQuartz *) impl;

  /* 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);
}

static guint
ctk_application_impl_quartz_inhibit (CtkApplicationImpl         *impl,
                                     CtkWindow                  *window,
                                     CtkApplicationInhibitFlags  flags,
                                     const gchar                *reason)
{
  CtkApplicationImplQuartz *quartz = (CtkApplicationImplQuartz *) impl;
  CtkApplicationQuartzInhibitor *inhibitor;

  inhibitor = g_slice_new (CtkApplicationQuartzInhibitor);
  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 & CTK_APPLICATION_INHIBIT_LOGOUT)
    quartz->quit_inhibit++;

  return inhibitor->cookie;
}

static void
ctk_application_impl_quartz_uninhibit (CtkApplicationImpl *impl,
                                       guint               cookie)
{
  CtkApplicationImplQuartz *quartz = (CtkApplicationImplQuartz *) impl;
  GSList *iter;

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

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

  g_warning ("Invalid inhibitor cookie");
}

static gboolean
ctk_application_impl_quartz_is_inhibited (CtkApplicationImpl         *impl,
                                          CtkApplicationInhibitFlags  flags)
{
  CtkApplicationImplQuartz *quartz = (CtkApplicationImplQuartz *) impl;

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

  return FALSE;
}

static void
ctk_application_impl_quartz_init (CtkApplicationImplQuartz *quartz)
{
  /* This is required so that Cocoa is not going to parse the
     command line arguments by itself and generate OpenFile events.
     We already parse the command line ourselves, so this is needed
     to prevent opening files twice, etc. */
  [[NSUserDefaults standardUserDefaults] setObject:@"NO"
                                            forKey:@"NSTreatUnknownArgumentsAsOpen"];

  quartz->combined = g_menu_new ();
}

static void
ctk_application_impl_quartz_finalize (GObject *object)
{
  CtkApplicationImplQuartz *quartz = (CtkApplicationImplQuartz *) object;

  g_clear_object (&quartz->combined);

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

static void
ctk_application_impl_quartz_class_init (CtkApplicationImplClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

  class->startup = ctk_application_impl_quartz_startup;
  class->shutdown = ctk_application_impl_quartz_shutdown;
  class->active_window_changed = ctk_application_impl_quartz_active_window_changed;
  class->set_app_menu = ctk_application_impl_quartz_set_app_menu;
  class->set_menubar = ctk_application_impl_quartz_set_menubar;
  class->inhibit = ctk_application_impl_quartz_inhibit;
  class->uninhibit = ctk_application_impl_quartz_uninhibit;
  class->is_inhibited = ctk_application_impl_quartz_is_inhibited;

  gobject_class->finalize = ctk_application_impl_quartz_finalize;
}