File: sarmenuop.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (337 lines) | stat: -rw-r--r-- 7,412 bytes parent folder | download | duplicates (3)
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
#include <stdlib.h>
#include <string.h>

#include "menu.h"
#include "sar.h"
#include "sarmenuop.h"


void SARMenuSwitchToMenu(sar_core_struct *core_ptr, const char *name);
sar_menu_list_struct *SARMenuGetList(
	sar_menu_struct *m, int skip, int *list_num
);
sar_menu_spin_struct *SARMenuGetSpin(
	sar_menu_struct *m, int skip, int *spin_num
);
sar_menu_list_item_struct *SARMenuListGetSelectedItem(
	sar_menu_struct *m, int id
);
char *SARMenuGetCurrentMenuName(sar_core_struct *core_ptr);


#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)


/*
 *	Switches to the specified menu.
 *
 *	If no menu is specified (name=NULL) then simulation will
 *	begin.
 */
void SARMenuSwitchToMenu(sar_core_struct *core_ptr, const char *name)
{
	int new_menu_num, old_menu_num;
	gw_display_struct *display;
	const sar_option_struct *opt;

	if(core_ptr == NULL)
	    return;

	display = core_ptr->display;
	opt = &core_ptr->option;

	/* Record previous menu number */
	old_menu_num = core_ptr->cur_menu;

	/* No specified menu to switch to? */
	if(name == NULL)
	{
	    /* This implies we are going out of the menu system and
	     * entering simulation
	     */
	    sar_menu_struct *old_menu = SARIsMenuAllocated(core_ptr, old_menu_num) ?
		core_ptr->menu[old_menu_num] : NULL;

	    /* Unload image on old menu */
/*	    if(opt->prioritize_memory && (old_menu != NULL)) */
	    if(old_menu != NULL)
		SARMenuUnloadBackgroundImage(old_menu);

	    /* Unselect current menu on core structure, indicating that
	     * no menu is selected and that we are not in the menus
	     */
	    core_ptr->cur_menu = -1;

	    /* Skip redrawing, since redrawing would be done rather
	     * frequently during the simulation
	     */
/*	    GWPostRedraw(display); */

	    return;
	}

	/* Get the number of the menu that we want to switch to */
	new_menu_num = SARMatchMenuByName(core_ptr, name);
	/* Got match and matched menu number is different from the one
	 * currently selected?
	 */
	if((new_menu_num > -1) && (new_menu_num != core_ptr->cur_menu))
	{
	    sar_menu_struct *old_menu = SARIsMenuAllocated(core_ptr, old_menu_num) ?
		core_ptr->menu[old_menu_num] : NULL;
	    sar_menu_struct *new_menu = SARIsMenuAllocated(core_ptr, new_menu_num) ?
		core_ptr->menu[new_menu_num] : NULL;
	    const char	*old_bg_image_path = (old_menu != NULL) ?
		old_menu->bg_image_path : NULL,
			*new_bg_image_path = (new_menu != NULL) ?
		new_menu->bg_image_path : NULL;

	    GWSetInputBusy(display);

	    /* Reset values on the previous menu */
	    if(old_menu != NULL)
	    {





	    }

	    /* Check if background images are the same on both old and
	     * new menus by comparing the background image paths (if
	     * defined)
	     */
	    if(((old_bg_image_path != NULL) &&
		(new_bg_image_path != NULL)) ?
	         ((old_menu->bg_image != NULL) ?
		  !strcmp(old_bg_image_path, new_bg_image_path) : False)
		: False
	    )
	    {
		/* Image paths are the same, so unload the image on the
		 * new menu and transfer the image from the old menu to
		 * the new menu
		 *
		 * Mark the old menu's image NULL after transfer so
		 * that it dosen't get referenced again
		 *
		 * Note that both new_menu and old_menu are known to be
		 * not NULL from the above check
		 */
		SARMenuUnloadBackgroundImage(new_menu);
		if(new_menu->bg_image == NULL)
		{
		    new_menu->bg_image = old_menu->bg_image;
		    old_menu->bg_image = NULL;
		}
	    }
	    else
	    {
		/* Image paths are different, so unload background image
		 * on old menu and load new background image on new menu
		 */
/*		if(opt->prioritize_memory && (old_menu != NULL)) */
		if(old_menu != NULL)
		    SARMenuUnloadBackgroundImage(old_menu);

		if(opt->menu_backgrounds &&
		   ((new_menu != NULL) ? (new_menu->bg_image == NULL) : False)
		)
		    SARMenuLoadBackgroundImage(new_menu);
	    }

	    /* Is the new menu valid? */
	    if(new_menu != NULL)
	    {
		/* New menu is valid, so switch to it */
		int n;
		void *o;
		sar_menu_button_struct *btn;
		sar_menu_struct *m = new_menu;

		/* Begin resetting object values on the new menu */
		for(n = 0; n < m->total_objects; n++)
		{
		    o = m->object[n];
		    if(o == NULL)
			continue;

		    switch(SAR_MENU_OBJECT_TYPE(o))
		    {
		      case SAR_MENU_OBJECT_TYPE_BUTTON:
			btn = SAR_MENU_BUTTON(o);
			btn->state = SAR_MENU_BUTTON_STATE_UNARMED;
			break;

		      case SAR_MENU_OBJECT_TYPE_PROGRESS:
			SARMenuProgressSet(
			    display, m, n,
			    0.0f, False
			);
			break;
		    }
		}

		/* Select first object on menu */
		m->selected_object = 0;

		/* Update index number of the current menu */
	        core_ptr->cur_menu = new_menu_num;
	    }

	    /* Redraw */
	    GWPostRedraw(display);

	    /* Mark input as ready */
	    GWSetInputReady(display);
	}
}

