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