]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ButtonController.h
Modification to ButtonController, so that read-only list of widgets is
[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 template <class Button, class Widget>
23 class GuiBC : public ButtonControllerBase
24 {
25 public:
26         ///
27         GuiBC(string const & cancel, string const & close);
28
29         /// 
30         void setOK(Button * obj) { okay_ = obj; }
31         /// 
32         void setApply(Button * obj) { apply_ = obj; }
33         /// 
34         void setCancel(Button * obj) { cancel_ = obj; }
35         ///
36         void setRestore(Button * obj) { restore_ = obj; }
37         ///
38         void addReadOnly(Widget * obj) { read_only_.push_back(obj); }
39         ///
40         void eraseReadOnly() { read_only_.clear(); }
41
42         /// Refresh the status of the Ok, Apply, Restore, Cancel buttons.
43         void refresh();
44         /// Refresh the status of any widgets in the read_only list
45         void refreshReadOnly();
46
47 private:
48         /// Enable/Disable a widget
49         virtual void setWidgetEnabled(Widget * obj, bool enable) = 0;
50         /// Enable/Disable a button
51         virtual void setButtonEnabled(Button * obj, bool enable) = 0;
52         /// Set the Label on the button
53         virtual void setButtonLabel(Button * obj, string const & label) = 0;
54
55         Button * okay_;
56         Button * apply_;
57         Button * cancel_;
58         Button * restore_;
59         
60         typedef std::list<Widget *> Widgets;
61         Widgets read_only_;
62 };
63
64
65 template <class Button, class Widget>
66 GuiBC<Button, Widget>::GuiBC(string const & cancel, string const & close)
67         : ButtonControllerBase(cancel, close),
68           okay_(0), apply_(0), cancel_(0), restore_(0)
69 {}
70
71
72 template <class Button, class Widget>
73 void GuiBC<Button, Widget>::refresh()
74 {
75         if (okay_) {
76                 bool const enabled = bp().buttonStatus(ButtonPolicy::OKAY);
77                 setButtonEnabled(okay_, enabled);
78         }
79         if (apply_) {
80                 bool const enabled = bp().buttonStatus(ButtonPolicy::APPLY);
81                 setButtonEnabled(apply_, enabled);
82         }
83         if (restore_) {
84                 bool const enabled = bp().buttonStatus(ButtonPolicy::RESTORE);
85                 setButtonEnabled(restore_, enabled);
86         }
87         if (cancel_) {
88                 bool const enabled = bp().buttonStatus(ButtonPolicy::CANCEL);
89                 if (enabled)
90                         setButtonLabel(cancel_, cancel_label_);
91                 else
92                         setButtonLabel(cancel_, close_label_);
93         }
94 }
95
96
97 template <class Button, class Widget>
98 void GuiBC<Button, Widget>::refreshReadOnly()
99 {
100         if (read_only_.empty()) return;
101
102         bool const enable = !bp().isReadOnly();
103
104         Widgets::const_iterator end = read_only_.end();
105         Widgets::const_iterator iter = read_only_.begin();
106         for (; iter != end; ++iter) {
107                 setWidgetEnabled(*iter, enable);
108         }
109 }
110
111
112 template <class BP, class GUIBC>
113 class ButtonController: public GUIBC
114 {
115 public:
116         ///
117         ButtonController(string const & = _("Cancel"),
118                          string const & = _("Close"));
119         ///
120         virtual ButtonPolicy & bp() { return bp_; }
121
122 protected:
123         ///
124         BP bp_;
125 };
126
127
128 template <class BP, class GUIBC>
129 ButtonController<BP, GUIBC>::ButtonController(string const & cancel,
130                                               string const & close)
131         : GUIBC(cancel, close)
132 {}
133
134 #endif // BUTTONCONTROLLER_H