1 /* $Id: listbox.c,v 1.3 2004-12-19 15:21:11 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
16 static char rcsid[] = "$Id: listbox.c,v 1.3 2004-12-19 15:21:11 btb Exp $";
31 #define TICKER (*(volatile int *)0x46C)
33 void ui_draw_listbox( UI_GADGET_LISTBOX * listbox )
38 //if (listbox->current_item<0)
39 // listbox->current_item=0;
40 //if (listbox->current_item>=listbox->num_items)
41 // listbox->current_item = listbox->num_items-1;
42 //if (listbox->first_item<0)
43 // listbox->first_item=0;
44 //if (listbox->first_item>(listbox->num_items-listbox->num_items_displayed))
45 // listbox->first_item=(listbox->num_items-listbox->num_items_displayed);
47 if ((listbox->status!=1) && !listbox->moved )
50 stop = listbox->first_item+listbox->num_items_displayed;
51 if (stop>listbox->num_items) stop = listbox->num_items;
57 gr_set_current_canvas( listbox->canvas );
59 for (i= listbox->first_item; i< stop; i++ )
61 if (i !=listbox->current_item)
63 if ((listbox->current_item == -1) && (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox) && (i == listbox->first_item) )
64 gr_set_fontcolor( CRED, CBLACK );
66 gr_set_fontcolor( CWHITE, CBLACK );
70 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
71 gr_set_fontcolor( CRED, CGREY );
73 gr_set_fontcolor( CBLACK, CGREY );
75 gr_string( x+2, y, listbox->list+i*listbox->text_width );
76 gr_get_string_size(listbox->list+i*listbox->text_width, &w, &h,&aw );
78 if (i==listbox->current_item)
81 gr_setcolor( CBLACK );
83 gr_rect( x+w+2, y, listbox->width-1, y+h-1 );
84 gr_rect( x, y, x+1, y+h-1 );
89 if (stop < listbox->num_items_displayed-1 )
92 gr_rect( x, y, listbox->width-1, listbox->height-1 );
95 //gr_ubox( -1, -1, listbox->width, listbox->height);
101 void gr_draw_sunken_border( short x1, short y1, short x2, short y2 )
104 gr_setcolor( CGREY );
105 Hline( x1-1, x2+1, y1-1);
106 Vline( y1-1, y2+1, x1-1);
108 gr_setcolor( CBRIGHT );
109 Hline( x1-1, x2+1, y2+1);
110 Vline( y1, y2+1, x2+1);
115 UI_GADGET_LISTBOX * ui_add_gadget_listbox( UI_WINDOW * wnd, short x, short y, short w, short h, short numitems, char * list, int text_width )
119 UI_GADGET_LISTBOX * listbox;
121 gr_get_string_size("*", &tw, &th, &taw );
126 listbox = (UI_GADGET_LISTBOX *)ui_gadget_add( wnd, 2, x, y, x+w-1, y+h-1 );
128 listbox->list = list;
129 listbox->text_width = text_width;
132 listbox->num_items = numitems;
133 listbox->num_items_displayed = i;
134 listbox->first_item = 0;
135 listbox->current_item = -1;
136 listbox->last_scrolled = 0;
137 listbox->textheight = th;
138 listbox->dragging = 0;
139 listbox->selected_item = -1;
142 listbox->scrollbar = ui_add_gadget_scrollbar( wnd, x+w+3, y, 0, h, 0, numitems-i, 0, i );
143 listbox->scrollbar->parent = (UI_GADGET *)listbox;
145 gr_set_current_canvas( listbox->canvas );
148 gr_rect( 0, 0, w-1, h-1);
150 gr_draw_sunken_border( -2, -2, w+listbox->scrollbar->width+4, h+1);
156 void ui_listbox_do( UI_GADGET_LISTBOX * listbox, int keypress )
158 int OnMe, mitem, oldfakepos, kf;
160 listbox->selected_item = -1;
164 if (listbox->num_items < 1 ) {
165 listbox->current_item = -1;
166 listbox->first_item = 0;
167 listbox->old_current_item = listbox->current_item;
168 listbox->old_first_item = listbox->first_item;
169 ui_draw_listbox( listbox );
171 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
173 CurWindow->keyboard_focus_gadget = ui_gadget_get_next((UI_GADGET *)listbox);
179 listbox->old_current_item = listbox->current_item;
180 listbox->old_first_item = listbox->first_item;
182 OnMe = ui_mouse_on_gadget( (UI_GADGET *)listbox );
185 if (listbox->scrollbar->moved )
189 listbox->first_item = listbox->scrollbar->position;
191 if (listbox->current_item<listbox->first_item)
192 listbox->current_item = listbox->first_item;
194 if (listbox->current_item>(listbox->first_item+listbox->num_items_displayed-1))
195 listbox->current_item = listbox->first_item + listbox->num_items_displayed-1;
200 listbox->dragging = 0;
202 if (B1_PRESSED && OnMe )
203 listbox->dragging = 1;
205 if ( CurWindow->keyboard_focus_gadget==(UI_GADGET *)listbox )
207 if (keypress==KEY_ENTER) {
208 listbox->selected_item = listbox->current_item;
216 listbox->current_item--;
220 listbox->current_item++;
224 listbox->current_item=0;
228 listbox->current_item=listbox->num_items-1;
232 listbox->current_item -= listbox->num_items_displayed;
236 listbox->current_item += listbox->num_items_displayed;
245 if (listbox->current_item<0)
246 listbox->current_item=0;
248 if (listbox->current_item>=listbox->num_items)
249 listbox->current_item = listbox->num_items-1;
251 if (listbox->current_item<listbox->first_item)
252 listbox->first_item = listbox->current_item;
254 if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
255 listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
257 if (listbox->num_items <= listbox->num_items_displayed )
258 listbox->first_item = 0;
261 oldfakepos = listbox->scrollbar->position;
262 listbox->scrollbar->position = listbox->first_item;
264 listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
265 listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
266 listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
268 if (listbox->scrollbar->fake_position<0)
270 listbox->scrollbar->fake_position = 0;
272 if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
274 listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
277 if (oldfakepos != listbox->scrollbar->position )
278 listbox->scrollbar->status = 1;
284 if (selected_gadget==(UI_GADGET *)listbox)
286 if (B1_PRESSED && listbox->dragging)
288 if (Mouse.y < listbox->y1)
291 mitem = (Mouse.y - listbox->y1)/listbox->textheight;
293 if ( (mitem < 0 ) && ( TICKER > listbox->last_scrolled+1) )
295 listbox->current_item--;
296 listbox->last_scrolled = TICKER;
300 if ( ( mitem >= listbox->num_items_displayed ) &&
301 ( TICKER > listbox->last_scrolled+1) )
303 listbox->current_item++;
304 listbox->last_scrolled = TICKER;
308 if ((mitem>=0) && (mitem<listbox->num_items_displayed))
310 listbox->current_item = mitem+listbox->first_item;
314 if (listbox->current_item <0 )
315 listbox->current_item = 0;
317 if (listbox->current_item >= listbox->num_items )
318 listbox->current_item = listbox->num_items-1;
320 if (listbox->current_item<listbox->first_item)
321 listbox->first_item = listbox->current_item;
323 if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
324 listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
326 if (listbox->num_items <= listbox->num_items_displayed )
327 listbox->first_item = 0;
330 oldfakepos = listbox->scrollbar->position;
331 listbox->scrollbar->position = listbox->first_item;
333 listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
334 listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
335 listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
337 if (listbox->scrollbar->fake_position<0)
339 listbox->scrollbar->fake_position = 0;
341 if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
343 listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
346 if (oldfakepos != listbox->scrollbar->position )
347 listbox->scrollbar->status = 1;
352 if (B1_DOUBLE_CLICKED )
354 listbox->selected_item = listbox->current_item;
359 ui_draw_listbox( listbox );
363 void ui_listbox_change( UI_WINDOW * wnd, UI_GADGET_LISTBOX * listbox, short numitems, char * list, int text_width )
366 UI_GADGET_SCROLLBAR * scrollbar;
370 listbox->list = list;
371 listbox->text_width = text_width;
372 listbox->num_items = numitems;
373 listbox->first_item = 0;
374 listbox->current_item = -1;
375 listbox->last_scrolled = TICKER;
376 listbox->dragging = 0;
377 listbox->selected_item = -1;
379 listbox->first_item = 0;
380 listbox->current_item = listbox->old_current_item = 0;
383 scrollbar = listbox->scrollbar;
386 stop= numitems - listbox->num_items_displayed;
388 if (stop < start) stop = start;
391 scrollbar->start = start;
392 scrollbar->stop = stop;
393 scrollbar->fake_length = scrollbar->height;
394 scrollbar->fake_position = 0;
396 scrollbar->fake_size = (listbox->num_items_displayed * scrollbar->height)/(stop-start+1+listbox->num_items_displayed);
398 scrollbar->fake_size = scrollbar->height;
400 if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
401 scrollbar->dragging = 0;