2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
14 #pragma off (unreferenced)
15 static char rcsid[] = "$Id: listbox.c,v 1.1.1.1 2001-01-19 03:30:14 bradleyb Exp $";
16 #pragma on (unreferenced)
25 #define TICKER (*(volatile int *)0x46C)
27 void ui_draw_listbox( UI_GADGET_LISTBOX * listbox )
32 //if (listbox->current_item<0)
33 // listbox->current_item=0;
34 //if (listbox->current_item>=listbox->num_items)
35 // listbox->current_item = listbox->num_items-1;
36 //if (listbox->first_item<0)
37 // listbox->first_item=0;
38 //if (listbox->first_item>(listbox->num_items-listbox->num_items_displayed))
39 // listbox->first_item=(listbox->num_items-listbox->num_items_displayed);
41 if ((listbox->status!=1) && !listbox->moved )
44 stop = listbox->first_item+listbox->num_items_displayed;
45 if (stop>listbox->num_items) stop = listbox->num_items;
51 gr_set_current_canvas( listbox->canvas );
53 for (i= listbox->first_item; i< stop; i++ )
55 if (i !=listbox->current_item)
57 if ((listbox->current_item == -1) && (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox) && (i == listbox->first_item) )
58 gr_set_fontcolor( CRED, CBLACK );
60 gr_set_fontcolor( CWHITE, CBLACK );
64 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
65 gr_set_fontcolor( CRED, CGREY );
67 gr_set_fontcolor( CBLACK, CGREY );
69 gr_string( x+2, y, listbox->list+i*listbox->text_width );
70 gr_get_string_size(listbox->list+i*listbox->text_width, &w, &h,&aw );
72 if (i==listbox->current_item)
75 gr_setcolor( CBLACK );
77 gr_rect( x+w+2, y, listbox->width-1, y+h-1 );
78 gr_rect( x, y, x+1, y+h-1 );
83 if (stop < listbox->num_items_displayed-1 )
86 gr_rect( x, y, listbox->width-1, listbox->height-1 );
89 //gr_ubox( -1, -1, listbox->width, listbox->height);
95 void gr_draw_sunken_border( short x1, short y1, short x2, short y2 )
99 Hline( x1-1, x2+1, y1-1);
100 Vline( y1-1, y2+1, x1-1);
102 gr_setcolor( CBRIGHT );
103 Hline( x1-1, x2+1, y2+1);
104 Vline( y1, y2+1, x2+1);
109 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 )
113 UI_GADGET_LISTBOX * listbox;
115 gr_get_string_size("*", &tw, &th, &taw );
120 listbox = (UI_GADGET_LISTBOX *)ui_gadget_add( wnd, 2, x, y, x+w-1, y+h-1 );
122 listbox->list = list;
123 listbox->text_width = text_width;
126 listbox->num_items = numitems;
127 listbox->num_items_displayed = i;
128 listbox->first_item = 0;
129 listbox->current_item = -1;
130 listbox->last_scrolled = 0;
131 listbox->textheight = th;
132 listbox->dragging = 0;
133 listbox->selected_item = -1;
136 listbox->scrollbar = ui_add_gadget_scrollbar( wnd, x+w+3, y, 0, h, 0, numitems-i, 0, i );
137 listbox->scrollbar->parent = (UI_GADGET *)listbox;
139 gr_set_current_canvas( listbox->canvas );
142 gr_rect( 0, 0, w-1, h-1);
144 gr_draw_sunken_border( -2, -2, w+listbox->scrollbar->width+4, h+1);
150 void ui_listbox_do( UI_GADGET_LISTBOX * listbox, int keypress )
152 int OnMe, mitem, oldfakepos, kf;
154 listbox->selected_item = -1;
158 if (listbox->num_items < 1 ) {
159 listbox->current_item = -1;
160 listbox->first_item = 0;
161 listbox->old_current_item = listbox->current_item;
162 listbox->old_first_item = listbox->first_item;
163 ui_draw_listbox( listbox );
165 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
167 CurWindow->keyboard_focus_gadget = ui_gadget_get_next((UI_GADGET *)listbox);
173 listbox->old_current_item = listbox->current_item;
174 listbox->old_first_item = listbox->first_item;
176 OnMe = ui_mouse_on_gadget( (UI_GADGET *)listbox );
179 if (listbox->scrollbar->moved )
183 listbox->first_item = listbox->scrollbar->position;
185 if (listbox->current_item<listbox->first_item)
186 listbox->current_item = listbox->first_item;
188 if (listbox->current_item>(listbox->first_item+listbox->num_items_displayed-1))
189 listbox->current_item = listbox->first_item + listbox->num_items_displayed-1;
194 listbox->dragging = 0;
196 if (B1_PRESSED && OnMe )
197 listbox->dragging = 1;
199 if ( CurWindow->keyboard_focus_gadget==(UI_GADGET *)listbox )
201 if (keypress==KEY_ENTER) {
202 listbox->selected_item = listbox->current_item;
210 listbox->current_item--;
214 listbox->current_item++;
218 listbox->current_item=0;
222 listbox->current_item=listbox->num_items-1;
226 listbox->current_item -= listbox->num_items_displayed;
230 listbox->current_item += listbox->num_items_displayed;
239 if (listbox->current_item<0)
240 listbox->current_item=0;
242 if (listbox->current_item>=listbox->num_items)
243 listbox->current_item = listbox->num_items-1;
245 if (listbox->current_item<listbox->first_item)
246 listbox->first_item = listbox->current_item;
248 if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
249 listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
251 if (listbox->num_items <= listbox->num_items_displayed )
252 listbox->first_item = 0;
255 oldfakepos = listbox->scrollbar->position;
256 listbox->scrollbar->position = listbox->first_item;
258 listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
259 listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
260 listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
262 if (listbox->scrollbar->fake_position<0)
264 listbox->scrollbar->fake_position = 0;
266 if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
268 listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
271 if (oldfakepos != listbox->scrollbar->position )
272 listbox->scrollbar->status = 1;
278 if (selected_gadget==(UI_GADGET *)listbox)
280 if (B1_PRESSED && listbox->dragging)
282 if (Mouse.y < listbox->y1)
285 mitem = (Mouse.y - listbox->y1)/listbox->textheight;
287 if ( (mitem < 0 ) && ( TICKER > listbox->last_scrolled+1) )
289 listbox->current_item--;
290 listbox->last_scrolled = TICKER;
294 if ( ( mitem >= listbox->num_items_displayed ) &&
295 ( TICKER > listbox->last_scrolled+1) )
297 listbox->current_item++;
298 listbox->last_scrolled = TICKER;
302 if ((mitem>=0) && (mitem<listbox->num_items_displayed))
304 listbox->current_item = mitem+listbox->first_item;
308 if (listbox->current_item <0 )
309 listbox->current_item = 0;
311 if (listbox->current_item >= listbox->num_items )
312 listbox->current_item = listbox->num_items-1;
314 if (listbox->current_item<listbox->first_item)
315 listbox->first_item = listbox->current_item;
317 if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
318 listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
320 if (listbox->num_items <= listbox->num_items_displayed )
321 listbox->first_item = 0;
324 oldfakepos = listbox->scrollbar->position;
325 listbox->scrollbar->position = listbox->first_item;
327 listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
328 listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
329 listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
331 if (listbox->scrollbar->fake_position<0)
333 listbox->scrollbar->fake_position = 0;
335 if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
337 listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
340 if (oldfakepos != listbox->scrollbar->position )
341 listbox->scrollbar->status = 1;
346 if (B1_DOUBLE_CLICKED )
348 listbox->selected_item = listbox->current_item;
353 ui_draw_listbox( listbox );
357 void ui_listbox_change( UI_WINDOW * wnd, UI_GADGET_LISTBOX * listbox, short numitems, char * list, int text_width )
360 UI_GADGET_SCROLLBAR * scrollbar;
364 listbox->list = list;
365 listbox->text_width = text_width;
366 listbox->num_items = numitems;
367 listbox->first_item = 0;
368 listbox->current_item = -1;
369 listbox->last_scrolled = TICKER;
370 listbox->dragging = 0;
371 listbox->selected_item = -1;
373 listbox->first_item = 0;
374 listbox->current_item = listbox->old_current_item = 0;
377 scrollbar = listbox->scrollbar;
380 stop= numitems - listbox->num_items_displayed;
382 if (stop < start) stop = start;
385 scrollbar->start = start;
386 scrollbar->stop = stop;
387 scrollbar->fake_length = scrollbar->height;
388 scrollbar->fake_position = 0;
390 scrollbar->fake_size = (listbox->num_items_displayed * scrollbar->height)/(stop-start+1+listbox->num_items_displayed);
392 scrollbar->fake_size = scrollbar->height;
394 if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
395 scrollbar->dragging = 0;