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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
/*
* gedit-app-osx.c
* This file is part of gedit
*
* Copyright (C) 2010 - Jesse van den Kieboom
*
* gedit 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.
*
* gedit 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 gedit; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "gedit-app-osx.h"
#include <gdk/gdkquartz.h>
#include <string.h>
#include <glib/gi18n.h>
#include "gedit-app-private.h"
#include "gedit-dirs.h"
#include "gedit-debug.h"
#include "gedit-commands.h"
#include "gedit-commands-private.h"
#include "gedit-recent.h"
#import <AppKit/AppKit.h>
NSWindow *gdk_quartz_window_get_nswindow(GdkWindow *window);
NSEvent *gdk_quartz_event_get_nsevent(GdkEvent *event);
static GeditWindow *
ensure_window (GeditAppOSX *app,
gboolean with_empty_document)
{
GList *windows;
GeditWindow *ret = NULL;
windows = gtk_application_get_windows (GTK_APPLICATION (app));
while (windows)
{
GtkWindow *window;
GdkWindow *win;
NSWindow *nswin;
window = windows->data;
windows = g_list_next (windows);
if (!gtk_widget_get_realized (GTK_WIDGET (window)))
{
continue;
}
if (!GEDIT_IS_WINDOW (window))
{
continue;
}
win = gtk_widget_get_window (GTK_WIDGET (window));
nswin = gdk_quartz_window_get_nswindow (win);
if ([nswin isOnActiveSpace])
{
ret = GEDIT_WINDOW (window);
break;
}
}
if (!ret)
{
ret = gedit_app_create_window (GEDIT_APP (app), NULL);
gtk_widget_show (GTK_WIDGET (ret));
}
if (with_empty_document && gedit_window_get_active_document (ret) == NULL)
{
gedit_window_create_tab (ret, TRUE);
}
gtk_window_present (GTK_WINDOW (ret));
return ret;
}
@interface GeditAppOSXDelegate : NSObject
{
GeditAppOSX *app;
id<NSApplicationDelegate> orig;
}
- (id)initWithApp:(GeditAppOSX *)theApp;
- (void)release;
- (id)forwardingTargetForSelector:(SEL)aSelector;
- (BOOL)respondsToSelector:(SEL)aSelector;
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag;
- (void)applicationWillBecomeActive:(NSNotification *)aNotification;
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames;
@end
@implementation GeditAppOSXDelegate
- (id)initWithApp:(GeditAppOSX *)theApp
{
[super init];
app = theApp;
orig = [NSApp delegate];
[NSApp setDelegate:self];
return self;
}
- (void)release
{
[NSApp setDelegate:orig];
[super release];
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return orig;
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
return [super respondsToSelector:aSelector] || [orig respondsToSelector:aSelector];
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
ensure_window (app, TRUE);
return NO;
}
- (void)applicationWillBecomeActive:(NSNotification *)aNotification
{
ensure_window (app, TRUE);
}
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
{
ensure_window (app, FALSE);
[orig application:sender openFiles:filenames];
}
@end
struct _GeditAppOSX
{
GeditApp parent_instance;
GeditMenuExtension *recent_files_menu;
gulong recent_manager_changed_id;
GeditAppOSXDelegate *app_delegate;
GList *recent_actions;
GeditRecentConfiguration recent_config;
};
G_DEFINE_TYPE (GeditAppOSX, gedit_app_osx, GEDIT_TYPE_APP)
static void
remove_recent_actions (GeditAppOSX *app)
{
while (app->recent_actions)
{
gchar *action_name = app->recent_actions->data;
g_action_map_remove_action (G_ACTION_MAP (app), action_name);
g_free (action_name);
app->recent_actions = g_list_delete_link (app->recent_actions,
app->recent_actions);
}
}
static void
gedit_app_osx_finalize (GObject *object)
{
GeditAppOSX *app = GEDIT_APP_OSX (object);
g_object_unref (app->recent_files_menu);
remove_recent_actions (app);
g_signal_handler_disconnect (app->recent_config.manager,
app->recent_manager_changed_id);
gedit_recent_configuration_destroy (&app->recent_config);
[app->app_delegate release];
G_OBJECT_CLASS (gedit_app_osx_parent_class)->finalize (object);
}
gboolean
gedit_app_osx_show_url (GeditAppOSX *app,
const gchar *url)
{
return [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url]]];
}
static gboolean
gedit_app_osx_show_help_impl (GeditApp *app,
GtkWindow *parent,
const gchar *name,
const gchar *link_id)
{
gboolean ret = FALSE;
if (name == NULL || g_strcmp0 (name, "gedit") == 0)
{
gchar *link;
if (link_id)
{
link = g_strdup_printf ("http://library.gnome.org/users/gedit/stable/%s",
link_id);
}
else
{
link = g_strdup ("http://library.gnome.org/users/gedit/stable/");
}
ret = gedit_app_osx_show_url (GEDIT_APP_OSX (app), link);
g_free (link);
}
return ret;
}
static void
gedit_app_osx_set_window_title_impl (GeditApp *app,
GeditWindow *window,
const gchar *title)
{
NSWindow *native;
GeditDocument *document;
GdkWindow *wnd;
g_return_if_fail (GEDIT_IS_WINDOW (window));
wnd = gtk_widget_get_window (GTK_WIDGET (window));
if (wnd == NULL)
{
return;
}
native = gdk_quartz_window_get_nswindow (wnd);
document = gedit_window_get_active_document (window);
if (document)
{
bool ismodified;
if (gedit_document_is_untitled (document))
{
[native setRepresentedURL:nil];
}
else
{
GtkSourceFile *file;
GFile *location;
gchar *uri;
file = gedit_document_get_file (document);
location = gtk_source_file_get_location (file);
uri = g_file_get_uri (location);
NSURL *nsurl = [NSURL URLWithString:[NSString stringWithUTF8String:uri]];
[native setRepresentedURL:nsurl];
g_free (uri);
}
ismodified = !gedit_document_is_untouched (document);
[native setDocumentEdited:ismodified];
}
else
{
[native setRepresentedURL:nil];
[native setDocumentEdited:false];
}
GEDIT_APP_CLASS (gedit_app_osx_parent_class)->set_window_title (app, window, title);
}
typedef struct
{
GeditAppOSX *app;
GtkRecentInfo *info;
} RecentFileInfo;
static void
recent_file_info_free (gpointer data,
GClosure *closure)
{
RecentFileInfo *info = data;
g_object_unref (info->app);
gtk_recent_info_unref (info->info);
g_slice_free (RecentFileInfo, data);
}
static void
recent_file_activated (GAction *action,
GVariant *parameter,
RecentFileInfo *info)
{
GeditWindow *window;
const gchar *uri;
GFile *file;
uri = gtk_recent_info_get_uri (info->info);
file = g_file_new_for_uri (uri);
window = ensure_window (info->app, FALSE);
gedit_commands_load_location (GEDIT_WINDOW (window), file, NULL, 0, 0);
g_object_unref (file);
}
static void
recent_files_menu_populate (GeditAppOSX *app)
{
GList *items;
gint i = 0;
gedit_menu_extension_remove_items (app->recent_files_menu);
remove_recent_actions (app);
items = gedit_recent_get_items (&app->recent_config);
while (items)
{
GtkRecentInfo *info = items->data;
GMenuItem *mitem;
const gchar *name;
gchar *acname;
gchar *acfullname;
GSimpleAction *action;
RecentFileInfo *finfo;
name = gtk_recent_info_get_display_name (info);
acname = g_strdup_printf ("recent-file-action-%d", ++i);
action = g_simple_action_new (acname, NULL);
finfo = g_slice_new (RecentFileInfo);
finfo->app = g_object_ref (app);
finfo->info = gtk_recent_info_ref (info);
g_signal_connect_data (action,
"activate",
G_CALLBACK (recent_file_activated),
finfo,
recent_file_info_free,
0);
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
g_object_unref (action);
acfullname = g_strdup_printf ("app.%s", acname);
app->recent_actions = g_list_prepend (app->recent_actions, acname);
mitem = g_menu_item_new (name, acfullname);
gedit_menu_extension_append_menu_item (app->recent_files_menu, mitem);
g_free (acfullname);
g_object_unref (mitem);
gtk_recent_info_unref (info);
items = g_list_delete_link (items, items);
}
}
static void
recent_manager_changed (GtkRecentManager *manager,
GeditAppOSX *app)
{
recent_files_menu_populate (app);
}
static void
open_activated (GSimpleAction *action,
GVariant *parameter,
gpointer userdata)
{
_gedit_cmd_file_open (NULL, NULL, NULL);
}
static GActionEntry app_entries[] = {
{ "open", open_activated, NULL, NULL, NULL }
};
static void
update_open_sensitivity (GeditAppOSX *app)
{
GAction *action;
gboolean has_windows;
has_windows = (gtk_application_get_windows (GTK_APPLICATION (app)) != NULL);
action = g_action_map_lookup_action (G_ACTION_MAP (app), "open");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), !has_windows);
}
static void
gedit_app_osx_startup (GApplication *application)
{
const gchar *replace_accels[] = {
"<Primary><Alt>F",
NULL
};
const gchar *open_accels[] = {
"<Primary>O",
NULL
};
const gchar *fullscreen_accels[] = {
"<Primary><Control>F",
NULL
};
GeditAppOSX *app = GEDIT_APP_OSX (application);
G_APPLICATION_CLASS (gedit_app_osx_parent_class)->startup (application);
app->app_delegate = [[[GeditAppOSXDelegate alloc] initWithApp:app] retain];
g_action_map_add_action_entries (G_ACTION_MAP (application),
app_entries,
G_N_ELEMENTS (app_entries),
application);
gtk_application_set_accels_for_action (GTK_APPLICATION (application),
"win.replace",
replace_accels);
gtk_application_set_accels_for_action (GTK_APPLICATION (application),
"app.open",
open_accels);
gtk_application_set_accels_for_action (GTK_APPLICATION (application),
"win.fullscreen",
fullscreen_accels);
gedit_recent_configuration_init_default (&app->recent_config);
app->recent_files_menu = _gedit_app_extend_menu (GEDIT_APP (application),
"recent-files-section");
app->recent_manager_changed_id = g_signal_connect (app->recent_config.manager,
"changed",
G_CALLBACK (recent_manager_changed),
app);
recent_files_menu_populate (app);
g_application_hold (application);
update_open_sensitivity (app);
}
static void
set_window_allow_fullscreen (GeditWindow *window)
{
GdkWindow *wnd;
NSWindow *native;
wnd = gtk_widget_get_window (GTK_WIDGET (window));
if (wnd != NULL)
{
native = gdk_quartz_window_get_nswindow (wnd);
[native setCollectionBehavior: [native collectionBehavior] | NSWindowCollectionBehaviorFullScreenPrimary];
}
}
static void
on_window_realized (GtkWidget *widget)
{
set_window_allow_fullscreen (GEDIT_WINDOW (widget));
}
static GeditWindow *
gedit_app_osx_create_window_impl (GeditApp *app)
{
GeditWindow *window;
window = GEDIT_APP_CLASS (gedit_app_osx_parent_class)->create_window (app);
gtk_window_set_titlebar (GTK_WINDOW (window), NULL);
if (gtk_widget_get_realized (GTK_WIDGET (window)))
{
set_window_allow_fullscreen (window);
}
else
{
g_signal_connect (window, "realize", G_CALLBACK (on_window_realized), NULL);
}
return window;
}
static gboolean
gedit_app_osx_process_window_event_impl (GeditApp *app,
GeditWindow *window,
GdkEvent *event)
{
NSEvent *nsevent;
/* For OS X we will propagate the event to NSApp, which handles some OS X
* specific keybindings and the accelerators for the menu
*/
nsevent = gdk_quartz_event_get_nsevent (event);
[NSApp sendEvent:nsevent];
/* It does not really matter what we return here since it's the last thing
* in the chain. Also we can't get from sendEvent whether the event was
* actually handled by NSApp anyway
*/
return TRUE;
}
static void
gedit_app_osx_constructed (GObject *object)
{
/* FIXME: should we do this on all platforms? */
g_object_set (object, "register-session", TRUE, NULL);
G_OBJECT_CLASS (gedit_app_osx_parent_class)->constructed (object);
}
static void
gedit_app_osx_window_added (GtkApplication *application,
GtkWindow *window)
{
GTK_APPLICATION_CLASS (gedit_app_osx_parent_class)->window_added (application, window);
update_open_sensitivity (GEDIT_APP_OSX (application));
}
static void
gedit_app_osx_window_removed (GtkApplication *application,
GtkWindow *window)
{
GTK_APPLICATION_CLASS (gedit_app_osx_parent_class)->window_removed (application, window);
update_open_sensitivity (GEDIT_APP_OSX (application));
}
static void
gedit_app_osx_class_init (GeditAppOSXClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GeditAppClass *app_class = GEDIT_APP_CLASS (klass);
GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
GtkApplicationClass *gtkapplication_class = GTK_APPLICATION_CLASS (klass);
object_class->finalize = gedit_app_osx_finalize;
object_class->constructed = gedit_app_osx_constructed;
application_class->startup = gedit_app_osx_startup;
gtkapplication_class->window_added = gedit_app_osx_window_added;
gtkapplication_class->window_removed = gedit_app_osx_window_removed;
app_class->show_help = gedit_app_osx_show_help_impl;
app_class->set_window_title = gedit_app_osx_set_window_title_impl;
app_class->create_window = gedit_app_osx_create_window_impl;
app_class->process_window_event = gedit_app_osx_process_window_event_impl;
}
static void
gedit_app_osx_init (GeditAppOSX *app)
{
/* 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"];
}
/* ex:set ts=8 noet: */
|