GToolBar.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <LibGUI/GAction.h>
  2. #include <LibGUI/GBoxLayout.h>
  3. #include <LibGUI/GButton.h>
  4. #include <LibGUI/GPainter.h>
  5. #include <LibGUI/GToolBar.h>
  6. GToolBar::GToolBar(GWidget* parent)
  7. : GWidget(parent)
  8. {
  9. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  10. set_preferred_size({ 0, 28 });
  11. set_layout(make<GBoxLayout>(Orientation::Horizontal));
  12. layout()->set_spacing(0);
  13. layout()->set_margins({ 2, 2, 2, 2 });
  14. }
  15. GToolBar::~GToolBar()
  16. {
  17. }
  18. void GToolBar::add_action(NonnullRefPtr<GAction>&& action)
  19. {
  20. GAction* raw_action_ptr = action.ptr();
  21. auto item = make<Item>();
  22. item->type = Item::Action;
  23. item->action = move(action);
  24. auto* button = new GButton(this);
  25. button->set_action(*item->action);
  26. button->set_tooltip(item->action->text());
  27. if (item->action->icon())
  28. button->set_icon(item->action->icon());
  29. else
  30. button->set_text(item->action->text());
  31. button->on_click = [raw_action_ptr](const GButton&) {
  32. raw_action_ptr->activate();
  33. };
  34. button->set_button_style(ButtonStyle::CoolBar);
  35. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  36. ASSERT(button->size_policy(Orientation::Horizontal) == SizePolicy::Fixed);
  37. ASSERT(button->size_policy(Orientation::Vertical) == SizePolicy::Fixed);
  38. button->set_preferred_size({ 24, 24 });
  39. m_items.append(move(item));
  40. }
  41. class SeparatorWidget final : public GWidget {
  42. public:
  43. SeparatorWidget(GWidget* parent)
  44. : GWidget(parent)
  45. {
  46. set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  47. set_background_color(Color::White);
  48. set_preferred_size({ 8, 22 });
  49. }
  50. virtual ~SeparatorWidget() override {}
  51. virtual void paint_event(GPaintEvent& event) override
  52. {
  53. GPainter painter(*this);
  54. painter.add_clip_rect(event.rect());
  55. painter.translate(rect().center().x() - 1, 0);
  56. painter.draw_line({ 0, 0 }, { 0, rect().bottom() }, Color::MidGray);
  57. painter.draw_line({ 1, 0 }, { 1, rect().bottom() }, Color::White);
  58. }
  59. private:
  60. virtual const char* class_name() const override { return "SeparatorWidget"; }
  61. };
  62. void GToolBar::add_separator()
  63. {
  64. auto item = make<Item>();
  65. item->type = Item::Separator;
  66. new SeparatorWidget(this);
  67. m_items.append(move(item));
  68. }
  69. void GToolBar::paint_event(GPaintEvent& event)
  70. {
  71. GPainter painter(*this);
  72. painter.add_clip_rect(event.rect());
  73. if (m_has_frame)
  74. StylePainter::paint_surface(painter, rect(), x() != 0, y() != 0);
  75. else
  76. painter.fill_rect(event.rect(), Color::WarmGray);
  77. }