GToolBar.cpp 2.4 KB

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