You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
6.0KB

  1. //
  2. // "$Id: input.cxx 8419 2011-02-13 16:13:04Z greg.ercolano $"
  3. //
  4. // Input field test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. #include <stdio.h>
  28. #include <FL/Fl.H>
  29. #include <FL/Fl_Window.H>
  30. #include <FL/Fl_Input.H>
  31. #include <FL/Fl_Float_Input.H>
  32. #include <FL/Fl_Int_Input.H>
  33. #include <FL/Fl_Secret_Input.H>
  34. #include <FL/Fl_Multiline_Input.H>
  35. #include <FL/Fl_Button.H>
  36. #include <FL/Fl_Toggle_Button.H>
  37. #include <FL/Fl_Light_Button.H>
  38. #include <FL/Fl_Color_Chooser.H>
  39. void cb(Fl_Widget *ob) {
  40. printf("Callback for %s '%s'\n",ob->label(),((Fl_Input*)ob)->value());
  41. }
  42. int when = 0;
  43. Fl_Input *input[5];
  44. void toggle_cb(Fl_Widget *o, long v) {
  45. if (((Fl_Toggle_Button*)o)->value()) when |= v; else when &= ~v;
  46. for (int i=0; i<5; i++) input[i]->when(when);
  47. }
  48. void test(Fl_Input *i) {
  49. if (i->changed()) {
  50. i->clear_changed(); printf("%s '%s'\n",i->label(),i->value());
  51. char utf8buf[10];
  52. int last = fl_utf8encode(i->index(i->position()), utf8buf);
  53. utf8buf[last] = 0;
  54. printf("Symbol at cursor position: %s\n", utf8buf);
  55. }
  56. }
  57. void button_cb(Fl_Widget *,void *) {
  58. for (int i=0; i<5; i++) test(input[i]);
  59. }
  60. void color_cb(Fl_Widget* button, void* v) {
  61. Fl_Color c;
  62. switch ((fl_intptr_t)v) {
  63. case 0: c = FL_BACKGROUND2_COLOR; break;
  64. case 1: c = FL_SELECTION_COLOR; break;
  65. default: c = FL_FOREGROUND_COLOR; break;
  66. }
  67. uchar r,g,b; Fl::get_color(c, r,g,b);
  68. if (fl_color_chooser(0,r,g,b)) {
  69. Fl::set_color(c,r,g,b); Fl::redraw();
  70. button->labelcolor(fl_contrast(FL_BLACK,c));
  71. button->redraw();
  72. }
  73. }
  74. void tabnav_cb(Fl_Widget *w, void *v) {
  75. Fl_Light_Button *b = (Fl_Light_Button*)w;
  76. Fl_Multiline_Input *fmi = (Fl_Multiline_Input*)v;
  77. fmi->tab_nav(b->value() ? 1 : 0);
  78. }
  79. void arrownav_cb(Fl_Widget *w, void *v) {
  80. Fl_Light_Button *b = (Fl_Light_Button*)w;
  81. Fl::option(Fl::OPTION_ARROW_FOCUS, b->value() ? true : false);
  82. }
  83. int main(int argc, char **argv) {
  84. // the following two lines set the correct color scheme, so that
  85. // calling fl_contrast below will return good results
  86. Fl::args(argc, argv);
  87. Fl::get_system_colors();
  88. Fl_Window *window = new Fl_Window(400,420);
  89. int y = 10;
  90. input[0] = new Fl_Input(70,y,300,30,"Normal:"); y += 35;
  91. input[0]->tooltip("Normal input field");
  92. // input[0]->cursor_color(FL_SELECTION_COLOR);
  93. // input[0]->maximum_size(20);
  94. // input[0]->static_value("this is a testgarbage");
  95. input[1] = new Fl_Float_Input(70,y,300,30,"Float:"); y += 35;
  96. input[1]->tooltip("Input field for floating-point number (F1)");
  97. input[1]->shortcut(FL_F+1);
  98. input[2] = new Fl_Int_Input(70,y,300,30,"Int:"); y += 35;
  99. input[2]->tooltip("Input field for integer number (F2)");
  100. input[2]->shortcut(FL_F+2);
  101. input[3] = new Fl_Secret_Input(70,y,300,30,"&Secret:"); y += 35;
  102. input[3]->tooltip("Input field for password (Alt-S)");
  103. input[4] = new Fl_Multiline_Input(70,y,300,100,"&Multiline:"); y += 105;
  104. input[4]->tooltip("Input field for short text with newlines (Alt-M)");
  105. input[4]->wrap(1);
  106. for (int i = 0; i < 4; i++) {
  107. input[i]->when(0); input[i]->callback(cb);
  108. }
  109. int y1 = y;
  110. Fl_Button *b;
  111. b = new Fl_Toggle_Button(10,y,200,25,"FL_WHEN_CHANGED");
  112. b->callback(toggle_cb, FL_WHEN_CHANGED); y += 25;
  113. b->tooltip("Do callback each time the text changes");
  114. b = new Fl_Toggle_Button(10,y,200,25,"FL_WHEN_RELEASE");
  115. b->callback(toggle_cb, FL_WHEN_RELEASE); y += 25;
  116. b->tooltip("Do callback when widget loses focus");
  117. b = new Fl_Toggle_Button(10,y,200,25,"FL_WHEN_ENTER_KEY");
  118. b->callback(toggle_cb, FL_WHEN_ENTER_KEY); y += 25;
  119. b->tooltip("Do callback when user hits Enter key");
  120. b = new Fl_Toggle_Button(10,y,200,25,"FL_WHEN_NOT_CHANGED");
  121. b->callback(toggle_cb, FL_WHEN_NOT_CHANGED); y += 25;
  122. b->tooltip("Do callback even if the text is not changed");
  123. y += 5;
  124. b = new Fl_Button(10,y,200,25,"&print changed()"); y += 25;
  125. b->callback(button_cb);
  126. b->tooltip("Print widgets that have changed() flag set");
  127. b = new Fl_Light_Button(10,y,100,25," Tab Nav");
  128. b->tooltip("Control tab navigation for the multiline input field");
  129. b->callback(tabnav_cb, (void*)input[4]);
  130. b->value(input[4]->tab_nav() ? 1 : 0);
  131. b = new Fl_Light_Button(110,y,100,25," Arrow Nav"); y += 25;
  132. b->tooltip("Control horizontal arrow key focus navigation behavior.\n"
  133. "e.g. Fl::OPTION_ARROW_FOCUS");
  134. b->callback(arrownav_cb);
  135. b->value(input[4]->tab_nav() ? 1 : 0);
  136. b->value(Fl::option(Fl::OPTION_ARROW_FOCUS) ? 1 : 0);
  137. b = new Fl_Button(220,y1,120,25,"color"); y1 += 25;
  138. b->color(input[0]->color()); b->callback(color_cb, (void*)0);
  139. b->tooltip("Color behind the text");
  140. b = new Fl_Button(220,y1,120,25,"selection_color"); y1 += 25;
  141. b->color(input[0]->selection_color()); b->callback(color_cb, (void*)1);
  142. b->labelcolor(fl_contrast(FL_BLACK,b->color()));
  143. b->tooltip("Color behind selected text");
  144. b = new Fl_Button(220,y1,120,25,"textcolor"); y1 += 25;
  145. b->color(input[0]->textcolor()); b->callback(color_cb, (void*)2);
  146. b->labelcolor(fl_contrast(FL_BLACK,b->color()));
  147. b->tooltip("Color of the text");
  148. window->end();
  149. window->show(argc,argv);
  150. return Fl::run();
  151. }
  152. //
  153. // End of "$Id: input.cxx 8419 2011-02-13 16:13:04Z greg.ercolano $".
  154. //