gwenhywfar
4.3.3
|
00001 /*************************************************************************** 00002 begin : Fri Jan 22 2010 00003 copyright : (C) 2010 by Martin Preuss 00004 email : martin@libchipcard.de 00005 00006 *************************************************************************** 00007 * Please see toplevel file COPYING for license details * 00008 ***************************************************************************/ 00009 00010 #ifdef HAVE_CONFIG_H 00011 # include <config.h> 00012 #endif 00013 00014 00015 #include "fox16_gui.hpp" 00016 #include "fox16_gui_dialog_l.hpp" 00017 #include "fox16_gui_updater_l.hpp" 00018 00019 #include <gwenhywfar/debug.h> 00020 00021 00022 00023 FOX16_Gui::WinScope::WinScope(uint32_t parentId, FXWindow *w) 00024 : m_parentId(parentId) 00025 , m_id(0) 00026 , m_window(w) 00027 , m_type(WIN_SCOPE_TYPE_WINDOW) { 00028 FOX16_Gui *gui=FOX16_Gui::getFgGui(); 00029 assert(gui); 00030 00031 if (m_parentId==0) 00032 m_parentId=gui->getIdOfLastScope(); 00033 m_id=gui->getNextId(); 00034 gui->addWinScope(this); 00035 } 00036 00037 00038 00039 FOX16_Gui::WinScope::WinScope(FXWindow *w) 00040 : m_parentId() 00041 , m_id(ID_MAINWINDOW) 00042 , m_window(w) 00043 , m_type(WIN_SCOPE_TYPE_WINDOW) { 00044 FOX16_Gui *gui=FOX16_Gui::getFgGui(); 00045 assert(gui); 00046 00047 gui->addWinScope(this); 00048 } 00049 00050 00051 00052 FOX16_Gui::WinScope::WinScope(FOX16_Gui::WinScope::WIN_SCOPE_TYPE t, uint32_t parentId, FXWindow *w) 00053 : m_parentId(parentId) 00054 , m_id(0) 00055 , m_window(w) 00056 , m_type(t) { 00057 FOX16_Gui *gui=FOX16_Gui::getFgGui(); 00058 assert(gui); 00059 00060 if (m_parentId==0) 00061 m_parentId=gui->getIdOfLastScope(); 00062 m_id=gui->getNextId(); 00063 gui->addWinScope(this); 00064 } 00065 00066 00067 00068 FOX16_Gui::WinScope::~WinScope() { 00069 FOX16_Gui *gui=FOX16_Gui::getFgGui(); 00070 assert(gui); 00071 gui->delWinScope(this); 00072 } 00073 00074 00075 00076 00077 00078 00079 FOX16_Gui::FOX16_Gui(FXApp *a) 00080 :CppGui() 00081 ,m_app(a) 00082 ,m_lastId(0) 00083 ,m_updater() 00084 ,m_fontList(NULL) 00085 { 00086 m_updater=new FOX16_GuiUpdater(); 00087 GWEN_Gui_AddFlags(_gui, GWEN_GUI_FLAGS_DIALOGSUPPORTED); 00088 GWEN_Gui_UseDialogs(_gui); 00089 GWEN_Gui_SetName(_gui, "fox16-gui"); 00090 m_fontList=HtmlFont_List_new(); 00091 } 00092 00093 00094 00095 FOX16_Gui::~FOX16_Gui() { 00096 if (!m_scopeList.empty()) { 00097 DBG_ERROR(GWEN_LOGDOMAIN, "ScopeList is not empty!"); 00098 } 00099 00100 if (m_updater) 00101 delete m_updater; 00102 HtmlFont_List_free(m_fontList); 00103 } 00104 00105 00106 00107 uint32_t FOX16_Gui::getNextId() { 00108 return ++m_lastId; 00109 } 00110 00111 00112 00113 uint32_t FOX16_Gui::getIdOfLastScope() { 00114 if (!m_scopeList.empty()) 00115 return m_scopeList.back()->getId(); 00116 00117 return 0; 00118 } 00119 00120 00121 00122 void FOX16_Gui::addWinScope(WinScope *ws) { 00123 m_scopeList.push_back(ws); 00124 } 00125 00126 00127 00128 void FOX16_Gui::delWinScope(WinScope *ws) { 00129 m_scopeList.remove(ws); 00130 } 00131 00132 00133 00134 FOX16_Gui::WinScope *FOX16_Gui::findWinScope(uint32_t id) { 00135 WinScopePtrList::iterator it; 00136 00137 for (it=m_scopeList.begin(); 00138 it!=m_scopeList.end(); 00139 it++) { 00140 if ((*it)->getId()==id) 00141 return (*it); 00142 } 00143 00144 return NULL; 00145 } 00146 00147 00148 00149 FXWindow *FOX16_Gui::getGuiWindow(uint32_t id) { 00150 return m_app->getActiveWindow(); 00151 } 00152 00153 00154 00155 00156 FOX16_Gui *FOX16_Gui::getFgGui() { 00157 CppGui *cppgui; 00158 00159 cppgui=CppGui::getCppGui(); 00160 if (cppgui) 00161 return dynamic_cast<FOX16_Gui*>(cppgui); 00162 else 00163 return NULL; 00164 } 00165 00166 00167 00168 void FOX16_Gui::dumpScopeList() { 00169 WinScopePtrList::iterator it; 00170 00171 for (it=m_scopeList.begin(); 00172 it!=m_scopeList.end(); 00173 it++) { 00174 const char *s; 00175 00176 switch((*it)->getType()) { 00177 case WinScope::WIN_SCOPE_TYPE_WINDOW: 00178 s="window"; 00179 break; 00180 default: 00181 s="unknown"; 00182 break; 00183 } 00184 fprintf(stderr, "WinScope: id %08x, parent %08x, type %s\n", 00185 (*it)->getId(), 00186 (*it)->getParentId(), 00187 s); 00188 } 00189 } 00190 00191 00192 00193 00194 int FOX16_Gui::print(const char *docTitle, 00195 const char *docType, 00196 const char *descr, 00197 const char *text, 00198 uint32_t guiid) { 00199 DBG_ERROR(GWEN_LOGDOMAIN, "Not implemented"); 00200 return GWEN_ERROR_NOT_IMPLEMENTED; 00201 } 00202 00203 00204 00205 FXString FOX16_Gui::getRawText(const char *text) { 00206 const char *p=0; 00207 const char *p2=0; 00208 00209 if (text==NULL) 00210 return FXString(""); 00211 00212 /* find begin of HTML area */ 00213 p=text; 00214 while ((p=strchr(p, '<'))) { 00215 const char *t; 00216 00217 t=p; 00218 t++; 00219 if (toupper(*t)=='H') { 00220 t++; 00221 if (toupper(*t)=='T') { 00222 t++; 00223 if (toupper(*t)=='M') { 00224 t++; 00225 if (toupper(*t)=='L') { 00226 t++; 00227 if (toupper(*t)=='>') { 00228 break; 00229 } 00230 } 00231 } 00232 } 00233 } 00234 p++; 00235 } /* while */ 00236 00237 /* find end of HTML area */ 00238 if (p) { 00239 p2=p; 00240 p2+=6; /* skip "<html>" */ 00241 while ((p2=strchr(p2, '<'))) { 00242 const char *t; 00243 00244 t=p2; 00245 t++; 00246 if (toupper(*t)=='/') { 00247 t++; 00248 if (toupper(*t)=='H') { 00249 t++; 00250 if (toupper(*t)=='T') { 00251 t++; 00252 if (toupper(*t)=='M') { 00253 t++; 00254 if (toupper(*t)=='L') { 00255 t++; 00256 if (toupper(*t)=='>') { 00257 break; 00258 } 00259 } 00260 } 00261 } 00262 } 00263 } 00264 p2++; 00265 } /* while */ 00266 } 00267 00268 if (p && p2) { 00269 p2+=7; /* skip "</html>" */ 00270 00271 int startPos=(p-text); 00272 int endPos=(p2-text); 00273 FXString result; 00274 00275 result=FXString(text); 00276 result.erase(startPos, endPos); 00277 return result; 00278 } 00279 else 00280 return FXString(text); 00281 } 00282 00283 00284 00285 FXString FOX16_Gui::getHtmlText(const char *text) { 00286 const char *p=0; 00287 const char *p2=0; 00288 00289 if (text==NULL) 00290 return FXString(""); 00291 00292 /* find begin of HTML area */ 00293 p=text; 00294 while ((p=strchr(p, '<'))) { 00295 const char *t; 00296 00297 t=p; 00298 t++; 00299 if (toupper(*t)=='H') { 00300 t++; 00301 if (toupper(*t)=='T') { 00302 t++; 00303 if (toupper(*t)=='M') { 00304 t++; 00305 if (toupper(*t)=='L') { 00306 t++; 00307 if (toupper(*t)=='>') { 00308 break; 00309 } 00310 } 00311 } 00312 } 00313 } 00314 p++; 00315 } /* while */ 00316 00317 /* find end of HTML area */ 00318 if (p) { 00319 p+=6; /* skip "<html>" */ 00320 p2=p; 00321 while ((p2=strchr(p2, '<'))) { 00322 const char *t; 00323 00324 t=p2; 00325 t++; 00326 if (toupper(*t)=='/') { 00327 t++; 00328 if (toupper(*t)=='H') { 00329 t++; 00330 if (toupper(*t)=='T') { 00331 t++; 00332 if (toupper(*t)=='M') { 00333 t++; 00334 if (toupper(*t)=='L') { 00335 t++; 00336 if (toupper(*t)=='>') { 00337 break; 00338 } 00339 } 00340 } 00341 } 00342 } 00343 } 00344 p2++; 00345 } /* while */ 00346 } 00347 00348 if (p && p2) 00349 return FXString(p, p2-p); 00350 else 00351 return FXString(text); 00352 } 00353 00354 00355 00356 int FOX16_Gui::execDialog(GWEN_DIALOG *dlg, uint32_t guiid) { 00357 FOX16_GuiDialog foxDlg(this, dlg); 00358 FXWindow *owner; 00359 00360 /* get main window of parent dialog (if any) */ 00361 owner=m_app->getActiveWindow(); 00362 00363 /* setup widget tree for the dialog */ 00364 if (!(foxDlg.setup(owner))) { 00365 return GWEN_ERROR_GENERIC; 00366 } 00367 00368 return foxDlg.execute(); 00369 } 00370 00371 00372 00373 int FOX16_Gui::openDialog(GWEN_DIALOG *dlg, uint32_t guiid) { 00374 FOX16_GuiDialog *foxDlg; 00375 FXWindow *owner; 00376 00377 /* get main window of parent dialog (if any) */ 00378 owner=m_app->getActiveWindow(); 00379 00380 foxDlg=new FOX16_GuiDialog(this, dlg); 00381 00382 /* setup widget tree for the dialog */ 00383 if (!(foxDlg->setup(owner))) { 00384 delete foxDlg; 00385 return GWEN_ERROR_GENERIC; 00386 } 00387 00388 foxDlg->openDialog(); 00389 m_updater->guiUpdate(); 00390 00391 return 0; 00392 } 00393 00394 00395 00396 int FOX16_Gui::closeDialog(GWEN_DIALOG *dlg) { 00397 FOX16_GuiDialog *foxDlg; 00398 00399 foxDlg=FOX16_GuiDialog::getDialog(dlg); 00400 assert(foxDlg); 00401 00402 foxDlg->closeDialog(); 00403 delete foxDlg; 00404 m_updater->guiUpdate(); 00405 00406 return 0; 00407 } 00408 00409 00410 00411 int FOX16_Gui::runDialog(GWEN_DIALOG *dlg, int untilEnd) { 00412 FOX16_GuiDialog *foxDlg; 00413 00414 foxDlg=FOX16_GuiDialog::getDialog(dlg); 00415 assert(foxDlg); 00416 00417 if (untilEnd) 00418 return foxDlg->cont(); 00419 else { 00420 m_updater->guiUpdate(); 00421 return 0; 00422 } 00423 } 00424 00425 00426 00427 int FOX16_Gui::getFileName(const char *caption, 00428 GWEN_GUI_FILENAME_TYPE fnt, 00429 uint32_t flags, 00430 const char *patterns, 00431 GWEN_BUFFER *pathBuffer, 00432 uint32_t guiid) { 00433 FXString sCaption; 00434 FXString sPatterns; 00435 FXString sPath; 00436 FXString str; 00437 FXWindow *owner; 00438 00439 if (caption) 00440 sCaption=FXString(caption); 00441 00442 if (patterns) { 00443 const char *s1; 00444 const char *s2; 00445 00446 s1=patterns; 00447 while(s1 && *s1) { 00448 s2=strchr(s1, '\t'); 00449 if (s2) { 00450 str=FXString(s1, s2-s1); 00451 /* skip tab */ 00452 s2++; 00453 } 00454 else { 00455 str=FXString(s1); 00456 s2=NULL; 00457 } 00458 00459 if (str.contains('(')) { 00460 if (!sPatterns.empty()) 00461 sPatterns+='\n'; 00462 sPatterns+=str.before('('); 00463 str=str.after('('); 00464 sPatterns+='('; 00465 sPatterns+=str.substitute(';', ','); 00466 } 00467 else { 00468 if (!sPatterns.empty()) 00469 sPatterns+='\n'; 00470 sPatterns+=str.substitute(';', ','); 00471 } 00472 00473 s1=s2; 00474 } 00475 } 00476 00477 if (GWEN_Buffer_GetUsedBytes(pathBuffer)) 00478 sPath=FXString(GWEN_Buffer_GetStart(pathBuffer)); 00479 00480 owner=m_app->getModalWindow(); 00481 if (owner==NULL) { 00482 owner=m_app->getActiveWindow(); 00483 } 00484 if (owner==NULL) { 00485 owner=m_app->getRootWindow(); 00486 } 00487 if (owner==NULL) { 00488 DBG_ERROR(GWEN_LOGDOMAIN, "Could not determine owner window"); 00489 return GWEN_ERROR_INTERNAL; 00490 } 00491 00492 switch(fnt) { 00493 case GWEN_Gui_FileNameType_OpenFileName: 00494 str=FXFileDialog::getOpenFilename(owner, sCaption, sPath, sPatterns, 0); 00495 break; 00496 00497 case GWEN_Gui_FileNameType_SaveFileName: 00498 str=FXFileDialog::getSaveFilename(owner, sCaption, sPath, sPatterns, 0); 00499 break; 00500 00501 case GWEN_Gui_FileNameType_OpenDirectory: 00502 str=FXFileDialog::getOpenDirectory(owner, sCaption, sPath); 00503 break; 00504 } 00505 00506 if (str.empty()) { 00507 DBG_ERROR(GWEN_LOGDOMAIN, "Empty filename returned."); 00508 return GWEN_ERROR_ABORTED; 00509 } 00510 else { 00511 GWEN_Buffer_Reset(pathBuffer); 00512 GWEN_Buffer_AppendString(pathBuffer, str.text()); 00513 return 0; 00514 } 00515 } 00516 00517 00518 00519 HTML_FONT *FOX16_Gui::findFont(const char *fontName, 00520 int fontSize, 00521 uint32_t fontFlags) { 00522 HTML_FONT *fnt; 00523 00524 assert(m_fontList); 00525 fnt=HtmlFont_List_First(m_fontList); 00526 while(fnt) { 00527 const char *s; 00528 00529 s=HtmlFont_GetFontName(fnt); 00530 if (s && *s && 00531 HtmlFont_GetFontSize(fnt)==fontSize && 00532 HtmlFont_GetFontFlags(fnt)==fontFlags && 00533 strcasecmp(s, fontName)==0) 00534 break; 00535 fnt=HtmlFont_List_Next(fnt); 00536 } 00537 00538 return fnt; 00539 } 00540 00541 00542 00543 HTML_FONT *FOX16_Gui::getFont(const char *fontName, 00544 int fontSize, 00545 uint32_t fontFlags) { 00546 HTML_FONT *fnt; 00547 00548 fnt=findFont(fontName, fontSize, fontFlags); 00549 if (fnt) 00550 return fnt; 00551 else { 00552 fnt=HtmlFont_new(); 00553 HtmlFont_SetFontName(fnt, fontName); 00554 HtmlFont_SetFontSize(fnt, fontSize); 00555 HtmlFont_SetFontFlags(fnt, fontFlags); 00556 HtmlFont_List_Add(fnt, m_fontList); 00557 return fnt; 00558 } 00559 } 00560 00561 00562 00563 00564