Serenity Operating System
at hosted 120 lines 4.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <LibGfx/Palette.h> 28#include <LibGUI/Action.h> 29#include <LibGUI/ActionGroup.h> 30#include <LibGUI/BoxLayout.h> 31#include <LibGUI/Button.h> 32#include <LibGUI/Painter.h> 33#include <LibGUI/ToolBar.h> 34 35namespace GUI { 36 37ToolBar::ToolBar(Orientation orientation, int button_size) 38 : m_button_size(button_size) 39{ 40 if (orientation == Orientation::Horizontal) { 41 set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 42 set_preferred_size(0, button_size + 12); 43 } else { 44 set_size_policy(SizePolicy::Fixed, SizePolicy::Fill); 45 set_preferred_size(button_size + 12, 0); 46 } 47 set_layout<BoxLayout>(orientation); 48 layout()->set_spacing(0); 49 layout()->set_margins({ 2, 2, 2, 2 }); 50} 51 52ToolBar::~ToolBar() 53{ 54} 55 56void ToolBar::add_action(Action& action) 57{ 58 auto item = make<Item>(); 59 item->type = Item::Type::Action; 60 item->action = action; 61 62 auto& button = add<Button>(); 63 if (action.group() && action.group()->is_exclusive()) 64 button.set_exclusive(true); 65 button.set_action(*item->action); 66 button.set_tooltip(item->action->text()); 67 if (item->action->icon()) 68 button.set_icon(item->action->icon()); 69 else 70 button.set_text(item->action->text()); 71 72 button.set_button_style(Gfx::ButtonStyle::CoolBar); 73 button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); 74 ASSERT(button.size_policy(Orientation::Horizontal) == SizePolicy::Fixed); 75 ASSERT(button.size_policy(Orientation::Vertical) == SizePolicy::Fixed); 76 button.set_preferred_size(m_button_size + 8, m_button_size + 8); 77 78 m_items.append(move(item)); 79} 80 81class SeparatorWidget final : public Widget { 82 C_OBJECT(SeparatorWidget) 83public: 84 SeparatorWidget() 85 { 86 set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); 87 set_preferred_size(8, 22); 88 } 89 virtual ~SeparatorWidget() override {} 90 91 virtual void paint_event(PaintEvent& event) override 92 { 93 Painter painter(*this); 94 painter.add_clip_rect(event.rect()); 95 painter.translate(rect().center().x() - 1, 0); 96 painter.draw_line({ 0, 0 }, { 0, rect().bottom() }, palette().threed_shadow1()); 97 painter.draw_line({ 1, 0 }, { 1, rect().bottom() }, palette().threed_highlight()); 98 } 99}; 100 101void ToolBar::add_separator() 102{ 103 auto item = make<Item>(); 104 item->type = Item::Type::Separator; 105 add<SeparatorWidget>(); 106 m_items.append(move(item)); 107} 108 109void ToolBar::paint_event(PaintEvent& event) 110{ 111 Painter painter(*this); 112 painter.add_clip_rect(event.rect()); 113 114 if (m_has_frame) 115 Gfx::StylePainter::paint_surface(painter, rect(), palette(), x() != 0, y() != 0); 116 else 117 painter.fill_rect(event.rect(), palette().button()); 118} 119 120}