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