]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormBaseDeprecated.C
Prevent crashes experienced by speakers of "long" languages like Norwegian
[lyx.git] / src / frontends / xforms / FormBaseDeprecated.C
index 4838a85dfa45d7fba9068e6ca3d07fa53f3735fa..41c808f0c425c54fb96a84464d599ccd7cc71c55 100644 (file)
@@ -1,62 +1,60 @@
-// -*- C++ -*-
-/* This file is part of
- * ====================================================== 
+/**
+ * \file FormBaseDeprecated.C
+ * Copyright 2000-2001 The LyX Team.
+ * See the file COPYING.
  *
- *           LyX, The Document Processor
- *
- *           Copyright 2000 The LyX Team.
- *
- * ======================================================
+ * \author Angus Leeming, a.leeming@ic.ac.uk
  */
 
 #include <config.h>
 
-#include FORMS_H_LOCATION
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
 #include "Dialogs.h"
 #include "FormBaseDeprecated.h"
+#include "xformsBC.h"
+#include "xforms_resize.h"
+#include "GUIRunTime.h"
+#include "Tooltips.h"
 #include "LyXView.h"
+#include "lyxrc.h"
 #include "support/LAssert.h"
-#include "xformsBC.h"
-//#include "debug.h"
 
 using SigC::slot;
 
-extern "C" int C_FormBaseDeprecatedWMHideCB(FL_FORM * ob, void * d)
-{
-       return FormBaseDeprecated::WMHideCB(ob, d);
-}
-extern "C" void C_FormBaseDeprecatedApplyCB(FL_OBJECT * ob, long d)
-{
-       FormBaseDeprecated::ApplyCB(ob, d);
-}
-extern "C" void C_FormBaseDeprecatedOKCB(FL_OBJECT * ob, long d)
-{
-       FormBaseDeprecated::OKCB(ob, d);
-}
-extern "C" void C_FormBaseDeprecatedCancelCB(FL_OBJECT * ob, long d)
-{
-       FormBaseDeprecated::CancelCB(ob, d);
-}
-extern "C" void C_FormBaseDeprecatedInputCB(FL_OBJECT * ob, long d)
+extern "C" {
+
+// Callback function invoked by xforms when the dialog is closed by the
+// window manager
+static int C_WMHideCB(FL_FORM *, void *);
+
+// Callback function invoked by the xforms pre- and post-handler routines
+static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
+
+} // extern "C"
+
+
+FormBaseDeprecated::FormBaseDeprecated(LyXView * lv, Dialogs * d,
+                                      string const & t, bool allowResize)
+       : lv_(lv), d_(d), h_(0), r_(0), title_(t),
+         minw_(0), minh_(0), allow_resize_(allowResize),
+         tooltips_(new Tooltips)
 {
-       FormBaseDeprecated::InputCB(ob, d);
+       lyx::Assert(lv && d);
 }
-extern "C" void C_FormBaseDeprecatedRestoreCB(FL_OBJECT * ob, long d)
+
+
+FormBaseDeprecated::~FormBaseDeprecated()
 {
-       FormBaseDeprecated::RestoreCB(ob, d);
+       delete tooltips_;
 }
 
 
-FormBaseDeprecated::FormBaseDeprecated(LyXView * lv, Dialogs * d,
-                                      string const & t)
-       : lv_(lv), d_(d), h_(0), r_(0), title(t), minw_(0), minh_(0)
+Tooltips & FormBaseDeprecated::tooltips()
 {
-       Assert(lv && d);
+       return *tooltips_;
 }
 
 
