4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 Timer Button::popup_timer;
30 Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x, int y, int mw, int mh)
32 popup_timer.init(false);
36 if(!icon_file.empty())
38 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
39 if(!faccessible(filename))
40 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
44 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
47 if(mw != -1 || mh != -1)
49 icon = new Surface(filename,USE_ALPHA);
60 SDL_SoftStretch(icon->impl->get_sdl_surface(), NULL,
61 icon->impl->get_sdl_surface(), &dest);
64 icon = new Surface(filename,USE_ALPHA);
80 void Button::change_icon(std::string icon_file, int /*mw*/, int /*mh*/)
84 if(!icon_file.empty())
86 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
87 if(!faccessible(filename))
88 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
92 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
96 icon = new Surface(filename,USE_ALPHA);
101 if(state == BUTTON_HOVER)
102 if(!popup_timer.check())
105 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
106 fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
109 bkgd->draw(rect.x,rect.y);
111 icon->draw(rect.x,rect.y);
117 if(0 > rect.x - (int)strlen(info.c_str()) * white_small_text->w)
118 i = rect.w + strlen(info.c_str()) * white_small_text->w;
121 white_small_text->draw(info.c_str(), i + rect.x - strlen(info.c_str()) * white_small_text->w, rect.y, 1);
122 sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
123 white_small_text->draw(str, i + rect.x - strlen(str) * white_small_text->w, rect.y + white_small_text->h+2, 1);
125 if(state == BUTTON_PRESSED)
126 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
127 else if(state == BUTTON_HOVER)
128 fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
136 void Button::event(SDL_Event &event)
138 SDLKey key = event.key.keysym.sym;
140 if(event.motion.x > rect.x && event.motion.x < rect.x + rect.w &&
141 event.motion.y > rect.y && event.motion.y < rect.y + rect.h)
143 if(event.type == SDL_MOUSEBUTTONDOWN)
145 if(event.button.button == SDL_BUTTON_LEFT)
147 state = BUTTON_PRESSED;
154 else if(event.type == SDL_MOUSEBUTTONUP)
156 if(event.button.button == SDL_BUTTON_LEFT && state == BUTTON_PRESSED)
158 state = BUTTON_CLICKED;
160 else if(event.button.button != SDL_BUTTON_LEFT && state != BUTTON_PRESSED)
166 if(state != BUTTON_PRESSED && state != BUTTON_CLICKED)
168 state = BUTTON_HOVER;
169 mouse_cursor->set_state(MC_LINK);
172 else if((event.type != SDL_KEYDOWN && event.type != SDL_KEYUP) || event.type == SDL_MOUSEMOTION)
181 if(event.type == SDL_KEYDOWN)
184 state = BUTTON_PRESSED;
186 else if(event.type == SDL_KEYUP)
188 if(state == BUTTON_PRESSED && key == shortcut)
189 state = BUTTON_CLICKED;
191 else if(event.type == SDL_MOUSEMOTION)
193 popup_timer.start(1500);
203 int Button::get_state()
206 if(state == BUTTON_CLICKED)
218 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
229 Button* ButtonPanel::event(SDL_Event& event)
233 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
236 if((*it)->state != BUTTON_NONE)
247 ButtonPanel::~ButtonPanel()
249 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
256 void ButtonPanel::draw()
261 fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
262 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
269 void ButtonPanel::additem(Button* pbutton, int tag)
271 int max_cols, row, col;
273 item.push_back(pbutton);
275 /* A button_panel takes control of the buttons it contains and arranges them */
277 max_cols = rect.w / bw;
279 row = (item.size()-1) / max_cols;
280 col = (item.size()-1) % max_cols;
282 item[item.size()-1]->rect.x = rect.x + col * bw;
283 item[item.size()-1]->rect.y = rect.y + row * bh;
284 item[item.size()-1]->tag = tag;