Snippets
Created by
Glus
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 | #include <gtkmm.h>
#include <libintl.h>
#include <glibmm/i18n.h>
#include <iostream>
#include <map>
#include <vector>
// Window Properties
enum class WindowProperty
{
Title,
Deletable,
Resizable,
BorderWidth,
Position,
Modal,
Decorated,
Spacing,
ButtonsSpacing,
ActionButtonsLayout
};
// Buttons structures
enum class ButtonId
{
Ok,
Cancel,
Close,
Yes,
No,
Apply
};
struct ButtonInfo
{
ButtonInfo() {}
ButtonInfo (
const Glib::ustring _label,
const gint _response_id)
:
label (_label),
response_id (_response_id)
{}
Glib::ustring label;
gint response_id;
};
Glib::ustring search_replace (
const Glib::ustring source, const Glib::ustring to_replace, const Glib::ustring replace_with,
const gint offset = 0, const gint total_replaces = 0)
{
gint total_replaced = 0;
Glib::ustring::size_type pos = offset;
Glib::ustring replaced = source;
while (
((pos = source.find (to_replace, pos)) < Glib::ustring::npos) && ((total_replaces == 0) ||
(total_replaced++ < total_replaces)))
{
replaced.replace (pos, to_replace.length(), replace_with);
pos += replace_with.size();
}
return replaced;
}
bool get_bool_by_ustring (
const Glib::ustring _us_value)
{
return
(_us_value == "true");
}
gint get_gint_by_ustring (
const Glib::ustring _us_value)
{
return
atoi (_us_value.c_str());
}
Gtk::WindowPosition get_position_by_ustring (
const Glib::ustring _us_value)
{
Gtk::WindowPosition window_position = Gtk::WIN_POS_NONE;
if (_us_value.casefold() == "center")
window_position = Gtk::WIN_POS_CENTER;
if (_us_value.casefold() == "mouse")
window_position = Gtk::WIN_POS_MOUSE;
if (_us_value.casefold() == "center-always")
window_position = Gtk::WIN_POS_CENTER_ALWAYS;
if (_us_value.casefold() == "center-on-parent")
window_position = Gtk::WIN_POS_CENTER_ON_PARENT;
return window_position;
}
Gtk::ButtonBoxStyle get_action_buttons_layout_by_ustring (
const Glib::ustring layout_ustring)
{
Gtk::ButtonBoxStyle layout_style = Gtk::BUTTONBOX_END;
if (layout_ustring == "spread")
layout_style = Gtk::BUTTONBOX_SPREAD;
if (layout_ustring == "edge")
layout_style = Gtk::BUTTONBOX_EDGE;
if (layout_ustring == "start")
layout_style = Gtk::BUTTONBOX_START;
if (layout_ustring == "center")
layout_style = Gtk::BUTTONBOX_CENTER;
if (layout_ustring == "expand")
layout_style = Gtk::BUTTONBOX_EXPAND;
return layout_style;
}
class WindowBase
:
public Gtk::Window
{
public:
WindowBase (
std::map<WindowProperty, Glib::ustring> properties = {})
:
dialog_vertical_box (Gtk::ORIENTATION_VERTICAL),
vb_content_area (Gtk::ORIENTATION_VERTICAL)
{
set_title (
(properties[WindowProperty::Title].empty())
? _("Title")
: properties[WindowProperty::Title]);
set_deletable (
(properties[WindowProperty::Deletable].empty())
? true
: get_bool_by_ustring (properties[WindowProperty::Deletable]));
set_resizable (
(properties[WindowProperty::Resizable].empty())
? false
: get_bool_by_ustring (properties[WindowProperty::Resizable]));
set_border_width (
(properties[WindowProperty::BorderWidth].empty())
? 10
: get_gint_by_ustring (properties[WindowProperty::BorderWidth]));
set_position (
(properties[WindowProperty::Position].empty())
? Gtk::WIN_POS_CENTER_ALWAYS
: get_position_by_ustring (properties[WindowProperty::Position]));
set_modal (
(properties[WindowProperty::Modal].empty())
? true
: get_bool_by_ustring (properties[WindowProperty::Modal]));
set_decorated (
(properties[WindowProperty::Decorated].empty())
? true
: get_bool_by_ustring (properties[WindowProperty::Decorated]));
bb_action_area.set_spacing (
(properties[WindowProperty::ButtonsSpacing].empty())
? 5
: get_gint_by_ustring (properties[WindowProperty::ButtonsSpacing]));
bb_action_area.set_layout (
(properties[WindowProperty::ActionButtonsLayout].empty())
? Gtk::BUTTONBOX_END
: get_action_buttons_layout_by_ustring (properties[WindowProperty::ActionButtonsLayout]));
dialog_vertical_box.set_spacing (
(properties[WindowProperty::Spacing].empty())
? 0
: get_gint_by_ustring (properties[WindowProperty::Spacing]));
dialog_vertical_box.pack_start (vb_content_area);
dialog_vertical_box.pack_start (bb_action_area, Gtk::PACK_SHRINK);
add (dialog_vertical_box);
button_info.insert (std::make_pair (ButtonId::Ok, ButtonInfo (_("Ok"), Gtk::RESPONSE_OK)));
button_info.insert (std::make_pair (ButtonId::Cancel, ButtonInfo (_("Cancel"), Gtk::RESPONSE_CANCEL)));
button_info.insert (std::make_pair (ButtonId::Close, ButtonInfo (_("Close"), Gtk::RESPONSE_CLOSE)));
button_info.insert (std::make_pair (ButtonId::Yes, ButtonInfo (_("Yes"), Gtk::RESPONSE_YES)));
button_info.insert (std::make_pair (ButtonId::No, ButtonInfo (_("No"), Gtk::RESPONSE_NO)));
button_info.insert (std::make_pair (ButtonId::Apply, ButtonInfo (_("Apply"), Gtk::RESPONSE_APPLY)));
}
virtual ~WindowBase(){}
protected:
Gtk::Box * get_content_area()
{
return &vb_content_area;
}
Gtk::Button * add_button (
const Glib::ustring button_label, const gint button_response_id)
{
vb_content_area.set_margin_bottom (10);
action_area_buttons.insert (std::make_pair (button_response_id, new Gtk::Button (button_label)));
bb_action_area.pack_start (*action_area_buttons[button_response_id].button);
action_area_buttons[button_response_id].button->set_can_default();
return action_area_buttons[button_response_id].button;
}
Gtk::Button * add_button (
ButtonId button_id)
{
return
add_button (button_info[button_id].label, button_info[button_id].response_id);
}
gint run()
{
dialog_response_id.set (false, 0);
for (auto &button_in_box : action_area_buttons)
{
if (button_in_box.second.button_signal.empty())
{
button_in_box.second.button->signal_clicked().connect (sigc::bind<gint> (
sigc::mem_fun (*this, &WindowBase::get_button_response_id), button_in_box.first));
}
}
while (!dialog_response_id.stored)
{
show_now();
Glib::MainContext::get_default()->iteration (true);
}
for (auto &button_in_box : action_area_buttons)
{
if (button_in_box.second.button_signal.connected())
button_in_box.second.button_signal.disconnect();
}
return dialog_response_id.response_id;
}
void get_button_response_id (
const gint response_id)
{
dialog_response_id.set (true, response_id);
}
Gtk::Button * get_button_by_response_id (
const gint response_id)
{
return action_area_buttons[response_id].button;
}
void set_default_response (
const gint response_id)
{
if (action_area_buttons[response_id].button)
{
action_area_buttons[response_id].button->set_can_default();
action_area_buttons[response_id].button->grab_default();
}
}
void set_response_sensitive (
const gint response_id, const bool sensitivity)
{
if (action_area_buttons[response_id].button)
action_area_buttons[response_id].button->set_sensitive (sensitivity);
}
Glib::ustring title;
bool deletable;
bool resizable;
gint border_width;
gint spacing;
Gtk::WindowPosition position;
bool modal;
bool decorated;
struct DialogResponseId
{
DialogResponseId() {set();}
void set (
const bool _stored = false,
const gint _response_id = 0)
{
stored = _stored;
response_id = _response_id;
}
bool stored;
gint response_id;
}
dialog_response_id;
// Gtkmm widgets...
Gtk::Box dialog_vertical_box;
Gtk::Grid grid;
Gtk::Box vb_content_area;
struct ButtonInBox
{
ButtonInBox() {}
ButtonInBox (
Gtk::Button * _button)
:
button (_button)
{}
Gtk::Button * button;
sigc::connection button_signal;
};
Gtk::ButtonBox bb_action_area;
std::map<gint, ButtonInBox> action_area_buttons;
std::map<ButtonId, ButtonInfo> button_info;
};
class ErasableEntry
:
public Gtk::Entry
{
public:
ErasableEntry()
:
icon_visibility (false),
icon_visible (false)
{
/*
* Hey.... Expand Property on ALL ErasableEntry Objects !!!!
*/
set_hexpand();
// ^^^^^^^^^^^^^^^^^^^
signal_changed().connect (
sigc::mem_fun (*this, &ErasableEntry::on_changed));
signal_icon_press().connect (
sigc::mem_fun (*this, &ErasableEntry::on_icon_pressed));
property_editable().signal_changed().connect (
sigc::mem_fun (*this, &ErasableEntry::on_changed));
}
void set_icon_name (const Glib::ustring _icon_name)
{
icon_name = _icon_name;
icon_visibility = !icon_name.empty();
}
Gtk::EntryIconPosition set_icon_visible (const bool _icon_visible = true)
{
icon_visible = (icon_visibility) ? _icon_visible : false;
on_changed();
return icon_position;
}
private:
void on_changed()
{
gint entry_position = get_position();
Glib::ustring clear_icon_name = "";
switch (get_pango_context()->get_base_dir())
{
case (Pango::DIRECTION_LTR):
icon_position = Gtk::ENTRY_ICON_PRIMARY;
clear_icon_position = Gtk::ENTRY_ICON_SECONDARY;
if (get_editable() && !get_text().empty())
clear_icon_name = "edit-clear-symbolic";
break;
case (Pango::DIRECTION_RTL):
clear_icon_position = Gtk::ENTRY_ICON_PRIMARY;
icon_position = Gtk::ENTRY_ICON_SECONDARY;
if (get_editable() && !get_text().empty())
clear_icon_name = "edit-clear-symbolic-rtl";
break;
}
set_icon_from_icon_name (clear_icon_name, clear_icon_position);
if (icon_visible)
set_icon_from_icon_name (icon_name, icon_position);
else
unset_icon (icon_position);
if (get_editable())
{
grab_focus();
set_position (entry_position);
}
}
void on_icon_pressed (
Gtk::EntryIconPosition _icon_position, const GdkEventButton*)
{
if (_icon_position == clear_icon_position)
{
if (get_editable())
set_text ("");
}
}
bool icon_visibility;
Glib::ustring icon_name;
bool icon_visible;
Gtk::EntryIconPosition icon_position;
Gtk::EntryIconPosition clear_icon_position;
};
class SimpleEntryItem
{
public:
SimpleEntryItem()
:
label (new Gtk::Label()),
code_entry (new Gtk::Entry()),
value_entry (new ErasableEntry()),
show_code_entry (false),
secrets_value_entry (false)
{
label->set_halign (Gtk::ALIGN_END);
code_entry->set_halign (Gtk::ALIGN_START);
value_entry->set_halign (Gtk::ALIGN_START);
/*
* "value_entry" is an ErasableEntry object...
* set_hexpand()... again...
*/
value_entry->set_hexpand();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
code_entry->signal_changed().connect (
sigc::mem_fun (*this, &SimpleEntryItem::normalize_text));
}
Gtk::Label *
get_label() const
{
return label;
}
Gtk::Entry *
get_code_entry() const
{
return code_entry;
}
void
set_code_entry_length (
const gint _code_entry_length)
{
show_code_entry = (_code_entry_length > 0);
code_entry->set_width_chars (_code_entry_length);
code_entry->set_max_length (_code_entry_length);
}
bool
get_show_code_entry() const
{
return show_code_entry;
}
ErasableEntry *
get_value_entry() const
{
return value_entry;
}
void
set_secrets_value_entry (
const bool _secrets_value_entry)
{
secrets_value_entry = _secrets_value_entry;
}
void
set_default_value_entry_visibility (
const bool _default_value_entry_visibility)
{
set_value_entry_visibility (_default_value_entry_visibility);
}
void
set_value_entry_visibility (
const bool _value_entry_visibility)
{
value_entry_visibility = _value_entry_visibility;
if (secrets_value_entry)
{
value_entry->set_icon_name ((value_entry_visibility)
? "visibility-off" : "visibility");
visibility_icon_position = value_entry->set_icon_visible();
value_entry->set_visibility (value_entry_visibility);
}
}
void
switch_value_entry_visibility()
{
set_value_entry_visibility (!value_entry_visibility);
}
void
normalize_text()
{
gint entry_position = code_entry->get_position();
for (auto & char_it : set_chars)
code_entry->set_text (
search_replace (code_entry->get_text(),
char_it.first, char_it.second));
code_entry->set_position (entry_position);
}
void
set_normalize_chars (
const std::vector<std::pair<Glib::ustring, Glib::ustring>> _set_chars)
{
set_chars = _set_chars;
}
Gtk::EntryIconPosition
get_visibility_icon_position() const
{
return visibility_icon_position;
}
private:
bool show_code_entry;
bool secrets_value_entry;
std::vector<std::pair<Glib::ustring, Glib::ustring>> set_chars;
bool value_entry_visibility;
// Gtkmm widgets...
Gtk::Label * label;
Gtk::Entry * code_entry;
ErasableEntry * value_entry;
Gtk::EntryIconPosition visibility_icon_position;
};
class SimpleEntryDialog
:
public WindowBase
{
public:
SimpleEntryDialog()
:
WindowBase ({
{WindowProperty::Resizable, "true"}})
{
grid.set_column_spacing (5);
grid.set_row_spacing (5);
grid.set_column_homogeneous (false);
get_content_area()->pack_start (grid);
add_button (ButtonId::Cancel);
add_button (ButtonId::Ok)->grab_default();
}
virtual ~SimpleEntryDialog(){}
gint add_label (
const Glib::ustring _label_text)
{
SimpleEntryItem * simple_entry_item = new SimpleEntryItem;
simple_entry_item->get_label()->set_markup (_label_text);
simple_entry_items.push_back (simple_entry_item);
return simple_entry_items.size()-1;
}
void set_code_entry (
const gint entries_item_number,
const Glib::ustring _default_code_entry_text,
const gint _code_entry_text_length)
{
if (entries_item_number < simple_entry_items.size())
{
simple_entry_items[entries_item_number]
->get_code_entry()->set_text (_default_code_entry_text);
simple_entry_items[entries_item_number]
->set_code_entry_length (_code_entry_text_length);
}
}
void set_normalize_chars (
const gint entries_item_number,
std::vector<std::pair<Glib::ustring, Glib::ustring>> _normalize_chars)
{
if (entries_item_number < simple_entry_items.size())
{
simple_entry_items[entries_item_number]
->set_normalize_chars (_normalize_chars);
}
}
void set_value_entry (
const gint entries_item_number,
const Glib::ustring _default_value_entry_text,
const bool _value_entry_text_editable,
const bool _secrets_value_entry,
const bool _default_visibility)
{
if (entries_item_number < simple_entry_items.size())
{
simple_entry_items[entries_item_number]
->get_value_entry()->set_text (_default_value_entry_text);
simple_entry_items[entries_item_number]
->get_value_entry()->set_editable (_value_entry_text_editable);
simple_entry_items[entries_item_number]
->set_secrets_value_entry (_secrets_value_entry);
simple_entry_items[entries_item_number]
->set_default_value_entry_visibility (_default_visibility);
}
}
gint show_dialog()
{
compose();
show_all_children();
gint result = run();
hide();
return result;
}
std::vector<std::pair<Glib::ustring, Glib::ustring>> get_values()
{
std::vector<std::pair<Glib::ustring, Glib::ustring>> values;
for (auto & entries_item : simple_entry_items)
{
values.push_back (std::pair<Glib::ustring, Glib::ustring> (
entries_item->get_code_entry()->get_text(), entries_item->get_value_entry()->get_text()));
}
return values;
}
private:
void compose()
{
for (auto & child_widget : grid.get_children())
grid.remove (*child_widget);
gint focus_grab_to = (-1);
for (gint i = 0; i < simple_entry_items.size(); i++)
{
grid.attach (*simple_entry_items[i]->get_label(), 0, i, 1, 1);
if (focus_grab_to == (-1) && simple_entry_items[i]->get_value_entry()->get_editable())
focus_grab_to = i;
if (simple_entry_items[i]->get_show_code_entry())
{
grid.attach (*simple_entry_items[i]->get_code_entry(), 1, i, 1, 1);
grid.attach (*simple_entry_items[i]->get_value_entry(), 2, i, 1, 1);
}
else
{
grid.attach (*simple_entry_items[i]->get_value_entry(), 1, i, 2, 1);
}
simple_entry_items[i]->get_value_entry()->signal_icon_press().connect (
sigc::bind<gint> (sigc::mem_fun (*this, &SimpleEntryDialog::change_entry_visibility), i));
if (key_press_signal.empty())
{
simple_entry_items[i]->get_value_entry()->set_events (Gdk::KEY_PRESS_MASK);
key_press_signal =
simple_entry_items[i]->get_value_entry()->signal_key_press_event().connect (
sigc::bind<gint> (sigc::mem_fun (*this, &SimpleEntryDialog::manage_keys), i));
simple_entry_items[i]->get_value_entry()->set_activates_default();
}
}
if (focus_grab_to != (-1))
{
if (simple_entry_items[focus_grab_to]->get_show_code_entry())
{
simple_entry_items[focus_grab_to]->get_code_entry()->grab_focus();
simple_entry_items[focus_grab_to]->get_code_entry()->set_position (-1);
}
else
{
simple_entry_items[focus_grab_to]->get_value_entry()->grab_focus();
simple_entry_items[focus_grab_to]->get_value_entry()->set_position (-1);
}
}
}
void change_entry_visibility (
Gtk::EntryIconPosition _entry_icon_position, const GdkEventButton *, gint item)
{
if (item < simple_entry_items.size())
{
if (_entry_icon_position == simple_entry_items[item]->get_visibility_icon_position())
{
simple_entry_items[item]->switch_value_entry_visibility();
}
}
}
bool manage_keys (GdkEventKey * ek, gint entries_row)
{
switch (ek->keyval)
{
case (GDK_KEY_Escape):
{
for (auto & entries_item : simple_entry_items)
entries_item->switch_value_entry_visibility();
simple_entry_items[entries_row]->get_value_entry()->grab_focus();
break;
}
}
return false;
}
// Gtkmm widgets...
std::vector<SimpleEntryItem *> simple_entry_items;
sigc::connection
key_press_signal;
};
class MainWindow
:
public Gtk::Window
{
public:
MainWindow()
{
SimpleEntryDialog simple_entry_dialog;
simple_entry_dialog.add_label ("Why Entry (ErasableEntry) does not expand (horizontally) when dialog resizes ? ");
simple_entry_dialog.show_dialog();
}
};
int main (int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> winexp_app = Gtk::Application::create();
MainWindow main_window;
return winexp_app->run (main_window);
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.