Fix potential memory leak under X11 when pasted image is not accepted by the receiving app.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3-porting@11709 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
parent
86a7bbe93a
commit
d47b431750
4
FL/Fl.H
4
FL/Fl.H
@ -828,7 +828,7 @@ int main() {
|
||||
static int event_length() {return e_length;}
|
||||
|
||||
/** During an FL_PASTE event of non-textual data, returns a pointer to the pasted data.
|
||||
The returned data is an Fl_Image * when the result of Fl::event_clipboard_type() is Fl::clipboard_image.
|
||||
The returned data is an Fl_RGB_Image * when the result of Fl::event_clipboard_type() is Fl::clipboard_image.
|
||||
*/
|
||||
static void *event_clipboard() { return e_clipboard_data; }
|
||||
/** Returns the type of the pasted data during an FL_PASTE event.
|
||||
@ -900,7 +900,7 @@ int main() {
|
||||
and if \p type is Fl::clipboard_plain_text, the text data from the specified \p source are in Fl::event_text()
|
||||
with UTF-8 encoding, and the number of bytes in Fl::event_length();
|
||||
if \p type is Fl::clipboard_image, Fl::event_clipboard() returns a pointer to the
|
||||
image data, as an Fl_Image *.
|
||||
image data, as an Fl_RGB_Image *.
|
||||
The receiver
|
||||
should be prepared to be called \e directly by this, or for
|
||||
it to happen \e later, or possibly <i>not at all</i>. This
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <FL/Fl_Window.H>
|
||||
#include <FL/Fl_Box.H>
|
||||
#include <FL/Fl_Image.H>
|
||||
#include <FL/Fl_Shared_Image.H>
|
||||
#include <FL/Fl_Text_Buffer.H>
|
||||
#include <FL/Fl_Text_Display.H>
|
||||
#include <FL/Fl_Tabs.H>
|
||||
@ -71,7 +72,7 @@ public:
|
||||
virtual int handle(int event) {
|
||||
if (event != FL_PASTE) return Fl_Tabs::handle(event);
|
||||
if (strcmp(Fl::event_clipboard_type(), Fl::clipboard_image) == 0) { // an image is being pasted
|
||||
Fl_Image *im = (Fl_Image*)Fl::event_clipboard(); // get it as an Fl_Image object
|
||||
Fl_RGB_Image *im = (Fl_RGB_Image*)Fl::event_clipboard(); // get it as an Fl_RGB_Image object
|
||||
if (!im) return 1;
|
||||
char title[300];
|
||||
sprintf(title, "%dx%d",im->w(), im->h()); // display the image original size
|
||||
@ -92,18 +93,11 @@ public:
|
||||
}
|
||||
CloseClipboard();
|
||||
#endif
|
||||
float scale_x = (float)im->w() / image_box->w(); // rescale the image if larger than the display box
|
||||
float scale_y = (float)im->h() / image_box->h();
|
||||
float scale = scale_x;
|
||||
if (scale_y > scale) scale = scale_y;
|
||||
if (scale > 1) {
|
||||
Fl_Image *im2 = im->copy(im->w()/scale, im->h()/scale);
|
||||
delete im;
|
||||
im = im2;
|
||||
}
|
||||
Fl_Image *oldim = image_box->image();
|
||||
if (oldim) delete oldim;
|
||||
image_box->image(im); // show the scaled image
|
||||
Fl_Shared_Image *oldim = (Fl_Shared_Image*)image_box->image();
|
||||
if (oldim) oldim->release();
|
||||
Fl_Shared_Image *shared = Fl_Shared_Image::get(im);
|
||||
shared->scale(image_box->w(), image_box->h());
|
||||
image_box->image(shared); // show the scaled image
|
||||
image_size->copy_label(title);
|
||||
value(image_box->parent());
|
||||
window()->redraw();
|
||||
|
17
src/Fl_x.cxx
17
src/Fl_x.cxx
@ -1365,9 +1365,9 @@ fprintf(stderr,"\n");*/
|
||||
sn_buffer[bytesread] = 0;
|
||||
convert_crlf(sn_buffer, bytesread);
|
||||
}
|
||||
if (!fl_selection_requestor) return 0;
|
||||
if (Fl::e_clipboard_type == Fl::clipboard_image) {
|
||||
if (bytesread == 0) return 0;
|
||||
Fl_Image *image = 0;
|
||||
static char tmp_fname[21];
|
||||
static Fl_Shared_Image *shared = 0;
|
||||
strcpy(tmp_fname, "/tmp/clipboardXXXXXX");
|
||||
@ -1383,18 +1383,23 @@ fprintf(stderr,"\n");*/
|
||||
shared = Fl_Shared_Image::get(tmp_fname);
|
||||
unlink(tmp_fname);
|
||||
if (!shared) return 0;
|
||||
image = shared->copy();
|
||||
uchar *rgb = new uchar[shared->w() * shared->h() * shared->d()];
|
||||
memcpy(rgb, shared->data()[0], shared->w() * shared->h() * shared->d());
|
||||
Fl_RGB_Image *image = new Fl_RGB_Image(rgb, shared->w(), shared->h(), shared->d());
|
||||
shared->release();
|
||||
image->alloc_array = 1;
|
||||
Fl::e_clipboard_data = (void*)image;
|
||||
}
|
||||
if (!fl_selection_requestor) return 0;
|
||||
|
||||
if (Fl::e_clipboard_type == Fl::clipboard_plain_text) {
|
||||
else if (Fl::e_clipboard_type == Fl::clipboard_plain_text) {
|
||||
Fl::e_text = sn_buffer ? (char*)sn_buffer : (char *)"";
|
||||
Fl::e_length = bytesread;
|
||||
}
|
||||
int old_event = Fl::e_number;
|
||||
fl_selection_requestor->handle(Fl::e_number = FL_PASTE);
|
||||
int retval = fl_selection_requestor->handle(Fl::e_number = FL_PASTE);
|
||||
if (!retval && Fl::e_clipboard_type == Fl::clipboard_image) {
|
||||
delete (Fl_RGB_Image*)Fl::e_clipboard_data;
|
||||
Fl::e_clipboard_data= NULL;
|
||||
}
|
||||
Fl::e_number = old_event;
|
||||
// Detect if this paste is due to Xdnd by the property name (I use
|
||||
// XA_SECONDARY for that) and send an XdndFinished message. It is not
|
||||
|
Loading…
x
Reference in New Issue
Block a user