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->sdl_surface, NULL, icon->impl->sdl_surface, &dest);
63 icon = new Surface(filename,USE_ALPHA);
79 void Button::change_icon(std::string icon_file, int /*mw*/, int /*mh*/)
83 if(!icon_file.empty())
85 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
86 if(!faccessible(filename))
87 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
91 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
95 icon = new Surface(filename,USE_ALPHA);
100 if(state == BUTTON_HOVER)
101 if(!popup_timer.check())
104 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
105 fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
108 bkgd->draw(rect.x,rect.y);
110 icon->draw(rect.x,rect.y);
116 if(0 > rect.x - (int)strlen(info.c_str()) * white_small_text->w)
117 i = rect.w + strlen(info.c_str()) * white_small_text->w;
120 white_small_text->draw(info.c_str(), i + rect.x - strlen(info.c_str()) * white_small_text->w, rect.y, 1);
121 sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
122 white_small_text->draw(str, i + rect.x - strlen(str) * white_small_text->w, rect.y + white_small_text->h+2, 1);
124 if(state == BUTTON_PRESSED)
125 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
126 else if(state == BUTTON_HOVER)
127 fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
135 void Button::event(SDL_Event &event)
137 SDLKey key = event.key.keysym.sym;
139 if(event.motion.x > rect.x && event.motion.x < rect.x + rect.w &&
140 event.motion.y > rect.y && event.motion.y < rect.y + rect.h)
142 if(event.type == SDL_MOUSEBUTTONDOWN)
144 if(event.button.button == SDL_BUTTON_LEFT)
146 state = BUTTON_PRESSED;
153 else if(event.type == SDL_MOUSEBUTTONUP)
155 if(event.button.button == SDL_BUTTON_LEFT && state == BUTTON_PRESSED)
157 state = BUTTON_CLICKED;
159 else if(event.button.button != SDL_BUTTON_LEFT && state != BUTTON_PRESSED)
165 if(state != BUTTON_PRESSED && state != BUTTON_CLICKED)
167 state = BUTTON_HOVER;
168 mouse_cursor->set_state(MC_LINK);
171 else if((event.type != SDL_KEYDOWN && event.type != SDL_KEYUP) || event.type == SDL_MOUSEMOTION)
180 if(event.type == SDL_KEYDOWN)
183 state = BUTTON_PRESSED;
185 else if(event.type == SDL_KEYUP)
187 if(state == BUTTON_PRESSED && key == shortcut)
188 state = BUTTON_CLICKED;
190 else if(event.type == SDL_MOUSEMOTION)
192 popup_timer.start(1500);
202 int Button::get_state()
205 if(state == BUTTON_CLICKED)
217 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
228 Button* ButtonPanel::event(SDL_Event& event)
232 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
235 if((*it)->state != BUTTON_NONE)
246 ButtonPanel::~ButtonPanel()
248 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
255 void ButtonPanel::draw()
260 fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
261 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
268 void ButtonPanel::additem(Button* pbutton, int tag)
270 int max_cols, row, col;
272 item.push_back(pbutton);
274 /* A button_panel takes control of the buttons it contains and arranges them */
276 max_cols = rect.w / bw;
278 row = (item.size()-1) / max_cols;
279 col = (item.size()-1) % max_cols;
281 item[item.size()-1]->rect.x = rect.x + col * bw;
282 item[item.size()-1]->rect.y = rect.y + row * bh;
283 item[item.size()-1]->tag = tag;