-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfl_font_chooser.h
More file actions
150 lines (128 loc) · 5.1 KB
/
fl_font_chooser.h
File metadata and controls
150 lines (128 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Color_Chooser.H>
#include <FL/Fl_Return_Button.H>
#include <FL/Fl_Hold_Browser.H>
#include <FL/Fl_Window.H>
inline int fl_font_chooser(const char* name, Fl_Font& font, int& size, Fl_Color& color, const char* fontset = nullptr, int color_mode = -1) {
using namespace std;
using namespace std::literals;
class Fl_Font_Chooser {
public:
Fl_Font_Chooser() {
dialog_.end();
int numberOfFont = Fl::set_fonts(font_set_ ? font_set_->c_str() : nullptr);
for (int index = 0; index < numberOfFont; index++) {
int fontType = 0;
string font_name = Fl::get_font_name((Fl_Font)index, &fontType);
if (fontType) {
//if (fontType & FL_BOLD) font_name = "@b" + font_name;
//if (fontType & FL_ITALIC) font_name = "@i" + font_name;
font_name = "@F" + std::to_string(index) + font_name;
if (font_name.find("@.") != string::npos) font_name.erase(font_name.find("@."), "@."s.length()); // Suppress subsequent formatting - some MS fonts have '@' in their name
}
font_names_.add(font_name.c_str());
}
font_names_.callback([](Fl_Widget* sender, void* data) {
auto font_chooser = reinterpret_cast<Fl_Font_Chooser*>(data);
font_chooser->font_ = font_chooser->font_names_.value() - 1;
font_chooser->font_view_.labelfont(font_chooser->font_);
font_chooser->font_view_.redraw();
}, this);
for (int index = 1; index <= 72; index++)
font_sizes_.add(to_string(index).c_str());
font_sizes_.callback([](Fl_Widget* sender, void* data) {
auto font_chooser = reinterpret_cast<Fl_Font_Chooser*>(data);
font_chooser->size_ = font_chooser->font_sizes_.value() - 1;
font_chooser->font_view_.labelsize(font_chooser->size_);
font_chooser->font_view_.redraw();
}, this);
font_view_.box(FL_DOWN_BOX);
font_view_.align(FL_ALIGN_LEFT | FL_ALIGN_TOP | FL_ALIGN_INSIDE | FL_ALIGN_CLIP | FL_ALIGN_WRAP);
button_ok_.callback([](Fl_Widget* sender, void* data) {
auto font_chooser = reinterpret_cast<Fl_Font_Chooser*>(data);
font_chooser->result_ = 1;
font_chooser->dialog_.hide();
}, this);
button_cancel_.shortcut(FL_Escape);
button_cancel_.callback([](Fl_Widget* sender, void* data) {
auto font_chooser = reinterpret_cast<Fl_Font_Chooser*>(data);
font_chooser->result_ = 0;
font_chooser->dialog_.hide();
}, this);
button_color_.callback([](Fl_Widget* sender, void* data) {
auto font_chooser = reinterpret_cast<Fl_Font_Chooser*>(data);
uchar r = 0, g = 0, b = 0;
Fl::get_color(font_chooser->color_, r, g, b);
if (fl_color_chooser("Color", r, g, b, font_chooser->color_mode_) != 0) {
font_chooser->color_ = fl_rgb_color(r, g, b);
font_chooser->font_view_.labelcolor(fl_rgb_color(r, g, b));
font_chooser->font_view_.redraw();
}
}, this);
}
Fl_Font_Chooser(const Fl_Font_Chooser&) = delete;
Fl_Font_Chooser& operator=(const Fl_Font_Chooser&) = delete;
int show() {
dialog_.set_modal();
dialog_.show();
while (dialog_.visible())
Fl::wait();
return result_;
}
Fl_Font font() const {return font_;}
void font(Fl_Font f) {
font_ = f;
font_names_.value(font_ + 1);
font_view_.labelfont(font_);
}
void font_set(const string& fs) {font_set_ = make_unique<string>(fs);}
string label() const {return dialog_.label();}
void label(const string& l) {dialog_.label(l.c_str());}
Fl_Fontsize size() const {return size_;}
void size(Fl_Fontsize s) {
size_ = s;
font_sizes_.value(size_ + 1);
font_view_.labelsize(size_);
}
Fl_Color color() const {return color_;}
void color(int c) {
color_ = c;
font_view_.labelcolor(color_);
}
int color_mode() const {return color_mode_;}
void color_mode(int m) {
color_mode_ = m;
}
private:
Fl_Window dialog_ {100, 200, 550, 430, ""};
Fl_Hold_Browser font_names_ {10, 10, 390, 170};
Fl_Hold_Browser font_sizes_ {410, 10, 130, 170};
Fl_Box font_view_ {10, 190, 530, 180, "The quick brown fox jumps over the lazy dog.\nTHE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."};
Fl_Return_Button button_ok_ {295, 390, 75, 25, "Ok"};
Fl_Button button_cancel_ {380, 390, 75, 25, "Cancel"};
Fl_Button button_color_ {465, 390, 75, 25, "Color..."};
Fl_Font font_ = FL_HELVETICA;
int size_ = FL_NORMAL_SIZE;
Fl_Color color_ = FL_FOREGROUND_COLOR;
unique_ptr<string> font_set_;
int color_mode_ = -1;
int result_ = 0;
} font_dialog;
font_dialog.font(font);
if (fontset) font_dialog.font_set(fontset);
font_dialog.label(name);
font_dialog.size(size);
font_dialog.color(color);
int result_ = font_dialog.show();
if (result_ == 1) {
font = font_dialog.font();
size = font_dialog.size();
color = font_dialog.color();
}
return result_;
}