Ticket #40795: patch-app-mac_integration.c.diff

File patch-app-mac_integration.c.diff, 6.1 KB (added by nano103, 11 years ago)
  • app/mac_integration.c

    old new  
    11/*
    2  *  @(#) $Id: mac_integration.c 12576 2011-07-11 14:51:57Z yeti-dn $
     2 *  @(#) $Id: mac_integration.c 15469 2013-10-22 15:32:45Z yeti-dn $
    33 *  Copyright (C) 2009 Andrey Gruzdev.
    44 *  E-mail: gruzdev@ntmdt.ru.
    55 *
     
    1919 */
    2020
    2121#ifdef __APPLE__
    22 #include <Carbon/Carbon.h>
     22#include <AppKit/AppKit.h>
    2323#include <CoreFoundation/CoreFoundation.h>
    2424#include <file.h>
    25 #include "ige-mac-menu.h"
     25#include "config.h"
     26
     27#ifdef HAVE_GTK_MAC_INTEGRATION
     28#include <gtkmacintegration/gtkosxapplication.h>
     29#endif
     30
    2631#define USE_MAC_INTEGRATION
    2732#define USED_ON_MAC /* */
    2833#else
    2934#define USED_ON_MAC G_GNUC_UNUSED
    3035#endif
    3136
     37#ifdef HAVE_GTK_MAC_INTEGRATION
     38#define USED_ON_MAC_QUARTZ /* */
     39#else
     40#define USED_ON_MAC_QUARTZ G_GNUC_UNUSED
     41#endif
     42
    3243#include "mac_integration.h"
    3344
    3445#ifdef USE_MAC_INTEGRATION
    3546int fileModulesReady = 0;
    3647GPtrArray *files_array = NULL;
     48#ifdef HAVE_GTK_MAC_INTEGRATION
     49GtkosxApplication *theApp = NULL;
     50#endif
    3751#endif
    3852
    3953void
    40 gwy_osx_get_menu_from_widget(USED_ON_MAC GtkWidget *container)
     54gwy_osx_get_menu_from_widget(USED_ON_MAC_QUARTZ GtkWidget *container)
    4155{
    42 #ifdef GDK_WINDOWING_QUARTZ
    43 #ifdef USE_MAC_INTEGRATION
     56#ifdef HAVE_GTK_MAC_INTEGRATION
    4457    GList *children;            //,*subchildren,*subsubchildren;
    4558    GList *l, *ll, *lll;
    4659    GtkWidget *menubar = gtk_menu_bar_new();
     
    7689    }
    7790    gtk_container_add(GTK_CONTAINER(container), menubar);
    7891    gtk_widget_hide(menubar);
    79     ige_mac_menu_set_menu_bar(GTK_MENU_SHELL(menubar));
    80 #endif
     92    gtkosx_application_set_menu_bar ( theApp, GTK_MENU_SHELL(menubar));
     93    gtkosx_application_ready (theApp);
    8194#endif
    8295}
    8396
    8497
    8598#ifdef USE_MAC_INTEGRATION
     99
    86100static void
    87101gwy_osx_open_file(gpointer data,
    88102                  G_GNUC_UNUSED gpointer user_data)
     
    90104    gwy_app_file_load((const gchar*)data, (const gchar*)data, NULL);
    91105}
    92106
    93 static OSStatus
    94 appleEventHandler(const AppleEvent * event, AppleEvent * event2, long param)
     107@interface GwyOSXEventHandler:NSObject
     108@end
     109
     110@implementation GwyOSXEventHandler
     111- (void)handleOpenEvent:(NSAppleEventDescriptor *)event withReplyEvent: (NSAppleEventDescriptor *)replyEvent
    95112{
    96     enum { BUFLEN = 1024 };
    97     AEDescList docs;
     113    #pragma unused (replyEvent)
     114    NSAppleEventDescriptor *descr = [event descriptorForKeyword:keyDirectObject];
     115    NSInteger i,count = [descr numberOfItems];
     116
     117    for(i=0;i<count;i++)
     118    {
     119        NSAppleEventDescriptor *descr1 = i==0?descr:[descr descriptorAtIndex:i];
     120        NSString *url = [descr1 stringValue];
     121        NSString *filename = [[NSURL URLWithString:url]  path];
     122
     123
     124        char * strBuffer = (char*)[filename UTF8String];
     125
     126        if (fileModulesReady)
     127            gwy_osx_open_file(strBuffer, NULL);
     128        else {
     129            if (!files_array)
     130                files_array = g_ptr_array_new();
     131            g_ptr_array_add(files_array, g_strdup((gchar*)strBuffer));
     132        }
     133    }
     134}
    98135
    99     if (AEGetParamDesc(event, keyDirectObject, typeAEList, &docs) == noErr) {
    100         long n = 0;
    101         int i;
     136- (void)handleQuitEvent:(NSAppleEventDescriptor *)event withReplyEvent: (NSAppleEventDescriptor *)replyEvent
     137{
     138   #pragma unused (event)
     139   #pragma unused (replyEvent)
     140   gtk_main_quit();
     141}
    102142
    103         AECountItems(&docs, &n);
    104         static UInt8 strBuffer[BUFLEN];
     143@end
    105144
    106         for (i = 0; i < n; i++) {
    107             FSRef ref;
     145GwyOSXEventHandler *eventHandler;
    108146
    109             if (AEGetNthPtr(&docs, i + 1, typeFSRef, 0, 0, &ref, sizeof(ref), 0)
    110                 != noErr)
    111                 continue;
    112             if (FSRefMakePath(&ref, strBuffer, BUFLEN) == noErr) {
    113                 if (fileModulesReady)
    114                     gwy_osx_open_file(strBuffer, NULL);
    115                 else {
    116                     if (!files_array)
    117                         files_array = g_ptr_array_new();
    118                     g_ptr_array_add(files_array, g_strdup((gchar*)strBuffer));
    119                 }
    120             }
    121         }
    122     }
    123     return noErr;
    124 }
    125147#endif
    126148
    127149void
    128150gwy_osx_init_handler(USED_ON_MAC int *argc)
    129151{
    130152#ifdef USE_MAC_INTEGRATION
     153    NSAppleEventManager *appleEventManager;
    131154    CFURLRef res_url_ref = NULL, bundle_url_ref = NULL;
    132 
    133155    res_url_ref = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
    134156    bundle_url_ref = CFBundleCopyBundleURL(CFBundleGetMainBundle());
    135157
     
    142164    if (bundle_url_ref)
    143165        CFRelease(bundle_url_ref);
    144166
    145     AEInstallEventHandler(kCoreEventClass,      // handle open file events
    146                           kAEOpenDocuments,
    147                           (AEEventHandlerUPP)appleEventHandler, 0, false);
     167
     168    eventHandler = [[GwyOSXEventHandler alloc] init];
     169
     170
     171    appleEventManager = [NSAppleEventManager sharedAppleEventManager];
     172    [appleEventManager setEventHandler:eventHandler
     173                           andSelector:@selector(handleOpenEvent:withReplyEvent:)
     174                         forEventClass:kCoreEventClass
     175                            andEventID:kAEOpenDocuments];
     176
     177    [appleEventManager setEventHandler:eventHandler
     178                           andSelector:@selector(handleQuitEvent:withReplyEvent:)
     179                         forEventClass:kCoreEventClass
     180                            andEventID:kAEQuitApplication];
     181
     182#ifdef HAVE_GTK_MAC_INTEGRATION
     183    theApp  = g_object_new (GTKOSX_TYPE_APPLICATION, NULL);
     184#endif
    148185#endif
    149186}
    150187
     
    152189gwy_osx_remove_handler(void)
    153190{
    154191#ifdef USE_MAC_INTEGRATION
    155     AERemoveEventHandler(kCoreEventClass,
    156                          kAEOpenDocuments,
    157                          (AEEventHandlerUPP)appleEventHandler, false);
     192    NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
     193    [appleEventManager removeEventHandlerForEventClass:kCoreEventClass andEventID:kAEOpenDocuments];
     194    [appleEventManager removeEventHandlerForEventClass:kCoreEventClass andEventID:kAEQuitApplication];
     195    [eventHandler release];
     196    eventHandler = nil;
     197#ifdef HAVE_GTK_MAC_INTEGRATION
     198    g_object_unref (theApp);
     199#endif
    158200#endif
    159201}
    160202