-
Notifications
You must be signed in to change notification settings - Fork 772
/
FontButton.cpp
132 lines (98 loc) · 3.74 KB
/
FontButton.cpp
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
#include "FontButton.h"
#include <locale>
#include <sstream>
#include <utility>
#include <config.h>
#include "util/i18n.h"
#include "util/serdesstream.h"
using std::string;
FontButton::FontButton(ActionHandler* handler, GladeGui* gui, string id, ActionType type, string description,
GtkWidget* menuitem):
AbstractToolItem(std::move(id), handler, type, menuitem) {
this->gui = gui;
this->description = std::move(description);
}
FontButton::~FontButton() = default;
void FontButton::activated(GdkEvent* event, GtkMenuItem* menuitem, GtkToolButton* toolbutton) {
GtkFontButton* button = GTK_FONT_BUTTON(fontButton);
string name = gtk_font_button_get_font_name(button);
auto pos = name.find_last_of(' ');
this->font.setName(name.substr(0, pos));
this->font.setSize(std::stod(name.substr(pos + 1)));
handler->actionPerformed(ACTION_FONT_BUTTON_CHANGED, GROUP_NOGROUP, event, menuitem, nullptr, true);
}
void FontButton::setFontFontButton(GtkWidget* fontButton, XojFont& font) {
GtkFontButton* button = GTK_FONT_BUTTON(fontButton);
auto fontSizeStream = serdes_stream<std::stringstream>();
fontSizeStream << font.getSize();
string name = font.getName() + " " + fontSizeStream.str();
gtk_font_button_set_font_name(button, name.c_str());
}
void FontButton::setFont(XojFont& font) {
this->font = font;
if (this->fontButton == nullptr) {
return;
}
setFontFontButton(this->fontButton, font);
}
auto FontButton::getFont() const -> XojFont {
// essentially, copy the font object to prevent a memory leak.
XojFont newfont;
newfont.setName(font.getName());
newfont.setSize(font.getSize());
return newfont;
}
auto FontButton::getToolDisplayName() const -> string { return _("Font"); }
auto FontButton::getNewToolIcon() const -> GtkWidget* {
return gtk_image_new_from_icon_name("font-x-generic", GTK_ICON_SIZE_SMALL_TOOLBAR);
}
auto FontButton::getNewToolPixbuf() const -> GdkPixbuf* { return getPixbufFromImageIconName(); }
auto FontButton::createItem(bool horizontal) -> GtkToolItem* {
if (this->item) {
return this->item;
}
this->item = newItem();
g_object_ref(this->item);
g_signal_connect(fontButton, "font_set", G_CALLBACK(&toolButtonCallback), this);
return this->item;
}
auto FontButton::createTmpItem(bool horizontal) -> GtkToolItem* {
GtkWidget* fontButton = newFontButton();
GtkToolItem* it = gtk_tool_item_new();
gtk_container_add(GTK_CONTAINER(it), fontButton);
gtk_tool_item_set_tooltip_text(it, this->description.c_str());
gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(it), false);
if (!this->font.getName().empty()) {
setFontFontButton(fontButton, this->font);
}
gtk_widget_show_all(GTK_WIDGET(it));
return it;
}
void FontButton::showFontDialog() {
if (this->fontButton == nullptr) {
newItem();
}
gtk_button_clicked(GTK_BUTTON(this->fontButton));
}
auto FontButton::newFontButton() -> GtkWidget* {
GtkWidget* w = gtk_font_button_new();
gtk_widget_show(w);
gtk_font_button_set_use_font(GTK_FONT_BUTTON(w), true);
gtk_button_set_focus_on_click(GTK_BUTTON(w), false);
return w;
}
auto FontButton::newItem() -> GtkToolItem* {
if (this->fontButton) {
g_object_unref(this->fontButton);
}
GtkToolItem* it = gtk_tool_item_new();
this->fontButton = newFontButton();
gtk_container_add(GTK_CONTAINER(it), this->fontButton);
gtk_tool_item_set_tooltip_text(it, this->description.c_str());
gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(it), false);
g_signal_connect(this->fontButton, "font_set", G_CALLBACK(&toolButtonCallback), this);
if (!this->font.getName().empty()) {
setFont(this->font);
}
return it;
}