]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/Dialog.C
refine the logic for checking whether a dialog may apply its data or not
[lyx.git] / src / frontends / controllers / Dialog.C
index 76610286ede0be8de0d1aa9eba46a8a2eb0e6b55..039c1c4f16b937636db561cbca9f1d692b4b493a 100644 (file)
@@ -1,4 +1,3 @@
-// -*- C++ -*-
 /**
  * \file Dialog.C
  * This file is part of LyX, the document processor.
@@ -6,7 +5,7 @@
  *
  * \author Angus Leeming
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
 #include "ButtonController.h"
 #include "BCView.h"
-#include "debug.h"
-#include "support/LAssert.h"
 
-using namespace lyx::support;
+#include "frontends/LyXView.h"
+
+#include "funcrequest.h"
+#include "FuncStatus.h"
+#include "lyxfunc.h"
+
+
+using std::string;
+
+namespace lyx {
+namespace frontend {
 
 Dialog::Dialog(LyXView & lv, string const & name)
        : is_closing_(false), kernel_(lv), name_(name),
@@ -91,8 +98,7 @@ void Dialog::update(string const & data)
 
        if (!controller().initialiseParams(data)) {
                lyxerr << "Dialog \"" << name_
-                      << "\" failed to translate the data "
-                       "string passed to update()" << std::endl;
+                      << "\" could not be initialized" << std::endl;
                return;
        }
 
@@ -111,13 +117,17 @@ void Dialog::hide()
 
        controller().clearParams();
        view().hide();
+       kernel().disconnect(name());
 }
 
 
 void Dialog::apply()
 {
-       if (kernel().isBufferReadonly())
-               return;
+       if (controller().isBufferDependent()) {
+               if (!kernel().isBufferAvailable() ||
+                   kernel().isBufferReadonly())
+                       return;
+       }
 
        view().apply();
        controller().dispatchParams();
@@ -144,33 +154,62 @@ void Dialog::redraw()
 
 ButtonController & Dialog::bc() const
 {
-       Assert(bc_ptr_.get());
+       BOOST_ASSERT(bc_ptr_.get());
        return *bc_ptr_.get();
 }
 
 
 void Dialog::setController(Controller * i)
 {
-       Assert(i && !controller_ptr_.get());
+       BOOST_ASSERT(i && !controller_ptr_.get());
        controller_ptr_.reset(i);
 }
 
 
 void Dialog::setView(View * v)
 {
-       Assert(v && !view_ptr_.get());
+       BOOST_ASSERT(v && !view_ptr_.get());
        view_ptr_.reset(v);
 }
 
 
+void Dialog::checkStatus()
+{
+       // buffer independant dialogs are always active.
+       // This check allows us leave canApply unimplemented for some dialogs.
+       if (!controller().isBufferDependent())
+               return;
+
+       // deactivate the dialog if we have no buffer
+       if (!kernel().isBufferAvailable()) {
+               bc().readOnly(true);
+               return;
+       }
+
+       // check whether this dialog may be active
+       if (controller().canApply())
+               bc().readOnly(kernel().isBufferReadonly());
+       else
+               bc().readOnly(true);
+}
+
+
 Dialog::Controller::Controller(Dialog & parent)
        : parent_(parent)
 {}
 
 
+bool Dialog::Controller::canApply() const
+{
+       FuncRequest const fr(getLfun(), dialog().name());
+       FuncStatus const fs(kernel().lyxview().getLyXFunc().getStatus(fr));
+       return fs.enabled();
+}
+
+
 Dialog::Controller & Dialog::controller() const
 {
-       Assert(controller_ptr_.get());
+       BOOST_ASSERT(controller_ptr_.get());
        return *controller_ptr_.get();
 }
 
@@ -182,7 +221,7 @@ Dialog::View::View(Dialog & parent, string title) :
 
 Dialog::View & Dialog::view() const
 {
-       Assert(view_ptr_.get());
+       BOOST_ASSERT(view_ptr_.get());
        return *view_ptr_.get();
 }
 
@@ -201,3 +240,6 @@ string const & Dialog::View::getTitle() const
 
 void Dialog::View::partialUpdate(int)
 {}
+
+} // namespace frontend
+} // namespace lyx