]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ButtonController.h
* Baruch's GuiBC template.
[lyx.git] / src / frontends / controllers / ButtonController.h
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000-2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file ButtonController.h
11  * \author Allan Rae, rae@lyx.org
12  * \author Angus Leeming, a.leeming@ic.ac.uk
13  * \author Baruch Even, baruch.even@writeme.com
14  */
15
16 #ifndef BUTTONCONTROLLER_H
17 #define BUTTONCONTROLLER_H
18
19 #include <list>
20
21 template <class Button, class Widget>
22 class GuiBC : public ButtonControllerBase
23 {
24 public:
25         ///
26         GuiBC(string const & cancel, string const & close);
27
28         /// 
29         void setOK(Button * obj) { okay_ = obj; }
30         /// 
31         void setApply(Button * obj) { apply_ = obj; }
32         /// 
33         void setCancel(Button * obj) { cancel_ = obj; }
34         ///
35         void setUndoAll(Button * obj) { undo_all_ = obj; }
36         ///
37         void addReadOnly(Widget * obj) { read_only_.push_back(obj); }
38         ///
39         void eraseReadOnly() { read_only_.clear(); }
40
41         /// Refresh the widgets status.
42         void refresh();
43
44 private:
45         /// Enable/Disable a widget
46         virtual void setWidgetEnabled(Widget * obj, bool enable) = 0;
47         /// Enable/Disable a button
48         virtual void setButtonEnabled(Button * obj, bool enable) = 0;
49         /// Set the Label on the button
50         virtual void setButtonLabel(Button * obj, string const & label) = 0;
51
52         Button * okay_;
53         Button * apply_;
54         Button * undo_all_;
55         Button * cancel_;
56         
57         typedef std::list<Widget *> Widgets;
58         Widgets read_only_;
59 };
60
61
62 template <class Button, class Widget>
63 GuiBC<Button, Widget>::GuiBC(string const & cancel, string const & close)
64         : ButtonControllerBase(cancel, close)
65         , okay_(0), apply_(0), cancel_(0), undo_all_(0)
66 {}
67
68
69 template <class Button, class Widget>
70 void GuiBC<Button, Widget>::refresh()
71 {
72         if (okay_) {
73                 bool const enabled = bp().buttonStatus(ButtonPolicy::OKAY);
74                 setButtonEnabled(okay_, enabled);
75         }
76         if (apply_) {
77                 bool const enabled = bp().buttonStatus(ButtonPolicy::APPLY);
78                 setButtonEnabled(apply_, enabled);
79         }
80         if (undo_all_) {
81                 bool const enabled = bp().buttonStatus(ButtonPolicy::UNDO_ALL);
82                 setButtonEnabled(undo_all_, enabled);
83         }
84         if (cancel_) {
85                 bool const enabled = bp().buttonStatus(ButtonPolicy::CANCEL);
86                 if (enabled)
87                         setButtonLabel(cancel_, cancel_label_);
88                 else
89                         setButtonLabel(cancel_, close_label_);
90         }
91
92         // Enable/Disable read-only handled widgets.
93         if (!read_only_.empty()) {
94                 bool const enable = !bp().isReadOnly();
95
96                 Widgets::const_iterator end = read_only_.end();
97                 Widgets::const_iterator iter = read_only_.begin();
98                 for (; iter != end; ++iter) {
99                         setWidgetEnabled(*iter, enable);
100                 }
101         }
102
103 }
104
105
106 template <class BP, class GUIBC>
107 class ButtonController: public GUIBC
108 {
109 public:
110         ///
111         ButtonController(string const & = _("Cancel"),
112                          string const & = _("Close"));
113         ///
114         virtual ButtonPolicy & bp() { return bp_; }
115
116 protected:
117         ///
118         BP bp_;
119 };
120
121
122 template <class BP, class GUIBC>
123 ButtonController<BP, GUIBC>::ButtonController(string const & cancel,
124                                               string const & close)
125         : GUIBC(cancel, close)
126 {}
127
128 #endif // BUTTONCONTROLLER_H