chiark / gitweb /
Initial revision
[ssr] / StraySrc / Libraries / Steel / c / tcol
1 /*
2  * Tcol
3  *   A true-colour dialogue box thingy
4  *
5  * v. 1.00 24 July 1993
6  *
7  * © 1994-1998 Straylight
8  */
9
10 /*----- Licensing note ----------------------------------------------------*
11  *
12  * This file is part of Straylight's Steel library.
13  *
14  * Steel is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * Steel is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with Steel.  If not, write to the Free Software Foundation,
26  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 #include "dbox.h"
30 #include "buttons.h"
31 #include "wimp.h"
32 #include "wimpt.h"
33 #include "werr.h"
34 #include "tcol.h"
35 #include "help.h"
36 #include "msgs.h"
37 #include "mem.h"
38 #include "akbd.h"
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42
43 #define tcol__REDUP 27
44 #define tcol__REDDOWN 2
45 #define tcol__REDWRITE 3
46 #define tcol__REDSLIDE 6
47 #define tcol__GREENUP 9
48 #define tcol__GREENDOWN 10
49 #define tcol__GREENWRITE 11
50 #define tcol__GREENSLIDE 14
51 #define tcol__BLUEUP 17
52 #define tcol__BLUEDOWN 18
53 #define tcol__BLUEWRITE 19
54 #define tcol__BLUESLIDE 22
55 #define tcol__ACTIONOK 0
56 #define tcol__COLOUR 25
57 #define tcol__ACTIONCANCEL 1
58 #define tcol__EDITING 30
59
60 /*
61  * A structure to handle everything
62  */
63 typedef struct
64 {
65   dbox d;
66   tcol_finishhandler proc;
67   void *handle;
68   int red;
69   int green;
70   int blue;
71 }
72 tcol__tcolstr;
73
74 static void tcol__updateColour(tcol__tcolstr *t)
75 {
76   wimp_paletteword pal;
77   pal.bytes.red=t->red*255/100;
78   pal.bytes.green=t->green*255/100;
79   pal.bytes.blue=t->blue*255/100;
80   buttons_updateColourButton(t->d,tcol__COLOUR,pal);
81 }
82
83 static void tcol__updateRed(tcol__tcolstr *t)
84 {
85   dbox_setNumeric(t->d,tcol__REDWRITE,t->red);
86   buttons_updateSlider(t->d,tcol__REDSLIDE,100,t->red,11,FALSE);
87   tcol__updateColour(t);
88 }
89
90 static void tcol__updateGreen(tcol__tcolstr *t)
91 {
92   dbox_setNumeric(t->d,tcol__GREENWRITE,t->green);
93   buttons_updateSlider(t->d,tcol__GREENSLIDE,100,t->green,10,FALSE);
94   tcol__updateColour(t);
95 }
96
97 static void tcol__updateBlue(tcol__tcolstr *t)
98 {
99   dbox_setNumeric(t->d,tcol__BLUEWRITE,t->blue);
100   buttons_updateSlider(t->d,tcol__BLUESLIDE,100,t->blue,8,FALSE);
101   tcol__updateColour(t);
102 }
103
104 static void tcol__redAltered(tcol__tcolstr *t)
105 {
106   buttons_arrowClick(t->d,tcol__REDWRITE,0,100,0,FALSE);
107   t->red=dbox_getNumeric(t->d,tcol__REDWRITE);
108   buttons_updateSlider(t->d,tcol__REDSLIDE,100,t->red,11,FALSE);
109   tcol__updateColour(t);
110 }
111
112 static void tcol__greenAltered(tcol__tcolstr *t)
113 {
114   buttons_arrowClick(t->d,tcol__GREENWRITE,0,100,0,FALSE);
115   t->green=dbox_getNumeric(t->d,tcol__GREENWRITE);
116   buttons_updateSlider(t->d,tcol__GREENSLIDE,100,t->green,10,FALSE);
117   tcol__updateColour(t);
118 }
119
120 static void tcol__blueAltered(tcol__tcolstr *t)
121 {
122   buttons_arrowClick(t->d,tcol__BLUEWRITE,0,100,0,FALSE);
123   t->blue=dbox_getNumeric(t->d,tcol__BLUEWRITE);
124   buttons_updateSlider(t->d,tcol__BLUESLIDE,100,t->blue,8,FALSE);
125   tcol__updateColour(t);
126 }
127
128 static void tcol__redraw(wimp_redrawstr *r,void *handle)
129 {
130   tcol__tcolstr *t=(tcol__tcolstr *)handle;
131   wimp_paletteword pal;
132   r=r;
133   pal.bytes.red=t->red*255/100;
134   pal.bytes.green=t->green*255/100;
135   pal.bytes.blue=t->blue*255/100;
136   buttons_redrawSlider(t->d,tcol__REDSLIDE,100,t->red,11,FALSE);
137   buttons_redrawSlider(t->d,tcol__GREENSLIDE,100,t->green,10,FALSE);
138   buttons_redrawSlider(t->d,tcol__BLUESLIDE,100,t->blue,8,FALSE);
139   buttons_redrawColourButton(t->d,tcol__COLOUR,pal);
140 }
141
142 static BOOL tcol__raw(dbox d,wimp_eventstr *e,void *handle)
143 {
144   tcol__tcolstr *t=(tcol__tcolstr *)handle;
145   BOOL handled=FALSE;
146   BOOL cursor=FALSE;
147   int key;
148   switch (e->e)
149   {
150     case wimp_EREDRAW:
151       wimpt_redraw(tcol__redraw,t);
152       handled=TRUE;
153       break;
154     case wimp_EKEY:
155       key=e->data.key.chcode;
156       switch (key)
157       {
158         case akbd_DownK:
159         case akbd_UpK:
160         case akbd_TabK:
161         case akbd_TabK+akbd_Sh:
162         case akbd_UpK+akbd_Ctl:
163         case akbd_DownK+akbd_Ctl:
164           cursor=TRUE;
165       }
166       switch (e->data.key.c.i)
167       {
168         case tcol__REDWRITE:
169           if (handled=buttons_insertChar(d,key,'0','9'),handled || cursor)
170             tcol__redAltered(t);
171           break;
172         case tcol__GREENWRITE:
173           if (handled=buttons_insertChar(d,key,'0','9'),handled || cursor)
174             tcol__greenAltered(t);
175           break;
176         case tcol__BLUEWRITE:
177           if (handled=buttons_insertChar(d,key,'0','9'),handled || cursor)
178             tcol__blueAltered(t);
179           break;
180       }
181       break;
182   }
183   return (handled);
184 }
185
186 static void tcol__redSlide(dbox d,wimp_i i,int val,void *handle)
187 {
188   tcol__tcolstr *t=(tcol__tcolstr *)handle;
189   i=i;
190   d=d;
191   val=val;
192   dbox_setNumeric(t->d,tcol__REDWRITE,t->red);
193   tcol__updateColour(t);
194 }
195
196 static void tcol__greenSlide(dbox d,wimp_i i,int val,void *handle)
197 {
198   tcol__tcolstr *t=(tcol__tcolstr *)handle;
199   i=i;
200   d=d;
201   val=val;
202   dbox_setNumeric(t->d,tcol__GREENWRITE,t->green);
203   tcol__updateColour(t);
204 }
205 static void tcol__blueSlide(dbox d,wimp_i i,int val,void *handle)
206 {
207   tcol__tcolstr *t=(tcol__tcolstr *)handle;
208   i=i;
209   d=d;
210   val=val;
211   dbox_setNumeric(t->d,tcol__BLUEWRITE,t->blue);
212   tcol__updateColour(t);
213 }
214
215 static void tcol__redArrow(dbox d,dbox_field f,int diff,void *handle)
216 {
217   tcol__tcolstr *t=(tcol__tcolstr *)handle;
218   buttons_arrowClick(d,f,0,100,diff,FALSE);
219   tcol__redAltered(t);
220 }
221
222 static void tcol__greenArrow(dbox d,dbox_field f,int diff,void *handle)
223 {
224   tcol__tcolstr *t=(tcol__tcolstr *)handle;
225   buttons_arrowClick(d,f,0,100,diff,FALSE);
226   tcol__greenAltered(t);
227 }
228
229 static void tcol__blueArrow(dbox d,dbox_field f,int diff,void *handle)
230 {
231   tcol__tcolstr *t=(tcol__tcolstr *)handle;
232   buttons_arrowClick(d,f,0,100,diff,FALSE);
233   tcol__blueAltered(t);
234 }
235
236 static void tcol__handler(dbox d,dbox_field clicked,void *handle)
237 {
238   wimp_paletteword pal;
239   tcol__tcolstr *t=(tcol__tcolstr *)handle;
240   switch (clicked)
241   {
242     case dbox_CLOSE:
243       dbox_delete(d);
244       if (t->proc)
245       {
246         (t->proc)(tcol_CANCEL,
247                   pal,
248                   t->handle);
249       }
250       mem_free(t);
251       break;
252     case dbox_HELP:
253       help_startHelp();
254       help_addLine(msgs_lookup("tcolHM:This is the Colour Selector."));
255       help_readFromIcon();
256       help_endHelp();
257       break;
258     case tcol__COLOUR:
259       break;
260     case tcol__ACTIONOK:
261       if (!dbox_wasAdjustClick())
262       {
263         dbox_clickicon(d,tcol__ACTIONOK);
264         dbox_hide(d);
265         dbox_unclick();
266         dbox_delete(d);
267         mem_free(t);
268       }
269       else
270       {
271         dbox_clickicon(d,tcol__ACTIONOK);
272         dbox_unclick();
273       }
274       if (t->proc)
275       {
276         pal.bytes.red=t->red*255/100;
277         pal.bytes.green=t->green*255/100;
278         pal.bytes.blue=t->blue*255/100;
279         (t->proc)(dbox_wasAdjustClick() ? tcol_OK_REOPEN : tcol_OK,
280                   pal,
281                   t->handle);
282       }
283       break;
284     case tcol__ACTIONCANCEL:
285       dbox_clickicon(d,tcol__ACTIONCANCEL);
286       dbox_hide(d);
287       dbox_unclick();
288       dbox_delete(d);
289       if (t->proc)
290       {
291         (t->proc)(tcol_CANCEL,
292                   pal,
293                   t->handle);
294       }
295       mem_free(t);
296       break;
297     case tcol__REDUP:
298       buttons_arrow(d,clicked,d,tcol__REDWRITE,tcol__redArrow,+1,t);
299       break;
300     case tcol__REDDOWN:
301       buttons_arrow(d,clicked,d,tcol__REDWRITE,tcol__redArrow,-1,t);
302       break;
303     case tcol__GREENUP:
304       buttons_arrow(d,clicked,d,tcol__GREENWRITE,tcol__greenArrow,+1,t);
305       break;
306     case tcol__GREENDOWN:
307       buttons_arrow(d,clicked,d,tcol__GREENWRITE,tcol__greenArrow,-1,t);
308       break;
309     case tcol__BLUEUP:
310       buttons_arrow(d,clicked,d,tcol__BLUEWRITE,tcol__blueArrow,+1,t);
311       break;
312     case tcol__BLUEDOWN:
313       buttons_arrow(d,clicked,d,tcol__BLUEWRITE,tcol__blueArrow,-1,t);
314       break;
315     case tcol__REDSLIDE:
316       buttons_slideSlider(d,tcol__REDSLIDE,100,
317         &(t->red),11,FALSE,tcol__redSlide,t);
318       break;
319     case tcol__GREENSLIDE:
320       buttons_slideSlider(d,tcol__GREENSLIDE,100,
321         &(t->green),10,FALSE,tcol__greenSlide,t);
322       break;
323     case tcol__BLUESLIDE:
324       buttons_slideSlider(d,tcol__BLUESLIDE,100,
325         &(t->blue),8,FALSE,tcol__blueSlide,t);
326       break;
327   }
328 }
329
330 /*
331  * void tcol
332  * (
333  *   char *editing,
334  *   wimp_paletteword c,
335  *   BOOL isStatic,
336  *   tcol_finishhandler proc,
337  *   void *handle
338  *  )
339  *
340  * Use
341  *  Creates and handles a dbox, which allows the user to input a true
342  *  colour.  You must have a template called 'tcol' for this routine.
343  *
344  * Parameters
345  *  char *editing == what we're editing (put in the little box at the top)
346  *  wimp_paletteword c == the initial colour
347  *  BOOL isStatic == whether the dbox is static.  Actually, you can use
348  *   any of the dbox display types (e.g. dbox_STATIC_OVERPTR)
349  *  tcol_finishhandler proc == procedure called when the user clicks OK
350  *  void *handle == the jolly old handle
351  */
352
353 void tcol
354 (
355   char *editing,
356   wimp_paletteword c,
357   BOOL isStatic,
358   tcol_finishhandler proc,
359   void *handle
360 )
361 {
362   tcol__tcolstr *t=(tcol__tcolstr *)mem_alloc(sizeof(tcol__tcolstr));
363   if (!t)
364   {
365     werr(FALSE,msgs_lookup("tcolNEM:Not enough memory to create dbox."));
366     return;
367   }
368   t->d=dbox_create("tcol");
369   if (!t->d)
370     return;
371   dbox_rawEventHandler(t->d,tcol__raw,t);
372   dbox_eventHandler(t->d,tcol__handler,t);
373   dbox_setfield(t->d,tcol__EDITING,"%s",editing);
374   t->red=c.bytes.red*100/255;
375   t->green=c.bytes.green*100/255;
376   t->blue=c.bytes.blue*100/255;
377   t->proc=proc;
378   t->handle=handle;
379   tcol__updateRed(t);
380   tcol__updateGreen(t);
381   tcol__updateBlue(t);
382   dbox_display(t->d,isStatic);
383 }