Debian Codegraph @

src/widgets/icons.cxx

raw
1 // ----------------------------------------------------------------------------
2 // icons.cxx
3 //
4 // Copyright (C) 2008
5 // Stelios Bounanos, M0GLD
6 //
7 // This file is part of flmsg.
8 //
9 // This is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This software is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 // =====================================================================
23 // ----------------------------------------------------------------------------
24
25 #include <config.h>
26 #include "flmsg_config.h"
27 #include "icons.h"
28 #include "util.h"
29
30 #include <FL/Fl.H>
31 #include <FL/Fl_Menu_Item.H>
32 #include <FL/Fl_Widget.H>
33 #include <FL/Fl_Group.H>
34
35 #if USE_IMAGE_LABELS
36 # include <map>
37 # include <cassert>
38 # include <cstring>
39
40 # include <FL/Fl_Multi_Label.H>
41 # include <FL/Fl_Image.H>
42 # include <FL/Fl_Pixmap.H>
43
44 #endif
45
46
47 namespace icons {
48
49 #if USE_IMAGE_LABELS
50 typedef std::map<Fl_Multi_Label*, Fl_Image**> imap_t;
51 static imap_t* imap = 0;
52 #endif
53
54 #define FL_EMPTY_LABEL FL_FREE_LABELTYPE
55 static void draw_empty(const Fl_Label*, int, int, int, int, Fl_Align) { }
56 static void measure_empty(const Fl_Label*, int& w, int& h) { w = h = 0; }
57
58 // The following functions create image+text menu item labels.
59 // You've had too much FLTK if you already know how to do that.
60
61
62 // Return a multi_label pointer, cast to a std::string, for `text' and
63 // `pixmap'. This goes into the label pointer of a widget or menu
64 // item. The text label is copied if we are using multi labels. You must
65 // call set_icon_label on the widget or menu item before its draw()
66 // function is called for the first time.
67 //
68 // A NULL pixmap means that the caller wants an empty, transparent, icon.
69 const char* make_icon_label(const char* text, const char** pixmap)
70 {
71 #if USE_IMAGE_LABELS
72 static imap_t* imap_ = 0;
73 if (unlikely(!imap_)) {
74 imap = imap_ = new imap_t;
75 Fl::set_labeltype(FL_EMPTY_LABEL, draw_empty, measure_empty);
76 }
77
78 // Create a multi label and associate it with an Fl_Image* array
79 Fl_Multi_Label* mlabel = new Fl_Multi_Label;
80 Fl_Image** images = new Fl_Image*[2];
81 images[0] = new Fl_Pixmap(pixmap ? pixmap : clear_row_icon);
82 images[1] = 0; // we create this on demand
83 // set_icon_label_ will set mlabel->labela later
84 mlabel->typea = _FL_IMAGE_LABEL;
85
86 if (!text)
87 text = "";
88 size_t len = strlen(text);
89 char* s = new char[len + 2];
90 s[0] = ' ';
91 memcpy(s + 1, text, len + 1);
92 mlabel->labelb = s;
93 mlabel->typeb = FL_NORMAL_LABEL;
94
95 (*imap)[mlabel] = images;
96
97 return (const char*)mlabel;
98 #else
99 return text;
100 #endif
101 }
102
103 #if USE_IMAGE_LABELS
104 // Find the item's label, which should be something that was returned by
105 // make_icon_label, and set the active or inactive image.
106 template <typename T>
107 void set_icon_label_(T* item)
108 {
109 imap_t::iterator j = imap->find((Fl_Multi_Label*)(item->label()));
110 if (j == imap->end())
111 return;
112
113 Fl_Multi_Label* mlabel = j->first;
114 Fl_Image** images = j->second;
115 unsigned char i = !item->active();
116
117 if (!images[i]) { // create inactive version of other image
118 images[i] = images[!i]->copy();
119 images[i]->inactive();
120 }
121 if (mlabel->typea == _FL_IMAGE_LABEL)
122 mlabel->labela = (const char*)images[i];
123 else
124 mlabel->labelb = (const char*)images[i];
125 item->image(images[i]);
126 mlabel->label(item);
127 item->labeltype(_FL_MULTI_LABEL);
128 }
129 #endif
130
131 void set_icon_label(Fl_Menu_Item* item)
132 {
133 #if USE_IMAGE_LABELS
134 set_icon_label_(item);
135 #else
136 // this isn't needed but it simplifies fldigi's UI setup code
137 if (item->labeltype() == _FL_MULTI_LABEL)
138 item->labeltype(FL_NORMAL_LABEL);
139 #endif
140 }
141
142 void set_icon_label(Fl_Widget* w)
143 {
144 #if USE_IMAGE_LABELS
145 set_icon_label_(w);
146 w->image(0);
147 #else
148 if (w->labeltype() == _FL_MULTI_LABEL)
149 w->labeltype(FL_NORMAL_LABEL);
150 #endif
151 }
152
153 void toggle_icon_labels(void)
154 {
155 #if USE_IMAGE_LABELS
156 for (imap_t::iterator i = imap->begin(); i != imap->end(); ++i) {
157 // swap sublabels
158 const char* l = i->first->labela;
159 i->first->labela = i->first->labelb;
160 i->first->labelb = l;
161 if (i->first->typea == _FL_IMAGE_LABEL) {
162 i->first->typea = FL_NORMAL_LABEL;
163 i->first->typeb = FL_EMPTY_LABEL;
164 i->first->labela++;
165 }
166 else {
167 i->first->typea = _FL_IMAGE_LABEL;
168 i->first->typeb = FL_NORMAL_LABEL;
169 i->first->labelb--;
170 }
171 }
172
173 #endif
174 }
175
176 template <typename T>
177 const char* get_icon_label_text_(T* item)
178 {
179 #if USE_IMAGE_LABELS
180 if (item->labeltype() == _FL_MULTI_LABEL) {
181 imap_t::iterator i = imap->find((Fl_Multi_Label*)(item->label()));
182 if (i == imap->end())
183 return 0;
184 if (i->first->typeb == FL_NORMAL_LABEL)
185 return i->first->labelb + 1;
186 else // disabled icons
187 return i->first->labela;
188 }
189 else
190 #endif
191 return item->label();
192 }
193
194 const char* get_icon_label_text(Fl_Menu_Item* item)
195 {
196 return get_icon_label_text_(item);
197 }
198 const char* get_icon_label_text(Fl_Widget* w)
199 {
200 return get_icon_label_text_(w);
201 }
202
203 template <typename T>
204 void free_icon_label_(T* item)
205 {
206 #if USE_IMAGE_LABELS
207 if (item->labeltype() == FL_NORMAL_LABEL) {
208 delete [] item->label();
209 item->label(0);
210 return;
211 }
212
213 imap_t::iterator i = imap->find((Fl_Multi_Label*)item->label());
214 if (i == imap->end())
215 return;
216
217 item->label(0);
218
219 // delete the images
220 delete i->second[0];
221 delete i->second[1];
222 delete [] i->second;
223
224 // delete the multi label
225 delete [] ((i->first->typeb == FL_NORMAL_LABEL) ? i->first->labelb : i->first->labela-1);
226 delete i->first;
227
228 imap->erase(i);
229 #endif
230 }
231
232 void free_icon_label(Fl_Menu_Item* item) { free_icon_label_(item); }
233 void free_icon_label(Fl_Widget* w) { free_icon_label_(w); }
234
235 template <typename T>
236 void set_active_(T* t, bool v) {
237 if (v)
238 t->activate();
239 else
240 t->deactivate();
241 if (t->labeltype() == _FL_MULTI_LABEL)
242 set_icon_label(t);
243 }
244
245 void set_active(Fl_Menu_Item* item, bool v) { set_active_(item, v); }
246 void set_active(Fl_Widget* w, bool v) { set_active_(w, v); }
247
248 static Fl_Image* msg_icon;
249 void set_message_icon(const char** pixmap)
250 {
251 if (msg_icon && msg_icon->data() == pixmap)
252 return;
253 delete msg_icon;
254
255 Fl_Widget* msg = fl_message_icon();
256 msg->label("");
257 msg->align(FL_ALIGN_TOP_LEFT | FL_ALIGN_INSIDE);
258
259 if (!msg->parent())
260 msg->color(FL_BACKGROUND_COLOR);
261 else
262 msg->color(msg->parent()->color());
263
264 msg->box(FL_NO_BOX);
265 msg->image(msg_icon = new Fl_Pixmap(pixmap));
266 }
267
268 } // icons