wip: add clipboard support
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "wordprocessor.hpp"
|
||||
#include <gui/dialogs.hpp>
|
||||
#include <gui/clipboard.hpp>
|
||||
|
||||
static void wp_set_status(WordProcessorState* wp, const char* msg) {
|
||||
if (!msg) msg = "";
|
||||
@@ -13,6 +14,57 @@ static void wp_set_status(WordProcessorState* wp, const char* msg) {
|
||||
wp->status_msg[sizeof(wp->status_msg) - 1] = '\0';
|
||||
}
|
||||
|
||||
static bool wp_copy_selection_to_clipboard(WordProcessorState* wp) {
|
||||
int len = 0;
|
||||
char* text = wp_selection_plain_text_alloc(wp, &len);
|
||||
if (text == nullptr || len <= 0) {
|
||||
if (text) montauk::mfree(text);
|
||||
wp_set_status(wp, "Nothing selected");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = gui::clipboard_set_text(text, len);
|
||||
montauk::mfree(text);
|
||||
if (!ok) {
|
||||
wp_set_status(wp, "Copy failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
wp_set_status(wp, "Copied selection");
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool wp_cut_selection_to_clipboard(WordProcessorState* wp) {
|
||||
if (!wp_copy_selection_to_clipboard(wp))
|
||||
return false;
|
||||
|
||||
wp_delete_selection(wp);
|
||||
wp_history_checkpoint(wp);
|
||||
wp_set_status(wp, "Cut selection");
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool wp_paste_from_clipboard(WordProcessorState* wp) {
|
||||
int len = 0;
|
||||
char* text = gui::clipboard_get_text_alloc(&len);
|
||||
if (text == nullptr || len <= 0) {
|
||||
if (text) montauk::mfree(text);
|
||||
wp_set_status(wp, "Clipboard is empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = wp_replace_selection_with_text(wp, text, len);
|
||||
montauk::mfree(text);
|
||||
if (!ok) {
|
||||
wp_set_status(wp, "Clipboard text could not be inserted");
|
||||
return false;
|
||||
}
|
||||
|
||||
wp_history_checkpoint(wp);
|
||||
wp_set_status(wp, "Pasted clipboard");
|
||||
return true;
|
||||
}
|
||||
|
||||
static void wp_open_pathbar_for_open(WordProcessorState* wp) {
|
||||
char path[256] = {};
|
||||
char msg[160] = {};
|
||||
@@ -584,6 +636,19 @@ void wp_handle_key(const Montauk::KeyEvent& key) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (key.ctrl && !key.alt && (key.ascii == 'c' || key.ascii == 'C')) {
|
||||
wp_copy_selection_to_clipboard(wp);
|
||||
return;
|
||||
}
|
||||
if (key.ctrl && !key.alt && (key.ascii == 'x' || key.ascii == 'X')) {
|
||||
wp_cut_selection_to_clipboard(wp);
|
||||
return;
|
||||
}
|
||||
if (key.ctrl && !key.alt && (key.ascii == 'v' || key.ascii == 'V')) {
|
||||
wp_paste_from_clipboard(wp);
|
||||
return;
|
||||
}
|
||||
|
||||
if (key.scancode == 0x48) {
|
||||
if (key.shift) wp_start_selection(wp);
|
||||
else wp_clear_selection(wp);
|
||||
|
||||
Reference in New Issue
Block a user