]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FeedbackController.C
Improved understanding leads to an improved commentary ;-)
[lyx.git] / src / frontends / xforms / FeedbackController.C
1 /**
2  * \file FeedbackController.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming 
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 /* A common interface for posting feedback messages to a message widget in
12  * xforms.
13  * Derive FormBase and FormBaseDeprecated from it, so daughter classes of
14  * either can use the same interface.
15  */
16
17 #include <config.h>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "FeedbackController.h"
24 #include "gettext.h"        // _()
25 #include "xforms_helpers.h" // formatted
26 #include "support/LAssert.h"
27 #include FORMS_H_LOCATION
28
29 FeedbackController::FeedbackController()
30         : warning_posted_(false), message_widget_(0)
31 {}
32
33
34 FeedbackController::~FeedbackController()
35 {}
36
37
38 void FeedbackController::setMessageWidget(FL_OBJECT * ob)
39 {
40         lyx::Assert(ob && ob->objclass == FL_TEXT);
41         message_widget_ = ob;
42         fl_set_object_lsize(message_widget_, FL_SMALL_SIZE);
43 }
44
45
46 // preemptive handler for feedback messages
47 void FeedbackController::MessageCB(FL_OBJECT * ob, int event)
48 {
49         lyx::Assert(ob);
50
51         switch (event) {
52         case FL_ENTER:
53         {
54                 string const feedback = getFeedback(ob);
55                 if (feedback.empty() && warning_posted_)
56                         break;
57
58                 warning_posted_ = false;
59                 postMessage(getFeedback(ob));
60                 break;
61         }
62
63         case FL_LEAVE:
64                 if (!warning_posted_)
65                         clearMessage();
66                 break;
67
68         default:
69                 break;
70         }
71 }
72
73
74 void FeedbackController::PrehandlerCB(FL_OBJECT * ob, int event, int key)
75 {
76         lyx::Assert(ob);
77
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.
81                 InputCB(ob, 0);
82                 
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.
88
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.
93
94                 FL_FORM * const folder = fl_get_active_folder(ob);
95                 if (folder->window) {
96                         FL_Coord w, h;
97                         fl_get_wingeometry(folder->window,
98                                            &(folder->x), &(folder->y), &w, &h);
99                 }
100
101         } else if (message_widget_ &&
102                    (event == FL_ENTER || event == FL_LEAVE)) {
103                 // Post feedback as the mouse enters the object,
104                 // remove it as the mouse leaves.
105                 MessageCB(ob, event);
106         }
107 }
108
109
110 void FeedbackController::postWarning(string const & warning)
111 {
112         warning_posted_ = true;
113         postMessage(warning);
114 }
115
116
117 void FeedbackController::clearMessage()
118 {
119         lyx::Assert(message_widget_);
120
121         warning_posted_ = false;
122
123         string const existing = message_widget_->label
124                 ? message_widget_->label : string();
125         if (existing.empty())
126                 return;
127
128         // This trick is needed to get xforms to clear the label...
129         fl_set_object_label(message_widget_, "");
130         fl_hide_object(message_widget_);
131 }
132
133
134 void FeedbackController::postMessage(string const & message)
135 {
136         lyx::Assert(message_widget_);
137
138         string str;
139         if (warning_posted_)
140                 str = _("WARNING! ") + message;
141         else
142                 str = message;
143
144         str = formatted(str, message_widget_->w-10, FL_SMALL_SIZE);
145
146         fl_set_object_label(message_widget_, str.c_str());
147         FL_COLOR const label_color = warning_posted_ ? FL_TOMATO : FL_BLACK;
148         fl_set_object_lcol(message_widget_, label_color);
149
150         if (!message_widget_->visible)
151                 fl_show_object(message_widget_);
152 }