summaryrefslogtreecommitdiff
path: root/navit/gui/sdl/sdl_events.cpp
blob: b9904c4df9251b2d998333b640421de81bd67594 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "CEGUI.h"
#include "sdl_events.h"
#include "gui_sdl.h"

#include <CEGUI/RendererModules/OpenGLGUIRenderer/openglrenderer.h>

//  FIXME temporary fix for enum
#include "projection.h"
#include "item.h"
#include "navit.h"
#include "debug.h"
#include "track.h"
#include "search.h"
#include "coord.h"
#include "country.h"
#include "string.h"

// Library for window switching (-> nGhost)
#include "wmcontrol.h"


struct sdl_destination{
	int country;
	int town;
	int town_street_assoc;
	int current_search;
} SDL_dest;


static struct search_param { 	 
	         struct navit *nav; 	 
	         struct mapset *ms; 	 
	         struct search_list *sl; 	 
	         struct attr attr; 	 
	 } search_param;


void route_to(int x,int y){
	struct pcoord pos;
	pos.x=x;
	pos.y=y; 
	/* FIXME: Get projection from list like x,y or use pcoord from search directly */
	pos.pro = projection_mg;
	using namespace CEGUI;
	extern struct navit *sdl_gui_navit;

	try {
		WindowManager::getSingleton().getWindow("AdressSearchWindow")->hide();
		WindowManager::getSingleton().getWindow("Navit/Routing/Tips")->show();

// 		WindowManager::getSingleton().getWindow("Navit/ProgressWindow")->show();
	// 	route_set_destination(co->route, &pos);
		// I could have been using search->nav instead of sdl_gui_navit. is it better this way?
	
// 		WindowManager::getSingleton().getWindow("Navit/ProgressWindow")->hide();
// 		WindowManager::getSingleton().getWindow("OSD/RoadbookButton")->show();
// 		WindowManager::getSingleton().getWindow("OSD/ETA")->show();
	}
	catch (CEGUI::Exception& e)
	{
		fprintf(stderr,"CEGUI Exception occured: \n%s\n", e.getMessage().c_str());
		printf("Missing control!...\n");
	}
		navit_set_destination(sdl_gui_navit, &pos, "FIXME");

}

bool Handle_Virtual_Key_Down(const CEGUI::EventArgs& event){
	
	using namespace CEGUI;

	const WindowEventArgs& we =  static_cast<const CEGUI::WindowEventArgs&>(event);
	String senderID = we.window->getName();

	Window* editbox = WindowManager::getSingleton().getWindow("Navit/Keyboard/Input");
	String content=editbox->getText();


	if(senderID=="OK"){
		WindowManager::getSingleton().getWindow("Navit/Keyboard")->hide();
		return 0;
	} else if(senderID=="BACK"){
		content=content.substr(0, content.length()-1);
		editbox->setText(content);
	} else {
		content+=senderID;
		editbox->setText(content);
	}

	Window* country_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/CountryEditbox"));
	Window* town_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
	Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));

	switch (SDL_dest.current_search) {
		case SRCH_COUNTRY:
			country_edit->setText(content);
			break;
		case SRCH_TOWN :
			town_edit->setText(content);
			break;
		case SRCH_STREET :
			street_edit->setText(content);
			break;
	}
	handle_destination_change();
}

