forked from navit-gps/navit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_internal.c
3454 lines (3087 loc) · 123 KB
/
gui_internal.c
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Navit, a modular navigation system.
* Copyright (C) 2005-2010 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.
*/
//##############################################################################################################
//#
//# File: gui_internal.c
//# Description: New "internal" GUI for use with any graphics library
//# Comment: Trying to make a touchscreen friendly GUI
//# Authors: Martin Schaller (04/2008), Stefan Klumpp (04/2008)
//#
//##############################################################################################################
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <glib.h>
#include <time.h>
#include "config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_API_WIN32_BASE
#include <windows.h>
#endif
#ifndef _MSC_VER
#include <sys/time.h>
#endif /* _MSC_VER */
#include "item.h"
#include "xmlconfig.h"
#include "file.h"
#include "navit.h"
#include "navit_nls.h"
#include "gui.h"
#include "coord.h"
#include "point.h"
#include "plugin.h"
#include "graphics.h"
#include "transform.h"
#include "color.h"
#include "map.h"
#include "callback.h"
#include "vehicle.h"
#include "vehicleprofile.h"
#include "window.h"
#include "config_.h"
#include "keys.h"
#include "mapset.h"
#include "route.h"
#include "navit/search.h"
#include "track.h"
#include "country.h"
#include "config.h"
#include "event.h"
#include "navit_nls.h"
#include "navigation.h"
#include "gui_internal.h"
#include "command.h"
#include "util.h"
#include "bookmarks.h"
#include "linguistics.h"
#include "debug.h"
#include "fib.h"
#include "types.h"
#include "gui_internal_widget.h"
#include "gui_internal_priv.h"
#include "gui_internal_html.h"
#include "gui_internal_bookmark.h"
#include "gui_internal_menu.h"
#include "gui_internal_search.h"
#include "gui_internal_gesture.h"
#include "gui_internal_poi.h"
#include "gui_internal_command.h"
#include "gui_internal_keyboard.h"
/**
* Indexes into the config_profiles array.
*/
const int LARGE_PROFILE=0;
const int MEDIUM_PROFILE=1;
const int SMALL_PROFILE=2;
/**
* The default config profiles.
*
* [0] => LARGE_PROFILE (screens 640 in one dimension)
* [1] => MEDIUM PROFILE (screens larger than 320 in one dimension
* [2] => Small profile (default)
*/
static struct gui_config_settings config_profiles[]= {
{545,32,48,96,10}
, {300,32,48,64,3}
,{200,16,32,48,2}
};
static void gui_internal_cmd_view_in_browser(struct gui_priv *this, struct widget *wm, void *data);
static void gui_internal_prepare_search_results_map(struct gui_priv *this, struct widget *table, struct coord_rect *r);
static int gui_internal_is_active_vehicle(struct gui_priv *this, struct vehicle *vehicle);
/**
* @brief Displays an image scaled to a specific size
*
* Searches for scaleable and pre-scaled image
*
* @param this Our gui context
* @param name image name
* @param w desired width of image
* @param h desired height of image
*
* @return image_struct Ptr to scaled image struct or NULL if not scaled or found
*/
static struct graphics_image *image_new_scaled(struct gui_priv *this, const char *name, int w, int h) {
struct graphics_image *ret=NULL;
char *full_path=NULL;
full_path=graphics_icon_path(name);
ret=graphics_image_new_scaled(this->gra, full_path, w, h);
dbg(lvl_debug,"Trying to load image '%s' (w=%d, h=%d): %s", name, w, h, ret ? "OK" : "NOT FOUND");
g_free(full_path);
if (!ret) {
dbg(lvl_error,"Failed to load image for '%s' (w=%d, h=%d)", name, w, h);
full_path=graphics_icon_path("unknown");
ret=graphics_image_new_scaled(this->gra, full_path, w, h);
g_free(full_path);
}
return ret;
}
#if 0
static struct graphics_image *image_new_o(struct gui_priv *this, char *name) {
return image_new_scaled(this, name, -1, -1);
}
#endif
/**
* @brief Displays an image scaled to xs (extra small) size
*
* This image size can be too small to click it on some devices.
*
* @param this Our gui context
* @param name image name
*
* @return image_struct Ptr to scaled image struct or NULL if not scaled or found
*/
struct graphics_image *
image_new_xs(struct gui_priv *this, const char *name) {
return image_new_scaled(this, name, this->icon_xs, this->icon_xs);
}
/**
* @brief Displays an image scaled to s (small) size
*
* @param this Our gui context
* @param name image name
*
* @return image_struct Ptr to scaled image struct or NULL if not scaled or found
*/
struct graphics_image *
image_new_s(struct gui_priv *this, const char *name) {
return image_new_scaled(this, name, this->icon_s, this->icon_s);
}
/**
* @brief Displays an image scaled to l (large) size
* @param this Our gui context
* @param name image name
*
* @return image_struct Ptr to scaled image struct or NULL if not scaled or found
*/
struct graphics_image *
image_new_l(struct gui_priv *this, const char *name) {
return image_new_scaled(this, name, this->icon_l, this->icon_l);
}
static int gui_internal_button_attr_update(struct gui_priv *this, struct widget *w) {
struct widget *wi;
int is_on=0;
struct attr curr;
GList *l;
if (w->get_attr(w->instance, w->on.type, &curr, NULL))
is_on=curr.u.data == w->on.u.data;
else
is_on=w->deflt;
if (is_on != w->is_on) {
if (w->redraw)
this->redraw=1;
w->is_on=is_on;
l=g_list_first(w->children);
if (l) {
wi=l->data;
if (wi->img)
graphics_image_free(this->gra, wi->img);
wi->img=image_new_xs(this, is_on ? "gui_active" : "gui_inactive");
}
if (w->is_on && w->off.type == attr_none)
w->state &= ~STATE_SENSITIVE;
else
w->state |= STATE_SENSITIVE;
return 1;
}
return 0;
}
static void gui_internal_button_attr_callback(struct gui_priv *this, struct widget *w) {
if (gui_internal_button_attr_update(this, w))
gui_internal_widget_render(this, w);
}
static void gui_internal_button_attr_pressed(struct gui_priv *this, struct widget *w, void *data) {
if (w->is_on)
w->set_attr(w->instance, &w->off);
else
w->set_attr(w->instance, &w->on);
gui_internal_button_attr_update(this, w);
}
struct widget *
gui_internal_button_navit_attr_new(struct gui_priv *this, const char *text, enum flags flags, struct attr *on,
struct attr *off) {
struct graphics_image *image=NULL;
struct widget *ret;
if (!on && !off)
return NULL;
image=image_new_xs(this, "gui_inactive");
ret=gui_internal_button_new_with_callback(this, text, image, flags, gui_internal_button_attr_pressed, NULL);
if (on)
ret->on=*on;
if (off)
ret->off=*off;
ret->get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))navit_get_attr;
ret->set_attr=(int (*)(void *, struct attr *))navit_set_attr;
ret->remove_cb=(void (*)(void *, struct callback *))navit_remove_callback;
ret->instance=this->nav;
ret->cb=callback_new_attr_2(callback_cast(gui_internal_button_attr_callback), on?on->type:off->type, this, ret);
navit_add_callback(this->nav, ret->cb);
gui_internal_button_attr_update(this, ret);
return ret;
}
struct widget *
gui_internal_button_map_attr_new(struct gui_priv *this, const char *text, enum flags flags, struct map *map,
struct attr *on, struct attr *off, int deflt) {
struct graphics_image *image=NULL;
struct widget *ret;
image=image_new_xs(this, "gui_inactive");
if (!on && !off)
return NULL;
ret=gui_internal_button_new_with_callback(this, text, image, flags, gui_internal_button_attr_pressed, NULL);
if (on)
ret->on=*on;
if (off)
ret->off=*off;
ret->deflt=deflt;
ret->get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))map_get_attr;
ret->set_attr=(int (*)(void *, struct attr *))map_set_attr;
ret->remove_cb=(void (*)(void *, struct callback *))map_remove_callback;
ret->instance=map;
ret->redraw=1;
ret->cb=callback_new_attr_2(callback_cast(gui_internal_button_attr_callback), on?on->type:off->type, this, ret);
map_add_callback(map, ret->cb);
gui_internal_button_attr_update(this, ret);
return ret;
}
/*
* @brief Calculate movement vector and timing of the gesture.
* @param in this gui context
* @param in msec time in milliseconds to find gesture within
* @param out p0 pointer to the point object, where gesture starting point coordinates should be placed. Can be NULL.
* @param out dx pointer to variable to store horizontal movement of the gesture.
* @param out dy pointer to variable to store vertical movement of the gesture.
* @return amount of time the actual movement took.
*/
/* FIXME where is the implementation? */
static void gui_internal_motion_cb(struct gui_priv *this) {
this->motion_timeout_event=NULL;
gui_internal_gesture_ring_add(this, &(this->current));
/* Check for scrollable table below the highligted item if there's a movement with the button pressed */
if (this->pressed && this->highlighted) {
struct widget *wt=NULL;
struct widget *wr=NULL;
int dx,dy;
/* Guard against accidental scrolling when user is likely going to swipe */
gui_internal_gesture_get_vector(this, 1000, NULL, &dx, &dy);
if(abs(dx)>abs(dy) || abs(dy)<this->icon_s)
return;
if(this->highlighted)
for(wr=this->highlighted; wr && wr->type!=widget_table_row; wr=wr->parent);
if(wr)
wt=wr->parent;
if(wt && wt->type==widget_table && (wt->state & STATE_SCROLLABLE)) {
struct table_data *td=wt->data;
GList *top=NULL;
GList *btm=NULL;
GList *ttop, *tbtm;
if(!wr || !wr->h)
return;
if(this->current.y < wr->p.y && wr!=td->top_row->data ) {
int n=(wr->p.y-this->current.y)/wr->h+1;
btm=td->bottom_row;
top=td->top_row;
while(n-->0 && (tbtm=gui_internal_widget_table_next_row(btm))!=NULL
&& (ttop=gui_internal_widget_table_next_row(top))!=NULL) {
top=ttop;
btm=tbtm;
if(top->data==wr)
break;
}
this->pressed=2;
} else if (this->current.y > wr->p.y + wr->h ) {
int y=wt->p.y+wt->h-wr->h;
int n;
if(td->scroll_buttons.button_box && td->scroll_buttons.button_box->p.y!=0)
y=td->scroll_buttons.button_box->p.y - td->scroll_buttons.button_box->h;
if(y>this->current.y)
y=this->current.y;
n=(y - wr->p.y )/wr->h;
btm=td->bottom_row;
top=td->top_row;
while(n-->0 && (ttop=gui_internal_widget_table_prev_row(top))!=NULL
&& (tbtm=gui_internal_widget_table_prev_row(btm))!=NULL) {
btm=tbtm;
top=ttop;
if(btm->data==wr)
break;
}
this->pressed=2;
}
if( top && btm && (td->top_row!=top || td->bottom_row!=btm) ) {
gui_internal_table_hide_rows(wt->data);
td->top_row=top;
td->bottom_row=btm;
graphics_draw_mode(this->gra, draw_mode_begin);
gui_internal_widget_render(this,wt);
graphics_draw_mode(this->gra, draw_mode_end);
}
return;
}
}
/* Else, just move highlight after pointer if there's nothing to scroll */
gui_internal_highlight(this);
}
//##############################################################################################################
//# Description:
//# Comment:
//# Authors: Martin Schaller (04/2008)
//##############################################################################################################
static void gui_internal_call_highlighted(struct gui_priv *this) {
if (! this->highlighted || ! this->highlighted->func)
return;
this->highlighted->reason=gui_internal_reason_click;
this->highlighted->func(this, this->highlighted, this->highlighted->data);
}
void gui_internal_say(struct gui_priv *this, struct widget *w, int questionmark) {
char *text=w->speech;
if (! this->speech)
return;
if (!text)
text=w->text;
if (!text)
text=w->name;
if (text) {
text=g_strdup_printf("%s%c", text, questionmark ? '?':'\0');
navit_say(this->nav, text);
g_free(text);
}
}
void gui_internal_back(struct gui_priv *this, struct widget *w, void *data) {
gui_internal_prune_menu_count(this, 1, 1);
}
void gui_internal_cmd_return(struct gui_priv *this, struct widget *wm, void *data) {
gui_internal_prune_menu(this, wm->data);
}
void gui_internal_cmd_main_menu(struct gui_priv *this, struct widget *wm, void *data) {
struct widget *w=this->root.children->data;
if (w && w->menu_data && w->menu_data->href && !strcmp(w->menu_data->href,"#Main Menu"))
gui_internal_prune_menu(this, w);
else
gui_internal_html_main_menu(this);
}
struct widget *
gui_internal_time_help(struct gui_priv *this) {
struct widget *w,*wc,*wcn;
char timestr[64];
struct tm *tm;
time_t timep;
w=gui_internal_box_new(this, gravity_right_center|orientation_horizontal|flags_fill);
w->bl=this->spacing;
w->spx=this->spacing;
w->spx=10;
w->bl=10;
w->br=10;
w->bt=6;
w->bb=6;
if (this->flags & 64) {
wc=gui_internal_box_new(this, gravity_right_top|orientation_vertical|flags_fill);
wc->bl=10;
wc->br=20;
wc->bt=6;
wc->bb=6;
timep=time(NULL);
tm=localtime(&timep);
strftime(timestr, 64, "%H:%M %d.%m.%Y", tm);
wcn=gui_internal_label_new(this, timestr);
gui_internal_widget_append(wc, wcn);
gui_internal_widget_append(w, wc);
}
if (this->flags & 128) {
wcn=gui_internal_button_new_with_callback(this, _("Help"), image_new_l(this, "gui_help"),
gravity_center|orientation_vertical|flags_fill, NULL, NULL);
gui_internal_widget_append(w, wcn);
}
return w;
}
/**
* Applies the configuration values to this based on the settings
* specified in the configuration file (this->config) and
* the most appropriate default profile based on screen resolution.
*
* This function should be run after this->root is setup and could
* be rerun after the window is resized.
*
* @author Steve Singer <[email protected]> (09/2008)
*/
void gui_internal_apply_config(struct gui_priv *this) {
struct gui_config_settings * current_config=0;
dbg(lvl_debug,"w=%d h=%d", this->root.w, this->root.h);
/*
* Select default values from profile based on the screen.
*/
if((this->root.w > 320 || this->root.h > 320) && this->root.w > 240 && this->root.h > 240) {
if((this->root.w > 640 || this->root.h > 640) && this->root.w > 480 && this->root.h > 480 ) {
current_config = &config_profiles[LARGE_PROFILE];
} else {
current_config = &config_profiles[MEDIUM_PROFILE];
}
} else {
current_config = &config_profiles[SMALL_PROFILE];
}
/*
* Apply override values from config file
*/
if(this->config.font_size == -1 ) {
this->font_size = current_config->font_size;
} else {
this->font_size = this->config.font_size;
}
if(this->config.icon_xs == -1 ) {
this->icon_xs = current_config->icon_xs;
} else {
this->icon_xs = this->config.icon_xs;
}
if(this->config.icon_s == -1 ) {
this->icon_s = current_config->icon_s;
} else {
this->icon_s = this->config.icon_s;
}
if(this->config.icon_l == -1 ) {
this->icon_l = current_config->icon_l;
} else {
this->icon_l = this->config.icon_l;
}
if(this->config.spacing == -1 ) {
this->spacing = current_config->spacing;
} else {
this->spacing = this->config.spacing;
dbg(lvl_info, "Overriding default spacing %d with value %d provided in config file", current_config->spacing,
this->config.spacing);
}
if (!this->fonts[0]) {
int i,sizes[]= {100,66,50};
for (i = 0 ; i < 3 ; i++) {
if (this->font_name)
this->fonts[i]=graphics_named_font_new(this->gra,this->font_name,this->font_size*sizes[i]/100,1);
else
this->fonts[i]=graphics_font_new(this->gra,this->font_size*sizes[i]/100,1);
}
}
}
static void gui_internal_cmd_set_destination(struct gui_priv *this, struct widget *wm, void *data) {
char *name=data;
dbg(lvl_info,"c=%d:0x%x,0x%x", wm->c.pro, wm->c.x, wm->c.y);
navit_set_destination(this->nav, &wm->c, name, 1);
if (this->flags & 512) {
struct attr follow;
follow.type=attr_follow;
follow.u.num=180;
navit_set_attr(this->nav, &this->osd_configuration);
navit_set_attr(this->nav, &follow);
navit_zoom_to_route(this->nav, 0);
}
gui_internal_prune_menu(this, NULL);
}
static void gui_internal_cmd_insert_destination_do(struct gui_priv *this, struct widget *wm, void *data) {
char *name=data;
int dstcount=navit_get_destination_count(this->nav)+1;
int pos,i;
struct pcoord *dst=g_alloca(dstcount*sizeof(struct pcoord));
dstcount=navit_get_destinations(this->nav,dst,dstcount);
pos=dstcount-wm->datai;
if(pos<0)
pos=0;
for(i=dstcount; i>pos; i--)
dst[i]=dst[i-1];
dst[pos]=wm->c;
navit_add_destination_description(this->nav,&wm->c,(char*)data);
navit_set_destinations(this->nav,dst,dstcount+1,name,1);
gui_internal_prune_menu(this, NULL);
}
/*
* @brief Displays a waypoint list to the user.
*
* This display a waypoint list to the user. When the user chooses an item from the list, the callback
* function passed as {@code cmd} will be called.
*
* Widget passed as wm parameter of the called cmd function will have item set to user chosen waypoint item. Its data will be set
* to zero-based chosen waypoint number, counting from the route end. Coordinates to wm->c will be copied from wm_->c if wm_ is not null. Otherwise,
* waypoint coordinates will be copied to wm->c.
*
* @param this gui context
* @param title Menu title
* @param hint Text to display above the waypoint list describing the action to be performed, can be NULL
* @param wm_ The called widget pointer. Can be NULL.
* @param cmd Callback function which will be called on item selection
* @param data data argument to be passed to the callback function
*/
void gui_internal_select_waypoint(struct gui_priv *this, const char *title, const char *hint, struct widget *wm_,
void(*cmd)(struct gui_priv *priv, struct widget *widget, void *data),void *data) {
struct widget *wb,*w,*wtable,*row,*wc;
struct map *map;
struct map_rect *mr;
struct item *item;
char *text;
int i;
int dstcount=navit_get_destination_count(this->nav)+1;
map=route_get_map(navit_get_route(this->nav));
if(!map)
return;
mr = map_rect_new(map, NULL);
if(!mr)
return;
wb=gui_internal_menu(this, title);
w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
gui_internal_widget_append(wb, w);
if(hint)
gui_internal_widget_append(w, gui_internal_label_new(this, hint));
wtable = gui_internal_widget_table_new(this,gravity_left_top | flags_fill | flags_expand |orientation_vertical,1);
gui_internal_widget_append(w,wtable);
i=0;
while((item = map_rect_get_item(mr))!=NULL) {
struct attr attr;
if(item->type!=type_waypoint && item->type!=type_route_end)
continue;
if (item_attr_get(item, attr_label, &attr)) {
text=g_strdup_printf(_("Waypoint %s"), map_convert_string_tmp(item->map, attr.u.str));
} else
continue;
gui_internal_widget_append(wtable,row=gui_internal_widget_table_row_new(this,
gravity_left|orientation_horizontal|flags_fill));
gui_internal_widget_append(row, wc=gui_internal_button_new_with_callback(this, text,
image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
cmd, data));
wc->item=*item;
if(wm_)
wc->c=wm_->c;
else {
struct coord c;
item_coord_get(item,&c,1);
wc->c.x=c.x;
wc->c.y=c.y;
wc->c.pro=map_projection(item->map);
}
i++;
wc->datai=dstcount-i;
g_free(text);
}
map_rect_destroy(mr);
gui_internal_menu_render(this);
}
static void gui_internal_cmd_insert_destination(struct gui_priv *this, struct widget *wm, void *data) {
gui_internal_select_waypoint(this, data, _("Select waypoint to insert the new one before"), wm,
gui_internal_cmd_insert_destination_do, data);
}
static void gui_internal_cmd_set_position(struct gui_priv *this, struct widget *wm, void *data) {
struct attr v;
if(data) {
v.type=attr_vehicle;
v.u.vehicle=NULL;
navit_set_attr(this->nav, &v);
}
navit_set_position(this->nav, &wm->c);
gui_internal_prune_menu(this, NULL);
}
/**
* @brief Generic notification function for Editable widgets to call Another widget notification function when Enter is pressed in editable field.
* The Editable widget should have data member pointing to the Another widget.
*/
void gui_internal_call_linked_on_finish(struct gui_priv *this, struct widget *wm, void *data) {
if (wm->reason==gui_internal_reason_keypress_finish && data) {
struct widget *w=data;
if(w->func)
w->func(this, w, w->data);
}
}
struct widget * gui_internal_keyboard(struct gui_priv *this, int mode);
struct widget * gui_internal_keyboard_show_native(struct gui_priv *this, struct widget *w, int mode, char *lang);
static void gui_internal_cmd_delete_bookmark(struct gui_priv *this, struct widget *wm, void *data) {
struct attr mattr;
GList *l;
navit_get_attr(this->nav, attr_bookmarks, &mattr, NULL);
bookmarks_delete_bookmark(mattr.u.bookmarks,wm->text);
l=g_list_previous(g_list_previous(g_list_last(this->root.children)));
gui_internal_prune_menu(this, l->data);
}
/**
* @brief Remove the case in a string
*
* @warning Result should be g_free()d after use.
*
* @param s The input utf-8 string
* @return An equivalent string prepared for case insensitive search
*/
char *removecase(char *s) {
char *r;
r=linguistics_casefold(s);
return r;
}
/**
* @brief Apply the command "View on Map", centers the map on the selected point and highlight this point using
* type_found_item style
*
* @param this The GUI context
* @param wm The widget that points to this function as a callback
* @param data Private data provided during callback (unused)
*/
static void gui_internal_cmd_view_on_map(struct gui_priv *this, struct widget *wm, void *data) {
struct widget *w;
struct widget *wr;
struct widget *wi;
char *label;
if (wm->item.type != type_none) {
enum item_type type;
if (wm->item.type < type_line)
type=type_selected_point;
else if (wm->item.type < type_area)
type=type_selected_point;
else
type=type_selected_area;
graphics_clear_selection(this->gra, NULL);
graphics_add_selection(this->gra, &wm->item, type, NULL);
} else {
if (wm->item.priv_data)
label = wm->item.priv_data; /* Use the label of the point to view on map */
else
label = g_strdup("");
w = gui_internal_widget_table_new(this, 0, 0); /* Create a basic table */
gui_internal_widget_append(w,wr=gui_internal_widget_table_row_new(this,0)); /* In this table, add one row */
gui_internal_widget_append(wr,wi=gui_internal_box_new_with_label(this,0,
label)); /* That row contains a widget of type widget_box */
wi->name = label; /* Use the label of the point to view on map */
wi->c.x=wm->c.x; /* Use the coordinates of the point to place it on the map */
wi->c.y=wm->c.y;
gui_internal_prepare_search_results_map(this, w, NULL);
g_free(label);
wi->name = NULL;
gui_internal_widget_destroy(this, w);
}
navit_set_center(this->nav, &wm->c, 1);
gui_internal_prune_menu(this, NULL);
}
static void gui_internal_cmd_view_attribute_details(struct gui_priv *this, struct widget *wm, void *data) {
struct widget *w,*wb;
struct map_rect *mr;
struct item *item;
struct attr attr;
char *text,*url;
int i;
text=g_strdup_printf("Attribute %s",wm->name);
wb=gui_internal_menu(this, text);
g_free(text);
w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
gui_internal_widget_append(wb, w);
mr=map_rect_new(wm->item.map, NULL);
item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
for (i = 0 ; i < wm->datai ; i++) {
item_attr_get(item, attr_any, &attr);
}
if (item_attr_get(item, attr_any, &attr)) {
url=NULL;
switch (attr.type) {
case attr_osm_nodeid:
url=g_strdup_printf("http://www.openstreetmap.org/browse/node/"LONGLONG_FMT"\n",*attr.u.num64);
break;
case attr_osm_wayid:
url=g_strdup_printf("http://www.openstreetmap.org/browse/way/"LONGLONG_FMT"\n",*attr.u.num64);
break;
case attr_osm_relationid:
url=g_strdup_printf("http://www.openstreetmap.org/browse/relation/"LONGLONG_FMT"\n",*attr.u.num64);
break;
default:
break;
}
if (url) {
gui_internal_widget_append(w,
wb=gui_internal_button_new_with_callback(this, _("View in Browser"),
image_new_xs(this, "gui_active"), gravity_left_center|orientation_horizontal|flags_fill,
gui_internal_cmd_view_in_browser, NULL));
wb->name=url;
}
}
map_rect_destroy(mr);
gui_internal_menu_render(this);
}
static void gui_internal_cmd_view_attributes(struct gui_priv *this, struct widget *wm, void *data) {
struct widget *w,*wb;
struct map_rect *mr;
struct item *item;
struct attr attr;
char *text;
int count=0;
dbg(lvl_info,"item=%p 0x%x 0x%x", wm->item.map,wm->item.id_hi, wm->item.id_lo);
wb=gui_internal_menu(this, "Attributes");
w=gui_internal_box_new(this, gravity_top_center|orientation_vertical|flags_expand|flags_fill);
gui_internal_widget_append(wb, w);
mr=map_rect_new(wm->item.map, NULL);
item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
dbg(lvl_info,"item=%p", item);
if (item) {
text=g_strdup_printf("%s:%s", _("Item type"), item_to_name(item->type));
gui_internal_widget_append(w,
wb=gui_internal_button_new(this, text,
NULL, gravity_left_center|orientation_horizontal|flags_fill));
wb->name=g_strdup(text);
wb->item=wm->item;
g_free(text);
while(item_attr_get(item, attr_any, &attr)) {
char *attrtxt;
text=g_strdup_printf("%s:%s", attr_to_name(attr.type), attrtxt=attr_to_text(&attr, wm->item.map, 1));
g_free(attrtxt);
gui_internal_widget_append(w,
wb=gui_internal_button_new_with_callback(this, text,
NULL, gravity_left_center|orientation_horizontal|flags_fill,
gui_internal_cmd_view_attribute_details, NULL));
wb->name=g_strdup(text);
wb->item=wm->item;
wb->datai=count++;
g_free(text);
}
text=g_strdup_printf("%s:0x%x,0x%x", "ID", item->id_hi, item->id_lo);
gui_internal_widget_append(w,
wb=gui_internal_button_new(this, text,
NULL, gravity_left_center|orientation_horizontal|flags_fill));
wb->name=text;
wb->item=wm->item;
}
map_rect_destroy(mr);
gui_internal_menu_render(this);
}
static void gui_internal_cmd_view_in_browser(struct gui_priv *this, struct widget *wm, void *data) {
struct map_rect *mr;
struct item *item;
struct attr attr;
char *cmd=NULL;
if (!wm->name) {
dbg(lvl_info,"item=%p 0x%x 0x%x", wm->item.map,wm->item.id_hi, wm->item.id_lo);
mr=map_rect_new(wm->item.map, NULL);
item = map_rect_get_item_byid(mr, wm->item.id_hi, wm->item.id_lo);
dbg(lvl_info,"item=%p", item);
if (item) {
while(item_attr_get(item, attr_url_local, &attr)) {
if (! cmd)
cmd=g_strdup_printf("navit-browser.sh '%s' &",map_convert_string_tmp(item->map,attr.u.str));
}
}
map_rect_destroy(mr);
} else {
cmd=g_strdup_printf("navit-browser.sh '%s' &",wm->name);
}
if (cmd) {
#ifdef HAVE_SYSTEM
system(cmd);
#else
dbg(lvl_error,"Error: External commands were disabled during compilation, cannot call '%s'.",cmd);
#endif
g_free(cmd);
}
}
/**
* @brief Get the search result map (and create it if it does not exist)
*
* @param this The GUI context
*
* @return A pointer to the map named "search_results" or NULL if there wasa failure
*/
static struct map *get_search_results_map(struct gui_priv *this) {
struct mapset *ms;
struct map *map;
ms=navit_get_mapset(this->nav);
if(!ms)
return NULL;
map=mapset_get_map_by_name(ms, "search_results");
if(!map) {
struct attr *attrs[10], attrmap;
enum attr_type types[]= {attr_position_longitude,attr_position_latitude,attr_label,attr_none};
int i;
attrs[0]=g_new0(struct attr,1);
attrs[0]->type=attr_type;
attrs[0]->u.str="csv";
attrs[1]=g_new0(struct attr,1);
attrs[1]->type=attr_name;
attrs[1]->u.str="search_results";
attrs[2]=g_new0(struct attr,1);
attrs[2]->type=attr_charset;
attrs[2]->u.str="utf-8";
attrs[3]=g_new0(struct attr,1);
attrs[3]->type=attr_item_type;
attrs[3]->u.num=type_found_item;
attrs[4]=g_new0(struct attr,1);
attrs[4]->type=attr_attr_types;
attrs[4]->u.attr_types=types;
attrs[5]=NULL;
attrmap.type=attr_map;
map=attrmap.u.map=map_new(NULL,attrs);
if(map)
mapset_add_attr(ms,&attrmap);
for(i=0; attrs[i]; i++)
g_free(attrs[i]);
}
return map;
}
/**
* @brief Optimizes the format of a string, adding carriage returns so that when displayed, the result text zone is roughly as wide as high
*
* @param[in,out] s The string to proces (will be modified by this function, but length will be unchanged)
*/
static void square_shape_str(char *s) {
char *c;
char *last_break;
unsigned int max_cols = 0;
unsigned int cur_cols = 0;
unsigned int max_rows = 0;
unsigned int surface;
unsigned int target_cols;
if (!s)
return;
for (c=s; *c!='\0'; c++) {
if (*c==' ') {
if (max_cols < cur_cols)
max_cols = cur_cols;
cur_cols = 0;
max_rows++;
} else
cur_cols++;
}
if (max_cols < cur_cols)
max_cols = cur_cols;
if (cur_cols) /* If last line does not end with CR, add it to line numbers anyway */
max_rows++;
/* Give twice more room for rows (hence the factor 2 below)
* This will render as a rectangular shape, taking more horizontal space than vertical */
surface = max_rows * 2 * max_cols;
target_cols = uint_sqrt(surface);
if (target_cols < max_cols)
target_cols = max_cols;
target_cols = target_cols + target_cols/10; /* Allow 10% extra on columns */
dbg(lvl_debug, "square_shape_str(): analyzing input text=\"%s\". max_rows=%u, max_cols=%u, surface=%u, target_cols=%u",
s, max_rows, max_cols, max_rows * 2 * max_cols, target_cols);
cur_cols = 0;
last_break = NULL;
for (c=s; *c!='\0'; c++) {
if (*c==' ') {
if (cur_cols>=target_cols) { /* This line is too long, break at the previous non alnum character */
if (last_break) {
*last_break =
'\n'; /* Replace the previous non alnum character with a line break, this creates a new line and prevents the previous line from being too long */