File: misc_osx.m

package info (click to toggle)
libgtkada 18-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 28,348 kB
  • sloc: ada: 61,514; xml: 7,709; python: 4,310; sh: 2,822; ansic: 1,598; makefile: 240; objc: 160; perl: 70
file content (280 lines) | stat: -rw-r--r-- 8,724 bytes parent folder | download | duplicates (3)
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
/*
-----------------------------------------------------------------------
--               GtkAda - Ada95 binding for Gtk+/Gnome               --
--                                                                   --
--   Copyright (C) 1998-2000 E. Briot, J. Brobecker and A. Charlet   --
--                Copyright (C) 2000-2013, AdaCore                   --
--                                                                   --
-- This library 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 3 of the License, 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 --
-- General Public License for more details.                          --
--                                                                   --
-- You should have received a copy of the GNU General Public         --
-- License along with this library; if not, write to the             --
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330,      --
-- Boston, MA 02111-1307, USA.                                       --
--                                                                   --
-- As a special exception, if other files instantiate generics from  --
-- this unit, or you link this unit with other files to produce an   --
-- executable, this  unit  does not  by itself cause  the resulting  --
-- executable to be covered by the GNU General Public License. This  --
-- exception does not however invalidate any other reasons why the   --
-- executable file  might be covered by the  GNU Public License.     --
-----------------------------------------------------------------------
*/

//  Support for opening files from the OS in OSX

#import <Cocoa/Cocoa.h>

#include <glib.h>
#include <gdk/gdkquartz.h>

#include "misc_osx.h"

/****************************************************************************
 * OPEN FILE HANDLING PART
 ****************************************************************************/

/* As OSX Event Handlers are called from a separate thread, we can't call
   any of the glib or gtk functions directly. Instead we build a temporary
   structure with a copy of all the paths to open, and in an idle callback
   we create the final structure containing gfiles
 */
typedef struct {
  char             **paths;
  gint             num_paths;
  gpointer         user_data;
  osx_open_files_cb callback;
} open_files_data;

/*****************
 * _open_file_cb *
 *****************/

/* used to create gfiles from within an idle callback */
static gboolean
_open_files_cb
  (gpointer of_data)
{
  open_files_data   *data      = (open_files_data*)of_data;
  osx_open_files_cb callback   = data->callback;
  gpointer          user_data  = data->user_data;
  gint              n_files    = data->num_paths;
  GFile             **files;

  files = malloc (n_files * sizeof(GFile*));

  for (gint i = 0; i < n_files; i++) {
    files[i] = g_file_new_for_path (data->paths[i]);
    g_free (data->paths[i]);
  }
  g_free (data->paths);
  g_free (data);

  callback (files, n_files, user_data);

  return G_SOURCE_REMOVE;
}

@interface OSXEventHandler : NSObject {
  osx_open_files_cb callback;
  gpointer          user_data;
}

/* Constructor: creates an event handler object calling cb on event, with
 * u_data as user_data parameter
 */
- (id)initWithCallback:(osx_open_files_cb)cb withData:(gpointer)u_data;

/* Actually installs the handler
 */
- (void)installEventHandler;

@end

@implementation OSXEventHandler

/********************
 * initWithCallback *
 ********************/

- (id)initWithCallback:(osx_open_files_cb)cb withData:(gpointer)u_data
{
   callback  = cb;
   user_data = u_data;

   return [super init];
}

/***********************
 * installEventHandler *
 ***********************/

- (void)installEventHandler
{
  NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
  [em
   setEventHandler:self
   andSelector:@selector(handleOpenDocument:withReplyEvent:)
   forEventClass:kCoreEventClass
   andEventID:kAEOpenDocuments];
}

/**********************
 * handleOpenDocument *
 **********************/

