revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / datatypes / newdtobjecta.c
blob95201e403b2cb0925a758b475d55f0f84fa8c803
1 /*
2 Copyright © 1995-2015, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
8 #include <proto/intuition.h>
9 #include <proto/utility.h>
10 #include <proto/iffparse.h>
11 #include <proto/dos.h>
12 #include <utility/tagitem.h>
13 #include <intuition/intuition.h>
14 #include "datatypes_intern.h" /* Must be after <intuition/intuition.h> */
16 #include <aros/debug.h>
18 /*****************************************************************************
20 NAME */
21 #include <proto/datatypes.h>
23 AROS_LH2(Object *, NewDTObjectA,
25 /* SYNOPSIS */
26 AROS_LHA(APTR , name , D0),
27 AROS_LHA(struct TagItem *, attrs, A0),
29 /* LOCATION */
30 struct Library *, DataTypesBase, 8, DataTypes)
32 /* FUNCTION
34 Create a data type object from a BOOPSI class.
36 INPUTS
38 name -- name of the data source; generally an existing file name
39 attrs -- pointer to a TagList specifying additional arguments
41 TAGS
43 DTA_SourceType -- The type of source data (defaults to DTST_FILE).
44 If the source is the clipboard the name field
45 contains the numeric clipboard unit.
47 DTA_Handle -- Can be used instead of the 'name' field. If the
48 source is DTST_FILE, ths must be a valid FileHandle;
49 must be a valid IFFHandle if source is DTST_CLIPBOARD.
51 DTA_DataType -- The class of the data. Data is a pointer to a valid
52 DataType; only used when creating a new object that
53 doens't have any source data.
55 DTA_GroupID -- If the object isn't of this type, fail with an IoErr()
56 of ERROR_OBJECT_WRONG_TYPE.
58 GA_Left
59 GA_RelRight
60 GA_Top
61 GA_RelBottom
62 GA_Width
63 GA_RelWidth
64 GA_Height
65 GA_RelHeight -- Specify the position of the object relative to the
66 window.
68 GA_ID -- ID of the object.
70 GA_UserData -- Application specific data for the object.
72 GA_Previous -- Previous object / gadget in the list.
74 RESULT
76 A BOOPSI object. This may be used in different contexts such as a gadget
77 or image. NULL indicates failure -- in that case IoErr() gives more
78 information:
80 ERROR_REQUIRED_ARG_MISSING -- A required attribute wasn't specified.
82 ERROR_BAD_NUMBER -- The group ID specified was invalid.
84 ERROR_OBJECT_WRONG_TYPE -- Object data type doesn't match DTA_GroupID.
86 NOTES
88 This function invokes the method OM_NEW for the specified class.
90 The object should (eventually) be freed by DisposeDTObject() when no
91 longer needed.
93 EXAMPLE
95 BUGS
97 SEE ALSO
99 AddDTObject(), DisposeDTObject(), RemoveDTObject(),
100 intuition.library/NewObjectA()
102 INTERNALS
104 HISTORY
106 *****************************************************************************/
108 AROS_LIBFUNC_INIT
110 ULONG SourceType;
111 struct DataType *DataType;
112 APTR Handle;
113 ULONG GroupID;
114 BPTR lock = BNULL;
115 struct IFFHandle *iff = NULL;
116 Object *dtobj = NULL;
117 UBYTE *BaseName = NULL;
118 LONG error = 0;
120 D(bug("datatypes.library/NewDTObjectA\n"));
122 if(!(SourceType = GetTagData(DTA_SourceType, DTST_FILE, attrs)))
124 D(bug("datatypes.library/NewDTObjectA: Bad DTA_SourceType (or no such tag)\n"));
125 error = ERROR_REQUIRED_ARG_MISSING;
127 else
129 D(bug("datatypes.library/NewDTObjectA: DTA_SourceType okay\n"));
131 DataType = (struct DataType *)GetTagData(DTA_DataType, (IPTR) NULL, attrs);
132 Handle = (APTR)GetTagData(DTA_Handle, (IPTR) NULL, attrs);
133 GroupID = GetTagData(DTA_GroupID, 0, attrs);
134 BaseName = (UBYTE *)GetTagData(DTA_BaseName, (IPTR) NULL, attrs);
136 D(bug("datatypes.library/NewDTObjectA: Got attrs DTA_DataType, DTA_Handle and DTA_GroupID\n"));
138 if((SourceType == DTST_RAM) && GroupID)
140 D(bug("datatypes.library/NewDTObjectA: SourceType is DTST_RAM and GroupID is != 0\n"));
141 switch (GroupID)
143 case GID_ANIMATION: BaseName="animation"; break;
144 case GID_DOCUMENT: BaseName="document"; break;
145 case GID_INSTRUMENT: BaseName="instrument"; break;
146 case GID_MOVIE: BaseName="movie"; break;
147 case GID_MUSIC: BaseName="music"; break;
148 case GID_PICTURE: BaseName="picture"; break;
149 case GID_SOUND: BaseName="sound"; break;
150 case GID_TEXT: BaseName="ascii"; break;
152 default: error = ERROR_BAD_NUMBER; break;
155 else
157 D(bug("datatypes.library/NewDTObjectA: SourceType is *not* DTST_RAM or GroupID is 0\n"));
159 if(Handle != NULL)
161 D(bug("datatypes.library/NewDTObjectA: We have a DTA_Handle. Calling ObtainDataTypeA\n"));
163 DataType = ObtainDataTypeA(SourceType, Handle, attrs);
165 D(bug("datatypes.library/NewDTObjectA: ObtainDataTypeA() returned %x\n", DataType));
168 else
170 D(bug("datatypes.library/NewDTObjectA: DTA_Handle is NULL\n"));
171 if(DataType == NULL)
173 D(bug("datatypes.library/NewDTObjectA: DataType is NULL\n"));
175 switch(SourceType)
177 case DTST_FILE:
178 D(bug("datatypes.library/NewDTObjectA: SourceType is DTST_FILE\n"));
180 if((lock = Lock(name, ACCESS_READ)))
182 D(bug("datatypes.library/NewDTObjectA: Lock(\"%s\") okay\n", name));
183 if((DataType = ObtainDataTypeA(SourceType,
184 (APTR)lock, attrs)))
186 D(bug("datatypes.library/NewDTObjectA: ObtainDataType returned %x\n", DataType));
187 if (GroupID && (DataType->dtn_Header->dth_GroupID != GroupID))
189 D(bug("datatypes.library/NewDTObjectA: Bad GroupID\n"));
191 ReleaseDataType(DataType);
192 DataType = NULL;
193 error = ERROR_OBJECT_WRONG_TYPE;
195 else
196 Handle = (APTR)lock;
199 if(Handle == NULL)
201 UnLock(lock);
202 lock = BNULL;
205 } /* if lock aquired */
206 else
207 error = IoErr();
208 break;
210 case DTST_CLIPBOARD:
211 D(bug("datatypes.library/NewDTObjectA: SourceType = DTST_CLIPBOARD\n"));
213 if(!(iff = AllocIFF()))
214 error = ERROR_NO_FREE_STORE;
215 else
217 D(bug("datatypes.library/NewDTObjectA: AllocIFF okay\n"));
218 if((iff->iff_Stream = (IPTR)OpenClipboard((IPTR)name)))
220 D(bug("datatypes.library/NewDTObjectA: OpenClipBoard okay\n"));
222 InitIFFasClip(iff);
224 if(!OpenIFF(iff, IFFF_READ))
226 D(bug("datatypes.library/NewDTObjectA: OpenIFF okay\n"));
228 if((DataType = ObtainDataTypeA(SourceType,
229 iff, attrs)))
231 D(bug("datatypes.library/NewDTObjectA: ObtainDataType returned %x\n", DataType));
233 if (GroupID && (DataType->dtn_Header->dth_GroupID != GroupID))
235 D(bug("datatypes.library/NewDTObjectA: Bad GroupID\n"));
237 ReleaseDataType(DataType);
238 DataType = NULL;
239 error = ERROR_OBJECT_WRONG_TYPE;
241 else
242 Handle = iff;
244 } /* ObtainDataType okay */
245 if(Handle == NULL)
246 CloseIFF(iff);
248 } /* OpenIFF okay */
250 if(Handle == NULL)
251 CloseClipboard((struct ClipboardHandle*)iff->iff_Stream);
253 } /* OpenClipBoard okay */
255 if(Handle == NULL)
257 FreeIFF(iff);
258 iff = NULL;
261 } /* AllocIFF okay */
263 break;
265 } /* switch(SourceType) */
267 } /* if (DataType == NULL */
269 } /* DTA_Handle == NULL */
271 } /* SourceType != DTST_RAM or GroupID == 0 */
273 if(DataType != NULL)
274 BaseName = DataType->dtn_Header->dth_BaseName;
276 if(BaseName != NULL)
278 UBYTE libname[120];
279 struct Library *DTClassBase;
281 D(bug("datatypes.library/NewDTObjectA: Trying OpenLibrary(datatypes/%s.datatype)\n", BaseName));
282 strcpy(libname, "datatypes/");
283 strcat(libname, BaseName);
284 strcat(libname, ".datatype");
286 if(!(DTClassBase = OpenLibrary(libname, 0)))
287 error = DTERROR_UNKNOWN_DATATYPE;
288 else
290 struct IClass *DTClass;
292 D(bug("datatypes.library/NewDTObjectA: OpenLibrary okay. Now calling ObtainEngine\n"));
294 /* Call ObtainEngine() */
295 if((DTClass = AROS_LVO_CALL0(Class *, struct Library *,
296 DTClassBase, 5,)))
299 struct TagItem Tags[4];
301 D(bug("datatypes.library/NewDTObjectA: ObtainEngine returned %x\n", DTClass));
303 Tags[0].ti_Tag = DTA_Name;
304 Tags[0].ti_Data = (IPTR)name;
305 Tags[1].ti_Tag = DTA_DataType;
306 Tags[1].ti_Data = (IPTR)DataType;
307 Tags[2].ti_Tag = DTA_Handle;
308 Tags[2].ti_Data = (IPTR)Handle;
309 Tags[3].ti_Tag = TAG_MORE;
310 Tags[3].ti_Data = (IPTR)attrs;
312 D(bug("datatypes.library/NewDTObjectA: Calling NewObjectA on obtained engine\n"));
314 dtobj = NewObjectA(DTClass, NULL, Tags);
316 D(bug("datatypes.library/NewDTObjectA: NewObjectA returned %x\n", dtobj));
318 lock = BNULL;
319 iff = NULL;
321 } /* ObtainEngine okay */
323 if(dtobj == NULL)
324 CloseLibrary(DTClassBase);
326 } /* datatype class library could be opened */
328 } /* if (BaseName != NULL) */
330 if(dtobj == NULL)
332 D(bug("datatypes.library/NewDTObjectA: dtobj is NULL. Cleaning up\n"));
334 if(lock != BNULL)
335 UnLock(lock);
337 if(iff != NULL)
339 D(bug("datatypes.library/NewDTObjectA: Calling CloseIFF\n"));
340 CloseIFF(iff);
341 D(bug("datatypes.library/NewDTObjectA: Calling CloseClipboard\n"));
342 CloseClipboard((struct ClipboardHandle *)iff->iff_Stream);
343 D(bug("datatypes.library/NewDTObjectA: Calling FreeIFF\n"));
344 FreeIFF(iff);
345 D(bug("datatypes.library/NewDTObjectA: IFF cleanup done\n"));
347 } /* if (iff != NULL) */
349 } /* if (dtobj == NULL) */
351 } /* SourceType okay */
353 if(error == ERROR_OBJECT_NOT_FOUND)
354 error = DTERROR_COULDNT_OPEN;
356 D(bug("datatypes.library/NewDTObjectA: Done. Returning %x\n", dtobj));
358 SetIoErr(error);
359 return dtobj;
361 AROS_LIBFUNC_EXIT
363 } /* NewDTObjectA */