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

#import "ecore_cocoa_window.h"
#include "Ecore_Cocoa.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];

   return self;
}

- (BOOL)acceptsFirstResponder
{
   return YES;
}

- (BOOL)canBecomeKeyWindow
{
   return YES;
}

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

- (void)windowDidResize:(NSNotification *)notification {
   NSRect content_rect = [self contentRectForFrameRect:[self frame]];
   Ecore_Cocoa_Window *window = ecore_window_data;
   ecore_cocoa_window_resize(window, content_rect.size.width, content_rect.size.height);
   printf("THIS IS A RESIZE......................%gx%g\n", content_rect.size.width, content_rect.size.height);
}

@end



static float _title_bar_height(void)
{
   NSRect frame = NSMakeRect (0, 0, 100, 100);
   NSRect contentRect;

   contentRect = [NSWindow contentRectForFrameRect: frame
                                         styleMask: NSTitledWindowMask];

   return (frame.size.height - contentRect.size.height);
}

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.
   ProcessSerialNumber psn;
   GetCurrentProcess(&psn);
   TransformProcessType(&psn, kProcessTransformToForegroundApplication);
   SetFrontProcess(&psn);

   w = calloc(1, sizeof(Ecore_Cocoa_Window));
   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_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;

   if (!window)
     return;

   win_frame = [window->window frame];
   win_frame.size.height = height + _title_bar_height();
   win_frame.size.width = width;
   printf("Resize ..............\n");
   [window->window setFrame:win_frame display:YES];
   //ecore_event_add(ECORE_COCOA_EVENT_RESIZE, NULL, NULL, NULL);
}

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

   NSRect win_frame;

   if (!window)
     return;

   win_frame = [window->window frame];
   win_frame.size.height = height + _title_bar_height();
   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])
     {
        printf("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_borderless_set(Ecore_Cocoa_Window *window,
                                  int                 on)
{
   if (!window)
     return;

   if (on)
     [window->window setContentBorderThickness:0.0
                                       forEdje: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];
   [window->window setContentView:view];

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

   [area release];
}