added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / demos / dto.c
blob821105f1fb004f86600ec919d112bf2bcff11804
1 /******************************************************************************
3 * COPYRIGHT: Unless otherwise noted, all files are Copyright (c) 1992-1999
4 * Amiga, Inc. All rights reserved.
6 * DISCLAIMER: This software is provided "as is". No representations or
7 * warranties are made with respect to the accuracy, reliability, performance,
8 * currentness, or operation of this software, and all use is at your own risk.
9 * Neither Amiga nor the authors assume any responsibility or liability
10 * whatsoever with respect to your use of this software.
12 ******************************************************************************
13 * dto.c
14 * How to embed a DataType object within an Intuition window
15 * Written by David N. Junod
19 #include <exec/types.h>
20 #include <exec/memory.h>
21 #include <exec/libraries.h>
22 #include <dos/dos.h>
23 #include <dos/rdargs.h>
24 #include <datatypes/datatypes.h>
25 #include <datatypes/datatypesclass.h>
26 #include <datatypes/pictureclass.h>
27 #include <intuition/intuition.h>
28 #include <intuition/icclass.h>
29 #include <graphics/gfx.h>
30 #include <graphics/displayinfo.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
35 #include <proto/alib.h>
36 #include <proto/datatypes.h>
37 #include <proto/dos.h>
38 #include <proto/exec.h>
39 #include <proto/graphics.h>
40 #include <proto/intuition.h>
41 #include <proto/utility.h>
43 /*****************************************************************************/
46 struct IntuitionBase *IntuitionBase;
47 struct GfxBase *GfxBase;
48 struct UtilityBase *UtilityBase;
49 struct Library *DataTypesBase;
51 /*****************************************************************************/
53 #define TEMPLATE "NAME/A"
54 #define OPT_NAME 0
55 #define NUM_OPTS 1
57 /*****************************************************************************/
59 #define IDCMP_FLAGS IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY | IDCMP_IDCMPUPDATE
61 /*****************************************************************************/
63 void PrintErrorMsg (ULONG errnum, STRPTR name)
65 UBYTE errbuff[80];
67 if (errnum >= DTERROR_UNKNOWN_DATATYPE)
68 sprintf (errbuff, GetDTString (errnum), name);
69 else
70 Fault (errnum, NULL, errbuff, sizeof (errbuff));
72 printf ("%s\nerror #%ld\n", errbuff, errnum);
75 /*****************************************************************************/
77 int main (int argc, char **argv)
79 Object *dto;
80 STRPTR name;
82 ULONG modeid = INVALID_ID;
83 ULONG nomwidth, nomheight;
84 BOOL useScreen = FALSE;
85 struct dtFrameBox dtf;
86 struct FrameInfo fri;
87 struct Screen *scr;
88 struct Window *win;
89 BOOL going = TRUE;
91 struct IntuiMessage *imsg;
92 ULONG sigr;
94 const struct TagItem *tstate;
95 struct TagItem *tag;
96 struct TagItem *tags;
97 ULONG tidata;
98 ULONG errnum;
100 ULONG options[NUM_OPTS];
101 struct RDArgs *rdargs;
103 /* Parse the arguments. Note that this simple example assumes
104 * that it was started from the shell. */
105 memset (options, 0, sizeof (options));
106 if ((rdargs = ReadArgs (TEMPLATE, (LONG *)options, NULL)))
108 /* Open DataTypes */
109 if ((DataTypesBase = OpenLibrary ("datatypes.library", 39)))
111 /* Open the other libraries */
112 IntuitionBase = (struct IntuitionBase *)OpenLibrary ("intuition.library", 39);
113 GfxBase = (struct GfxBase *)OpenLibrary ("graphics.library", 39);
114 UtilityBase = (struct UtilityBase *)OpenLibrary ("utility.library", 39);
116 /* Get a DataType object */
117 if ((dto = NewDTObject ((APTR) options[OPT_NAME], TAG_DONE, TAG_DONE)))
119 /* Get information about the object */
120 if (GetDTAttrs (dto,
121 /* Get the name of the object */
122 DTA_ObjName, (IPTR) &name,
124 /* Get the desired size */
125 DTA_NominalHoriz, (IPTR) &nomwidth,
126 DTA_NominalVert, (IPTR) &nomheight,
128 /* Get the mode ID for graphical objects */
129 PDTA_ModeID, (IPTR) &modeid,
130 TAG_DONE))
133 /* Display any information we obtained */
134 if (name)
135 printf ("opened \"%s\"\n", name);
137 /* Show the mode ID */
138 printf ("mode ID %08lx\n", modeid);
140 /* Display the nominal size */
141 printf ("nominal width %ld, height %ld\n", nomwidth, nomheight);
144 /* Ask the object what kind of environment it needs */
145 memset (&dtf, 0, sizeof (struct dtFrameBox));
146 memset (&fri, 0, sizeof (struct FrameInfo));
147 dtf.MethodID = DTM_FRAMEBOX;
148 dtf.dtf_FrameInfo = &fri;
149 dtf.dtf_ContentsInfo = &fri;
150 dtf.dtf_SizeFrameInfo = sizeof (struct FrameInfo);
152 if (DoDTMethodA (dto, NULL, NULL, (Msg) &dtf) && fri.fri_Dimensions.Depth)
154 printf ("PropertyFlags : 0x%lx\n", fri.fri_PropertyFlags);
155 printf ("RedBits : 0x%x\n", fri.fri_RedBits);
156 printf ("GreenBits : 0x%x\n", fri.fri_GreenBits);
157 printf ("BlueBits : 0x%x\n", fri.fri_BlueBits);
158 printf ("Width : %ld\n", (ULONG) fri.fri_Dimensions.Width);
159 printf ("Height : %ld\n", (ULONG) fri.fri_Dimensions.Height);
160 printf ("Depth : %ld\n", (ULONG) fri.fri_Dimensions.Depth);
161 printf ("Screen : 0x%p\n", fri.fri_Screen);
162 printf ("ColorMap : 0x%p\n", fri.fri_ColorMap);
164 if ((fri.fri_PropertyFlags & DIPF_IS_HAM) ||
165 (fri.fri_PropertyFlags & DIPF_IS_EXTRAHALFBRITE))
167 printf ("HAM or ExtraHalfBrite\n");
168 useScreen = TRUE;
171 if ((fri.fri_PropertyFlags == 0) && (modeid & 0x800) && (modeid != INVALID_ID))
173 printf ("ModeID=0x%08lx\n", modeid);
174 useScreen = TRUE;
177 else
178 printf ("couldn't obtain environment information\n");
180 if (useScreen)
182 printf ("this object requires a private screen\n");
184 /* Get a lock on the default public screen */
185 else if ((scr = LockPubScreen (NULL)))
187 /* Make sure we have the dimensions */
188 nomwidth = ((nomwidth) ? nomwidth : 600);
189 nomheight = ((nomheight) ? nomheight : 175);
191 /* Open the window */
193 if ((win = OpenWindowTags (NULL,
194 WA_InnerWidth, nomwidth,
195 WA_InnerHeight, nomheight,
196 WA_Title, (IPTR) name,
197 WA_IDCMP, IDCMP_FLAGS,
198 WA_DragBar, TRUE,
199 WA_DepthGadget, TRUE,
200 WA_CloseGadget, TRUE,
201 WA_AutoAdjust, TRUE,
202 WA_SimpleRefresh, TRUE,
203 WA_BusyPointer, TRUE,
204 WA_Activate, TRUE,
205 WA_SizeGadget, TRUE,
206 WA_SizeBBottom, TRUE,
207 WA_MinWidth, 50,
208 WA_MinHeight, 50,
209 WA_MaxWidth, 10000,
210 WA_MaxHeight, 10000,
211 TAG_DONE)))
215 /* Set the dimensions of the DataType object. */
216 SetDTAttrs (dto, NULL, NULL,
217 GA_Left, win->BorderLeft,
218 GA_Top, win->BorderTop,
219 GA_RelWidth, - win->BorderLeft - win->BorderRight,
220 GA_RelHeight, - win->BorderTop - win->BorderBottom,
221 ICA_TARGET, ICTARGET_IDCMP,
222 TAG_DONE);
224 /* Add the object to the window */
225 AddDTObject (win, NULL, dto, -1);
227 /* Refresh the DataType object */
228 RefreshDTObjectA (dto, win, NULL, NULL);
230 /* Keep going until we're told to stop */
231 while (going)
233 /* Wait for an event */
234 sigr = Wait ((1L << win->UserPort->mp_SigBit) | SIGBREAKF_CTRL_C);
236 /* Did we get a break signal */
237 if (sigr & SIGBREAKF_CTRL_C)
238 going = FALSE;
240 /* Pull Intuition messages */
241 while ((imsg = (struct IntuiMessage *) GetMsg (win->UserPort)))
243 /* Handle each message */
244 switch (imsg->Class)
246 case IDCMP_CLOSEWINDOW:
247 going = FALSE;
248 break;
250 case IDCMP_VANILLAKEY:
251 switch (imsg->Code)
253 case 'Q':
254 case 'q':
255 case 27:
256 going = FALSE;
257 break;
259 break;
261 case IDCMP_IDCMPUPDATE:
262 tstate = tags = (struct TagItem *) imsg->IAddress;
263 while ((tag = NextTagItem(&tstate)))
265 tidata = tag->ti_Data;
266 switch (tag->ti_Tag)
268 /* Change in busy state */
269 case DTA_Busy:
270 if (tidata)
271 SetWindowPointer (win, WA_BusyPointer, TRUE, TAG_DONE);
272 else
273 SetWindowPointer (win, WA_Pointer, NULL, TAG_DONE);
274 break;
276 /* Error message */
277 case DTA_ErrorLevel:
278 if (tidata)
280 errnum = GetTagData (DTA_ErrorNumber, 0, tags);
281 PrintErrorMsg (errnum, (STRPTR) options[OPT_NAME]);
283 break;
285 /* Time to refresh */
286 case DTA_Sync:
287 /* Refresh the DataType object */
288 RefreshDTObjectA (dto, win, NULL, NULL);
289 break;
292 break;
295 /* Done with the message, so reply to it */
296 ReplyMsg ((struct Message *) imsg);
300 /* Remove the object from the window */
301 RemoveDTObject (win, dto);
303 /* Close the window now */
304 CloseWindow (win);
306 else
307 printf ("couldn't open window\n");
309 /* Unlock the screen */
310 UnlockPubScreen (NULL, scr);
312 else
313 printf ("couldn't lock default public screen\n");
315 /* Dispose of the DataType object */
316 DisposeDTObject (dto);
318 else
319 PrintErrorMsg (IoErr (), (STRPTR) options[OPT_NAME]);
321 /* Close the libraries */
322 CloseLibrary ((struct Library *)UtilityBase);
323 CloseLibrary ((struct Library *)GfxBase);
324 CloseLibrary ((struct Library *)IntuitionBase);
325 CloseLibrary (DataTypesBase);
327 else
328 printf ("couldn't open datatypes.library\n");
330 FreeArgs (rdargs);
332 else
333 PrintErrorMsg (IoErr (), NULL);
335 return 0;