]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/BCView.h
Overhaul the branches code.
[lyx.git] / src / frontends / controllers / BCView.h
1 // -*- C++ -*-
2 /**
3  * \file BCView.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Allan Rae
8  * \author Angus Leeming
9  * \author Baruch Even
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef BCVIEW_H
15 #define BCVIEW_H
16
17 #include <boost/shared_ptr.hpp>
18 #include <list>
19 #include <string>
20
21
22 class ButtonController;
23 class ButtonPolicy;
24
25
26 /** \c CheckedWidget is an abstract base class that can be stored
27  *  in the button controller's view and can be interrogated by it
28  *  when the activation state of the Ok, Apply buttons is refreshed.
29  *  Ideally, the user will be prevented from returning invalid data
30  *  to the LyX kernel.
31  *
32  *  Many widgets can be grouped together in the derived class if they
33  *  make a logical whole. E.g., an input and a choice widget that together
34  *  are used to set a LyXLength can be interrogated together.
35  */
36 struct CheckedWidget {
37         ///
38         virtual ~CheckedWidget();
39
40         /** Returns true if the widget is in a valid state.
41         *  Might also change the visual appearance of the widget,
42         *  to reflect this state.
43         */
44         virtual bool check() const = 0;
45 };
46
47
48 /** \c BCView is the View to ButtonController's Controller. It
49  *  stores the individual GUI widgets and sets their activation state
50  *  upon receipt of instructions from the controller.
51  *
52  *  It is a base class. The true, GUI, instantiations derive from it.
53  */
54 class BCView {
55 public:
56         BCView(ButtonController const &);
57         virtual ~BCView() {}
58
59         //@{
60         /// Refresh the status of the Ok, Apply, Restore, Cancel buttons.
61         virtual void refresh() const = 0;
62         /// Refresh the status of any widgets in the read_only list
63         virtual void refreshReadOnly() const = 0;
64         //@}
65
66         /// A shortcut to the BP of the BC.
67         ButtonPolicy & bp() const;
68
69         /** Add a widget to the list of all widgets whose validity should
70          *  be checked explicitly when the buttons are refreshed.
71          */
72         void addCheckedWidget(CheckedWidget * ptr);
73
74 protected:
75         /// \return true if all CheckedWidgets are in a valid state.
76         bool checkWidgets() const;
77
78 private:
79         typedef boost::shared_ptr<CheckedWidget> checked_widget_ptr;
80         typedef std::list<checked_widget_ptr> checked_widget_list;
81         checked_widget_list checked_widgets;
82         ButtonController const & parent;
83 };
84
85
86 /** A templatised instantiation of the ButtonController's View requiring the
87  *  gui-frontend widgets.
88  */
89 template <class Button, class Widget>
90 class GuiBC : public BCView {
91 public:
92         ///
93         GuiBC(ButtonController const & parent,
94               std::string const & cancel, std::string const & close);
95
96         //@{
97         /** Store pointers to these widgets. The pointers are _not_
98          *  owned by GuiBC.
99          */
100         void setOK(Button * obj) { okay_ = obj; }
101         void setApply(Button * obj) { apply_ = obj; }
102         void setCancel(Button * obj) { cancel_ = obj; }
103         void setRestore(Button * obj) { restore_ = obj; }
104         //@}
105
106         /** Add a pointer to the list of widgets whose activation
107          *  state is dependent upon the read-only status of the
108          *  underlying buffer.
109          */
110         void addReadOnly(Widget * obj) { read_only_.push_back(obj); }
111
112         /// Refresh the status of the Ok, Apply, Restore, Cancel buttons.
113         virtual void refresh() const;
114         /// Refresh the status of any widgets in the read_only list
115         virtual void refreshReadOnly() const;
116
117 private:
118         /// Enable/Disable a widget
119         virtual void setWidgetEnabled(Widget * obj, bool enable) const = 0;
120         /// Enable/Disable a button
121         virtual void setButtonEnabled(Button * obj, bool enable) const = 0;
122         /// Set the Label on the button
123         virtual void setButtonLabel(Button * obj, std::string const & label) const = 0;
124
125         std::string const cancel_label_;
126         std::string const close_label_;
127
128         Button * okay_;
129         Button * apply_;
130         Button * cancel_;
131         Button * restore_;
132
133         typedef std::list<Widget *> Widgets;
134         Widgets read_only_;
135 };
136
137
138 #include "BCView.tmpl"
139
140 #endif // BCVIEW_H