forgotten commit. disabled until egl is adapted.
[AROS-Contrib.git] / scalos / libraries / popupmenu / pmfind.c
blobe3511f117551ecd9b69cb254e8b96fd2e069c59d
1 //
2 // pmfind.c - Various support functions used to search the menu item tree.
3 //
4 // Copyright ©1996 - 2002 Henrik Isaksson
5 // All Rights Reserved.
6 //
7 // $Date$
8 // $Revision$
9 //
11 #include "pmpriv.h"
14 // Find an item
17 // Find a selectable item after a->Selected. If none is found, return NULL.
18 struct PopupMenu *PM_FindNextSelectable(struct PM_Window *a, struct PopupMenu *pm, BOOL *found)
20 struct PopupMenu *tmp;
22 if (pm) do {
23 if (pm->Flags&NPM_GROUP) {
24 tmp=PM_FindNextSelectable(a, pm->SubGroup.Sub, found);
25 if (tmp) {
26 if (*found)
27 return tmp;
31 if (!(pm->Flags&NPM_NOSELECT) && !(pm->Flags&NPM_HIDDEN) && *found)
32 return pm;
34 if (pm==a->Selected) {
35 *found=TRUE;
38 pm=pm->Next;
39 } while (pm);
41 return NULL;
44 // Find the first selectable item in the menu. If none is found, return NULL.
45 struct PopupMenu *PM_FindFirstSelectable(struct PopupMenu *pm)
47 struct PopupMenu *tmp;
49 if (pm) do {
50 if (pm->Flags&NPM_GROUP) {
51 tmp=PM_FindFirstSelectable(pm->SubGroup.Sub);
52 if (tmp) {
53 return tmp;
57 if (!(pm->Flags&NPM_NOSELECT) && !(pm->Flags&NPM_HIDDEN))
58 return pm;
60 pm=pm->Next;
61 } while (pm);
63 return NULL;
67 // Find a selectable item ahead of a->Selected. If none is found, return NULL.
68 struct PopupMenu *PM_FindPrevSelectable(struct PM_Window *a, struct PopupMenu *pm, BOOL *found)
70 struct PopupMenu *prv=NULL, *tmp;
72 if (pm) do {
73 if (pm->Flags&NPM_GROUP) {
74 tmp=PM_FindPrevSelectable(a, pm->SubGroup.Sub, found);
75 if (tmp) {
76 if (*found)
77 return tmp;
78 prv=tmp;
79 } else if (*found)
80 return prv;
83 if (pm==a->Selected) {
84 *found=TRUE;
85 return prv;
87 if (!(pm->Flags&NPM_NOSELECT) && !(pm->Flags&NPM_HIDDEN))
88 prv=pm;
89 pm=pm->Next;
90 } while (pm);
92 return prv;
95 // Find the last selectable item in the menu. If none is found, return NULL.
96 struct PopupMenu *PM_FindLastSelectable(struct PopupMenu *pm)
98 struct PopupMenu *prv=NULL, *tmp;
100 if (pm) do {
101 if (pm->Flags&NPM_GROUP) {
102 tmp=PM_FindLastSelectable(pm->SubGroup.Sub);
103 if (tmp) {
104 prv=tmp;
106 } else if (!(pm->Flags&NPM_NOSELECT) && !(pm->Flags&NPM_HIDDEN))
107 prv=pm;
109 pm=pm->Next;
110 } while (pm);
112 return prv;
115 // PM_FindID - Find an item based on it's ID
116 struct PopupMenu *PM_FindID(struct PopupMenu *base, ULONG ID)
118 struct PopupMenu *pm=base,*xm;
120 while (pm) {
121 if (pm->ID==ID) return pm;
122 if (pm->SubGroup.Sub) {
123 xm=PM_FindID(pm->SubGroup.Sub, ID);
124 if (xm) return xm;
126 pm=pm->Next;
128 return NULL;
131 // PM_FindBeforeID - find the item before item with ID in pm
132 struct PopupMenu *PM_FindBeforeID(struct PopupMenu *pm, ULONG ID)
134 struct PopupMenu *prev=pm,*xm;
136 while (pm) {
137 if (pm->ID==ID) return prev;
138 if (pm->SubGroup.Sub) {
139 xm=PM_FindBeforeID(pm->SubGroup.Sub, ID);
140 if (xm) return xm;
142 prev=pm;
143 pm=pm->Next;
145 return NULL;
148 // PM_FindBefore - Find the item before fm in pm
149 struct PopupMenu *PM_FindBefore(struct PopupMenu *pm, struct PopupMenu *fm)
151 struct PopupMenu *prev=pm,*xm;
153 while (pm) {
154 if (pm==fm) return prev;
155 if (pm->SubGroup.Sub) {
156 xm=PM_FindBefore(pm->SubGroup.Sub, fm);
157 if (xm) return xm;
159 prev=pm;
160 pm=pm->Next;
162 return NULL;
165 // PM_FindSortedInsertPoint - find where to insert the item fm in the list pm
166 struct PopupMenu *PM_FindSortedInsertPoint(struct PopupMenu *pm, struct PopupMenu *fm)
168 struct PopupMenu *prev=pm;
170 while (pm) {
171 if (PM_String_Compare(pm->TitleUnion.Title, fm->TitleUnion.Title) < 0)
172 return prev;
173 prev=pm;
174 pm=pm->Next;
176 return NULL;
179 // PM_FindLast - Find the end of the list.
180 struct PopupMenu *PM_FindLast(struct PopupMenu *base)
182 struct PopupMenu *pm=base;
184 while (pm) {
185 if (pm->Next==NULL) return pm;
186 pm=pm->Next;
189 return NULL;
192 // PM_FindItemCommKey - Find an item based on it's CommKey (command key)
193 struct PopupMenu *PM_FindItemCommKey(struct PopupMenu *base, UBYTE key)
195 struct PopupMenu *pm=base,*xm;
197 while (pm) {
198 if (ToUpper((UBYTE)pm->CommKey)==ToUpper(key)) return pm;
200 if (pm->SubGroup.Sub) {
201 xm=PM_FindItemCommKey(pm->SubGroup.Sub, key);
202 if (xm) return xm;
204 pm=pm->Next;
206 return NULL;
209 // PM_FindItem - find an item based on it's ID
210 LIBFUNC_P3(struct PopupMenu *, LIBPM_FindItem,
211 A1, struct PopupMenu *, menu,
212 D1, ULONG, ID,
213 A6, struct PopupMenuBase *, l)
215 (void) l;
217 if (!menu) return NULL;
218 return PM_FindID(menu, ID);
220 LIBFUNC_END
223 // See if an item is checked
225 LIBFUNC_P3(BOOL, LIBPM_ItemChecked,
226 A1, struct PopupMenu *, pm,
227 D1, ULONG, ID,
228 A6, struct PopupMenuBase *, l);
230 struct PopupMenu *p;
232 (void) l;
234 p=PM_FindID(pm, ID);
236 if (p) {
237 if (p->Flags&NPM_CHECKED) return TRUE;
238 return FALSE;
240 return -5L;
242 LIBFUNC_END
245 // Set/Clear all items in a list
248 LIBFUNC_P4(void, LIBPM_AlterState,
249 A1, struct PopupMenu *, base,
250 A2, struct PM_IDLst *, ids,
251 D1, UWORD, action,
252 A6, struct PopupMenuBase *, l)
254 (void) l;
256 pm_AlterState(base, ids, action);
258 LIBFUNC_END
261 void pm_AlterState(struct PopupMenu *base, struct PM_IDLst *ids, UWORD action)
263 struct PopupMenu *pm;
264 struct PM_IDLst *id=ids;
265 while (id) {
266 pm=PM_FindID(base, id->ID);
267 if (pm) {
268 if (action==PMACT_SELECT) {
269 if (id->Kind==IDKND_REFLECT) {
270 pm->Flags|=NPM_CHECKED|NPM_ISSELECTED;
271 if (pm->AutoSetPtr) *pm->AutoSetPtr=TRUE;
272 } else if (id->Kind==IDKND_INVERSE) {
273 pm->Flags&=~(NPM_CHECKED|NPM_ISSELECTED);
274 if (pm->AutoSetPtr) *pm->AutoSetPtr=FALSE;
275 } else if (id->Kind==IDKND_INCLUDE) {
276 pm->Flags|=NPM_CHECKED|NPM_ISSELECTED;
277 if (pm->AutoSetPtr) *pm->AutoSetPtr=TRUE;
278 } else if (id->Kind==IDKND_EXCLUDE) {
279 pm->Flags&=~(NPM_CHECKED|NPM_ISSELECTED);
280 if (pm->AutoSetPtr) *pm->AutoSetPtr=FALSE;
282 } else if (action==PMACT_DESELECT) {
283 if (id->Kind==IDKND_INVERSE) {
284 pm->Flags|=NPM_CHECKED|NPM_ISSELECTED;
285 if (pm->AutoSetPtr) *pm->AutoSetPtr=TRUE;
286 } else if (id->Kind==IDKND_REFLECT) {
287 pm->Flags&=~(NPM_CHECKED|NPM_ISSELECTED);
288 if (pm->AutoSetPtr) *pm->AutoSetPtr=FALSE;
292 id=id->Next;