2 * \file FeedbackController.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS
11 /* A common interface for posting feedback messages to a message widget in
13 * Derive FormBase and FormBaseDeprecated from it, so daughter classes of
14 * either can use the same interface.
20 #pragma implementation
23 #include "FeedbackController.h"
24 #include "gettext.h" // _()
25 #include "xforms_helpers.h" // formatted
26 #include "support/LAssert.h"
27 #include FORMS_H_LOCATION
29 FeedbackController::FeedbackController()
30 : warning_posted_(false), message_widget_(0)
34 FeedbackController::~FeedbackController()
38 void FeedbackController::setMessageWidget(FL_OBJECT * ob)
40 lyx::Assert(ob && ob->objclass == FL_TEXT);
42 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
46 // preemptive handler for feedback messages
47 void FeedbackController::MessageCB(FL_OBJECT * ob, int event)
54 string const feedback = getFeedback(ob);
55 if (feedback.empty() && warning_posted_)
58 warning_posted_ = false;
59 postMessage(getFeedback(ob));
74 void FeedbackController::PrehandlerCB(FL_OBJECT * ob, int event, int key)
78 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
79 // Trigger an input event when pasting in an xforms input object
80 // using the middle mouse button.
83 } else if (ob->objclass == FL_TABFOLDER &&
84 (event == FL_ENTER || event == FL_LEAVE)) {
85 // This prehandler is used to work-around an xforms bug and
86 // ensures that the form->x, form->y coords of the active
87 // tabfolder are up to date.
89 // The tabfolder itself can be very narrow, being just
90 // the visible border to the tabs.
91 // We thus use both FL_ENTER and FL_LEAVE as flags,
92 // in case the FL_ENTER event is not caught.
94 FL_FORM * const folder = fl_get_active_folder(ob);
95 if (folder && folder->window) {
96 fl_get_winorigin(folder->window,
97 &(folder->x), &(folder->y));
100 } else if (message_widget_ &&
101 (event == FL_ENTER || event == FL_LEAVE)) {
102 // Post feedback as the mouse enters the object,
103 // remove it as the mouse leaves.
104 MessageCB(ob, event);
109 void FeedbackController::postWarning(string const & warning)
111 warning_posted_ = true;
112 postMessage(warning);
116 void FeedbackController::clearMessage()
118 lyx::Assert(message_widget_);
120 warning_posted_ = false;
122 string const existing = message_widget_->label
123 ? message_widget_->label : string();
124 if (existing.empty())
127 // This trick is needed to get xforms to clear the label...
128 fl_set_object_label(message_widget_, "");
129 fl_hide_object(message_widget_);
133 void FeedbackController::postMessage(string const & message)
135 lyx::Assert(message_widget_);
139 str = _("WARNING! ") + message;
143 int const width = message_widget_->w - 10;
144 str = formatted(str, width, FL_NORMAL_SIZE);
146 fl_set_object_label(message_widget_, str.c_str());
147 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
148 fl_set_object_lcol(message_widget_, label_color);
150 if (!message_widget_->visible)
151 fl_show_object(message_widget_);