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 "pgwindow.h"
00030 #include "pgapplication.h"
00031 #include "pgtheme.h"
00032 #include "pgbutton.h"
00033
00034 PG_Window::PG_Window(PG_Widget* parent, const PG_Rect& r, const std::string& windowtext, WindowFlags flags, const std::string& style, int heightTitlebar)
00035 : PG_ThemeWidget(parent, r), my_heightTitlebar(heightTitlebar), my_moveMode(false),
00036 my_showCloseButton((flags & PG_Window::SHOW_CLOSE) != 0), my_showMinimizeButton((flags & PG_Window::SHOW_MINIMIZE) != 0), my_moveable(true) {
00037
00038 my_titlebar = new PG_ThemeWidget(this, PG_Rect(0, 0, my_width, my_heightTitlebar), style);
00039 my_titlebar->EnableReceiver(false);
00040
00041 int w = my_width - my_heightTitlebar*2;
00042 if(w < 0 ) {
00043 w = 0;
00044 }
00045 my_labelTitle = new PG_Label(my_titlebar, PG_Rect(my_heightTitlebar, 0, w, my_heightTitlebar), windowtext, style);
00046 my_labelTitle->SetAlignment(PG_Label::CENTER);
00047
00048 my_buttonClose = new PG_Button(my_titlebar);
00049 my_buttonClose->SetID(IDWINDOW_CLOSE);
00050 my_buttonClose->sigClick.connect(slot(*this, &PG_Window::handleButtonClick));
00051
00052 my_buttonMinimize = new PG_Button(my_titlebar);
00053 my_buttonMinimize->SetID(IDWINDOW_MINIMIZE);
00054 my_buttonMinimize->sigClick.connect(slot(*this, &PG_Window::handleButtonClick));
00055
00056 LoadThemeStyle(style);
00057
00058 if(!my_showCloseButton) {
00059 my_buttonClose->Hide();
00060 }
00061 if(!my_showMinimizeButton) {
00062 my_buttonMinimize->Hide();
00063 }
00064 }
00065
00066 PG_Window::~PG_Window() {}
00067
00068 void PG_Window::SetText(const std::string& text) {
00069 my_labelTitle->SetText(text);
00070 }
00071
00072 void PG_Window::SetTitle(const std::string& title, PG_Label::TextAlign alignment) {
00073 my_labelTitle->SetAlignment(alignment);
00074 SetText(title);
00075 }
00076
00077 const PG_String& PG_Window::GetText() {
00078 return my_labelTitle->GetText();
00079 }
00080
00081 const PG_String& PG_Window::GetTitle() {
00082 return GetText();
00083 }
00084
00085 void PG_Window::RecalcPositions() {
00086 my_titlebar->SizeWidget(my_width, my_heightTitlebar);
00087 int w = my_width - my_heightTitlebar*2;
00088 if(w < 0) {
00089 w = 0;
00090 }
00091 my_labelTitle->MoveWidget(PG_Rect(my_heightTitlebar, 0, w, my_heightTitlebar));
00092 w = my_width - my_heightTitlebar;
00093 if(w < 0) {
00094 w = 0;
00095 }
00096 my_buttonClose->MoveWidget(PG_Rect(w, 0, my_heightTitlebar, my_heightTitlebar));
00097 my_buttonMinimize->MoveWidget(PG_Rect(0, 0, my_heightTitlebar, my_heightTitlebar));
00098 }
00099
00100 void PG_Window::LoadThemeStyle(const std::string& widgettype) {
00101 PG_Theme* t = PG_Application::GetTheme();
00102
00103 PG_ThemeWidget::LoadThemeStyle(widgettype, "Window");
00104
00105 my_titlebar->LoadThemeStyle(widgettype, "Titlebar");
00106
00107 t->GetProperty(widgettype, "Titlebar", "height", my_heightTitlebar);
00108
00109 PG_Color c = my_labelTitle->GetFontColor();
00110 t->GetColor(widgettype, "Titlebar", "textcolor", c);
00111 SetTitlebarColor(c);
00112
00113 t->GetProperty(widgettype, "Titlebar", "showclosebutton", my_showCloseButton);
00114 my_buttonClose->LoadThemeStyle(widgettype, "CloseButton");
00115
00116 t->GetProperty(widgettype, "Titlebar", "showminimizebutton", my_showMinimizeButton);
00117 my_buttonMinimize->LoadThemeStyle(widgettype, "MinimizeButton");
00118
00119 if(my_showCloseButton) {
00120 my_buttonClose->Show();
00121 }
00122 if(my_showMinimizeButton) {
00123 my_buttonMinimize->Show();
00124 }
00125
00126 RecalcPositions();
00127 }
00128
00129 void PG_Window::eventSizeWidget(Uint16 w, Uint16 h) {
00130
00131 PG_ThemeWidget::eventSizeWidget(w, h);
00132
00133 my_width = w;
00134 my_height = h;
00135
00136 RecalcPositions();
00137 }
00138
00139 void PG_Window::eventBlit(SDL_Surface* srf, const PG_Rect& src, const PG_Rect& dst) {
00140
00141 PG_ThemeWidget::eventBlit(srf, src, dst);
00142
00143 PG_Rect client(0, my_heightTitlebar, my_width, my_height-my_heightTitlebar);
00144 DrawBorder(client, my_bordersize);
00145
00146 client.my_xpos++;
00147 client.my_ypos++;
00148 client.my_width -= 2;
00149 client.my_height -= 2;
00150
00151 DrawBorder(client, my_bordersize, false);
00152 }
00153
00154 bool PG_Window::eventMouseButtonDown(const SDL_MouseButtonEvent* button) {
00155 if (!my_moveable)
00156 return false;
00157
00158 PG_Rect ta(*this);
00159 ta.my_width = my_titlebar->w;
00160 ta.my_height = my_titlebar->h;
00161
00162 int x,y;
00163 x = button->x;
00164 y = button->y;
00165
00166 if((ta.my_xpos <= x) && (x <= ta.my_xpos + ta.my_width)) {
00167 if((ta.my_ypos <= y) && (y <= ta.my_ypos + ta.my_height)) {
00168 my_moveMode = true;
00169 my_moveDelta.x = ta.my_xpos - x;
00170 my_moveDelta.y = ta.my_ypos - y;
00171 Show();
00172 SetCapture();
00173 return true;
00174 }
00175 }
00176
00177 Show();
00178
00179 return false;
00180 }
00181
00182 bool PG_Window::eventMouseButtonUp(const SDL_MouseButtonEvent* button) {
00183 SDL_Surface* screen = PG_Application::GetScreen();
00184 int x,y;
00185
00186 x = button->x;
00187 y = button->y;
00188
00189 x += my_moveDelta.x;
00190 y += my_moveDelta.y;
00191
00192 if(x < 0)
00193 x=0;
00194 if(y < 0)
00195 y=0;
00196 if(x+my_width > (screen->w - my_width))
00197 x = (screen->w - my_width);
00198 if(y+my_height > (screen->h - my_height))
00199 y = (screen->h - my_height);
00200
00201 if(my_moveMode) {
00202 my_moveMode = false;
00203 ReleaseCapture();
00204 return true;
00205 }
00206
00207 return false;
00208 }
00209
00210 bool PG_Window::eventMouseMotion(const SDL_MouseMotionEvent* motion) {
00211 if(!my_moveMode) {
00212 return PG_Widget::eventMouseMotion(motion);
00213 }
00214
00215 SDL_Surface* screen = PG_Application::GetScreen();
00216 int x = motion->x;
00217 int y = motion->y;
00218
00219 x += my_moveDelta.x;
00220 y += my_moveDelta.y;
00221
00222 if(GetParent() != NULL) {
00223 PG_Point pos = GetParent()->ScreenToClient(x,y);
00224 x = pos.x;
00225 y = pos.y;
00226 if(x+my_width > GetParent()->Width())
00227 x = GetParent()->Width() - my_width;
00228 if(y+my_height > GetParent()->Height())
00229 y = GetParent()->Height() - my_height;
00230 }
00231
00232 if(x+my_width > screen->w)
00233 x = (screen->w - my_width);
00234 if(y+my_height > screen->h)
00235 y = (screen->h - my_height);
00236
00237 if(x < 0)
00238 x=0;
00239 if(y < 0)
00240 y=0;
00241
00242 MoveWidget(x,y);
00243 return true;
00244 }
00245
00246 bool PG_Window::handleButtonClick(PG_Button* button) {
00247 switch(button->GetID()) {
00248
00249 case IDWINDOW_CLOSE:
00250 Hide();
00251 sigClose(this);
00252 break;
00253
00254
00255 case IDWINDOW_MINIMIZE:
00256 Hide();
00257 sigMinimize(this);
00258 break;
00259 }
00260
00261
00262 QuitModal();
00263
00264 return true;
00265 }
00266
00267 void PG_Window::SetTitlebarColor(const PG_Color& c) {
00268 my_labelTitle->SetFontColor(c);
00269 }
00270
00271 PG_Color PG_Window::GetTitlebarColor() {
00272 return my_labelTitle->GetFontColor();
00273 }
00274
00275 void PG_Window::SetIcon(const std::string& filename) {
00276 my_labelTitle->SetIcon(filename);
00277 }
00278
00279 void PG_Window::SetIcon(SDL_Surface* icon) {
00280 my_labelTitle->SetIcon(icon);
00281 }
00282
00283 SDL_Surface* PG_Window::GetIcon() {
00284 return my_labelTitle->GetIcon();
00285 }
00286
00287 void PG_Window::SetTitlebarHeight(Uint8 height) {
00288 my_heightTitlebar = height;
00289 RecalcPositions();
00290 }
00291
00292 Uint8 PG_Window::GetTitlebarHeight() {
00293 return my_heightTitlebar;
00294 }
00295
00296 void PG_Window::SetMoveable(bool moveable) {
00297 my_moveable = moveable;
00298 }