File: mac_integration.c

package info (click to toggle)
gwyddion 2.47-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,180 kB
  • ctags: 37,276
  • sloc: ansic: 317,531; sh: 5,122; xml: 3,583; python: 3,067; makefile: 2,694; cpp: 2,550; pascal: 418; perl: 154; ruby: 130; sed: 17
file content (274 lines) | stat: -rw-r--r-- 8,654 bytes parent folder | download
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
/*
 *  @(#) $Id: mac_integration.c 18552 2016-03-29 09:00:59Z yeti-dn $
 *  Copyright (C) 2009 Andrey Gruzdev.
 *  E-mail: gruzdev@ntmdt.ru.
 *
 *  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., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#ifdef __APPLE__
#include <AppKit/AppKit.h>
#include <CoreFoundation/CoreFoundation.h>
#include <file.h>
#include "config.h"

#ifdef HAVE_GTK_MAC_INTEGRATION
#include <gtkmacintegration/gtkosxapplication.h>
#endif

#define USE_MAC_INTEGRATION
#define USED_ON_MAC /* */
#else
#define USED_ON_MAC G_GNUC_UNUSED
#endif

#ifdef HAVE_GTK_MAC_INTEGRATION
#define USED_ON_MAC_QUARTZ /* */
#else
#define USED_ON_MAC_QUARTZ G_GNUC_UNUSED
#endif

#include "mac_integration.h"

#ifdef USE_MAC_INTEGRATION
int fileModulesReady = 0;
GPtrArray *files_array = NULL;
#ifdef HAVE_GTK_MAC_INTEGRATION
GtkosxApplication *theApp = NULL;
#endif
#endif

void
gwy_osx_get_menu_from_widget(USED_ON_MAC_QUARTZ GtkWidget *container)
{
#ifdef HAVE_GTK_MAC_INTEGRATION
    GList *children;            //,*subchildren,*subsubchildren;
    GList *l, *ll, *lll;
    GtkWidget *menubar = gtk_menu_bar_new();

    gtk_widget_set_name(menubar, "toolboxmenubar");
    children = gtk_container_get_children(GTK_CONTAINER(container));
    for (l = children; l; l = l->next) {
        GtkWidget *widget = l->data;

        if (GTK_IS_CONTAINER(widget)) {
            children = gtk_container_get_children(GTK_CONTAINER(widget));
            for (ll = children; ll; ll = ll->next) {
                GtkWidget *subwidget = ll->data;

                if (GTK_IS_CONTAINER(subwidget)) {
                    children =
                        gtk_container_get_children(GTK_CONTAINER(subwidget));
                    for (lll = children; lll; lll = lll->next) {
                        GtkWidget *subsubwidget = lll->data;

                        if (GTK_IS_MENU_ITEM(subsubwidget)) {
                            gtk_widget_hide(widget);
                            g_object_ref(subsubwidget);
                            gtk_container_remove(GTK_CONTAINER(subwidget), subsubwidget);
                            gtk_menu_shell_append(GTK_MENU_SHELL(menubar),
                                                  subsubwidget);
                            g_object_unref(subsubwidget);
                        }
                    }
                }
            }
        }
    }
    gtk_container_add(GTK_CONTAINER(container), menubar);
    gtk_widget_hide(menubar);
    gtkosx_application_set_menu_bar ( theApp, GTK_MENU_SHELL(menubar));
    gtkosx_application_ready (theApp);
#endif
}


#ifdef USE_MAC_INTEGRATION

static void
gwy_osx_open_file(gpointer data,
                  G_GNUC_UNUSED gpointer user_data)
{
    gwy_app_file_load((const gchar*)data, (const gchar*)data, NULL);
}

@interface GwyOSXEventHandler:NSObject
@end

@implementation GwyOSXEventHandler
- (void)handleOpenEvent:(NSAppleEventDescriptor *)event withReplyEvent: (NSAppleEventDescriptor *)replyEvent
{
#pragma unused (replyEvent)
    NSAppleEventDescriptor *descr = [event descriptorForKeyword:keyDirectObject];
    NSInteger i,count = [descr numberOfItems];

    for(i = 0; i < count; i++) {
        NSAppleEventDescriptor *descr1 = (i == 0
                                          ? descr
                                          : [descr descriptorAtIndex:i]);
        NSString *url = [descr1 stringValue];
        NSString *filename = [[NSURL URLWithString:url]  path];

        char *strBuffer = (char*)[filename UTF8String];

        if (fileModulesReady)
            gwy_osx_open_file(strBuffer, NULL);
        else {
            if (!files_array)
                files_array = g_ptr_array_new();
            g_ptr_array_add(files_array, g_strdup((gchar*)strBuffer));
        }
    }
}

