FLTK logo

[master] 56f4e43 - X11: use std::string to store clipboard and DnD data.

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Library      Forums      Links      Apps     Login 
[  All Forums  |  Back to fltk.commit  ]
 
[ Previous Message ] [ Next Message ]

[master] 56f4e43 - X11: use std::string to store clipboard and DnD data. "ManoloFLTK" 08:32 Sep 30  
 
commit 56f4e43d4a28b5404661bf9f0f1a34fe18772d7c
Author:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
AuthorDate: Wed Sep 30 10:23:20 2026 +0200
Commit:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
CommitDate: Wed Sep 30 10:23:20 2026 +0200

    X11: use std::string to store clipboard and DnD data.

 src/Fl_x.cxx                             | 39 +++++++++++++-------------------
 src/drivers/X11/Fl_X11_Screen_Driver.H   |  1 +
 src/drivers/X11/Fl_X11_Screen_Driver.cxx |  2 ++
 src/fl_dnd_x.cxx                         | 24 ++++++++++----------
 4 files changed, 31 insertions(+), 35 deletions(-)

diff --git src/Fl_x.cxx src/Fl_x.cxx
index ffdeb8c..39d5407 100644
--- src/Fl_x.cxx
+++ src/Fl_x.cxx
@@ -688,10 +688,7 @@ int Fl_X11_Screen_Driver::get_mouse(int &xx, int &yy) {
 ////////////////////////////////////////////////////////////////
 // Code used for paste and DnD into the program:
 
-char *fl_selection_buffer[2];
-int fl_selection_length[2];
 const char * fl_selection_type[2];
-int fl_selection_buffer_length[2];
 char fl_i_own_selection[2] = {0,0};
 
 
@@ -703,11 +700,11 @@ void Fl_X11_Screen_Driver::paste(Fl_Widget &receiver, int clipboard, const char
       // Notice that the text is clobbered if set_selection is
       // called in response to FL_PASTE!
       // However, for now, we only paste text in this function
-      Fl::e_text = fl_selection_buffer[clipboard];
-      Fl::e_length = fl_selection_length[clipboard];
+      Fl::e_text = (char*)selection_string[clipboard].c_str();
+      Fl::e_length = selection_string[clipboard].length();
       if (!Fl::e_text) Fl::e_text = (char *)"";
     } else if (clipboard == 1 && type == Fl::clipboard_image && fl_selection_type[1] == type) {
-      Fl::e_clipboard_data = Fl_Unix_System_Driver::own_bmp_to_RGB(fl_selection_buffer[1]);
+      Fl::e_clipboard_data = Fl_Unix_System_Driver::own_bmp_to_RGB(selection_string[1].c_str());
       Fl::e_clipboard_type = Fl::clipboard_image;
     } else return;
     int retval = receiver.handle(FL_PASTE);
@@ -838,14 +835,8 @@ void Fl_X11_Screen_Driver::copy(const char *stuff, int len, int clipboard, const
     clipboard = 0;              // ... and then to selection buffer: fall through
   }
 
-  if (len+1 > fl_selection_buffer_length[clipboard]) {
-    delete[] fl_selection_buffer[clipboard];
-    fl_selection_buffer[clipboard] = new char[len+100];
-    fl_selection_buffer_length[clipboard] = len+100;
-  }
-  memcpy(fl_selection_buffer[clipboard], stuff, len);
-  fl_selection_buffer[clipboard][len] = 0; // needed for direct paste
-  fl_selection_length[clipboard] = len;
+  selection_string[clipboard].clear();
+  selection_string[clipboard].insert(selection_string[clipboard].begin(), stuff, stuff + len);
   fl_i_own_selection[clipboard] = 1;
   fl_selection_type[clipboard] = Fl::clipboard_plain_text;
   Atom property = clipboard ? CLIPBOARD : XA_PRIMARY;
@@ -856,9 +847,11 @@ void Fl_X11_Screen_Driver::copy(const char *stuff, int len, int clipboard, const
 // takes a raw RGB image and puts it in the copy/paste buffer
 void Fl_X11_Screen_Driver::copy_image(const unsigned char *data, int W, int H, int clipboard){
   if (!data || W <= 0 || H <= 0) return;
-  delete[] fl_selection_buffer[clipboard];
-  fl_selection_buffer[clipboard] = (char *) Fl_Unix_System_Driver::create_bmp(data,W,H,&fl_selection_length[clipboard]);
-  fl_selection_buffer_length[clipboard] = fl_selection_length[clipboard];
+  selection_string[clipboard].clear();
+  int len;
+  uchar *bmp = Fl_Unix_System_Driver::create_bmp(data, W, H, &len);
+  selection_string[clipboard].insert(selection_string[clipboard].begin(), bmp, bmp + len);
+  delete[] bmp;
   fl_i_own_selection[clipboard] = 1;
   fl_selection_type[clipboard] = Fl::clipboard_image;
   Atom property = clipboard ? CLIPBOARD : XA_PRIMARY;
@@ -1555,7 +1548,7 @@ int fl_handle(const XEvent& thisevent)
         XChangeProperty(fl_display, e.requestor, e.property,
                         XA_ATOM, atom_bits, 0, (unsigned char*)a, 3);
       } else {
-        if (fl_selection_length[clipboard]) { // data available
+        if (Fl_X11_Screen_Driver::selection_string[clipboard].length()) { // data available
           if (e.target == fl_XaUtf8String ||
               e.target == XA_STRING ||
               e.target == fl_XaCompoundText ||
@@ -1569,8 +1562,8 @@ int fl_handle(const XEvent& thisevent)
             if (e.target != XA_STRING) e.target = fl_XaUtf8String;
             XChangeProperty(fl_display, e.requestor, e.property,
                             e.target, 8, 0,
-                            (unsigned char *)fl_selection_buffer[clipboard],
-                            fl_selection_length[clipboard]);
+                            (unsigned char *)Fl_X11_Screen_Driver::selection_string[clipboard].c_str(),
+                            Fl_X11_Screen_Driver::selection_string[clipboard].length());
           }
         } else { // no data available
           e.property = 0;
@@ -1582,11 +1575,11 @@ int fl_handle(const XEvent& thisevent)
         XChangeProperty(fl_display, e.requestor, e.property,
                         XA_ATOM, atom_bits, 0, (unsigned char*)a, 1);
       } else {
-        if (e.target == fl_XaImageBmp && fl_selection_length[clipboard]) {
+        if (e.target == fl_XaImageBmp && Fl_X11_Screen_Driver::selection_string[clipboard].length()) {
             XChangeProperty(fl_display, e.requestor, e.property,
                             e.target, 8, 0,
-                            (unsigned char *)fl_selection_buffer[clipboard],
-                            fl_selection_length[clipboard]);
+                            (unsigned char *)Fl_X11_Screen_Driver::selection_string[clipboard].c_str(),
+                            Fl_X11_Screen_Driver::selection_string[clipboard].length());
         } else {
           e.property = 0;
         }
diff --git src/drivers/X11/Fl_X11_Screen_Driver.H src/drivers/X11/Fl_X11_Screen_Driver.H
index a22e380..764a682 100644
--- src/drivers/X11/Fl_X11_Screen_Driver.H
+++ src/drivers/X11/Fl_X11_Screen_Driver.H
@@ -121,6 +121,7 @@ public:
   static XIM xim_im;
   static XIC xim_ic;
   static Window xim_win;
+  static std::string selection_string[2]; // stores clipboard and DnD data
   static void new_ic();
   static void xim_activate(Window xid);
   static void xim_deactivate(void);
diff --git src/drivers/X11/Fl_X11_Screen_Driver.cxx src/drivers/X11/Fl_X11_Screen_Driver.cxx
index 4fa3b86..d7507a0 100644
--- src/drivers/X11/Fl_X11_Screen_Driver.cxx
+++ src/drivers/X11/Fl_X11_Screen_Driver.cxx
@@ -70,6 +70,8 @@ char Fl_X11_Screen_Driver::fl_is_over_the_spot = 0;
 
 Window Fl_X11_Screen_Driver::xim_win = 0;
 
+std::string Fl_X11_Screen_Driver::selection_string[2] = {"", ""};
+
 Fl_X11_Screen_Driver::Fl_X11_Screen_Driver() : Fl_Unix_Screen_Driver() {
   // X11 screen driver does not use a key table
   key_table = NULL;
diff --git src/fl_dnd_x.cxx src/fl_dnd_x.cxx
index e04dc24..b5a191e 100644
--- src/fl_dnd_x.cxx
+++ src/fl_dnd_x.cxx
@@ -39,7 +39,6 @@ extern Atom fl_XaUtf8String;
 extern Atom fl_Xatextplain;
 
 extern char fl_i_own_selection[2];
-extern char *fl_selection_buffer[2];
 
 extern void fl_sendClientMessage(Window window, Atom message,
                                  unsigned long d0,
@@ -134,17 +133,18 @@ int Fl_X11_Screen_Driver::dnd(int unused) {
         // contains at least one CR LF, then we flag the data as
         // both a URI list (MIME media type "text/uri-list") and
         // plain text.  Otherwise, we just say it is plain text.
-        if ((!strncmp(fl_selection_buffer[0], "file:///", 8) ||
-             !strncmp(fl_selection_buffer[0], "ftp://";, 6) ||
-             !strncmp(fl_selection_buffer[0], "http://";, 7) ||
-             !strncmp(fl_selection_buffer[0], "https://";, 8) ||
-             !strncmp(fl_selection_buffer[0], "ipp://", 6) ||
-             !strncmp(fl_selection_buffer[0], "ldap:", 5) ||
-             !strncmp(fl_selection_buffer[0], "mailto:";, 7) ||
-             !strncmp(fl_selection_buffer[0], "news:";, 5) ||
-             !strncmp(fl_selection_buffer[0], "smb://", 6)) &&
-            !strchr(fl_selection_buffer[0], ' ') &&
-            strstr(fl_selection_buffer[0], "\r\n")) {
+        const char *p = selection_string[0].c_str();
+        if ((!strncmp(p, "file:///", 8) ||
+             !strncmp(p, "ftp://";, 6) ||
+             !strncmp(p, "http://";, 7) ||
+             !strncmp(p, "https://";, 8) ||
+             !strncmp(p, "ipp://", 6) ||
+             !strncmp(p, "ldap:", 5) ||
+             !strncmp(p, "mailto:";, 7) ||
+             !strncmp(p, "news:";, 5) ||
+             !strncmp(p, "smb://", 6)) &&
+            !strchr(p, ' ') &&
+            strstr(p, "\r\n")) {
           // Send file/URI list...
           fl_sendClientMessage(target_window, fl_XdndEnter, source_window, dndversion<<24,
                                fl_XdndURIList, fl_XaUtf8String, XA_STRING);
[ Direct Link to Message ]
 
     
[ Previous Message ] [ Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2026 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.