bool handleItemSelect(int r)
{
	using namespace CEGUI;

	MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
	
	ListboxItem * item = mcl->getItemAtGridReference(MCLGridRef(r,0));
	ListboxItem * itemid = mcl->getItemAtGridReference(MCLGridRef(r,1));
	ListboxItem * item_assoc = mcl->getItemAtGridReference(MCLGridRef(r,2));


	Window* country_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/CountryEditbox"));
	Window* twn_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
	Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));

	if(SDL_dest.current_search==SRCH_COUNTRY){
		country_edit->setText(item->getText());
		twn_edit->activate();
		SDL_dest.current_search=SRCH_TOWN;
		WindowManager::getSingleton().getWindow("Navit/Keyboard/Input")->setText("");

	} else 	if(SDL_dest.current_search==SRCH_TOWN){
		twn_edit->setText(item->getText());

		ListboxItem * itemx = mcl->getItemAtGridReference(MCLGridRef(r,3));
		ListboxItem * itemy = mcl->getItemAtGridReference(MCLGridRef(r,4));
	
		Window* Dest_x = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_x"));
		Dest_x->setText(itemx->getText().c_str());

		Window* Dest_y = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_y"));
		Dest_y->setText(itemy->getText().c_str());

		mcl->resetList();

		SDL_dest.current_search=SRCH_STREET;
		street_edit->activate();
		WindowManager::getSingleton().getWindow("Navit/Keyboard/Input")->setText("");

	} else if(SDL_dest.current_search==SRCH_STREET){
		street_edit->setText(item->getText());

		WindowManager::getSingleton().getWindow("Navit/Keyboard")->hide();

		ListboxItem * itemx = mcl->getItemAtGridReference(MCLGridRef(r,3));
		ListboxItem * itemy = mcl->getItemAtGridReference(MCLGridRef(r,4));
	
		Window* Dest_x = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_x"));
		Dest_x->setText(itemx->getText().c_str());

		Window* Dest_y = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_y"));
		Dest_y->setText(itemy->getText().c_str());

		mcl->resetList();

		SDL_dest.current_search=SRCH_STREET;

		WindowManager::getSingleton().getWindow("Navit/Keyboard/Input")->setText("");


	} else if (SDL_dest.current_search==SRCH_NUMBER){

		struct coord pos;
		ListboxItem * itemx = mcl->getItemAtGridReference(MCLGridRef(r,3));
		ListboxItem * itemy = mcl->getItemAtGridReference(MCLGridRef(r,4));
	
		pos.x=atoi(itemx->getText().c_str());
		pos.y=atoi(itemy->getText().c_str());

		route_to(pos.x,pos.y);
	}

	return true;
}

bool route_clear(const CEGUI::EventArgs& event)
{
// 	navit_set_destination(gui->nav, NULL, NULL);
}

bool ItemSelect(const CEGUI::EventArgs& event)
{
	//FIXME maybe merge with handleItemSelect
	using namespace CEGUI;

	MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
	ListboxItem * item = mcl->getFirstSelectedItem();
	handleItemSelect(mcl->getItemRowIndex(item));
}

bool BookmarkSelect(const CEGUI::EventArgs& event)
{
	using namespace CEGUI;
	dbg(0,"1\n");
	MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("Bookmarks/Listbox"));
	dbg(0,"2\n");
	ListboxItem * item = mcl->getFirstSelectedItem();
	if(item){
		dbg(0,"item %s is at row %i\n",item->getText().c_str(),mcl->getItemRowIndex(item));
		BookmarkGo(item->getText().c_str());
	}
	WindowManager::getSingleton().getWindow("BookmarkSelection")->hide();
}

bool FormerDestSelect(const CEGUI::EventArgs& event)
{
	using namespace CEGUI;
	dbg(0,"1\n");
	MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("FormerDests/Listbox"));
	dbg(0,"2\n");
	ListboxItem * item = mcl->getFirstSelectedItem();
	if(item){
		dbg(0,"item %s is at row %i\n",item->getText().c_str(),mcl->getItemRowIndex(item));
		FormerDestGo(item->getText().c_str());
	}
	WindowManager::getSingleton().getWindow("FormerDestSelection")->hide();
	dbg(0,"hided\n");
}

