GToolBar.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <LibGUI/GToolBar.h>
  2. #include <LibGUI/GBoxLayout.h>
  3. #include <LibGUI/GButton.h>
  4. #include <LibGUI/GAction.h>
  5. #include <LibGUI/GPainter.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(Retained<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. if (item->action->icon())
  26. button->set_icon(item->action->icon());
  27. else
  28. button->set_caption(item->action->text());
  29. button->on_click = [raw_action_ptr] (const GButton&) {
  30. raw_action_ptr->activate();
  31. };
  32. button->set_button_style(ButtonStyle::CoolBar);
  33. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  34. ASSERT(button->size_policy(Orientation::Horizontal) == SizePolicy::Fixed);
  35. ASSERT(button->size_policy(Orientation::Vertical) == SizePolicy::Fixed);
  36. button->set_preferred_size({ 24, 24 });
  37. m_items.append(move(item));
  38. }
  39. class SeparatorWidget final : public GWidget {
  40. public:
  41. SeparatorWidget(GWidget* parent)
  42. : GWidget(parent)
  43. {
  44. set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  45. set_background_color(Color::White);
  46. set_preferred_size({ 8, 22 });
  47. }
  48. virtual ~SeparatorWidget() override { }
  49. virtual void paint_event(GPaintEvent& event) override
  50. {
  51. GPainter painter(*this);
  52. painter.set_clip_rect(event.rect());
  53. painter.translate(rect().center().x() - 1, 0);
  54. painter.draw_line({ 0, 0 }, { 0, rect().bottom() }, Color::MidGray);
  55. painter.draw_line({ 1, 0 }, { 1, rect().bottom() }, Color::White);
  56. }
  57. private:
  58. virtual const char* class_name() const override { return "SeparatorWidget"; }
  59. };
  60. void GToolBar::add_separator()
  61. {
  62. auto item = make<Item>();
  63. item->type = Item::Separator;
  64. new SeparatorWidget(this);
  65. m_items.append(move(item));
  66. }
  67. void GToolBar::paint_event(GPaintEvent& event)
  68. {
  69. GPainter painter(*this);
  70. painter.set_clip_rect(event.rect());
  71. StylePainter::the().paint_surface(painter, rect());
  72. }