GToolBar.cpp 2.3 KB

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