bool handleMouseEnters(const CEGUI::EventArgs& event)
{
	// FIXME this whole function could maybe be removed
	const CEGUI::WindowEventArgs& we =  static_cast<const CEGUI::WindowEventArgs&>(event);

	// FIXME theses variables should be shared
	extern CEGUI::OpenGLRenderer* renderer;

	using namespace CEGUI;
	WindowManager::getSingleton().getWindow("Navit/Keyboard/Input")->setText("");
	MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));

	String senderID = we.window->getName();

	if (senderID == "AdressSearch/CountryEditbox"){
		// First, clean off the Street and Town Editbox
		Window* town_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
		town_edit->setText("");
		Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
		street_edit->setText("");
		SDL_dest.current_search=SRCH_COUNTRY;

	} else if (senderID == "AdressSearch/TownEditbox"){
		// First, clean off the Street Editbox
		Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
		street_edit->setText("");
		SDL_dest.current_search=SRCH_TOWN;

	} else if (senderID == "AdressSearch/StreetEditbox"){
		// First, make sure the user selected an entry in the town choice. If he hadn't, select the first for him.
  		if(SDL_dest.current_search==SRCH_TOWN){
			if (mcl->getRowCount()>0)
			{
				handleItemSelect(0);
			}			
		}
		SDL_dest.current_search=SRCH_STREET;

	}
}


void handle_destination_change(){

	using namespace CEGUI;

 	struct search_param *search=&search_param;
	struct search_list_result *res;

	MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));


	if (SDL_dest.current_search==SRCH_COUNTRY)
	{	
		Editbox* country_edit = static_cast<Editbox*>(WindowManager::getSingleton().getWindow("AdressSearch/CountryEditbox"));
		String content=country_edit->getText();

		mcl->resetList();
		dbg(0,"Starting a country search : %s\n",content.c_str());

		search->attr.type=attr_country_all;

		// FIXME the following codeblock could be shared between country, town and street search
		search->attr.u.str=(char *)content.c_str();

		search_list_search(search->sl, &search->attr, 1);
		while((res=search_list_get_result(search->sl))) {
			ListboxTextItem* itemListbox = new ListboxTextItem((CEGUI::utf8*)(res->country->name));
			itemListbox->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

			mcl->addRow(itemListbox,0);
		}

	} else if (SDL_dest.current_search==SRCH_TOWN)
	{	
		
		Editbox* town_edit = static_cast<Editbox*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
		String content=town_edit->getText();


		mcl->resetList();

		if(strlen(content.c_str())<4){

		}  else {
			dbg(0,"town searching for %s\n",content.c_str());
			search->attr.type=attr_town_name;
			search->attr.u.str=(char *)content.c_str();

			search_list_search(search->sl, &search->attr, 1);
			while((res=search_list_get_result(search->sl))) {
				ListboxTextItem* itemListbox = new ListboxTextItem((CEGUI::utf8*)(res->town->name));
				itemListbox->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

				mcl->addRow(itemListbox,0);

				char x [256];
				sprintf(x,"%li",res->c->x);
				ListboxTextItem* xitem = new ListboxTextItem(x);
			
				char y [256];
				sprintf(y,"%li",res->c->y);
			
				ListboxTextItem* yitem = new ListboxTextItem(y);
			
				try
				{
					mcl->setItem(xitem, 3, mcl->getRowCount()-1);
					mcl->setItem(yitem, 4, mcl->getRowCount()-1);
				}
				// something went wrong, so cleanup the ListboxTextItem.
				catch (InvalidRequestException)
				{
// 					delete item;
				}
	
			}

		}


	} else if (SDL_dest.current_search==SRCH_STREET)
	{	
		Editbox* street_edit = static_cast<Editbox*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
		
		String content=street_edit->getText();
		if(strlen(content.c_str())<1){

		}  else {
			// dbg(1,"street searching for %s\n",content.c_str());
			search->attr.type=attr_street_name;
			search->attr.u.str=(char *)content.c_str();

			mcl->resetList();

			search_list_search(search->sl, &search->attr, 1);
			while((res=search_list_get_result(search->sl))) {
				ListboxTextItem* itemListbox = new ListboxTextItem((CEGUI::utf8*)(res->street->name));
				itemListbox->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

				mcl->addRow(itemListbox,0);

				char x [256];
				sprintf(x,"%li",res->c->x);
				ListboxTextItem* xitem = new ListboxTextItem(x);
			
				char y [256];
				sprintf(y,"%li",res->c->y);
			
				ListboxTextItem* yitem = new ListboxTextItem(y);
			
				try
				{
					mcl->setItem(xitem, 3, mcl->getRowCount()-1);
					mcl->setItem(yitem, 4, mcl->getRowCount()-1);
				}
				// something went wrong, so cleanup the ListboxTextItem.
				catch (InvalidRequestException)
				{
// 					delete item;
				}

	
			}
			}
	}

}


