GToolBar.cpp 2.8 KB

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