]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ButtonController.h
inherit privately from noncopyable, set c++ mode, swap init order
[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 widgets status.
43         void refresh();
44
45 private:
46         /// Enable/Disable a widget
47         virtual void setWidgetEnabled(Widget * obj, bool enable) = 0;
48         /// Enable/Disable a button
49         virtual void setButtonEnabled(Button * obj, bool enable) = 0;
50         /// Set the Label on the button
51         virtual void setButtonLabel(Button * obj, string const & label) = 0;
52
53         Button * okay_;
54         Button * apply_;
55         Button * cancel_;
56         Button * restore_;
57         
58         typedef std::list<Widget *> Widgets;
59         Widgets read_only_;
60 };
61
62
63 template <class Button, class Widget>
64 GuiBC<Button, Widget>::GuiBC(string const & cancel, string const & close)
65         : ButtonControllerBase(cancel, close),
66           okay_(0), apply_(0), cancel_(0), restore_(0)
67 {}
68
69
70 template <class Button, class Widget>
71 void GuiBC<Button, Widget>::refresh()
72 {
73         if (okay_) {
74                 bool const enabled = bp().buttonStatus(ButtonPolicy::OKAY);
75                 setButtonEnabled(okay_, enabled);
76         }
77         if (apply_) {
78                 bool const enabled = bp().buttonStatus(ButtonPolicy::APPLY);
79                 setButtonEnabled(apply_, enabled);
80         }
81         if (restore_) {
82                 bool const enabled = bp().buttonStatus(ButtonPolicy::RESTORE);
83                 setButtonEnabled(restore_, enabled);
84         }
85         if (cancel_) {
86                 bool const enabled = bp().buttonStatus(ButtonPolicy::CANCEL);
87                 if (enabled)
88                         setButtonLabel(cancel_, cancel_label_);
89                 else
90                         setButtonLabel(cancel_, close_label_);
91         }
92
93         // Enable/Disable read-only handled widgets.
94         if (!read_only_.empty()) {
95                 bool const enable = !bp().isReadOnly();
96
97                 Widgets::const_iterator end = read_only_.end();
98                 Widgets::const_iterator iter = read_only_.begin();
99                 for (; iter != end; ++iter) {
100                         setWidgetEnabled(*iter, enable);
101                 }
102         }
103
104 }
105
106
107 template <class BP, class GUIBC>
108 class ButtonController: public GUIBC
109 {
110 public:
111         ///
112         ButtonController(string const & = _("Cancel"),
113                          string const & = _("Close"));
114         ///
115         virtual ButtonPolicy & bp() { return bp_; }
116
117 protected:
118         ///
119         BP bp_;
120 };
121
122
123 template <class BP, class GUIBC>
124 ButtonController<BP, GUIBC>::ButtonController(string const & cancel,
125                                               string const & close)
126         : GUIBC(cancel, close)
127 {}
128
129 #endif // BUTTONCONTROLLER_H