bool DestinationEntryChange(const CEGUI::EventArgs& event)
{
	handleMouseEnters(event);
	handle_destination_change();

	return true;
}

bool DestinationWindowSwitch(const CEGUI::EventArgs& event)
{
	using namespace CEGUI;

	WindowManager::getSingleton().getWindow("AdressSearchWindow")->hide();
	WindowManager::getSingleton().getWindow("BookmarkSelection")->hide();
	WindowManager::getSingleton().getWindow("FormerDestSelection")->hide();

	if(WindowManager::getSingleton().getWindow("DestinationChoose")->isVisible()){
		WindowManager::getSingleton().getWindow("DestinationChoose")->hide();
	} else {
		WindowManager::getSingleton().getWindow("DestinationChoose")->show();
	}
}

bool BookmarkSelectionSwitch(const CEGUI::EventArgs& event)
{
	using namespace CEGUI;
	if(WindowManager::getSingleton().getWindow("BookmarkSelection")->isVisible()){
		WindowManager::getSingleton().getWindow("BookmarkSelection")->hide();
	} else {
		WindowManager::getSingleton().getWindow("DestinationChoose")->hide();
		WindowManager::getSingleton().getWindow("BookmarkSelection")->show();
	}
}

bool FormerDestSelectionSwitch(const CEGUI::EventArgs& event)
{
	using namespace CEGUI;
	if(WindowManager::getSingleton().getWindow("FormerDestSelection")->isVisible()){
		WindowManager::getSingleton().getWindow("FormerDestSelection")->hide();
	} else {
		WindowManager::getSingleton().getWindow("DestinationChoose")->hide();
		WindowManager::getSingleton().getWindow("FormerDestSelection")->show();
	}
}

bool AddressSearchSwitch(const CEGUI::EventArgs& event)
{
	using namespace CEGUI;
	WindowManager::getSingleton().getWindow("DestinationChoose")->hide();
	WindowManager::getSingleton().getWindow("AdressSearchWindow")->show();


	if(sdl_gui_navit){	
	} else {
		dbg(0,"*** Invalid navit instance in sdl_events\n");
	}
 	struct search_param *search=&search_param;
	struct search_list_result *res;

 	// dbg(1,"search->nav=sdl_gui_navit;\n");
 	search->nav=sdl_gui_navit;
 	// dbg(1,"search->ms=navit_get_mapset(sdl_gui_navit);\n");
 	search->ms=navit_get_mapset(sdl_gui_navit);
 	// dbg(1,"search->sl=search_list_new(search->ms);\n");
 	search->sl=search_list_new(search->ms);


	const CEGUI::WindowEventArgs& we =  static_cast<const CEGUI::WindowEventArgs&>(event);

	Window* town_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/TownEditbox"));
	town_edit->setText("");
	Window* street_edit = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/StreetEditbox"));
	street_edit->setText("");
	town_edit->activate();

	SDL_dest.current_search=SRCH_COUNTRY;
	MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
	mcl->resetList();
	
	
	// Code to get the current country and set it by default for the search
	struct attr search_attr, country_name, *country_attr;
	struct tracking *tracking;
	struct country_search *cs;
	struct item *item;


	Editbox* country_edit = static_cast<Editbox*>(WindowManager::getSingleton().getWindow("AdressSearch/CountryEditbox"));

	country_attr=country_default();
	tracking=navit_get_tracking(sdl_gui_navit);
	if (tracking && tracking_get_current_attr(tracking, attr_country_id, &search_attr)) 
		country_attr=&search_attr;
	cs=country_search_new(country_attr, 0);
	item=country_search_get_item(cs);
	if (item && item_attr_get(item, attr_country_name, &country_name)){
		country_edit->setText(country_name.u.str);
		handle_destination_change();
		}
	country_search_destroy(cs);

	SDL_dest.current_search=SRCH_TOWN;

	return true;
}

