| 1 |
/* gtkmm example Copyright (C) 2002 gtkmm development team |
| 2 |
* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <gtkmm.h> |
| 18 |
#include <iostream> |
| 19 |
#include <algorithm> |
| 20 |
|
| 21 |
#define HAS_STYLE_PROVIDER_ADD_PROVIDER_FOR_DISPLAY GTKMM_CHECK_VERSION(4,9,1) |
| 22 |
#define HAS_FONT_DIALOG_BUTTON GTKMM_CHECK_VERSION(4,9,1) |
| 23 |
|
| 24 |
enum { DEF_PAD = 10 }; |
| 25 |
enum { DEF_PAD_SMALL = 5 }; |
| 26 |
|
| 27 |
enum { TM_YEAR_BASE = 1900 }; |
| 28 |
|
| 29 |
//TODO: This whole example is not very good and needs review. |
| 30 |
class CalendarExample : public Gtk::Window |
| 31 |
{ |
| 32 |
|
| 33 |
public: |
| 34 |
CalendarExample(); |
| 35 |
~CalendarExample() override; |
| 36 |
|
| 37 |
void set_flags(); |
| 38 |
void toggle_flag(Gtk::CheckButton *toggle); |
| 39 |
|
| 40 |
void month_changed(); |
| 41 |
void day_selected(); |
| 42 |
|
| 43 |
protected: |
| 44 |
static Glib::ustring font_style_to_string(Pango::Style font_style); |
| 45 |
void on_font_button_font_set(); |
| 46 |
void on_button_close(); |
| 47 |
static void on_parsing_error(const Glib::RefPtr<const Gtk::CssSection>& section, |
| 48 |
const Glib::Error& error); |
| 49 |
|
| 50 |
static const int n_checkboxes = 3; |
| 51 |
Gtk::CheckButton* flag_checkboxes_[n_checkboxes]; |
| 52 |
bool settings_[n_checkboxes]; |
| 53 |
|
| 54 |
Glib::RefPtr<Gtk::CssProvider> css_provider_; |
| 55 |
|
| 56 |
#if HAS_FONT_DIALOG_BUTTON |
| 57 |
Gtk::FontDialogButton* font_button_; |
| 58 |
#else |
| 59 |
Gtk::FontButton* font_button_; |
| 60 |
#endif |
| 61 |
Gtk::Calendar* calendar_; |
| 62 |
Gtk::Label* label_selected_; |
| 63 |
Gtk::Label* label_month_; |
| 64 |
|
| 65 |
Glib::DateTime get_date() const; |
| 66 |
}; |
| 67 |
|
| 68 |
CalendarExample::~CalendarExample() |
| 69 |
{ |
| 70 |
for (int i = 0; i < n_checkboxes; i++) |
| 71 |
{ |
| 72 |
delete flag_checkboxes_[i]; |
| 73 |
} |
| 74 |
|
| 75 |
delete calendar_; |
| 76 |
delete label_selected_; |
| 77 |
delete label_month_; |
| 78 |
} |
| 79 |
|
| 80 |
/* |
| 81 |
* GtkCalendar |
| 82 |
*/ |
| 83 |
|
| 84 |
void CalendarExample::month_changed() |
| 85 |
{ |
| 86 |
label_month_->set_text(get_date().format("%x")); |
| 87 |
} |
| 88 |
|
| 89 |
void CalendarExample::day_selected() |
| 90 |
{ |
| 91 |
label_selected_->set_text(get_date().format("%x")); |
| 92 |
} |
| 93 |
|
| 94 |
void CalendarExample::set_flags() |
| 95 |
{ |
| 96 |
if (calendar_) |
| 97 |
{ |
| 98 |
calendar_->set_show_heading(settings_[0]); |
| 99 |
calendar_->set_show_day_names(settings_[1]); |
| 100 |
calendar_->set_show_week_numbers(settings_[2]); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void CalendarExample::toggle_flag(Gtk::CheckButton *toggle) |
| 105 |
{ |
| 106 |
int j = 0; |
| 107 |
for (int i = 0; i < n_checkboxes; i++) |
| 108 |
if (flag_checkboxes_[i] == (Gtk::CheckButton *)toggle) |
| 109 |
j = i; |
| 110 |
|
| 111 |
settings_[j] = !settings_[j]; |
| 112 |
set_flags(); |
| 113 |
} |
| 114 |
|
| 115 |
void CalendarExample::on_font_button_font_set() |
| 116 |
{ |
| 117 |
const Pango::FontDescription font_desc = font_button_->get_font_desc(); |
| 118 |
const auto font_family = font_desc.get_family(); |
| 119 |
const auto font_size = font_desc.get_size(); |
| 120 |
const auto font_style = font_desc.get_style(); |
| 121 |
const auto font_weight = font_desc.get_weight(); |
| 122 |
// See https://www.w3.org/TR/css-fonts-3/ |
| 123 |
const Glib::ustring css = |
| 124 |
"calendar {\n" + |
| 125 |
(font_family.empty() ? "" : " font-family: " + font_family + ";\n") + |
| 126 |
(font_size == 0 ? "" : " font-size: " + std::to_string(font_size / PANGO_SCALE) + "pt;\n") + |
| 127 |
(" font-style: " + font_style_to_string(font_style) + ";\n") + |
| 128 |
(" font-weight: " + std::to_string(std::clamp(static_cast<int>(font_weight), 100, 900)) + ";\n") + |
| 129 |
"}"; |
| 130 |
|
| 131 |
css_provider_->load_from_data(css.raw()); |
| 132 |
} |
| 133 |
|
| 134 |
Glib::ustring CalendarExample::font_style_to_string(Pango::Style font_style) |
| 135 |
{ |
| 136 |
switch (font_style) |
| 137 |
{ |
| 138 |
case Pango::Style::OBLIQUE: return "oblique"; |
| 139 |
case Pango::Style::ITALIC: return "italic"; |
| 140 |
default: return "normal"; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void CalendarExample::on_button_close() |
| 145 |
{ |
| 146 |
close(); |
| 147 |
} |
| 148 |
|
| 149 |
void CalendarExample::on_parsing_error(const Glib::RefPtr<const Gtk::CssSection>& section, const Glib::Error& error) |
| 150 |
{ |
| 151 |
std::cerr << "on_parsing_error(): " << error.what() << std::endl; |
| 152 |
if (section) |
| 153 |
{ |
| 154 |
const auto file = section->get_file(); |
| 155 |
if (file) |
| 156 |
{ |
| 157 |
std::cerr << " URI = " << file->get_uri() << std::endl; |
| 158 |
} |
| 159 |
|
| 160 |
auto start_location = section->get_start_location(); |
| 161 |
auto end_location = section->get_end_location(); |
| 162 |
std::cerr << " start_line = " << start_location.get_lines()+1 |
| 163 |
<< ", end_line = " << end_location.get_lines()+1 << std::endl; |
| 164 |
std::cerr << " start_position = " << start_location.get_line_chars() |
| 165 |
<< ", end_position = " << end_location.get_line_chars() << std::endl; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
CalendarExample::CalendarExample() |
| 170 |
{ |
| 171 |
for (int i = 0; i < n_checkboxes; i++) { |
| 172 |
settings_[i] = 0; |
| 173 |
} |
| 174 |
|
| 175 |
set_resizable(false); |
| 176 |
|
| 177 |
auto vbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, DEF_PAD * 2); |
| 178 |
vbox->set_margin(5); |
| 179 |
set_child(*vbox); |
| 180 |
|
| 181 |
/* |
| 182 |
* The top part of the CalendarExample, flags and fontsel. |
| 183 |
*/ |
| 184 |
|
| 185 |
auto hbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, DEF_PAD); |
| 186 |
vbox->append(*hbox); |
| 187 |
auto hbbox = Gtk::make_managed<Gtk::Box>(); |
| 188 |
hbox->append(*hbbox); |
| 189 |
hbbox->set_margin_end(DEF_PAD); |
| 190 |
hbbox->set_spacing(5); |
| 191 |
|
| 192 |
/* Calendar widget */ |
| 193 |
auto frame = Gtk::make_managed<Gtk::Frame>("Calendar"); |
| 194 |
hbbox->append(*frame); |
| 195 |
frame->set_margin_start(DEF_PAD); |
| 196 |
frame->set_margin_end(DEF_PAD); |
| 197 |
frame->set_hexpand(); |
| 198 |
frame->set_halign(Gtk::Align::CENTER); |
| 199 |
calendar_ = new Gtk::Calendar(); |
| 200 |
set_flags(); |
| 201 |
calendar_->mark_day(19); |
| 202 |
frame->set_child(*calendar_); |
| 203 |
calendar_->signal_prev_month().connect(sigc::mem_fun(*this, &CalendarExample::month_changed)); |
| 204 |
calendar_->signal_next_month().connect(sigc::mem_fun(*this, &CalendarExample::month_changed)); |
| 205 |
calendar_->signal_day_selected().connect(sigc::mem_fun(*this, &CalendarExample::day_selected)); |
| 206 |
|
| 207 |
auto separator = Gtk::make_managed<Gtk::Separator>(Gtk::Orientation::VERTICAL); |
| 208 |
hbox->append(*separator); |
| 209 |
|
| 210 |
auto vbox2 = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, DEF_PAD); |
| 211 |
hbox->append(*vbox2); |
| 212 |
vbox2->set_margin_start(DEF_PAD); |
| 213 |
vbox2->set_margin_end(DEF_PAD); |
| 214 |
|
| 215 |
/* Build the Right frame with the flags in */ |
| 216 |
|
| 217 |
auto frameFlags = Gtk::make_managed<Gtk::Frame>("Flags"); |
| 218 |
vbox2->append(*frameFlags); |
| 219 |
frameFlags->set_margin_end(DEF_PAD); |
| 220 |
auto vbox3 = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, DEF_PAD_SMALL); |
| 221 |
frameFlags->set_child(*vbox3); |
| 222 |
|
| 223 |
struct { |
| 224 |
const char* label; |
| 225 |
} flags[n_checkboxes] = |
| 226 |
{ |
| 227 |
{ "Show Heading" }, |
| 228 |
{ "Show Day Names" }, |
| 229 |
{ "Show Week Numbers" }, |
| 230 |
}; |
| 231 |
|
| 232 |
for (int i = 0; i < n_checkboxes; i++) |
| 233 |
{ |
| 234 |
auto toggle = new Gtk::CheckButton(flags[i].label); |
| 235 |
toggle->signal_toggled().connect(sigc::bind(sigc::mem_fun(*this, &CalendarExample::toggle_flag), toggle)); |
| 236 |
vbox3->append(*toggle); |
| 237 |
flag_checkboxes_[i] = toggle; |
| 238 |
} |
| 239 |
|
| 240 |
/* Build the right font-button */ |
| 241 |
#if HAS_FONT_DIALOG_BUTTON |
| 242 |
auto font_dialog = Gtk::FontDialog::create(); |
| 243 |
font_button_ = Gtk::make_managed<Gtk::FontDialogButton>(font_dialog); |
| 244 |
font_button_->property_font_desc().signal_changed().connect( |
| 245 |
sigc::mem_fun(*this, &CalendarExample::on_font_button_font_set)); |
| 246 |
#else |
| 247 |
font_button_ = Gtk::make_managed<Gtk::FontButton>(); |
| 248 |
font_button_->signal_font_set().connect( |
| 249 |
sigc::mem_fun(*this, &CalendarExample::on_font_button_font_set)); |
| 250 |
#endif |
| 251 |
vbox2->append(*font_button_); |
| 252 |
|
| 253 |
// Add a StyleProvider to the Gtk::Calendar, so we can change the font. |
| 254 |
css_provider_ = Gtk::CssProvider::create(); |
| 255 |
#if HAS_STYLE_PROVIDER_ADD_PROVIDER_FOR_DISPLAY |
| 256 |
Gtk::StyleProvider::add_provider_for_display( |
| 257 |
calendar_->get_display(), css_provider_, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
| 258 |
#else |
| 259 |
Gtk::StyleContext::add_provider_for_display( |
| 260 |
calendar_->get_display(), css_provider_, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
| 261 |
#endif |
| 262 |
css_provider_->signal_parsing_error().connect( |
| 263 |
[](const auto& section, const auto& error) |
| 264 |
{ on_parsing_error(section, error); } |
| 265 |
); |
| 266 |
|
| 267 |
// Set initial font. |
| 268 |
on_font_button_font_set(); |
| 269 |
|
| 270 |
/* |
| 271 |
* Build the Signal-event part. |
| 272 |
*/ |
| 273 |
frame = Gtk::make_managed<Gtk::Frame>("Signal events"); |
| 274 |
vbox->append(*frame); |
| 275 |
vbox2 = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, DEF_PAD_SMALL); |
| 276 |
frame->set_child(*vbox2); |
| 277 |
|
| 278 |
hbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 5); |
| 279 |
vbox2->append(*hbox); |
| 280 |
auto label = Gtk::make_managed<Gtk::Label>("Day selected:"); |
| 281 |
hbox->append(*label); |
| 282 |
label_selected_ = new Gtk::Label(""); |
| 283 |
hbox->append(*label_selected_); |
| 284 |
|
| 285 |
hbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 5); |
| 286 |
vbox2->append(*hbox); |
| 287 |
label = Gtk::make_managed<Gtk::Label>("Month change:"); |
| 288 |
hbox->append(*label); |
| 289 |
label_month_ = new Gtk::Label(""); |
| 290 |
hbox->append(*label_month_); |
| 291 |
|
| 292 |
auto bbox = Gtk::make_managed<Gtk::Box>(); |
| 293 |
vbox->append(*bbox); |
| 294 |
|
| 295 |
auto button = Gtk::make_managed<Gtk::Button>("Close"); |
| 296 |
button->signal_clicked().connect( |
| 297 |
sigc::mem_fun(*this, &CalendarExample::on_button_close)); |
| 298 |
bbox->append(*button); |
| 299 |
button->set_hexpand(); |
| 300 |
button->set_halign(Gtk::Align::END); |
| 301 |
set_default_widget(*button); |
| 302 |
} |
| 303 |
|
| 304 |
Glib::DateTime CalendarExample::get_date() const |
| 305 |
{ |
| 306 |
return calendar_->get_date(); |
| 307 |
} |
| 308 |
|
| 309 |
int main(int argc, char** argv) |
| 310 |
{ |
| 311 |
auto app = Gtk::Application::create("org.gtkmm.example"); |
| 312 |
return app->make_window_and_run<CalendarExample>(argc, argv); |
| 313 |
} |