From 84fe7cf1342f3964d1460ab4eba58f1b9deb2d47 Mon Sep 17 00:00:00 2001 From: Thilo Graf Date: Mon, 12 Nov 2012 10:31:52 +0100 Subject: [PATCH] CComponents: add sub class CComponentsIconForm based upon CComponentsForm Collects only icons.You can add icons step by step or with a vector and paint the form at once. Width and height are dynamic calculated if parameters width or height are smaller then summary of lenght of all added icons. It's also possible to manipulate the icon array with members: - insertIcon() - removeIcon() - removeAllIcons() TODO: support for resizable images --- src/gui/components/cc.h | 29 ++++++ src/gui/components/components.cpp | 141 +++++++++++++++++++++++++++++- 2 files changed, 167 insertions(+), 3 deletions(-) diff --git a/src/gui/components/cc.h b/src/gui/components/cc.h index 509a16523..282ca76f9 100644 --- a/src/gui/components/cc.h +++ b/src/gui/components/cc.h @@ -102,6 +102,7 @@ typedef struct comp_element_data_t void* handler2; }comp_element_data_struct_t; + #define CC_WIDTH_MIN 16 #define CC_HEIGHT_MIN 16 #define CC_SHADOW_ON true @@ -478,4 +479,32 @@ class CComponentsHeader : public CComponentsForm void setHeaderIcon(const char* icon_name); }; +class CComponentsIconForm : public CComponentsForm +{ + private: + std::vector v_icons; + int ccif_offset; + + protected: + void initVarIconForm(); + + public: + 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 = 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); + + void paint(bool do_save_bg = CC_SAVE_SCREEN_YES); + void initCCIcons(); + void addIcon(const std::string& icon_name); + void addIcon(std::vector icon_name); + void removeIcons(){v_icons.clear();}; + void insertIcon(const uint& icon_id, const std::string& icon_name); + void removeIcon(const uint& icon_id); + void removeIcon(const std::string& icon_name); + void removeAllIcons(); + int getIconId(const std::string& icon_name); + + void setIconOffset(const int offset){ccif_offset = offset;}; +}; + #endif diff --git a/src/gui/components/components.cpp b/src/gui/components/components.cpp index 9f76878bb..b8e1143be 100644 --- a/src/gui/components/components.cpp +++ b/src/gui/components/components.cpp @@ -1530,6 +1530,9 @@ CComponentsForm::~CComponentsForm() void CComponentsForm::clearCCItems() { + if (v_cc_items.empty()) + return; + for(size_t i=0; i v_icon_names, bool has_shadow, + fb_pixel_t color_frame, fb_pixel_t color_body, fb_pixel_t color_shadow) +{ + initVarIconForm(); + + x = x_pos; + y = y_pos; + width = w; + height = h; + shadow = has_shadow; + col_frame = color_frame; + col_body = color_body; + col_shadow = color_shadow; + + v_icons = v_icon_names; +} + +void CComponentsIconForm::initVarIconForm() +{ + //CComponentsForm + initVarForm(); + + //set default width and height to 0, this causes a dynamic adaptation of width and height of form + width = 0; + height = 0; + + v_icons.clear(); + ccif_offset = 2; +} + +void CComponentsIconForm::addIcon(const std::string& icon_name) +{ + v_icons.push_back(icon_name); +} + +void CComponentsIconForm::addIcon(std::vector icon_name) +{ + for (size_t i= 0; i< icon_name.size(); i++) + v_icons.push_back(icon_name[i]); +} + +void CComponentsIconForm::insertIcon(const uint& icon_id, const std::string& icon_name) +{ + v_icons.insert(v_icons.begin()+icon_id, icon_name); +} + +void CComponentsIconForm::removeIcon(const uint& icon_id) +{ + v_icons.erase(v_icons.begin()+icon_id); +} + +void CComponentsIconForm::removeIcon(const std::string& icon_name) +{ + int id = getIconId(icon_name); + removeIcon(id); +} + +int CComponentsIconForm::getIconId(const std::string& icon_name) +{ + for (size_t i= 0; i< v_icons.size(); i++) + if (v_icons[i] == icon_name) + return i; + return -1; +} + +//For existing instances it's recommended +//to remove old items before add new icons, otherwise icons will be appended. +void CComponentsIconForm::removeAllIcons() +{ + clearCCItems(); + if (!v_icons.empty()) + v_icons.clear(); +} + +void CComponentsIconForm::initCCIcons() +{ + //clean up first possible old item objects, includes delete and clean up vector and icons + clearCCItems(); + + int ccp_x = 0+fr_thickness; + int ccp_y = 0; + int ccp_w = 0; + int ccp_h = 0; + + //detect maximal form height + int h = height; + for (size_t i= 0; i< v_icons.size(); i++){ + frameBuffer->getIconSize(v_icons[i].c_str(), &ccp_w, &ccp_h); + h = max(ccp_h, h)/*+2*fr_thickness*/; + } + + //init and add item objects + for (size_t i= 0; i< v_icons.size(); i++){ + frameBuffer->getIconSize(v_icons[i].c_str(), &ccp_w, &ccp_h); + + CComponentsPicture *ccp = NULL; + 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); + + addCCItem(ccp); + + ccp_x += ccif_offset + ccp_w; + } + + //calculate form width and height + int w_tmp = 0; + int h_tmp = 0; + for (size_t i= 0; i< v_cc_items.size(); i++){ + w_tmp += v_cc_items[i]->getWidth()+ccif_offset+fr_thickness; + h_tmp = max(h_tmp, v_cc_items[i]->getHeight()+2*fr_thickness); + } + width = max(w_tmp, width)-ccif_offset; //terminate last offset + height = max(h_tmp, height); +} + +void CComponentsIconForm::paint(bool do_save_bg) +{ + //init and add icons + initCCIcons(); + + //paint body + paintInit(do_save_bg); + + //paint + paintCCItems(); +}