From f991ac4421699843081001d6ef01791026068036 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Mon, 30 Sep 2002 20:00:42 +0000 Subject: [PATCH] Fix leaking pixmap icon. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5352 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/xforms/ChangeLog | 16 ++++ src/frontends/xforms/FormBase.C | 89 +++++++++++++---------- src/frontends/xforms/FormBase.h | 17 ++++- src/frontends/xforms/FormBaseDeprecated.C | 81 ++++++++++++--------- src/frontends/xforms/FormBaseDeprecated.h | 13 +++- src/frontends/xforms/XFormsView.C | 17 +++-- src/frontends/xforms/XFormsView.h | 5 ++ 7 files changed, 150 insertions(+), 88 deletions(-) diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index 9b9968ce00..fedad6633c 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,19 @@ +2002-09-30 Angus Leeming + + * FormBase.[Ch]: + * FormBaseDeprecated.[Ch]: + (icon_pixmap_, icon_mask_) new member variables. + (prepare_to_show) new method, containing initialisation code invoked + the first time show() is called. + (d-tor) destroy icon_pixmap_, if it exists. + (show) ensure that the icon pixmap is generated only once. + + * XFormsView.[Ch]: + (icon_pixmap_, icon_mask_) new member variables. + (d-tor) destroy icon_pixmap_, if it exists. + (create_form_form_main) assign the generated pixmap to the new + icon_pixmap_ member variable. + 2002-09-26 Angus Leeming * xscreen.h: don't #include "XWorkArea.h", forward-declare XWorkArea. diff --git a/src/frontends/xforms/FormBase.C b/src/frontends/xforms/FormBase.C index 4486572f88..545234c9ef 100644 --- a/src/frontends/xforms/FormBase.C +++ b/src/frontends/xforms/FormBase.C @@ -40,12 +40,16 @@ static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *); FormBase::FormBase(string const & t, bool allowResize) : ViewBase(), minw_(0), minh_(0), allow_resize_(allowResize), - title_(t), tooltips_(new Tooltips()) + title_(t), icon_pixmap_(0), icon_mask_(0), + tooltips_(new Tooltips()) {} FormBase::~FormBase() { + if (icon_pixmap_) + XFreePixmap(fl_get_display(), icon_pixmap_); + delete tooltips_; } @@ -70,30 +74,55 @@ xformsBC & FormBase::bc() } -void FormBase::show() +void FormBase::prepare_to_show() { - if (!form()) { - build(); + double const scale = scale_to_fit_tabs(form()); + if (scale > 1.001) + scale_form(form(), scale); + + bc().refresh(); + + // work around dumb xforms sizing bug + minw_ = form()->w; + minh_ = form()->h; + + fl_set_form_atclose(form(), C_WMHideCB, 0); + + // set the title for the minimized form + if (!getController().IconifyWithMain()) + fl_winicontitle(form()->window, title_.c_str()); + + // assign an icon to the form + string const iconname = LibFileSearch("images", "lyx", "xpm"); + if (!iconname.empty()) { + unsigned int w, h; + icon_pixmap_ = fl_read_pixmapfile(fl_root, + iconname.c_str(), + &w, + &h, + &icon_mask_, + 0, 0, 0); + fl_set_form_icon(form(), icon_pixmap_, icon_mask_); } +} - // use minw_ to flag whether the dialog has ever been shown - // (Needed now that build() is/should be called from the controller) - if (minw_ == 0) { - double const scale = scale_to_fit_tabs(form()); - if (scale > 1.001) - scale_form(form(), scale); - - bc().refresh(); - // work around dumb xforms sizing bug - minw_ = form()->w; - minh_ = form()->h; +void FormBase::show() +{ + // build() is/should be called from the controller, so form() should + // always exist. + lyx::Assert(form()); - fl_set_form_atclose(form(), C_WMHideCB, 0); + // we use minw_ to flag whether the dialog has ever been shown. + // In turn, prepare_to_show() initialises various bits 'n' pieces + // (including minw_). + if (minw_ == 0) { + prepare_to_show(); } + // make sure the form is up to date. fl_freeze_form(form()); - update(); // make sure its up-to-date + update(); fl_unfreeze_form(form()); if (form()->visible) { @@ -115,35 +144,17 @@ void FormBase::show() fl_set_form_maxsize(form(), minw_, minh_); string const maximize_title = "LyX: " + title_; - int const iconify_policy = getController().IconifyWithMain() ? - FL_TRANSIENT : 0; + int const iconify_policy = + getController().IconifyWithMain() ? FL_TRANSIENT : 0; fl_show_form(form(), FL_PLACE_MOUSE | FL_FREE_SIZE, iconify_policy, maximize_title.c_str()); - - if (iconify_policy == 0) { - // set title for minimized form - string const minimize_title = title_; - fl_winicontitle(form()->window, minimize_title.c_str()); - - // assign an icon to form - string const iconname = LibFileSearch("images", "lyx", "xpm"); - if (!iconname.empty()) { - unsigned int w, h; - Pixmap icon_mask; - Pixmap const icon_p = fl_read_pixmapfile(fl_root, - iconname.c_str(), - &w, - &h, - &icon_mask, - 0, 0, 0); // this leaks - fl_set_form_icon(form(), icon_p, icon_mask); - } - } } + // For some strange reason known only to xforms, the tooltips can only + // be set on a form that is already visible... tooltips().set(); } diff --git a/src/frontends/xforms/FormBase.h b/src/frontends/xforms/FormBase.h index f67731cd0f..d578803b4f 100644 --- a/src/frontends/xforms/FormBase.h +++ b/src/frontends/xforms/FormBase.h @@ -20,13 +20,13 @@ #endif #include "ViewBase.h" -#include "LString.h" #include "ButtonPolicies.h" #include "FeedbackController.h" +#include "forms_fwd.h" +#include "LString.h" #include - -#include "forms_fwd.h" +#include // for Pixmap class xformsBC; class Tooltips; @@ -79,14 +79,23 @@ private: */ virtual void redraw(); + /** Called on the first show() request, initialising various bits and + * pieces. + */ + void prepare_to_show(); + /// The dialog's minimum allowable dimensions. int minw_; /// int minh_; /// Can the dialog be resized after it has been created? bool allow_resize_; - /// dialog title, displayed by WM. + /// dialog title, displayed by the window manager. string title_; + /// Passed to the window manager to give a pretty little symbol ;-) + Pixmap icon_pixmap_; + /// + Pixmap icon_mask_; /// Tooltips * tooltips_; }; diff --git a/src/frontends/xforms/FormBaseDeprecated.C b/src/frontends/xforms/FormBaseDeprecated.C index d07bb0a13b..0984fb6bd2 100644 --- a/src/frontends/xforms/FormBaseDeprecated.C +++ b/src/frontends/xforms/FormBaseDeprecated.C @@ -44,7 +44,7 @@ static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *); FormBaseDeprecated::FormBaseDeprecated(LyXView & lv, Dialogs & d, string const & t, bool allowResize) - : lv_(lv), d_(d), title_(t), + : lv_(lv), d_(d), title_(t), icon_pixmap_(0), icon_mask_(0), minw_(0), minh_(0), allow_resize_(allowResize), tooltips_(new Tooltips()) {} @@ -52,6 +52,9 @@ FormBaseDeprecated::FormBaseDeprecated(LyXView & lv, Dialogs & d, FormBaseDeprecated::~FormBaseDeprecated() { + if (icon_pixmap_) + XFreePixmap(fl_get_display(), icon_pixmap_); + delete tooltips_; } @@ -83,24 +86,48 @@ void FormBaseDeprecated::disconnect() } -void FormBaseDeprecated::show() +void FormBaseDeprecated::prepare_to_show() { - if (!form()) { - build(); - - double const scale = scale_to_fit_tabs(form()); - if (scale > 1.001) - scale_form(form(), scale); - - bc().refresh(); + build(); + + double const scale = scale_to_fit_tabs(form()); + if (scale > 1.001) + scale_form(form(), scale); + + bc().refresh(); + + // work around dumb xforms sizing bug + minw_ = form()->w; + minh_ = form()->h; + + fl_set_form_atclose(form(), C_WMHideCB, 0); + + // set the title for the minimized form + if (!lyxrc.dialogs_iconify_with_main) + fl_winicontitle(form()->window, title_.c_str()); + + // assign an icon to the form + string const iconname = LibFileSearch("images", "lyx", "xpm"); + if (!iconname.empty()) { + unsigned int w, h; + icon_pixmap_ = fl_read_pixmapfile(fl_root, + iconname.c_str(), + &w, + &h, + &icon_mask_, + 0, 0, 0); + fl_set_form_icon(form(), icon_pixmap_, icon_mask_); + } +} - // work around dumb xforms sizing bug - minw_ = form()->w; - minh_ = form()->h; - fl_set_form_atclose(form(), C_WMHideCB, 0); +void FormBaseDeprecated::show() +{ + if (!form()) { + prepare_to_show(); } + // make sure the form is up to date. fl_freeze_form(form()); update(); fl_unfreeze_form(form()); @@ -126,35 +153,17 @@ void FormBaseDeprecated::show() fl_set_form_maxsize(form(), minw_, minh_); string const maximize_title = "LyX: " + title_; - int const iconify_policy = lyxrc.dialogs_iconify_with_main ? - FL_TRANSIENT : 0; + int const iconify_policy = + lyxrc.dialogs_iconify_with_main ? FL_TRANSIENT : 0; fl_show_form(form(), FL_PLACE_MOUSE | FL_FREE_SIZE, iconify_policy, maximize_title.c_str()); - - if (iconify_policy == 0) { - // set title for minimized form - string const minimize_title = title_; - fl_winicontitle(form()->window, minimize_title.c_str()); - - // assign an icon to form - string const iconname = LibFileSearch("images", "lyx", "xpm"); - if (!iconname.empty()) { - unsigned int w, h; - Pixmap icon_mask; - Pixmap const icon_p = fl_read_pixmapfile(fl_root, - iconname.c_str(), - &w, - &h, - &icon_mask, - 0, 0, 0); // this leaks - fl_set_form_icon(form(), icon_p, icon_mask); - } - } } + // For some strange reason known only to xforms, the tooltips can only + // be set on a form that is already visible... tooltips().set(); } diff --git a/src/frontends/xforms/FormBaseDeprecated.h b/src/frontends/xforms/FormBaseDeprecated.h index 5a8d181a7e..65c2c7dfd2 100644 --- a/src/frontends/xforms/FormBaseDeprecated.h +++ b/src/frontends/xforms/FormBaseDeprecated.h @@ -22,14 +22,14 @@ #pragma interface #endif -#include "LString.h" #include "xformsBC.h" #include "FeedbackController.h" - #include "forms_fwd.h" +#include "LString.h" #include #include +#include // for Pixmap class Buffer; class Dialogs; @@ -131,6 +131,15 @@ protected: // methods string title_; private: + /** Called on the first show() request, initialising various bits and + * pieces. + */ + void prepare_to_show(); + + /// Passed to the window manager to give a pretty little symbol ;-) + Pixmap icon_pixmap_; + /// + Pixmap icon_mask_; /// The dialog's minimum allowable dimensions. int minw_; /// diff --git a/src/frontends/xforms/XFormsView.C b/src/frontends/xforms/XFormsView.C index 1f504959d8..59e25ac9bc 100644 --- a/src/frontends/xforms/XFormsView.C +++ b/src/frontends/xforms/XFormsView.C @@ -57,7 +57,8 @@ int C_XFormsView_atCloseMainFormCB(FL_FORM * form, void * p) XFormsView::XFormsView(int width, int height) - : LyXView() + : LyXView(), + icon_pixmap_(0), icon_mask_(0) { create_form_form_main(width, height); fl_set_form_atclose(getForm(), C_XFormsView_atCloseMainFormCB, 0); @@ -73,6 +74,9 @@ XFormsView::XFormsView(int width, int height) XFormsView::~XFormsView() { + if (icon_pixmap_) + XFreePixmap(fl_get_display(), icon_pixmap_); + minibuffer_->freeze(); fl_hide_form(form_); fl_free_form(form_); @@ -159,19 +163,18 @@ void XFormsView::create_form_form_main(int width, int height) air, height - (25 + air), width - (2 * air), 25)); // assign an icon to main form - string iconname = LibFileSearch("images", "lyx", "xpm"); + string const iconname = LibFileSearch("images", "lyx", "xpm"); if (!iconname.empty()) { unsigned int w, h; - Pixmap lyx_p, lyx_mask; - lyx_p = fl_read_pixmapfile(fl_root, + icon_pixmap_ = fl_read_pixmapfile(fl_root, iconname.c_str(), &w, &h, - &lyx_mask, + &icon_mask_, 0, 0, - 0); // this leaks - fl_set_form_icon(getForm(), lyx_p, lyx_mask); + 0); + fl_set_form_icon(getForm(), icon_pixmap_, icon_mask_); } // set min size diff --git a/src/frontends/xforms/XFormsView.h b/src/frontends/xforms/XFormsView.h index 6577cc3bdb..bb81268005 100644 --- a/src/frontends/xforms/XFormsView.h +++ b/src/frontends/xforms/XFormsView.h @@ -19,6 +19,7 @@ #include "forms_fwd.h" #include "frontends/LyXView.h" +#include // for Pixmap class XMiniBuffer; @@ -81,5 +82,9 @@ private: /// the main form. FL_FORM * form_; + /// Passed to the window manager to give a pretty little symbol ;-) + Pixmap icon_pixmap_; + /// + Pixmap icon_mask_; }; #endif -- 2.39.2