Serenity Operating System
at portability 117 lines 4.3 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 <LibGUI/BoxLayout.h> 28#include <LibGUI/Button.h> 29#include <LibGUI/ColorPicker.h> 30#include <LibGUI/Frame.h> 31#include <LibGUI/SpinBox.h> 32#include <LibGUI/Widget.h> 33#include <LibGfx/Palette.h> 34 35namespace GUI { 36 37ColorPicker::ColorPicker(Color color, Core::Object* parent) 38 : Dialog(parent) 39 , m_color(color) 40{ 41 set_title("Edit Color"); 42 build(); 43} 44 45ColorPicker::~ColorPicker() 46{ 47} 48 49void ColorPicker::build() 50{ 51 auto horizontal_container = Widget::construct(); 52 horizontal_container->set_fill_with_background_color(true); 53 horizontal_container->set_layout(make<HorizontalBoxLayout>()); 54 horizontal_container->layout()->set_margins({ 4, 4, 4, 4 }); 55 set_main_widget(horizontal_container); 56 57 auto left_vertical_container = horizontal_container->add<Widget>(); 58 left_vertical_container->set_layout(make<VerticalBoxLayout>()); 59 60 auto right_vertical_container = horizontal_container->add<Widget>(); 61 right_vertical_container->set_layout(make<VerticalBoxLayout>()); 62 63 enum RGBComponent { 64 Red, 65 Green, 66 Blue 67 }; 68 69 m_preview_widget = right_vertical_container->add<Frame>(); 70 auto pal = m_preview_widget->palette(); 71 pal.set_color(ColorRole::Background, m_color); 72 m_preview_widget->set_fill_with_background_color(true); 73 m_preview_widget->set_palette(pal); 74 right_vertical_container->layout()->add_spacer(); 75 auto cancel_button = right_vertical_container->add<Button>("Cancel"); 76 cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 77 cancel_button->set_preferred_size(0, 20); 78 cancel_button->on_click = [&](auto&) { 79 done(Dialog::ExecCancel); 80 }; 81 auto ok_button = right_vertical_container->add<Button>("Okay"); 82 ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 83 ok_button->set_preferred_size(0, 20); 84 ok_button->on_click = [&](auto&) { 85 done(Dialog::ExecOK); 86 }; 87 88 auto make_spinbox = [&](RGBComponent component, int initial_value) { 89 auto spinbox = left_vertical_container->add<SpinBox>(); 90 spinbox->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); 91 spinbox->set_preferred_size(0, 20); 92 spinbox->set_min(0); 93 spinbox->set_max(255); 94 spinbox->set_value(initial_value); 95 96 spinbox->on_change = [this, component](auto value) { 97 if (component == Red) 98 m_color.set_red(value); 99 if (component == Green) 100 m_color.set_green(value); 101 if (component == Blue) 102 m_color.set_blue(value); 103 104 auto pal = m_preview_widget->palette(); 105 pal.set_color(ColorRole::Background, m_color); 106 m_preview_widget->set_palette(pal); 107 m_preview_widget->update(); 108 }; 109 return spinbox; 110 }; 111 112 make_spinbox(Red, m_color.red()); 113 make_spinbox(Green, m_color.green()); 114 make_spinbox(Blue, m_color.blue()); 115} 116 117}