ncurses2-color_edit.adb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- ncurses --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 2000-2004,2006 Free Software Foundation, Inc. --
  11. -- --
  12. -- Permission is hereby granted, free of charge, to any person obtaining a --
  13. -- copy of this software and associated documentation files (the --
  14. -- "Software"), to deal in the Software without restriction, including --
  15. -- without limitation the rights to use, copy, modify, merge, publish, --
  16. -- distribute, distribute with modifications, sublicense, and/or sell --
  17. -- copies of the Software, and to permit persons to whom the Software is --
  18. -- furnished to do so, subject to the following conditions: --
  19. -- --
  20. -- The above copyright notice and this permission notice shall be included --
  21. -- in all copies or substantial portions of the Software. --
  22. -- --
  23. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
  24. -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
  25. -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
  26. -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
  27. -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
  28. -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
  29. -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
  30. -- --
  31. -- Except as contained in this notice, the name(s) of the above copyright --
  32. -- holders shall not be used in advertising or otherwise to promote the --
  33. -- sale, use or other dealings in this Software without prior written --
  34. -- authorization. --
  35. ------------------------------------------------------------------------------
  36. -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
  37. -- Version Control
  38. -- $Revision: 1.5 $
  39. -- $Date: 2006/06/25 14:24:40 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with ncurses2.util; use ncurses2.util;
  43. with ncurses2.genericPuts;
  44. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  45. procedure ncurses2.color_edit is
  46. use Int_IO;
  47. type RGB_Enum is (Redx, Greenx, Bluex);
  48. procedure change_color (current : Color_Number;
  49. field : RGB_Enum;
  50. value : RGB_Value;
  51. usebase : Boolean);
  52. procedure change_color (current : Color_Number;
  53. field : RGB_Enum;
  54. value : RGB_Value;
  55. usebase : Boolean) is
  56. red, green, blue : RGB_Value;
  57. begin
  58. if usebase then
  59. Color_Content (current, red, green, blue);
  60. else
  61. red := 0;
  62. green := 0;
  63. blue := 0;
  64. end if;
  65. case field is
  66. when Redx => red := red + value;
  67. when Greenx => green := green + value;
  68. when Bluex => blue := blue + value;
  69. end case;
  70. declare
  71. begin
  72. Init_Color (current, red, green, blue);
  73. exception
  74. when Curses_Exception => Beep;
  75. end;
  76. end change_color;
  77. package x is new ncurses2.genericPuts (100); use x;
  78. tmpb : x.BS.Bounded_String;
  79. tmp4 : String (1 .. 4);
  80. tmp6 : String (1 .. 6);
  81. tmp8 : String (1 .. 8);
  82. -- This would be easier if Ada had a Bounded_String
  83. -- defined as a class instead of the inferior generic package,
  84. -- then I could define Put, Add, and Get for them. Blech.
  85. value : RGB_Value := 0;
  86. red, green, blue : RGB_Value;
  87. max_colors : constant Natural := Number_Of_Colors;
  88. current : Color_Number := 0;
  89. field : RGB_Enum := Redx;
  90. this_c : Key_Code := 0;
  91. begin
  92. Refresh;
  93. for i in Color_Number'(0) .. Color_Number (Number_Of_Colors) loop
  94. Init_Pair (Color_Pair (i), White, i);
  95. end loop;
  96. Move_Cursor (Line => Lines - 2, Column => 0);
  97. Add (Str => "Number: ");
  98. myPut (tmpb, Integer (value));
  99. myAdd (Str => tmpb);
  100. loop
  101. Switch_Character_Attribute (On => False,
  102. Attr => (Bold_Character => True,
  103. others => False));
  104. Add (Line => 0, Column => 20, Str => "Color RGB Value Editing");
  105. Switch_Character_Attribute (On => False,
  106. Attr => (Bold_Character => True,
  107. others => False));
  108. for i in Color_Number'(0) .. Color_Number (Number_Of_Colors) loop
  109. Move_Cursor (Line => 2 + Line_Position (i), Column => 0);
  110. if current = i then
  111. Add (Ch => '>');
  112. else
  113. Add (Ch => ' ');
  114. end if;
  115. -- TODO if i <= color_names'Max then
  116. Put (tmp8, Integer (i));
  117. Set_Character_Attributes (Color => Color_Pair (i));
  118. Add (Str => " ");
  119. Set_Character_Attributes;
  120. Refresh;
  121. Color_Content (i, red, green, blue);
  122. Add (Str => " R = ");
  123. if current = i and field = Redx then
  124. Switch_Character_Attribute (On => True,
  125. Attr => (Stand_Out => True,
  126. others => False));
  127. end if;
  128. Put (tmp4, Integer (red));
  129. Add (Str => tmp4);
  130. if current = i and field = Redx then
  131. Set_Character_Attributes;
  132. end if;
  133. Add (Str => " G = ");
  134. if current = i and field = Greenx then
  135. Switch_Character_Attribute (On => True,
  136. Attr => (Stand_Out => True,
  137. others => False));
  138. end if;
  139. Put (tmp4, Integer (green));
  140. Add (Str => tmp4);
  141. if current = i and field = Greenx then
  142. Set_Character_Attributes;
  143. end if;
  144. Add (Str => " B = ");
  145. if current = i and field = Bluex then
  146. Switch_Character_Attribute (On => True,
  147. Attr => (Stand_Out => True,
  148. others => False));
  149. end if;
  150. Put (tmp4, Integer (blue));
  151. Add (Str => tmp4);
  152. if current = i and field = Bluex then
  153. Set_Character_Attributes;
  154. end if;
  155. Set_Character_Attributes;
  156. Add (ch => ')');
  157. end loop;
  158. Add (Line => Line_Position (Number_Of_Colors + 3), Column => 0,
  159. Str => "Use up/down to select a color, left/right to change " &
  160. "fields.");
  161. Add (Line => Line_Position (Number_Of_Colors + 4), Column => 0,
  162. Str => "Modify field by typing nnn=, nnn-, or nnn+. ? for help.");
  163. Move_Cursor (Line => 2 + Line_Position (current), Column => 0);
  164. this_c := Getchar;
  165. if Is_Digit (this_c) then
  166. value := 0;
  167. end if;
  168. case this_c is
  169. when KEY_UP =>
  170. current := (current - 1) mod Color_Number (max_colors);
  171. when KEY_DOWN =>
  172. current := (current + 1) mod Color_Number (max_colors);
  173. when KEY_RIGHT =>
  174. field := RGB_Enum'Val ((RGB_Enum'Pos (field) + 1) mod 3);
  175. when KEY_LEFT =>
  176. field := RGB_Enum'Val ((RGB_Enum'Pos (field) - 1) mod 3);
  177. when
  178. Character'Pos ('0') |
  179. Character'Pos ('1') |
  180. Character'Pos ('2') |
  181. Character'Pos ('3') |
  182. Character'Pos ('4') |
  183. Character'Pos ('5') |
  184. Character'Pos ('6') |
  185. Character'Pos ('7') |
  186. Character'Pos ('8') |
  187. Character'Pos ('9') =>
  188. value := value * 10 + RGB_Value (ctoi (Code_To_Char (this_c)));
  189. when Character'Pos ('+') =>
  190. change_color (current, field, value, True);
  191. when Character'Pos ('-') =>
  192. change_color (current, field, -value, True);
  193. when Character'Pos ('=') =>
  194. change_color (current, field, value, False);
  195. when Character'Pos ('?') =>
  196. Erase;
  197. P (" RGB Value Editing Help");
  198. P ("");
  199. P ("You are in the RGB value editor. Use the arrow keys to " &
  200. "select one of");
  201. P ("the fields in one of the RGB triples of the current colors;" &
  202. " the one");
  203. P ("currently selected will be reverse-video highlighted.");
  204. P ("");
  205. P ("To change a field, enter the digits of the new value; they" &
  206. " are echoed");
  207. P ("as entered. Finish by typing `='. The change will take" &
  208. " effect instantly.");
  209. P ("To increment or decrement a value, use the same procedure," &
  210. " but finish");
  211. P ("with a `+' or `-'.");
  212. P ("");
  213. P ("To quit, do `x' or 'q'");
  214. Pause;
  215. Erase;
  216. when Character'Pos ('q') |
  217. Character'Pos ('x') =>
  218. null;
  219. when others =>
  220. Beep;
  221. end case;
  222. Move_Cursor (Line => Lines - 2, Column => 0);
  223. Put (tmp6, Integer (value));
  224. Add (Str => "Number: " & tmp6);
  225. Clear_To_End_Of_Line;
  226. exit when this_c = Character'Pos ('x') or
  227. this_c = Character'Pos ('q');
  228. end loop;
  229. Erase;
  230. End_Windows;
  231. end ncurses2.color_edit;