Fixed typos in comments.
[AROS.git] / workbench / libs / icon / geticontaglist.c
blob65ed6cc58a4481cd4dac819e62c972d74cfce24d
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/debug.h>
7 #include <workbench/icon.h>
9 #include "icon_intern.h"
10 #include "support.h"
11 #include "support_builtin.h"
12 #include "identify.h"
14 /*****************************************************************************
16 NAME */
17 #include <proto/icon.h>
19 AROS_LH2(struct DiskObject *, GetIconTagList,
21 /* SYNOPSIS */
22 AROS_LHA(CONST_STRPTR, name, A0),
23 AROS_LHA(const struct TagItem *, tags, A1),
24 /* LOCATION */
25 struct IconBase *, IconBase, 30, Icon)
27 /* FUNCTION
28 Open icon from disk
29 INPUTS
31 TAGS
32 ICONA_ErrorCode (LONG *)
33 ICONGETA_GetDefaultType (LONG) - Default icon type to get. This
34 overrides the "name" parameter.
35 ICONGETA_GetDefaultName (STRPTR) - Name of default icon to get. This
36 overrides the "name" parameter.
37 ICONGETA_FailIfUnavailable (BOOL)
38 ICONGETA_GetPaletteMappedIcon (BOOL)
39 ICONGETA_IsDefaultIcon (LONG *) - Upon completion of this function, the
40 referenced LONG will be set to a boolean value indicating whether
41 the returned icon is a default icon.
42 ICONGETA_RemapIcon (BOOL)
43 ICONGETA_GenerateImageMasks (BOOL)
44 ICONGETA_Label (STRPTR)
45 ICONGETA_Screen (struct Screen *)
47 RESULT
49 NOTES
51 EXAMPLE
53 BUGS
55 SEE ALSO
57 INTERNALS
59 *****************************************************************************/
61 AROS_LIBFUNC_INIT
63 struct TagItem *tstate = (struct TagItem *)tags;
64 struct TagItem *tag;
65 struct DiskObject *icon = NULL;
66 LONG defaultType = -1;
67 CONST_STRPTR defaultName = NULL;
68 IPTR failIfUnavailable = TRUE;
69 LONG *isDefaultIcon = NULL;
71 IPTR getPaletteMappedIcon = TRUE;
72 IPTR remapIcon = TRUE;
73 IPTR generateImageMasks = TRUE;
74 struct Screen *screen = NULL;
75 STRPTR label = NULL; // FIXME: not used
76 LONG error = 0;
77 LONG *errorCode = NULL;
79 # define SET_ISDEFAULTICON(value) (isDefaultIcon != NULL ? *isDefaultIcon = (value) : (value))
81 D(bug("[%s] name %s, tags %p\n", __func__, name, tags));
83 /* Parse taglist -------------------------------------------------------*/
84 while ((tag = NextTagItem(&tstate)) != NULL)
86 switch (tag->ti_Tag)
88 case ICONGETA_GetDefaultType:
89 if (defaultType == -1)
90 defaultType = tag->ti_Data;
91 break;
93 case ICONGETA_GetDefaultName:
94 if (defaultName == NULL)
95 defaultName = (CONST_STRPTR) tag->ti_Data;
96 break;
98 case ICONGETA_FailIfUnavailable:
99 failIfUnavailable = tag->ti_Data;
100 break;
102 case ICONGETA_IsDefaultIcon:
103 isDefaultIcon = (LONG *) tag->ti_Data;
104 SET_ISDEFAULTICON(FALSE);
105 break;
107 case ICONGETA_GetPaletteMappedIcon:
108 getPaletteMappedIcon = tag->ti_Data;
109 break;
111 case ICONGETA_RemapIcon:
112 remapIcon = tag->ti_Data;
113 break;
115 case ICONGETA_GenerateImageMasks:
116 generateImageMasks = tag->ti_Data;
117 break;
119 case ICONGETA_Screen:
120 screen = (struct Screen *) tag->ti_Data;
121 break;
123 case ICONGETA_Label:
124 label = (STRPTR) tag->ti_Data;
125 break;
127 case ICONA_ErrorCode:
128 errorCode = (LONG *) tag->ti_Data;
129 break;
133 if (defaultType != -1 || defaultName != NULL)
135 if (defaultName != NULL)
137 BPTR file = OpenDefaultIcon(defaultName, MODE_OLDFILE);
139 D(bug("[%s] Find default icon '%s'\n", __func__, defaultName));
140 if (file != BNULL)
142 D(bug("[%s] Found default icon '%s'\n", __func__, defaultName));
143 icon = ReadIcon(file);
144 CloseDefaultIcon(file);
145 SET_ISDEFAULTICON(TRUE);
149 if (icon == NULL && defaultType != -1 && defaultName == NULL)
151 CONST_STRPTR defaultIconName = GetDefaultIconName(defaultType);
153 D(bug("[%s] Find default icon type %d\n", __func__, defaultType));
154 if (defaultIconName != NULL)
156 icon = GetIconTags
158 NULL,
159 ICONGETA_GetDefaultName, (IPTR) defaultIconName,
160 TAG_END
162 D(bug("[%s] Find default icon type %d as %p\n", __func__, defaultType, icon));
164 if (icon == NULL)
166 icon = GetBuiltinIcon(defaultType);
167 D(bug("[%s] Using builtin icon %p\n", __func__, icon));
168 SET_ISDEFAULTICON(TRUE);
173 else if (name != NULL)
175 BPTR file = OpenIcon(name, MODE_OLDFILE);
177 D(bug("[%s] Find custom icon '%s'\n", __func__, name));
178 if (file != BNULL)
180 D(bug("[%s] Found custom icon '%s'\n", __func__, name));
181 icon = ReadIcon(file);
182 CloseIcon(file);
184 if (icon != NULL && icon->do_Type == 0)
186 /* Force the icon type */
187 BPTR lock = LockObject(name, ACCESS_READ);
188 if (lock != BNULL)
190 LONG type = FindType(lock);
191 if (type != -1) icon->do_Type = type;
193 UnLockObject(lock);
197 } else {
198 /* NULL name = return empty DiskObject */
199 D(bug("[%s] Get an empty DiskObject\n", __func__));
200 icon = NewDiskObject(0);
203 /* Try to identify it by name or type */
204 if (icon == NULL && !failIfUnavailable)
206 struct IconIdentifyMsg iim;
208 D(bug("[%s] Falling back to default icon for %s\n", __func__, name));
210 if (name && (iim.iim_FileLock = LockObject(name, ACCESS_READ)) != BNULL)
212 D(bug("[%s] Locked %s, identifying\n", __func__, name));
213 if ((iim.iim_FIB = AllocDosObject(DOS_FIB, TAG_DONE)) != NULL)
215 if (Examine(iim.iim_FileLock, iim.iim_FIB))
217 iim.iim_SysBase = (struct Library *) SysBase;
218 iim.iim_DOSBase = (struct Library *) DOSBase;
219 iim.iim_UtilityBase = (struct Library *) UtilityBase;
220 iim.iim_IconBase = (struct Library *) IconBase;
221 iim.iim_Tags = tags;
223 iim.iim_ParentLock = ParentDir(iim.iim_FileLock);
224 if (iim.iim_ParentLock == BNULL && IoErr() == 0)
225 iim.iim_FIB->fib_DirEntryType = ST_ROOT;
226 iim.iim_FileHandle = Open(name, MODE_OLDFILE);
228 if (LB(IconBase)->ib_IdentifyHook != NULL)
230 /* Use user-provided identify hook */
231 icon = (struct DiskObject *) CALLHOOKPKT
233 LB(IconBase)->ib_IdentifyHook, NULL, &iim
236 if (icon != NULL)
239 Sanity check since we don't trust the
240 user-provided hook too much. ;-)
243 LONG type = FindType(iim.iim_FileLock);
244 if (type != -1) icon->do_Type = type;
248 if (icon == NULL)
250 /* Fall back to the default identify function */
251 D(bug("[%s] Finding default icon for iim %p\n", __func__, &iim));
252 icon = FindDefaultIcon(&iim);
255 if (iim.iim_ParentLock != BNULL) UnLock(iim.iim_ParentLock);
256 if (iim.iim_FileHandle != BNULL) Close(iim.iim_FileHandle);
259 FreeDosObject(DOS_FIB, iim.iim_FIB);
261 else
263 error = IoErr();
266 UnLockObject(iim.iim_FileLock);
269 if (icon == NULL && defaultType > 0) {
270 icon = GetBuiltinIcon(defaultType);
271 D(bug("[%s] Using builtin icon %p\n", __func__, icon));
274 if (icon == NULL) {
275 LONG type = (name && Stricmp("Disk", name)==0) ? WBDISK : WBPROJECT;
276 icon = GetBuiltinIcon(type);
277 D(bug("[%s] Using builtin icon %p, type %d\n", __func__, icon, type));
282 if (!icon)
283 goto exit;
285 if (label != NULL) {
286 /* TODO: Add the label specified in 'label' to the icon */
289 if (generateImageMasks) {
290 D(bug("[%s] %p: Generate image masks\n", __func__, icon));
291 /* A side effect of palette mapping the icon... */
292 getPaletteMappedIcon = TRUE;
295 if (getPaletteMappedIcon) {
296 D(bug("[%s] %p: Generate palette mapped icon\n", __func__, icon));
297 /* A side effect of remapping the icon to the DefaultPubScreen */
298 remapIcon = TRUE;
301 if (remapIcon) {
302 if (screen == NULL)
303 IconControl(NULL, ICONCTRLA_GetGlobalScreen, &screen, TAG_END);
304 } else {
305 screen = NULL;
308 /* Any last-minute fixups */
309 PrepareIcon(icon);
311 D(bug("[%s] %p: Performing initial layout for screen %p\n", __func__, icon, screen));
312 LayoutIconA(icon, screen, (struct TagItem *)tags);
314 exit:
315 /* Set error code */
316 if (errorCode != NULL)
317 *errorCode = error;
318 SetIoErr(error);
320 D(bug("[%s] name %s, tags %p => %p\n", __func__, name, tags, icon));
322 return icon;
324 AROS_LIBFUNC_EXIT
326 # undef SET_ISDEFAULTICON
328 } /* GetIconTagList() */