lyx_mirror/src/frontends/xforms/FormBase.C

354 lines
7.0 KiB
C++
Raw Normal View History

/* This file is part of
* ======================================================
*
* LyX, The Document Processor
*
* Copyright 2000-2001 The LyX Team.
*
* ======================================================
*
* \author Angus Leeming <a.leeming@ic.ac.uk>
*/
#include <config.h>
#ifdef __GNUG__
#pragma implementation
#endif
#include "Dialogs.h"
#include "FormBase.h"
#include "xformsBC.h"
#include "support/LAssert.h"
#if FL_REVISION < 89
namespace {
int TooltipHandler(FL_OBJECT *ob, int event);
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);
}
static void C_FormBaseTooltipTimerCB(FL_OBJECT * ob, long data)
{
TooltipTimerCB(ob, data);
}
}
#endif // FL_REVISION < 89
extern "C" {
// Callback function invoked by xforms when the dialog is closed by the
// window manager
static int C_FormBaseWMHideCB(FL_FORM * form, void *);
// 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(ControlButtons & c, string const & t, bool allowResize)
: ViewBC<xformsBC>(c), minw_(0), minh_(0), allow_resize_(allowResize),
title_(t), warning_posted_(false)
{
#if FL_REVISION < 89
tooltip_timer_ = 0;
#endif
}
void FormBase::redraw()
{
if (form() && form()->visible)
fl_redraw_form(form());
}
void FormBase::show()
{
if (!form()) {
build();
}
// 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;
fl_set_form_atclose(form(), C_FormBaseWMHideCB, 0);
}
fl_freeze_form(form());
update(); // make sure its up-to-date
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 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,
(controller_.IconifyWithMain() ? FL_TRANSIENT : 0),
title_.c_str());
}
}
void FormBase::hide()
{
if (form() && form()->visible)
fl_hide_form(form());
}
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));
}
ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
{
return ButtonPolicy::SMI_VALID;
}
// preemptive handler for feedback messages
void FormBase::FeedbackCB(FL_OBJECT * ob, int event)
{
lyx::Assert(ob);
switch (event) {
case FL_ENTER:
warning_posted_ = false;
feedback(ob);
break;
case FL_LEAVE:
if (!warning_posted_)
clear_feedback();
break;
default:
break;
}
}
void FormBase::setTooltipHandler(FL_OBJECT * ob)
{
lyx::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();
}
fl_set_object_posthandler(ob, C_FormBaseTooltipHandler);
ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
#else
string const help(getTooltip(ob));
if (!help.empty())
fl_set_object_helper(ob, help.c_str());
#endif // FL_REVISION < 89
}
void FormBase::setPrehandler(FL_OBJECT * ob)
{
lyx::Assert(ob);
fl_set_object_prehandler(ob, C_FormBasePrehandler);
}
void FormBase::setWarningPosted(bool warning)
{
warning_posted_ = warning;
}
#if FL_REVISION < 89
string const FormBase::getTooltipCB(FL_OBJECT * ob)
{
return getTooltip(ob);
}
#endif // FL_REVISION < 89
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;
}
} // namespace anon
extern "C" {
void C_FormBaseApplyCB(FL_OBJECT * ob, long)
{
GetForm(ob)->ApplyButton();
}
void C_FormBaseOKCB(FL_OBJECT * ob, long)
{
GetForm(ob)->OKButton();
}
void C_FormBaseCancelCB(FL_OBJECT * ob, long)
{
FormBase * form = GetForm(ob);
form->CancelButton();
}
void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
{
GetForm(ob)->RestoreButton();
}
void C_FormBaseInputCB(FL_OBJECT * ob, long d)
{
GetForm(ob)->InputCB(ob, d);
}
static int C_FormBaseWMHideCB(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();
return FL_CANCEL;
}
static int C_FormBasePrehandler(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;
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
namespace {
void TooltipTimerCB(FL_OBJECT * timer, long data)
{
FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
lyx::Assert(ob && ob->form);
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