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