cocoa_gui.m

Go to the documentation of this file.
00001 /***************************************************************************
00002  begin       : August 03 2010
00003  copyright   : (C) 2010 by Samuel Strupp
00004  
00005  ***************************************************************************
00006  *          Please see toplevel file COPYING for license details           *
00007  ***************************************************************************/
00008 
00009 
00010 #ifdef HAVE_CONFIG_H
00011 # include <config.h>
00012 #endif
00013 
00014 
00015 #include "cocoa_gui_dialog_l.h"
00016 #include "cocoa_gui_p.h"
00017 
00018 #include <assert.h>
00019 
00020 #include <gwenhywfar/inherit.h>
00021 #include <gwenhywfar/debug.h>
00022 #include <gwenhywfar/gui_be.h>
00023 #include <gwenhywfar/i18n.h>
00024 
00025 #include <gwenhywfar/text.h>
00026 #include <gwenhywfar/mdigest.h>
00027 #include <gwenhywfar/debug.h>
00028 
00029 
00030 GWEN_INHERIT(GWEN_GUI, COCOA_GUI)
00031 
00032 
00033 #define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
00034 
00035 
00036 
00037 GWEN_GUI *Cocoa_Gui_new() {
00038         GWEN_GUI *gui;
00039         COCOA_GUI *xgui;
00040         
00041         gui=GWEN_Gui_new();
00042         GWEN_NEW_OBJECT(COCOA_GUI, xgui);
00043         GWEN_INHERIT_SETDATA(GWEN_GUI, COCOA_GUI, gui, xgui, Cocoa_Gui_FreeData);
00044         
00045         GWEN_Gui_AddFlags(gui, GWEN_GUI_FLAGS_DIALOGSUPPORTED);
00046         GWEN_Gui_UseDialogs(gui);
00047         xgui->execDialogFn=GWEN_Gui_SetExecDialogFn(gui, COCOA_Gui_ExecDialog);
00048         xgui->openDialogFn=GWEN_Gui_SetOpenDialogFn(gui, COCOA_Gui_OpenDialog);
00049         xgui->closeDialogFn=GWEN_Gui_SetCloseDialogFn(gui, COCOA_Gui_CloseDialog);
00050         xgui->runDialogFn=GWEN_Gui_SetRunDialogFn(gui, COCOA_Gui_RunDialog);
00051         xgui->getFileNameDialogFn=GWEN_Gui_SetGetFileNameFn(gui, COCOA_Gui_GetFileName);
00052         
00053         return gui;
00054 }
00055 
00056 
00057 
00058 void Cocoa_Gui_FreeData(void *bp, void *p) {
00059         COCOA_GUI *xgui;
00060         
00061         xgui=(COCOA_GUI*) p;
00062         
00063         GWEN_FREE_OBJECT(xgui);
00064 }
00065 
00066 
00067 
00068 
00069 int COCOA_Gui_ExecDialog(GWEN_GUI *gui, GWEN_DIALOG *dlg, uint32_t guiid) {
00070         NSLog(@"ExecDialog");
00071         
00072         int rv;
00073         
00074         rv = COCOA_Gui_OpenDialog(gui, dlg, guiid);
00075         if (rv<0) {
00076                 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00077                 return rv;
00078         }
00079         
00080         rv = COCOA_Gui_RunDialog(gui, dlg, 1);
00081         COCOA_Gui_CloseDialog(gui, dlg);
00082         if (rv<0) {
00083                 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00084                 return rv;
00085         }
00086         
00087         return rv;
00088 }
00089 
00090 
00091 
00092 int COCOA_Gui_OpenDialog(GWEN_GUI *gui, GWEN_DIALOG *dlg, uint32_t guiid) {
00093         int rv;
00094         NSWindow *g;
00095         
00096         NSLog(@"OpenDialog");
00097         
00098         CocoaGui_Dialog_Extend(dlg);
00099         rv=CocoaGui_Dialog_Setup(dlg, NULL);
00100         if (rv<0) {
00101                 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00102                 CocoaGui_Dialog_Unextend(dlg);
00103                 return rv;
00104         }
00105         
00106         NSLog(@"OpenDialog Setup");
00107         
00108         g=CocoaGui_Dialog_GetMainWidget(dlg);
00109         if (g==NULL) {
00110                 DBG_ERROR(GWEN_LOGDOMAIN, "No main widget");
00111                 CocoaGui_Dialog_Unextend(dlg);
00112                 return GWEN_ERROR_INVALID;
00113         }
00114         
00115         NSLog(@"OpenDialog GetMainWidget");
00116         
00117         rv=GWEN_Dialog_EmitSignalToAll(dlg, GWEN_DialogEvent_TypeInit, "");
00118         if (rv<0) {
00119                 DBG_INFO(GWEN_LOGDOMAIN, "Error initializing dialog: %d", rv);
00120                 CocoaGui_Dialog_Unextend(dlg);
00121                 return rv;
00122         }
00123         
00124         NSLog(@"OpenDialog EmitSignalToAll");
00125         
00126         /* show dialog */
00127         [g center];
00128         [g makeKeyAndOrderFront:nil];
00129         
00130         
00131         return 0;
00132 }
00133 
00134 
00135 
00136 int COCOA_Gui_CloseDialog(GWEN_GUI *gui, GWEN_DIALOG *dlg) {
00137         NSWindow *g;
00138         int rv;
00139         
00140         g=CocoaGui_Dialog_GetMainWidget(dlg);
00141         if (g==NULL) {
00142                 DBG_ERROR(GWEN_LOGDOMAIN, "No main widget");
00143                 CocoaGui_Dialog_Unextend(dlg);
00144                 return GWEN_ERROR_INVALID;
00145         }
00146         
00147         /* hide dialog */
00148         [g close];
00149         
00150         
00151         /* send fini signal to dialog */
00152         rv=GWEN_Dialog_EmitSignalToAll(dlg, GWEN_DialogEvent_TypeFini, "");
00153         if (rv<0) {
00154                 DBG_INFO(GWEN_LOGDOMAIN, "Error deinitializing dialog: %d", rv);
00155                 CocoaGui_Dialog_Unextend(dlg);
00156                 return rv;
00157         }
00158         
00159         CocoaGui_Dialog_Unextend(dlg);
00160         return 0;
00161 }
00162 
00163 
00164 
00165 int COCOA_Gui_RunDialog(GWEN_GUI *gui, GWEN_DIALOG *dlg, int untilEnd) {
00166         int rv;
00167         
00168         rv=Cocoa_Gui_Dialog_Run(dlg, untilEnd);
00169         if (rv<0) {
00170                 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00171                 return rv;
00172         }
00173         return rv;
00174 }
00175 
00176 
00177 
00178 int COCOA_Gui_GetFileName(GWEN_GUI *gui,
00179                                                   const char *caption,
00180                                                   GWEN_GUI_FILENAME_TYPE fnt,
00181                                                   uint32_t flags,
00182                                                   const char *patterns,
00183                                                   GWEN_BUFFER *pathBuffer,
00184                                                   uint32_t guiid) {
00185         char *folder=NULL;
00186         char *fileName=NULL;
00187         
00188         if (GWEN_Buffer_GetUsedBytes(pathBuffer)) {
00189                 folder=strdup(GWEN_Buffer_GetStart(pathBuffer));
00190                 fileName=strchr(folder, GWEN_DIR_SEPARATOR);
00191                 if (fileName) {
00192                         *fileName=0;
00193                         fileName++;
00194                         if (*fileName==0)
00195                                 fileName=NULL;
00196                 }
00197         }
00198         
00199         switch(fnt) {
00200                 case GWEN_Gui_FileNameType_OpenFileName:
00201                 case GWEN_Gui_FileNameType_OpenDirectory: {
00202                         
00203                         NSString *title = nil;
00204                         if (caption && *caption)
00205                                 title = [[[NSString alloc] initWithCString:caption encoding:NSUTF8StringEncoding] autorelease];
00206                         
00207                         NSOpenPanel *openPanel = [NSOpenPanel openPanel];
00208                         if (openPanel) {
00209                                 if (title) [openPanel setTitle:title];
00210                                 [openPanel setAllowsMultipleSelection:NO];
00211                                 [openPanel setCanChooseFiles:(fnt == GWEN_Gui_FileNameType_OpenFileName)];
00212                                 [openPanel setCanChooseDirectories:(fnt == GWEN_Gui_FileNameType_OpenDirectory)];
00213                                 
00214                                 NSString *pathToFolder = nil;
00215                                 
00216                                 if (fileName && *fileName)
00217                                         pathToFolder = [[[NSString alloc] initWithCString:fileName encoding:NSUTF8StringEncoding] autorelease];
00218                                 
00219                                 if (pathToFolder)
00220                                         [openPanel setDirectory:pathToFolder];
00221                                 
00222                                 NSInteger result = [openPanel runModal];
00223                                 if (result == NSFileHandlingPanelOKButton) {
00224                                         NSArray *urls = [openPanel URLs];
00225                                         if (urls && [urls count] > 0) {
00226                                                 NSString *path = [[(NSURL*)[urls objectAtIndex:0] path] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
00227                                                 if (path && [path length]>0) {
00228                                                         const char *filename = [path cStringUsingEncoding:NSUTF8StringEncoding];
00229                                                         GWEN_Buffer_Reset(pathBuffer);
00230                                                         GWEN_Buffer_AppendString(pathBuffer, filename);
00231                                                         return 0;
00232                                                 }
00233                                         }
00234                                 }
00235                         }
00236                         
00237                         return GWEN_ERROR_USER_ABORTED;
00238                 }
00239                         
00240                 case GWEN_Gui_FileNameType_SaveFileName: {
00241                         
00242                         NSString *title = nil;
00243                         if (caption && *caption)
00244                                 title = [[[NSString alloc] initWithCString:caption encoding:NSUTF8StringEncoding] autorelease];
00245                         
00246                         NSSavePanel *savePanel = [NSSavePanel savePanel];
00247                         if (savePanel) {
00248                                 if (title) [savePanel setTitle:title];
00249                                 [savePanel setCanCreateDirectories:YES];
00250                                 
00251                                 NSString *pathToFolder = nil;
00252                                 
00253                                 if (fileName && *fileName)
00254                                         pathToFolder = [[[NSString alloc] initWithCString:fileName encoding:NSUTF8StringEncoding] autorelease];
00255                                 
00256                                 if (pathToFolder)
00257                                         [savePanel setDirectory:pathToFolder];
00258                                 
00259                                 NSInteger result = [savePanel runModal];
00260                                 if (result == NSFileHandlingPanelOKButton) {
00261                                         NSURL *url = [savePanel URL];
00262                                         if (url) {
00263                                                 NSString *path = [[url path] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
00264                                                 if (path && [path length]>0) {
00265                                                         const char *filename = [path cStringUsingEncoding:NSUTF8StringEncoding];
00266                                                         GWEN_Buffer_Reset(pathBuffer);
00267                                                         GWEN_Buffer_AppendString(pathBuffer, filename);
00268                                                         return 0;
00269                                                 }
00270                                         }
00271                                 }
00272                         }
00273                         
00274                         return GWEN_ERROR_USER_ABORTED;
00275                 }
00276                         
00277                 default:
00278                         break;
00279         }
00280         free(folder);
00281         
00282         return GWEN_ERROR_USER_ABORTED;
00283 }
00284 
00285 
00286