GToolBar.cpp 2.6 KB

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