1 #include "FontButton.h"
2 
3 #include <locale>
4 #include <sstream>
5 #include <utility>
6 
7 #include <config.h>
8 
9 #include "i18n.h"
10 
FontButton(ActionHandler * handler,GladeGui * gui,string id,ActionType type,string description,GtkWidget * menuitem)11 FontButton::FontButton(ActionHandler* handler, GladeGui* gui, string id, ActionType type, string description,
12                        GtkWidget* menuitem):
13         AbstractToolItem(std::move(id), handler, type, menuitem) {
14     this->gui = gui;
15     this->description = std::move(description);
16 }
17 
18 FontButton::~FontButton() = default;
19 
activated(GdkEvent * event,GtkMenuItem * menuitem,GtkToolButton * toolbutton)20 void FontButton::activated(GdkEvent* event, GtkMenuItem* menuitem, GtkToolButton* toolbutton) {
21     GtkFontButton* button = GTK_FONT_BUTTON(fontButton);
22 
23     string name = gtk_font_button_get_font_name(button);
24 
25     int pos = name.find_last_of(' ');
26     this->font.setName(name.substr(0, pos));
27     this->font.setSize(std::stod(name.substr(pos + 1)));
28 
29     handler->actionPerformed(ACTION_FONT_BUTTON_CHANGED, GROUP_NOGROUP, event, menuitem, nullptr, true);
30 }
31 
setFontFontButton(GtkWidget * fontButton,XojFont & font)32 void FontButton::setFontFontButton(GtkWidget* fontButton, XojFont& font) {
33     GtkFontButton* button = GTK_FONT_BUTTON(fontButton);
34     // Fixing locale to make format of font-size string independent of localization setting
35     std::stringstream fontSizeStream;
36     fontSizeStream.imbue(std::locale("C"));
37     fontSizeStream << font.getSize();
38     string name = font.getName() + " " + fontSizeStream.str();
39     gtk_font_button_set_font_name(button, name.c_str());
40 }
41 
setFont(XojFont & font)42 void FontButton::setFont(XojFont& font) {
43     this->font = font;
44     if (this->fontButton == nullptr) {
45         return;
46     }
47 
48     setFontFontButton(this->fontButton, font);
49 }
50 
getFont()51 auto FontButton::getFont() -> XojFont {
52     // essentially, copy the font object to prevent a memory leak.
53     XojFont newfont;
54     newfont.setName(font.getName());
55     newfont.setSize(font.getSize());
56 
57     return newfont;
58 }
59 
getToolDisplayName()60 auto FontButton::getToolDisplayName() -> string { return _("Font"); }
61 
getNewToolIcon()62 auto FontButton::getNewToolIcon() -> GtkWidget* {
63     return gtk_image_new_from_icon_name("font-x-generic", GTK_ICON_SIZE_SMALL_TOOLBAR);
64 }
65 
createItem(bool horizontal)66 auto FontButton::createItem(bool horizontal) -> GtkToolItem* {
67     if (this->item) {
68         return this->item;
69     }
70 
71     this->item = newItem();
72     g_object_ref(this->item);
73     g_signal_connect(fontButton, "font_set", G_CALLBACK(&toolButtonCallback), this);
74     return this->item;
75 }
76 
createTmpItem(bool horizontal)77 auto FontButton::createTmpItem(bool horizontal) -> GtkToolItem* {
78     GtkWidget* fontButton = newFontButton();
79 
80     GtkToolItem* it = gtk_tool_item_new();
81 
82     gtk_container_add(GTK_CONTAINER(it), fontButton);
83     gtk_tool_item_set_tooltip_text(it, this->description.c_str());
84     gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(it), false);
85 
86     if (!this->font.getName().empty()) {
87         setFontFontButton(fontButton, this->font);
88     }
89 
90     gtk_widget_show_all(GTK_WIDGET(it));
91     return it;
92 }
93 
showFontDialog()94 void FontButton::showFontDialog() {
95     if (this->fontButton == nullptr) {
96         newItem();
97     }
98 
99     gtk_button_clicked(GTK_BUTTON(this->fontButton));
100 }
101 
newFontButton()102 auto FontButton::newFontButton() -> GtkWidget* {
103     GtkWidget* w = gtk_font_button_new();
104     gtk_widget_show(w);
105     gtk_font_button_set_use_font(GTK_FONT_BUTTON(w), true);
106     gtk_button_set_focus_on_click(GTK_BUTTON(w), false);
107 
108     return w;
109 }
110 
newItem()111 auto FontButton::newItem() -> GtkToolItem* {
112     if (this->fontButton) {
113         g_object_unref(this->fontButton);
114     }
115     GtkToolItem* it = gtk_tool_item_new();
116 
117     this->fontButton = newFontButton();
118     gtk_container_add(GTK_CONTAINER(it), this->fontButton);
119     gtk_tool_item_set_tooltip_text(it, this->description.c_str());
120     gtk_tool_item_set_homogeneous(GTK_TOOL_ITEM(it), false);
121 
122     g_signal_connect(this->fontButton, "font_set", G_CALLBACK(&toolButtonCallback), this);
123 
124     if (!this->font.getName().empty()) {
125         setFont(this->font);
126     }
127 
128     return it;
129 }
130