GToolBar.cpp 2.7 KB

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