00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "precomp.h"
00010 #include <stdio.h>
00011 #include "API/Core/Display/surface.h"
00012 #include "API/Core/Font/font.h"
00013 #include "API/Core/Resources/resource_manager.h"
00014 #include "menuitem_default.h"
00015
00016 CL_MenuItem_Default::CL_MenuItem_Default(
00017 CL_MenuItem *_menuitem,
00018 const CL_ComponentOptions &options,
00019 CL_StyleManager_Default *style)
00020 : CL_ComponentStyle(_menuitem), menuitem(_menuitem)
00021 {
00022 this->style = style;
00023
00024 menuitem->set_impl(this);
00025
00026 resources = style->get_resources();
00027 font = CL_Font::load("MenuItem/font", resources);
00028 sur_unchecked = CL_Surface::load("MenuItem/sur_unchecked", resources);
00029 sur_checked = CL_Surface::load("MenuItem/sur_checked", resources);
00030 sur_submenu = CL_Surface::load("MenuItem/sur_submenu", resources);
00031 if(options.exists("surface"))
00032 sur_icon = CL_Surface::load(options.get_value("surface").c_str(), resources);
00033 else
00034 sur_icon = NULL;
00035
00036 slot_paint = menuitem->sig_paint().connect(CL_CreateSlot(this, &CL_MenuItem_Default::on_paint));
00037
00038 draw_filled = true;
00039 draw_submenu_icon = false;
00040 }
00041
00042 CL_MenuItem_Default::~CL_MenuItem_Default()
00043 {
00044 resources->get_resource("MenuItem/font")->unload();
00045 resources->get_resource("MenuItem/sur_unchecked")->unload();
00046 resources->get_resource("MenuItem/sur_checked")->unload();
00047 resources->get_resource("MenuItem/sur_submenu")->unload();
00048 }
00049
00050 void CL_MenuItem_Default::set_draw_style(bool filled)
00051 {
00052 draw_filled = filled;
00053 }
00054
00055 int CL_MenuItem_Default::calc_height()
00056 {
00057 unsigned int height = font->get_height();
00058
00059 if(menuitem->is_separator())
00060 return height / 2;
00061
00062 if(sur_icon)
00063 if(sur_icon->get_height() > height)
00064 height = sur_icon->get_height();
00065
00066 if(sur_submenu)
00067 if(sur_submenu->get_height() > height)
00068 height = sur_submenu->get_height();
00069
00070 return height + 8;
00071 }
00072
00073 int CL_MenuItem_Default::calc_width()
00074 {
00075 return
00076 calc_surface_check_width() +
00077 calc_surface_icon_width() +
00078 calc_text_width() +
00079 calc_surface_submenu_width();
00080 }
00081
00082 int CL_MenuItem_Default::calc_text_width()
00083 {
00084 if(menuitem->get_text().length())
00085 return font->get_text_width(menuitem->get_text().c_str()) + 4;
00086 else
00087 return 0;
00088 }
00089
00090 int CL_MenuItem_Default::calc_surface_check_width()
00091 {
00092 if(menuitem->is_checkable())
00093 {
00094 unsigned int width = sur_checked->get_width();
00095 if(sur_unchecked->get_width() > width)
00096 width = sur_unchecked->get_width();
00097 return width + 4;
00098 }
00099 else
00100 return 0;
00101 }
00102
00103 int CL_MenuItem_Default::calc_surface_icon_width()
00104 {
00105 if(sur_icon)
00106 return sur_icon->get_width() + 4;
00107 else
00108 return 0;
00109 }
00110
00111 int CL_MenuItem_Default::calc_surface_submenu_width()
00112 {
00113 if(sur_submenu && menuitem->has_submenu())
00114 return sur_submenu->get_width() + 4;
00115 else
00116 return 0;
00117 }
00118
00119 void CL_MenuItem_Default::on_paint()
00120 {
00121 int height = menuitem->get_height();
00122 int width = menuitem->get_width();
00123
00124 if(menuitem->is_separator())
00125 {
00126 if(draw_filled)
00127 {
00128 style->draw_line(0, height / 2 - 1, width, height / 2 - 1, GUICOL_TEXT_DISABLED_FG);
00129 style->draw_line(0, height / 2, width, height / 2, GUICOL_TEXT_DISABLED_BG);
00130 }
00131 else
00132 {
00133 style->draw_line(width / 2 - 1, 0, width / 2 - 1, height, GUICOL_TEXT_DISABLED_FG);
00134 style->draw_line(width / 2, 0, width / 2, height, GUICOL_TEXT_DISABLED_BG);
00135 }
00136 }
00137 else {
00138 if(menuitem->is_highlighted())
00139 {
00140 if(draw_filled)
00141 style->fill_rect(0, 0, width, height, GUICOL_SELECTION);
00142 else
00143 style->draw_box(0, 0, width, height, GUICOL_BOTTOM_SHADE, GUICOL_BRIGHT_OUTLINE);
00144 }
00145
00146 if(menuitem->is_checkable())
00147 {
00148 if(menuitem->is_checked())
00149 {
00150 int sur_height = sur_checked->get_height();
00151 sur_checked->put_screen(surface_check_pos, (height - sur_height) / 2);
00152 }
00153 else
00154 {
00155 int sur_height = sur_unchecked->get_height();
00156 sur_unchecked->put_screen(surface_check_pos, (height - sur_height) / 2);
00157 }
00158 }
00159
00160 if(sur_icon)
00161 {
00162 int sur_height = sur_icon->get_height();
00163 sur_icon->put_screen(surface_icon_pos, (height - sur_height) / 2);
00164 }
00165
00166 if(sur_submenu && menuitem->has_submenu() && draw_submenu_icon)
00167 {
00168 int sur_height = sur_submenu->get_height();
00169 sur_submenu->put_screen(surface_submenu_pos, (height - sur_height) / 2);
00170 }
00171
00172 int sur_height = font->get_height();
00173 font->print_left(text_pos, (height - sur_height) / 2, menuitem->get_text().c_str());
00174 }
00175 }