Fix segfault setting MenuFace pixmap style for menus.
[fvwm.git] / modules / FvwmWinList / List.c
blob8356aeadd9e46b3d87c33291877f8f11d6f5f974
1 /* -*-c-*- */
2 /* FvwmWinList Module for Fvwm.
4 * Copyright 1994, Mike Finger (mfinger@mermaid.micro.umn.edu or
5 * Mike_Finger@atk.com)
7 * The functions in this source file are the original work of Mike Finger.
9 * No guarantees or warantees or anything are provided or implied in any way
10 * whatsoever. Use this program at your own risk. Permission to use this
11 * program for any purpose is given, as long as the copyright is kept intact.
13 * Things to do: Convert to C++ (In Progress)
16 /* This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "config.h"
32 #include <stdio.h>
34 #include "FvwmWinList.h"
35 #include "List.h"
36 #include "Mallocs.h"
37 #include "libs/Module.h"
38 #include "fvwm/fvwm.h"
40 extern long CurrentDesk;
41 extern int ShowCurrentDesk;
42 extern int UseSkipList;
45 InitList - Initialize the list
47 void InitList(List *list)
49 list->head=list->tail=NULL;
50 list->count=0;
54 AddItem - Allocates spaces for and appends an item to the list
56 /*void AddItem(List *list, long id,long flags, long desk)*/
57 void AddItem(List *list, ConfigWinPacket *cfgpacket)
59 Item *new;
60 new=(Item *)safemalloc(sizeof(Item));
61 new->id=cfgpacket->w;
62 new->name=NULL;
63 new->flags=cfgpacket->flags;
64 new->desk=cfgpacket->desk;
65 new->next=NULL;
67 if (list->tail==NULL) list->head=list->tail=new;
68 else {
69 list->tail->next=new;
70 list->tail=new;
72 list->count++;
76 ReorderList - Make the list order matcht the internal fvwm winlist
77 FlipFocus is False when the Focus command has been used, True for all other
78 cases of Focus change.
79 If true the item is plucked from the list and placed at the start
80 If false the list is closed into a loop, rotated around to bring id to
81 the top and then opened up into a terminated line again.
83 void ReorderList(List *list, long id, long FlipFocus)
85 Item *temp = list->head, *prev = NULL;
86 int i = 0;
88 if (!id) return; /* this is a NOP if id == 0 */
90 /* find the item */
91 while (temp && i != id) {
92 prev = temp;
93 temp = temp->next;
94 i++;
97 if (!temp) return; /* we fell off the list */
99 /* prev is guaranteed to be !NULL */
100 if (FlipFocus) {
101 /* take care of the tail of the list */
102 if (list->tail == temp) list->tail = prev;
103 /* pluck it */
104 prev->next = temp->next;
105 /* shove it */
106 temp->next = list->head;
107 list->head = temp;
108 } else {
109 /* close the end */
110 list->tail->next = list->head;
111 /* rotate around by changing the list pointers */
112 list->head = temp;
113 list->tail = prev;
114 /* unclose the end */
115 prev->next = NULL;
120 FindItem - Find the item in the list matching the id
122 int FindItem(List *list, long id)
124 Item *temp;
125 int i;
127 for(i=0,temp=list->head;temp!=NULL && temp->id!=id;i++,temp=temp->next);
128 if (temp==NULL) return -1;
129 return i;
133 FindItemVisible - Find the item which should be in winlist in the list
134 matching the id
136 int FindItemVisible(List *list, long id)
138 Item *temp;
139 int i=0;
141 for(temp=list->head;temp!=NULL && temp->id!=id;temp=temp->next)
143 if (IsItemVisible(temp)) i++;
145 if (temp==NULL) return -1;
146 return i;
151 UpdateItem - Update the item in the list, setting name & flags as necessary.
153 int UpdateItemName(List *list, long id, char *string)
155 Item *temp;
156 int i;
158 for(i=0,temp=list->head;temp!=NULL && id!=temp->id;i++,temp=temp->next);
159 if (temp==NULL) return -1;
160 UpdateString(&temp->name, string);
161 return i;
165 UpdateItemDesk - Update the item in the list, setting desk as necessary.
166 returns 1 if desk was updated,
167 returns 0, if not changed
168 returns -1 if not found
170 int UpdateItemDesk(List *list, ConfigWinPacket *cfgpacket)
172 Item *temp;
174 for(temp=list->head;temp != NULL && cfgpacket->w!=temp->id;temp=temp->next);
175 if (temp ==NULL ) return -1;
176 if(temp->desk != cfgpacket->desk)
178 temp->desk = cfgpacket->desk;
179 return 1;
182 return 0;
186 UpdateItemGSFRFlags - Update the GSFR flags
187 returns -1 if not found
189 int UpdateItemGSFRFlags(List *list, ConfigWinPacket *cfgpacket)
191 Item *temp;
192 for(temp=list->head;temp!=NULL && cfgpacket->w!=temp->id;temp=temp->next);
193 if (temp==NULL) return -1;
194 temp->flags = cfgpacket->flags;
195 return 0;
199 FreeItem - Frees allocated space for an Item
201 void FreeItem(Item *ptr)
203 if (ptr != NULL) {
204 if (ptr->name!=NULL) free(ptr->name);
205 free(ptr);
210 DeleteItem - Deletes an item from the list
212 int DeleteItem(List *list,long id)
214 Item *temp,*temp2;
215 int i;
217 if (list->head==NULL) return -1;
218 if (list->head->id==id)
220 temp2=list->head;
221 temp=list->head=list->head->next;
222 i=-1;
224 else
226 for(i=0,temp=list->head;temp->next!=NULL && temp->next->id!=id;
227 i++,temp=temp->next);
228 if (temp->next==NULL) return -1;
229 temp2=temp->next;
230 temp->next=temp2->next;
233 if (temp2==list->tail) list->tail=temp;
235 FreeItem(temp2);
236 list->count--;
237 return i+1;
241 FreeList - Free the entire list of Items
243 void FreeList(List *list)
245 Item *temp,*temp2;
247 for(temp=list->head;temp!=NULL;)
249 temp2=temp;
250 temp=temp->next;
251 FreeItem(temp2);
253 list->count=0;
257 PrintList - Print the list of item on the console. (Debugging)
259 void PrintList(List *list)
261 Item *temp;
262 ConsoleMessage("List of Items:\n");
263 ConsoleMessage(" %10s %-15s %-15s %-15s %-15s Flgs\n",
264 "ID","Name","I-Name", "R-Name","R-Class");
265 ConsoleMessage(" ---------- --------------- --------------- --------------- --------------- ----\n");
266 for(temp=list->head;temp!=NULL;temp=temp->next) {
267 int i;
268 unsigned char* p = (unsigned char*)&temp->flags;
269 ConsoleMessage(" %10ld %-15.15s ",
270 temp->id,
271 (temp->name==NULL) ? "<null>" : temp->name);
272 for( i = 0; i < sizeof(temp->flags); ++i ) {
273 ConsoleMessage( "%x2", *p++ );
275 ConsoleMessage( "\n" );
280 ItemName - Return the name of an Item
282 char *ItemName(List *list, int n)
284 Item *temp;
285 int i;
287 for(i=0,temp=list->head;temp!=NULL && i<n;i++,temp=temp->next);
288 if (temp==NULL) return NULL;
289 return temp->name;
293 ItemFlags - Return the flags for an item
294 RBW - this is no longer appropriate since the Great Style Flag Rewrite, so
295 this function will just return the Item pointer. The GSFR macros know how
296 deal with that.
297 Function returns NULL if the item is not found.
299 Item *ItemFlags(List *list, long id)
301 Item *temp;
303 for(temp=list->head; temp != NULL && id!=temp->id; temp=temp->next);
305 return temp;
310 ItemDesk - Return the desk for an item
312 long ItemDesk(List *list, long id)
314 Item *temp;
316 for(temp=list->head;temp!=NULL && id!=temp->id;temp=temp->next);
318 if (temp==NULL) return -1;
319 else return temp->desk;
325 ItemCount - Return the number of items in the list
327 int ItemCount(List *list)
329 return list->count;
333 ItemCountDesk - Return the number of items in the list which should be in
334 in winlist
337 int ItemCountVisible(List *list)
339 Item *temp;
340 int count=0;
342 for(temp=list->head;temp != NULL;temp = temp->next)
344 if (IsItemVisible(temp))
345 count++;
348 return count;
352 ItemID - Return the ID of the item in the list.
354 long ItemID(List *list, int n)
356 Item *temp;
357 int i;
359 for(i=0,temp=list->head;temp!=NULL && i<n;i++,temp=temp->next);
360 if (temp==NULL) return -1;
361 return temp->id;
365 CopyItem - Copy an item from one list to another
367 void CopyItem(List *dest, List *source, int n)
369 Item *temp;
370 int i;
371 ConfigWinPacket cfgpkt;
373 for(i=0,temp=source->head;temp!=NULL && i<n;i++,temp=temp->next);
374 if (temp==NULL) return;
375 memset(&cfgpkt, '\0', sizeof(cfgpkt));
376 AddItem(dest, &cfgpkt);
377 UpdateItemName(dest,temp->id,temp->name);
378 DeleteItem(source,temp->id);
382 IsItemVisible - Says if the item should be in winlist
384 int IsItemVisible(Item *temp)
386 if ((!ShowCurrentDesk || temp->desk == CurrentDesk ||
387 IS_STICKY_ACROSS_DESKS(temp)) &&
388 (!DO_SKIP_WINDOW_LIST(temp) || !UseSkipList))
389 return 1;
390 else
391 return 0;
395 IsItemIndexVisible - Says if the item of index i in the list should be in
396 winlist
398 int IsItemIndexVisible(List *list,int n)
400 Item *temp;
401 int i;
403 for(i=0,temp=list->head;temp!=NULL && i<n;i++,temp=temp->next);
404 if (temp == NULL) return 0;
405 if (IsItemVisible(temp))
406 return 1;
407 else
408 return 0;