Initial commit
[amiga/OpenBoopsi.git] / gadgets / SliderBar / SliderBarDemo.c
1 /*
2 **      $Id: SliderBarDemo.c,v 1.1 1999/02/07 14:24:43 bernie Exp $
3 **
4 **      Copyright (C) 1999 Bernardo Innocenti (<bernardo.innocenti@usa.net>)
5 **      All rights reserved.
6 **
7 **      Use 4 chars wide TABs to read this file
8 **
9 **
10 **      Introduction
11 **      ============
12 **
13 **      This program shows the usage of the `boopsi' "sliderbargclass" gadget.
14 **      It can only be started from the shell (not from Workbench).
15 **
16 **
17 **      Compiling
18 **      =========
19 **
20 **      This project can be compiled with SAS/C 6.58 or better and
21 **      StormC 2.00.23 or better. You get the smaller and faster
22 **      executable with SAS/C. SAS/C will give you a couple of warnings
23 **      on the library bases which are defined static. This warning can't be
24 **      fixed without patching the system headers. Don't worry about it.
25 **
26 **
27 **      History
28 **      =======
29 **
30 **      1.0 (30.1.99)   First release
31 **
32 **
33 **      Known Bugs
34 **      ==========
35 **
36 **              - This code has never been tested on V37.
37 **
38 **
39 **      License
40 **      =======
41 **
42 **      This software is freely distributable as long as the source code,
43 **      documentation and executables are kept together.  Permission is granted
44 **      to release modified versions of this program as long as all existing
45 **      copyright notices are left intact.
46 */
47
48 #define INTUI_V36_NAMES_ONLY
49 #define __USE_SYSBASE
50 #define  CLIB_ALIB_PROTOS_H     /* Avoid including this header file because of
51                                                          * conflicting definitions in BoopsiStubs.h
52                                                          */
53
54 #include <exec/types.h>
55 #include <exec/memory.h>
56 #include <exec/execbase.h>
57
58 #include <dos/dos.h>
59
60 #include <intuition/intuition.h>
61 #include <intuition/intuitionbase.h>
62 #include <intuition/screens.h>
63 #include <intuition/classes.h>
64 #include <intuition/gadgetclass.h>
65 #include <intuition/icclass.h>
66
67 #include <proto/exec.h>
68 #include <proto/intuition.h>
69
70 #include "CompilerSpecific.h"
71 #include "DebugMacros.h"
72 #include "BoopsiStubs.h"
73
74 #include "SliderBarGClass.h"
75
76
77 /* Local function prototypes */
78 LONG SAVEDS                                      main                   (void);
79 static struct SBDHandle         *OpenSBDWin             (UBYTE *pubscreen, ULONG left, ULONG top,
80                                                                                         ULONG width, ULONG height);
81 static void                                      CloseSBDWin    (struct SBDHandle *sbdhandle);
82 static struct Gadget            *CreateGadgets  (struct SBDHandle *sbdhandle);
83 static void                                      DisposeGadgets (struct SBDHandle *sbdhandle);
84 static struct ClassLibrary      *OpenClass              (STRPTR name, ULONG version);
85
86
87
88 /* This structure describes an open SliderBarDemo window */
89
90 struct SBDHandle
91 {
92         struct Window   *Win;
93         struct Screen   *Scr;
94         struct Gadget   *Slider[4];
95 };
96
97
98
99 /* Version tag */
100
101 static UBYTE versiontag[] = "$VER: SlideBarDemo 1.0 (30.1.99) by Bernardo Innocenti";
102
103
104 /* Library bases */
105 static struct ExecBase          *SysBase;
106 static struct IntuitionBase     *IntuitionBase;
107
108 /* `boopsi' class library base */
109 static struct ClassLibrary      *SliderBarBase;
110
111
112
113 LONG SAVEDS _main (void)
114
115 /* Main program entry point.  When linking without startup code, this
116  * must be the first function in the first object module listed on the
117  * linker command line.  We also need to initialize SysBase and open
118  * all needed libraries manually.
119  */
120 {
121         struct SBDHandle *sbdhandle;
122         LONG                     sigwait, sigrcvd;
123         LONG                     retval = RETURN_FAIL;
124         BOOL                     quit   = FALSE;
125
126
127         /* Initialize SysBase */
128         SysBase = *((struct ExecBase **)4UL);
129
130         if (IntuitionBase = (struct IntuitionBase *)OpenLibrary ("intuition.library", 39L))
131         {
132                 if (SliderBarBase = OpenClass ("sliderbar.gadget", 0))
133                 {
134                         if (sbdhandle = OpenSBDWin (NULL, 0, 20, 320, 200))
135                         {
136                                 /* Pre-calculate the signal mask for Wait() */
137                                 sigwait = (1 << sbdhandle->Win->UserPort->mp_SigBit) |
138                                         SIGBREAKF_CTRL_C;
139
140                                 /* Now for the main loop.  As you can see, it is really
141                                  * very compact. That's the magic of boopsi! :-)
142                                  */
143                                 while (!quit)
144                                 {
145                                         /* Sleep until something interesting occurs */
146                                         sigrcvd = Wait (sigwait);
147
148                                         /* Now handle received signals */
149
150                                         /* Break signal? */
151                                         if (sigrcvd & SIGBREAKF_CTRL_C)
152                                                 quit = TRUE;
153
154                                         /* IDCMP message? */
155                                         if (sigrcvd & (1 << sbdhandle->Win->UserPort->mp_SigBit))
156                                         {
157                                                 struct IntuiMessage     *msg;
158
159                                                 while (msg = (struct IntuiMessage *) GetMsg (sbdhandle->Win->UserPort))
160                                                 {
161                                                         switch (msg->Class)
162                                                         {
163                                                                 case IDCMP_CLOSEWINDOW:
164                                                                         quit = TRUE;
165                                                                         break;
166
167                                                                 default:
168                                                                         break;
169                                                         }
170                                                         ReplyMsg ((struct Message *) msg);
171                                                 }
172                                         }
173                                 } /* End while (!quit) */
174
175                                 retval = RETURN_OK;
176
177                                 CloseSBDWin (sbdhandle);
178                         }
179
180                         /* This cannot fail. Passing NULL is ok. */
181                         CloseLibrary ((struct Library *)SliderBarBase);
182                 }
183                 CloseLibrary ((struct Library *)IntuitionBase);
184         }
185
186         return retval;
187 }
188
189
190
191 static struct SBDHandle *OpenSBDWin (UBYTE *pubscreen,
192         ULONG left, ULONG top, ULONG width, ULONG height)
193 {
194         struct SBDHandle        *sbdhandle;
195         struct Gadget           *glist;
196
197         if (sbdhandle = AllocMem (sizeof (struct SBDHandle), MEMF_ANY | MEMF_CLEAR))
198         {
199                 if (sbdhandle->Scr = LockPubScreen (pubscreen))
200                 {
201                         if (glist = CreateGadgets (sbdhandle))
202                         {
203                                 if (sbdhandle->Win = OpenWindowTags (NULL,
204                                         WA_Top,                         top,
205                                         WA_Left,                        left,
206                                         WA_InnerWidth,          width,
207                                         WA_InnerHeight,         height,
208                                         WA_PubScreen,           sbdhandle->Scr,
209                                         WA_PubScreenFallBack, TRUE,
210                                         WA_AutoAdjust,          TRUE,
211                                         WA_IDCMP,                       IDCMP_CLOSEWINDOW,
212                                         WA_Flags,                       WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
213                                                                                 WFLG_SIZEGADGET | WFLG_SIMPLE_REFRESH | WFLG_NOCAREREFRESH,
214                                         WA_Gadgets,                     glist,
215                                         WA_Title,                       versiontag + 6,
216                                         WA_MinWidth,            64,
217                                         WA_MinHeight,           64,
218                                         WA_MaxWidth,            -1,
219                                         WA_MaxHeight,           -1,
220                                         TAG_DONE))
221                                 {
222                                         UnlockPubScreen (NULL, sbdhandle->Scr);
223                                         return sbdhandle;
224                                 }
225                                 DisposeGadgets (sbdhandle);
226                         }
227                         UnlockPubScreen (NULL, sbdhandle->Scr);
228                 }
229                 FreeMem (sbdhandle, sizeof (struct SBDHandle));
230         }
231         return NULL;
232 }
233
234
235
236 static void CloseSBDWin (struct SBDHandle *sbdhandle)
237 {
238         /* Close our window. No need to reply queued messages,
239          * Intuition is clever enough to care about this for us.
240          */
241         CloseWindow (sbdhandle->Win);
242         DisposeGadgets (sbdhandle);
243         FreeMem (sbdhandle, sizeof (struct SBDHandle));
244 }
245
246 #define SLIDER_THICKNESS 20
247
248 static struct Gadget *CreateGadgets (struct SBDHandle *sbdhandle)
249 {
250         struct Screen   *scr = sbdhandle->Scr;
251         LONG left,right, top, bottom, width, height;
252
253
254         left    = scr->WBorLeft + 10;
255         right   = - scr->WBorRight - 25 - SLIDER_THICKNESS;
256         top             = scr->WBorTop + scr->Font->ta_YSize + 10;
257         bottom  = - scr->WBorBottom - 10 - SLIDER_THICKNESS;
258         width   = right - left - 1;
259         height  = bottom - top - 1;
260
261         sbdhandle->Slider[0] = NewObject (NULL, SLIDERBARGCLASS,
262                 GA_Left,                left,
263                 GA_Top,                 top,
264                 GA_Width,               SLIDER_THICKNESS,
265                 GA_RelHeight,   height,
266                 PGA_Total,              120,
267                 PGA_Top,                50,
268                 PGA_Visible,    20,
269                 LAYOUTA_Orientation, LORIENT_VERT,
270                 TAG_DONE);
271
272         sbdhandle->Slider[1] = NewObject (NULL, SLIDERBARGCLASS,
273                 GA_Previous,    sbdhandle->Slider[0],
274                 GA_Left,                left,
275                 GA_RelBottom,   bottom,
276                 GA_RelWidth,    width + SLIDER_THICKNESS,
277                 GA_Height,              SLIDER_THICKNESS,
278                 PGA_Total,              140,
279                 PGA_Top,                50,
280                 PGA_Visible,    40,
281                 LAYOUTA_Orientation, LORIENT_HORIZ,
282                 ICA_TARGET,             sbdhandle->Slider[0],
283                 TAG_DONE);
284
285         sbdhandle->Slider[2] = NewObject (NULL, SLIDERBARGCLASS,
286                 GA_Previous,    sbdhandle->Slider[1],
287                 GA_RelRight,    right,
288                 GA_Top,                 top,
289                 GA_Width,               SLIDER_THICKNESS,
290                 GA_RelHeight,   height,
291                 PGA_Total,              140,
292                 PGA_Top,                50,
293                 PGA_Visible,    40,
294                 LAYOUTA_Orientation, LORIENT_VERT,
295                 ICA_TARGET,             sbdhandle->Slider[1],
296                 TAG_DONE);
297
298         sbdhandle->Slider[3] = NewObject (NULL, SLIDERBARGCLASS,
299                 GA_Previous,    sbdhandle->Slider[2],
300                 GA_Top,                 top,
301                 GA_Left,                left + SLIDER_THICKNESS,
302                 GA_RelWidth,    width - SLIDER_THICKNESS,
303                 GA_Height,              SLIDER_THICKNESS,
304                 PGA_Total,              140,
305                 PGA_Top,                50,
306                 PGA_Visible,    40,
307                 LAYOUTA_Orientation, LORIENT_HORIZ,
308                 ICA_TARGET,             sbdhandle->Slider[2],
309                 TAG_DONE);
310
311
312         if (sbdhandle->Slider[0] &&
313                 sbdhandle->Slider[1] &&
314                 sbdhandle->Slider[2] &&
315                 sbdhandle->Slider[3])
316         {
317                 return sbdhandle->Slider[0];
318         }
319         else
320         {
321                 DisposeGadgets (sbdhandle);
322                 return NULL;
323         }
324 }
325
326
327
328 static void DisposeGadgets (struct SBDHandle *sbdhandle)
329 {
330         int i;
331
332         for (i = 0; i < 4; i++)
333                 DisposeObject (sbdhandle->Slider[i]);
334 }
335
336
337
338 static struct ClassLibrary *OpenClass (STRPTR name, ULONG version)
339
340 /* Open named class. Look both in current and gadgets/ directory
341  */
342 {
343         static struct EasyStruct OpenClassES =
344         {
345                 sizeof (struct EasyStruct),
346                 0,
347                 versiontag + 6,
348                 "Couldn't open %s version %ld or greater",
349                 "Ok"
350         };
351         static const char prefix[] = "gadgets/";
352
353         struct ClassLibrary *classbase;
354         char buf[256];
355         int i;
356
357
358         if (!(classbase = (struct ClassLibrary *)OpenLibrary (name, version)))
359         {
360                 /* We can't use AddPart() here because we didn't open dos.library */
361
362                 /* Copy the prefix in the buffer */
363                 for (i = 0; buf[i] = prefix[i]; i++);
364
365                 /* Append the name */
366                 while (buf[i++] = *name++);
367
368                 /* Try again */
369                 classbase = (struct ClassLibrary *)OpenLibrary (buf, version);
370
371                 if (!classbase)
372                 {
373                         /* Report an error */
374                         EasyRequest (NULL, &OpenClassES, NULL, buf, version);
375                 }
376         }
377         return classbase;
378 }