GToolBar.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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(0);
  13. layout()->set_margins({1, 1, 1, 1});
  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. if (item->action->icon())
  26. button->set_icon(item->action->icon());
  27. else
  28. button->set_caption(item->action->text());
  29. button->on_click = [raw_action_ptr] (const GButton&) {
  30. raw_action_ptr->activate();
  31. };
  32. button->set_button_style(GButtonStyle::CoolBar);
  33. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  34. button->set_preferred_size({ 22, 22 });
  35. m_items.append(move(item));
  36. }
  37. void GToolBar::add_separator()
  38. {
  39. auto item = make<Item>();
  40. item->type = Item::Separator;
  41. m_items.append(move(item));
  42. }
  43. void GToolBar::paint_event(GPaintEvent& event)
  44. {
  45. Painter painter(*this);
  46. painter.set_clip_rect(event.rect());
  47. painter.fill_rect({ 0, 0, width(), height() - 1 }, Color::LightGray);
  48. painter.draw_line({ 0, rect().bottom() }, { width() - 1, rect().bottom() }, Color::DarkGray);
  49. }