# HG changeset patch # User Olaf Wintermann # Date 1397122661 -7200 # Node ID c96169444d886e5713ee9fd16fab0fcf869717d3 # Parent 794a5c91c47943a587282f1bf6fbcffdd9011a7b added locale support (Cocoa) and ucx update diff -r 794a5c91c479 -r c96169444d88 application/main.c --- 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"); diff -r 794a5c91c479 -r c96169444d88 make/package_osx.sh --- 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/ + diff -r 794a5c91c479 -r c96169444d88 resource/.DS_Store Binary file resource/.DS_Store has changed diff -r 794a5c91c479 -r c96169444d88 resource/locales/en_EN.properties --- /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! diff -r 794a5c91c479 -r c96169444d88 resource/template.app/Contents/Info.plist --- 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 @@ CFBundleExecutable mk12 CFBundleIdentifier - com.yourcompany.Mk12 + com.yourcompany.toolkit CFBundleInfoDictionaryVersion 6.0 CFBundleName - Test11 + toolkit CFBundlePackageType APPL CFBundleShortVersionString @@ -37,9 +37,34 @@ DTXcodeBuild 10M2518 LSMinimumSystemVersion - 10.6 + 10.7 NSMainNibFile MainMenu + CFBundleDisplayName + + CFBundleGetInfoString + + LSApplicationCategoryType + + CFBundleDocumentTypes + + + LSItemContentTypes + + public.data + + CFBundleTypeIconFile + + CFBundleTypeName + DocumentType + CFBundleTypeRole + Editor + + + NSPrincipalClass NSApplication diff -r 794a5c91c479 -r c96169444d88 ucx/list.c --- 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; } diff -r 794a5c91c479 -r c96169444d88 ui/cocoa/objs.mk --- 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)%) diff -r 794a5c91c479 -r c96169444d88 ui/cocoa/resource.h --- /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" + + diff -r 794a5c91c479 -r c96169444d88 ui/cocoa/resource.m --- /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 +#import +#import + +#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); +} diff -r 794a5c91c479 -r c96169444d88 ui/cocoa/text.h --- 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); diff -r 794a5c91c479 -r c96169444d88 ui/cocoa/text.m --- 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]; } diff -r 794a5c91c479 -r c96169444d88 ui/cocoa/toolkit.h --- 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 { + +} + +- (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; diff -r 794a5c91c479 -r c96169444d88 ui/cocoa/toolkit.m --- 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 { diff -r 794a5c91c479 -r c96169444d88 ui/common/properties.c --- 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; + } +} diff -r 794a5c91c479 -r c96169444d88 ui/common/properties.h --- 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 diff -r 794a5c91c479 -r c96169444d88 ui/gtk/text.c --- 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); } diff -r 794a5c91c479 -r c96169444d88 ui/gtk/text.h --- 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( diff -r 794a5c91c479 -r c96169444d88 ui/gtk/toolkit.c --- 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() { diff -r 794a5c91c479 -r c96169444d88 ui/motif/text.c --- 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; diff -r 794a5c91c479 -r c96169444d88 ui/motif/text.h --- 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( diff -r 794a5c91c479 -r c96169444d88 ui/motif/toolkit.c --- 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) { diff -r 794a5c91c479 -r c96169444d88 ui/ui/properties.h --- 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 diff -r 794a5c91c479 -r c96169444d88 ui/ui/toolkit.h --- 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);