--- a/ui/cocoa/toolkit.m Fri Nov 29 22:21:36 2024 +0100 +++ b/ui/cocoa/toolkit.m Thu Dec 12 20:01:43 2024 +0100 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2012 Olaf Wintermann. All rights reserved. + * Copyright 2024 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -26,321 +26,126 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#import <stdio.h> -#import <stdlib.h> -#import <sys/stat.h> -#import <sys/types.h> -#import <errno.h> - -#import "../common/context.h" -#import "../common/document.h" -#import "../common/properties.h" +#import "toolkit.h" -#import "toolkit.h" -#import "window.h" -#import "menu.h" -#import "toolbar.h" -#import "stock.h" +#include "../common/document.h" +#include "../common/properties.h" +#include "../common/menu.h" +#include "../common/toolbar.h" +#include "../common/threadpool.h" -NSAutoreleasePool *pool; +#import "AppDelegate.h" -static char *application_name; +static const char *application_name; + +static int app_argc; +static const char **app_argv; -static ui_callback appclose_fnc; -static void *appclose_udata; +static ui_callback startup_func; +static void *startup_data; +static ui_callback open_func; +void *open_data; +static ui_callback exit_func; +void *exit_data; -static ui_callback openfile_fnc; -static void *openfile_udata; +/* ------------------- App Init / Event Loop functions ------------------- */ -void ui_init(char *appname, int argc, char **argv) { - pool = [[NSAutoreleasePool alloc] init]; - [NSApplication sharedApplication]; - [NSBundle loadNibNamed:@"MainMenu" owner:NSApp]; +void ui_init(const char *appname, int argc, char **argv) { + application_name = appname; + app_argc = argc; + app_argv = (const char**)argv; - UiApplicationDelegate *delegate = [[UiApplicationDelegate alloc]init]; - [NSApp setDelegate: delegate]; - - + uic_init_global_context(); + uic_docmgr_init(); - ui_menu_init(); - ui_toolbar_init(); - ui_stock_init(); - + uic_menu_init(); + uic_toolbar_init(); + uic_load_app_properties(); + + [NSApplication sharedApplication]; + //[NSBundle loadNibNamed:@"MainMenu" owner:NSApp ]; + //[[NSBundle mainBundle] loadNibNamed:@"MainMenu" owner:NSApp topLevelObjects:&topLevelObjects]; } -char* ui_appname() { +const char* ui_appname() { return application_name; } -void ui_exitfunc(ui_callback f, void *userdata) { - appclose_fnc = f; - appclose_udata = userdata; -} - -void ui_openfilefunc(ui_callback f, void *userdata) { - openfile_fnc = f; - openfile_udata = userdata; +void ui_onstartup(ui_callback f, void *userdata) { + startup_func = f; + startup_data = userdata; } -void ui_show(UiObject *obj) { - uic_check_group_widgets(obj->ctx); - if([obj->widget class] == [UiCocoaWindow class]) { - UiCocoaWindow *window = (UiCocoaWindow*)obj->widget; - [window makeKeyAndOrderFront:nil]; - } else { - printf("Error: ui_show: Object is not a Window!\n"); - } -} - -void ui_set_show_all(UIWIDGET widget, int value) { - // TODO -} - -void ui_set_visible(UIWIDGET widget, int visible) { - // TODO -} - -void ui_set_enabled(UIWIDGET widget, int enabled) { - [(id)widget setEnabled: enabled]; +void ui_onopen(ui_callback f, void *userdata) { + open_func = f; + open_data = userdata; } - - -void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd) { - UiThread *thread = [[UiThread alloc]initWithObject:obj]; - [thread setJobFunction:tf]; - [thread setJobData:td]; - [thread setFinishCallback:f]; - [thread setFinishData:fd]; - [thread start]; -} - -void ui_main() { - [NSApp run]; - [pool release]; +void ui_onexit(ui_callback f, void *userdata) { + exit_func = f; + exit_data = userdata; } - -void ui_clipboard_set(char *str) { - NSString *string = [[NSString alloc] initWithUTF8String:str]; - NSPasteboard * pasteBoard = [NSPasteboard generalPasteboard]; - [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; - [pasteBoard setString:string forType:NSStringPboardType]; -} - -char* ui_clipboard_get() { - NSPasteboard * pasteBoard = [NSPasteboard generalPasteboard]; - NSArray *classes = [[NSArray alloc] initWithObjects:[NSString class], nil]; - NSDictionary *options = [NSDictionary dictionary]; - NSArray *data = [pasteBoard readObjectsForClasses:classes options:options]; - - if(data != nil) { - NSString *str = [data componentsJoinedByString: @""]; - - // copy C string - size_t length = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; - const char *cstr = [str UTF8String]; - char *value = malloc(length + 1); - memcpy(value, cstr, length); - value[length] = '\0'; - - return value; - } else { - return NULL; +void ui_cocoa_onstartup(void) { + UiEvent e; + e.obj = NULL; + e.window = NULL; + e.document = NULL; + e.eventdata = NULL; + e.intval = 0; + if(startup_func) { + startup_func(&e, startup_data); } } - -@implementation UiApplicationDelegate - -- (void)applicationWillTerminate:(NSNotification*)notification { - printf("terminate\n"); -} - -- (BOOL)applicationShouldHandleReopen:(NSApplication *)app hasVisibleWindows:(BOOL)visible; { - if(!visible) { - printf("reopen\n"); - } - return NO; -} - -- (BOOL)application:(NSApplication*)application openFile:(NSString*)filename { - if(openfile_fnc) { - UiEvent event; - event.obj = NULL; - event.document = NULL; - event.window = NULL; - event.eventdata = (void*)[filename UTF8String]; - event.intval = 0; - openfile_fnc(&event, openfile_udata); +void ui_cocoa_onopen(const char *file) { + UiEvent e; + e.obj = NULL; + e.window = NULL; + e.document = NULL; + e.eventdata = NULL; + e.intval = 0; + if(open_func) { + open_func(&e, open_data); } - - return NO; -} - -@end - - -@implementation EventWrapper - -- (EventWrapper*) initWithData: (void*)d callback:(ui_callback) f { - data = d; - callback = f; - value = 0; - return self; -} - - -- (void*) data { - return data; -} - -- (void) setData:(void*)d { - data = d; -} - - -- (ui_callback) callback { - return callback; -} - -- (void) setCallback: (ui_callback)f { - callback = f; -} - -- (int) intval { - return value; -} - -- (void) setIntval:(int)i { - value = i; -} - - -- (BOOL)handleEvent:(id)sender { - NSWindow *activeWindow = [NSApp keyWindow]; - - UiEvent event; - event.eventdata = NULL; - if([activeWindow class] == [UiCocoaWindow class]) { - event.obj = [(UiCocoaWindow*)activeWindow object]; - event.window = event.obj->window; - event.document = event.obj->ctx->document; - event.intval = value; - } - if(callback) { - callback(&event, data); - } - - return true; } -- (BOOL)handleStateEvent:(id)sender { - NSWindow *activeWindow = [NSApp keyWindow]; - int state = [sender state] ? NSOffState : NSOnState; - - UiEvent event; - event.intval = state; - event.eventdata = NULL; - if([activeWindow class] == [UiCocoaWindow class]) { - event.obj = [(UiCocoaWindow*)activeWindow object]; - event.window = event.obj->window; - event.document = event.obj->ctx->document; - // if the sender is a menu item, we have to save the state for this - // window - UiMenuItem *wmi = [(UiCocoaWindow*)activeWindow getMenuItem: sender]; - if(wmi) { - // update state in window data - wmi->state = state; - } - } else { - event.window = NULL; - event.document = NULL; - } - if(callback) { - callback(&event, data); - } - [sender setState: state]; - - return true; -} - -- (BOOL)handleToggleEvent:(id)sender { - NSWindow *activeWindow = [NSApp keyWindow]; - - UiEvent event; - event.intval = [sender state]; - event.eventdata = NULL; - if([activeWindow class] == [UiCocoaWindow class]) { - event.obj = [(UiCocoaWindow*)activeWindow object]; - event.window = event.obj->window; - event.document = event.obj->ctx->document; - } else { - event.window = NULL; - event.document = NULL; - } - if(callback) { - callback(&event, data); - } - - return true; -} - -@end - -@implementation UiThread - -- (id) initWithObject:(UiObject*)object { - obj = object; - job_func = NULL; - job_data = NULL; - finish_callback = NULL; - finish_data = NULL; - return self; -} - -- (void) setJobFunction:(ui_threadfunc)func { - job_func = func; -} - -- (void) setJobData:(void*)data { - job_data = data; -} - -- (void) setFinishCallback:(ui_callback)callback { - finish_callback = callback; -} - -- (void) setFinishData:(void*)data { - finish_data = data; -} - -- (void) start { - [NSThread detachNewThreadSelector:@selector(runJob:) - toTarget:self - withObject:nil]; -} - -- (void) runJob:(id)n { - int result = job_func(job_data); - if(!result) { - [self performSelectorOnMainThread:@selector(finish:) - withObject:nil - waitUntilDone:NO]; +void ui_cocoa_onexit(void) { + UiEvent e; + e.obj = NULL; + e.window = NULL; + e.document = NULL; + e.eventdata = NULL; + e.intval = 0; + if(exit_func) { + exit_func(&e, exit_data); } } -- (void) finish:(id)n { - UiEvent event; - event.obj = obj; - event.window = obj->window; - event.document = obj->ctx->document; - event.eventdata = NULL; - event.intval = 0; - finish_callback(&event, finish_data); +void ui_main(void) { + NSApplicationMain(app_argc, app_argv); +} + +/* ------------------- Window Visibility functions ------------------- */ + +void ui_show(UiObject *obj) { + if(obj->wobj) { + NSWindow *window = (__bridge NSWindow*)obj->wobj; + [window makeKeyAndOrderFront:nil]; + } } -@end +void ui_close(UiObject *obj) { + +} + +/* ------------------- Job Control / Threadpool functions ------------------- */ +void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd) { +} + +void ui_call_mainthread(ui_threadfunc tf, void* td) { + +}