From 12fdfe0864280dff994dcf0019925c4b1db72adf Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Fri, 7 Nov 2025 01:01:34 +0000 Subject: [PATCH] add clipboard feature to entry git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@611 b9cfdab3-6d41-4d17-bbe4-086880011989 --- src/backend/x11.c | 40 +++++++++++++++++++++++++++++++++++++--- src/widget/entry.c | 14 ++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/backend/x11.c b/src/backend/x11.c index 978fb82..1989188 100644 --- a/src/backend/x11.c +++ b/src/backend/x11.c @@ -785,8 +785,42 @@ void MwLLSetClipboard(MwLL handle, const char* text) { } char* MwLLGetClipboard(MwLL handle) { - /* TODO */ + Atom clip, target, prop; + XEvent ev; + XEvent* queue = NULL; + char* r = NULL; - (void)handle; - return NULL; + clip = XInternAtom(handle->display, "CLIPBOARD", 0); + target = XA_STRING; + prop = XInternAtom(handle->display, "XSEL_DATA", 0); + + XConvertSelection(handle->display, clip, target, prop, handle->window, CurrentTime); + + while(1){ + XNextEvent(handle->display, &ev); + if(ev.type == SelectionNotify){ + if(ev.xselection.selection == clip && ev.xselection.property != 0){ + Atom t; + unsigned long size, N; + char* data; + int format; + + XGetWindowProperty(ev.xselection.display, ev.xselection.requestor, ev.xselection.property, 0L, (~0L), 0, AnyPropertyType, &t, &format, &size, &N, (unsigned char**)&data); + if(t == target){ + r = MwStringDupliacte(data); + XFree(data); + } + XDeleteProperty(ev.xselection.display, ev.xselection.requestor, ev.xselection.property); + } + break; + } + } + + while(arrlen(queue) > 0){ + XPutBackEvent(handle->display, &queue[0]); + arrdel(queue, 0); + } + arrfree(queue); + + return r; } diff --git a/src/widget/entry.c b/src/widget/entry.c index 3a9c327..f092f04 100644 --- a/src/widget/entry.c +++ b/src/widget/entry.c @@ -107,6 +107,20 @@ static void key(MwWidget handle, int code) { } else if(code == MwLLKeyEnter) { MwDispatchUserHandler(handle, MwNactivateHandler, NULL); } else if(code == (MwLLControlMask | 'v')){ + char* c = MwLLGetClipboard(handle->lowlevel); + if(c != NULL){ + char* out = malloc(strlen(str) + strlen(c) + 1); + + MwUTF8Copy(str, 0, out, 0, t->cursor); + MwUTF8Copy(c, 0, out, t->cursor, MwUTF8Length(c)); + MwUTF8Copy(str, t->cursor, out, t->cursor + MwUTF8Length(c), MwUTF8Length(str) - t->cursor); + + t->cursor += MwUTF8Length(c); + + MwSetText(handle, MwNtext, out); + free(out); + free(c); + } } else if(!(code & MwLLKeyMask)) { int incr = 0; out = malloc(strlen(str) + 5 + 1);