]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FeedbackController.C
This file is part of LyX, the document processor.
[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         if (event == FL_PUSH && key == 2 && ob->objclass == FL_INPUT) {
77                 // Trigger an input event when pasting in an xforms input object
78                 // using the middle mouse button.
79                 InputCB(ob, 0);
80
81         } else if (message_widget_ &&
82                    (event == FL_ENTER || event == FL_LEAVE)) {
83                 // Post feedback as the mouse enters the object,
84                 // remove it as the mouse leaves.
85                 MessageCB(ob, event);
86         }
87 }
88
89
90 void FeedbackController::postWarning(string const & warning)
91 {
92         warning_posted_ = true;
93         postMessage(warning);
94 }
95
96
97 void FeedbackController::clearMessage()
98 {
99         lyx::Assert(message_widget_);
100
101         warning_posted_ = false;
102
103         string const existing = message_widget_->label
104                 ? message_widget_->label : string();
105         if (existing.empty())
106                 return;
107
108         // This trick is needed to get xforms to clear the label...
109         fl_set_object_label(message_widget_, "");
110         fl_hide_object(message_widget_);
111 }
112
113
114 void FeedbackController::postMessage(string const & message)
115 {
116         lyx::Assert(message_widget_);
117
118         string str;
119         if (warning_posted_)
120                 str = _("WARNING! ") + message;
121         else
122                 str = message;
123
124         str = formatted(str, message_widget_->w-10, FL_SMALL_SIZE);
125
126         fl_set_object_label(message_widget_, str.c_str());
127         FL_COLOR const label_color = warning_posted_ ? FL_TOMATO : FL_BLACK;
128         fl_set_object_lcol(message_widget_, label_color);
129
130         if (!message_widget_->visible)
131                 fl_show_object(message_widget_);
132 }