diff --git a/src/gui/mainwindow.cc b/src/gui/mainwindow.cc index 34983f93..15d979c3 100644 --- a/src/gui/mainwindow.cc +++ b/src/gui/mainwindow.cc @@ -34,14 +34,34 @@ public: ->setChild( make() ->add(make() - ->add(make>()->setOptions(configs)) - ->add(_busyButton = make("busy button")) - ->add(make("press me!")) + ->add(make>() + ->setOptions(configs) + ->setBoxParams({ .expand = true })) + ->add(make() + ->setOptions({ + "drive:0", + "drive:1", + }) + ->setBoxParams({ .expand = true })) + ) + ->add(make() + ->add(make("Load image")->setGridParams({ .x = 0 })) + ->add(make("Write disk")->setGridParams({ .x = 1 })) + ->add(make() + ->add(make("Cylinders:")) + ->add(make()) + ->add(make("Heads:")) + ->add(make()) + ->add(make("HD")) + ->setGridParams({ .x = 2, .hexpand = true, .vexpand = true })) + ->add(make("Read disk")->setGridParams({ .x = 3 })) + ->add(make("Save image")->setGridParams({ .x = 4 })) ) ->add(make() ->setOnDraw(_onredraw_cb) - ->setStretchy(true) - ->disable()) + ->disable() + ->setBoxParams({ .expand = true })) + ->add(_busyButton = make("busy button")) ->add(make("press me again!")) ); diff --git a/src/gui/uipp.h b/src/gui/uipp.h index 020c39a7..e001b679 100644 --- a/src/gui/uipp.h +++ b/src/gui/uipp.h @@ -1,6 +1,23 @@ #ifndef UIPP_H #define UIPP_H +struct UIGridParams +{ + int x = 0; + int y = 0; + int xspan = 1; + int yspan = 1; + bool hexpand = false; + uiAlign halign = uiAlignFill; + bool vexpand = false; + uiAlign valign = uiAlignFill; +}; + +struct UIBoxParams +{ + bool expand = false; +}; + class Closeable { public: @@ -47,29 +64,46 @@ public: return _control; } - bool stretchy() const - { - return _stretchy; - } - bool built() const { return !!_control; } public: - UIControl* setStretchy(bool stretchy) { _stretchy = stretchy; return this; } - UIControl* show() { uiControlShow(claim()); return this; } UIControl* hide() { uiControlHide(control()); return this; } UIControl* enable() { uiControlEnable(control()); return this; } UIControl* disable() { uiControlDisable(control()); return this; } +public: + UIGridParams& getGridParams() { + if (!_gridParams) + _gridParams = UIGridParams(); + return *_gridParams; + } + + UIBoxParams& getBoxParams() { + if (!_boxParams) + _boxParams = UIBoxParams(); + return *_boxParams; + } + + UIControl* setGridParams(UIGridParams params) { + _gridParams = params; + return this; + } + + UIControl* setBoxParams(UIBoxParams params) { + _boxParams = params; + return this; + } + private: uiControl* _control; bool _owned = true; - bool _stretchy = false; bool _enabled = true; + std::optional _gridParams; + std::optional _boxParams; }; template @@ -86,34 +120,20 @@ public: } }; -template -class UIContainerControl : public UITypedControl +template +class UIBox : public UITypedControl { public: - UIContainerControl(T* control): - UITypedControl(control) + UIBox(uiBox* control): + UITypedControl(control) {} B* add(UIControl* child) { - uiBoxAppend(this->typedControl(), child->claim(), child->stretchy()); - _children.push_back(child); + const auto& params = child->getBoxParams(); + uiBoxAppend(this->typedControl(), child->claim(), params.expand); return (B*) this; } - - const std::vector& children() const { return _children; } - -private: - std::vector _children; -}; - -template -class UIBox : public UIContainerControl -{ -public: - UIBox(uiBox* control): - UIContainerControl(control) - {} }; class UIHBox : public UIBox @@ -132,6 +152,49 @@ public: {} }; +class UIGrid : public UITypedControl +{ +public: + UIGrid(): + UITypedControl(uiNewGrid()) + {} + + UIGrid* add(UIControl* child) + { + const auto& params = child->getGridParams(); + uiGridAppend(this->typedControl(), child->claim(), + params.x, params.y, + params.xspan, params.yspan, + params.hexpand, params.halign, + params.vexpand, params.valign); + return this; + } +}; + +class UILabel : public UITypedControl +{ +public: + UILabel(const std::string& text): + UITypedControl(uiNewLabel(text.c_str())) + {} +}; + +class UITextEntry : public UITypedControl +{ +public: + UITextEntry(): + UITypedControl(uiNewEntry()) + {} +}; + +class UICheckBox : public UITypedControl +{ +public: + UICheckBox(const std::string& text): + UITypedControl(uiNewCheckbox(text.c_str())) + {} +}; + class UIArea : public UITypedControl { public: @@ -290,16 +353,16 @@ private: }; template -class UICombo : public UITypedControl> +class UISelect : public UITypedControl> { public: - UICombo(): - UITypedControl>(uiNewCombobox()) + UISelect(): + UITypedControl>(uiNewCombobox()) { uiComboboxOnSelected(this->typedControl(), _selected_cb, this); } - UICombo* setOptions(const std::vector>& options) + UISelect* setOptions(const std::vector>& options) { assert(_forwards.empty()); int count = 0; @@ -313,7 +376,7 @@ public: return this; } - UICombo* select(T item) + UISelect* select(T item) { auto it = _backwards.find(item); if (it != _backwards.end()) @@ -333,12 +396,12 @@ public: private: static void _selected_cb(uiCombobox*, void* p) { - auto combo = ((UICombo*)p); - if (combo->_onselected) + auto c = ((UISelect*)p); + if (c->_onselected) { - auto value = combo->getSelected(); + auto value = c->getSelected(); if (value) - combo->_onselected(*value); + c->_onselected(*value); } } @@ -348,5 +411,46 @@ private: std::function _onselected; }; +class UICombo : public UITypedControl +{ +public: + UICombo(): + UITypedControl(uiNewEditableCombobox()) + { + uiEditableComboboxOnChanged(this->typedControl(), _changed_cb, this); + } + + UICombo* setOptions(const std::vector& options) + { + for (auto& p : options) + uiEditableComboboxAppend(this->typedControl(), p.c_str()); + return this; + } + + UICombo* setText(const std::string& text) + { + uiEditableComboboxSetText(this->typedControl(), text.c_str()); + return this; + } + + std::string getText() + { + return uiEditableComboboxText(this->typedControl()); + } + +private: + static void _changed_cb(uiEditableCombobox*, void* p) + { + auto c = ((UICombo*)p); + if (c->_onchanged) + { + auto value = c->getText(); + c->_onchanged(value); + } + } + +private: + std::function _onchanged; +}; #endif