@@ -85,8 +83,18 @@ void FormBaseDeprecated::show()
 {
        if (!form()) {
                build();
-               fl_set_form_atclose(form(),
-                                   C_FormBaseDeprecatedWMHideCB, 0);
+
+               double const scale = scale_to_fit_tabs(form());
+               if (scale > 1.001)
+                       scale_form(form(), scale);
+
+               bc().refresh();
+
+               // work around dumb xforms sizing bug
+               minw_ = form()->w;
+               minh_ = form()->h;
+
+               fl_set_form_atclose(form(), C_WMHideCB, 0);
        }
 
        fl_freeze_form(form());
@@ -95,20 +103,43 @@ void FormBaseDeprecated::show()
 
        if (form()->visible) {
                fl_raise_form(form());
+               /* This XMapWindow() will hopefully ensure that
+                * iconified dialogs are de-iconified. Mad props
+                * out to those crazy Xlib guys for forgetting a
+                * XDeiconifyWindow(). At least WindowMaker, when
+                * being notified of the redirected MapRequest will
+                * specifically de-iconify. From source, fvwm2 seems
+                * to do the same.
+                */
+               XMapWindow(fl_get_display(), form()->window);
        } else {
-               // calls to fl_set_form_minsize/maxsize apply only to the next
-               // fl_show_form(), so connect() comes first.
                connect();
+
+               // calls to fl_set_form_minsize/maxsize apply only to the next
+               // fl_show_form(), so this comes first.
+               fl_set_form_minsize(form(), minw_, minh_);
+               if (!allow_resize_)
+                       fl_set_form_maxsize(form(), minw_, minh_);
+
                fl_show_form(form(),
                             FL_PLACE_MOUSE | FL_FREE_SIZE,
-                            FL_TRANSIENT,
-                            title.c_str());
+                            (lyxrc.dialogs_iconify_with_main ? FL_TRANSIENT : 0),
+                            title_.c_str());
        }
+
+       tooltips().set();
 }
 
 
 void FormBaseDeprecated::hide()
 {
+       // xforms sometimes tries to process a hint-type MotionNotify, and
+       // use XQueryPointer, without verifying if the window still exists.
+       // So we try to clear out motion events in the queue before the
+       // DestroyNotify
+       XSync(fl_get_display(), false);
+       GUIRunTime::processEvents();
+
        if (form() && form()->visible) {
                // some dialogs might do things to the form first
                // such as the nested tabfolder problem in Preferences
@@ -118,76 +149,63 @@ void FormBaseDeprecated::hide()
 }
 
 
-int FormBaseDeprecated::WMHideCB(FL_FORM * form, void *)
+void FormBaseDeprecated::setPrehandler(FL_OBJECT * ob)
 {
-       Assert(form);
-       // Ensure that the signals (u and h) are disconnected even if the
-       // window manager is used to close the dialog.
-       FormBaseDeprecated * pre =
-               static_cast<FormBaseDeprecated*>(form->u_vdata);
-       Assert(pre);
-       pre->hide();
-       pre->bc().hide();
-       return FL_CANCEL;
+       lyx::Assert(ob);
+       fl_set_object_prehandler(ob, C_PrehandlerCB);
 }
 
 
-void FormBaseDeprecated::ApplyCB(FL_OBJECT * ob, long)
+void FormBaseDeprecated::WMHideCB()
 {
-       Assert(ob && ob->form);
-       FormBaseDeprecated * pre =
-               static_cast<FormBaseDeprecated*>(ob->form->u_vdata);
-       Assert(pre);
-       pre->apply();
-       pre->bc().apply();
+       hide();
+       bc().hide();
 }
 
 
-void FormBaseDeprecated::OKCB(FL_OBJECT * ob, long)
+void FormBaseDeprecated::ApplyCB()
 {
-       Assert(ob && ob->form);
-       FormBaseDeprecated * pre =
-               static_cast<FormBaseDeprecated*>(ob->form->u_vdata);
-       Assert(pre);
-       pre->ok();
-       pre->bc().ok();
+       apply();
+       bc().apply();
 }
 
 
-void FormBaseDeprecated::CancelCB(FL_OBJECT * ob, long)
+void FormBaseDeprecated::OKCB()
 {
-       Assert(ob && ob->form);
-       FormBaseDeprecated * pre =
-               static_cast<FormBaseDeprecated*>(ob->form->u_vdata);
-       Assert(pre);
-       pre->cancel();
-       pre->bc().cancel();
+       ok();
+       bc().ok();
+}
+
+
+void FormBaseDeprecated::CancelCB()
+{
+       cancel();
+       bc().cancel();
 }
 
 
 void FormBaseDeprecated::InputCB(FL_OBJECT * ob, long data)
 {
-       Assert(ob && ob->form);
-       FormBaseDeprecated * pre =
-               static_cast<FormBaseDeprecated*>(ob->form->u_vdata);
-       Assert(pre);
-       pre->bc().valid(pre->input(ob, data));
+       // It is possible to set the choice to 0 when using the
+       // keyboard shortcuts. This work-around deals with the problem.
+       if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
+               fl_set_choice(ob, 1);
+       }
+
+       bc().valid(input(ob, data));
 }
 
 
