GToolBar.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <LibGUI/GAction.h>
  2. #include <LibGUI/GActionGroup.h>
  3. #include <LibGUI/GBoxLayout.h>
  4. #include <LibGUI/GButton.h>
  5. #include <LibGUI/GPainter.h>
  6. #include <LibGUI/GToolBar.h>
  7. GToolBar::GToolBar(GWidget* parent)
  8. : GToolBar(Orientation::Horizontal, 16, parent)
  9. {
  10. }
  11. GToolBar::GToolBar(Orientation orientation, int button_size, GWidget* parent)
  12. : GWidget(parent)
  13. , m_button_size(button_size)
  14. {
  15. if (orientation == Orientation::Horizontal) {
  16. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  17. set_preferred_size(0, button_size + 12);
  18. } else {
  19. set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  20. set_preferred_size(button_size + 12, 0);
  21. }
  22. set_layout(make<GBoxLayout>(orientation));
  23. layout()->set_spacing(0);
  24. layout()->set_margins({ 2, 2, 2, 2 });
  25. }
  26. GToolBar::~GToolBar()
  27. {
  28. }
  29. void GToolBar::add_action(GAction& action)
  30. {
  31. auto item = make<Item>();
  32. item->type = Item::Action;
  33. item->action = action;
  34. auto button = GButton::construct(this);
  35. if (action.group() && action.group()->is_exclusive())
  36. button->set_exclusive(true);
  37. button->set_action(*item->action);
  38. button->set_tooltip(item->action->text());
  39. if (item->action->icon())
  40. button->set_icon(item->action->icon());
  41. else
  42. button->set_text(item->action->text());
  43. button->set_button_style(ButtonStyle::CoolBar);
  44. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  45. ASSERT(button->size_policy(Orientation::Horizontal) == SizePolicy::Fixed);
  46. ASSERT(button->size_policy(Orientation::Vertical) == SizePolicy::Fixed);
  47. button->set_preferred_size(m_button_size + 8, m_button_size + 8);
  48. m_items.append(move(item));
  49. }
  50. class SeparatorWidget final : public GWidget {
  51. C_OBJECT(SeparatorWidget)
  52. public:
  53. SeparatorWidget(GWidget* parent)
  54. : GWidget(parent)
  55. {
  56. set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  57. set_preferred_size(8, 22);
  58. }
  59. virtual ~SeparatorWidget() override {}
  60. virtual void paint_event(GPaintEvent& event) override
  61. {
  62. GPainter painter(*this);
  63. painter.add_clip_rect(event.rect());
  64. painter.translate(rect().center().x() - 1, 0);
  65. painter.draw_line({ 0, 0 }, { 0, rect().bottom() }, SystemColor::ThreedShadow1);
  66. painter.draw_line({ 1, 0 }, { 1, rect().bottom() }, SystemColor::ThreedHighlight);
  67. }
  68. };
  69. void GToolBar::add_separator()
  70. {
  71. auto item = make<Item>();
  72. item->type = Item::Separator;
  73. new SeparatorWidget(this);
  74. m_items.append(move(item));
  75. }
  76. void GToolBar::paint_event(GPaintEvent& event)
  77. {
  78. GPainter painter(*this);
  79. painter.add_clip_rect(event.rect());
  80. if (m_has_frame)
  81. StylePainter::paint_surface(painter, rect(), x() != 0, y() != 0);
  82. else
  83. painter.fill_rect(event.rect(), background_color());
  84. }