00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "paragui.h"
00030 #include "pglabel.h"
00031 #include "pgapplication.h"
00032 #include "pgtheme.h"
00033 #include "pglog.h"
00034
00035 #include "propstrings_priv.h"
00036
00037 PG_Label::PG_Label(PG_Widget* parent, const PG_Rect& r, const std::string& text, const std::string& style) :
00038 PG_Widget(parent, r),
00039 my_indent(0) {
00040
00041 my_alignment = LEFT;
00042 my_srfIcon = NULL;
00043 my_freeicon = false;
00044
00045 SetText(text);
00046 LoadThemeStyle(style);
00047 }
00048
00049 PG_Label::~PG_Label() {
00050 if(my_freeicon) {
00051 PG_Application::UnloadSurface(my_srfIcon);
00052 }
00053 }
00054
00055 void PG_Label::LoadThemeStyle(const std::string& style) {
00056 if(style != PG_PropStr::Label) {
00057 PG_Label::LoadThemeStyle(PG_PropStr::Label, PG_PropStr::Label);
00058 }
00059 PG_Label::LoadThemeStyle(style, PG_PropStr::Label);
00060 }
00061
00062 void PG_Label::LoadThemeStyle(const std::string& widgettype, const std::string& object) {
00063 PG_Theme* t = PG_Application::GetTheme();
00064
00065 const std::string& s = t->FindString(widgettype, object, PG_PropStr::label);
00066
00067 if(!s.empty()) {
00068 SetText(s);
00069 }
00070
00071 t->GetAlignment(widgettype, object, PG_PropStr::alignment, my_alignment);
00072
00073 PG_Widget::LoadThemeStyle(widgettype, object);
00074 }
00075
00076 void PG_Label::eventBlit(SDL_Surface* srf, const PG_Rect& src, const PG_Rect& dst) {
00077 PG_Rect my_rectLabel;
00078 int xshift = my_indent;
00079
00080
00081 if(my_srfIcon != NULL) {
00082 xshift = my_srfIcon->w + my_indent;
00083
00084 PG_Rect icon_rect(
00085 my_xpos + my_indent,
00086 my_ypos + (my_height - my_srfIcon->h)/2,
00087 my_srfIcon->w,
00088 my_srfIcon->h);
00089
00090 PG_Rect icon_src;
00091 PG_Rect icon_dst;
00092
00093 GetClipRects(icon_src, icon_dst, icon_rect);
00094
00095 PG_Widget::eventBlit(my_srfIcon, icon_src, icon_dst);
00096 }
00097
00098 Uint16 wl;
00099 Uint16 hl;
00100
00101 GetTextSize(wl, hl);
00102
00103 switch (my_alignment) {
00104 case LEFT:
00105 my_rectLabel.my_xpos = xshift;
00106 my_rectLabel.my_ypos = (my_height - hl) >> 1;
00107 break;
00108
00109 case RIGHT:
00110 my_rectLabel.my_xpos = (my_width - wl);
00111 my_rectLabel.my_ypos = (my_height - hl) >> 1;
00112 break;
00113
00114 case CENTER:
00115 my_rectLabel.my_xpos = (my_width - wl) >> 1;
00116 my_rectLabel.my_ypos = (my_height - hl) >> 1;
00117 break;
00118 }
00119
00120 DrawText(my_rectLabel, my_text);
00121 }
00122
00124 void PG_Label::eventDraw(SDL_Surface* surface, const PG_Rect& rect) {}
00125
00126 void PG_Label::SetAlignment(TextAlign a) {
00127 my_alignment = a;
00128 Update();
00129 }
00130
00131 SDL_Surface* PG_Label::SetIcon(SDL_Surface* icon) {
00132
00133 if((icon != my_srfIcon) && my_freeicon) {
00134 PG_Application::UnloadSurface(my_srfIcon);
00135 }
00136
00137 my_srfIcon = icon;
00138 my_freeicon = false;
00139 Update();
00140
00141 return my_srfIcon;
00142 }
00143
00144 SDL_Surface* PG_Label::SetIcon(const std::string& filename) {
00145 if(my_freeicon) {
00146 PG_Application::UnloadSurface(my_srfIcon);
00147 }
00148
00149 my_srfIcon = PG_Application::LoadSurface(filename);
00150 my_freeicon = true;
00151 Update();
00152
00153 return my_srfIcon;
00154 }
00155
00156 SDL_Surface* PG_Label::GetIcon() {
00157 return my_srfIcon;
00158 }
00159
00160 void PG_Label::SetIndent(Uint16 indent) {
00161 my_indent = indent;
00162 }
00163
00164 Uint16 PG_Label::GetIndent() {
00165 return my_indent;
00166 }
00167
00168 void PG_Label::SetSizeByText(int Width, int Height, const std::string& Text) {
00169 if (GetIcon() == NULL) {
00170 PG_Widget::SetSizeByText(Width, Height, Text);
00171 return;
00172 }
00173
00174 Uint16 w,h;
00175 int baselineY;
00176
00177 if(Text.empty()) {
00178 if (!PG_FontEngine::GetTextSize(my_text, GetFont(), &w, &h, &baselineY)) {
00179 return;
00180 }
00181 } else {
00182 PG_String ytext = Text;
00183 if (!PG_FontEngine::GetTextSize(ytext, GetFont(), &w, &h, &baselineY)) {
00184 return;
00185 }
00186 }
00187
00188
00189 if (GetIcon()->w > w) {
00190 my_width = GetIcon()->w + my_indent + Width;
00191 } else {
00192 my_width = w + GetIcon()->w + my_indent + Width;
00193 }
00194 my_height = PG_MAX(GetIcon()->h, h + baselineY) + Height + baselineY;
00195 }