Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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/screen.hpp"
36 #include "video/drawing_context.hpp"
37 #include "gettext.hpp"
38 #include "math/vector.hpp"
39 #include "main.hpp"
40 #include "resources.hpp"
41 #include "control/joystickkeyboardcontroller.hpp"
42
43 static const int MENU_REPEAT_INITIAL = 400;
44 static const int MENU_REPEAT_RATE = 200;
45 static const int FLICK_CURSOR_TIME = 500;
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_ticks = SDL_GetTicks();
126 }
127
128 void
129 Menu::pop_current()
130 {
131   if (last_menus.size() >= 1) {
132     current_ = last_menus.back();
133     current_->effect_ticks = SDL_GetTicks();
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_ticks = SDL_GetTicks();
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 = (SDL_GetTicks() / 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 }
198
199 Menu::Menu()
200 {
201   hit_item = -1;
202   menuaction = MENU_ACTION_NONE;
203   delete_character = 0;
204   mn_input_char = '\0';
205
206   pos_x        = SCREEN_WIDTH/2;
207   pos_y        = SCREEN_HEIGHT/2;
208   arrange_left = 0;
209   active_item  = -1;
210
211   checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
212   checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
213   back.reset(new Surface("images/engine/menu/arrow-back.png"));
214   arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
215   arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
216 }
217
218 void Menu::set_pos(float x, float y, float rw, float rh)
219 {
220   pos_x = x + get_width() * rw;
221   pos_y = y + get_height() * rh;
222 }
223
224 /* Add an item to a menu */
225 void
226 Menu::additem(MenuItem* item)
227 {
228   items.push_back(item);
229
230   /* If a new menu is being built, the active item shouldn't be set to
231    * something that isnt selectable. Set the active_item to the first
232    * selectable item added
233    */
234   if (active_item == -1
235       && item->kind != MN_HL 
236       && item->kind != MN_LABEL
237       && item->kind != MN_DEACTIVE) {
238     active_item = items.size() - 1;
239   }
240 }
241
242 void
243 Menu::add_hl()
244 {
245   additem(new MenuItem(MN_HL));
246 }
247
248 void
249 Menu::add_label(const std::string& text)
250 {
251   MenuItem* item = new MenuItem(MN_LABEL);
252   item->text = text;
253   additem(item);
254 }
255
256 void
257 Menu::add_controlfield(int id, const std::string& text,
258                 const std::string& mapping)
259 {
260   MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
261   item->change_text(text);
262         item->change_input(mapping);
263   additem(item);
264 }
265
266 void
267 Menu::add_entry(int id, const std::string& text)
268 {
269   MenuItem* item = new MenuItem(MN_ACTION, id);
270   item->text = text;
271   additem(item);
272 }
273
274 void
275 Menu::add_deactive(int id, const std::string& text)
276 {
277   MenuItem* item = new MenuItem(MN_DEACTIVE, id);
278   item->text = text;
279   additem(item);
280 }
281
282 void
283 Menu::add_toggle(int id, const std::string& text, bool toogled)
284 {
285   MenuItem* item = new MenuItem(MN_TOGGLE, id);
286   item->text = text;
287   item->toggled = toogled;
288   additem(item);
289 }
290
291 void
292 Menu::add_back(const std::string& text)
293 {
294   MenuItem* item = new MenuItem(MN_BACK);
295   item->text = text;
296   additem(item);
297 }
298
299 void
300 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
301 {
302   MenuItem* item = new MenuItem(MN_GOTO, id);
303   item->text = text;
304   item->target_menu = submenu;
305   additem(item);
306 }
307
308 void
309 Menu::clear()
310 {
311   for(std::vector<MenuItem*>::iterator i = items.begin();
312       i != items.end(); ++i) {
313     delete *i;
314   }
315   items.clear();
316   active_item = -1;
317 }
318
319 /* Process actions done on the menu */
320 void
321 Menu::update()
322 {
323   /** check main input controller... */
324   Uint32 ticks = SDL_GetTicks();
325   if(main_controller->pressed(Controller::UP)) {
326     menuaction = MENU_ACTION_UP;
327     menu_repeat_ticks = ticks + MENU_REPEAT_INITIAL;
328   }
329   if(main_controller->hold(Controller::UP) && 
330       menu_repeat_ticks != 0 && ticks > menu_repeat_ticks) {
331     menuaction = MENU_ACTION_UP;
332     menu_repeat_ticks = ticks + MENU_REPEAT_RATE;
333   } 
334   if(main_controller->pressed(Controller::DOWN)) {
335     menuaction = MENU_ACTION_DOWN;
336     menu_repeat_ticks = ticks + MENU_REPEAT_INITIAL;
337   }
338   if(main_controller->hold(Controller::DOWN) && 
339       menu_repeat_ticks != 0 && ticks > menu_repeat_ticks) {
340     menuaction = MENU_ACTION_DOWN;
341     menu_repeat_ticks = ticks + MENU_REPEAT_RATE;
342   }
343   if(main_controller->pressed(Controller::JUMP)
344      || main_controller->pressed(Controller::ACTION)
345      || main_controller->pressed(Controller::MENU_SELECT)) {
346     menuaction = MENU_ACTION_HIT;
347   }
348   if(main_controller->pressed(Controller::PAUSE_MENU)) {
349     menuaction = MENU_ACTION_BACK;
350   }
351
352   hit_item = -1;
353   if(items.size() == 0)
354     return;
355   
356   int last_active_item = active_item;
357   switch(menuaction) {
358     case MENU_ACTION_UP:
359       do {
360         if (active_item > 0)
361           --active_item;
362         else
363           active_item = int(items.size())-1;
364       } while ((items[active_item]->kind == MN_HL 
365                 || items[active_item]->kind == MN_LABEL
366                 || items[active_item]->kind == MN_DEACTIVE)
367                && (active_item != last_active_item));
368       
369       break;
370       
371     case MENU_ACTION_DOWN:
372       do {
373         if(active_item < int(items.size())-1 )
374           ++active_item;
375         else
376           active_item = 0;
377       } while ((items[active_item]->kind == MN_HL
378                 || items[active_item]->kind == MN_LABEL
379                 || items[active_item]->kind == MN_DEACTIVE)
380                && (active_item != last_active_item));
381       
382       break;
383       
384     case MENU_ACTION_LEFT:
385       if(items[active_item]->kind == MN_STRINGSELECT) {
386         if(items[active_item]->selected > 0)
387           items[active_item]->selected--;
388         else
389           items[active_item]->selected = items[active_item]->list.size()-1;
390       }
391       break;
392       
393     case MENU_ACTION_RIGHT:
394       if(items[active_item]->kind == MN_STRINGSELECT) {
395         if(items[active_item]->selected+1 < items[active_item]->list.size())
396           items[active_item]->selected++;
397         else
398           items[active_item]->selected = 0;
399       }
400       break;
401       
402     case MENU_ACTION_HIT: {
403       hit_item = active_item;
404       switch (items[active_item]->kind) {
405         case MN_GOTO:
406           assert(items[active_item]->target_menu != 0);
407           Menu::push_current(items[active_item]->target_menu);
408           break;
409           
410         case MN_TOGGLE:
411           items[active_item]->toggled = !items[active_item]->toggled;
412           menu_action(items[active_item]);
413           break;
414           
415         case MN_CONTROLFIELD:
416           menu_action(items[active_item]);
417           break;
418           
419         case MN_ACTION:
420           menu_action(items[active_item]);
421           break;
422           
423         case MN_TEXTFIELD:
424         case MN_NUMFIELD:
425           menuaction = MENU_ACTION_DOWN;
426           update();
427           break;
428           
429         case MN_BACK:
430           Menu::pop_current();
431           break;
432         default:
433           break;
434       }
435       break;
436     }
437     
438     case MENU_ACTION_REMOVE:
439       if(items[active_item]->kind == MN_TEXTFIELD
440          || items[active_item]->kind == MN_NUMFIELD)
441       {
442         if(!items[active_item]->input.empty())
443         {
444           int i = items[active_item]->input.size();
445           
446           while(delete_character > 0)   /* remove charactes */
447           {
448             items[active_item]->input.resize(i-1);
449             delete_character--;
450           }
451         }
452       }
453       break;
454       
455     case MENU_ACTION_INPUT:
456       if(items[active_item]->kind == MN_TEXTFIELD
457          || (items[active_item]->kind == MN_NUMFIELD 
458              && mn_input_char >= '0' && mn_input_char <= '9'))
459       {
460         items[active_item]->input.push_back(mn_input_char);
461       }
462       break;
463       
464     case MENU_ACTION_BACK:
465       Menu::pop_current();
466       break;
467
468     case MENU_ACTION_NONE:
469       break;
470   }
471   menuaction = MENU_ACTION_NONE;
472
473   assert(active_item < int(items.size()));
474 }
475
476 int
477 Menu::check()
478 {
479   if (hit_item != -1)
480     return items[hit_item]->id;
481   else
482     return -1;
483 }
484
485 void
486 Menu::menu_action(MenuItem* )
487 {}
488
489 void
490 Menu::draw_item(DrawingContext& context, int index)
491 {
492   float menu_height = get_height();
493   float menu_width = get_width();  
494
495   MenuItem& pitem = *(items[index]);
496
497   int effect_offset = 0;
498   if(effect_ticks != 0) {
499     if(SDL_GetTicks() - effect_ticks > 500) {
500       effect_ticks = 0;
501     } else {
502       Uint32 effect_time = (500 - (SDL_GetTicks() - effect_ticks)) / 4;
503       effect_offset = (index % 2) ? effect_time : -effect_time;
504     }
505   }
506
507   Font* text_font = default_font;
508   float x_pos       = pos_x;
509   float y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
510   int shadow_size = 2;
511   int text_width  = int(text_font->get_text_width(pitem.text));
512   int input_width = int(text_font->get_text_width(pitem.input) + 10);
513   int list_width = 0;
514   if(pitem.list.size() > 0) {
515     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
516   }
517   
518   if (arrange_left)
519     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
520
521   if(index == active_item)
522     {
523       shadow_size = 3;
524       text_font = active_font;
525     }
526
527   switch (pitem.kind)
528     {
529     case MN_DEACTIVE:
530       {
531         context.draw_text(deactive_font, pitem.text,
532                           Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
533                           CENTER_ALLIGN, LAYER_GUI);
534         break;
535       }
536
537     case MN_HL:
538       {
539         // TODO
540         float x = pos_x - menu_width/2;
541         float y = y_pos - 12 - effect_offset;
542         /* Draw a horizontal line with a little 3d effect */
543         context.draw_filled_rect(Vector(x, y + 6),
544                                  Vector(menu_width, 4),
545                                  Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
546         context.draw_filled_rect(Vector(x, y + 6),
547                                  Vector(menu_width, 2),
548                                  Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
549         break;
550       }
551     case MN_LABEL:
552       {
553         context.draw_text(label_font, pitem.text,
554                           Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
555                           CENTER_ALLIGN, LAYER_GUI);
556         break;
557       }
558     case MN_TEXTFIELD:
559     case MN_NUMFIELD:
560     case MN_CONTROLFIELD:
561       {
562         float width = text_width + input_width + 5;
563         float text_pos = SCREEN_WIDTH/2 - width/2;
564         float input_pos = text_pos + text_width + 10;
565
566         context.draw_filled_rect(
567           Vector(input_pos - 5, y_pos - 10),
568           Vector(input_width + 10, 20),
569           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI-5);
570         context.draw_filled_rect(
571           Vector(input_pos - 4, y_pos - 9),
572           Vector(input_width + 8, 18),
573           Color(0, 0, 0, 0.5f), LAYER_GUI-4);
574
575         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
576           {
577             if(active_item == index)
578               context.draw_text(field_font,
579                                 pitem.get_input_with_symbol(true),
580                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
581                                 LEFT_ALLIGN, LAYER_GUI);
582             else
583               context.draw_text(field_font,
584                                 pitem.get_input_with_symbol(false),
585                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
586                                 LEFT_ALLIGN, LAYER_GUI);
587           }
588         else
589           context.draw_text(field_font, pitem.input,
590                             Vector(input_pos, y_pos - int(field_font->get_height()/2)),
591                             LEFT_ALLIGN, LAYER_GUI);
592
593         context.draw_text(text_font, pitem.text,
594                           Vector(text_pos, y_pos - int(text_font->get_height()/2)),
595                           LEFT_ALLIGN, LAYER_GUI);
596         break;
597       }
598     case MN_STRINGSELECT:
599       {
600         int list_pos_2 = list_width + 16;
601         int list_pos   = list_width/2;
602         int text_pos   = (text_width + 16)/2;
603
604         /* Draw arrows */
605         context.draw_surface(arrow_left.get(),
606                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
607                              LAYER_GUI);
608         context.draw_surface(arrow_right.get(),
609                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
610                              LAYER_GUI);
611
612         /* Draw input background */
613         context.draw_filled_rect(
614           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
615           Vector(list_pos_2 + 2, 20),
616           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
617         context.draw_filled_rect(
618           Vector(x_pos - list_pos + text_pos, y_pos - 9),
619           Vector(list_pos_2, 18),
620           Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
621
622         context.draw_text(text_font, pitem.list[pitem.selected],
623                                  Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
624                                  CENTER_ALLIGN, LAYER_GUI);
625         context.draw_text(text_font, pitem.text,
626                                  Vector(SCREEN_WIDTH/2  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
627                                  CENTER_ALLIGN, LAYER_GUI);
628         break;
629       }
630     case MN_BACK:
631       {
632         context.draw_text(text_font, pitem.text,
633                           Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
634                           CENTER_ALLIGN, LAYER_GUI);
635         context.draw_surface(back.get(),
636                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
637                              LAYER_GUI);
638         break;
639       }
640
641     case MN_TOGGLE:
642       {
643         context.draw_text(text_font, pitem.text,
644                           Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
645                           CENTER_ALLIGN, LAYER_GUI);
646
647         if(pitem.toggled)
648           context.draw_surface(checkbox_checked.get(),
649                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
650                                LAYER_GUI + 1);
651         else
652           context.draw_surface(checkbox.get(),
653                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
654                                LAYER_GUI + 1);
655         break;
656       }
657     case MN_ACTION:
658       context.draw_text(text_font, pitem.text,
659                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
660                         CENTER_ALLIGN, LAYER_GUI);
661       break;
662
663     case MN_GOTO:
664       context.draw_text(text_font, pitem.text,
665                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
666                         CENTER_ALLIGN, LAYER_GUI);
667       break;
668     }
669 }
670
671 float Menu::get_width() const
672 {
673   /* The width of the menu has to be more than the width of the text
674      with the most characters */
675   float menu_width = 0;
676   for(unsigned int i = 0; i < items.size(); ++i)
677   {
678     Font* font = default_font;
679     if(items[i]->kind == MN_LABEL)
680       font = label_font;
681
682     float w = font->get_text_width(items[i]->text) +
683         label_font->get_text_width(items[i]->input) + 16;
684     if(items[i]->kind == MN_TOGGLE)
685       w += 32;
686     
687     if(w > menu_width)
688       menu_width = w;
689   }
690   
691   return menu_width + 24;
692 }
693
694 float Menu::get_height() const
695 {
696   return items.size() * 24;
697 }
698
699 /* Draw the current menu. */
700 void
701 Menu::draw(DrawingContext& context)
702 {
703   if(MouseCursor::current()) {
704     MouseCursor::current()->draw(context);
705   }
706   
707   float menu_height = get_height();
708   float menu_width = get_width();  
709
710   /* Draw a transparent background */
711   context.draw_filled_rect(
712     Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10),
713     Vector(menu_width,menu_height + 20),
714     Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-10);
715
716   for(unsigned int i = 0; i < items.size(); ++i)
717     {
718       draw_item(context, i);
719     }
720 }
721
722 MenuItem&
723 Menu::get_item_by_id(int id)
724 {
725   for(std::vector<MenuItem*>::iterator i = items.begin();
726       i != items.end(); ++i) {
727     MenuItem& item = **i;
728     
729     if(item.id == id)
730       return item;
731   }
732
733   throw std::runtime_error("MenuItem not found");
734 }
735
736 const MenuItem&
737 Menu::get_item_by_id(int id) const
738 {
739   for(std::vector<MenuItem*>::const_iterator i = items.begin();
740       i != items.end(); ++i) {
741     const MenuItem& item = **i;
742     
743     if(item.id == id)
744       return item;
745   }
746
747   throw std::runtime_error("MenuItem not found");
748 }
749
750 int Menu::get_active_item_id()
751 {
752   return items[active_item]->id;
753 }
754
755 bool
756 Menu::is_toggled(int id) const
757 {
758   return get_item_by_id(id).toggled;
759 }
760
761 /* Check for menu event */
762 void
763 Menu::event(const SDL_Event& event)
764 {
765   if(effect_ticks != 0)
766     return;
767
768   switch(event.type) {
769     case SDL_MOUSEBUTTONDOWN:
770       {
771         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
772         int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
773
774         if(x > pos_x - get_width()/2 &&
775             x < pos_x + get_width()/2 &&
776             y > pos_y - get_height()/2 &&
777             y < pos_y + get_height()/2)
778           {
779             menuaction = MENU_ACTION_HIT;
780           }
781       }
782       break;
783
784     case SDL_MOUSEMOTION:
785       {
786         float x = event.motion.x * SCREEN_WIDTH/screen->w;
787         float y = event.motion.y * SCREEN_HEIGHT/screen->h;
788
789         if(x > pos_x - get_width()/2 &&
790             x < pos_x + get_width()/2 &&
791             y > pos_y - get_height()/2 &&
792             y < pos_y + get_height()/2)
793           {
794             int new_active_item 
795               = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
796           
797             /* only change the mouse focus to a selectable item */
798             if ((items[new_active_item]->kind != MN_HL)
799                 && (items[new_active_item]->kind != MN_LABEL)
800                 && (items[new_active_item]->kind != MN_DEACTIVE))
801               active_item = new_active_item;
802             
803             if(MouseCursor::current())
804               MouseCursor::current()->set_state(MC_LINK);
805           }
806         else
807         {
808           if(MouseCursor::current())
809             MouseCursor::current()->set_state(MC_NORMAL);
810         }
811       }
812       break;
813
814     default:
815       break;
816     }
817 }
818
819 void
820 Menu::set_active_item(int id)
821 {
822   for(size_t i = 0; i < items.size(); ++i) {
823     MenuItem* item = items[i];
824     if(item->id == id) {
825       active_item = i;
826       break;
827     }
828   }
829 }