added threads (Cocoa)

Mon, 19 May 2014 21:15:43 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 19 May 2014 21:15:43 +0200
changeset 48
0b8ac9d6d473
parent 47
97792f44d919
child 49
a80ba8741be6

added threads (Cocoa)

ui/cocoa/toolkit.h file | annotate | diff | comparison | revisions
ui/cocoa/toolkit.m file | annotate | diff | comparison | revisions
--- a/ui/cocoa/toolkit.h	Mon May 19 15:54:58 2014 +0200
+++ b/ui/cocoa/toolkit.h	Mon May 19 21:15:43 2014 +0200
@@ -64,4 +64,24 @@
 
 @end
 
+@interface UiThread : NSObject {
+    UiObject      *obj;
+    ui_threadfunc job_func;
+    void          *job_data;
+    ui_callback   finish_callback;
+    void          *finish_data;
+}
 
+- (id) initWithObject:(UiObject*)object;
+- (void) setJobFunction:(ui_threadfunc)func;
+- (void) setJobData:(void*)data;
+- (void) setFinishCallback:(ui_callback)callback;
+- (void) setFinishData:(void*)data;
+
+- (void) start;
+- (void) runJob:(id)n;
+- (void) finish:(id)n;
+
+@end
+
+
--- a/ui/cocoa/toolkit.m	Mon May 19 15:54:58 2014 +0200
+++ b/ui/cocoa/toolkit.m	Mon May 19 21:15:43 2014 +0200
@@ -32,6 +32,7 @@
 #import <sys/types.h>
 #import <errno.h>
 
+#import "../common/context.h"
 #import "../common/document.h"
 #import "../common/properties.h"
 
@@ -92,10 +93,29 @@
     }
 }
 
+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_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];
@@ -206,7 +226,7 @@
     if([activeWindow class] == [UiCocoaWindow class]) {
         event.obj = [(UiCocoaWindow*)activeWindow object];
         event.window = event.obj->window;
-        event.document = event.obj->document;
+        event.document = event.obj->ctx->document;
         event.intval = value;
     }
     if(callback) {
@@ -227,7 +247,7 @@
     if([activeWindow class] == [UiCocoaWindow class]) {
         event.obj = [(UiCocoaWindow*)activeWindow object];
         event.window = event.obj->window;
-        event.document = event.obj->document;
+        event.document = event.obj->ctx->document;
         // update state in window data
         UiMenuItem *wmi = [(UiCocoaWindow*)activeWindow getMenuItem: sender];
         wmi->state = state;
@@ -245,4 +265,58 @@
 
 @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) 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);
+}
+
+@end
+
+

mercurial