@@ -266,7 +266,7 @@ static int inputHandler(Id id, void *instance) {
266266 return ErrorCode;
267267 }
268268
269- Context *ctx = static_cast <Context *>(instance);
269+ auto *ctx = static_cast <Context *>(instance);
270270 ctx->mFocus = Widgets::findWidget (id, ctx->mRoot );
271271
272272 return ResultOk;
@@ -399,8 +399,7 @@ ret_code Widgets::panel(Id id, Id parentId, const char *title, const Rect &rect,
399399 return InvalidRenderHandle;
400400 }
401401
402- Widget *child = createWidget (ctx, id, parentId, rect, WidgetType::Panel);
403- if (child == nullptr ) {
402+ if (const Widget *child = createWidget (ctx, id, parentId, rect, WidgetType::Panel); child == nullptr ) {
404403 return ErrorCode;
405404 }
406405
@@ -412,14 +411,17 @@ static int onTreeViewItemClicked(Id id, void *data) {
412411 if (treeView == nullptr ) {
413412 return ErrorCode;
414413 }
414+
415415 for (size_t i = 0 ; i < treeView->mChildren .size (); ++i ) {
416416 Widget *child = treeView->mChildren [i];
417417 if (child == nullptr ) {
418418 continue ;
419419 }
420420 child->mEnabled = !child->mEnabled ;
421421 }
422+
422423 std::cout << " TreeView item clicked: " << id << std::endl;
424+
423425 return 0 ;
424426}
425427
@@ -432,7 +434,7 @@ ret_code Widgets::treeView(Id id, Id parentId, const char *title, const Rect &re
432434 if (ctx.mRoot == nullptr ) {
433435 return InvalidRenderHandle;
434436 }
435-
437+
436438 Widget *widget = createWidget (ctx, id, parentId, rect, WidgetType::TreeView);
437439 if (widget == nullptr ) {
438440 return ErrorCode;
@@ -461,12 +463,12 @@ ret_code Widgets::treeItem(Id id, Id parentItemId, const char *text) {
461463 return InvalidRenderHandle;
462464 }
463465
464- Widget *parentWidget = findWidget (parentItemId, ctx.mRoot );
466+ const Widget *parentWidget = findWidget (parentItemId, ctx.mRoot );
465467 if (parentWidget == nullptr ) {
466468 return ErrorCode;
467469 }
468470
469- auto &parentRect = parentWidget->mRect ;
471+ const auto &parentRect = parentWidget->mRect ;
470472
471473 const int32_t margin = ctx.mStyle .mMargin ;
472474 const int32_t w = parentRect.width ;
@@ -579,7 +581,7 @@ static void render(Context &ctx, const Widget *currentWidget) {
579581 if (payload == nullptr ) {
580582 break ;
581583 }
582- FilledState *state = reinterpret_cast <FilledState *>(payload->payload );
584+ const auto *state = reinterpret_cast <FilledState *>(payload->payload );
583585 if (state == nullptr ) {
584586 break ;
585587 }
@@ -771,7 +773,7 @@ void Widgets::setEnableState(Id id, bool enabled) {
771773
772774bool Widgets::isEnabled (Id id) {
773775 auto &ctx = TinyUi::getContext ();
774- Widget *widget = findWidget (id, ctx.mRoot );
776+ const Widget *widget = findWidget (id, ctx.mRoot );
775777 if (widget != nullptr ) {
776778 return widget->isEnabled ();
777779 }
@@ -806,4 +808,90 @@ bool Widgets::endChild() {
806808 return true ;
807809}
808810
811+ static constexpr size_t BufferSize = 1024 ;
812+
813+ ret_code Widgets::getOpenFileDialog (const char *title, const char *extensions, std::string &filename) {
814+ filename.clear ();
815+ #ifdef TINYUI_WINDOWS
816+ // Init data
817+ char szFile[BufferSize] = { ' \0 ' };
818+ OPENFILENAME ofn;
819+ memset (&ofn, 0 , sizeof (ofn));
820+ ofn.lStructSize = sizeof (ofn);
821+ ofn.lpstrFile = szFile;
822+
823+ // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
824+ // use the contents of szFile to initialize itself.
825+ ofn.lpstrTitle = title;
826+ ofn.lpstrFile [0 ] = ' \0 ' ;
827+ ofn.nMaxFile = sizeof (szFile);
828+ ofn.lpstrFilter = extensions;
829+ ofn.nFilterIndex = 1 ;
830+ ofn.lpstrFileTitle = nullptr ;
831+ ofn.nMaxFileTitle = 0 ;
832+ ofn.lpstrInitialDir = nullptr ;
833+ ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
834+
835+ // Display the Open dialog box.
836+ if (::GetOpenFileName (&ofn) == TRUE ) {
837+ filename = ofn.lpstrFile ;
838+ } else {
839+ return OpCancelled;
840+ }
841+ #else
842+ char buffer[BufferSize] = { ' \0 ' };
843+ FILE *f = popen (" zenity --file-selection" , " r" );
844+ if (f == nullptr ) {
845+ return OpCancelled;
846+ }
847+ fgets (buffer, BufferSize, f);
848+ filename = buffer;
849+ #endif // TINYUI_WINDOWS
850+
851+ return ResultOk;
852+ }
853+
854+ ret_code Widgets::getSaveFileDialog (const char *title, const char *extensions, std::string &filename) {
855+ filename.clear ();
856+
857+ #ifdef TINYUI_WINDOWS
858+ char szFile[BufferSize] = { ' \0 ' };
859+ // Initialize OPENFILENAME
860+ OPENFILENAME ofn;
861+ memset (&ofn, 0 , sizeof (ofn));
862+ ofn.lStructSize = sizeof (ofn);
863+ ofn.lpstrFile = szFile;
864+
865+ // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
866+ // use the contents of szFile to initialize itself.
867+ ofn.lpstrTitle = title;
868+ ofn.lpstrFile [0 ] = ' \0 ' ;
869+ ofn.nMaxFile = sizeof (szFile);
870+ ofn.lpstrFilter = extensions;
871+ ofn.nFilterIndex = 1 ;
872+ ofn.lpstrFileTitle = nullptr ;
873+ ofn.nMaxFileTitle = 0 ;
874+ ofn.lpstrInitialDir = nullptr ;
875+ ofn.Flags = OFN_PATHMUSTEXIST;
876+
877+ // Display the Open dialog box.
878+ if (TRUE == GetSaveFileName (&ofn)) {
879+ filename = ofn.lpstrFile ;
880+ } else {
881+ return OpCancelled;
882+ }
883+ #else
884+ FILE *f = popen (" zenity --file-selection" , " w" );
885+ if (f == nullptr ) {
886+ return OpCancelled;
887+ }
888+ char buffer[BufferSize] = { ' \0 ' };
889+ fgets (buffer, BufferSize, f);
890+
891+ filename = buffer;
892+ #endif // TINYUI_WINDOWS
893+
894+ return ResultOk;
895+ }
896+
809897} // namespace tinyui
0 commit comments