BUTTON.C 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/button.c $
  15. * $Revision: 1.10 $
  16. * $Author: john $
  17. * $Date: 1994/11/18 23:07:32 $
  18. *
  19. * Routines for manipulating the button gadgets.
  20. *
  21. * $Log: button.c $
  22. * Revision 1.10 1994/11/18 23:07:32 john
  23. * Changed a bunch of shorts to ints.
  24. *
  25. * Revision 1.9 1994/09/22 18:18:42 john
  26. * Fixed bug with Enter being pressed when a window opens.
  27. *
  28. * Revision 1.8 1994/06/09 12:18:28 john
  29. * Took out keyboard flushes.
  30. *
  31. * Revision 1.7 1994/04/22 11:10:11 john
  32. * *** empty log message ***
  33. *
  34. * Revision 1.6 1993/12/10 14:16:29 john
  35. * made buttons have 2 user-functions.
  36. *
  37. * Revision 1.5 1993/12/07 12:31:18 john
  38. * new version.
  39. *
  40. * Revision 1.4 1993/10/26 13:45:56 john
  41. * *** empty log message ***
  42. *
  43. * Revision 1.3 1993/10/05 17:30:21 john
  44. * *** empty log message ***
  45. *
  46. * Revision 1.2 1993/09/20 10:34:49 john
  47. * *** empty log message ***
  48. *
  49. * Revision 1.1 1993/09/20 10:27:15 john
  50. * Initial revision
  51. *
  52. *
  53. */
  54. #pragma off (unreferenced)
  55. static char rcsid[] = "$Id: button.c 1.10 1994/11/18 23:07:32 john Exp $";
  56. #pragma on (unreferenced)
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include "mem.h"
  60. #include "fix.h"
  61. #include "types.h"
  62. #include "gr.h"
  63. #include "ui.h"
  64. #include "key.h"
  65. #include "mono.h"
  66. #define Middle(x) ((2*(x)+1)/4)
  67. #define BUTTON_EXTRA_WIDTH 15
  68. #define BUTTON_EXTRA_HEIGHT 2
  69. int ui_button_any_drawn = 0;
  70. void ui_get_button_size( char * text, int * width, int * height )
  71. {
  72. int avg;
  73. gr_get_string_size(text, width, height, &avg );
  74. *width += BUTTON_EXTRA_WIDTH*2;
  75. *width += 6;
  76. *height += BUTTON_EXTRA_HEIGHT*2;
  77. *height += 6;
  78. }
  79. void ui_draw_button( UI_GADGET_BUTTON * button )
  80. {
  81. int color;
  82. if ((button->status==1) || (button->position != button->oldposition))
  83. {
  84. ui_button_any_drawn = 1;
  85. ui_mouse_hide();
  86. gr_set_current_canvas( button->canvas );
  87. color = button->canvas->cv_color;
  88. if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)button)
  89. gr_set_fontcolor( CRED, -1 );
  90. else
  91. {
  92. if ((button->user_function==NULL) && button->dim_if_no_function )
  93. gr_set_fontcolor( CGREY, -1 );
  94. else
  95. gr_set_fontcolor( CBLACK, -1 );
  96. }
  97. button->status = 0;
  98. if (button->position == 0 )
  99. {
  100. if (button->text ) {
  101. ui_draw_box_out( 0, 0, button->width-1, button->height-1 );
  102. ui_string_centered( Middle(button->width), Middle(button->height), button->text );
  103. } else {
  104. gr_setcolor( CBLACK );
  105. gr_rect( 0, 0, button->width, button->height );
  106. gr_setcolor( color );
  107. gr_rect( 1, 1, button->width-1, button->height-1 );
  108. }
  109. } else {
  110. if (button->text ) {
  111. ui_draw_box_in( 0, 0, button->width-1, button->height-1 );
  112. ui_string_centered( Middle(button->width)+1, Middle(button->height)+1, button->text );
  113. } else {
  114. gr_setcolor( CBLACK );
  115. gr_rect( 0, 0, button->width, button->height );
  116. gr_setcolor( color );
  117. gr_rect( 2, 2, button->width, button->height );
  118. }
  119. }
  120. button->canvas->cv_color = color;
  121. ui_mouse_show();
  122. }
  123. }
  124. UI_GADGET_BUTTON * ui_add_gadget_button( UI_WINDOW * wnd, short x, short y, short w, short h, char * text, int (*function_to_call)(void) )
  125. {
  126. UI_GADGET_BUTTON * button;
  127. button = (UI_GADGET_BUTTON *)ui_gadget_add( wnd, 1, x, y, x+w-1, y+h-1 );
  128. if ( text )
  129. {
  130. //MALLOC( button->text, char, strlen(text)+1 );//Yet another hack -KRB
  131. button->text = (char *)malloc((strlen(text)+1)*sizeof(char));
  132. strcpy( button->text, text );
  133. } else {
  134. button->text = NULL;
  135. }
  136. button->width = w;
  137. button->height = h;
  138. button->position = 0;
  139. button->oldposition = 0;
  140. button->pressed = 0;
  141. button->user_function = function_to_call;
  142. button->user_function1 = NULL;
  143. button->hotkey1= -1;
  144. button->dim_if_no_function = 0;
  145. return button;
  146. }
  147. void ui_button_do( UI_GADGET_BUTTON * button, int keypress )
  148. {
  149. int result;
  150. int OnMe, ButtonLastSelected;
  151. OnMe = ui_mouse_on_gadget( (UI_GADGET *)button );
  152. button->oldposition = button->position;
  153. if (selected_gadget != NULL)
  154. {
  155. if (selected_gadget->kind==1)
  156. ButtonLastSelected = 1;
  157. else
  158. ButtonLastSelected = 0;
  159. } else
  160. ButtonLastSelected = 1;
  161. if ( B1_PRESSED && OnMe && ButtonLastSelected )
  162. {
  163. button->position = 1;
  164. } else {
  165. button->position = 0;
  166. }
  167. if (keypress == button->hotkey )
  168. {
  169. button->position = 2;
  170. last_keypress = 0;
  171. }
  172. if ((keypress == button->hotkey1) && button->user_function1 )
  173. {
  174. result = button->user_function1();
  175. last_keypress = 0;
  176. }
  177. //if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
  178. // button->position = 2;
  179. if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) && ((keypress==KEY_SPACEBAR) || (keypress==KEY_ENTER)) )
  180. button->position = 2;
  181. if (CurWindow->keyboard_focus_gadget==(UI_GADGET *)button)
  182. if ((button->oldposition==2) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
  183. button->position = 2;
  184. button->pressed = 0;
  185. if (button->position==0) {
  186. if ( (button->oldposition==1) && OnMe )
  187. button->pressed = 1;
  188. if ( (button->oldposition==2) && (CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) )
  189. button->pressed = 1;
  190. }
  191. ui_draw_button( button );
  192. if (button->pressed && button->user_function )
  193. {
  194. result = button->user_function();
  195. }
  196. }
  197.