button.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef _UI_BUTTON_H_
  2. #define _UI_BUTTON_H_
  3. #include <gtkmm.h>
  4. namespace ui
  5. {
  6. class ButtonWidget: public Gtk::Button
  7. {
  8. void (*on_click)() = nullptr;
  9. public:
  10. ButtonWidget(char const * const text,
  11. char const * const icon,
  12. void(*on_click_fn)()):
  13. Gtk::Button(),
  14. on_click(on_click_fn)
  15. {
  16. auto box = Gtk::manage(new Gtk::Box(Gtk::Orientation::HORIZONTAL, 4));
  17. box->set_halign(Gtk::Align::CENTER);
  18. if(icon != nullptr && icon[0] != 0) {
  19. auto image = Gtk::manage(new Gtk::Image());
  20. image->set_from_icon_name(icon);
  21. box->append(*image);
  22. }
  23. if(text != nullptr && text[0] != 0) {
  24. auto label = Gtk::manage(new Gtk::Label(text));
  25. box->append(*label);
  26. }
  27. this->set_child(*box);
  28. }
  29. void on_clicked()
  30. {
  31. if(on_click != nullptr) {
  32. on_click();
  33. }
  34. }
  35. };
  36. ButtonWidget & Button(char const * const text,
  37. char const * const icon,
  38. void(*on_click_fn)())
  39. {
  40. auto button = Gtk::manage(new ButtonWidget(text, icon, on_click_fn));
  41. return *button;
  42. }
  43. }
  44. #endif