- (void)handleOpenDocument:(NSAppleEventDescriptor*)event
            withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
  if (!event) return;

  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSAppleEventDescriptor *fileListDescriptor = NULL;
  NSInteger numberOfFiles = 0;

  if ([event eventClass] == kCoreEventClass &&
      [event eventID] == kAEOpenDocuments)
    {
      fileListDescriptor = [event paramDescriptorForKeyword:keyDirectObject];
    }

  if (fileListDescriptor)
    {
      numberOfFiles = [fileListDescriptor numberOfItems];
    }

   if (numberOfFiles > 0)
    {
      open_files_data *of_data = g_malloc (sizeof (open_files_data));

      of_data->user_data = user_data;
      of_data->callback  = callback;
      of_data->num_paths = 0;
      of_data->paths     = (char**)g_malloc(numberOfFiles * sizeof(char*));

      for (NSInteger i = 1; i <= numberOfFiles; i++)
        {
          NSString* urlString =
            [[fileListDescriptor descriptorAtIndex:i] stringValue];
          if (!urlString) continue;

          // String in URL form: file://...
          // so we create an actual NSURL object from it, then retrieve the
          // fs representation that we need
          NSURL* url = [NSURL URLWithString:urlString];
          if (!url) continue;

          const char* path = [url fileSystemRepresentation];

          of_data->paths[of_data->num_paths] =
            (char *)g_malloc(strlen (path) + 1);
          strcpy (of_data->paths[of_data->num_paths], path);

          of_data->num_paths += 1;
        }

      // Cocoa events are processed by a separate thread. So we need to
      // create the gfiles from the main context explicitely
      g_main_context_invoke
        (NULL, _open_files_cb, (gpointer)of_data);
    }

    [pool drain];
}

@end

/*************************************
 * init_osx_open_files_event_handler *
 *************************************/

/* Sets an event handler that reacts to the Open File event and calls
 * ada_gtk_application_open_files.
 */
void
init_osx_open_files_event_handler
  (osx_open_files_cb callback,
   gpointer          user_data)
  {
    OSXEventHandler *handler =
      [[OSXEventHandler alloc] initWithCallback:callback withData:user_data];
    [handler installEventHandler];
  }


/****************************************************************************
 * FULLSCREEN SUPPORT
 ****************************************************************************/

/****************************************
 * _ada_gtk_osx_allow_fullscreen_mapped *
 ****************************************/

/* Enable the full-screen mode for the mapped GtkWindow window
 */
static void
_ada_gtk_osx_allow_fullscreen_mapped (GtkWindow *window)
  {
    GdkWindow *gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
    GdkWindow *toplevel = gdk_window_get_effective_toplevel (gdk_window);
    NSWindow *nswindow = gdk_quartz_window_get_nswindow (toplevel);
    [nswindow
       setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
  }

/*******************************************
 * _ada_gtk_osx_allow_fullscreen_on_mapped *
 *******************************************/

/* Called when the 'mapped' event is signalled on window
 */
static void
_ada_gtk_osx_allow_fullscreen_on_mapped
  (GtkWindow *window, GdkEvent *event, gpointer user_data)
{
  _ada_gtk_osx_allow_fullscreen_mapped (window);
  g_signal_handlers_disconnect_by_func
    (window, _ada_gtk_osx_allow_fullscreen_on_mapped, NULL);
}

/********************************
 * ada_gtk_osx_allow_fullscreen *
 ********************************/

/* exported function, setting fullscreen mode on window
 */
void ada_gtk_osx_allow_fullscreen (GtkWindow *window)
{
  GdkWindow *gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
  if (!gdk_window)
    {
      g_signal_connect
	(window,
         "map-event",
         G_CALLBACK (_ada_gtk_osx_allow_fullscreen_on_mapped),
         NULL);
    }
  else
    {
      _ada_gtk_osx_allow_fullscreen_mapped (window);
    }
}

/********************
 * ada_activate_app *
 ********************/

void ada_activate_app (void)
{
  if (![NSApp isActive])
     [NSApp activateIgnoringOtherApps:YES];
}