1 /* $Id: listbox.c,v 1.5 2005-01-25 19:44:27 schaffner 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.5 2005-01-25 19:44:27 schaffner Exp $";
32 void ui_draw_listbox( UI_GADGET_LISTBOX * listbox )
37 //if (listbox->current_item<0)
38 // listbox->current_item=0;
39 //if (listbox->current_item>=listbox->num_items)
40 // listbox->current_item = listbox->num_items-1;
41 //if (listbox->first_item<0)
42 // listbox->first_item=0;
43 //if (listbox->first_item>(listbox->num_items-listbox->num_items_displayed))
44 // listbox->first_item=(listbox->num_items-listbox->num_items_displayed);
46 if ((listbox->status!=1) && !listbox->moved )
49 stop = listbox->first_item+listbox->num_items_displayed;
50 if (stop>listbox->num_items) stop = listbox->num_items;
56 gr_set_current_canvas( listbox->canvas );
58 for (i= listbox->first_item; i< stop; i++ )
60 if (i !=listbox->current_item)
62 if ((listbox->current_item == -1) && (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox) && (i == listbox->first_item) )
63 gr_set_fontcolor( CRED, CBLACK );
65 gr_set_fontcolor( CWHITE, CBLACK );
69 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
70 gr_set_fontcolor( CRED, CGREY );
72 gr_set_fontcolor( CBLACK, CGREY );
74 gr_string( x+2, y, listbox->list+i*listbox->text_width );
75 gr_get_string_size(listbox->list+i*listbox->text_width, &w, &h,&aw );
77 if (i==listbox->current_item)
80 gr_setcolor( CBLACK );
82 gr_rect( x+w+2, y, listbox->width-1, y+h-1 );
83 gr_rect( x, y, x+1, y+h-1 );
88 if (stop < listbox->num_items_displayed-1 )
91 gr_rect( x, y, listbox->width-1, listbox->height-1 );
94 //gr_ubox( -1, -1, listbox->width, listbox->height);
100 void gr_draw_sunken_border( short x1, short y1, short x2, short y2 )
103 gr_setcolor( CGREY );
104 Hline( x1-1, x2+1, y1-1);
105 Vline( y1-1, y2+1, x1-1);
107 gr_setcolor( CBRIGHT );
108 Hline( x1-1, x2+1, y2+1);
109 Vline( y1, y2+1, x2+1);
114 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 )
118 UI_GADGET_LISTBOX * listbox;
120 gr_get_string_size("*", &tw, &th, &taw );
125 listbox = (UI_GADGET_LISTBOX *)ui_gadget_add( wnd, 2, x, y, x+w-1, y+h-1 );
127 listbox->list = list;
128 listbox->text_width = text_width;
131 listbox->num_items = numitems;
132 listbox->num_items_displayed = i;
133 listbox->first_item = 0;
134 listbox->current_item = -1;
135 listbox->last_scrolled = 0;
136 listbox->textheight = th;
137 listbox->dragging = 0;
138 listbox->selected_item = -1;
141 listbox->scrollbar = ui_add_gadget_scrollbar( wnd, x+w+3, y, 0, h, 0, numitems-i, 0, i );
142 listbox->scrollbar->parent = (UI_GADGET *)listbox;
144 gr_set_current_canvas( listbox->canvas );
147 gr_rect( 0, 0, w-1, h-1);
149 gr_draw_sunken_border( -2, -2, w+listbox->scrollbar->width+4, h+1);
155 void ui_listbox_do( UI_GADGET_LISTBOX * listbox, int keypress )
157 int OnMe, mitem, oldfakepos, kf;
159 listbox->selected_item = -1;
163 if (listbox->num_items < 1 ) {
164 listbox->current_item = -1;
165 listbox->first_item = 0;
166 listbox->old_current_item = listbox->current_item;
167 listbox->old_first_item = listbox->first_item;
168 ui_draw_listbox( listbox );
170 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
172 CurWindow->keyboard_focus_gadget = ui_gadget_get_next((UI_GADGET *)listbox);
178 listbox->old_current_item = listbox->current_item;
179 listbox->old_first_item = listbox->first_item;
181 OnMe = ui_mouse_on_gadget( (UI_GADGET *)listbox );
184 if (listbox->scrollbar->moved )
188 listbox->first_item = listbox->scrollbar->position;
190 if (listbox->current_item<listbox->first_item)
191 listbox->current_item = listbox->first_item;
193 if (listbox->current_item>(listbox->first_item+listbox->num_items_displayed-1))
194 listbox->current_item = listbox->first_item + listbox->num_items_displayed-1;
199 listbox->dragging = 0;
201 if (B1_PRESSED && OnMe )
202 listbox->dragging = 1;
204 if ( CurWindow->keyboard_focus_gadget==(UI_GADGET *)listbox )
206 if (keypress==KEY_ENTER) {
207 listbox->selected_item = listbox->current_item;
215 listbox->current_item--;
219 listbox->current_item++;
223 listbox->current_item=0;
227 listbox->current_item=listbox->num_items-1;
231 listbox->current_item -= listbox->num_items_displayed;
235 listbox->current_item += listbox->num_items_displayed;
244 if (listbox->current_item<0)
245 listbox->current_item=0;
247 if (listbox->current_item>=listbox->num_items)
248 listbox->current_item = listbox->num_items-1;
250 if (listbox->current_item<listbox->first_item)
251 listbox->first_item = listbox->current_item;
253 if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
254 listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
256 if (listbox->num_items <= listbox->num_items_displayed )
257 listbox->first_item = 0;
260 oldfakepos = listbox->scrollbar->position;
261 listbox->scrollbar->position = listbox->first_item;
263 listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
264 listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
265 listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
267 if (listbox->scrollbar->fake_position<0)
269 listbox->scrollbar->fake_position = 0;
271 if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
273 listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
276 if (oldfakepos != listbox->scrollbar->position )
277 listbox->scrollbar->status = 1;
283 if (selected_gadget==(UI_GADGET *)listbox)
285 if (B1_PRESSED && listbox->dragging)
287 if (Mouse.y < listbox->y1)
290 mitem = (Mouse.y - listbox->y1)/listbox->textheight;
292 if ((mitem < 0) && (timer_get_fixed_seconds() > listbox->last_scrolled + 1))
294 listbox->current_item--;
295 listbox->last_scrolled = timer_get_fixed_seconds();
299 if ((mitem >= listbox->num_items_displayed) &&
300 (timer_get_fixed_seconds() > listbox->last_scrolled + 1))
302 listbox->current_item++;
303 listbox->last_scrolled = timer_get_fixed_seconds();
307 if ((mitem>=0) && (mitem<listbox->num_items_displayed))
309 listbox->current_item = mitem+listbox->first_item;
313 if (listbox->current_item <0 )
314 listbox->current_item = 0;
316 if (listbox->current_item >= listbox->num_items )
317 listbox->current_item = listbox->num_items-1;
319 if (listbox->current_item<listbox->first_item)
320 listbox->first_item = listbox->current_item;
322 if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
323 listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
325 if (listbox->num_items <= listbox->num_items_displayed )
326 listbox->first_item = 0;
329 oldfakepos = listbox->scrollbar->position;
330 listbox->scrollbar->position = listbox->first_item;
332 listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
333 listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
334 listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
336 if (listbox->scrollbar->fake_position<0)
338 listbox->scrollbar->fake_position = 0;
340 if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
342 listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
345 if (oldfakepos != listbox->scrollbar->position )
346 listbox->scrollbar->status = 1;
351 if (B1_DOUBLE_CLICKED )
353 listbox->selected_item = listbox->current_item;
358 ui_draw_listbox( listbox );
362 void ui_listbox_change( UI_WINDOW * wnd, UI_GADGET_LISTBOX * listbox, short numitems, char * list, int text_width )
365 UI_GADGET_SCROLLBAR * scrollbar;
369 listbox->list = list;
370 listbox->text_width = text_width;
371 listbox->num_items = numitems;
372 listbox->first_item = 0;
373 listbox->current_item = -1;
374 listbox->last_scrolled = timer_get_fixed_seconds();
375 listbox->dragging = 0;
376 listbox->selected_item = -1;
378 listbox->first_item = 0;
379 listbox->current_item = listbox->old_current_item = 0;
382 scrollbar = listbox->scrollbar;
385 stop= numitems - listbox->num_items_displayed;
387 if (stop < start) stop = start;
390 scrollbar->start = start;
391 scrollbar->stop = stop;
392 scrollbar->fake_length = scrollbar->height;
393 scrollbar->fake_position = 0;
395 scrollbar->fake_size = (listbox->num_items_displayed * scrollbar->height)/(stop-start+1+listbox->num_items_displayed);
397 scrollbar->fake_size = scrollbar->height;
399 if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
400 scrollbar->dragging = 0;