/*
 *	Returns the pointer to the list object on the menu,
 *	skipping the indicated number of lists.
 *
 *	Can return NULL for no match.
 */
sar_menu_list_struct *SARMenuGetList(
	sar_menu_struct *m, int skip, int *list_num
)     
{
	int i;
	void *ptr;
	sar_menu_list_struct *list_ptr;


	if(m == NULL)
	    return(NULL);

	if(list_num != NULL)
	    *list_num = -1;
 
	for(i = 0; i < m->total_objects; i++)
	{       
	    ptr = m->object[i];
	    if(ptr == NULL)
		continue;

	    if(*(int *)ptr != SAR_MENU_OBJECT_TYPE_LIST)
		continue;

	    /* Get pointer to list object. */
	    list_ptr = ptr;

	    /* Skip this list object? */
	    if(skip > 0)
	    {
		skip--;
		continue;
	    }
	    else
	    {
		if(list_num != NULL)
		    *list_num = i;
		return(list_ptr);
	    }
	}

	return(NULL);
}

/*
 *      Returns the pointer to the spin object on the menu,
 *      skipping the indicated number of spin objects.
 *
 *	Can return NULL for no match.
 */
sar_menu_spin_struct *SARMenuGetSpin(
	sar_menu_struct *m, int skip, int *spin_num
)
{
	int i;
	void *ptr;
	sar_menu_spin_struct *spin_ptr;


	if(m == NULL)
	    return(NULL);

	if(spin_num != NULL)
	    *spin_num = -1;

	for(i = 0; i < m->total_objects; i++)
	{
	    ptr = m->object[i];
	    if(ptr == NULL)
		continue;
 
	    if(*(int *)ptr != SAR_MENU_OBJECT_TYPE_SPIN)
		continue;

	    /* Get pointer to spin object. */
	    spin_ptr = ptr;

	    /* Skip this spin object? */
	    if(skip > 0)
	    {
		skip--;
		continue;
	    }
	    else
	    {
		if(spin_num != NULL)
		    *spin_num = i;

		return(spin_ptr);
	    }
	}

	return(NULL);
}

/*
 *	Returns the pointer to the selected list item on the list object
 *	on the menu, skipping the indicated number of list objects.
 */
sar_menu_list_item_struct *SARMenuListGetSelectedItem(
	sar_menu_struct *m, int id
)
{
	int i;
	sar_menu_list_struct *list = SAR_MENU_LIST(
	    SARMenuGetObjectByID(m, id, NULL)
	);
	if(list == NULL)
	    return(NULL);

	i = list->selected_item;
	if((i >= 0) && (i < list->total_items))
	    return(list->item[i]);
	else
	    return(NULL);
}

/*
 *	Returns the pointer to the name of the currently selected
 *	menu or NULL on failure.
 */
char *SARMenuGetCurrentMenuName(sar_core_struct *core_ptr)
{
	int cur_menu;
	sar_menu_struct *m;


	if(core_ptr == NULL)
	    return(NULL);

	cur_menu = core_ptr->cur_menu;
	if(SARIsMenuAllocated(core_ptr, cur_menu))
	    m = core_ptr->menu[cur_menu];
	else
	    return(NULL);

	return(m->name);
}