File: ui.c

package info (click to toggle)
s3d 0.2.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,256 kB
  • ctags: 2,684
  • sloc: ansic: 20,585; perl: 98; makefile: 32; python: 8; sh: 4
file content (174 lines) | stat: -rw-r--r-- 4,773 bytes parent folder | download
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
/*
 * ui.c
 *
 * Copyright (C) 2006-2008 Simon Wunderlich <dotslash@packetmixer.de>
 *
 * This file is part of s3dosm, a gps card application for s3d.
 * See http://s3d.berlios.de/ for more updates.
 *
 * s3dosm is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * s3dosm 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 s3dosm; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <s3d.h>
#include <s3dw.h>
#include "s3dosm.h"
#include <stdio.h> /* NULL */
#include <string.h> /* strcmp */
#include <stdlib.h> /* atoi() */
icon_t icons[ICON_NUM] = {
	{"objs/accesspoint.3ds", 0},
	{"objs/noinetwep.3ds", 0},
	{"objs/noinetwpa.3ds", 0},
	{"objs/arrow2.3ds", 0}
};

/* load icons, we want to clone each of them later */
static void ui_loadicons(void)
{
	int i;
	for (i = 0;i < ICON_NUM;i++)
		icons[i].oid = s3d_import_model_file(icons[i].path);
}

static s3dw_surface *loadwindow = NULL;
static s3dw_label   *loadlabel = NULL;
static s3dw_label   *loadstatus = NULL;

static void key_button(s3dw_widget *button)
{
	s3dw_delete(button->parent); /* parent =surface. this means close containing window */
}

static int ui_getinfo_node(void *S3DOSMUNUSED(data), int argc, char **argv, char **azColName)
{
	int i, tagid = -1;
	char type[MAXQ];
	char name[MAXQ];
	char string[128];
	s3dw_surface *miniwin;
	s3dw_button  *button;

	for (i = 0; i < argc; i++) {
		if (argv[i]) {
			if (0 == strcmp(azColName[i], "tag_id"))    tagid = atoi(argv[i]);
		}
	}
	if (db_gettag(tagid, "amenity", type)) type[0] = 0;
	if (db_gettag(tagid, "name", name)) name[0] = 0;

	miniwin = s3dw_surface_new("About node", 30, 6);
	snprintf(string, 128, "name: %s", name);
	s3dw_label_new(miniwin, string, 1, 2);
	snprintf(string, 128, "type: %s", type);
	s3dw_label_new(miniwin, string, 1, 4);
	button = s3dw_button_new(miniwin, "OK", 2, 6);
	button->onclick = key_button;
	s3dw_show(S3DWIDGET(miniwin));

	return(0);
}
static int ui_getinfo_way(void *S3DOSMUNUSED(data), int argc, char **argv, char **azColName)
{
	int i, tagid = -1;
	char name[MAXQ];
	char string[128];
	s3dw_surface *miniwin;
	s3dw_button  *button;

	for (i = 0; i < argc; i++) {
		if (argv[i]) {
			if (0 == strcmp(azColName[i], "tag_id"))    tagid = atoi(argv[i]);
		}
	}
	if (db_gettag(tagid, "name", name)) name[0] = 0;
	printf("reporting street %s\n", name);

	miniwin = s3dw_surface_new("About street", 30, 6);
	snprintf(string, 128, "name: %s", name);
	s3dw_label_new(miniwin, string, 1, 2);
	button = s3dw_button_new(miniwin, "OK", 2, 4);
	button->onclick = key_button;
	s3dw_show(S3DWIDGET(miniwin));

	return(0);
}

static int ui_click(struct s3d_evt *evt)
{
	int oid = (int) * ((uint32_t *)evt->buf);
	char query[MAXQ];
	if (s3dw_handle_click(evt)) return(0);
	snprintf(query, MAXQ, "SELECT * FROM node WHERE s3doid=%d;", oid);
	db_exec(query, ui_getinfo_node, NULL);
	snprintf(query, MAXQ, "SELECT * FROM way WHERE s3doid=%d;", oid);
	db_exec(query, ui_getinfo_way, NULL);

	return(0);
}
static int ui_key(struct s3d_evt *evt)
{
	/* struct s3d_key_event *key=(struct s3d_key_event *)evt->buf;*/
	if (s3dw_handle_key(evt)) return(0);
	return(0);
}
static int ui_oinfo(struct s3d_evt *evt)
{
	s3dw_object_info(evt);
	return(0);
}
int ui_init(void)
{
	ui_loadicons();
	s3d_set_callback(S3D_EVENT_OBJ_CLICK, ui_click);
	s3d_set_callback(S3D_EVENT_KEY, ui_key);
	s3d_set_callback(S3D_EVENT_OBJ_INFO, ui_oinfo);
	return(0);
}

/* initialize the loadwindow or change its caption text */
int load_window(const char *text)
{
	if (loadwindow == NULL) { /* create it */
		loadwindow = s3dw_surface_new("Now loading ...", 20, 5);
		loadlabel = s3dw_label_new(loadwindow, text, 1, 2);
		loadstatus = s3dw_label_new(loadwindow, "", 1, 3);
		s3dw_show(S3DWIDGET(loadwindow));
	} else {
		s3dw_label_change_text(loadlabel, text);
		s3dw_label_change_text(loadstatus, "");
	}
	return(0);
}
/* remove it if still here */
int load_window_remove(void)
{
	if (loadwindow != NULL) {
		s3dw_delete(S3DWIDGET(loadwindow));
		loadwindow = NULL;
		loadlabel = NULL;
	}
	return(0);
}
/* update the load status ... */
int load_update_status(float percent)
{
	char text[128];
	if (loadwindow != NULL) {
		snprintf(text, 128, "%3.1f", percent);
		s3dw_label_change_text(loadstatus, text);
	}
	mainloop();
	return(0);
}