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