]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/ButtonController.h
More pref work from Angus
[lyx.git] / src / frontends / xforms / ButtonController.h
index b6f1086d5aa7b8b75985f50adb50ce3a256ab986..5f4c6462a6cee6697f7936f8a159a99118453e02 100644 (file)
 #define BUTTONCONTROLLER_H
 
 #include "ButtonPolicies.h"
+#include <list>
 
 /** General purpose button controller for up to four buttons.
     Controls the activation of the OK, Apply and Cancel buttons.
     Actually supports 4 buttons in all and it's up to the user to decide on
     the activation policy and which buttons correspond to which output of the
     state machine.
+    @author Allan Rae <rae@lyx.org>
+    20001001 Switch from template implementation to taking Policy parameter.
+             Allows FormBase to provide a ButtonController for any dialog.
 */
-template <class Policy>
 class ButtonController : public noncopyable
 {
 public:
-       /**@name Constructors and Deconstructors */
-       //@{
        /** Constructor.
            The cancel/close label entries are _not_ managed within the class
            thereby allowing you to reassign at will and to use static labels.
@@ -43,131 +44,166 @@ public:
            you can just assign "Cancel" to both labels.  Or even reuse this
            class for something completely different.
         */
-       ButtonController(char const * cancel, char const * close)
-               : bp_(), okay_(0), apply_(0), cancel_(0), undo_all_(0),
-                 cancel_label(cancel), close_label(close) {}
+       ButtonController(ButtonPolicy * bp, char const * cancel, char const * close)
+               : bp_(bp), okay_(0), apply_(0), cancel_(0), undo_all_(0),
+                 read_only_(), cancel_label(cancel), close_label(close) {}
        /// Somebody else owns the FL_OBJECTs we just manipulate them.
        ~ButtonController() {}
-       //@}
 
-       /**@name Initialise Button Functions */
-       //@{
+       /* Initialise Button Functions */
        /// Call refresh() when finished setting the buttons.
-       void setOk(FL_OBJECT * obj)
-               { okay_ = obj; }
+       void setOK(FL_OBJECT * obj) {
+               okay_ = obj;
+       }
        ///
-       void setApply(FL_OBJECT * obj)
-               { apply_ = obj; }
+       void setApply(FL_OBJECT * obj) {
+               apply_ = obj;
+       }
        ///
-       void setCancel(FL_OBJECT * obj)
-               { cancel_ = obj; }
+       void setCancel(FL_OBJECT * obj) {
+               cancel_ = obj;
+       }
        ///
-       void setUndoAll(FL_OBJECT * obj)
-               { undo_all_ = obj; }
+       void setUndoAll(FL_OBJECT * obj) {
+               undo_all_ = obj;
+       }
        ///
-       void setCancelTrueLabel(char const * c)
-               { cancel_label = c; }
+       void setCancelTrueLabel(char const * c) {
+               cancel_label = c;
+       }
        ///
-       void setCancelFalseLabel(char const * c)
-               { close_label = c; }
-       //@}
-
-       /**@name Action Functions */
-       //@{
-       ///
-       void input(ButtonPolicy::SMInput in)
-               {
-                       bp_.input(in);
-                       refresh();
-               }
-       ///
-       void ok()
-               { input(ButtonPolicy::SMI_OKAY); }
-       ///
-       void apply()
-               { input(ButtonPolicy::SMI_APPLY); }
+       void setCancelFalseLabel(char const * c) {
+               close_label = c;
+       }
        ///
-       void cancel()
-               { input(ButtonPolicy::SMI_CANCEL); }
+       void addReadOnly(FL_OBJECT * obj) {
+               read_only_.push_front(obj);
+       }
        ///
-       void undoAll()
-               { input(ButtonPolicy::SMI_UNDO_ALL); }
-       ///
-       void hide()
-               { input(ButtonPolicy::SMI_HIDE); }
-       /// Passthrough function -- returns its input value
-       bool read_only(bool ro = true)
-               {
-                       if (ro) {
-                               input(ButtonPolicy::SMI_READ_ONLY);
+       void eraseReadOnly() {
+               read_only_.erase(read_only_.begin(), read_only_.end());
+       }
+
+       /* Action Functions */
+       /// force a refresh of the buttons
+       void refresh() {
+               if (okay_) {
+                       if (bp_->buttonStatus(ButtonPolicy::OKAY)) {
+                               fl_activate_object(okay_);
+                               fl_set_object_lcol(okay_, FL_BLACK);
                        } else {
-                               input(ButtonPolicy::SMI_READ_WRITE);
+                               fl_deactivate_object(okay_);
+                               fl_set_object_lcol(okay_, FL_INACTIVE);
                        }
-                       return ro;
                }
-       ///
-       void read_write()
-               { read_only(false); }
-       /// Passthrough function -- returns its input value
-       bool valid(bool v = true)
-               { 
-                       if (v) {
-                               input(ButtonPolicy::SMI_VALID);
+               if (apply_) {
+                       if (bp_->buttonStatus(ButtonPolicy::APPLY)) {
+                               fl_activate_object(apply_);
+                               fl_set_object_lcol(apply_, FL_BLACK);
                        } else {
-                               input(ButtonPolicy::SMI_INVALID);
+                               fl_deactivate_object(apply_);
+                               fl_set_object_lcol(apply_, FL_INACTIVE);
                        }
-                       return v;
                }
-       ///
-       void invalid()
-               { valid(false); }
-       /// force a refresh of the buttons
-       void refresh()
-               {
-                       if (okay_) {
-                               if (bp_.buttonStatus(ButtonPolicy::OKAY)) {
-                                       fl_activate_object(okay_);
-                                       fl_set_object_lcol(okay_, FL_BLACK);
-                               } else {
-                                       fl_deactivate_object(okay_);
-                                       fl_set_object_lcol(okay_, FL_INACTIVE);
-                               }
+               if (undo_all_) {
+                       if (bp_->buttonStatus(ButtonPolicy::UNDO_ALL)) {
+                               fl_activate_object(undo_all_);
+                               fl_set_object_lcol(undo_all_, FL_BLACK);
+                       } else {
+                               fl_deactivate_object(undo_all_);
+                               fl_set_object_lcol(undo_all_,
+                                                  FL_INACTIVE);
                        }
-                       if (apply_) {
-                               if (bp_.buttonStatus(ButtonPolicy::APPLY)) {
-                                       fl_activate_object(apply_);
-                                       fl_set_object_lcol(apply_, FL_BLACK);
-                               } else {
-                                       fl_deactivate_object(apply_);
-                                       fl_set_object_lcol(apply_, FL_INACTIVE);
-                               }
+               }
+               if (cancel_) {
+                       if (bp_->buttonStatus(ButtonPolicy::CANCEL)) {
+                               fl_set_object_label(cancel_,
+                                                   cancel_label);
+                       } else {
+                               fl_set_object_label(cancel_,
+                                                   close_label);
                        }
-                       if (undo_all_) {
-                               if (bp_.buttonStatus(ButtonPolicy::UNDO_ALL)) {
-                                       fl_activate_object(undo_all_);
-                                       fl_set_object_lcol(undo_all_, FL_BLACK);
-                               } else {
-                                       fl_deactivate_object(undo_all_);
-                                       fl_set_object_lcol(undo_all_,
+               }
+               if (!read_only_.empty()) {
+                       if (bp_->isReadOnly()) {
+                               std::list<FL_OBJECT *>::iterator
+                                       end = read_only_.end();
+                               for (std::list<FL_OBJECT *>::iterator
+                                    iter = read_only_.begin();
+                                    iter != end;
+                                    ++iter) {
+                                       fl_deactivate_object(*iter);
+                                       fl_set_object_lcol(*iter,
                                                           FL_INACTIVE);
                                }
-                       }
-                       if (cancel_) {
-                               if (bp_.buttonStatus(ButtonPolicy::CANCEL)) {
-                                       fl_set_object_label(cancel_,
-                                                           cancel_label);
-                               } else {
-                                       fl_set_object_label(cancel_,
-                                                           close_label);
+                       } else {
+                               std::list<FL_OBJECT *>::iterator
+                                       end = read_only_.end();
+                               for (std::list<FL_OBJECT *>::iterator
+                                    iter = read_only_.begin();
+                                    iter != end;
+                                    ++iter) {
+                                       fl_activate_object(*iter);
+                                       fl_set_object_lcol(*iter,
+                                                          FL_BLACK);
                                }
                        }
                }
-       //@}
+       }
+       ///
+       void input(ButtonPolicy::SMInput in) {
+               bp_->input(in);
+               refresh();
+       }
+       ///
+       void ok() {
+               input(ButtonPolicy::SMI_OKAY);
+       }
+       ///
+       void apply() {
+               input(ButtonPolicy::SMI_APPLY);
+       }
+       ///
+       void cancel() {
+               input(ButtonPolicy::SMI_CANCEL);
+       }
+       ///
+       void undoAll() {
+               input(ButtonPolicy::SMI_UNDO_ALL);
+       }
+       ///
+       void hide() {
+               input(ButtonPolicy::SMI_HIDE);
+       }
+       /// Passthrough function -- returns its input value
+       bool readOnly(bool ro = true) {
+               if (ro) {
+                       input(ButtonPolicy::SMI_READ_ONLY);
+               } else {
+                       input(ButtonPolicy::SMI_READ_WRITE);
+               }
+               return ro;
+       }
+       ///
+       void readWrite() {
+               readOnly(false);
+       }
+       /// Passthrough function -- returns its input value
+       bool valid(bool v = true) { 
+               if (v) {
+                       input(ButtonPolicy::SMI_VALID);
+               } else {
+                       input(ButtonPolicy::SMI_INVALID);
+               }
+               return v;
+       }
+       ///
+       void invalid() {
+               valid(false);
+       }
 private:
        ///
-       Policy bp_;
-       /**@name Button Widgets */
-       //@{
+       ButtonPolicy * bp_;
        ///
        FL_OBJECT * okay_;
        ///
@@ -176,14 +212,12 @@ private:
        FL_OBJECT * cancel_;
        ///
        FL_OBJECT * undo_all_;
-       //@}
-       /**@name Cancel/Close Button Labels */
-       //@{
+       /// List of items to be deactivated when in one of the read-only states
+       std::list<FL_OBJECT *> read_only_;
        ///
        char const * cancel_label;
        ///
        char const * close_label;       
-       //@}
 };
 
 #endif