]> git.tdb.fi Git - libs/gltk.git/blob - source/dialog.h
cc5ed41d1cfa6a4d967b376a71114ec6f8636b59
[libs/gltk.git] / source / dialog.h
1 #ifndef MSP_GLTK_DIALOG_H_
2 #define MSP_GLTK_DIALOG_H_
3
4 #include "mspgltk_api.h"
5 #include "panel.h"
6
7 namespace Msp {
8 namespace GLtk {
9
10 class Button;
11
12 /**
13 A Dialog is used for temporary interaction with the user.  When any of the
14 Dialog's action buttons are clicked, it will emit a signal and hide itself.
15 */
16 class MSPGLTK_API Dialog: public Panel
17 {
18 public:
19         class MSPGLTK_API Loader: public DataFile::DerivedObjectLoader<Dialog, Panel::Loader>
20         {
21         public:
22                 Loader(Dialog &, WidgetMap &);
23
24         private:
25                 void action_button(const std::string &, int);
26         };
27
28         sigc::signal<void, int> signal_response;
29
30         const char *get_class() const override { return "dialog"; }
31
32         /** Adds an action button to the dialog.  Pressing the button will invoke
33         response handlers and delete the dialog. */
34         void add_button(Button &, int);
35
36         /** Sets the modality of the dialog.  When modal, the user can't navigate
37         away from the dialog. */
38         void set_modal(bool);
39
40 protected:
41         void response(int);
42
43         /** Called when an action button is pressed. */
44         virtual void on_response(int) { }
45 };
46
47
48 /**
49 A dialog which automatically deletes itself after being responded to.
50 */
51 template<typename D>
52 class MSPGLTK_API AutoDialog: public D
53 {
54 private:
55         struct StaleChecker
56         {
57                 AutoDialog *dialog;
58
59                 StaleChecker(AutoDialog &d): dialog(&d) { }
60                 ~StaleChecker() { if(dialog->stale) delete dialog; }
61         };
62
63         bool stale = false;
64
65         template<typename... Args>
66         AutoDialog(Args &&... args): D(std::forward<Args>(args)...) { }
67
68 public:
69         // Ensure that AutoDialog is always created with new.
70         template<typename... Args>
71         static AutoDialog *create(Args &&... args) { return new AutoDialog(std::forward<Args>(args)...); }
72
73         void button_release(int x, int y, unsigned b) override { StaleChecker sc(*this); D::button_release(x, y, b); }
74         bool key_release(unsigned k, unsigned m) override { StaleChecker sc(*this); return D::key_release(k, m); }
75         bool navigate(Navigation n) override { StaleChecker sc(*this); return D::navigate(n); }
76
77 protected:
78         void on_response(int c) override { D::on_response(c); stale = true; }
79 };
80
81 } // namespace GLtk
82 } // namespace Msp
83
84 #endif