]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FeedbackController.C
ws cleanup
[lyx.git] / src / frontends / xforms / FeedbackController.C
1 /**
2  * \file FeedbackController.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming, a.leeming@ic.ac.uk
7  */
8
9 /* A common interface for posting feedback messages to a message widget in
10  * xforms.
11  * Derive FormBase and FormBaseDeprecated from it, so daughter classes of
12  * either can use the same interface.
13  */
14
15 #include <config.h>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "FeedbackController.h"
22 #include "gettext.h"        // _()
23 #include "xforms_helpers.h" // formatted
24 #include "support/LAssert.h"
25
26
27 FeedbackController::FeedbackController()
28         : warning_posted_(false), message_widget_(0)
29 {}
30
31
32 FeedbackController::~FeedbackController()
33 {}
34
35
36 void FeedbackController::setMessageWidget(FL_OBJECT * ob)
37 {
38         lyx::Assert(ob && ob->objclass == FL_TEXT);
39         message_widget_ = ob;
40         fl_set_object_lsize(message_widget_, FL_SMALL_SIZE);
41 }
42
43
44 // preemptive handler for feedback messages
45 void FeedbackController::MessageCB(FL_OBJECT * ob, int event)
46 {
47         lyx::Assert(ob);
48
49         switch (event) {
50         case FL_ENTER:
51         {
52                 string const feedback = getFeedback(ob);
53                 if (feedback.empty() && warning_posted_)
54                         break;
55
56                 warning_posted_ = false;
57                 postMessage(getFeedback(ob));
58                 break;
59         }
60
61         case FL_LEAVE:
62                 if (!warning_posted_)
63                         clearMessage();
64                 break;
65
66         default:
67                 break;
68         }
69 }
70
71
72 void FeedbackController::PrehandlerCB(FL_OBJECT * ob, int event, int key)
73 {
74         if (event == FL_PUSH && key == 2 && ob->objclass == FL_INPUT) {
75                 // Trigger an input event when pasting in an xforms input object
76                 // using the middle mouse button.
77                 InputCB(ob, 0);
78
79         } else if (message_widget_ &&
80                    (event == FL_ENTER || event == FL_LEAVE)) {
81                 // Post feedback as the mouse enters the object,
82                 // remove it as the mouse leaves.
83                 MessageCB(ob, event);
84         }
85 }
86
87
88 void FeedbackController::postWarning(string const & warning)
89 {
90         warning_posted_ = true;
91
92         string const str = _("WARNING! ") + warning;
93         postMessage(str);
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_hide_object(message_widget_);
110         fl_set_object_label(message_widget_, "");
111         fl_show_object(message_widget_);
112 }
113
114
115 void FeedbackController::postMessage(string const & message)
116 {
117         lyx::Assert(message_widget_);
118
119         string const str = formatted(message,
120                                      message_widget_->w-10, FL_SMALL_SIZE);
121
122         fl_set_object_label(message_widget_, str.c_str());
123         FL_COLOR const label_color = warning_posted_ ? FL_TOMATO : FL_BLACK;
124         fl_set_object_lcol(message_widget_, label_color);
125 }