chiark / gitweb /
Initial revision
[ssr] / StraySrc / Glass / !Glass / c / toolbox
1 /*
2  * toolbox.c
3  *
4  * Toolbox handling
5  *
6  * © 1994-1998 Straylight
7  */
8
9 /*----- Licensing note ----------------------------------------------------*
10  *
11  * This file is part of Straylight's Glass.
12  *
13  * Glass is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * Glass is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with Glass.  If not, write to the Free Software Foundation,
25  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 /*
31  * ANSI standard headers
32  */
33
34 /*
35  * Steel headers
36  */
37
38 #define _STDAPP
39 #include "steel/Steel.h"
40
41 #include "steel/sculptrix.h"
42 #include "steel/pointer.h"
43 #include "steel/bbc.h"
44
45 /*
46  * Glass headers
47  */
48
49 #include "gIcons.h"
50
51 #include "glass.h"
52 #include "toolbox.h"
53
54 /*----- Constants ---------------------------------------------------------*/
55
56 /*----- Static global variables -------------------------------------------*/
57
58 static BOOL toolbox__opened;
59 static dbox toolbox__dbox;
60 static dbox_field toolbox__selected=-1;
61 static BOOL toolbox__released;
62 static int toolbox__oldCol;
63
64 /*----- Function prototypes -----------------------------------------------*/
65
66 static void toolbox__idles(void *handle);
67 void toolSupprt_doDrag(wimp_dragstr *d);
68
69 /*----- Low-level routines ------------------------------------------------*/
70
71 /*
72  * void toolbox__deselect(void)
73  *
74  * Use
75  *  Deselects the current tool (if any)
76  */
77
78 static void toolbox__deselect(void)
79 {
80   wimpt_noerr(sculptrix_doSlab(dbox_syshandle(toolbox__dbox),
81                                toolbox__selected,
82                                toolbox__oldCol,
83                                0));
84   win_removeIdleClaimer(toolbox__idles,0);
85   pointer_reset_shape();
86   toolbox__selected=-1;
87 }
88
89 /*
90  * void toolbox__selectTool(dbox_field f)
91  *
92  * Use
93  *  Selects the given tool, and sets up event handlers and things as
94  *  necessary.
95  *
96  * Parameters
97  *  dbox_field f == the field to select
98  */
99
100 static void toolbox__selectTool(dbox_field f)
101 {
102   if (toolbox__selected!=-1)
103   {
104     if (toolbox__selected==f)
105     {
106       toolbox__deselect();
107       return;
108     }
109     else
110     {
111       wimpt_noerr(sculptrix_doSlab(dbox_syshandle(toolbox__dbox),
112                                    toolbox__selected,
113                                    toolbox__oldCol,
114                                    0));
115       win_removeIdleClaimer(toolbox__idles,0);
116     }
117   }
118   wimpt_noerr(sculptrix_doSlab(dbox_syshandle(toolbox__dbox),
119                                f,
120                                sculptrix_slabColour(),
121                                &toolbox__oldCol));
122   win_addIdleClaimer(toolbox__idles,2,0);
123   toolbox__selected=f;
124   toolbox__released=FALSE;
125 }
126
127 /*----- Event handlers ----------------------------------------------------*/
128
129 /*
130  * void toolbox__idles(void *handle)
131  *
132  * Use
133  *  Handles polling while the toolbox is active.
134  *
135  * Parameters
136  *  void *handle == 0
137  */
138
139 static void toolbox__idles(void *handle)
140 {
141   wimp_mousestr m;
142   wimp_wstate s;
143   wimp_dragstr d;
144   int sel;
145   static sprite_id id={"ptr_tbx",0};
146   static int trans[6]={0,0,2,3,4,1};
147   wimp_eventstr e;
148   unused(handle);
149   wimpt_noerr(pointer_set_shape(resspr_area(),&id,0,0));
150   wimpt_noerr(wimp_get_point_info(&m));
151   if (!m.bbits)
152     toolbox__released=TRUE;
153   if ((m.bbits==4 || m.bbits==1) && m.w!=dbox_syshandle(toolbox__dbox)
154                                                         && toolbox__released)
155   {
156     sel=toolbox__selected;
157     toolbox__released=FALSE;
158     if (m.bbits==4)
159       toolbox__deselect();
160     e.e=wimp_EUSERDRAG;
161     wimpt_fake_event(&e);
162     event_process();             /* Stop any drags in rest of program      */
163     switch (sel)
164     {
165       case glass_TBCLOSE:
166         s.o.w=m.w;
167         wimp_sendwmessage(wimp_ECLOSE,(wimp_msgstr *)&s.o,m.w,m.i);
168         break;
169       case glass_TBBACK:
170         wimp_get_wind_state(m.w,&s);
171         s.o.behind=-2;
172         wimp_sendwmessage(wimp_EOPEN,(wimp_msgstr *)&s.o,m.w,m.i);
173         break;
174       default:
175         d.window=m.w;
176         d.type=trans[sel];
177         d.box.x0=d.box.x1=m.x;
178         d.box.y0=d.box.y1=m.y;
179         toolSupprt_doDrag(&d);
180         break;
181     }
182     toolbox__released=FALSE;
183     if (m.bbits==4)
184       toolbox__deselect();
185   }
186   else if (bbc_inkey(-113))
187     toolbox__deselect();
188 }
189
190 /*
191  * void toolbox__events(dbox d,dbox_field f,void *handle)
192  *
193  * Use
194  *  Handles events for the toolbox window.
195  *
196  * Parameters
197  *  dbox d == toolbox__dbox
198  *  dbox_field f == the event to handle (icon handle or special code)
199  *  void *handle == 0
200  */
201
202 static void toolbox__events(dbox d,dbox_field f,void *handle)
203 {
204   unused(d);
205   unused(handle);
206   switch (f)
207   {
208     case dbox_CLOSE:
209       dbox_hide(toolbox__dbox);
210       if (toolbox__selected!=-1)
211         toolbox__deselect();
212       dbox_delete(toolbox__dbox);
213       toolbox__dbox=0;
214       break;
215     case dbox_HELP:
216       help_startHelp();
217       help_addLine(msgs_lookup("tbhTB"));
218       help_readFromIcon();
219       help_endHelp();
220       break;
221     case glass_TBCLOSE:
222     case glass_TBBACK:
223     case glass_TBSIZE:
224     case glass_TBHSCR:
225     case glass_TBVSCR:
226     case glass_TBMOVE:
227       toolbox__selectTool(f);
228       break;
229   }
230 }
231
232 /*----- External routines -------------------------------------------------*/
233
234 /*
235  * BOOL toolbox_toolSelected(void)
236  *
237  * Returns
238  *  TRUE if there is a tool selected.  This is useful for handlers thinking
239  *  of starting drag events.
240  */
241
242 BOOL toolbox_toolSelected(void)
243 {
244   return (toolbox__selected!=-1);
245 }
246
247 /*
248  * void toolbox(void)
249  *
250  * Use
251  *  Opens the toolbox window.
252  */
253
254 void toolbox(void)
255 {
256   wimp_wstate s;
257   int xoff,yoff;
258   if (!toolbox__dbox)            /* Is it open yet?                        */
259   {
260     if (toolbox__dbox=dbox_create("toolbox"),!toolbox__dbox)
261       return;
262     if (!toolbox__opened)        /* Move to top-right corner to start off  */
263     {
264       wimpt_noerr(wimp_get_wind_state(dbox_syshandle(toolbox__dbox),&s));
265       xoff=wimpt_scwidth()-s.o.box.x1;
266       yoff=wimpt_scheight()-s.o.box.y1;
267       s.o.box.x0+=xoff;
268       s.o.box.x1+=xoff;
269       s.o.box.y0+=yoff;
270       s.o.box.y1+=yoff;
271       wimpt_noerr(wimp_open_wind(&s.o));
272       toolbox__opened=TRUE;
273     }
274     dbox_eventHandler(toolbox__dbox,toolbox__events,0);
275   }
276   dbox_display(toolbox__dbox,TRUE);
277 }