Strenghten Fl::paste(): the widget's handle method may change Fl::e_text.

The widget's handle method may change the value of Fl::e_text.
 This occurs for instance if this method calls fl_choice().
 So, memorize the value of Fl::e_text before, to delete the correct array after the call to the handle method.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@12373 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Manolo Gouy 2017-08-06 06:29:33 +00:00
parent 7b64a4656a
commit 4703285d28

View File

@ -696,17 +696,18 @@ void Fl::paste(Fl_Widget &receiver, int clipboard, const char *type) {
Fl::e_text = 0;
return;
}
Fl::e_text = new char[fl_selection_length[clipboard]+1];
char *o = Fl::e_text;
char *clip_text = new char[fl_selection_length[clipboard]+1];
char *o = clip_text;
while (*i) { // Convert \r\n -> \n
if ( *i == '\r' && *(i+1) == '\n') i++;
else *o++ = *i++;
}
*o = 0;
Fl::e_text = clip_text;
Fl::e_length = (int) (o - Fl::e_text);
Fl::e_clipboard_type = Fl::clipboard_plain_text;
receiver.handle(FL_PASTE);
delete [] Fl::e_text;
delete [] clip_text;
Fl::e_text = 0;
} else if (clipboard) {
HANDLE h;
@ -715,21 +716,22 @@ void Fl::paste(Fl_Widget &receiver, int clipboard, const char *type) {
if ((h = GetClipboardData(CF_UNICODETEXT))) { // there's text in the clipboard
wchar_t *memLock = (wchar_t*) GlobalLock(h);
size_t utf16_len = wcslen(memLock);
Fl::e_text = new char[utf16_len * 4 + 1];
unsigned utf8_len = fl_utf8fromwc(Fl::e_text, (unsigned) (utf16_len * 4), memLock, (unsigned) utf16_len);
*(Fl::e_text + utf8_len) = 0;
char *clip_text = new char[utf16_len * 4 + 1];
unsigned utf8_len = fl_utf8fromwc(clip_text, (unsigned) (utf16_len * 4), memLock, (unsigned) utf16_len);
*(clip_text + utf8_len) = 0;
GlobalUnlock(h);
LPSTR a,b;
a = b = Fl::e_text;
a = b = clip_text;
while (*a) { // strip the CRLF pairs ($%$#@^)
if (*a == '\r' && a[1] == '\n') a++;
else *b++ = *a++;
}
*b = 0;
Fl::e_text = clip_text;
Fl::e_length = (int) (b - Fl::e_text);
Fl::e_clipboard_type = Fl::clipboard_plain_text; // indicates that the paste event is for plain UTF8 text
receiver.handle(FL_PASTE); // send the FL_PASTE event to the widget
delete[] Fl::e_text;
delete[] clip_text;
Fl::e_text = 0;
}
}