]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FeedbackController.C
Refine yesterday's bug fix a little and apply to the combox also.
[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         // This is very odd:
84         // event == FL_LEAVE when the mouse enters the folder and
85         // event == FL_ENTER are it leaves!
86         } else if (ob->objclass == FL_TABFOLDER && event == FL_LEAVE) {
87                 // This prehandler is used to work-around an xforms bug.
88                 // It updates the form->x, form->y coords of the active
89                 // tabfolder when the mouse enters.
90                 FL_FORM * const form = fl_get_active_folder(ob);
91                 if (form->window) {
92                         FL_Coord w, h;
93                         fl_get_wingeometry(form->window,
94                                            &(form->x), &(form->y), &w, &h);
95                 }
96
97         } else if (message_widget_ &&
98                    (event == FL_ENTER || event == FL_LEAVE)) {
99                 // Post feedback as the mouse enters the object,
100                 // remove it as the mouse leaves.
101                 MessageCB(ob, event);
102         }
103 }
104
105
106 void FeedbackController::postWarning(string const & warning)
107 {
108         warning_posted_ = true;
109         postMessage(warning);
110 }
111
112
113 void FeedbackController::clearMessage()
114 {
115         lyx::Assert(message_widget_);
116
117         warning_posted_ = false;
118
119         string const existing = message_widget_->label
120                 ? message_widget_->label : string();
121         if (existing.empty())
122                 return;
123
124         // This trick is needed to get xforms to clear the label...
125         fl_set_object_label(message_widget_, "");
126         fl_hide_object(message_widget_);
127 }
128
129
130 void FeedbackController::postMessage(string const & message)
131 {
132         lyx::Assert(message_widget_);
133
134         string str;
135         if (warning_posted_)
136                 str = _("WARNING! ") + message;
137         else
138                 str = message;
139
140         str = formatted(str, message_widget_->w-10, FL_SMALL_SIZE);
141
142         fl_set_object_label(message_widget_, str.c_str());
143         FL_COLOR const label_color = warning_posted_ ? FL_TOMATO : FL_BLACK;
144         fl_set_object_lcol(message_widget_, label_color);
145
146         if (!message_widget_->visible)
147                 fl_show_object(message_widget_);
148 }