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