INPUTBOX.C 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/inputbox.c $
  15. * $Revision: 1.6 $
  16. * $Author: john $
  17. * $Date: 1994/11/18 23:07:30 $
  18. *
  19. * Routines for doing inputbox gadgets.
  20. *
  21. * $Log: inputbox.c $
  22. * Revision 1.6 1994/11/18 23:07:30 john
  23. * Changed a bunch of shorts to ints.
  24. *
  25. * Revision 1.5 1994/07/22 17:02:17 john
  26. * fixed bug with inputbox not sucking up its keypresses.
  27. *
  28. * Revision 1.4 1993/12/07 12:30:55 john
  29. * new version.
  30. *
  31. * Revision 1.3 1993/10/26 13:45:59 john
  32. * *** empty log message ***
  33. *
  34. * Revision 1.2 1993/10/05 17:30:50 john
  35. * *** empty log message ***
  36. *
  37. * Revision 1.1 1993/09/20 10:35:12 john
  38. * Initial revision
  39. *
  40. *
  41. */
  42. #pragma off (unreferenced)
  43. static char rcsid[] = "$Id: inputbox.c 1.6 1994/11/18 23:07:30 john Exp $";
  44. #pragma on (unreferenced)
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include "mem.h"
  48. #include "fix.h"
  49. #include "types.h"
  50. #include "gr.h"
  51. #include "ui.h"
  52. #include "key.h"
  53. // insert character c into string s at position p.
  54. void strcins(char *s, int p, char c)
  55. {
  56. int n;
  57. for (n=strlen(s)-p; n>=0; n-- )
  58. *(s+p+n+1) = *(s+p+n); // Move everything over
  59. *(s+p) = c; // then insert the character
  60. }
  61. // delete n character from string s starting at position p
  62. void strndel(char *s, int p, int n)
  63. {
  64. for (; (*(s+p) = *(s+p+n)) != '\0'; s++ )
  65. *(s+p+n) = '\0'; // Delete and zero fill
  66. }
  67. void ui_draw_inputbox( UI_GADGET_INPUTBOX * inputbox )
  68. {
  69. int w, h, aw;
  70. if ((inputbox->status==1) || (inputbox->position != inputbox->oldposition))
  71. {
  72. ui_mouse_hide();
  73. gr_set_current_canvas( inputbox->canvas );
  74. if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)inputbox)
  75. {
  76. if (inputbox->first_time)
  77. gr_set_fontcolor( CBLACK, CRED );
  78. else
  79. gr_set_fontcolor( CRED, CBLACK );
  80. }
  81. else
  82. gr_set_fontcolor( CWHITE, CBLACK );
  83. inputbox->status = 0;
  84. gr_string( 2, 2, inputbox->text );
  85. gr_get_string_size(inputbox->text, &w, &h, &aw );
  86. gr_setcolor( CBLACK );
  87. gr_rect( 2+w, 0, inputbox->width-1, inputbox->height-1 );
  88. if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)inputbox && !inputbox->first_time )
  89. {
  90. gr_setcolor(CRED);
  91. Vline( 2,inputbox->height-3, 2+w+1 );
  92. Vline( 2,inputbox->height-3, 2+w+2 );
  93. }
  94. ui_mouse_show();
  95. }
  96. }
  97. UI_GADGET_INPUTBOX * ui_add_gadget_inputbox( UI_WINDOW * wnd, short x, short y, short length, short slength, char * text )
  98. {
  99. int h, w, aw, f;
  100. UI_GADGET_INPUTBOX * inputbox;
  101. gr_get_string_size( NULL, &w, &h, &aw );
  102. inputbox = (UI_GADGET_INPUTBOX *)ui_gadget_add( wnd, 6, x, y, x+aw*slength-1, y+h-1+4 );
  103. f = 0;
  104. inputbox->text = malloc( length + 1);
  105. strncpy( inputbox->text, text, length );
  106. inputbox->position = strlen(inputbox->text);
  107. inputbox->oldposition = inputbox->position;
  108. inputbox->width = aw*slength;
  109. inputbox->height = h+4;
  110. inputbox->length = length;
  111. inputbox->slength = slength;
  112. inputbox->pressed = 0;
  113. inputbox->first_time = 1;
  114. gr_set_current_canvas( inputbox->canvas );
  115. gr_setcolor( CBLACK );
  116. gr_rect( 0, 0, inputbox->width-1, inputbox->height-1 );
  117. return inputbox;
  118. }
  119. void ui_inputbox_do( UI_GADGET_INPUTBOX * inputbox, int keypress )
  120. {
  121. char ascii;
  122. inputbox->oldposition = inputbox->position;
  123. inputbox->pressed=0;
  124. if (CurWindow->keyboard_focus_gadget==(UI_GADGET *)inputbox)
  125. {
  126. switch( keypress )
  127. {
  128. case 0:
  129. break;
  130. case (KEY_LEFT):
  131. case (KEY_BACKSP):
  132. if (inputbox->position > 0)
  133. inputbox->position--;
  134. inputbox->text[inputbox->position] = 0;
  135. inputbox->status = 1;
  136. if (inputbox->first_time) inputbox->first_time = 0;
  137. break;
  138. case (KEY_ENTER):
  139. inputbox->pressed=1;
  140. inputbox->status = 1;
  141. if (inputbox->first_time) inputbox->first_time = 0;
  142. break;
  143. default:
  144. ascii = key_to_ascii(keypress);
  145. if ((ascii < 255 ) && (inputbox->position < inputbox->length-2))
  146. {
  147. if (inputbox->first_time) {
  148. inputbox->first_time = 0;
  149. inputbox->position = 0;
  150. }
  151. inputbox->text[inputbox->position++] = ascii;
  152. inputbox->text[inputbox->position] = 0;
  153. }
  154. inputbox->status = 1;
  155. break;
  156. }
  157. } else {
  158. inputbox->first_time = 1;
  159. }
  160. last_keypress=0;
  161. ui_draw_inputbox( inputbox );
  162. }
  163.