Calculator: Add support for copy+paste using system clipboard

This commit is contained in:
Brendan Coles 2021-02-24 14:35:39 +00:00 committed by Andreas Kling
parent 1ba1ca76d8
commit 9bc3c3c962
Notes: sideshowbarker 2024-07-18 21:56:56 +09:00
3 changed files with 29 additions and 1 deletions

View file

@ -147,6 +147,17 @@ void CalculatorWidget::add_digit_button(GUI::Button& button, int digit)
}; };
} }
String CalculatorWidget::get_entry()
{
return m_entry->text();
}
void CalculatorWidget::set_entry(double value)
{
m_keypad.set_value(value);
update_display();
}
void CalculatorWidget::update_display() void CalculatorWidget::update_display()
{ {
m_entry->set_text(m_keypad.to_string()); m_entry->set_text(m_keypad.to_string());

View file

@ -36,6 +36,8 @@ class CalculatorWidget final : public GUI::Widget {
C_OBJECT(CalculatorWidget) C_OBJECT(CalculatorWidget)
public: public:
virtual ~CalculatorWidget() override; virtual ~CalculatorWidget() override;
String get_entry();
void set_entry(double);
private: private:
CalculatorWidget(); CalculatorWidget();

View file

@ -27,6 +27,7 @@
#include "CalculatorWidget.h" #include "CalculatorWidget.h"
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/Icon.h> #include <LibGUI/Icon.h>
#include <LibGUI/Menu.h> #include <LibGUI/Menu.h>
#include <LibGUI/MenuBar.h> #include <LibGUI/MenuBar.h>
@ -62,7 +63,7 @@ int main(int argc, char** argv)
window->set_resizable(false); window->set_resizable(false);
window->resize(254, 213); window->resize(254, 213);
window->set_main_widget<CalculatorWidget>(); auto& widget = window->set_main_widget<CalculatorWidget>();
window->show(); window->show();
window->set_icon(app_icon.bitmap_for_size(16)); window->set_icon(app_icon.bitmap_for_size(16));
@ -75,6 +76,20 @@ int main(int argc, char** argv)
return; return;
})); }));
auto& edit_menu = menubar->add_menu("Edit");
edit_menu.add_action(GUI::Action::create("Copy", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"), [&](const GUI::Action&) {
GUI::Clipboard::the().set_plain_text(widget.get_entry());
}));
edit_menu.add_action(GUI::Action::create("Paste", { Mod_Ctrl, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/16x16/paste.png"), [&](const GUI::Action&) {
auto clipboard = GUI::Clipboard::the().data_and_type();
if (clipboard.mime_type == "text/plain") {
if (!clipboard.data.is_empty()) {
auto data = atof(StringView(clipboard.data).to_string().characters());
widget.set_entry(data);
}
}
}));
auto& help_menu = menubar->add_menu("Help"); auto& help_menu = menubar->add_menu("Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Calculator", app_icon, window)); help_menu.add_action(GUI::CommonActions::make_about_action("Calculator", app_icon, window));