bool Switch_to_nGhost(const CEGUI::EventArgs& event)
{
	extern char media_window_title[255], media_cmd[255];
	dbg(0,"trying to switch to %s (%s)\n",media_window_title,media_cmd);
	if (window_switch(media_window_title)==EXIT_FAILURE)
	{
		popen(media_cmd,"r");
	}

}


bool RoadBookSwitch(const CEGUI::EventArgs& event)
{
	using namespace CEGUI;

// 	const CEGUI::WindowEventArgs& we =  static_cast<const CEGUI::WindowEventArgs&>(event);
	if(WindowManager::getSingleton().getWindow("Navit/RoadBook")->isVisible()){
		WindowManager::getSingleton().getWindow("Navit/RoadBook")->hide();
		WindowManager::getSingleton().getWindow("OSD/RoadbookButton")->show();
	} else {
		WindowManager::getSingleton().getWindow("Navit/RoadBook")->show();
		WindowManager::getSingleton().getWindow("OSD/RoadbookButton")->hide();
	}
	return true;
}

bool ButtonGo(const CEGUI::EventArgs& event)
{
	using namespace CEGUI;
	
	MultiColumnList* mcl = static_cast<MultiColumnList*>(WindowManager::getSingleton().getWindow("AdressSearch/Listbox"));
	// First, make sure the user selected an entry in the town choice. If he hadn't, select the first for him.
	if(SDL_dest.current_search==SRCH_TOWN){
		if (mcl->getRowCount()>0)
		{
			handleItemSelect(0);
		}
	}


	Window* Dest_x = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_x"));
	Window* Dest_y = static_cast<Window*>(WindowManager::getSingleton().getWindow("AdressSearch/Dest_y"));
	
	extern struct navit *sdl_gui_navit;
 	route_to(atoi(Dest_x->getText().c_str()),atoi(Dest_y->getText().c_str()));

	return true;
}


bool ZoomIn(const CEGUI::EventArgs& event)
{
	extern struct navit *sdl_gui_navit;
	navit_zoom_in(sdl_gui_navit, 2, NULL);

}

bool ZoomOut(const CEGUI::EventArgs& event)
{
	extern struct navit *sdl_gui_navit;
	navit_zoom_out(sdl_gui_navit, 2, NULL);
}

bool ButtonQuit(const CEGUI::EventArgs& event)
{
	exit(0);
}




// Nothing really interesting below.

void handle_mouse_down(Uint8 button)
{
	switch ( button )
	{
		// handle real mouse buttons
		case SDL_BUTTON_LEFT:
			CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
			break;
		case SDL_BUTTON_MIDDLE:
			CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
			break;
		case SDL_BUTTON_RIGHT:
			CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
			break;
		
		// handle the mouse wheel
		case SDL_BUTTON_WHEELDOWN:
			CEGUI::System::getSingleton().injectMouseWheelChange( -1 );
			break;
		case SDL_BUTTON_WHEELUP:
			CEGUI::System::getSingleton().injectMouseWheelChange( +1 );
			break;
	}
}


void handle_mouse_up(Uint8 button)
{
	switch ( button )
	{
	case SDL_BUTTON_LEFT:
		CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);
		break;
	case SDL_BUTTON_MIDDLE:
		CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);
		break;
	case SDL_BUTTON_RIGHT:
		CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);
		break;
	}
}

