- Avoid some expensive SDL_GetTicks() calls
[supertux.git] / src / gui / menu.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <sys/types.h>
23 #include <ctype.h>
24
25 #include <iostream>
26 #include <sstream>
27 #include <cstdlib>
28 #include <cstdio>
29 #include <string>
30 #include <cassert>
31 #include <stdexcept>
32
33 #include "menu.hpp"
34 #include "mainloop.hpp"
35 #include "video/drawing_context.hpp"
36 #include "gettext.hpp"
37 #include "math/vector.hpp"
38 #include "main.hpp"
39 #include "resources.hpp"
40 #include "timer.hpp"
41 #include "control/joystickkeyboardcontroller.hpp"
42
43 static const float MENU_REPEAT_INITIAL = 0.4;
44 static const float MENU_REPEAT_RATE = 0.2;
45 static const float FLICK_CURSOR_TIME = 0.5;
46
47 extern SDL_Surface* screen;
48
49 std::vector<Menu*> Menu::last_menus;
50 Menu* Menu::current_ = 0;
51 Font* Menu::default_font;
52 Font* Menu::active_font;
53 Font* Menu::deactive_font;
54 Font* Menu::label_font;
55 Font* Menu::field_font;
56
57 /* just displays a Yes/No text that can be used to confirm stuff */
58 bool confirm_dialog(Surface *background, std::string text)
59 {
60   //Surface* cap_screen = Surface::CaptureScreen();
61   Menu* dialog = new Menu;
62   dialog->add_deactive(-1, text);
63   dialog->add_hl();
64   dialog->add_entry(true, _("Yes"));
65   dialog->add_entry(false, _("No"));
66   dialog->add_hl();
67   
68   Menu::set_current(dialog);
69
70   DrawingContext context;
71
72   // TODO make this a screen and not another mainloop...
73   while(true)
74     {
75       SDL_Event event;
76       while (SDL_PollEvent(&event)) {
77         if(event.type == SDL_QUIT)
78           main_loop->quit();
79         main_controller->process_event(event);
80         dialog->event(event);
81       }
82
83       if(background == NULL)
84         context.draw_gradient(Color(0.8, 0.95, 0.85), Color(0.8, 0.8, 0.8),
85                               LAYER_BACKGROUND0);
86       else
87         context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
88
89       dialog->draw(context);
90       dialog->update();
91
92       switch (dialog->check())
93         {
94         case true:
95           //delete cap_screen;
96           Menu::set_current(0);
97           delete dialog;
98           return true;
99           break;
100         case false:
101           //delete cap_screen;
102           Menu::set_current(0);
103           delete dialog;
104           return false;
105           break;
106         default:
107           break;
108         }
109
110       mouse_cursor->draw(context);
111       context.do_drawing();
112       SDL_Delay(25);
113     }
114
115   return false;
116 }
117
118 void
119 Menu::push_current(Menu* pmenu)
120 {
121   if (current_)
122     last_menus.push_back(current_);
123
124   current_ = pmenu;
125   current_->effect_time = real_time;
126 }
127
128 void
129 Menu::pop_current()
130 {
131   if (last_menus.size() >= 1) {
132     current_ = last_menus.back();
133     current_->effect_time = real_time;
134     last_menus.pop_back();
135   } else {
136     current_ = 0;
137   }
138 }
139
140 void
141 Menu::set_current(Menu* menu)
142 {
143   last_menus.clear();
144
145   if (menu)
146     menu->effect_time = real_time;
147
148   current_ = menu;
149   // just to be sure...
150   main_controller->reset();
151 }
152
153 MenuItem::MenuItem(MenuItemKind _kind, int _id)
154   : kind(_kind) , id(_id)
155 {
156   toggled = false;
157   selected = false;
158   target_menu = 0;
159 }
160
161 void
162 MenuItem::change_text(const  std::string& text_)
163 {
164   text = text_;
165 }
166
167 void
168 MenuItem::change_input(const  std::string& text_)
169 {
170   input = text_;
171 }
172
173 std::string MenuItem::get_input_with_symbol(bool active_item)
174 {
175   if(!active_item) {
176     input_flickering = true;
177   } else {
178     input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
179   }
180
181   char str[1024];
182   if(input_flickering)
183     sprintf(str,"%s ",input.c_str());
184   else
185     sprintf(str,"%s_",input.c_str());
186
187   std::string string = str;
188
189   return string;
190 }
191
192 Menu::~Menu()
193 {
194   for(std::vector<MenuItem*>::iterator i = items.begin();
195       i != items.end(); ++i)
196     delete *i;
197 #ifdef DEBUG
198   assert(current_ != this);
199 #else
200   if(current_ == this)
201     current_ = NULL;
202 #endif
203 }
204
205 Menu::Menu()
206 {
207   hit_item = -1;
208   menuaction = MENU_ACTION_NONE;
209   delete_character = 0;
210   mn_input_char = '\0';
211
212   pos_x        = SCREEN_WIDTH/2;
213   pos_y        = SCREEN_HEIGHT/2;
214   arrange_left = 0;
215   active_item  = -1;
216
217   checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
218   checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
219   back.reset(new Surface("images/engine/menu/arrow-back.png"));
220   arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
221   arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
222 }
223
224 void Menu::set_pos(float x, float y, float rw, float rh)
225 {
226   pos_x = x + get_width() * rw;
227   pos_y = y + get_height() * rh;
228 }
229
230 /* Add an item to a menu */
231 void
232 Menu::additem(MenuItem* item)
233 {
234   items.push_back(item);
235
236   /* If a new menu is being built, the active item shouldn't be set to
237    * something that isnt selectable. Set the active_item to the first
238    * selectable item added
239    */
240   if (active_item == -1
241       && item->kind != MN_HL 
242       && item->kind != MN_LABEL
243       && item->kind != MN_DEACTIVE) {
244     active_item = items.size() - 1;
245   }
246 }
247
248 void
249 Menu::add_hl()
250 {
251   additem(new MenuItem(MN_HL));
252 }
253
254 void
255 Menu::add_label(const std::string& text)
256 {
257   MenuItem* item = new MenuItem(MN_LABEL);
258   item->text = text;
259   additem(item);
260 }
261
262 void
263 Menu::add_controlfield(int id, const std::string& text,
264                 const std::string& mapping)
265 {
266   MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
267   item->change_text(text);
268         item->change_input(mapping);
269   additem(item);
270 }
271
272 void
273 Menu::add_entry(int id, const std::string& text)
274 {
275   MenuItem* item = new MenuItem(MN_ACTION, id);
276   item->text = text;
277   additem(item);
278 }
279
280 void
281 Menu::add_deactive(int id, const std::string& text)
282 {
283   MenuItem* item = new MenuItem(MN_DEACTIVE, id);
284   item->text = text;
285   additem(item);
286 }
287
288 void
289 Menu::add_toggle(int id, const std::string& text, bool toogled)
290 {
291   MenuItem* item = new MenuItem(MN_TOGGLE, id);
292   item->text = text;
293   item->toggled = toogled;
294   additem(item);
295 }
296
297 void
298 Menu::add_back(const std::string& text)
299 {
300   MenuItem* item = new MenuItem(MN_BACK);
301   item->text = text;
302   additem(item);
303 }
304
305 void
306 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
307 {
308   MenuItem* item = new MenuItem(MN_GOTO, id);
309   item->text = text;
310   item->target_menu = submenu;
311   additem(item);
312 }
313
314 void
315 Menu::clear()
316 {
317   for(std::vector<MenuItem*>::iterator i = items.begin();
318       i != items.end(); ++i) {
319     delete *i;
320   }
321   items.clear();
322   active_item = -1;
323 }
324
325 /* Process actions done on the menu */
326 void
327 Menu::update()
328 {
329   /** check main input controller... */
330   if(main_controller->pressed(Controller::UP)) {
331     menuaction = MENU_ACTION_UP;
332     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
333   }
334   if(main_controller->hold(Controller::UP) && 
335       menu_repeat_time != 0 && real_time > menu_repeat_time) {
336     menuaction = MENU_ACTION_UP;
337     menu_repeat_time = real_time + MENU_REPEAT_RATE;
338   } 
339   if(main_controller->pressed(Controller::DOWN)) {
340     menuaction = MENU_ACTION_DOWN;
341     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
342   }
343   if(main_controller->hold(Controller::DOWN) && 
344       menu_repeat_time != 0 && real_time > menu_repeat_time) {
345     menuaction = MENU_ACTION_DOWN;
346     menu_repeat_time = real_time + MENU_REPEAT_RATE;
347   }
348   if(main_controller->pressed(Controller::JUMP)
349      || main_controller->pressed(Controller::ACTION)
350      || main_controller->pressed(Controller::MENU_SELECT)) {
351     menuaction = MENU_ACTION_HIT;
352   }
353   if(main_controller->pressed(Controller::PAUSE_MENU)) {
354     menuaction = MENU_ACTION_BACK;
355   }
356
357   hit_item = -1;
358   if(items.size() == 0)
359     return;
360   
361   int last_active_item = active_item;
362   switch(menuaction) {
363     case MENU_ACTION_UP:
364       do {
365         if (active_item > 0)
366           --active_item;
367         else
368           active_item = int(items.size())-1;
369       } while ((items[active_item]->kind == MN_HL 
370                 || items[active_item]->kind == MN_LABEL
371                 || items[active_item]->kind == MN_DEACTIVE)
372                && (active_item != last_active_item));
373       
374       break;
375       
376     case MENU_ACTION_DOWN:
377       do {
378         if(active_item < int(items.size())-1 )
379           ++active_item;
380         else
381           active_item = 0;
382       } while ((items[active_item]->kind == MN_HL
383                 || items[active_item]->kind == MN_LABEL
384                 || items[active_item]->kind == MN_DEACTIVE)
385                && (active_item != last_active_item));
386       
387       break;
388       
389     case MENU_ACTION_LEFT:
390       if(items[active_item]->kind == MN_STRINGSELECT) {
391         if(items[active_item]->selected > 0)
392           items[active_item]->selected--;
393         else
394           items[active_item]->selected = items[active_item]->list.size()-1;
395       }
396       break;
397       
398     case MENU_ACTION_RIGHT:
399       if(items[active_item]->kind == MN_STRINGSELECT) {
400         if(items[active_item]->selected+1 < items[active_item]->list.size())
401           items[active_item]->selected++;
402         else
403           items[active_item]->selected = 0;
404       }
405       break;
406       
407     case MENU_ACTION_HIT: {
408       hit_item = active_item;
409       switch (items[active_item]->kind) {
410         case MN_GOTO:
411           assert(items[active_item]->target_menu != 0);
412           Menu::push_current(items[active_item]->target_menu);
413           break;
414           
415         case MN_TOGGLE:
416           items[active_item]->toggled = !items[active_item]->toggled;
417           menu_action(items[active_item]);
418           break;
419           
420         case MN_CONTROLFIELD:
421           menu_action(items[active_item]);
422           break;
423           
424         case MN_ACTION:
425           menu_action(items[active_item]);
426           break;
427           
428         case MN_TEXTFIELD:
429         case MN_NUMFIELD:
430           menuaction = MENU_ACTION_DOWN;
431           update();
432           break;
433           
434         case MN_BACK:
435           Menu::pop_current();
436           break;
437         default:
438           break;
439       }
440       break;
441     }
442     
443     case MENU_ACTION_REMOVE:
444       if(items[active_item]->kind == MN_TEXTFIELD
445          || items[active_item]->kind == MN_NUMFIELD)
446       {
447         if(!items[active_item]->input.empty())
448         {
449           int i = items[active_item]->input.size();
450           
451           while(delete_character > 0)   /* remove charactes */
452           {
453             items[active_item]->input.resize(i-1);
454             delete_character--;
455           }
456         }
457       }
458       break;
459       
460     case MENU_ACTION_INPUT:
461       if(items[active_item]->kind == MN_TEXTFIELD
462          || (items[active_item]->kind == MN_NUMFIELD 
463              && mn_input_char >= '0' && mn_input_char <= '9'))
464       {
465         items[active_item]->input.push_back(mn_input_char);
466       }
467       break;
468       
469     case MENU_ACTION_BACK:
470       Menu::pop_current();
471       break;
472
473     case MENU_ACTION_NONE:
474       break;
475   }
476   menuaction = MENU_ACTION_NONE;
477
478   assert(active_item < int(items.size()));
479 }
480
481 int
482 Menu::check()
483 {
484   if (hit_item != -1)
485     return items[hit_item]->id;
486   else
487     return -1;
488 }
489
490 void
491 Menu::menu_action(MenuItem* )
492 {}
493
494 void
495 Menu::draw_item(DrawingContext& context, int index)
496 {
497   float menu_height = get_height();
498   float menu_width = get_width();  
499
500   MenuItem& pitem = *(items[index]);
501
502   int effect_offset = 0;
503   if(effect_time != 0) {
504     if(real_time - effect_time > 0.5) {
505       effect_time = 0;
506     } else {
507       float effect_delta = (0.5 - (real_time - effect_time)) * 250;
508       effect_offset = (int) ((index % 2) ? effect_delta : -effect_delta);
509     }
510   }
511
512   Font* text_font = default_font;
513   float x_pos       = pos_x;
514   float y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
515   int shadow_size = 2;
516   int text_width  = int(text_font->get_text_width(pitem.text));
517   int input_width = int(text_font->get_text_width(pitem.input) + 10);
518   int list_width = 0;
519   if(pitem.list.size() > 0) {
520     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
521   }
522   
523   if (arrange_left)
524     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
525
526   if(index == active_item)
527     {
528       shadow_size = 3;
529       text_font = active_font;
530     }
531
532   switch (pitem.kind)
533     {
534     case MN_DEACTIVE:
535       {
536         context.draw_text(deactive_font, pitem.text,
537                           Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
538                           CENTER_ALLIGN, LAYER_GUI);
539         break;
540       }
541
542     case MN_HL:
543       {
544         // TODO
545         float x = pos_x - menu_width/2;
546         float y = y_pos - 12 - effect_offset;
547         /* Draw a horizontal line with a little 3d effect */
548         context.draw_filled_rect(Vector(x, y + 6),
549                                  Vector(menu_width, 4),
550                                  Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
551         context.draw_filled_rect(Vector(x, y + 6),
552                                  Vector(menu_width, 2),
553                                  Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
554         break;
555       }
556     case MN_LABEL:
557       {
558         context.draw_text(label_font, pitem.text,
559                           Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
560                           CENTER_ALLIGN, LAYER_GUI);
561         break;
562       }
563     case MN_TEXTFIELD:
564     case MN_NUMFIELD:
565     case MN_CONTROLFIELD:
566       {
567         float width = text_width + input_width + 5;
568         float text_pos = SCREEN_WIDTH/2 - width/2;
569         float input_pos = text_pos + text_width + 10;
570
571         context.draw_filled_rect(
572           Vector(input_pos - 5, y_pos - 10),
573           Vector(input_width + 10, 20),
574           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI-5);
575         context.draw_filled_rect(
576           Vector(input_pos - 4, y_pos - 9),
577           Vector(input_width + 8, 18),
578           Color(0, 0, 0, 0.5f), LAYER_GUI-4);
579
580         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
581           {
582             if(active_item == index)
583               context.draw_text(field_font,
584                                 pitem.get_input_with_symbol(true),
585                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
586                                 LEFT_ALLIGN, LAYER_GUI);
587             else
588               context.draw_text(field_font,
589                                 pitem.get_input_with_symbol(false),
590                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
591                                 LEFT_ALLIGN, LAYER_GUI);
592           }
593         else
594           context.draw_text(field_font, pitem.input,
595                             Vector(input_pos, y_pos - int(field_font->get_height()/2)),
596                             LEFT_ALLIGN, LAYER_GUI);
597
598         context.draw_text(text_font, pitem.text,
599                           Vector(text_pos, y_pos - int(text_font->get_height()/2)),
600                           LEFT_ALLIGN, LAYER_GUI);
601         break;
602       }
603     case MN_STRINGSELECT:
604       {
605         int list_pos_2 = list_width + 16;
606         int list_pos   = list_width/2;
607         int text_pos   = (text_width + 16)/2;
608
609         /* Draw arrows */
610         context.draw_surface(arrow_left.get(),
611                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
612                              LAYER_GUI);
613         context.draw_surface(arrow_right.get(),
614                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
615                              LAYER_GUI);
616
617         /* Draw input background */
618         context.draw_filled_rect(
619           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
620           Vector(list_pos_2 + 2, 20),
621           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
622         context.draw_filled_rect(
623           Vector(x_pos - list_pos + text_pos, y_pos - 9),
624           Vector(list_pos_2, 18),
625           Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
626
627         context.draw_text(text_font, pitem.list[pitem.selected],
628                                  Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
629                                  CENTER_ALLIGN, LAYER_GUI);
630         context.draw_text(text_font, pitem.text,
631                                  Vector(SCREEN_WIDTH/2  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
632                                  CENTER_ALLIGN, LAYER_GUI);
633         break;
634       }
635     case MN_BACK:
636       {
637         context.draw_text(text_font, pitem.text,
638                           Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
639                           CENTER_ALLIGN, LAYER_GUI);
640         context.draw_surface(back.get(),
641                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
642                              LAYER_GUI);
643         break;
644       }
645
646     case MN_TOGGLE:
647       {
648         context.draw_text(text_font, pitem.text,
649                           Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
650                           CENTER_ALLIGN, LAYER_GUI);
651
652         if(pitem.toggled)
653           context.draw_surface(checkbox_checked.get(),
654                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
655                                LAYER_GUI + 1);
656         else
657           context.draw_surface(checkbox.get(),
658                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
659                                LAYER_GUI + 1);
660         break;
661       }
662     case MN_ACTION:
663       context.draw_text(text_font, pitem.text,
664                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
665                         CENTER_ALLIGN, LAYER_GUI);
666       break;
667
668     case MN_GOTO:
669       context.draw_text(text_font, pitem.text,
670                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
671                         CENTER_ALLIGN, LAYER_GUI);
672       break;
673     }
674 }
675
676 float Menu::get_width() const
677 {
678   /* The width of the menu has to be more than the width of the text
679      with the most characters */
680   float menu_width = 0;
681   for(unsigned int i = 0; i < items.size(); ++i)
682   {
683     Font* font = default_font;
684     if(items[i]->kind == MN_LABEL)
685       font = label_font;
686
687     float w = font->get_text_width(items[i]->text) +
688         label_font->get_text_width(items[i]->input) + 16;
689     if(items[i]->kind == MN_TOGGLE)
690       w += 32;
691     
692     if(w > menu_width)
693       menu_width = w;
694   }
695   
696   return menu_width + 24;
697 }
698
699 float Menu::get_height() const
700 {
701   return items.size() * 24;
702 }
703
704 /* Draw the current menu. */
705 void
706 Menu::draw(DrawingContext& context)
707 {
708   if(MouseCursor::current()) {
709     MouseCursor::current()->draw(context);
710   }
711   
712   float menu_height = get_height();
713   float menu_width = get_width();  
714
715   /* Draw a transparent background */
716   context.draw_filled_rect(
717     Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10),
718     Vector(menu_width,menu_height + 20),
719     Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-10);
720
721   for(unsigned int i = 0; i < items.size(); ++i)
722     {
723       draw_item(context, i);
724     }
725 }
726
727 MenuItem&
728 Menu::get_item_by_id(int id)
729 {
730   for(std::vector<MenuItem*>::iterator i = items.begin();
731       i != items.end(); ++i) {
732     MenuItem& item = **i;
733     
734     if(item.id == id)
735       return item;
736   }
737
738   throw std::runtime_error("MenuItem not found");
739 }
740
741 const MenuItem&
742 Menu::get_item_by_id(int id) const
743 {
744   for(std::vector<MenuItem*>::const_iterator i = items.begin();
745       i != items.end(); ++i) {
746     const MenuItem& item = **i;
747     
748     if(item.id == id)
749       return item;
750   }
751
752   throw std::runtime_error("MenuItem not found");
753 }
754
755 int Menu::get_active_item_id()
756 {
757   return items[active_item]->id;
758 }
759
760 bool
761 Menu::is_toggled(int id) const
762 {
763   return get_item_by_id(id).toggled;
764 }
765
766 /* Check for menu event */
767 void
768 Menu::event(const SDL_Event& event)
769 {
770   if(effect_time != 0)
771     return;
772
773   switch(event.type) {
774     case SDL_MOUSEBUTTONDOWN:
775       {
776         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
777         int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
778
779         if(x > pos_x - get_width()/2 &&
780             x < pos_x + get_width()/2 &&
781             y > pos_y - get_height()/2 &&
782             y < pos_y + get_height()/2)
783           {
784             menuaction = MENU_ACTION_HIT;
785           }
786       }
787       break;
788
789     case SDL_MOUSEMOTION:
790       {
791         float x = event.motion.x * SCREEN_WIDTH/screen->w;
792         float y = event.motion.y * SCREEN_HEIGHT/screen->h;
793
794         if(x > pos_x - get_width()/2 &&
795             x < pos_x + get_width()/2 &&
796             y > pos_y - get_height()/2 &&
797             y < pos_y + get_height()/2)
798           {
799             int new_active_item 
800               = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
801           
802             /* only change the mouse focus to a selectable item */
803             if ((items[new_active_item]->kind != MN_HL)
804                 && (items[new_active_item]->kind != MN_LABEL)
805                 && (items[new_active_item]->kind != MN_DEACTIVE))
806               active_item = new_active_item;
807             
808             if(MouseCursor::current())
809               MouseCursor::current()->set_state(MC_LINK);
810           }
811         else
812         {
813           if(MouseCursor::current())
814             MouseCursor::current()->set_state(MC_NORMAL);
815         }
816       }
817       break;
818
819     default:
820       break;
821     }
822 }
823
824 void
825 Menu::set_active_item(int id)
826 {
827   for(size_t i = 0; i < items.size(); ++i) {
828     MenuItem* item = items[i];
829     if(item->id == id) {
830       active_item = i;
831       break;
832     }
833   }
834 }