1
// ----------------------------------------------------------------------------
5
// Stelios Bounanos, M0GLD
6
// ----------------------------------------------------------------------------
10
// This file is part of flrig.
12
// flrig is free software; you can redistribute it and/or modify
13
// it under the terms of the GNU General Public License as published by
14
// the Free Software Foundation; either version 3 of the License, or
15
// (at your option) any later version.
17
// flrig is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
// GNU General Public License for more details.
22
// You should have received a copy of the GNU General Public License
23
// along with this program. If not, see <http://www.gnu.org/licenses/>.
24
// ----------------------------------------------------------------------------
31
#include <FL/Fl_Menu_Item.H>
32
#include <FL/Fl_Widget.H>
33
#include <FL/Fl_Group.H>
40
# include <FL/Fl_Multi_Label.H>
41
# include <FL/Fl_Image.H>
42
# include <FL/Fl_Pixmap.H>
50
typedef map<Fl_Multi_Label*, Fl_Image**> imap_t;
51
static imap_t* imap = 0;
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; }
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.
62
// Return a multi_label pointer, cast to a 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.
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)
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);
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;
88
size_t len = strlen(text);
89
char* s = new char[len + 2];
91
memcpy(s + 1, text, len + 1);
93
mlabel->typeb = FL_NORMAL_LABEL;
95
(*imap)[mlabel] = images;
97
return (const char*)mlabel;
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)
109
imap_t::iterator j = imap->find((Fl_Multi_Label*)(item->label()));
110
if (j == imap->end())
113
Fl_Multi_Label* mlabel = j->first;
114
Fl_Image** images = j->second;
115
unsigned char i = !item->active();
117
if (!images[i]) { // create inactive version of other image
118
images[i] = images[!i]->copy();
119
images[i]->inactive();
121
if (mlabel->typea == _FL_IMAGE_LABEL)
122
mlabel->labela = (const char*)images[i];
124
mlabel->labelb = (const char*)images[i];
125
item->image(images[i]);
127
item->labeltype(_FL_MULTI_LABEL);
131
void set_icon_label(Fl_Menu_Item* item)
134
set_icon_label_(item);
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);
142
void set_icon_label(Fl_Widget* w)
148
if (w->labeltype() == _FL_MULTI_LABEL)
149
w->labeltype(FL_NORMAL_LABEL);
153
void toggle_icon_labels(void)
156
for (imap_t::iterator i = imap->begin(); i != imap->end(); ++i) {
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;
167
i->first->typea = _FL_IMAGE_LABEL;
168
i->first->typeb = FL_NORMAL_LABEL;
176
template <typename T>
177
const char* get_icon_label_text_(T* item)
180
if (item->labeltype() == _FL_MULTI_LABEL) {
181
imap_t::iterator i = imap->find((Fl_Multi_Label*)(item->label()));
182
if (i == imap->end())
184
if (i->first->typeb == FL_NORMAL_LABEL)
185
return i->first->labelb + 1;
186
else // disabled icons
187
return i->first->labela;
191
return item->label();
194
const char* get_icon_label_text(Fl_Menu_Item* item)
196
return get_icon_label_text_(item);
198
const char* get_icon_label_text(Fl_Widget* w)
200
return get_icon_label_text_(w);
203
template <typename T>
204
void free_icon_label_(T* item)
207
if (item->labeltype() == FL_NORMAL_LABEL) {
208
delete [] item->label();
213
imap_t::iterator i = imap->find((Fl_Multi_Label*)item->label());
214
if (i == imap->end())
224
// delete the multi label
225
delete [] ((i->first->typeb == FL_NORMAL_LABEL) ? i->first->labelb : i->first->labela-1);
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); }
235
template <typename T>
236
void set_active_(T* t, bool v) {
241
if (t->labeltype() == _FL_MULTI_LABEL)
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); }
248
static Fl_Image* msg_icon;
249
void set_message_icon(const char** pixmap)
251
if (msg_icon && msg_icon->data() == pixmap)
255
Fl_Widget* msg = fl_message_icon();
257
msg->align(FL_ALIGN_TOP_LEFT | FL_ALIGN_INSIDE);
258
msg->color(msg->parent()->color());
260
msg->image(msg_icon = new Fl_Pixmap(pixmap));