added locale support (Cocoa) and ucx update

Thu, 10 Apr 2014 11:37:41 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 10 Apr 2014 11:37:41 +0200
changeset 29
c96169444d88
parent 28
794a5c91c479
child 30
34513f76d5a8

added locale support (Cocoa) and ucx update

application/main.c file | annotate | diff | comparison | revisions
make/package_osx.sh file | annotate | diff | comparison | revisions
resource/.DS_Store file | annotate | diff | comparison | revisions
resource/locales/en_EN.properties file | annotate | diff | comparison | revisions
resource/template.app/Contents/Info.plist file | annotate | diff | comparison | revisions
ucx/list.c file | annotate | diff | comparison | revisions
ui/cocoa/objs.mk file | annotate | diff | comparison | revisions
ui/cocoa/resource.h file | annotate | diff | comparison | revisions
ui/cocoa/resource.m file | annotate | diff | comparison | revisions
ui/cocoa/text.h file | annotate | diff | comparison | revisions
ui/cocoa/text.m file | annotate | diff | comparison | revisions
ui/cocoa/toolkit.h file | annotate | diff | comparison | revisions
ui/cocoa/toolkit.m file | annotate | diff | comparison | revisions
ui/common/properties.c file | annotate | diff | comparison | revisions
ui/common/properties.h file | annotate | diff | comparison | revisions
ui/gtk/text.c file | annotate | diff | comparison | revisions
ui/gtk/text.h file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/motif/text.c file | annotate | diff | comparison | revisions
ui/motif/text.h file | annotate | diff | comparison | revisions
ui/motif/toolkit.c file | annotate | diff | comparison | revisions
ui/ui/properties.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Apr 06 13:21:37 2014 +0200
+++ b/application/main.c	Thu Apr 10 11:37:41 2014 +0200
@@ -51,6 +51,9 @@
     UiObject *window = ui_window("Mod1", NULL);
     //ui_window_addint(window, "check1");
     ui_show(window);
