mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 09:17:45 +00:00
Applications: Move to Userland/Applications/
This commit is contained in:
parent
aa939c4b4b
commit
dc28c07fa5
287 changed files with 1 additions and 1 deletions
11
Userland/Applications/Calculator/CMakeLists.txt
Normal file
11
Userland/Applications/Calculator/CMakeLists.txt
Normal file
|
@ -0,0 +1,11 @@
|
|||
compile_gml(CalculatorWindow.gml CalculatorGML.h calculator_gml)
|
||||
set(SOURCES
|
||||
main.cpp
|
||||
Calculator.cpp
|
||||
CalculatorWidget.cpp
|
||||
Keypad.cpp
|
||||
CalculatorGML.h
|
||||
)
|
||||
|
||||
serenity_app(Calculator ICON app-calculator)
|
||||
target_link_libraries(Calculator LibGUI)
|
143
Userland/Applications/Calculator/Calculator.cpp
Normal file
143
Userland/Applications/Calculator/Calculator.cpp
Normal file
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "Calculator.h"
|
||||
#include <AK/Assertions.h>
|
||||
#include <math.h>
|
||||
|
||||
Calculator::Calculator()
|
||||
{
|
||||
}
|
||||
|
||||
Calculator::~Calculator()
|
||||
{
|
||||
}
|
||||
|
||||
double Calculator::begin_operation(Operation operation, double argument)
|
||||
{
|
||||
double res = 0.0;
|
||||
|
||||
switch (operation) {
|
||||
case Operation::None:
|
||||
ASSERT_NOT_REACHED();
|
||||
|
||||
case Operation::Add:
|
||||
case Operation::Subtract:
|
||||
case Operation::Multiply:
|
||||
case Operation::Divide:
|
||||
m_saved_argument = argument;
|
||||
m_operation_in_progress = operation;
|
||||
return argument;
|
||||
|
||||
case Operation::Sqrt:
|
||||
if (argument < 0.0) {
|
||||
m_has_error = true;
|
||||
return argument;
|
||||
}
|
||||
res = sqrt(argument);
|
||||
clear_operation();
|
||||
break;
|
||||
case Operation::Inverse:
|
||||
if (argument == 0.0) {
|
||||
m_has_error = true;
|
||||
return argument;
|
||||
}
|
||||
res = 1 / argument;
|
||||
clear_operation();
|
||||
break;
|
||||
case Operation::Percent:
|
||||
res = argument * 0.01;
|
||||
break;
|
||||
case Operation::ToggleSign:
|
||||
res = -argument;
|
||||
break;
|
||||
|
||||
case Operation::MemClear:
|
||||
m_mem = 0.0;
|
||||
res = argument;
|
||||
break;
|
||||
case Operation::MemRecall:
|
||||
res = m_mem;
|
||||
break;
|
||||
case Operation::MemSave:
|
||||
m_mem = argument;
|
||||
res = argument;
|
||||
break;
|
||||
case Operation::MemAdd:
|
||||
m_mem += argument;
|
||||
res = m_mem;
|
||||
break;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
double Calculator::finish_operation(double argument)
|
||||
{
|
||||
double res = 0.0;
|
||||
|
||||
switch (m_operation_in_progress) {
|
||||
case Operation::None:
|
||||
return argument;
|
||||
|
||||
case Operation::Add:
|
||||
res = m_saved_argument + argument;
|
||||
break;
|
||||
case Operation::Subtract:
|
||||
res = m_saved_argument - argument;
|
||||
break;
|
||||
case Operation::Multiply:
|
||||
res = m_saved_argument * argument;
|
||||
break;
|
||||
case Operation::Divide:
|
||||
if (argument == 0.0) {
|
||||
m_has_error = true;
|
||||
return argument;
|
||||
}
|
||||
res = m_saved_argument / argument;
|
||||
break;
|
||||
|
||||
case Operation::Sqrt:
|
||||
case Operation::Inverse:
|
||||
case Operation::Percent:
|
||||
case Operation::ToggleSign:
|
||||
case Operation::MemClear:
|
||||
case Operation::MemRecall:
|
||||
case Operation::MemSave:
|
||||
case Operation::MemAdd:
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
clear_operation();
|
||||
return res;
|
||||
}
|
||||
|
||||
void Calculator::clear_operation()
|
||||
{
|
||||
m_operation_in_progress = Operation::None;
|
||||
m_saved_argument = 0.0;
|
||||
clear_error();
|
||||
}
|
72
Userland/Applications/Calculator/Calculator.h
Normal file
72
Userland/Applications/Calculator/Calculator.h
Normal file
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
// This type implements the regular calculator
|
||||
// behavior, such as performing arithmetic
|
||||
// operations and providing a memory cell.
|
||||
// It does not deal with number input; you
|
||||
// have to pass in already parsed double
|
||||
// values.
|
||||
|
||||
class Calculator final {
|
||||
public:
|
||||
Calculator();
|
||||
~Calculator();
|
||||
|
||||
enum class Operation {
|
||||
None,
|
||||
Add,
|
||||
Subtract,
|
||||
Multiply,
|
||||
Divide,
|
||||
|
||||
Sqrt,
|
||||
Inverse,
|
||||
Percent,
|
||||
ToggleSign,
|
||||
|
||||
MemClear,
|
||||
MemRecall,
|
||||
MemSave,
|
||||
MemAdd
|
||||
};
|
||||
|
||||
double begin_operation(Operation, double);
|
||||
double finish_operation(double);
|
||||
|
||||
bool has_error() const { return m_has_error; }
|
||||
|
||||
void clear_operation();
|
||||
void clear_error() { m_has_error = false; }
|
||||
|
||||
private:
|
||||
Operation m_operation_in_progress { Operation::None };
|
||||
double m_saved_argument { 0.0 };
|
||||
double m_mem { 0.0 };
|
||||
bool m_has_error { false };
|
||||
};
|
208
Userland/Applications/Calculator/CalculatorWidget.cpp
Normal file
208
Userland/Applications/Calculator/CalculatorWidget.cpp
Normal file
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||
* Copyright (c) 2021 Glenford Williams <gw_dev@outlook.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "CalculatorWidget.h"
|
||||
#include "Applications/Calculator/CalculatorGML.h"
|
||||
#include <AK/Assertions.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/TextBox.h>
|
||||
#include <LibGfx/Font.h>
|
||||
#include <LibGfx/FontDatabase.h>
|
||||
#include <LibGfx/Palette.h>
|
||||
|
||||
CalculatorWidget::CalculatorWidget()
|
||||
{
|
||||
load_from_gml(calculator_gml);
|
||||
|
||||
m_entry = *find_descendant_of_type_named<GUI::TextBox>("entry_textbox");
|
||||
m_entry->set_relative_rect(5, 5, 244, 26);
|
||||
m_entry->set_text_alignment(Gfx::TextAlignment::CenterRight);
|
||||
m_entry->set_font(Gfx::FontDatabase::default_fixed_width_font());
|
||||
|
||||
m_label = *find_descendant_of_type_named<GUI::Label>("label");
|
||||
|
||||
m_label->set_frame_shadow(Gfx::FrameShadow::Sunken);
|
||||
m_label->set_frame_shape(Gfx::FrameShape::Container);
|
||||
m_label->set_frame_thickness(2);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
m_digit_button[i] = *find_descendant_of_type_named<GUI::Button>(String::formatted("{}_button", i));
|
||||
add_digit_button(*m_digit_button[i], i);
|
||||
}
|
||||
|
||||
m_mem_add_button = *find_descendant_of_type_named<GUI::Button>("mem_add_button");
|
||||
add_operation_button(*m_mem_add_button, Calculator::Operation::MemAdd);
|
||||
|
||||
m_mem_save_button = *find_descendant_of_type_named<GUI::Button>("mem_save_button");
|
||||
add_operation_button(*m_mem_save_button, Calculator::Operation::MemSave);
|
||||
|
||||
m_mem_recall_button = *find_descendant_of_type_named<GUI::Button>("mem_recall_button");
|
||||
add_operation_button(*m_mem_recall_button, Calculator::Operation::MemRecall);
|
||||
|
||||
m_mem_clear_button = *find_descendant_of_type_named<GUI::Button>("mem_clear_button");
|
||||
add_operation_button(*m_mem_clear_button, Calculator::Operation::MemClear);
|
||||
|
||||
m_clear_button = *find_descendant_of_type_named<GUI::Button>("clear_button");
|
||||
m_clear_button->on_click = [this](auto) {
|
||||
m_keypad.set_value(0.0);
|
||||
m_calculator.clear_operation();
|
||||
update_display();
|
||||
};
|
||||
|
||||
m_clear_error_button = *find_descendant_of_type_named<GUI::Button>("clear_error_button");
|
||||
m_clear_error_button->on_click = [this](auto) {
|
||||
m_keypad.set_value(0.0);
|
||||
update_display();
|
||||
};
|
||||
|
||||
m_backspace_button = *find_descendant_of_type_named<GUI::Button>("backspace_button");
|
||||
m_backspace_button->on_click = [this](auto) {
|
||||
m_keypad.type_backspace();
|
||||
update_display();
|
||||
};
|
||||
|
||||
m_decimal_point_button = *find_descendant_of_type_named<GUI::Button>("decimal_button");
|
||||
m_decimal_point_button->on_click = [this](auto) {
|
||||
m_keypad.type_decimal_point();
|
||||
update_display();
|
||||
};
|
||||
|
||||
m_sign_button = *find_descendant_of_type_named<GUI::Button>("sign_button");
|
||||
add_operation_button(*m_sign_button, Calculator::Operation::ToggleSign);
|
||||
|
||||
m_add_button = *find_descendant_of_type_named<GUI::Button>("add_button");
|
||||
add_operation_button(*m_add_button, Calculator::Operation::Add);
|
||||
|
||||
m_subtract_button = *find_descendant_of_type_named<GUI::Button>("subtract_button");
|
||||
add_operation_button(*m_subtract_button, Calculator::Operation::Subtract);
|
||||
|
||||
m_multiply_button = *find_descendant_of_type_named<GUI::Button>("multiply_button");
|
||||
add_operation_button(*m_multiply_button, Calculator::Operation::Multiply);
|
||||
|
||||
m_divide_button = *find_descendant_of_type_named<GUI::Button>("divide_button");
|
||||
add_operation_button(*m_divide_button, Calculator::Operation::Divide);
|
||||
|
||||
m_sqrt_button = *find_descendant_of_type_named<GUI::Button>("sqrt_button");
|
||||
add_operation_button(*m_sqrt_button, Calculator::Operation::Sqrt);
|
||||
|
||||
m_inverse_button = *find_descendant_of_type_named<GUI::Button>("inverse_button");
|
||||
add_operation_button(*m_inverse_button, Calculator::Operation::Inverse);
|
||||
|
||||
m_percent_button = *find_descendant_of_type_named<GUI::Button>("mod_button");
|
||||
add_operation_button(*m_percent_button, Calculator::Operation::Percent);
|
||||
|
||||
m_equals_button = *find_descendant_of_type_named<GUI::Button>("equal_button");
|
||||
m_equals_button->on_click = [this](auto) {
|
||||
double argument = m_keypad.value();
|
||||
double res = m_calculator.finish_operation(argument);
|
||||
m_keypad.set_value(res);
|
||||
update_display();
|
||||
};
|
||||
}
|
||||
|
||||
CalculatorWidget::~CalculatorWidget()
|
||||
{
|
||||
}
|
||||
|
||||
void CalculatorWidget::add_operation_button(GUI::Button& button, Calculator::Operation operation)
|
||||
{
|
||||
button.on_click = [this, operation](auto) {
|
||||
double argument = m_keypad.value();
|
||||
double res = m_calculator.begin_operation(operation, argument);
|
||||
m_keypad.set_value(res);
|
||||
update_display();
|
||||
};
|
||||
}
|
||||
|
||||
void CalculatorWidget::add_digit_button(GUI::Button& button, int digit)
|
||||
{
|
||||
button.on_click = [this, digit](auto) {
|
||||
m_keypad.type_digit(digit);
|
||||
update_display();
|
||||
};
|
||||
}
|
||||
|
||||
void CalculatorWidget::update_display()
|
||||
{
|
||||
m_entry->set_text(m_keypad.to_string());
|
||||
if (m_calculator.has_error())
|
||||
m_label->set_text("E");
|
||||
else
|
||||
m_label->set_text("");
|
||||
}
|
||||
|
||||
void CalculatorWidget::keydown_event(GUI::KeyEvent& event)
|
||||
{
|
||||
//Clear button selection when we are typing
|
||||
m_equals_button->set_focus(true);
|
||||
m_equals_button->set_focus(false);
|
||||
|
||||
if (event.key() == KeyCode::Key_Return) {
|
||||
m_keypad.set_value(m_calculator.finish_operation(m_keypad.value()));
|
||||
|
||||
} else if (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) {
|
||||
m_keypad.type_digit(atoi(event.text().characters()));
|
||||
|
||||
} else if (event.key() == KeyCode::Key_Period) {
|
||||
m_keypad.type_decimal_point();
|
||||
|
||||
} else if (event.key() == KeyCode::Key_Escape) {
|
||||
m_keypad.set_value(0.0);
|
||||
m_calculator.clear_operation();
|
||||
|
||||
} else if (event.key() == KeyCode::Key_Backspace) {
|
||||
m_keypad.type_backspace();
|
||||
|
||||
} else {
|
||||
Calculator::Operation operation;
|
||||
|
||||
switch (event.key()) {
|
||||
case KeyCode::Key_Plus:
|
||||
operation = Calculator::Operation::Add;
|
||||
break;
|
||||
case KeyCode::Key_Minus:
|
||||
operation = Calculator::Operation::Subtract;
|
||||
break;
|
||||
case KeyCode::Key_Asterisk:
|
||||
operation = Calculator::Operation::Multiply;
|
||||
break;
|
||||
case KeyCode::Key_Slash:
|
||||
operation = Calculator::Operation::Divide;
|
||||
break;
|
||||
case KeyCode::Key_Percent:
|
||||
operation = Calculator::Operation::Percent;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
m_keypad.set_value(m_calculator.begin_operation(operation, m_keypad.value()));
|
||||
}
|
||||
|
||||
update_display();
|
||||
}
|
73
Userland/Applications/Calculator/CalculatorWidget.h
Normal file
73
Userland/Applications/Calculator/CalculatorWidget.h
Normal file
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||
* Copyright (c) 2021 Glenford Williams <gw_dev@outlook.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Calculator.h"
|
||||
#include "Keypad.h"
|
||||
#include <AK/Vector.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
|
||||
class CalculatorWidget final : public GUI::Widget {
|
||||
C_OBJECT(CalculatorWidget)
|
||||
public:
|
||||
virtual ~CalculatorWidget() override;
|
||||
|
||||
private:
|
||||
CalculatorWidget();
|
||||
void add_operation_button(GUI::Button&, Calculator::Operation);
|
||||
void add_digit_button(GUI::Button&, int digit);
|
||||
|
||||
void update_display();
|
||||
|
||||
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||
|
||||
Calculator m_calculator;
|
||||
Keypad m_keypad;
|
||||
|
||||
RefPtr<GUI::TextBox> m_entry;
|
||||
RefPtr<GUI::Label> m_label;
|
||||
|
||||
RefPtr<GUI::Button> m_digit_button[10];
|
||||
RefPtr<GUI::Button> m_mem_add_button;
|
||||
RefPtr<GUI::Button> m_mem_save_button;
|
||||
RefPtr<GUI::Button> m_mem_recall_button;
|
||||
RefPtr<GUI::Button> m_mem_clear_button;
|
||||
RefPtr<GUI::Button> m_clear_button;
|
||||
RefPtr<GUI::Button> m_clear_error_button;
|
||||
RefPtr<GUI::Button> m_backspace_button;
|
||||
RefPtr<GUI::Button> m_decimal_point_button;
|
||||
RefPtr<GUI::Button> m_sign_button;
|
||||
RefPtr<GUI::Button> m_add_button;
|
||||
RefPtr<GUI::Button> m_subtract_button;
|
||||
RefPtr<GUI::Button> m_multiply_button;
|
||||
RefPtr<GUI::Button> m_divide_button;
|
||||
RefPtr<GUI::Button> m_sqrt_button;
|
||||
RefPtr<GUI::Button> m_inverse_button;
|
||||
RefPtr<GUI::Button> m_percent_button;
|
||||
RefPtr<GUI::Button> m_equals_button;
|
||||
};
|
272
Userland/Applications/Calculator/CalculatorWindow.gml
Normal file
272
Userland/Applications/Calculator/CalculatorWindow.gml
Normal file
|
@ -0,0 +1,272 @@
|
|||
@GUI::Widget {
|
||||
fixed_width: 254
|
||||
fixed_height: 213
|
||||
fill_with_background_color: true
|
||||
|
||||
layout: @GUI::VerticalBoxLayout {
|
||||
margins: [10, 0, 10, 0]
|
||||
}
|
||||
|
||||
@GUI::TextBox {
|
||||
name: "entry_textbox"
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {
|
||||
}
|
||||
|
||||
@GUI::Label {
|
||||
name: "label"
|
||||
fixed_width: 35
|
||||
fixed_height: 27
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
fixed_width: 5
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "backspace_button"
|
||||
text: "Backspace"
|
||||
fixed_width: 65
|
||||
fixed_height: 28
|
||||
foreground_color: "brown"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "clear_error_button"
|
||||
text: "CE"
|
||||
fixed_width: 55
|
||||
fixed_height: 28
|
||||
foreground_color: "brown"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "clear_button"
|
||||
text: "C"
|
||||
fixed_width: 60
|
||||
fixed_height: 28
|
||||
foreground_color: "brown"
|
||||
}
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "mem_clear_button"
|
||||
text: "MC"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "red"
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
fixed_width: 5
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "7_button"
|
||||
text: "7"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "8_button"
|
||||
text: "8"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "9_button"
|
||||
text: "9"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "divide_button"
|
||||
text: "/"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "sqrt_button"
|
||||
text: "sqrt"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {}
|
||||
|
||||
@GUI::Button {
|
||||
name: "mem_recall_button"
|
||||
text: "MR"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "red"
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
fixed_width: 5
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "4_button"
|
||||
text: "4"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "5_button"
|
||||
text: "5"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "6_button"
|
||||
text: "6"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "multiply_button"
|
||||
text: "*"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "mod_button"
|
||||
text: "%"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {}
|
||||
|
||||
@GUI::Button {
|
||||
name: "mem_save_button"
|
||||
text: "MS"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "red"
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
fixed_width: 5
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "1_button"
|
||||
text: "1"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "2_button"
|
||||
text: "2"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "3_button"
|
||||
text: "3"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "subtract_button"
|
||||
text: "-"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "inverse_button"
|
||||
text: "1/x"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {}
|
||||
|
||||
@GUI::Button {
|
||||
name: "mem_add_button"
|
||||
text: "M+"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "red"
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
fixed_width: 5
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "0_button"
|
||||
text: "0"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "sign_button"
|
||||
text: "+/-"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "decimal_button"
|
||||
text: "."
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "blue"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "add_button"
|
||||
text: "+"
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "equal_button"
|
||||
text: "="
|
||||
fixed_width: 35
|
||||
fixed_height: 28
|
||||
foreground_color: "red"
|
||||
}
|
||||
}
|
||||
}
|
171
Userland/Applications/Calculator/Keypad.cpp
Normal file
171
Userland/Applications/Calculator/Keypad.cpp
Normal file
|
@ -0,0 +1,171 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "Keypad.h"
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <math.h>
|
||||
|
||||
Keypad::Keypad()
|
||||
{
|
||||
}
|
||||
|
||||
Keypad::~Keypad()
|
||||
{
|
||||
}
|
||||
|
||||
void Keypad::type_digit(int digit)
|
||||
{
|
||||
switch (m_state) {
|
||||
case State::External:
|
||||
m_state = State::TypingInteger;
|
||||
m_negative = false;
|
||||
m_int_value = digit;
|
||||
m_frac_value = 0;
|
||||
m_frac_length = 0;
|
||||
break;
|
||||
case State::TypingInteger:
|
||||
ASSERT(m_frac_value == 0);
|
||||
ASSERT(m_frac_length == 0);
|
||||
m_int_value *= 10;
|
||||
m_int_value += digit;
|
||||
break;
|
||||
case State::TypingDecimal:
|
||||
if (m_frac_length > 6)
|
||||
break;
|
||||
m_frac_value *= 10;
|
||||
m_frac_value += digit;
|
||||
m_frac_length++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Keypad::type_decimal_point()
|
||||
{
|
||||
switch (m_state) {
|
||||
case State::External:
|
||||
m_negative = false;
|
||||
m_int_value = 0;
|
||||
m_frac_value = 0;
|
||||
m_frac_length = 0;
|
||||
break;
|
||||
case State::TypingInteger:
|
||||
ASSERT(m_frac_value == 0);
|
||||
ASSERT(m_frac_length == 0);
|
||||
m_state = State::TypingDecimal;
|
||||
break;
|
||||
case State::TypingDecimal:
|
||||
// Ignore it.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Keypad::type_backspace()
|
||||
{
|
||||
switch (m_state) {
|
||||
case State::External:
|
||||
m_negative = false;
|
||||
m_int_value = 0;
|
||||
m_frac_value = 0;
|
||||
m_frac_length = 0;
|
||||
break;
|
||||
case State::TypingDecimal:
|
||||
if (m_frac_length > 0) {
|
||||
m_frac_value /= 10;
|
||||
m_frac_length--;
|
||||
break;
|
||||
}
|
||||
ASSERT(m_frac_value == 0);
|
||||
m_state = State::TypingInteger;
|
||||
[[fallthrough]];
|
||||
case State::TypingInteger:
|
||||
ASSERT(m_frac_value == 0);
|
||||
ASSERT(m_frac_length == 0);
|
||||
m_int_value /= 10;
|
||||
if (m_int_value == 0)
|
||||
m_negative = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
double Keypad::value() const
|
||||
{
|
||||
double res = 0.0;
|
||||
|
||||
long frac = m_frac_value;
|
||||
for (int i = 0; i < m_frac_length; i++) {
|
||||
int digit = frac % 10;
|
||||
res += digit;
|
||||
res /= 10.0;
|
||||
frac /= 10;
|
||||
}
|
||||
|
||||
res += m_int_value;
|
||||
if (m_negative)
|
||||
res = -res;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void Keypad::set_value(double value)
|
||||
{
|
||||
m_state = State::External;
|
||||
|
||||
if (value < 0.0) {
|
||||
m_negative = true;
|
||||
value = -value;
|
||||
} else
|
||||
m_negative = false;
|
||||
|
||||
m_int_value = value;
|
||||
value -= m_int_value;
|
||||
|
||||
m_frac_value = 0;
|
||||
m_frac_length = 0;
|
||||
while (value != 0) {
|
||||
value *= 10.0;
|
||||
int digit = value;
|
||||
m_frac_value *= 10;
|
||||
m_frac_value += digit;
|
||||
m_frac_length++;
|
||||
value -= digit;
|
||||
|
||||
if (m_frac_length > 6)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String Keypad::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
if (m_negative)
|
||||
builder.append("-");
|
||||
builder.appendff("{}", m_int_value);
|
||||
|
||||
if (m_frac_length > 0)
|
||||
builder.appendff(".{:0{}}", m_frac_value, m_frac_length);
|
||||
|
||||
return builder.to_string();
|
||||
}
|
69
Userland/Applications/Calculator/Keypad.h
Normal file
69
Userland/Applications/Calculator/Keypad.h
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
|
||||
// This type implements number typing and
|
||||
// displaying mechanics. It does not perform
|
||||
// any arithmetic operations or anything on
|
||||
// the values it deals with.
|
||||
|
||||
class Keypad final {
|
||||
public:
|
||||
Keypad();
|
||||
~Keypad();
|
||||
|
||||
void type_digit(int digit);
|
||||
void type_decimal_point();
|
||||
void type_backspace();
|
||||
|
||||
double value() const;
|
||||
void set_value(double);
|
||||
|
||||
String to_string() const;
|
||||
|
||||
private:
|
||||
// Internal representation of the current decimal value.
|
||||
bool m_negative { false };
|
||||
long m_int_value { 0 };
|
||||
long m_frac_value { 0 };
|
||||
int m_frac_length { 0 };
|
||||
// E.g. for -35.004200,
|
||||
// m_negative = true
|
||||
// m_int_value = 35
|
||||
// m_frac_value = 4200
|
||||
// m_frac_length = 6
|
||||
|
||||
enum class State {
|
||||
External,
|
||||
TypingInteger,
|
||||
TypingDecimal
|
||||
};
|
||||
|
||||
State m_state { State::External };
|
||||
};
|
84
Userland/Applications/Calculator/main.cpp
Normal file
84
Userland/Applications/Calculator/main.cpp
Normal file
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "CalculatorWidget.h"
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/MenuBar.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (pledge("stdio shared_buffer rpath accept unix cpath fattr", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto app = GUI::Application::construct(argc, argv);
|
||||
|
||||
if (pledge("stdio shared_buffer rpath accept", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (unveil("/res", "r") < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
unveil(nullptr, nullptr);
|
||||
|
||||
auto app_icon = GUI::Icon::default_icon("app-calculator");
|
||||
|
||||
auto window = GUI::Window::construct();
|
||||
window->set_title("Calculator");
|
||||
window->set_resizable(false);
|
||||
window->resize(254, 213);
|
||||
|
||||
window->set_main_widget<CalculatorWidget>();
|
||||
|
||||
window->show();
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
auto menubar = GUI::MenuBar::construct();
|
||||
|
||||
auto& app_menu = menubar->add_menu("Calculator");
|
||||
app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
||||
GUI::Application::the()->quit();
|
||||
return;
|
||||
}));
|
||||
|
||||
auto& help_menu = menubar->add_menu("Help");
|
||||
help_menu.add_action(GUI::CommonActions::make_about_action("Calculator", app_icon));
|
||||
|
||||
app->set_menubar(move(menubar));
|
||||
|
||||
return app->exec();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue