From 4a931af6efb28f36b9de7f89809447198fd25d25 Mon Sep 17 00:00:00 2001 From: Thilo Graf Date: Wed, 13 Mar 2013 23:23:19 +0100 Subject: [PATCH] CComponentsForm/Header/Icons/Window: try to fix multiple inits and paints Some items had multiple inits and some calls of clearCCItems() have caused segfaults, hope this fix this. --- src/gui/components/cc.h | 3 +- src/gui/components/cc_frm.cpp | 47 +++++--- src/gui/components/cc_frm_header.cpp | 164 ++++++++++++++++----------- src/gui/components/cc_frm_icons.cpp | 19 +--- src/gui/components/cc_frm_window.cpp | 42 +++---- 5 files changed, 164 insertions(+), 111 deletions(-) diff --git a/src/gui/components/cc.h b/src/gui/components/cc.h index 6bc834ed9..8ba52e366 100644 --- a/src/gui/components/cc.h +++ b/src/gui/components/cc.h @@ -377,6 +377,7 @@ class CComponentsForm : public CComponentsItem std::vector v_cc_items; void initVarForm(); void clearCCItems(); + void paintForm(bool do_save_bg); public: CComponentsForm(); @@ -481,7 +482,7 @@ class CComponentsHeader : public CComponentsForm fb_pixel_t color_frame = COL_MENUCONTENT_PLUS_6, fb_pixel_t color_body = COL_MENUHEAD_PLUS_0, fb_pixel_t color_shadow = COL_MENUCONTENTDARK_PLUS_0); CComponentsHeader(const int x_pos, const int y_pos, const int w, const int h = 0, neutrino_locale_t caption_locale = NONEXISTANT_LOCALE, const char* icon_name = NULL, const int buttons = 0,bool has_shadow = CC_SHADOW_OFF, fb_pixel_t color_frame = COL_MENUCONTENT_PLUS_6, fb_pixel_t color_body = COL_MENUHEAD_PLUS_0, fb_pixel_t color_shadow = COL_MENUCONTENTDARK_PLUS_0); -// ~CComponentsHeader(); //inherited from CComponentsForm + ~CComponentsHeader(); void paint(bool do_save_bg = CC_SAVE_SCREEN_YES); void setHeaderText(const std::string& caption); diff --git a/src/gui/components/cc_frm.cpp b/src/gui/components/cc_frm.cpp index e49772a52..9e241cfc6 100644 --- a/src/gui/components/cc_frm.cpp +++ b/src/gui/components/cc_frm.cpp @@ -68,25 +68,32 @@ CComponentsForm::~CComponentsForm() void CComponentsForm::cleanCCForm() { #ifdef DEBUG_CC - printf("[CComponents] calling %s...\n", __FUNCTION__); + printf("[CComponentsForm] [%s - %d] clean up...\n", __FUNCTION__, __LINE__); #endif - hide(); - clearSavedScreen(); +// hide(); clearCCItems(); + clearSavedScreen(); clear(); } + + void CComponentsForm::clearCCItems() { - if (v_cc_items.empty()) + if (v_cc_items.empty()) return; #ifdef DEBUG_CC - printf("[CComponentsForm] %s... cleanup %d form cc-items\n", __FUNCTION__, v_cc_items.size()); + printf(" [CComponentsForm] %s... delete %d cc-item(s) \n", __FUNCTION__, v_cc_items.size()); #endif + for(size_t i=0; igetItemType()); +#endif + delete v_cc_items[i]; + v_cc_items[i] = NULL; + } } v_cc_items.clear(); } @@ -118,7 +125,16 @@ void CComponentsForm::initVarForm() void CComponentsForm::addCCItem(CComponentsItem* cc_Item) { - v_cc_items.push_back(cc_Item); + if (cc_Item){ +#ifdef DEBUG_CC + printf(" [CComponentsForm] %s-%d add cc_Item [type %d] [current count %d] \n", __FUNCTION__, __LINE__, cc_Item->getItemType(), v_cc_items.size()); +#endif + v_cc_items.push_back(cc_Item); + } +#ifdef DEBUG_CC + else + printf(" [CComponentsForm] %s-%d tried to add an empty or invalide cc_item !!!\n", __FUNCTION__, __LINE__); +#endif } int CComponentsForm::getCCItemId(CComponentsItem* cc_Item) @@ -191,13 +207,18 @@ void CComponentsForm::removeCCItem(const uint& cc_item_id) #endif } -void CComponentsForm::paint(bool do_save_bg) +void CComponentsForm::paintForm(bool do_save_bg) { //paint body paintInit(do_save_bg); - - //paint items - paintCCItems(); + + //paint + paintCCItems(); +} + +void CComponentsForm::paint(bool do_save_bg) +{ + paintForm(do_save_bg); } void CComponentsForm::paintCCItems() diff --git a/src/gui/components/cc_frm_header.cpp b/src/gui/components/cc_frm_header.cpp index 41417d1d0..23a3178d5 100644 --- a/src/gui/components/cc_frm_header.cpp +++ b/src/gui/components/cc_frm_header.cpp @@ -82,11 +82,11 @@ CComponentsHeader::CComponentsHeader( const int x_pos, const int y_pos, const in cch_locale_text = caption_locale; cch_icon_name = icon_name; cch_buttons = buttons; + initCCHDefaultButtons(); initCCHItems(); } - void CComponentsHeader::initVarHeader() { //CComponentsHeader @@ -105,7 +105,7 @@ void CComponentsHeader::initVarHeader() ccif_width = 0; cch_buttons = 0; cch_btn_offset = 8; - v_cch_btn.clear(); + v_cch_btn.clear(); //CComponentsForm initVarForm(); @@ -116,6 +116,15 @@ void CComponentsHeader::initVarHeader() corner_type = CORNER_TOP; } +CComponentsHeader::~CComponentsHeader() +{ +#ifdef DEBUG_CC + printf("[~CComponentsHeader] [%s - %d] delete...\n", __FUNCTION__, __LINE__); +#endif + v_cch_btn.clear(); + cleanCCForm(); +} + void CComponentsHeader::setHeaderText(const std::string& caption) { cch_text = caption; @@ -133,37 +142,51 @@ void CComponentsHeader::setHeaderIcon(const char* icon_name) void CComponentsHeader::initCCHeaderIcon() { + //reset cch_icon_w + cch_icon_w = cch_btn_offset; + + //init cch_icon_obj only if an icon available if (cch_icon_name == NULL) { - cch_icon_w = cch_btn_offset; + cch_icon_w = cch_btn_offset; + if (cch_icon_obj) + delete cch_icon_obj; + cch_icon_obj = NULL; return; } - if (cch_icon_name) - cch_icon_obj = new CComponentsPicture(cch_icon_x, cch_items_y, 0, 0, cch_icon_name); - cch_icon_obj->setWidth(height-2*fr_thickness); - cch_icon_obj->setHeight(height); - cch_icon_obj->setPictureAlign(CC_ALIGN_HOR_CENTER | CC_ALIGN_VER_CENTER); - cch_icon_obj->doPaintBg(false); + //create instance for cch_icon_obj + if (cch_icon_obj == NULL){ +#ifdef DEBUG_CC + printf(" [CComponentsHeader]\n [%s - %d] init header icon: %s\n", __FUNCTION__, __LINE__, cch_icon_name); +#endif + cch_icon_obj = new CComponentsPicture(cch_icon_x, cch_items_y, 0, 0, cch_icon_name); + //add item only one time + addCCItem(cch_icon_obj); //icon + } - //corner of icon item - cch_icon_obj->setCornerRadius(corner_rad-fr_thickness); - int cc_icon_corner_type = corner_type; - if (corner_type == CORNER_TOP_LEFT || corner_type == CORNER_TOP) - cc_icon_corner_type = CORNER_TOP_LEFT; - else - cc_icon_corner_type = CORNER_LEFT; - cch_icon_obj->setCornerType(cc_icon_corner_type); + //set properties for icon object + if (cch_icon_obj){ + cch_icon_obj->setWidth(height-2*fr_thickness); + cch_icon_obj->setHeight(height); + cch_icon_obj->setPictureAlign(CC_ALIGN_HOR_CENTER | CC_ALIGN_VER_CENTER); + cch_icon_obj->doPaintBg(false); - //set width of icon object - cch_icon_w = cch_icon_obj->getWidth(); + //set corner mode of icon item + cch_icon_obj->setCornerRadius(corner_rad-fr_thickness); + int cc_icon_corner_type = corner_type; + if (corner_type == CORNER_TOP_LEFT || corner_type == CORNER_TOP) + cc_icon_corner_type = CORNER_TOP_LEFT; + else + cc_icon_corner_type = CORNER_LEFT; + cch_icon_obj->setCornerType(cc_icon_corner_type); + + //set width of icon object + cch_icon_w = cch_icon_obj->getWidth(); + } } void CComponentsHeader::addHeaderButton(const std::string& button_name) { - if (cch_btn_obj){ - delete cch_btn_obj; - cch_btn_obj = NULL; - } v_cch_btn.push_back(button_name); initCCHeaderButtons(); } @@ -171,10 +194,9 @@ void CComponentsHeader::addHeaderButton(const std::string& button_name) void CComponentsHeader::removeHeaderButtons() { v_cch_btn.clear(); - if (cch_btn_obj){ - delete cch_btn_obj; - cch_btn_obj = NULL; - } + cch_btn_obj->removeAllIcons(); + initCCHeaderButtons(); + } void CComponentsHeader::initCCHDefaultButtons() @@ -187,6 +209,9 @@ void CComponentsHeader::initCCHDefaultButtons() v_cch_btn.push_back(NEUTRINO_ICON_BUTTON_INFO); if (cch_buttons & CC_BTN_MENU) v_cch_btn.push_back(NEUTRINO_ICON_BUTTON_MENU); +#ifdef DEBUG_CC + printf("[CComponentsHeader] %s added %d default buttons...\n", __FUNCTION__, v_cch_btn.size()); +#endif } // calculate minimal width of icon form @@ -203,41 +228,61 @@ void CComponentsHeader::initCCButtonFormSize() void CComponentsHeader::initCCHeaderButtons() { //exit if no button defined - if (v_cch_btn.empty()) + if (v_cch_btn.empty()) return; - + initCCButtonFormSize(); - cch_btn_obj = new CComponentsIconForm(); - cch_btn_obj->setDimensionsAll(0+width-ccif_width, 0, ccif_width-cch_btn_offset, height); - cch_btn_obj->doPaintBg(false); - cch_btn_obj->setIconOffset(cch_btn_offset); - cch_btn_obj->setIconAlign(CComponentsIconForm::CC_ICONS_FRM_ALIGN_RIGHT); - cch_btn_obj->removeAllIcons(); - cch_btn_obj->addIcon(v_cch_btn); + if (cch_btn_obj == NULL){ + cch_btn_obj = new CComponentsIconForm(); +#ifdef DEBUG_CC + printf(" [CComponentsHeader]\n [%s - %d] init header buttons...\n", __FUNCTION__, __LINE__); +#endif + //add button form + addCCItem(cch_btn_obj); //buttons + } + + //set button form properties + if (cch_btn_obj){ + cch_btn_obj->setDimensionsAll(0+width-ccif_width, 0, ccif_width-cch_btn_offset, height); + cch_btn_obj->doPaintBg(false); + cch_btn_obj->setIconOffset(cch_btn_offset); + cch_btn_obj->setIconAlign(CComponentsIconForm::CC_ICONS_FRM_ALIGN_RIGHT); + cch_btn_obj->removeAllIcons(); + cch_btn_obj->addIcon(v_cch_btn); + } } void CComponentsHeader::initCCHeaderText() { + //reset header text position first cch_text_x = cch_icon_x+cch_icon_w; - cch_text_obj = new CComponentsText(cch_text_x, cch_items_y, width-cch_icon_w-fr_thickness, height-2*fr_thickness, cch_text.c_str()); - cch_text_obj->setTextFont(cch_font); - cch_text_obj->setTextColor(cch_col_text); - cch_text_obj->setColorBody(col_body); - cch_text_obj->doPaintBg(false); - - //corner of text item - cch_text_obj->setCornerRadius(corner_rad-fr_thickness); - cch_text_obj->setCornerType(corner_type); + + //create cch_text_obj and add to collection + if (cch_text_obj == NULL){ +#ifdef DEBUG_CC + printf(" [CComponentsHeader]\n [%s - %d] init header text: %s\n", __FUNCTION__, __LINE__, cch_text.c_str()); +#endif + cch_text_obj = new CComponentsText(cch_text_x, cch_items_y, width-cch_icon_w-fr_thickness, height-2*fr_thickness, cch_text.c_str()); + //add text item + addCCItem(cch_text_obj); //text + } + + //set header text properties + if (cch_text_obj){ + cch_text_obj->setTextFont(cch_font); + cch_text_obj->setTextColor(cch_col_text); + cch_text_obj->setColorBody(col_body); + cch_text_obj->doPaintBg(false); + + //corner of text item + cch_text_obj->setCornerRadius(corner_rad-fr_thickness); + cch_text_obj->setCornerType(corner_type); + } } void CComponentsHeader::initCCHItems() { -#if 0 - //clean up first possible old item objects, includes delete and clean up vector - clearCCItems(); -#endif - //init icon initCCHeaderIcon(); @@ -246,22 +291,13 @@ void CComponentsHeader::initCCHItems() //init buttons initCCHeaderButtons(); - - //add elements - if (cch_icon_obj) - addCCItem(cch_icon_obj); //icon - if (cch_text_obj) - addCCItem(cch_text_obj); //text - if (cch_btn_obj) - addCCItem(cch_btn_obj); //buttons - } void CComponentsHeader::paint(bool do_save_bg) { - //paint body - paintInit(do_save_bg); - - //paint - paintCCItems(); + //prepare items + initCCHItems(); + + //paint form contents + paintForm(do_save_bg); } diff --git a/src/gui/components/cc_frm_icons.cpp b/src/gui/components/cc_frm_icons.cpp index 503cb57c0..fe79d9495 100644 --- a/src/gui/components/cc_frm_icons.cpp +++ b/src/gui/components/cc_frm_icons.cpp @@ -40,6 +40,7 @@ CComponentsIconForm::CComponentsIconForm() initVarIconForm(); } + CComponentsIconForm::CComponentsIconForm(const int x_pos, const int y_pos, const int w, const int h, const std::vector v_icon_names, bool has_shadow, fb_pixel_t color_frame, fb_pixel_t color_body, fb_pixel_t color_shadow) { @@ -111,9 +112,8 @@ int CComponentsIconForm::getIconId(const std::string& icon_name) //to remove old items before add new icons, otherwise icons will be appended. void CComponentsIconForm::removeAllIcons() { - if (!v_icons.empty()) - v_icons.clear(); clearCCItems(); + v_icons.clear(); } //get maximal form height depends of biggest icon height, but don't touch defined form height @@ -128,9 +128,6 @@ void CComponentsIconForm::initMaxHeight(int *pheight) void CComponentsIconForm::initCCIcons() { - //clean up first possible old item objects, includes delete and clean up vector and icons - clearCCItems(); - int ccp_y = 0; int ccp_h = 0; int ccp_w = 0; @@ -156,8 +153,7 @@ void CComponentsIconForm::initCCIcons() for (size_t i= 0; i< i_cnt; i++){ //create new cc-picture item object - CComponentsPicture *ccp = NULL; - ccp = new CComponentsPicture(ccp_x, ccp_y, ccp_w, h, v_icons[i]); + CComponentsPicture *ccp = new CComponentsPicture(ccp_x, ccp_y, ccp_w, h, v_icons[i]); ccp->setPictureAlign(CC_ALIGN_HOR_CENTER | CC_ALIGN_VER_CENTER); ccp->doPaintBg(false); //add item to form @@ -195,10 +191,7 @@ void CComponentsIconForm::paint(bool do_save_bg) { //init and add icons initCCIcons(); - - //paint body - paintInit(do_save_bg); - - //paint - paintCCItems(); + + //paint form contents + paintForm(do_save_bg); } diff --git a/src/gui/components/cc_frm_window.cpp b/src/gui/components/cc_frm_window.cpp index 1ed996f6d..b5db85d0d 100644 --- a/src/gui/components/cc_frm_window.cpp +++ b/src/gui/components/cc_frm_window.cpp @@ -44,8 +44,9 @@ CComponentsWindow::CComponentsWindow() CComponentsWindow::~CComponentsWindow() { - if (ccw_head) - delete ccw_head; +#ifdef DEBUG_CC + printf("[~CComponentsWindow] [%s - %d] delete...\n", __FUNCTION__, __LINE__); +#endif cleanCCForm(); } @@ -75,25 +76,29 @@ void CComponentsWindow::setWindowCaption(neutrino_locale_t locale_text) void CComponentsWindow::initHeader() { - if (ccw_head){ - delete ccw_head; - ccw_head = NULL; - } - ccw_head = new CComponentsHeader(); + if (ccw_head == NULL){ + ccw_head = new CComponentsHeader(); + initHeader(); + //add header item only one time + addCCItem(ccw_head); + } - ccw_head->setXPos(0); - ccw_head->setYPos(0); - ccw_head->setWidth(width); - ccw_head->setHeaderIcon(ccw_icon_name); - ccw_head->setHeaderText(ccw_caption); + //set header properties + if (ccw_head){ + ccw_head->setXPos(0); + ccw_head->setYPos(0); + ccw_head->setWidth(width); + ccw_head->setHeaderIcon(ccw_icon_name); + ccw_head->setHeaderText(ccw_caption); + } } void CComponentsWindow::initCCWItems() { +#ifdef DEBUG_CC + printf("[CComponentsWindow] [%s - %d] init items...\n", __FUNCTION__, __LINE__); +#endif initHeader(); - - if (ccw_head) - addCCItem(ccw_head); } void CComponentsWindow::paint(bool do_save_bg) @@ -101,9 +106,6 @@ void CComponentsWindow::paint(bool do_save_bg) //prepare items before paint initCCWItems(); - //paint body - paintInit(do_save_bg); - - //paint - paintCCItems(); + //paint form contents + paintForm(do_save_bg); }