| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// Font_Browser.cpp v 0.0.3 2007-04-21 |
| 3 |
// |
| 4 |
// for the Fast Light Tool Kit (FLTK) 1.1.x . |
| 5 |
// |
| 6 |
// David Freese, [email protected] |
| 7 |
// based on similar widget by Mariwan Jalal |
| 8 |
// ---------------------------------------------------------------------------- |
| 9 |
// Copyright (C) 2014 |
| 10 |
// David Freese, W1HKJ |
| 11 |
// |
| 12 |
// This file is part of flmsg |
| 13 |
// |
| 14 |
// flrig is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; either version 3 of the License, or |
| 17 |
// (at your option) any later version. |
| 18 |
// |
| 19 |
// flrig is distributed in the hope that it will be useful, |
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
// GNU General Public License for more details. |
| 23 |
// |
| 24 |
// You should have received a copy of the GNU General Public License |
| 25 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 26 |
// ---------------------------------------------------------------------------- |
| 27 |
|
| 28 |
#include <config.h> |
| 29 |
|
| 30 |
#include <string> |
| 31 |
#include <cstdlib> |
| 32 |
#include <cstring> |
| 33 |
#include <string> |
| 34 |
#include <cstdio> |
| 35 |
#include <stdint.h> |
| 36 |
|
| 37 |
#include <FL/Fl.H> |
| 38 |
#include <FL/Fl_Color_Chooser.H> |
| 39 |
#include <FL/fl_draw.H> |
| 40 |
|
| 41 |
#include "font_browser.h" |
| 42 |
#include "flslider2.h" |
| 43 |
#include "gettext.h" |
| 44 |
|
| 45 |
Font_Browser* font_browser; |
| 46 |
|
| 47 |
// Font Color selected |
| 48 |
|
| 49 |
void Font_Browser::ColorSelect() |
| 50 |
{ |
| 51 |
unsigned char r, g, b; |
| 52 |
Fl::get_color(fontcolor, r, g, b); |
| 53 |
if (fl_color_chooser(_("Font color"), r, g, b) == 0) |
| 54 |
return; |
| 55 |
fontcolor = fl_rgb_color(r, g, b); |
| 56 |
btn_Color->color(fontcolor); |
| 57 |
} |
| 58 |
|
| 59 |
void Font_Browser::fb_callback(Fl_Widget* w, void* arg) |
| 60 |
{ |
| 61 |
Font_Browser* fb = reinterpret_cast<Font_Browser*>(arg); |
| 62 |
|
| 63 |
if (w == fb->btn_Cancel) |
| 64 |
fb->hide(); |
| 65 |
else if (w == fb->btn_OK) { |
| 66 |
if (fb->callback_) |
| 67 |
(*fb->callback_)(fb, fb->data_); |
| 68 |
} |
| 69 |
else if (w == fb->btn_Color) |
| 70 |
fb->ColorSelect(); |
| 71 |
else if (w == fb->lst_Font) |
| 72 |
fb->FontNameSelect(); |
| 73 |
else { |
| 74 |
if (w == fb->lst_Size) |
| 75 |
fb->txt_Size->value(strtol(fb->lst_Size->text(fb->lst_Size->value()), NULL, 10)); |
| 76 |
fb->fontsize = static_cast<int>(fb->txt_Size->value()); |
| 77 |
} |
| 78 |
fb->box_Example->SetFont(fb->fontnbr, fb->fontsize, fb->fontcolor); |
| 79 |
} |
| 80 |
|
| 81 |
// Sort the font list |
| 82 |
void Font_Browser::FontSort() |
| 83 |
{ |
| 84 |
int size = lst_Font->size(); |
| 85 |
for ( int t = 1; t <= size - 1; t++ ) |
| 86 |
for ( int r = t+1; r <= size; r++ ) |
| 87 |
if ( strcasecmp(lst_Font->text(t), lst_Font->text(r)) > 0 ) |
| 88 |
lst_Font->swap(t,r); |
| 89 |
} |
| 90 |
|
| 91 |
// Font Name changed callback |
| 92 |
void Font_Browser::FontNameSelect() |
| 93 |
{ |
| 94 |
int fn = lst_Font->value(); |
| 95 |
if (!fn) |
| 96 |
return; |
| 97 |
|
| 98 |
fontnbr = (Fl_Font)reinterpret_cast<intptr_t>(lst_Font->data(fn)); |
| 99 |
|
| 100 |
// get sizes and fill browser; skip first element if it is zero |
| 101 |
lst_Size->clear(); |
| 102 |
int nsizes, *sizes; |
| 103 |
char buf[4]; |
| 104 |
nsizes = Fl::get_font_sizes(fontnbr, sizes); |
| 105 |
// |
| 106 |
for (int i = !*sizes; i < nsizes; i++) |
| 107 |
if ((size_t)snprintf(buf, sizeof(buf), "%d", sizes[i]) < sizeof(buf)) |
| 108 |
lst_Size->add(buf, reinterpret_cast<void *>(sizes[i])); |
| 109 |
|
| 110 |
// scalable font with no suggested sizes |
| 111 |
if (!lst_Size->size()) { |
| 112 |
for (int i = 1; i <= 48; i++) { |
| 113 |
snprintf(buf, sizeof(buf), "%d", i); |
| 114 |
lst_Size->add(buf, reinterpret_cast<void *>(i)); |
| 115 |
} |
| 116 |
} |
| 117 |
fontSize(fontsize); |
| 118 |
} |
| 119 |
|
| 120 |
Font_Browser::Font_Browser(int x, int y, int w, int h, const char *lbl ) |
| 121 |
: Fl_Window(x, y, w, h, lbl) |
| 122 |
{ |
| 123 |
lst_Font = new Fl_Browser(5, 15, 280, 125, _("Font:")); |
| 124 |
lst_Font->align(FL_ALIGN_TOP_LEFT); |
| 125 |
lst_Font->type(FL_HOLD_BROWSER); |
| 126 |
lst_Font->callback(fb_callback, this); |
| 127 |
|
| 128 |
txt_Size = new Fl_Value_Input2(290, 15, 50, 22, _("Size:")); |
| 129 |
txt_Size->align(FL_ALIGN_TOP_LEFT); |
| 130 |
txt_Size->range(1.0, 48.0); |
| 131 |
txt_Size->step(1.0); |
| 132 |
txt_Size->callback(fb_callback, this); |
| 133 |
|
| 134 |
lst_Size = new Fl_Browser(290, 40, 50, 100); |
| 135 |
lst_Size->type(FL_HOLD_BROWSER); |
| 136 |
lst_Size->callback(fb_callback, this); |
| 137 |
|
| 138 |
btn_OK = new Fl_Return_Button(345, 40, 80, 25, _("&OK")); |
| 139 |
btn_OK->shortcut(0x8006f); |
| 140 |
btn_OK->callback(fb_callback, this); |
| 141 |
|
| 142 |
btn_Cancel = new Fl_Button(345, 70, 80, 25, _("Cancel")); |
| 143 |
btn_Cancel->labelsize(12); |
| 144 |
btn_Cancel->callback(fb_callback, this); |
| 145 |
|
| 146 |
btn_Color = new Fl_Button(345, 100, 80, 25, _("Color")); |
| 147 |
btn_Color->down_box(FL_BORDER_BOX); |
| 148 |
btn_Color->color(FL_FOREGROUND_COLOR); |
| 149 |
btn_Color->callback(fb_callback, this); |
| 150 |
|
| 151 |
box_Example = new Preview_Box(5, 145, 420, 75, _("That crazy fox jumped over the dog again!\n" |
| 152 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789\n" |
| 153 |
"!\"#$%&'()*+,-./:;<=>?@@[\\]^_`{|}~")); |
| 154 |
box_Example->box(FL_DOWN_BOX); |
| 155 |
box_Example->align(FL_ALIGN_WRAP|FL_ALIGN_CLIP|FL_ALIGN_CENTER|FL_ALIGN_INSIDE); |
| 156 |
resizable(box_Example); |
| 157 |
|
| 158 |
set_modal(); |
| 159 |
end(); |
| 160 |
|
| 161 |
// Initializations |
| 162 |
|
| 163 |
this->callback_ = 0; // Initialize Widgets callback |
| 164 |
this->data_ = 0; // And the data |
| 165 |
|
| 166 |
numfonts = Fl::set_fonts(0); // Nr of fonts available on the server |
| 167 |
|
| 168 |
const char* name; |
| 169 |
for(int i = 0; i < numfonts; i++) { |
| 170 |
name = Fl::get_font_name((Fl_Font)i); |
| 171 |
if (isalpha(*name)) |
| 172 |
lst_Font->add(name, reinterpret_cast<void *>(i)); |
| 173 |
} |
| 174 |
FontSort(); |
| 175 |
|
| 176 |
fontnbr = FL_HELVETICA;; |
| 177 |
fontsize = FL_NORMAL_SIZE; // Font Size to be used |
| 178 |
fontcolor = FL_FOREGROUND_COLOR; |
| 179 |
filter = ALL_TYPES; |
| 180 |
|
| 181 |
lst_Font->value(1); |
| 182 |
FontNameSelect(); |
| 183 |
|
| 184 |
//! Fl::focus(lst_Font); |
| 185 |
|
| 186 |
xclass(PACKAGE_NAME); |
| 187 |
} |
| 188 |
|
| 189 |
void Font_Browser::fontNumber(Fl_Font n) |
| 190 |
{ |
| 191 |
fontnbr = n; |
| 192 |
lst_Font->value(1); |
| 193 |
int s = lst_Font->size(); |
| 194 |
for (int i = 1; i < s; i++ ) { |
| 195 |
if ((Fl_Font)reinterpret_cast<intptr_t>(lst_Font->data(i)) == n) { |
| 196 |
lst_Font->value(i); |
| 197 |
FontNameSelect(); |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void Font_Browser::fontSize(int s) |
| 204 |
{ |
| 205 |
fontsize = s; |
| 206 |
int n = lst_Size->size(); |
| 207 |
for (int i = 1; i < n; i++) { |
| 208 |
if ((intptr_t)lst_Size->data(i) == fontsize) { |
| 209 |
lst_Size->value(i); |
| 210 |
break; |
| 211 |
} |
| 212 |
} |
| 213 |
txt_Size->value(s); |
| 214 |
} |
| 215 |
|
| 216 |
void Font_Browser::fontColor(Fl_Color c) |
| 217 |
{ |
| 218 |
btn_Color->color(fontcolor = c); |
| 219 |
box_Example->SetFont(fontnbr, fontsize, fontcolor); |
| 220 |
box_Example->redraw(); |
| 221 |
} |
| 222 |
|
| 223 |
void Font_Browser::fontName(const char* n) |
| 224 |
{ |
| 225 |
int s = lst_Font->size(); |
| 226 |
for (int i = 1; i < s; i++) { |
| 227 |
if (!strcmp(lst_Font->text(i), n)) { |
| 228 |
lst_Font->value(i); |
| 229 |
FontNameSelect(); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
bool Font_Browser::fixed_width(Fl_Font f) |
| 235 |
{ |
| 236 |
fl_font(f, FL_NORMAL_SIZE); |
| 237 |
return fl_width('X') == fl_width('i'); |
| 238 |
} |
| 239 |
|
| 240 |
#include <vector> |
| 241 |
#include <FL/Fl_Double_Window.H> |
| 242 |
#include <FL/Fl_Progress.H> |
| 243 |
|
| 244 |
class Progress_Window : public Fl_Double_Window |
| 245 |
{ |
| 246 |
public: |
| 247 |
Progress_Window(float min = 0.0f, float max = 100.0f, const char* l = 0) |
| 248 |
: Fl_Double_Window(200, 34), ps(5, 5, 190, 24, l) |
| 249 |
{ |
| 250 |
end(); |
| 251 |
|
| 252 |
range(min, max); |
| 253 |
ps.align(FL_ALIGN_CENTER | FL_ALIGN_INSIDE); |
| 254 |
ps.selection_color(FL_SELECTION_COLOR); |
| 255 |
set_modal(); |
| 256 |
callback(nop); |
| 257 |
|
| 258 |
if (l && *l) { |
| 259 |
fl_font(FL_HELVETICA, FL_NORMAL_SIZE); |
| 260 |
int s = (int)(fl_width(l) + fl_width('W')); |
| 261 |
if (s > ps.w()) { |
| 262 |
ps.size(s, ps.h()); |
| 263 |
size(ps.w() + 10, h()); |
| 264 |
} |
| 265 |
} |
| 266 |
position(Fl::event_x_root() - w() / 2, Fl::event_y_root() - h()); |
| 267 |
|
| 268 |
xclass(PACKAGE_TARNAME); |
| 269 |
show(); |
| 270 |
} |
| 271 |
void range(float min, float max) { ps.minimum(min); ps.maximum(max); } |
| 272 |
void value(float val) { ps.value(val); } |
| 273 |
static void nop(Fl_Widget*, void*) { } |
| 274 |
private: |
| 275 |
Fl_Progress ps; |
| 276 |
}; |
| 277 |
|
| 278 |
void Font_Browser::fontFilter(filter_t filter) |
| 279 |
{ |
| 280 |
if (this->filter == filter) |
| 281 |
return; |
| 282 |
|
| 283 |
int s = lst_Font->size(); |
| 284 |
|
| 285 |
static std::vector<bool> fixed; |
| 286 |
if (fixed.empty()) { |
| 287 |
Progress_Window pw(1, s, _("Reading fonts...")); |
| 288 |
fixed.resize(s); |
| 289 |
for (int i = 1; i < s; i++) { |
| 290 |
fixed[i] = fixed_width((Fl_Font)(intptr_t)(lst_Font->data(i))); |
| 291 |
pw.value(i); |
| 292 |
Fl::check(); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
switch (this->filter = filter) { |
| 297 |
case FIXED_WIDTH: |
| 298 |
for (int i = 1; i < s; i++) { |
| 299 |
if (fixed[i]) |
| 300 |
lst_Font->show(i); |
| 301 |
else |
| 302 |
lst_Font->hide(i); |
| 303 |
} |
| 304 |
break; |
| 305 |
case VARIABLE_WIDTH: |
| 306 |
for (int i = 1; i < s; i++) { |
| 307 |
if (!fixed[i]) |
| 308 |
lst_Font->show(i); |
| 309 |
else |
| 310 |
lst_Font->hide(i); |
| 311 |
} |
| 312 |
break; |
| 313 |
case ALL_TYPES: |
| 314 |
for (int i = 1; i < s; i++) |
| 315 |
lst_Font->show(i); |
| 316 |
break; |
| 317 |
} |
| 318 |
lst_Font->topline(lst_Font->value()); |
| 319 |
} |
| 320 |
|
| 321 |
////////////////////////////////////////////////////////////////////// |
| 322 |
|
| 323 |
Preview_Box::Preview_Box(int x, int y, int w, int h, const char* l) |
| 324 |
: Fl_Widget(x, y, w, h, l) |
| 325 |
{ |
| 326 |
fontName = 1; |
| 327 |
fontSize = FL_NORMAL_SIZE; |
| 328 |
box(FL_DOWN_BOX); |
| 329 |
color(FL_BACKGROUND2_COLOR); |
| 330 |
fontColor = FL_FOREGROUND_COLOR; |
| 331 |
} |
| 332 |
|
| 333 |
void Preview_Box::draw() |
| 334 |
{ |
| 335 |
draw_box(); |
| 336 |
fl_font((Fl_Font)fontName, fontSize); |
| 337 |
fl_color(fontColor); |
| 338 |
fl_draw(label(), x()+3, y()+3, w()-6, h()-6, align()); |
| 339 |
} |
| 340 |
|
| 341 |
void Preview_Box::SetFont(int fontname, int fontsize, Fl_Color c) |
| 342 |
{ |
| 343 |
fontName = fontname; |
| 344 |
fontSize = fontsize; |
| 345 |
fontColor = c; |
| 346 |
redraw(); |
| 347 |
} |