1 /* $Id: listbox.c,v 1.6 2005-03-05 09:30:26 chris 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.6 2005-03-05 09:30:26 chris 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]);
75 gr_get_string_size(listbox->list[i], &w, &h, &aw);
77 if (i==listbox->current_item)
80 gr_setcolor( CBLACK );
82 if (x + w + 2 < listbox->width - 1)
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)
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;
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)
365 UI_GADGET_SCROLLBAR * scrollbar;
369 listbox->list = list;
370 listbox->num_items = numitems;
371 listbox->first_item = 0;
372 listbox->current_item = -1;
373 listbox->last_scrolled = timer_get_fixed_seconds();
374 listbox->dragging = 0;
375 listbox->selected_item = -1;
377 listbox->first_item = 0;
378 listbox->current_item = listbox->old_current_item = 0;
381 scrollbar = listbox->scrollbar;
384 stop= numitems - listbox->num_items_displayed;
386 if (stop < start) stop = start;
389 scrollbar->start = start;
390 scrollbar->stop = stop;
391 scrollbar->fake_length = scrollbar->height;
392 scrollbar->fake_position = 0;
394 scrollbar->fake_size = (listbox->num_items_displayed * scrollbar->height)/(stop-start+1+listbox->num_items_displayed);
396 scrollbar->fake_size = scrollbar->height;
398 if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
399 scrollbar->dragging = 0;