void inject_time_pulse(double& last_time_pulse)
{
	// get current "run-time" in seconds
	double t = 0.001*SDL_GetTicks();

	// inject the time that passed since the last call 
	CEGUI::System::getSingleton().injectTimePulse( float(t-last_time_pulse) );

	// store the new time as the last time
	last_time_pulse = t;
}

 CEGUI::uint SDLKeyToCEGUIKey(SDLKey key)
 {
     using namespace CEGUI;
     switch (key)
     {
     case SDLK_BACKSPACE:    return Key::Backspace;
     case SDLK_TAB:          return Key::Tab;
     case SDLK_RETURN:       return Key::Return;
     case SDLK_PAUSE:        return Key::Pause;
     case SDLK_ESCAPE:       return Key::Escape;
     case SDLK_SPACE:        return Key::Space;
     case SDLK_COMMA:        return Key::Comma;
     case SDLK_MINUS:        return Key::Minus;
     case SDLK_PERIOD:       return Key::Period;
     case SDLK_SLASH:        return Key::Slash;
     case SDLK_0:            return Key::Zero;
     case SDLK_1:            return Key::One;
     case SDLK_2:            return Key::Two;
     case SDLK_3:            return Key::Three;
     case SDLK_4:            return Key::Four;
     case SDLK_5:            return Key::Five;
     case SDLK_6:            return Key::Six;
     case SDLK_7:            return Key::Seven;
     case SDLK_8:            return Key::Eight;
     case SDLK_9:            return Key::Nine;
     case SDLK_COLON:        return Key::Colon;
     case SDLK_SEMICOLON:    return Key::Semicolon;
     case SDLK_EQUALS:       return Key::Equals;
     case SDLK_LEFTBRACKET:  return Key::LeftBracket;
     case SDLK_BACKSLASH:    return Key::Backslash;
     case SDLK_RIGHTBRACKET: return Key::RightBracket;
     case SDLK_a:            return Key::A;
     case SDLK_b:            return Key::B;
     case SDLK_c:            return Key::C;
     case SDLK_d:            return Key::D;
     case SDLK_e:            return Key::E;
     case SDLK_f:            return Key::F;
     case SDLK_g:            return Key::G;
     case SDLK_h:            return Key::H;
     case SDLK_i:            return Key::I;
     case SDLK_j:            return Key::J;
     case SDLK_k:            return Key::K;
     case SDLK_l:            return Key::L;
     case SDLK_m:            return Key::M;
     case SDLK_n:            return Key::N;
     case SDLK_o:            return Key::O;
     case SDLK_p:            return Key::P;
     case SDLK_q:            return Key::Q;
     case SDLK_r:            return Key::R;
     case SDLK_s:            return Key::S;
     case SDLK_t:            return Key::T;
     case SDLK_u:            return Key::U;
     case SDLK_v:            return Key::V;
     case SDLK_w:            return Key::W;
     case SDLK_x:            return Key::X;
     case SDLK_y:            return Key::Y;
     case SDLK_z:            return Key::Z;
     case SDLK_DELETE:       return Key::Delete;
     case SDLK_KP0:          return Key::Numpad0;
     case SDLK_KP1:          return Key::Numpad1;
     case SDLK_KP2:          return Key::Numpad2;
     case SDLK_KP3:          return Key::Numpad3;
     case SDLK_KP4:          return Key::Numpad4;
     case SDLK_KP5:          return Key::Numpad5;
     case SDLK_KP6:          return Key::Numpad6;
     case SDLK_KP7:          return Key::Numpad7;
     case SDLK_KP8:          return Key::Numpad8;
     case SDLK_KP9:          return Key::Numpad9;
     case SDLK_KP_PERIOD:    return Key::Decimal;
     case SDLK_KP_DIVIDE:    return Key::Divide;
     case SDLK_KP_MULTIPLY:  return Key::Multiply;
     case SDLK_KP_MINUS:     return Key::Subtract;
     case SDLK_KP_PLUS:      return Key::Add;
     case SDLK_KP_ENTER:     return Key::NumpadEnter;
     case SDLK_KP_EQUALS:    return Key::NumpadEquals;
     case SDLK_UP:           return Key::ArrowUp;
     case SDLK_DOWN:         return Key::ArrowDown;
     case SDLK_RIGHT:        return Key::ArrowRight;
     case SDLK_LEFT:         return Key::ArrowLeft;
     case SDLK_INSERT:       return Key::Insert;
     case SDLK_HOME:         return Key::Home;
     case SDLK_END:          return Key::End;
     case SDLK_PAGEUP:       return Key::PageUp;
     case SDLK_PAGEDOWN:     return Key::PageDown;
     case SDLK_F1:           return Key::F1;
     case SDLK_F2:           return Key::F2;
     case SDLK_F3:           return Key::F3;
     case SDLK_F4:           return Key::F4;
     case SDLK_F5:           return Key::F5;
     case SDLK_F6:           return Key::F6;
     case SDLK_F7:           return Key::F7;
     case SDLK_F8:           return Key::F8;
     case SDLK_F9:           return Key::F9;
     case SDLK_F10:          return Key::F10;
     case SDLK_F11:          return Key::F11;
     case SDLK_F12:          return Key::F12;
     case SDLK_F13:          return Key::F13;
     case SDLK_F14:          return Key::F14;
     case SDLK_F15:          return Key::F15;
     case SDLK_NUMLOCK:      return Key::NumLock;
     case SDLK_SCROLLOCK:    return Key::ScrollLock;
     case SDLK_RSHIFT:       return Key::RightShift;
     case SDLK_LSHIFT:       return Key::LeftShift;
     case SDLK_RCTRL:        return Key::RightControl;
     case SDLK_LCTRL:        return Key::LeftControl;
     case SDLK_RALT:         return Key::RightAlt;
     case SDLK_LALT:         return Key::LeftAlt;
     case SDLK_LSUPER:       return Key::LeftWindows;
     case SDLK_RSUPER:       return Key::RightWindows;
     case SDLK_SYSREQ:       return Key::SysRq;
     case SDLK_MENU:         return Key::AppMenu;
     case SDLK_POWER:        return Key::Power;
     default:                return 0;
     }
     return 0;
 }


