]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormBase.C
Introduce LFUN_PRINT.
[lyx.git] / src / frontends / xforms / FormBase.C
index 2732f314e596b6aca00cb0170990285f5ce02288..31836c0e681c47ecc67ce6fcfe61697689b39dfb 100644 (file)
@@ -1,76 +1,81 @@
-/* This file is part of
- * ====================================================== 
+/**
+ * \file FormBase.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *           LyX, The Document Processor
+ * \author Angus Leeming
  *
- *           Copyright 2000-2001 The LyX Team.
- *
- * ======================================================
- *
- * \author Angus Leeming <a.leeming@ic.ac.uk>
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
-#include "Dialogs.h"
 #include "FormBase.h"
+
+#include "Tooltips.h"
+#include "xforms_helpers.h" // formatted
+#include "xforms_resize.h"
 #include "xformsBC.h"
-#include "support/LAssert.h"
 
-#if FL_REVISION < 89
+#include "controllers/ButtonController.h"
+#include "controllers/ControlButtons.h"
 
-namespace {
+#include "support/filetools.h" //  LibFileSearch
+#include "support/lstrings.h"
 
-int TooltipHandler(FL_OBJECT *ob, int event);
+#include "lyx_forms.h"
+
+using lyx::support::bformat;
+using lyx::support::LibFileSearch;
+
+using std::string;
 
-void TooltipTimerCB(FL_OBJECT * timer, long data);
-}
 
 extern "C" {
 
-static int C_FormBaseTooltipHandler(FL_OBJECT * ob, int event,
-                                   FL_Coord, FL_Coord, int, void *)
-{
-       return TooltipHandler(ob, event);
-}
+#if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
+// These should be in forms.h but aren't
+void fl_show_tooltip(const char *, int, int);
+void fl_hide_tooltip();
+#endif
 
-static void C_FormBaseTooltipTimerCB(FL_OBJECT * ob, long data)
-{
-       TooltipTimerCB(ob, data);
-}
+// Callback function invoked by xforms when the dialog is closed by the
+// window manager.
+static int C_WMHideCB(FL_FORM * form, void *);
 
-}
-#endif // FL_REVISION < 89
+// Callback function invoked by the xforms pre-handler routine.
+static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
 
+} // extern "C"
 
-extern "C" {
 
-// Callback function invoked by xforms when the dialog is closed by the
-// window manager
-static int C_FormBaseWMHideCB(FL_FORM * form, void *);
+FormBase::FormBase(string const & t, bool allowResize)
+       : ViewBase(t),
+         warning_posted_(false), message_widget_(0),
+         minw_(0), minh_(0), allow_resize_(allowResize),
+         icon_pixmap_(0), icon_mask_(0),
+         tooltips_(new Tooltips)
+{}
 
-// Use this to diaplay feedback messages or to trigger an input event on paste
-// with the middle mouse button
-static int C_FormBasePrehandler(FL_OBJECT * ob, int event,
-                               FL_Coord, FL_Coord, int key, void *);
 
-} // extern "C"
+FormBase::~FormBase()
+{
+       if (icon_pixmap_)
+               XFreePixmap(fl_get_display(), icon_pixmap_);
 
+       delete tooltips_;
+}
 
-FormBase::FormBase(ControlButtons & c, string const & t, bool allowResize)
-       : ViewBC<xformsBC>(c), minw_(0), minh_(0), allow_resize_(allowResize),
-         title_(t), warning_posted_(false)
 
+bool FormBase::isVisible() const
 {
-#if FL_REVISION < 89
-       tooltip_timer_ = 0;
-#endif
+       return form() && form()->visible;
+}
+
+
+Tooltips & FormBase::tooltips()
+{
+       return *tooltips_;
 }
 
 
@@ -81,26 +86,59 @@ void FormBase::redraw()
 }
 
 
-void FormBase::show()
+xformsBC & FormBase::bcview()
+{
+       return static_cast<xformsBC &>(bc().view());
+}
+
+
+void FormBase::prepare_to_show()
 {
-       if (!form()) {
-               build();
+       double const scale = get_scale_to_fit(form());
+       if (scale > 1.001)
+               scale_form_horizontally(form(), scale);
+
+       // work around dumb xforms sizing bug
+       minw_ = form()->w;
+       minh_ = form()->h;
+
+       fl_set_form_atclose(form(), C_WMHideCB, 0);
+
+       // set the title for the minimized form
+       if (!getController().IconifyWithMain())
+               fl_winicontitle(form()->window, getTitle().c_str());
+
+       //  assign an icon to the form
+       string const iconname = LibFileSearch("images", "lyx", "xpm");
+       if (!iconname.empty()) {
+               unsigned int w, h;
+               icon_pixmap_ = fl_read_pixmapfile(fl_root,
+                                                 iconname.c_str(),
+                                                 &w,
+                                                 &h,
+                                                 &icon_mask_,
+                                                 0, 0, 0);
+               fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
        }
+}
 
-       // use minw_ to flag whether the dialog has ever been shown
-       // (Needed now that build() is/should be called from the controller)
-       if (minw_ == 0) {
-               bc().refresh();
 
-               // work around dumb xforms sizing bug
-               minw_ = form()->w;
-               minh_ = form()->h;
+void FormBase::show()
+{
+       // build() is/should be called from the controller, so form() should
+       // always exist.
+       BOOST_ASSERT(form());
 
-               fl_set_form_atclose(form(), C_FormBaseWMHideCB, 0);
+       // we use minw_ to flag whether the dialog has ever been shown.
+       // In turn, prepare_to_show() initialises various bits 'n' pieces
+       // (including minw_).
+       if (minw_ == 0) {
+               prepare_to_show();
        }
 
+       // make sure the form is up to date.
        fl_freeze_form(form());
-       update();  // make sure its up-to-date
+       update();
        fl_unfreeze_form(form());
 
        if (form()->visible) {
@@ -121,21 +159,46 @@ void FormBase::show()
                if (!allow_resize_)
                        fl_set_form_maxsize(form(), minw_, minh_);
 
+               string const maximize_title = "LyX: " + getTitle();
+               int const iconify_policy =
+                       getController().IconifyWithMain() ? FL_TRANSIENT : 0;
+
                fl_show_form(form(),
-                       FL_PLACE_MOUSE | FL_FREE_SIZE,
-                       (controller_.IconifyWithMain() ? FL_TRANSIENT : 0),
-                       title_.c_str());
+                            FL_PLACE_MOUSE | FL_FREE_SIZE,
+                            iconify_policy,
+                            maximize_title.c_str());
        }
 }
 
 
 void FormBase::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);
+
        if (form() && form()->visible)
                fl_hide_form(form());
 }
 
 
+void FormBase::setPrehandler(FL_OBJECT * ob)
+{
+       BOOST_ASSERT(ob);
+       fl_set_object_prehandler(ob, C_PrehandlerCB);
+}
+
+
+void FormBase::setMessageWidget(FL_OBJECT * ob)
+{
+       BOOST_ASSERT(ob && ob->objclass == FL_TEXT);
+       message_widget_ = ob;
+       fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
+}
+
+
 void FormBase::InputCB(FL_OBJECT * ob, long data)
 {
        // It is possible to set the choice to 0 when using the
@@ -155,19 +218,25 @@ ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
 
 
 // preemptive handler for feedback messages
-void FormBase::FeedbackCB(FL_OBJECT * ob, int event)
+void FormBase::MessageCB(FL_OBJECT * ob, int event)
 {
-       lyx::Assert(ob);
+       BOOST_ASSERT(ob);
 
        switch (event) {
        case FL_ENTER:
+       {
+               string const feedback = getFeedback(ob);
+               if (feedback.empty() && warning_posted_)
+                       break;
+
                warning_posted_ = false;
-               feedback(ob);
+               postMessage(getFeedback(ob));
                break;
+       }
 
        case FL_LEAVE:
                if (!warning_posted_)
-                       clear_feedback();
+                       clearMessage();
                break;
 
        default:
@@ -176,58 +245,102 @@ void FormBase::FeedbackCB(FL_OBJECT * ob, int event)
 }
 
 
-void FormBase::setTooltipHandler(FL_OBJECT * ob)
+void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
 {
-       lyx::Assert(ob);
+       BOOST_ASSERT(ob);
 
-#if FL_REVISION < 89
-       if (!tooltip_timer_) {
-               fl_addto_form(form());
-               tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
-               fl_end_form();
+       if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
+               // Trigger an input event when pasting in an xforms input object
+               // using the middle mouse button.
+               InputCB(ob, 0);
+               return;
        }
 
-       fl_set_object_posthandler(ob, C_FormBaseTooltipHandler);
-       ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
+       if (message_widget_) {
+               switch (event) {
+               case FL_ENTER:
+               case FL_LEAVE:
+                       // Post feedback as the mouse enters the object,
+                       // remove it as the mouse leaves.
+                       MessageCB(ob, event);
+                       break;
+               }
+       }
 
-#else
-       string const help(getTooltip(ob));
-       if (!help.empty())
-               fl_set_object_helper(ob, help.c_str()); 
-#endif // FL_REVISION < 89
+#if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
+       // Tooltips are not displayed on browser widgets due to an xforms' bug.
+       // This is a work-around:
+       if (ob->objclass == FL_BROWSER) {
+               switch (event) {
+               case FL_ENTER:
+                       if (ob->tooltip && *(ob->tooltip)) {
+                               int const x = ob->form->x + ob->x;
+                               int const y = ob->form->y + ob->y + ob->h + 1;
+                               fl_show_tooltip(ob->tooltip, x, y);
+                       }
+                       break;
+               case FL_LEAVE:
+               case FL_PUSH:
+               case FL_KEYPRESS:
+                       fl_hide_tooltip();
+                       break;
+               }
+       }
+#endif
 }
 
 
-void FormBase::setPrehandler(FL_OBJECT * ob)
+void FormBase::postWarning(string const & warning)
 {
-       lyx::Assert(ob);
-       fl_set_object_prehandler(ob, C_FormBasePrehandler);
+       warning_posted_ = true;
+       postMessage(warning);
 }
 
 
-void FormBase::setWarningPosted(bool warning)
+void FormBase::clearMessage()
 {
-       warning_posted_ = warning;
-}
+       BOOST_ASSERT(message_widget_);
 
+       warning_posted_ = false;
 
-#if FL_REVISION < 89
+       string const existing = message_widget_->label
+               ? message_widget_->label : string();
+       if (existing.empty())
+               return;
 
-string const FormBase::getTooltipCB(FL_OBJECT * ob)
-{
-       return getTooltip(ob);
+       // This trick is needed to get xforms to clear the label...
+       fl_set_object_label(message_widget_, "");
+       fl_hide_object(message_widget_);
 }
 
-#endif // FL_REVISION < 89
+
+void FormBase::postMessage(string const & message)
+{
+       BOOST_ASSERT(message_widget_);
+
+       int const width = message_widget_->w - 10;
+       string const tmp = warning_posted_ ?
+               bformat(_("WARNING! %1$s"), message) :
+               message;
+
+       string const str = formatted(tmp, width, FL_NORMAL_SIZE);
+
+       fl_set_object_label(message_widget_, str.c_str());
+       FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
+       fl_set_object_lcol(message_widget_, label_color);
+
+       if (!message_widget_->visible)
+               fl_show_object(message_widget_);
+}
 
 
 namespace {
 
 FormBase * GetForm(FL_OBJECT * ob)
 {
-       lyx::Assert(ob && ob->form && ob->form->u_vdata);
-       FormBase * pre = static_cast<FormBase *>(ob->form->u_vdata);
-       return pre;
+       BOOST_ASSERT(ob && ob->form && ob->form->u_vdata);
+       FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
+       return ptr;
 }
 
 } // namespace anon
@@ -237,26 +350,26 @@ extern "C" {
 
 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
 {
-       GetForm(ob)->ApplyButton();
+       GetForm(ob)->getController().ApplyButton();
 }
 
 
 void C_FormBaseOKCB(FL_OBJECT * ob, long)
 {
-       GetForm(ob)->OKButton();
+       GetForm(ob)->getController().OKButton();
 }
 
 
 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
 {
        FormBase * form = GetForm(ob);
-       form->CancelButton();
+       form->getController().CancelButton();
 }
 
 
 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
 {
-       GetForm(ob)->RestoreButton();
+       GetForm(ob)->getController().RestoreButton();
 }
 
 
@@ -266,88 +379,33 @@ void C_FormBaseInputCB(FL_OBJECT * ob, long d)
 }
 
 
-static int C_FormBaseWMHideCB(FL_FORM * form, void *)
+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);
-       FormBase * pre = static_cast<FormBase *>(form->u_vdata);
-       pre->CancelButton();
+       BOOST_ASSERT(form && form->u_vdata);
+       FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
+       ptr->getController().CancelButton();
        return FL_CANCEL;
 }
 
-
-static int C_FormBasePrehandler(FL_OBJECT * ob, int event,
-                               FL_Coord, FL_Coord, int key, void *)
+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);
+       BOOST_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);
+       //BOOST_ASSERT(ob->form);
        if (!ob->form) return 0;
 
-       FormBase * pre = static_cast<FormBase *>(ob->form->u_vdata);
-       if (!pre) return 0;
-
-       if (event == FL_PUSH && key == 2 && ob->objclass == FL_INPUT) {
-               // Trigger an input event when pasting in an xforms input object
-               // using the middle mouse button.
-               pre->InputCB(ob, 0);
-
-       } else if (event == FL_ENTER || event == FL_LEAVE){
-               // Post feedback as the mouse enters the object,
-               // remove it as the mouse leaves.
-               pre->FeedbackCB(ob, event);
-       }
-
-       return 0;
-}
-} // extern "C"
-
-
-#if FL_REVISION < 89
+       FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
 
-namespace {
-
-void TooltipTimerCB(FL_OBJECT * timer, long data)
-{
-       FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
-       lyx::Assert(ob && ob->form);
+       if (ptr)
+               ptr->PrehandlerCB(ob, event, key);
 
-       string const help = GetForm(timer)->getTooltipCB(ob);
-       if (help.empty())
-               return;
-
-       fl_show_oneliner(help.c_str(),
-                        ob->form->x + ob->x,
-                        ob->form->y + ob->y + ob->h);
-}
-
-
-// post_handler for bubble-help (Matthias)
-int TooltipHandler(FL_OBJECT *ob, int event)
-{
-       lyx::Assert(ob);
-       FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
-       lyx::Assert(timer);
-
-       // We do not test for empty help here, since this can never happen
-       if (event == FL_ENTER){
-               fl_set_object_callback(timer,
-                                      C_FormBaseTooltipTimerCB,
-                                      reinterpret_cast<long>(ob));
-               fl_set_timer(timer, 1);
-       }
-       else if (event != FL_MOTION){
-               fl_set_timer(timer, 0);
-               fl_hide_oneliner();
-       }
        return 0;
 }
 
-} // namespace anon
-
-#endif // FL_REVISION < 89
+} // extern "C"