gwenhywfar  4.3.1
cocoa_gui_dialog.m
Go to the documentation of this file.
00001 /***************************************************************************
00002  begin       : August 09 2010
00003  copyright   : (C) 2010 by Samuel Strupp
00004  
00005  ***************************************************************************
00006  *          Please see toplevel file COPYING for license details           *
00007  ***************************************************************************/
00008 
00009 #ifdef HAVE_CONFIG_H
00010 # include <config.h>
00011 #endif
00012 
00013 
00014 #include "cocoa_gui_dialog_p.h"
00015 #include <assert.h>
00016 
00017 #include <gwenhywfar/inherit.h>
00018 #include <gwenhywfar/debug.h>
00019 #include <gwenhywfar/gui_be.h>
00020 #include <gwenhywfar/i18n.h>
00021 
00022 #include <gwenhywfar/text.h>
00023 #include <gwenhywfar/mdigest.h>
00024 #include <gwenhywfar/debug.h>
00025 #include <gwenhywfar/directory.h>
00026 
00027 
00028 
00029 #define COCOA_DIALOG_WIDGET_REAL    0
00030 #define COCOA_DIALOG_WIDGET_CONTENT 1
00031 #define COCOA_DIALOG_WIDGET_DATA    2
00032 
00033 #define COCOA_DIALOG_STRING_TITLE  0
00034 #define COCOA_DIALOG_STRING_VALUE  1
00035 
00036 
00037 
00038 
00039 #include "w_dialog.mm"
00040 #include "w_label.mm"
00041 #include "w_vlayout.mm"
00042 #include "w_hlayout.mm"
00043 #include "w_pushbutton.mm"
00044 #include "w_lineedit.mm"
00045 #include "w_textedit.mm"
00046 #include "w_checkbox.mm"
00047 #include "w_hline.mm"
00048 #include "w_vline.mm"
00049 #include "w_progressbar.mm"
00050 #include "w_radiobutton.mm"
00051 #include "w_tabbook.mm"
00052 #include "w_groupbox.mm"
00053 #include "w_combobox.mm"
00054 #include "w_gridlayout.mm"
00055 #include "w_hspacer.mm"
00056 #include "w_vspacer.mm"
00057 #include "w_listbox.mm"
00058 #include "w_scrollarea.mm"
00059 #include "w_textbrowser.mm"
00060 #include "w_stack.mm"
00061 #include "w_spinbox.mm"
00062 
00063 
00064 
00065 GWEN_INHERIT(GWEN_DIALOG, COCOA_GUI_DIALOG)
00066 
00067 
00068 
00069 
00070 void CocoaGui_Dialog_Extend(GWEN_DIALOG *dlg) {
00071         COCOA_GUI_DIALOG *xdlg;
00072         
00073         GWEN_NEW_OBJECT(COCOA_GUI_DIALOG, xdlg);
00074         GWEN_INHERIT_SETDATA(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg, xdlg, CocoaGui_Dialog_FreeData);
00075         
00076         // set virtual functions
00077         xdlg->setIntPropertyFn=GWEN_Dialog_SetSetIntPropertyFn(dlg, CocoaGui_Dialog_SetIntProperty);
00078         xdlg->getIntPropertyFn=GWEN_Dialog_SetGetIntPropertyFn(dlg, CocoaGui_Dialog_GetIntProperty);
00079         xdlg->setCharPropertyFn=GWEN_Dialog_SetSetCharPropertyFn(dlg, CocoaGui_Dialog_SetCharProperty);
00080         xdlg->getCharPropertyFn=GWEN_Dialog_SetGetCharPropertyFn(dlg, CocoaGui_Dialog_GetCharProperty);
00081         
00082 }
00083 
00084 
00085 
00086 void CocoaGui_Dialog_Unextend(GWEN_DIALOG *dlg){
00087         COCOA_GUI_DIALOG *xdlg;
00088         
00089         assert(dlg);
00090         xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg);
00091         assert(xdlg);
00092         
00093         // reset virtual functions
00094         GWEN_Dialog_SetSetIntPropertyFn(dlg, xdlg->setIntPropertyFn);
00095         GWEN_Dialog_SetGetIntPropertyFn(dlg, xdlg->getIntPropertyFn);
00096         GWEN_Dialog_SetSetCharPropertyFn(dlg, xdlg->setCharPropertyFn);
00097         GWEN_Dialog_SetGetCharPropertyFn(dlg, xdlg->getCharPropertyFn);
00098         
00099         GWEN_INHERIT_UNLINK(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg);
00100 }
00101 
00102 
00103 
00104 void GWENHYWFAR_CB CocoaGui_Dialog_FreeData(void *bp, void *p) {
00105         COCOA_GUI_DIALOG *xdlg;
00106         
00107         xdlg=(COCOA_GUI_DIALOG*) p;
00108         
00109         if (xdlg->mainWindow) {
00110                 NSWindow *window = (xdlg->mainWindow);
00111                 [window release];
00112                 xdlg->mainWindow = nil;
00113         }
00114         
00115         GWEN_FREE_OBJECT(xdlg);
00116 }
00117 
00118 
00119 
00120 CocoaWindow *CocoaGui_Dialog_GetMainWidget(const GWEN_DIALOG *dlg) {
00121         COCOA_GUI_DIALOG *xdlg;
00122         
00123         assert(dlg);
00124         xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg);
00125         assert(xdlg);
00126         
00127         return xdlg->mainWindow;
00128 }
00129 
00130 
00131 
00132 int CocoaGui_Dialog_SetIntProperty(GWEN_DIALOG *dlg,
00133                                                                    GWEN_WIDGET *w,
00134                                                                    GWEN_DIALOG_PROPERTY prop,
00135                                                                    int index,
00136                                                                    int value,
00137                                                                    int doSignal) {
00138         return GWEN_Widget_SetIntProperty(w, prop, index, value, doSignal);
00139 }
00140 
00141 
00142 
00143 int CocoaGui_Dialog_GetIntProperty(GWEN_DIALOG *dlg,
00144                                                                    GWEN_WIDGET *w,
00145                                                                    GWEN_DIALOG_PROPERTY prop,
00146                                                                    int index,
00147                                                                    int defaultValue) {
00148         return GWEN_Widget_GetIntProperty(w, prop, index, defaultValue);
00149 }
00150 
00151 
00152 
00153 int CocoaGui_Dialog_SetCharProperty(GWEN_DIALOG *dlg,
00154                                                                         GWEN_WIDGET *w,
00155                                                                         GWEN_DIALOG_PROPERTY prop,
00156                                                                         int index,
00157                                                                         const char *value,
00158                                                                         int doSignal) {
00159         return GWEN_Widget_SetCharProperty(w, prop, index, value, doSignal);
00160 }
00161 
00162 
00163 
00164 const char *CocoaGui_Dialog_GetCharProperty(GWEN_DIALOG *dlg,
00165                                                                                         GWEN_WIDGET *w,
00166                                                                                         GWEN_DIALOG_PROPERTY prop,
00167                                                                                         int index,
00168                                                                                         const char *defaultValue) {
00169         return GWEN_Widget_GetCharProperty(w, prop, index, defaultValue);
00170 }
00171 
00172 
00173 
00174 int CocoaGui_Dialog_Setup(GWEN_DIALOG *dlg, NSWindow *parentWindow) {
00175         COCOA_GUI_DIALOG *xdlg;
00176         GWEN_WIDGET_TREE *wtree;
00177         GWEN_WIDGET *w;
00178         int rv;
00179         
00180         assert(dlg);
00181         xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg);
00182         assert(xdlg);
00183         
00184         wtree=GWEN_Dialog_GetWidgets(dlg);
00185         if (wtree==NULL) {
00186                 DBG_ERROR(0, "No widget tree in dialog");
00187                 return GWEN_ERROR_NOT_FOUND;
00188         }
00189         w=GWEN_Widget_Tree_GetFirst(wtree);
00190         if (w==NULL) {
00191                 DBG_ERROR(0, "No widgets in dialog");
00192                 return GWEN_ERROR_NOT_FOUND;
00193         }
00194         
00195         rv=CocoaGui_Dialog_SetupTree(w);
00196         if (rv<0) {
00197                 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00198                 return rv;
00199         }
00200         
00201         xdlg->mainWindow=(CocoaWindow*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00202         
00203         return 0;
00204 }
00205 
00206 
00207 
00208 void CocoaGui_Dialog_Leave(GWEN_DIALOG *dlg, int result) {
00209         COCOA_GUI_DIALOG *xdlg;
00210         GWEN_DIALOG *parent;
00211         
00212         // get toplevel dialog, the one which actually is the GUI dialog
00213         while( (parent=GWEN_Dialog_GetParentDialog(dlg)) )
00214                 dlg=parent;
00215         
00216         assert(dlg);
00217         xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg);
00218         assert(xdlg);
00219         
00220         //xdlg->response=result;
00221         /*if (g_main_loop_is_running(xdlg->loop))
00222          g_main_loop_quit(xdlg->loop);*/
00223         if (xdlg->mainWindow) {
00224                 [NSApp abortModal];
00225                 [xdlg->mainWindow close];
00226         }
00227 }
00228 
00229 
00230 
00231 /*static void
00232  run_unmap_handler (NSWindow *window, void* data){
00233  GWEN_DIALOG *dlg;
00234  COCOA_GUI_DIALOG *xdlg;
00235  
00236  dlg=data;
00237  assert(dlg);
00238  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg);
00239  assert(xdlg);
00240  
00241  DBG_ERROR(0, "here");
00242  CocoaGui_Dialog_Leave(dlg, 0);
00243  }
00244  
00245  
00246  
00247  static int run_delete_handler(NSWindow *window, void* data){
00248  GWEN_DIALOG *dlg;
00249  COCOA_GUI_DIALOG *xdlg;
00250  
00251  dlg=data;
00252  assert(dlg);
00253  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg);
00254  assert(xdlg);
00255  
00256  DBG_ERROR(0, "here");
00257  CocoaGui_Dialog_Leave(dlg, 0);
00258  return TRUE; // Do not destroy
00259  }
00260  
00261  
00262  
00263  static void
00264  run_destroy_handler(NSWindow *window, void* data) {
00265  GWEN_DIALOG *dlg;
00266  COCOA_GUI_DIALOG *xdlg;
00267  
00268  dlg=data;
00269  assert(dlg);
00270  xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg);
00271  assert(xdlg);
00272  
00273  DBG_ERROR(0, "Destroyed");
00274  xdlg->destroyed=1;
00275  }*/
00276 
00277 
00278 
00279 int Cocoa_Gui_Dialog_Run(GWEN_DIALOG *dlg, int untilEnd) {
00280         COCOA_GUI_DIALOG *xdlg;
00281         CocoaWindow *g;
00282         
00283         assert(dlg);
00284         xdlg=GWEN_INHERIT_GETDATA(GWEN_DIALOG, COCOA_GUI_DIALOG, dlg);
00285         assert(xdlg);
00286         
00287         g=CocoaGui_Dialog_GetMainWidget(dlg);
00288         if (g==NULL) {
00289                 DBG_ERROR(GWEN_LOGDOMAIN, "No main widget");
00290                 CocoaGui_Dialog_Unextend(dlg);
00291                 return GWEN_ERROR_INVALID;
00292         }
00293         
00294         if (untilEnd) {
00295                 [NSApp runModalForWindow:g];
00296         }
00297         else {
00298                 NSModalSession session = [NSApp beginModalSessionForWindow:g];
00299                 for (;;) {
00300                         if ([NSApp runModalSession:session] != NSRunContinuesResponse)
00301                                 break;
00302                 }
00303                 [NSApp endModalSession:session];
00304         }
00305         
00306         
00307         /* xdlg->unmap_handler =
00308          g_signal_connect(g,
00309          "unmap",
00310          G_CALLBACK (run_unmap_handler),
00311          dlg);
00312          
00313          xdlg->delete_handler =
00314          g_signal_connect(g,
00315          "delete-event",
00316          G_CALLBACK (run_delete_handler),
00317          dlg);
00318          
00319          xdlg->destroy_handler =
00320          g_signal_connect(g,
00321          "destroy",
00322          G_CALLBACK (run_destroy_handler),
00323          dlg);*/
00324         
00325         //xdlg->loop=g_main_loop_new(NULL, FALSE);
00326         /*if (untilEnd)
00327          g_main_loop_run(xdlg->loop);
00328          else {
00329          GMainContext *ctx;
00330          
00331          ctx=g_main_loop_get_context(xdlg->loop);
00332          while(g_main_context_pending(ctx))
00333          g_main_context_iteration(ctx, FALSE);
00334          }
00335          g_main_loop_unref(xdlg->loop);
00336          
00337          if (!xdlg->destroyed) {
00338          g_signal_handler_disconnect(g, xdlg->unmap_handler);
00339          g_signal_handler_disconnect(g, xdlg->delete_handler);
00340          g_signal_handler_disconnect(g, xdlg->destroy_handler);
00341          }
00342          
00343          return xdlg->response;*/
00344         return 0;
00345 }
00346 
00347 
00348 
00349 int CocoaGui_Dialog_SetupTree(GWEN_WIDGET *w) {
00350         int rv;
00351         
00352         switch(GWEN_Widget_GetType(w)) {
00353                 case GWEN_Widget_TypeDialog:
00354                         rv=CocoaGui_WDialog_Setup(w);
00355                         break;
00356                 case GWEN_Widget_TypeLabel:
00357                         rv=CocoaGui_WLabel_Setup(w);
00358                         break;
00359                 case GWEN_Widget_TypeVLayout:
00360                         rv=CocoaGui_WVLayout_Setup(w);
00361                         break;
00362                 case GWEN_Widget_TypePushButton:
00363                         rv=CocoaGui_WPushButton_Setup(w);
00364                         break;
00365                 case GWEN_Widget_TypeHLayout:
00366                         rv=CocoaGui_WHLayout_Setup(w);
00367                         break;
00368                 case GWEN_Widget_TypeLineEdit:
00369                         rv=CocoaGui_WLineEdit_Setup(w);
00370                         break;
00371                 case GWEN_Widget_TypeTextEdit:
00372                         rv=CocoaGui_WTextEdit_Setup(w);
00373                         break;
00374                 case GWEN_Widget_TypeCheckBox:
00375                         rv=CocoaGui_WCheckBox_Setup(w);
00376                         break;
00377                 case GWEN_Widget_TypeHLine:
00378                         rv=CocoaGui_WHLine_Setup(w);
00379                         break;
00380                 case GWEN_Widget_TypeVLine:
00381                         rv=CocoaGui_WVLine_Setup(w);
00382                         break;
00383                 case GWEN_Widget_TypeProgressBar:
00384                         rv=CocoaGui_WProgressBar_Setup(w);
00385                         break;
00386                 case GWEN_Widget_TypeRadioButton:
00387                         rv=CocoaGui_WRadioButton_Setup(w);
00388                         break;
00389                 case GWEN_Widget_TypeTabBook:
00390                         rv=CocoaGui_WTabBook_Setup(w);
00391                         break;
00392                 case GWEN_Widget_TypeTabPage:
00393                         // just re-use vbox 
00394                         GWEN_Widget_AddFlags(w, GWEN_WIDGET_FLAGS_FILLX | GWEN_WIDGET_FLAGS_FILLY);
00395                         rv=CocoaGui_WVLayout_Setup(w);
00396                         break;
00397                 case GWEN_Widget_TypeGroupBox:
00398                         rv=CocoaGui_WGroupBox_Setup(w);
00399                         break;
00400                 case GWEN_Widget_TypeComboBox:
00401                         rv=CocoaGui_WComboBox_Setup(w);
00402                         break;
00403                 case GWEN_Widget_TypeGridLayout:
00404                         rv=CocoaGui_WGridLayout_Setup(w);
00405                         break;
00406                 case GWEN_Widget_TypeHSpacer:
00407                         rv=CocoaGui_WHSpacer_Setup(w);
00408                         break;
00409                 case GWEN_Widget_TypeVSpacer:
00410                         rv=CocoaGui_WVSpacer_Setup(w);
00411                         break;
00412                 case GWEN_Widget_TypeListBox:
00413                         rv=CocoaGui_WListBox_Setup(w);
00414                         break;
00415                 case GWEN_Widget_TypeScrollArea:
00416                         rv=CocoaGui_WScrollArea_Setup(w);
00417                         break;
00418                 case GWEN_Widget_TypeTextBrowser:
00419                         rv=CocoaGui_WTextBrowser_Setup(w);
00420                         break;
00421                 case GWEN_Widget_TypeWidgetStack:
00422                         rv=CocoaGui_WStack_Setup(w);
00423                         break;
00424                 case GWEN_Widget_TypeSpinBox:
00425                         rv=CocoaGui_WSpinBox_Setup(w);
00426                         break;
00427                 default:
00428                         DBG_ERROR(GWEN_LOGDOMAIN, "Unhandled widget type %d", GWEN_Widget_GetType(w));
00429                         rv=GWEN_ERROR_INVALID;
00430                         break;
00431         }
00432         
00433         if (rv<0) {
00434                 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00435                 return rv;
00436         }
00437         else {
00438                 GWEN_WIDGET *wChild;
00439                 
00440                 // handle children
00441                 wChild=GWEN_Widget_Tree_GetFirstChild(w);
00442                 while(wChild) {
00443                         // recursion
00444                         rv=CocoaGui_Dialog_SetupTree(wChild);
00445                         if (rv<0) {
00446                                 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
00447                                 return rv;
00448                         }
00449                         wChild=GWEN_Widget_Tree_GetNext(wChild);
00450                 }
00451         }
00452         
00453         return 0;
00454 }
00455 
00456