GToolBar.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <LibGUI/GToolBar.h>
  2. #include <LibGUI/GBoxLayout.h>
  3. #include <LibGUI/GButton.h>
  4. #include <LibGUI/GAction.h>
  5. #include <SharedGraphics/Painter.h>
  6. GToolBar::GToolBar(GWidget* parent)
  7. : GWidget(parent)
  8. {
  9. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  10. set_preferred_size({ 0, 25 });
  11. set_layout(make<GBoxLayout>(Orientation::Horizontal));
  12. layout()->set_spacing(1);
  13. layout()->set_margins({1, 1, 1, 1});
  14. }
  15. GToolBar::~GToolBar()
  16. {
  17. }
  18. void GToolBar::add_action(RetainPtr<GAction>&& action)
  19. {
  20. ASSERT(action);
  21. GAction* raw_action_ptr = action.ptr();
  22. auto item = make<Item>();
  23. item->type = Item::Action;
  24. item->action = move(action);
  25. auto* button = new GButton(this);
  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(GButtonStyle::CoolBar);
  34. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  35. button->set_preferred_size({ 22, 22 });
  36. m_items.append(move(item));
  37. }
  38. void GToolBar::add_separator()
  39. {
  40. auto item = make<Item>();
  41. item->type = Item::Separator;
  42. m_items.append(move(item));
  43. }
  44. void GToolBar::paint_event(GPaintEvent& event)
  45. {
  46. Painter painter(*this);
  47. painter.set_clip_rect(event.rect());
  48. painter.fill_rect({ 0, 0, width(), height() - 1 }, Color::LightGray);
  49. painter.draw_line({ 0, rect().bottom() }, { width() - 1, rect().bottom() }, Color::DarkGray);
  50. }