-// -*- C++ -*-
-/* 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 The LyX Team.
- *
- * ======================================================
+ * Full author contact details are available in file CREDITS.
*/
#include <config.h>
-#include FORMS_H_LOCATION
+#include "FormBase.h"
+
+#include "Tooltips.h"
+#include "xforms_helpers.h" // formatted
+#include "xforms_resize.h"
+#include "xformsBC.h"
+
+#include "controllers/ButtonController.h"
+#include "controllers/ControlButtons.h"
+
+#include "support/filetools.h" // LibFileSearch
+#include "support/lstrings.h"
+
+#include "lyx_forms.h"
-#ifdef __GNUG__
-#pragma implementation
+using lyx::support::bformat;
+using lyx::support::LibFileSearch;
+
+using std::string;
+
+
+extern "C" {
+
+#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
-#include "Dialogs.h"
-#include "FormBase.h"
-#include "LyXView.h"
-#include "support/LAssert.h"
-#include "debug.h"
+// Callback function invoked by xforms when the dialog is closed by the
+// window manager.
+static int C_WMHideCB(FL_FORM * form, void *);
-extern "C" int C_FormBaseWMHideCB(FL_FORM * ob, void * d)
-{
- return FormBase::WMHideCB(ob, d);
-}
-extern "C" void C_FormBaseApplyCB(FL_OBJECT * ob, long d)
-{
- FormBase::ApplyCB(ob, d);
-}
-extern "C" void C_FormBaseOKCB(FL_OBJECT * ob, long d)
-{
- FormBase::OKCB(ob, d);
-}
-extern "C" void C_FormBaseCancelCB(FL_OBJECT * ob, long d)
-{
- FormBase::CancelCB(ob, d);
-}
-extern "C" void C_FormBaseInputCB(FL_OBJECT * ob, long d)
-{
- FormBase::InputCB(ob, d);
-}
-extern "C" void C_FormBaseRestoreCB(FL_OBJECT * ob, long d)
+// Callback function invoked by the xforms pre-handler routine.
+static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
+
+} // extern "C"
+
+
+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)
+{}
+
+
+FormBase::~FormBase()
{
- FormBase::RestoreCB(ob, d);
+ if (icon_pixmap_)
+ XFreePixmap(fl_get_display(), icon_pixmap_);
+
+ delete tooltips_;
}
-FormBase::FormBase(LyXView * lv, Dialogs * d, string const & t,
- ButtonPolicy * bp, char const * close, char const * cancel)
- : lv_(lv), bc_(bp, cancel, close), d_(d), h_(0), r_(0), title(t),
- bp_(bp), minw_(0), minh_(0)
+bool FormBase::isVisible() const
{
- Assert(lv && d && bp);
+ return form() && form()->visible;
}
-FormBase::~FormBase()
+Tooltips & FormBase::tooltips()
{
- //lyxerr << "bp_[" << bp_ << "]" << endl;
- delete bp_;
+ return *tooltips_;
}
}
-void FormBase::connect()
+xformsBC & FormBase::bcview()
{
- fl_set_form_minsize(form(), minw_, minh_);
- r_ = Dialogs::redrawGUI.connect(slot(this, &FormBase::redraw));
+ return static_cast<xformsBC &>(bc().view());
}
-void FormBase::disconnect()
+void FormBase::prepare_to_show()
{
- h_.disconnect();
- r_.disconnect();
+ 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_);
+ }
}
void FormBase::show()
{
- if (!form()) {
- build();
- fl_set_form_atclose(form(),
- C_FormBaseWMHideCB, 0);
+ // build() is/should be called from the controller, so form() should
+ // always exist.
+ BOOST_ASSERT(form());
+
+ // 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) {
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();
+ // fl_show_form(), so this comes first.
+ fl_set_form_minsize(form(), minw_, minh_);
+ 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,
- FL_TRANSIENT,
- title.c_str());
+ iconify_policy,
+ maximize_title.c_str());
}
}
void FormBase::hide()
{
- if (form() && form()->visible) {
- // some dialogs might do things to the form first
- // such as the nested tabfolder problem in Preferences
- disconnect();
+ // 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
+ // 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().input(input(ob, data));
}
-int FormBase::WMHideCB(FL_FORM * form, void *)
+ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
{
- Assert(form);
- // Ensure that the signals (u and h) are disconnected even if the
- // window manager is used to close the dialog.
- FormBase * pre = static_cast<FormBase*>(form->u_vdata);
- Assert(pre);
- pre->hide();
- pre->bc_.hide();
- return FL_CANCEL;
+ return ButtonPolicy::SMI_VALID;
}
-void FormBase::ApplyCB(FL_OBJECT * ob, long)
+// preemptive handler for feedback messages
+void FormBase::MessageCB(FL_OBJECT * ob, int event)
{
- Assert(ob && ob->form);
- FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
- Assert(pre);
- pre->apply();
- pre->bc_.apply();
+ BOOST_ASSERT(ob);
+
+ switch (event) {
+ case FL_ENTER:
+ {
+ string const feedback = getFeedback(ob);
+ if (feedback.empty() && warning_posted_)
+ break;
+
+ warning_posted_ = false;
+ postMessage(getFeedback(ob));
+ break;
+ }
+
+ case FL_LEAVE:
+ if (!warning_posted_)
+ clearMessage();
+ break;
+
+ default:
+ break;
+ }
}
-void FormBase::OKCB(FL_OBJECT * ob, long)
+void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
{
- Assert(ob && ob->form);
- FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
- Assert(pre);
- pre->ok();
- pre->bc_.ok();
+ BOOST_ASSERT(ob);
+
+ 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;
+ }
+
+ 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;
+ }
+ }
+
+#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::CancelCB(FL_OBJECT * ob, long)
+void FormBase::postWarning(string const & warning)
{
- Assert(ob && ob->form);
- FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
- Assert(pre);
- pre->cancel();
- pre->bc_.cancel();
+ warning_posted_ = true;
+ postMessage(warning);
}
-void FormBase::InputCB(FL_OBJECT * ob, long data)
+void FormBase::clearMessage()
{
- Assert(ob && ob->form);
- FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
- Assert(ob);
- pre->bc_.valid(pre->input(ob, data));
+ BOOST_ASSERT(message_widget_);
+
+ warning_posted_ = false;
+
+ string const existing = message_widget_->label
+ ? message_widget_->label : string();
+ if (existing.empty())
+ return;
+
+ // This trick is needed to get xforms to clear the label...
+ fl_set_object_label(message_widget_, "");
+ fl_hide_object(message_widget_);
}
-void FormBase::RestoreCB(FL_OBJECT * ob, long)
+void FormBase::postMessage(string const & message)
{
- Assert(ob && ob->form);
- FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
- Assert(ob);
- pre->restore();
- pre->bc_.undoAll();
+ 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_);
}
-FormBaseBI::FormBaseBI(LyXView * lv, Dialogs * d, string const & t,
- ButtonPolicy * bp,
- char const * close, char const * cancel)
- : FormBase(lv, d, t, bp, close, cancel)
-{}
+namespace {
+FormBase * GetForm(FL_OBJECT * ob)
+{
+ BOOST_ASSERT(ob && ob->form && ob->form->u_vdata);
+ FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
+ return ptr;
+}
-void FormBaseBI::connect()
+} // namespace anon
+
+
+extern "C" {
+
+void C_FormBaseApplyCB(FL_OBJECT * ob, long)
{
- h_ = d_->hideAll.connect(slot(this, &FormBaseBI::hide));
- FormBase::connect();
+ GetForm(ob)->getController().ApplyButton();
}
-FormBaseBD::FormBaseBD(LyXView * lv, Dialogs * d, string const & t,
- ButtonPolicy * bp,
- char const * close, char const * cancel)
- : FormBase(lv, d, t, bp, close, cancel),
- u_(0)
-{}
+void C_FormBaseOKCB(FL_OBJECT * ob, long)
+{
+ GetForm(ob)->getController().OKButton();
+}
-void FormBaseBD::connect()
+void C_FormBaseCancelCB(FL_OBJECT * ob, long)
{
- u_ = d_->updateBufferDependent.
- connect(slot(this, &FormBaseBD::updateSlot));
- h_ = d_->hideBufferDependent.
- connect(slot(this, &FormBaseBD::hide));
- FormBase::connect();
+ FormBase * form = GetForm(ob);
+ form->getController().CancelButton();
}
-void FormBaseBD::disconnect()
+void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
{
- u_.disconnect();
- FormBase::disconnect();
+ GetForm(ob)->getController().RestoreButton();
}
+
+
+void C_FormBaseInputCB(FL_OBJECT * ob, long d)
+{
+ GetForm(ob)->InputCB(ob, d);
+}
+
+
+static int C_WMHideCB(FL_FORM * form, void *)
+{
+ // Close the dialog cleanly, even if the WM is used to do so.
+ BOOST_ASSERT(form && form->u_vdata);
+ FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
+ ptr->getController().CancelButton();
+ 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.
+ BOOST_ASSERT(ob);
+
+ // Don't Assert this one, as it can happen quite naturally when things
+ // are being deleted in the d-tor.
+ //BOOST_ASSERT(ob->form);
+ if (!ob->form) return 0;
+
+ FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
+
+ if (ptr)
+ ptr->PrehandlerCB(ob, event, key);
+
+ return 0;
+}
+
+} // extern "C"