summaryrefslogtreecommitdiff
path: root/src/lib/ecore_cocoa/ecore_cocoa_window.m
blob: e3df62c81ba599e0205e57959fe701e325814cb1 (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Ecore_Input.h>

#include <Ecore.h>
#include <Ecore_Cocoa.h>
#import "ecore_cocoa_window.h"
#include "ecore_cocoa_private.h"

@implementation EcoreCocoaWindow

@synthesize ecore_window_data;

- (id) initWithContentRect: (NSRect) contentRect
                 styleMask: (unsigned int) aStyle
                   backing: (NSBackingStoreType) bufferingType
                     defer: (BOOL) flag
{
    if (![super initWithContentRect: contentRect
                          styleMask: aStyle
                            backing: bufferingType
                              defer: flag]) return nil;

    [self setBackgroundColor: [NSColor whiteColor]];
    [self makeKeyWindow];
    [self setDelegate:self];
    [self setAcceptsMouseMovedEvents:YES];

    [self setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];

    return self;
}

- (BOOL)isFullScreen
{
   return (([self styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask);
}

- (BOOL)acceptsFirstResponder
{
   return YES;
}

- (BOOL)canBecomeKeyWindow
{
    return YES;
}

- (void)windowWillClose:(NSNotification *) EINA_UNUSED notification
{
   NSLog(@"window is going to be closed");
   Ecore_Cocoa_Event_Window *event;

   event = malloc(sizeof(Ecore_Cocoa_Event_Window));
   if (event == NULL)
     {
	   // FIXME Use Eina_Log
	   printf("Failed to allocate Ecore_Cocoa_Event_Window_destroy\n");
	   return;
     }
   event->wid = [notification object];
   ecore_event_add(ECORE_COCOA_EVENT_WINDOW_DESTROY, event, NULL, NULL);
}

- (void)windowDidResize:(NSNotification *) EINA_UNUSED notif
{
   Ecore_Cocoa_Event_Video_Resize *event;
   NSSize size = self.frame.size;

   event = malloc(sizeof(Ecore_Cocoa_Event_Video_Resize));
   if (event == NULL)
     {
        DBG("Failed to allocate Ecore_Cocoa_Event_Video_Resize\n");
        return;
     }
   event->w = size.width;
   event->h = size.height -
      (([self isFullScreen] == YES) ? 0 : ecore_cocoa_titlebar_height_get());
   event->wid = [notif object];
   ecore_event_add(ECORE_COCOA_EVENT_RESIZE, event, NULL, NULL);
}

- (void)windowDidBecomeKey:(NSNotification *)notification
{
  Ecore_Cocoa_Event_Window *e;

  e = malloc(sizeof(Ecore_Cocoa_Event_Window));
  if (!e)
    {
      DBG("GOT_FOCUS: Failed to allocate Ecore_Cocoa_Event_Window\n");
      return;
    }
  e->wid = [notification object];
  ecore_event_add(ECORE_COCOA_EVENT_GOT_FOCUS, e, NULL, NULL);
}

- (void)windowDidResignKey:(NSNotification *)notification
{
  Ecore_Cocoa_Event_Window *e;

  e = malloc(sizeof(Ecore_Cocoa_Event_Window));
  if (!e)
    {
      DBG("LOST_FOCUS: Failed to allocate Ecore_Cocoa_Event_Window\n");
      return;
    }
  e->wid = [notification object];
  ecore_event_add(ECORE_COCOA_EVENT_LOST_FOCUS, e, NULL, NULL);
}

- (void) mouseDown:(NSEvent*) event
{
   unsigned int time = (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff);

   NSView *view = [self contentView];
   NSPoint event_location = [event locationInWindow];
   NSPoint pt = [view convertPoint:event_location fromView:nil];

   int h = [view frame].size.height;
   int x = pt.x;
   int y = h - pt.y;

   Ecore_Event_Mouse_Button * ev = calloc(1, sizeof(Ecore_Event_Mouse_Button));
   if (!ev) return;

   ev->x = x;
   ev->y = y;
   ev->root.x = ev->x;
   ev->root.y = ev->y;
   ev->timestamp = time;
   switch ([event buttonNumber])
     {
      case 0: ev->buttons = 1; break;
      case 1: ev->buttons = 3; break;
      case 2: ev->buttons = 2; break;
      default: ev->buttons = 0; break;
     }
   ev->window = (Ecore_Window)self.ecore_window_data;
   ev->event_window = ev->window;

   if ([event clickCount] == 2)
     ev->double_click = 1;
   else
     ev->double_click = 0;

   if ([event clickCount] >= 3)
     ev->triple_click = 1;
   else
     ev->triple_click = 0;

   ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, ev, NULL, NULL);
}

- (void) rightMouseDown:(NSEvent*) event
{
	[self mouseDown: event];
}

- (void) otherMouseDown:(NSEvent*) event
{
	[self mouseDown: event];
}

- (void) mouseUp:(NSEvent*) event
{
   unsigned int time = (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff);

   NSView *view = [self contentView];
   NSPoint event_location = [event locationInWindow];
   NSPoint pt = [view convertPoint:event_location fromView:nil];

   int h = [view frame].size.height;
   int x = pt.x;
   int y = h - pt.y;

   Ecore_Event_Mouse_Button * ev = calloc(1, sizeof(Ecore_Event_Mouse_Button));
   if (!ev) return;

   ev->x = x;
   ev->y = y;
   ev->root.x = ev->x;
   ev->root.y = ev->y;
   ev->timestamp = time;
   switch ([event buttonNumber])
     {
      case 0: ev->buttons = 1; break;
      case 1: ev->buttons = 3; break;
      case 2: ev->buttons = 2; break;
      default: ev->buttons = 0; break;
     }
   ev->window = (Ecore_Window)self.ecore_window_data;
   ev->event_window = ev->window;

   if ([event clickCount] == 2)
     ev->double_click = 1;
   else
     ev->double_click = 0;

   if ([event clickCount] >= 3)
     ev->triple_click = 1;
   else
     ev->triple_click = 0;

   ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, ev, NULL, NULL);
}

- (void) rightMouseUp:(NSEvent*) event
{
	[self mouseUp: event];
}

- (void) otherMouseUp:(NSEvent*) event
{
	[self mouseUp: event];
}

- (void) mouseMoved:(NSEvent*) event
{
   unsigned int time = (unsigned int)((unsigned long long)(ecore_time_get() * 1000.0) & 0xffffffff);
   Ecore_Event_Mouse_Move * ev = calloc(1, sizeof(Ecore_Event_Mouse_Move));
   if (!ev) return;

   NSView *view = [self contentView];
   NSPoint event_location = [event locationInWindow];
   NSPoint pt = [view convertPoint:event_location fromView:nil];

   ev->x = pt.x;
   ev->y = [view frame].size.height - pt.y;
   ev->root.x = ev->x;
   ev->root.y = ev->y;
   ev->timestamp = time;
   ev->window = (Ecore_Window)self.ecore_window_data;
   ev->event_window = ev->window;
   ev->modifiers = 0; /* FIXME: keep modifier around. */

   ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
}

- (void) mouseDragged: (NSEvent*) event
{
   [self mouseMoved:event];
}

@end

Ecore_Cocoa_Window *
ecore_cocoa_window_new(int x,
		       int y,
		       int width,
		       int height)
{
  Ecore_Cocoa_Window *w;

  EcoreCocoaWindow *window = [[EcoreCocoaWindow alloc] initWithContentRect:NSMakeRect(x, y, width, height)
                                                                 styleMask:(NSTitledWindowMask    |
                                                                            NSClosableWindowMask  |
                                                                            NSResizableWindowMask |
                                                                            NSMiniaturizableWindowMask)
                                                                    backing:NSBackingStoreBuffered
                                                                    defer:NO];

  if (!window)
    return NULL;

  //Set the process to be a foreground process,
  //without that it prevents the window to become the key window and
  //receive all mouse mouve events.
  [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  [NSApp activateIgnoringOtherApps:YES];


  w = calloc(1, sizeof(Ecore_Cocoa_Window));
  if (w == NULL)
    {
       return NULL;
    }
  w->window = window;
  w->borderless = 0;

  window.ecore_window_data = w;

  return w;
}

void
ecore_cocoa_window_free(Ecore_Cocoa_Window *window)
{
  if (!window)
    return;

  [window->window release];
  free(window);
}

void
ecore_cocoa_window_size_min_set(Ecore_Cocoa_Window *window,
                                unsigned int w,
                                unsigned int h)
{
  if (!window)
    return;
  NSSize size = {w,h};
  window->window.contentMinSize = size;
}

void
ecore_cocoa_window_size_max_set(Ecore_Cocoa_Window *window,
                                unsigned int w,
                                unsigned int h)
{
  if (!window)
    return;
  NSSize size = {w,h};
  window->window.contentMaxSize = size;
}

void
ecore_cocoa_window_size_step_set(Ecore_Cocoa_Window *window,
                                 unsigned int w,
                                 unsigned int h)
{
  if (!window)
    return;
  NSSize size = {w,h};
  window->window.contentResizeIncrements = size;
}

void
ecore_cocoa_window_move(Ecore_Cocoa_Window *window,
			int                 x,
			int                 y)
{
  NSRect win_frame;

  if (!window)
    return;

  win_frame = [window->window frame];
  win_frame.origin.x = x;
  win_frame.origin.y = y;

  [window->window setFrame:win_frame display:YES];
}

void
ecore_cocoa_window_resize(Ecore_Cocoa_Window *window,
			  int                 width,
			  int                 height)
{
  if (!window) return;

  NSRect win_frame;

  win_frame = [window->window frame];
  win_frame.size.height = height +
     (([window->window isFullScreen] == YES) ? 0 : ecore_cocoa_titlebar_height_get());
  win_frame.size.width = width;

  [window->window setFrame:win_frame display:YES];
}

void
ecore_cocoa_window_move_resize(Ecore_Cocoa_Window *window,
			       int                 x,
			       int                 y,
			       int                 width,
			       int                 height)
{
  if (!window) return;

  NSRect win_frame;

  win_frame = [window->window frame];
  win_frame.size.height = height +
     (([window->window isFullScreen] == YES) ? 0 : ecore_cocoa_titlebar_height_get());
  win_frame.size.width = width;
  win_frame.origin.x = x;
  win_frame.origin.y = y;

  [window->window setFrame:win_frame display:YES];
}

void
ecore_cocoa_window_title_set(Ecore_Cocoa_Window *window, const char *title)
{
  if (!window || !title)
    return;

  [window->window setTitle:[NSString stringWithUTF8String:title]];
}

void
ecore_cocoa_window_show(Ecore_Cocoa_Window *window)
{
  if (!window || [window->window isVisible])
    {
      DBG("Window(%p) is not visible\n", window->window);
      return;
    }

  [window->window makeKeyAndOrderFront:NSApp];
}

void
ecore_cocoa_window_hide(Ecore_Cocoa_Window *window)
{
  if (!window || ![window->window isVisible])
    return;

  [window->window orderOut:NSApp];
}

void ecore_cocoa_window_raise(Ecore_Cocoa_Window *window)
{
  if (!window)
    return;

  [window->window orderFront:nil];
}

void ecore_cocoa_window_lower(Ecore_Cocoa_Window *window)
{
   if (!window)
     return;

   [window->window orderBack:nil];
}

void ecore_cocoa_window_activate(Ecore_Cocoa_Window *window)
{
   if (!window)
     return;

   [window->window makeKeyAndOrderFront:nil];
}

void ecore_cocoa_window_iconified_set(Ecore_Cocoa_Window *window,
                                      int                 on)
{
   if (!window)
     return;

   if (on)
     {
        [window->window miniaturize:nil];
     }
   else
     {
        [window->window deminiaturize:nil];
     }
}

void
ecore_cocoa_window_borderless_set(Ecore_Cocoa_Window *window,
                                  int                 on)
{
  if (!window)
    return;

  if (on)
    [window->window setContentBorderThickness:0.0
	    forEdge:NSMinXEdge | NSMinYEdge | NSMaxXEdge | NSMaxYEdge];
}

void
ecore_cocoa_window_view_set(Ecore_Cocoa_Window *window,
			    void *view)
{
  if (!window || !view)
    return;

  //[[window->window contentView] addSubview:view];
  NSView *v = view;
  [window->window setContentView:view];

  NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[v frame]
                                                      options:NSTrackingMouseMoved |
                                                              NSTrackingActiveInActiveApp |
                                                              NSTrackingInVisibleRect
                                                        owner:v
                                                     userInfo:nil];
  [v addTrackingArea:area];

  [area release];
}

Ecore_Cocoa_Window_Id ecore_cocoa_window_get_window_id(Ecore_Cocoa_Window *window)
{
  if (!window)
    return 0;

  DBG("Return : %p", window->window);

  return window->window;
}