void inject_input(bool& must_quit)
{
  SDL_Event e;
  
  // go through all available events
  while (SDL_PollEvent(&e))
  {
    // we use a switch to determine the event type
    switch (e.type)
    {
      // mouse motion handler
      case SDL_MOUSEMOTION:
        // we inject the mouse position directly.
        CEGUI::System::getSingleton().injectMousePosition(
          static_cast<float>(e.motion.x),
          static_cast<float>(e.motion.y)
        );
        break;
    
    // mouse down handler
      case SDL_MOUSEBUTTONDOWN:
        // let a special function handle the mouse button down event
        handle_mouse_down(e.button.button);
        break;
    
      // mouse up handler
      case SDL_MOUSEBUTTONUP:
        // let a special function handle the mouse button up event
        handle_mouse_up(e.button.button);
        break;
    
    
      // key down
      case SDL_KEYDOWN:
        // to tell CEGUI that a key was pressed, we inject the scancode, translated from SDL
        CEGUI::System::getSingleton().injectKeyDown(SDLKeyToCEGUIKey(e.key.keysym.sym));
        
        // as for the character it's a litte more complicated. we'll use for translated unicode value.
        // this is described in more detail below.
        if ((e.key.keysym.unicode & 0xFF80) == 0)
        {
          CEGUI::System::getSingleton().injectChar(e.key.keysym.unicode & 0x7F);
        }
        break;
    
      // key up
      case SDL_KEYUP:
        // like before we inject the scancode translated from SDL.
        CEGUI::System::getSingleton().injectKeyUp(SDLKeyToCEGUIKey(e.key.keysym.sym));
        break;
    
    
      // WM quit event occured
      case SDL_QUIT:
        must_quit = true;
        break;
    
    }
  
  }

}