-void FormBaseDeprecated::RestoreCB(FL_OBJECT * ob, long)
+void FormBaseDeprecated::RestoreCB()
 {
-       Assert(ob && ob->form);
-       FormBaseDeprecated * pre =
-               static_cast<FormBaseDeprecated*>(ob->form->u_vdata);
-       Assert(pre);
-       pre->bc().undoAll();
-       pre->restore();
+       bc().restore();
+       restore();
 }
 
 
-FormBaseBI::FormBaseBI(LyXView * lv, Dialogs * d, string const & t)
-       : FormBaseDeprecated(lv, d, t)
+FormBaseBI::FormBaseBI(LyXView * lv, Dialogs * d, string const & t,
+                      bool allowResize)
+       : FormBaseDeprecated(lv, d, t, allowResize)
 {}
 
 
@@ -198,8 +216,9 @@ void FormBaseBI::connect()
 }
 
 
-FormBaseBD::FormBaseBD(LyXView * lv, Dialogs * d, string const & t)
-       : FormBaseDeprecated(lv, d, t),
+FormBaseBD::FormBaseBD(LyXView * lv, Dialogs * d, string const & t,
+                      bool allowResize)
+       : FormBaseDeprecated(lv, d, t, allowResize),
          u_(0)
 {}
 
@@ -207,9 +226,9 @@ FormBaseBD::FormBaseBD(LyXView * lv, Dialogs * d, string const & t)
 void FormBaseBD::connect()
 {
        u_ = d_->updateBufferDependent.
-                connect(slot(this, &FormBaseBD::updateSlot));
+               connect(slot(this, &FormBaseBD::updateSlot));
        h_ = d_->hideBufferDependent.
-                connect(slot(this, &FormBaseBD::hide));
+               connect(slot(this, &FormBaseBD::hide));
        FormBaseDeprecated::connect();
 }
 
@@ -219,3 +238,81 @@ void FormBaseBD::disconnect()
        u_.disconnect();
        FormBaseDeprecated::disconnect();
 }
+
+
+namespace {
+
+FormBaseDeprecated * GetForm(FL_OBJECT * ob)
+{
+       lyx::Assert(ob && ob->form && ob->form->u_vdata);
+       FormBaseDeprecated * ptr =
+               static_cast<FormBaseDeprecated *>(ob->form->u_vdata);
+       return ptr;
+}
+
+} // namespace anon
+
+
+extern "C" {
+
+void C_FormBaseDeprecatedApplyCB(FL_OBJECT * ob, long)
+{
+       GetForm(ob)->ApplyCB();
+}
+
+
+void C_FormBaseDeprecatedOKCB(FL_OBJECT * ob, long)
+{
+       GetForm(ob)->OKCB();
+}
+
+
+void C_FormBaseDeprecatedCancelCB(FL_OBJECT * ob, long)
+{
+       GetForm(ob)->CancelCB();
+}
+
+
+void C_FormBaseDeprecatedInputCB(FL_OBJECT * ob, long d)
+{
+       GetForm(ob)->InputCB(ob, d);
+}
+
+
+void C_FormBaseDeprecatedRestoreCB(FL_OBJECT * ob, long)
+{
+       GetForm(ob)->RestoreCB();
+}
+
+static int C_WMHideCB(FL_FORM * form, void *)
+{
+       // Close the dialog cleanly, even if the WM is used to do so.
+       lyx::Assert(form && form->u_vdata);
+       FormBaseDeprecated * ptr =
+               static_cast<FormBaseDeprecated *>(form->u_vdata);
+       ptr->WMHideCB();
+       return FL_CANCEL;
+}
+
+static int C_PrehandlerCB(FL_OBJECT * ob, int event,
+                         FL_Coord, FL_Coord, int key, void *)
+{
+       // Note that the return value is important in the pre-emptive handler.
+       // Don't return anything other than 0.
+       lyx::Assert(ob);
+
+       // Don't Assert this one, as it can happen quite naturally when things
+       // are being deleted in the d-tor.
+       //Assert(ob->form);
+       if (!ob->form) return 0;
+
+       FormBaseDeprecated * ptr =
+               static_cast<FormBaseDeprecated *>(ob->form->u_vdata);
+
+       if (ptr)
+               ptr->PrehandlerCB(ob, event, key);
+
+       return 0;
+}
+
+} // extern "C"