]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add an example of dialogs in the widget demo program
authorMikko Rasa <tdb@tdb.fi>
Mon, 21 Aug 2023 14:35:13 +0000 (17:35 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 21 Aug 2023 14:37:22 +0000 (17:37 +0300)
basic.skin
examples/widgetdemo/dialogdemo.cpp [new file with mode: 0644]
examples/widgetdemo/dialogdemo.h [new file with mode: 0644]
examples/widgetdemo/widgetdemo.cpp

index 73f60c410fc2d043754b669db4c43c3a09964e2b..5f9f0b10d7614ce326fc715c72669f9e9b79bb05 100644 (file)
@@ -404,6 +404,16 @@ style "panel"
        part "children";
 };
 
+style "dialog"
+{
+       part
+       {
+               graphic NORMAL "grey_beveled";
+       };
+
+       part "children";
+};
+
 style "root"
 {
        part "children";
diff --git a/examples/widgetdemo/dialogdemo.cpp b/examples/widgetdemo/dialogdemo.cpp
new file mode 100644 (file)
index 0000000..63fb68e
--- /dev/null
@@ -0,0 +1,71 @@
+#include <msp/gltk/button.h>
+#include <msp/gltk/dialog.h>
+#include <msp/gltk/entry.h>
+#include <msp/gltk/root.h>
+#include "dialogdemo.h"
+
+using namespace std;
+using namespace Msp;
+
+class PromptDialog: public GLtk::Dialog
+{
+public:
+       sigc::signal<void, string> signal_text_response;
+
+private:
+       GLtk::Entry ent_text;
+
+public:
+       PromptDialog();
+
+protected:
+       void on_response(int) override;
+};
+
+
+DialogDemo::DialogDemo()
+{
+       get_or_create_layout();
+
+       GLtk::Button *btn_prompt = new GLtk::Button("Enter some text");
+       add(*btn_prompt);
+       btn_prompt->signal_clicked.connect(sigc::mem_fun(this, &DialogDemo::button_clicked));
+
+       add(lbl_text);
+       layout->add_constraint(lbl_text, GLtk::Layout::BELOW, *btn_prompt);
+       layout->set_expand(lbl_text, true, false);
+}
+
+void DialogDemo::button_clicked()
+{
+       GLtk::Root *root = find_ancestor<GLtk::Root>();
+       PromptDialog *dialog = GLtk::AutoDialog<PromptDialog>::create();
+       root->add(*dialog);
+       root->get_or_create_layout().set_gravity(*dialog, 0, 0);
+       dialog->signal_text_response.connect(sigc::mem_fun(this, &DialogDemo::got_response));
+}
+
+void DialogDemo::got_response(const string &text)
+{
+       lbl_text.set_text(text);
+}
+
+
+PromptDialog::PromptDialog()
+{
+       get_or_create_layout();
+
+       ent_text.set_edit_size(50, 1);
+       add(ent_text);
+       layout->set_expand(ent_text, true, false);
+
+       GLtk::Button *btn_ok = new GLtk::Button("OK");
+       add_button(*btn_ok, 1);
+       layout->add_constraint(*btn_ok, GLtk::Layout::FAR_BELOW, ent_text);
+       layout->set_gravity(*btn_ok, 1, -1);
+}
+
+void PromptDialog::on_response(int)
+{
+       signal_text_response.emit(ent_text.get_text());
+}
diff --git a/examples/widgetdemo/dialogdemo.h b/examples/widgetdemo/dialogdemo.h
new file mode 100644 (file)
index 0000000..4e5f770
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef DIALOGDEMO_H_
+#define DIALOGDEMO_H_
+
+#include <msp/gltk/label.h>
+#include <msp/gltk/panel.h>
+
+class DialogDemo: public Msp::GLtk::Panel
+{
+private:
+       Msp::GLtk::Label lbl_text;
+
+public:
+       DialogDemo();
+
+private:
+       void button_clicked();
+       void got_response(const std::string &);
+};
+
+#endif
index 52545a2b2b86c1f099a2a3bc96ff4cbb43d801cd..b1482e72ae93f10c9652f5cadacdc954e6afbaae 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/gl/framebuffer.h>
 #include <msp/gltk/layout.h>
 #include "buttondemo.h"
+#include "dialogdemo.h"
 #include "dropdowndemo.h"
 #include "entrydemo.h"
 #include "toggledemo.h"
@@ -25,6 +26,7 @@ WidgetDemo::WidgetDemo(int, char **):
        root_layout.set_expand(selector, false, true);
 
        add_demo("Button", new ButtonDemo);
+       add_demo("Dialog", new DialogDemo);
        add_demo("Dropdown", new DropdownDemo);
        add_demo("Entry", new EntryDemo);
        add_demo("Toggle", new ToggleDemo);