LISTBOX.C 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  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-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. /*
  14. * $Source: f:/miner/source/ui/rcs/listbox.c $
  15. * $Revision: 1.5 $
  16. * $Author: john $
  17. * $Date: 1994/11/18 23:07:32 $
  18. *
  19. * Routines for doing listbox gadgets.
  20. *
  21. * $Log: listbox.c $
  22. * Revision 1.5 1994/11/18 23:07:32 john
  23. * Changed a bunch of shorts to ints.
  24. *
  25. * Revision 1.4 1993/12/07 12:31:02 john
  26. * new version.
  27. *
  28. * Revision 1.3 1993/10/26 13:46:16 john
  29. * *** empty log message ***
  30. *
  31. * Revision 1.2 1993/10/05 17:30:15 john
  32. * *** empty log message ***
  33. *
  34. * Revision 1.1 1993/09/20 10:34:45 john
  35. * Initial revision
  36. *
  37. *
  38. */
  39. #pragma off (unreferenced)
  40. static char rcsid[] = "$Id: listbox.c 1.5 1994/11/18 23:07:32 john Exp $";
  41. #pragma on (unreferenced)
  42. #include <stdlib.h>
  43. #include "fix.h"
  44. #include "types.h"
  45. #include "gr.h"
  46. #include "ui.h"
  47. #include "key.h"
  48. #define TICKER (*(volatile int *)0x46C)
  49. void ui_draw_listbox( UI_GADGET_LISTBOX * listbox )
  50. {
  51. int i, x, y, stop;
  52. int w, h, aw;
  53. //if (listbox->current_item<0)
  54. // listbox->current_item=0;
  55. //if (listbox->current_item>=listbox->num_items)
  56. // listbox->current_item = listbox->num_items-1;
  57. //if (listbox->first_item<0)
  58. // listbox->first_item=0;
  59. //if (listbox->first_item>(listbox->num_items-listbox->num_items_displayed))
  60. // listbox->first_item=(listbox->num_items-listbox->num_items_displayed);
  61. if ((listbox->status!=1) && !listbox->moved )
  62. return;
  63. stop = listbox->first_item+listbox->num_items_displayed;
  64. if (stop>listbox->num_items) stop = listbox->num_items;
  65. listbox->status = 0;
  66. x = y = 0;
  67. ui_mouse_hide();
  68. gr_set_current_canvas( listbox->canvas );
  69. for (i= listbox->first_item; i< stop; i++ )
  70. {
  71. if (i !=listbox->current_item)
  72. {
  73. if ((listbox->current_item == -1) && (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox) && (i == listbox->first_item) )
  74. gr_set_fontcolor( CRED, CBLACK );
  75. else
  76. gr_set_fontcolor( CWHITE, CBLACK );
  77. }
  78. else
  79. {
  80. if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
  81. gr_set_fontcolor( CRED, CGREY );
  82. else
  83. gr_set_fontcolor( CBLACK, CGREY );
  84. }
  85. gr_string( x+2, y, listbox->list+i*listbox->text_width );
  86. gr_get_string_size(listbox->list+i*listbox->text_width, &w, &h,&aw );
  87. if (i==listbox->current_item)
  88. gr_setcolor( CGREY );
  89. else
  90. gr_setcolor( CBLACK );
  91. gr_rect( x+w+2, y, listbox->width-1, y+h-1 );
  92. gr_rect( x, y, x+1, y+h-1 );
  93. y += h;
  94. }
  95. if (stop < listbox->num_items_displayed-1 )
  96. {
  97. gr_setcolor(CBLACK);
  98. gr_rect( x, y, listbox->width-1, listbox->height-1 );
  99. }
  100. //gr_ubox( -1, -1, listbox->width, listbox->height);
  101. ui_mouse_show();
  102. }
  103. void gr_draw_sunken_border( short x1, short y1, short x2, short y2 )
  104. {
  105. gr_setcolor( CGREY );
  106. Hline( x1-1, x2+1, y1-1);
  107. 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);
  111. }
  112. 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. {
  114. int tw, th, taw, i;
  115. UI_GADGET_LISTBOX * listbox;
  116. gr_get_string_size("*", &tw, &th, &taw );
  117. i = h / th;
  118. h = i * th;
  119. listbox = (UI_GADGET_LISTBOX *)ui_gadget_add( wnd, 2, x, y, x+w-1, y+h-1 );
  120. listbox->list = list;
  121. listbox->text_width = text_width;
  122. listbox->width = w;
  123. listbox->height = h;
  124. listbox->num_items = numitems;
  125. listbox->num_items_displayed = i;
  126. listbox->first_item = 0;
  127. listbox->current_item = -1;
  128. listbox->last_scrolled = 0;
  129. listbox->textheight = th;
  130. listbox->dragging = 0;
  131. listbox->selected_item = -1;
  132. listbox->moved = 1;
  133. listbox->scrollbar = ui_add_gadget_scrollbar( wnd, x+w+3, y, 0, h, 0, numitems-i, 0, i );
  134. listbox->scrollbar->parent = (UI_GADGET *)listbox;
  135. gr_set_current_canvas( listbox->canvas );
  136. gr_setcolor(CBLACK);
  137. gr_rect( 0, 0, w-1, h-1);
  138. gr_draw_sunken_border( -2, -2, w+listbox->scrollbar->width+4, h+1);
  139. return listbox;
  140. }
  141. void ui_listbox_do( UI_GADGET_LISTBOX * listbox, int keypress )
  142. {
  143. int OnMe, mitem, oldfakepos, kf;
  144. listbox->selected_item = -1;
  145. listbox->moved = 0;
  146. if (listbox->num_items < 1 ) {
  147. listbox->current_item = -1;
  148. listbox->first_item = 0;
  149. listbox->old_current_item = listbox->current_item;
  150. listbox->old_first_item = listbox->first_item;
  151. ui_draw_listbox( listbox );
  152. if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
  153. {
  154. CurWindow->keyboard_focus_gadget = ui_gadget_get_next((UI_GADGET *)listbox);
  155. }
  156. return;
  157. }
  158. listbox->old_current_item = listbox->current_item;
  159. listbox->old_first_item = listbox->first_item;
  160. OnMe = ui_mouse_on_gadget( (UI_GADGET *)listbox );
  161. if (listbox->scrollbar->moved )
  162. {
  163. listbox->moved = 1;
  164. listbox->first_item = listbox->scrollbar->position;
  165. if (listbox->current_item<listbox->first_item)
  166. listbox->current_item = listbox->first_item;
  167. if (listbox->current_item>(listbox->first_item+listbox->num_items_displayed-1))
  168. listbox->current_item = listbox->first_item + listbox->num_items_displayed-1;
  169. }
  170. if (!B1_PRESSED )
  171. listbox->dragging = 0;
  172. if (B1_PRESSED && OnMe )
  173. listbox->dragging = 1;
  174. if ( CurWindow->keyboard_focus_gadget==(UI_GADGET *)listbox )
  175. {
  176. if (keypress==KEY_ENTER) {
  177. listbox->selected_item = listbox->current_item;
  178. }
  179. kf = 0;
  180. switch(keypress)
  181. {
  182. case (KEY_UP):
  183. listbox->current_item--;
  184. kf = 1;
  185. break;
  186. case (KEY_DOWN):
  187. listbox->current_item++;
  188. kf = 1;
  189. break;
  190. case (KEY_HOME):
  191. listbox->current_item=0;
  192. kf = 1;
  193. break;
  194. case (KEY_END):
  195. listbox->current_item=listbox->num_items-1;
  196. kf = 1;
  197. break;
  198. case (KEY_PAGEUP):
  199. listbox->current_item -= listbox->num_items_displayed;
  200. kf = 1;
  201. break;
  202. case (KEY_PAGEDOWN):
  203. listbox->current_item += listbox->num_items_displayed;
  204. kf = 1;
  205. break;
  206. }
  207. if (kf==1)
  208. {
  209. listbox->moved = 1;
  210. if (listbox->current_item<0)
  211. listbox->current_item=0;
  212. if (listbox->current_item>=listbox->num_items)
  213. listbox->current_item = listbox->num_items-1;
  214. if (listbox->current_item<listbox->first_item)
  215. listbox->first_item = listbox->current_item;
  216. if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
  217. listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
  218. if (listbox->num_items <= listbox->num_items_displayed )
  219. listbox->first_item = 0;
  220. else
  221. {
  222. oldfakepos = listbox->scrollbar->position;
  223. listbox->scrollbar->position = listbox->first_item;
  224. listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
  225. listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
  226. listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
  227. if (listbox->scrollbar->fake_position<0)
  228. {
  229. listbox->scrollbar->fake_position = 0;
  230. }
  231. if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
  232. {
  233. listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
  234. }
  235. if (oldfakepos != listbox->scrollbar->position )
  236. listbox->scrollbar->status = 1;
  237. }
  238. }
  239. }
  240. if (selected_gadget==(UI_GADGET *)listbox)
  241. {
  242. if (B1_PRESSED && listbox->dragging)
  243. {
  244. if (Mouse.y < listbox->y1)
  245. mitem = -1;
  246. else
  247. mitem = (Mouse.y - listbox->y1)/listbox->textheight;
  248. if ( (mitem < 0 ) && ( TICKER > listbox->last_scrolled+1) )
  249. {
  250. listbox->current_item--;
  251. listbox->last_scrolled = TICKER;
  252. listbox->moved = 1;
  253. }
  254. if ( ( mitem >= listbox->num_items_displayed ) &&
  255. ( TICKER > listbox->last_scrolled+1) )
  256. {
  257. listbox->current_item++;
  258. listbox->last_scrolled = TICKER;
  259. listbox->moved = 1;
  260. }
  261. if ((mitem>=0) && (mitem<listbox->num_items_displayed))
  262. {
  263. listbox->current_item = mitem+listbox->first_item;
  264. listbox->moved=1;
  265. }
  266. if (listbox->current_item <0 )
  267. listbox->current_item = 0;
  268. if (listbox->current_item >= listbox->num_items )
  269. listbox->current_item = listbox->num_items-1;
  270. if (listbox->current_item<listbox->first_item)
  271. listbox->first_item = listbox->current_item;
  272. if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
  273. listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
  274. if (listbox->num_items <= listbox->num_items_displayed )
  275. listbox->first_item = 0;
  276. else
  277. {
  278. oldfakepos = listbox->scrollbar->position;
  279. listbox->scrollbar->position = listbox->first_item;
  280. listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
  281. listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
  282. listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
  283. if (listbox->scrollbar->fake_position<0)
  284. {
  285. listbox->scrollbar->fake_position = 0;
  286. }
  287. if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
  288. {
  289. listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
  290. }
  291. if (oldfakepos != listbox->scrollbar->position )
  292. listbox->scrollbar->status = 1;
  293. }
  294. }
  295. if (B1_DOUBLE_CLICKED )
  296. {
  297. listbox->selected_item = listbox->current_item;
  298. }
  299. }
  300. ui_draw_listbox( listbox );
  301. }
  302. void ui_listbox_change( UI_WINDOW * wnd, UI_GADGET_LISTBOX * listbox, short numitems, char * list, int text_width )
  303. {
  304. int stop, start;
  305. UI_GADGET_SCROLLBAR * scrollbar;
  306. wnd = wnd;
  307. listbox->list = list;
  308. listbox->text_width = text_width;
  309. listbox->num_items = numitems;
  310. listbox->first_item = 0;
  311. listbox->current_item = -1;
  312. listbox->last_scrolled = TICKER;
  313. listbox->dragging = 0;
  314. listbox->selected_item = -1;
  315. listbox->status = 1;
  316. listbox->first_item = 0;
  317. listbox->current_item = listbox->old_current_item = 0;
  318. listbox->moved = 0;
  319. scrollbar = listbox->scrollbar;
  320. start=0;
  321. stop= numitems - listbox->num_items_displayed;
  322. if (stop < start) stop = start;
  323. scrollbar->horz = 0;
  324. scrollbar->start = start;
  325. scrollbar->stop = stop;
  326. scrollbar->fake_length = scrollbar->height;
  327. scrollbar->fake_position = 0;
  328. if (stop!=start)
  329. scrollbar->fake_size = (listbox->num_items_displayed * scrollbar->height)/(stop-start+1+listbox->num_items_displayed);
  330. else
  331. scrollbar->fake_size = scrollbar->height;
  332. if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
  333. scrollbar->dragging = 0;
  334. scrollbar->moved=0;
  335. scrollbar->status=1;
  336. }
  337.