+    if(event->eventdata)  {
+        printf("%s\n", event->eventdata);
+    }
 }
 
 void action_open(UiEvent *event, void *data) {
@@ -77,7 +80,7 @@
 void action_save(UiEvent *event, void *data) {
     TestWindowData *wd = event->window;
     printf("Text: {%s}\n", ui_getval(wd->text));
-    ui_setval(wd->text, "--------");
+    ui_setval(wd->text, uistr("hello"));
     
     ui_list_append(list, "abc");
     ui_notify(list->observers, NULL);
@@ -150,8 +153,11 @@
 }
 
 
-int main(int argc, char** argv) {
+int main(int argc, char** argv) { 
     ui_init("app1", argc, argv);
+    ui_load_locale(NULL);
+    ui_openfilefunc(action_new, NULL);
+    
     
     list = ui_list_new();
     ui_list_append(list, "file1.txt");
--- a/make/package_osx.sh	Sun Apr 06 13:21:37 2014 +0200
+++ b/make/package_osx.sh	Thu Apr 10 11:37:41 2014 +0200
@@ -1,8 +1,12 @@
 #!/bin/sh
 
 # create .app
+rm -Rf build/mk12.app
 cp -R resource/template.app build/mk12.app
 
 mkdir -p build/mk12.app/Contents/MacOS/
+
 cp build/bin/mk12 build/mk12.app/Contents/MacOS/
 
+cp -R resource/locales build/mk12.app/Contents/Resources/
+
Binary file resource/.DS_Store has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resource/locales/en_EN.properties	Thu Apr 10 11:37:41 2014 +0200
@@ -0,0 +1,1 @@
+hello = HELLO WORLD!
--- a/resource/template.app/Contents/Info.plist	Sun Apr 06 13:21:37 2014 +0200
+++ b/resource/template.app/Contents/Info.plist	Thu Apr 10 11:37:41 2014 +0200
@@ -9,11 +9,11 @@
 	<key>CFBundleExecutable</key>
 	<string>mk12</string>
 	<key>CFBundleIdentifier</key>
-	<string>com.yourcompany.Mk12</string>
+	<string>com.yourcompany.toolkit</string>
 	<key>CFBundleInfoDictionaryVersion</key>
 	<string>6.0</string>
 	<key>CFBundleName</key>
-	<string>Test11</string>
+	<string>toolkit</string>
 	<key>CFBundlePackageType</key>
 	<string>APPL</string>
 	<key>CFBundleShortVersionString</key>
@@ -37,9 +37,34 @@
 	<key>DTXcodeBuild</key>
 	<string>10M2518</string>
 	<key>LSMinimumSystemVersion</key>
-	<string>10.6</string>
+	<string>10.7</string>
 	<key>NSMainNibFile</key>
 	<string>MainMenu</string>
+	<key>CFBundleDisplayName</key>
+	<string></string>
+	<key>CFBundleGetInfoString</key>
+	<string></string>
+	<key>LSApplicationCategoryType</key>
+	<string></string>
+	<key>CFBundleDocumentTypes</key>
+	<array>
+		<dict>
+			<key>LSItemContentTypes</key>
+			<array>
+				<string>public.data</string>
+			</array>
+			<key>CFBundleTypeIconFile</key>
+			<string></string>
+			<key>CFBundleTypeName</key>
+			<string>DocumentType</string>
+			<key>CFBundleTypeRole</key>
+			<string>Editor</string>
+<!--
+			<key>NSDocumentClass</key>
+			<string>Document</string>
+-->
+		</dict>
+	</array>
 	<key>NSPrincipalClass</key>
 	<string>NSApplication</string>
 </dict>
--- a/ucx/list.c	Sun Apr 06 13:21:37 2014 +0200
+++ b/ucx/list.c	Thu Apr 10 11:37:41 2014 +0200
@@ -304,18 +304,18 @@
 }
     
 UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) {
-    if (e->prev == NULL) {
-        if(e->next != NULL) {
-            e->next->prev = NULL;
-            l = e->next;
-        } else {
-            l = NULL;
-        }
-        
-    } else {
-        e->prev->next = e->next;
+    if (l == e) {
+        l = e->next;
+    }
+    
+    if (e->next) {
         e->next->prev = e->prev;
     }
+    
+    if (e->prev) {
+        e->prev->next = e->next;
+    }
+    
     alloc->free(alloc->pool, e);
     return l;
 }
--- a/ui/cocoa/objs.mk	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/cocoa/objs.mk	Thu Apr 10 11:37:41 2014 +0200
@@ -36,6 +36,7 @@
 COCOAOBJ += toolbar.o
 COCOAOBJ += container.o
 COCOAOBJ += text.o
+COCOAOBJ += resource.o
 
 
 TOOLKITOBJS = $(COCOAOBJ:%=$(COCOA_OBJPRE)%)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/cocoa/resource.h	Thu Apr 10 11:37:41 2014 +0200
@@ -0,0 +1,32 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "../ui/toolkit.h"
+#import "../ui/properties.h"
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/cocoa/resource.m	Thu Apr 10 11:37:41 2014 +0200
@@ -0,0 +1,49 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <stdio.h>
+#import <stdlib.h>
+#import <string.h>
+
+#import "resource.h"
+
+
+
+void ui_load_locale(char *locale) {
+    NSString *localeString = nil;
+    if(locale) {
+        localeString = [[NSString alloc]initWithUTF8String:locale];
+    } else {
+        localeString = @"en_EN";
+    }
+    
+    NSString *path = [[NSBundle mainBundle] pathForResource:localeString ofType:@"properties" inDirectory:@"locales"];
+    
+    const char *p = [path UTF8String];
+    uic_load_language_file(p);
+}
--- a/ui/cocoa/text.h	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/cocoa/text.h	Thu Apr 10 11:37:41 2014 +0200
@@ -60,3 +60,4 @@
 void ui_textarea_insert(UiText *text, int pos, char *str);
 int  ui_textarea_position(UiText *text);
 void ui_textarea_selection(UiText *text, int *begin, int *end);
+int ui_textarea_length(UiText *text);
--- a/ui/cocoa/text.m	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/cocoa/text.m	Thu Apr 10 11:37:41 2014 +0200
@@ -81,6 +81,8 @@
     [textview setAllowsUndo:TRUE];
     [textview setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
     
+    [textview setFont:[NSFont fontWithName:@"Menlo" size:12]];
+    
     [scrollview setDocumentView:textview];
     
     ct->add(ct, scrollview);
@@ -93,6 +95,7 @@
         value->insert = ui_textarea_insert;
         value->position = ui_textarea_position;
         value->selection = ui_textarea_selection;
+        value->length = ui_textarea_length;
         value->value = NULL;
         value->obj = textview;
         
@@ -173,6 +176,10 @@
     *end = range.location + range.length;
 }
 
+int ui_textarea_length(UiText *text) {
+    return [[(NSTextView*)text->obj textStorage] length];
+}
+
 void ui_text_undo(UiText *text) {
     [(NSUndoManager*)text->undomgr undo];
 }
--- a/ui/cocoa/toolkit.h	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/cocoa/toolkit.h	Thu Apr 10 11:37:41 2014 +0200
@@ -31,6 +31,19 @@
 #include "../common/context.h"
 #include "../common/object.h"
 
+
+@interface UiApplicationDelegate : NSObject<NSApplicationDelegate> {
+    
+}
+
+- (void)applicationWillTerminate:(NSNotification*)notification;
+
+- (BOOL)applicationShouldHandleReopen:(NSApplication *)app hasVisibleWindows:(BOOL)visible;
+
+- (BOOL)application:(NSApplication *)application openFile:(NSString *)filename;
+
+@end
+
 @interface EventWrapper : NSObject {
     void         *data;
     ui_callback  callback;
--- a/ui/cocoa/toolkit.m	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/cocoa/toolkit.m	Thu Apr 10 11:37:41 2014 +0200
@@ -45,14 +45,20 @@
 
 static char *application_name;
 
-static ui_callback appclose_fnc;
-static void *appclose_udata;
+static ui_callback   appclose_fnc;
+static void          *appclose_udata;
+
+static ui_callback   openfile_fnc;
+static void          *openfile_udata;
 
 void ui_init(char *appname, int argc, char **argv) {
     pool = [[NSAutoreleasePool alloc] init];
     [NSApplication sharedApplication];
     [NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
     
+    UiApplicationDelegate *delegate = [[UiApplicationDelegate alloc]init];
+    [NSApp setDelegate: delegate];
+    
     
     uic_docmgr_init();
     ui_menu_init();
@@ -66,6 +72,16 @@
     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_show(UiObject *obj) {
     uic_check_group_widgets(obj->ctx);
     if([obj->widget class] == [UiCocoaWindow class]) {
@@ -115,6 +131,37 @@
     }
 }
 
+
+@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);
+    }
+    
+    return NO;
+}
+
+@end
+
+
 @implementation EventWrapper
 
 - (EventWrapper*) initWithData: (void*)d callback:(ui_callback) f {
--- a/ui/common/properties.c	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/common/properties.c	Thu Apr 10 11:37:41 2014 +0200
@@ -38,6 +38,7 @@
 #include "../../ucx/properties.h"
 
 static UiProperties *application_properties;
+static UiProperties *language;
 
 char* ui_getappdir() {
     if(ui_appname() == NULL) {
@@ -172,3 +173,47 @@
         ucx_map_cstr_put(application_properties, name, value);
     }
 }
+
+
+
+#ifndef UI_COCOA
+
+void ui_load_locale(char *locale) {
+    char *basedir = ui_get_locale_basedir();
+}
+
+#endif
+
+void uic_load_language_file(char *path) {
+    UcxMap *lang = ucx_map_new(256);
+    
+    printf("path: %s\n", path);
+    FILE *file = fopen(path, "r");
+    if(!file) {
+        printf("cannot open file: %s\n", path);
+        free(path);
+        return;
+    }
+    
+    if(ucx_properties_load(lang, file)) {
+        fprintf(stderr, "Ui Error: Cannot load locale.\n");
+    }
+    
+    fclose(file);
+    
+    ucx_map_rehash(lang);
+    
+    language = lang;
+}
+
+char* uistr(char *name) {
+    if(!language) {
+        return "missing string";
+    }
+    char *value = ucx_map_cstr_get(language, name);
+    if(!value) {
+        return "missing string";
+    } else {
+        return value;
+    }
+}
--- a/ui/common/properties.h	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/common/properties.h	Thu Apr 10 11:37:41 2014 +0200
@@ -46,6 +46,8 @@
 void uic_load_app_properties();
 void uic_store_app_properties();
 
+void uic_load_language_file(char *path);
+    
 #ifdef	__cplusplus
 }
 #endif
--- a/ui/gtk/text.c	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/gtk/text.c	Thu Apr 10 11:37:41 2014 +0200
@@ -103,6 +103,7 @@
         value->insert = ui_textarea_insert;
         value->position = ui_textarea_position;
         value->selection = ui_textarea_selection;
+        value->length = ui_textarea_length;
         value->value = NULL;
         GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_area));
         value->obj = buf;
