wip: add clipboard support

This commit is contained in:
2026-04-09 20:44:52 +02:00
parent b7b810e2a4
commit 906828f3ec
11 changed files with 582 additions and 0 deletions
+77
View File
@@ -13,6 +13,7 @@
#include <gui/truetype.hpp>
#include <gui/svg.hpp>
#include <gui/dialogs.hpp>
#include <gui/clipboard.hpp>
#include <gui/stb_math.h>
extern "C" {
@@ -442,6 +443,64 @@ static void set_status(const char* msg) {
g_status_msg[sizeof(g_status_msg) - 1] = '\0';
}
static int selection_length() {
if (!g_has_selection) return 0;
int ss, se;
sel_range(&ss, &se);
return se - ss;
}
static bool copy_selection_to_clipboard() {
int len = selection_length();
if (len <= 0) {
set_status("Nothing selected");
return false;
}
int ss, se;
sel_range(&ss, &se);
if (!gui::clipboard_set_text(g_buffer + ss, len)) {
set_status("Copy failed");
return false;
}
set_status("Copied selection");
return true;
}
static bool cut_selection_to_clipboard() {
if (!copy_selection_to_clipboard())
return false;
delete_selection();
set_status("Cut selection");
return true;
}
static bool paste_from_clipboard() {
int clip_len = 0;
char* clip_text = gui::clipboard_get_text_alloc(&clip_len);
if (clip_text == nullptr || clip_len <= 0) {
if (clip_text) montauk::mfree(clip_text);
set_status("Clipboard is empty");
return false;
}
int replaced_len = selection_length();
if (g_buf_len - replaced_len + clip_len >= MAX_CAP) {
montauk::mfree(clip_text);
set_status("Clipboard text too large");
return false;
}
if (g_has_selection)
delete_selection();
insert_string(clip_text, clip_len);
montauk::mfree(clip_text);
set_status("Pasted clipboard");
return true;
}
static bool looks_binary(const char* data, int len) {
if (!data || len <= 0) return false;
@@ -951,6 +1010,24 @@ static bool handle_key(Montauk::KeyEvent& key, int win_id) {
return true;
}
// Ctrl+C: copy
if (key.ctrl && !key.alt && (key.ascii == 'c' || key.ascii == 'C')) {
copy_selection_to_clipboard();
return true;
}
// Ctrl+X: cut
if (key.ctrl && !key.alt && (key.ascii == 'x' || key.ascii == 'X')) {
cut_selection_to_clipboard();
return true;
}
// Ctrl+V: paste
if (key.ctrl && !key.alt && (key.ascii == 'v' || key.ascii == 'V')) {
paste_from_clipboard();
return true;
}
// Arrow keys
if (key.scancode == 0x48) { // Up
if (key.shift) start_selection(); else if (g_has_selection) clear_selection();