- (void)handleQuitEvent:(NSAppleEventDescriptor *)event withReplyEvent: (NSAppleEventDescriptor *)replyEvent
{
#pragma unused (event)
#pragma unused (replyEvent)
   gtk_main_quit();
}

@end

GwyOSXEventHandler *eventHandler;

#endif

void
gwy_osx_init_handler(USED_ON_MAC int *argc)
{
#ifdef USE_MAC_INTEGRATION
    NSAppleEventManager *appleEventManager;
    CFURLRef res_url_ref = NULL, bundle_url_ref = NULL;
    res_url_ref = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
    bundle_url_ref = CFBundleCopyBundleURL(CFBundleGetMainBundle());

    if (res_url_ref
        && bundle_url_ref && !CFEqual(res_url_ref, bundle_url_ref))
        *argc = 1;        // command line options not available in app bundles

    if (res_url_ref)
        CFRelease(res_url_ref);
    if (bundle_url_ref)
        CFRelease(bundle_url_ref);

    eventHandler = [[GwyOSXEventHandler alloc] init];

    appleEventManager = [NSAppleEventManager sharedAppleEventManager];
    [appleEventManager setEventHandler:eventHandler
                           andSelector:@selector(handleOpenEvent:withReplyEvent:)
                         forEventClass:kCoreEventClass
                            andEventID:kAEOpenDocuments];

    [appleEventManager setEventHandler:eventHandler
                           andSelector:@selector(handleQuitEvent:withReplyEvent:)
                         forEventClass:kCoreEventClass
                            andEventID:kAEQuitApplication];

#ifdef HAVE_GTK_MAC_INTEGRATION
    theApp = g_object_new(GTKOSX_TYPE_APPLICATION, NULL);
#endif
#endif
}

void
gwy_osx_remove_handler(void)
{
#ifdef USE_MAC_INTEGRATION
    NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
    [appleEventManager removeEventHandlerForEventClass:kCoreEventClass andEventID:kAEOpenDocuments];
    [appleEventManager removeEventHandlerForEventClass:kCoreEventClass andEventID:kAEQuitApplication];
    [eventHandler release];
    eventHandler = nil;
#ifdef HAVE_GTK_MAC_INTEGRATION
    g_object_unref(theApp);
#endif
#endif
}

gboolean
gwy_osx_open_files(void)
{
#ifdef USE_MAC_INTEGRATION
    gboolean opening_files = !!files_array;

    if (files_array) {
        g_ptr_array_foreach(files_array, gwy_osx_open_file, NULL);
        g_ptr_array_foreach(files_array, (GFunc)g_free, NULL);
        g_ptr_array_free(files_array, TRUE);
        files_array = NULL;
    }
    fileModulesReady = 1;

    return opening_files;
#endif
    return FALSE;
}

void
gwy_osx_set_locale()
{
#ifdef USE_MAC_INTEGRATION
    static const struct {
        const gchar *locale;
        const gchar *lang;
    }
    locales[] = {
/* The following generated part is updated by running utils/update-langs.py */
/* @@@ GENERATED LANG OS X BEGIN @@@ */
        { "en_US.UTF-8", "en" },
        { "cs_CZ.UTF-8", "cs" },
        { "de_DE.UTF-8", "de" },
        { "en_GB.UTF-8", "en_GB" },
        { "fr_FR.UTF-8", "fr" },
        { "it_IT.UTF-8", "it" },
        { "ko_KR.UTF-8", "ko" },
        { "ru_RU.UTF-8", "ru" },
        { "es_ES.UTF-8", "es" },
/* @@@ GENERATED LANG OS X END @@@ */
    };

    CFTypeRef preferences = CFPreferencesCopyAppValue(CFSTR("AppleLanguages"),
                                                      kCFPreferencesCurrentApplication);

    if (preferences != NULL && CFGetTypeID(preferences) == CFArrayGetTypeID()) {
        CFArrayRef prefArray = (CFArrayRef) preferences;
        int n = CFArrayGetCount(prefArray);
        static char buf[256];
        int i;

        for (i = 0; i < n; i++) {
            CFTypeRef element = CFArrayGetValueAtIndex(prefArray, i);

            if (element != NULL && CFGetTypeID(element) == CFStringGetTypeID()
                && CFStringGetCString((CFStringRef) element,
                                      buf, sizeof(buf),
                                      kCFStringEncodingASCII)) {
                int j;

                for (j = 0; j < G_N_ELEMENTS(locales); j++) {
                    if (strcmp(locales[j].lang, buf) == 0) {
                        g_setenv("LANG", locales[j].locale, TRUE);
                        goto exit;
                    }
                }
            }
        }
      exit:
        CFRelease(preferences);
    }
#endif
}