@@ -192,6 +193,14 @@
     *end = gtk_text_iter_get_offset(&e);
 }
 
+int ui_textarea_length(UiText *text) {
+    GtkTextBuffer *buf = text->obj;
+    GtkTextIter start;
+    GtkTextIter end;
+    gtk_text_buffer_get_bounds(buf, &start, &end);
+    return gtk_text_iter_get_offset(&end);
+}
+
 void ui_textarea_realize_event(GtkWidget *widget, gpointer data) {
     gtk_widget_grab_focus(widget);
 }
--- a/ui/gtk/text.h	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/gtk/text.h	Thu Apr 10 11:37:41 2014 +0200
@@ -65,6 +65,7 @@
 void ui_textarea_insert(UiText *text, int pos, char *str);
 int ui_textarea_position(UiText *text);
 void ui_textarea_selection(UiText *text, int *begin, int *end);
+int ui_textarea_length(UiText *text);
 
 void ui_textarea_realize_event(GtkWidget *widget, gpointer data);
 void ui_textbuf_insert(
--- a/ui/gtk/toolkit.c	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/gtk/toolkit.c	Thu Apr 10 11:37:41 2014 +0200
@@ -37,8 +37,8 @@
 
 static char *application_name;
 
-static ui_callback appclose_fnc;
-static void *appclose_udata;
+static ui_callback   appclose_fnc;
+static void          *appclose_udata;
 
 void ui_init(char *appname, int argc, char **argv) {
     gtk_init(&argc, &argv);
@@ -53,13 +53,17 @@
     uic_load_app_properties();
 }
 
+char* ui_appname() {
+    return application_name;
+}
+
 void ui_exitfunc(ui_callback f, void *udata) {
     appclose_fnc = f;
     appclose_udata = udata;
 }
 
-char* ui_appname() {
-    return application_name;
+void ui_openfilefunc(ui_callback f, void *userdata) {
+    // OS X only
 }
 
 void ui_main() {
--- a/ui/motif/text.c	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/motif/text.c	Thu Apr 10 11:37:41 2014 +0200
@@ -65,6 +65,7 @@
         value->insert = ui_textarea_insert;
         value->position = ui_textarea_position;
         value->selection = ui_textarea_selection;
+        value->length = ui_textarea_length;
         value->value = NULL;
         value->obj = text_area;
         
@@ -129,6 +130,10 @@
     XmTextGetSelectionPosition(text->obj, (long*)begin, (long*)end);
 }
 
+int ui_textarea_length(UiText *text) {
+    return (int)XmTextGetLastPosition(text->obj);
+}
+
 UiUndoMgr* ui_create_undomgr() {
     UiUndoMgr *mgr = malloc(sizeof(UiUndoMgr));
     mgr->begin = NULL;
--- a/ui/motif/text.h	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/motif/text.h	Thu Apr 10 11:37:41 2014 +0200
@@ -65,6 +65,7 @@
 void ui_textarea_insert(UiText *text, int pos, char *str);
 int ui_textarea_position(UiText *text);
 void ui_textarea_selection(UiText *text, int *begin, int *end);
+int ui_textarea_length(UiText *text);
 
 UiUndoMgr* ui_create_undomgr();
 void ui_text_selection_callback(
--- a/ui/motif/toolkit.c	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/motif/toolkit.c	Thu Apr 10 11:37:41 2014 +0200
@@ -80,6 +80,10 @@
     appclose_udata = udata;
 }
 
+void ui_openfilefunc(ui_callback f, void *userdata) {
+    // OS X only
+}
+
 void ui_main() {
     XtAppMainLoop(app);
     if(appclose_fnc) {
--- a/ui/ui/properties.h	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/ui/properties.h	Thu Apr 10 11:37:41 2014 +0200
@@ -45,6 +45,10 @@
 
 void ui_set_default_property(char *name, char *value);
 
+void ui_load_locale(char *locale);
+    
+char* uistr(char *name);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/ui/ui/toolkit.h	Sun Apr 06 13:21:37 2014 +0200
+++ b/ui/ui/toolkit.h	Thu Apr 10 11:37:41 2014 +0200
@@ -142,6 +142,7 @@
     void  (*insert)(UiText*, int, char*);
     int   (*position)(UiText*);
     void  (*selection)(UiText*, int*, int*); // text, begin, end
+    int   (*length)(UiText*);
     char  *value;
     void  *obj;
     void  *undomgr;
@@ -170,9 +171,11 @@
 };
 
 void ui_init(char *appname, int argc, char **argv);
-void ui_exitfunc(ui_callback f, void *udata);
 char* ui_appname();
 
+void ui_exitfunc(ui_callback f, void *userdata);
+void ui_openfilefunc(ui_callback f, void *userdata);
+
 void ui_main();
 void ui_show(UiObject *obj);
 void ui_set_enabled(UIWIDGET widget, int enabled);

mercurial