fix remapping behavior. Remapping is only necessary if we are rendering on the workbe...
[AROS-Contrib.git] / scalos / main / ToolTypes.c
blob4342ebbeae733c51de8f103b70c16e8bd8ea4fc9
1 // ToolTypes.c
2 // $Date$
3 // $Revision$
5 #include <exec/types.h>
6 #include <graphics/gels.h>
7 #include <graphics/rastport.h>
8 #include <intuition/classes.h>
9 #include <intuition/classusr.h>
10 #include <utility/hooks.h>
11 #include <intuition/gadgetclass.h>
12 #include <workbench/workbench.h>
13 #include <workbench/startup.h>
14 #include <dos/dostags.h>
15 #include <dos/datetime.h>
17 #define __USE_SYSBASE
19 #include <proto/dos.h>
20 #include <proto/exec.h>
21 #include <proto/layers.h>
22 #include <proto/intuition.h>
23 #include <proto/graphics.h>
24 #include <proto/utility.h>
25 #include <proto/iconobject.h>
26 #include "debug.h"
27 #include <proto/scalos.h>
29 #include <clib/alib_protos.h>
31 #include <defs.h>
32 #include <datatypes/iconobject.h>
33 #include <scalos/scalos.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
39 #include "scalos_structures.h"
40 #include "functions.h"
41 #include "Variables.h"
43 //----------------------------------------------------------------------------
45 // local data structures
47 //----------------------------------------------------------------------------
49 // local functions
51 static size_t CountToolTypeEntries(CONST_STRPTR *ToolTypeArray);
52 static size_t GetToolTypeLength(CONST_STRPTR *ToolTypeArray);
53 static STRPTR *scaFindToolType(STRPTR *ToolTypeArray, CONST_STRPTR typeName);
55 //----------------------------------------------------------------------------
57 LONG SetToolType(Object *iconObj, CONST_STRPTR ToolTypeName, CONST_STRPTR ToolTypeValue, BOOL SaveIcon)
59 LONG Result = RETURN_OK;
60 STRPTR newTT = NULL;
61 STRPTR *ttClone = NULL;
63 d1(kprintf("%s/%s/%ld: iconObj=%08lx ttName=<%s> ttValue=<%s>\n", \
64 __FILE__, __FUNC__, __LINE__, iconObj, ToolTypeName, ToolTypeValue));
66 do {
67 STRPTR *ToolTypeArray = NULL;
68 STRPTR *ptt;
69 STRPTR iconName;
70 size_t length;
72 if (NULL == iconObj)
73 break;
75 GetAttr(IDTA_ToolTypes, iconObj, (APTR) &ToolTypeArray);
76 GetAttr(DTA_Name, iconObj, (APTR) &iconName);
78 d1(kprintf("%s/%s/%ld: ToolTypeArray=%08lx iconName=<%s>\n", \
79 __FILE__, __FUNC__, __LINE__, ToolTypeArray, iconName));
81 // ToolTypeArray may be NULL if there are no tooltypes attached to the icon!
83 d1(for (ptt=ToolTypeArray; *ptt; ptt++)\
84 kprintf("%s/%s/%ld: old ToolType=<%s>\n", __FILE__, __FUNC__, __LINE__, *ptt));
86 length = 1 + strlen(ToolTypeName) + 1 + strlen(ToolTypeValue);
87 newTT = (STRPTR) ScalosAlloc(length);
88 if (NULL == newTT)
89 break;
91 if (strlen(ToolTypeValue) > 0)
92 snprintf(newTT, length, "%s=%s", ToolTypeName, ToolTypeValue);
93 else
94 stccpy(newTT, ToolTypeName, length);
96 ttClone = CloneToolTypeArray((CONST_STRPTR *) ToolTypeArray, 1);
97 if (NULL == ttClone)
98 break;
100 ptt = scaFindToolType(ttClone, ToolTypeName);
101 if (ptt)
103 // overwrite old tooltype
104 d1(kprintf("%s/%s/%ld: ToolType=<%s>\n", __FILE__, __FUNC__, __LINE__, *ptt));
105 *ptt = newTT;
107 else
109 // append new tooltype
110 for (ptt=ttClone; *ptt; ptt++)
113 *ptt = newTT;
116 for (ptt=ttClone; *ptt; ptt++)
117 d1(kprintf("%s/%s/%ld: new ToolType=<%s>\n", __FILE__, __FUNC__, __LINE__, *ptt));
119 SetAttrs(iconObj,
120 IDTA_ToolTypes, (IPTR) ttClone,
121 TAG_END);
123 if (SaveIcon)
125 Result = PutIconObjectTags(iconObj, iconName,
126 ICONA_NoNewImage, TRUE,
127 TAG_END);
129 } while (0);
131 if (newTT)
132 ScalosFree(newTT);
133 if (ttClone)
134 ScalosFree(ttClone);
136 return Result;
139 //----------------------------------------------------------------------------
141 LONG RemoveToolType(Object *iconObj, CONST_STRPTR ToolTypeName, BOOL SaveIcon)
143 LONG Result = RETURN_OK;
144 STRPTR *ttClone = NULL;
146 d1(kprintf("%s/%s/%ld: iconObj=%08lx ttName=<%s> ttValue=<%s>\n", \
147 __FILE__, __FUNC__, __LINE__, iconObj, ToolTypeName, ToolTypeValue));
149 do {
150 STRPTR *ToolTypeArray = NULL;
151 STRPTR *ptt;
152 STRPTR iconName;
154 if (NULL == iconObj)
155 break;
157 GetAttr(IDTA_ToolTypes, iconObj, (APTR) &ToolTypeArray);
158 GetAttr(DTA_Name, iconObj, (APTR) &iconName);
160 d1(kprintf("%s/%s/%ld: ToolTypeArray=%08lx iconName=<%s>\n", \
161 __FILE__, __FUNC__, __LINE__, ToolTypeArray, iconName));
163 if (NULL == ToolTypeArray)
164 break;
166 for (ptt=ToolTypeArray; *ptt; ptt++)
167 d1(kprintf("%s/%s/%ld: old ToolType=<%s>\n", __FILE__, __FUNC__, __LINE__, *ptt));
169 ttClone = CloneToolTypeArray((CONST_STRPTR *) ToolTypeArray, 0);
170 if (NULL == ttClone)
171 break;
173 ptt = scaFindToolType(ttClone, ToolTypeName);
174 if (ptt)
176 // old tooltype found - remove it
177 d1(kprintf("%s/%s/%ld: ToolType=<%s>\n", __FILE__, __FUNC__, __LINE__, *ptt));
178 while (*ptt)
180 *ptt = ptt[1];
181 if (NULL == *ptt)
182 break;
183 ptt++;
186 d1(for (ptt=ttClone; *ptt; ptt++) \
187 kprintf("%s/%s/%ld: new ToolType=<%s>\n", __FILE__, __FUNC__, __LINE__, *ptt));
189 SetAttrs(iconObj,
190 IDTA_ToolTypes, (IPTR) ttClone,
191 TAG_END);
193 if (SaveIcon)
195 Result = PutIconObjectTags(iconObj, iconName,
196 ICONA_NoNewImage, TRUE,
197 TAG_END);
200 } while (0);
202 if (ttClone)
203 ScalosFree(ttClone);
205 return Result;
208 //----------------------------------------------------------------------------
210 static size_t CountToolTypeEntries(CONST_STRPTR *ToolTypeArray)
212 size_t ttCount;
214 for (ttCount=0; ToolTypeArray && ToolTypeArray[ttCount]; ttCount++);
216 return ttCount;
219 //----------------------------------------------------------------------------
221 static size_t GetToolTypeLength(CONST_STRPTR *ToolTypeArray)
223 size_t Length = sizeof(STRPTR);
225 while (ToolTypeArray && *ToolTypeArray)
227 Length += sizeof(STRPTR) + strlen(*ToolTypeArray) + 1;
228 ToolTypeArray++;
231 return Length;
234 //----------------------------------------------------------------------------
236 STRPTR *CloneToolTypeArray(CONST_STRPTR *ToolTypeArray, ULONG AdditionalEntries)
238 size_t ttLength;
239 STRPTR *newTT, *newTTalloc;
240 STRPTR stringSpace;
241 ULONG ttCount;
243 d1(kprintf("%s/%s/%ld: AdditionalEntries=%ld\n", __FILE__, __FUNC__, __LINE__, AdditionalEntries));
245 ttLength = GetToolTypeLength(ToolTypeArray) + AdditionalEntries * sizeof(STRPTR);
246 ttCount = CountToolTypeEntries(ToolTypeArray);
248 d1(kprintf("%s/%s/%ld: ttLength=%ld ttCount=%ld\n", __FILE__, __FUNC__, __LINE__, ttLength, ttCount));
250 newTT = newTTalloc = ScalosAlloc(ttLength);
251 if (NULL == newTT)
252 return NULL;
254 stringSpace = (STRPTR) &newTT[1 + ttCount + AdditionalEntries];
256 d1(kprintf("%s/%s/%ld: newTT=%08lx stringSpace=%08lx\n", __FILE__, __FUNC__, __LINE__, newTT, stringSpace));
258 while (ToolTypeArray && *ToolTypeArray)
260 *newTT = stringSpace;
262 strcpy(stringSpace, *ToolTypeArray);
263 stringSpace += strlen(stringSpace) + 1;
265 newTT++;
266 ToolTypeArray++;
269 while (AdditionalEntries--)
270 *newTT++ = NULL;
272 *newTT = NULL;
274 return newTTalloc;
277 //----------------------------------------------------------------------------
279 static STRPTR *scaFindToolType(STRPTR *ToolTypeArray, CONST_STRPTR typeName)
281 if (NULL == ToolTypeArray)
282 return NULL;
284 while (*ToolTypeArray)
286 STRPTR lp = strchr(*ToolTypeArray, '=');
287 STRPTR CompEnd;
288 size_t CompLen;
290 if (lp)
292 CompEnd = lp;
294 if (CompEnd != *ToolTypeArray)
295 CompEnd--; // skip "="
296 while (CompEnd != *ToolTypeArray && ' ' == *CompEnd)
297 CompEnd--; // skip all " " before "="
299 // lp = stpblk(++lp); // skip "=" and trailing blanks
301 CompLen = CompEnd - *ToolTypeArray;
303 else
305 CompLen = strlen(*ToolTypeArray);
306 // lp = (STRPTR) "";
309 if (0 == Strnicmp(*ToolTypeArray, (STRPTR) typeName, CompLen))
311 return ToolTypeArray;
314 ToolTypeArray++;
317 return NULL;
320 //----------------------------------------------------------------------------