ui/cocoa/toolkit.m

branch
newapi
changeset 404
384f6d1f5784
parent 49
a80ba8741be6
equal deleted inserted replaced
403:b59935b2de79 404:384f6d1f5784
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2012 Olaf Wintermann. All rights reserved. 4 * Copyright 2024 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #import <stdio.h> 29 #import "toolkit.h"
30 #import <stdlib.h>
31 #import <sys/stat.h>
32 #import <sys/types.h>
33 #import <errno.h>
34 30
35 #import "../common/context.h" 31 #include "../common/document.h"
36 #import "../common/document.h" 32 #include "../common/properties.h"
37 #import "../common/properties.h" 33 #include "../common/menu.h"
34 #include "../common/toolbar.h"
35 #include "../common/threadpool.h"
38 36
39 #import "toolkit.h" 37 #import "AppDelegate.h"
40 #import "window.h"
41 #import "menu.h"
42 #import "toolbar.h"
43 #import "stock.h"
44 38
45 NSAutoreleasePool *pool; 39 static const char *application_name;
46 40
47 static char *application_name; 41 static int app_argc;
42 static const char **app_argv;
48 43
49 static ui_callback appclose_fnc; 44 static ui_callback startup_func;
50 static void *appclose_udata; 45 static void *startup_data;
46 static ui_callback open_func;
47 void *open_data;
48 static ui_callback exit_func;
49 void *exit_data;
51 50
52 static ui_callback openfile_fnc; 51 /* ------------------- App Init / Event Loop functions ------------------- */
53 static void *openfile_udata;
54 52
55 void ui_init(char *appname, int argc, char **argv) { 53 void ui_init(const char *appname, int argc, char **argv) {
56 pool = [[NSAutoreleasePool alloc] init]; 54 application_name = appname;
55 app_argc = argc;
56 app_argv = (const char**)argv;
57
58 uic_init_global_context();
59
60 uic_docmgr_init();
61 uic_menu_init();
62 uic_toolbar_init();
63
64 uic_load_app_properties();
65
57 [NSApplication sharedApplication]; 66 [NSApplication sharedApplication];
58 [NSBundle loadNibNamed:@"MainMenu" owner:NSApp]; 67 //[NSBundle loadNibNamed:@"MainMenu" owner:NSApp ];
59 68 //[[NSBundle mainBundle] loadNibNamed:@"MainMenu" owner:NSApp topLevelObjects:&topLevelObjects];
60 UiApplicationDelegate *delegate = [[UiApplicationDelegate alloc]init];
61 [NSApp setDelegate: delegate];
62
63
64 uic_docmgr_init();
65 ui_menu_init();
66 ui_toolbar_init();
67 ui_stock_init();
68
69 uic_load_app_properties();
70 } 69 }
71 70
72 char* ui_appname() { 71 const char* ui_appname() {
73 return application_name; 72 return application_name;
74 } 73 }
75 74
76 void ui_exitfunc(ui_callback f, void *userdata) { 75 void ui_onstartup(ui_callback f, void *userdata) {
77 appclose_fnc = f; 76 startup_func = f;
78 appclose_udata = userdata; 77 startup_data = userdata;
79 } 78 }
80 79
81 void ui_openfilefunc(ui_callback f, void *userdata) { 80 void ui_onopen(ui_callback f, void *userdata) {
82 openfile_fnc = f; 81 open_func = f;
83 openfile_udata = userdata; 82 open_data = userdata;
84 } 83 }
85 84
86 void ui_show(UiObject *obj) { 85 void ui_onexit(ui_callback f, void *userdata) {
87 uic_check_group_widgets(obj->ctx); 86 exit_func = f;
88 if([obj->widget class] == [UiCocoaWindow class]) { 87 exit_data = userdata;
89 UiCocoaWindow *window = (UiCocoaWindow*)obj->widget; 88 }
90 [window makeKeyAndOrderFront:nil]; 89
91 } else { 90 void ui_cocoa_onstartup(void) {
92 printf("Error: ui_show: Object is not a Window!\n"); 91 UiEvent e;
92 e.obj = NULL;
93 e.window = NULL;
94 e.document = NULL;
95 e.eventdata = NULL;
96 e.intval = 0;
97 if(startup_func) {
98 startup_func(&e, startup_data);
93 } 99 }
94 } 100 }
95 101
96 void ui_set_show_all(UIWIDGET widget, int value) { 102 void ui_cocoa_onopen(const char *file) {
97 // TODO 103 UiEvent e;
98 } 104 e.obj = NULL;
99 105 e.window = NULL;
100 void ui_set_visible(UIWIDGET widget, int visible) { 106 e.document = NULL;
101 // TODO 107 e.eventdata = NULL;
102 } 108 e.intval = 0;
103 109 if(open_func) {
104 void ui_set_enabled(UIWIDGET widget, int enabled) { 110 open_func(&e, open_data);
105 [(id)widget setEnabled: enabled];
106 }
107
108
109
110 void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd) {
111 UiThread *thread = [[UiThread alloc]initWithObject:obj];
112 [thread setJobFunction:tf];
113 [thread setJobData:td];
114 [thread setFinishCallback:f];
115 [thread setFinishData:fd];
116 [thread start];
117 }
118
119 void ui_main() {
120 [NSApp run];
121 [pool release];
122 }
123
124
125 void ui_clipboard_set(char *str) {
126 NSString *string = [[NSString alloc] initWithUTF8String:str];
127 NSPasteboard * pasteBoard = [NSPasteboard generalPasteboard];
128 [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
129 [pasteBoard setString:string forType:NSStringPboardType];
130 }
131
132 char* ui_clipboard_get() {
133 NSPasteboard * pasteBoard = [NSPasteboard generalPasteboard];
134 NSArray *classes = [[NSArray alloc] initWithObjects:[NSString class], nil];
135 NSDictionary *options = [NSDictionary dictionary];
136 NSArray *data = [pasteBoard readObjectsForClasses:classes options:options];
137
138 if(data != nil) {
139 NSString *str = [data componentsJoinedByString: @""];
140
141 // copy C string
142 size_t length = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
143 const char *cstr = [str UTF8String];
144 char *value = malloc(length + 1);
145 memcpy(value, cstr, length);
146 value[length] = '\0';
147
148 return value;
149 } else {
150 return NULL;
151 } 111 }
152 } 112 }
153 113
154 114 void ui_cocoa_onexit(void) {
155 @implementation UiApplicationDelegate 115 UiEvent e;
156 116 e.obj = NULL;
157 - (void)applicationWillTerminate:(NSNotification*)notification { 117 e.window = NULL;
158 printf("terminate\n"); 118 e.document = NULL;
159 } 119 e.eventdata = NULL;
160 120 e.intval = 0;
161 - (BOOL)applicationShouldHandleReopen:(NSApplication *)app hasVisibleWindows:(BOOL)visible; { 121 if(exit_func) {
162 if(!visible) { 122 exit_func(&e, exit_data);
163 printf("reopen\n");
164 }
165 return NO;
166 }
167
168 - (BOOL)application:(NSApplication*)application openFile:(NSString*)filename {
169 if(openfile_fnc) {
170 UiEvent event;
171 event.obj = NULL;
172 event.document = NULL;
173 event.window = NULL;
174 event.eventdata = (void*)[filename UTF8String];
175 event.intval = 0;
176 openfile_fnc(&event, openfile_udata);
177 }
178
179 return NO;
180 }
181
182 @end
183
184
185 @implementation EventWrapper
186
187 - (EventWrapper*) initWithData: (void*)d callback:(ui_callback) f {
188 data = d;
189 callback = f;
190 value = 0;
191 return self;
192 }
193
194
195 - (void*) data {
196 return data;
197 }
198
199 - (void) setData:(void*)d {
200 data = d;
201 }
202
203
204 - (ui_callback) callback {
205 return callback;
206 }
207
208 - (void) setCallback: (ui_callback)f {
209 callback = f;
210 }
211
212 - (int) intval {
213 return value;
214 }
215
216 - (void) setIntval:(int)i {
217 value = i;
218 }
219
220
221 - (BOOL)handleEvent:(id)sender {
222 NSWindow *activeWindow = [NSApp keyWindow];
223
224 UiEvent event;
225 event.eventdata = NULL;
226 if([activeWindow class] == [UiCocoaWindow class]) {
227 event.obj = [(UiCocoaWindow*)activeWindow object];
228 event.window = event.obj->window;
229 event.document = event.obj->ctx->document;
230 event.intval = value;
231 }
232 if(callback) {
233 callback(&event, data);
234 }
235
236 return true;
237 }
238
239 - (BOOL)handleStateEvent:(id)sender {
240 NSWindow *activeWindow = [NSApp keyWindow];
241 int state = [sender state] ? NSOffState : NSOnState;
242
243 UiEvent event;
244 event.intval = state;
245 event.eventdata = NULL;
246 if([activeWindow class] == [UiCocoaWindow class]) {
247 event.obj = [(UiCocoaWindow*)activeWindow object];
248 event.window = event.obj->window;
249 event.document = event.obj->ctx->document;
250 // if the sender is a menu item, we have to save the state for this
251 // window
252 UiMenuItem *wmi = [(UiCocoaWindow*)activeWindow getMenuItem: sender];
253 if(wmi) {
254 // update state in window data
255 wmi->state = state;
256 }
257 } else {
258 event.window = NULL;
259 event.document = NULL;
260 }
261 if(callback) {
262 callback(&event, data);
263 }
264 [sender setState: state];
265
266 return true;
267 }
268
269 - (BOOL)handleToggleEvent:(id)sender {
270 NSWindow *activeWindow = [NSApp keyWindow];
271
272 UiEvent event;
273 event.intval = [sender state];
274 event.eventdata = NULL;
275 if([activeWindow class] == [UiCocoaWindow class]) {
276 event.obj = [(UiCocoaWindow*)activeWindow object];
277 event.window = event.obj->window;
278 event.document = event.obj->ctx->document;
279 } else {
280 event.window = NULL;
281 event.document = NULL;
282 }
283 if(callback) {
284 callback(&event, data);
285 }
286
287 return true;
288 }
289
290 @end
291
292 @implementation UiThread
293
294 - (id) initWithObject:(UiObject*)object {
295 obj = object;
296 job_func = NULL;
297 job_data = NULL;
298 finish_callback = NULL;
299 finish_data = NULL;
300 return self;
301 }
302
303 - (void) setJobFunction:(ui_threadfunc)func {
304 job_func = func;
305 }
306
307 - (void) setJobData:(void*)data {
308 job_data = data;
309 }
310
311 - (void) setFinishCallback:(ui_callback)callback {
312 finish_callback = callback;
313 }
314
315 - (void) setFinishData:(void*)data {
316 finish_data = data;
317 }
318
319 - (void) start {
320 [NSThread detachNewThreadSelector:@selector(runJob:)
321 toTarget:self
322 withObject:nil];
323 }
324
325 - (void) runJob:(id)n {
326 int result = job_func(job_data);
327 if(!result) {
328 [self performSelectorOnMainThread:@selector(finish:)
329 withObject:nil
330 waitUntilDone:NO];
331 } 123 }
332 } 124 }
333 125
334 - (void) finish:(id)n { 126 void ui_main(void) {
335 UiEvent event; 127 NSApplicationMain(app_argc, app_argv);
336 event.obj = obj;
337 event.window = obj->window;
338 event.document = obj->ctx->document;
339 event.eventdata = NULL;
340 event.intval = 0;
341 finish_callback(&event, finish_data);
342 } 128 }
343 129
344 @end 130 /* ------------------- Window Visibility functions ------------------- */
345 131
132 void ui_show(UiObject *obj) {
133 if(obj->wobj) {
134 NSWindow *window = (__bridge NSWindow*)obj->wobj;
135 [window makeKeyAndOrderFront:nil];
136 }
137 }
346 138
139 void ui_close(UiObject *obj) {
140
141 }
142
143 /* ------------------- Job Control / Threadpool functions ------------------- */
144
145 void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd) {
146
147 }
148
149 void ui_call_mainthread(ui_threadfunc tf, void* td) {
150
151 }

mercurial