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.
This commit is contained in:
2013-03-13 23:23:19 +01:00
parent da356d94da
commit 4a931af6ef
5 changed files with 164 additions and 111 deletions

View File

@@ -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);
}