various table widget updates
[wmaker-crm.git] / WINGs / WUtil.h
blobaa853664798b809efe1aa069270f151ceba725b7
1 #ifndef _WUTIL_H_
2 #define _WUTIL_H_
4 #include <X11/Xlib.h>
5 #include <limits.h>
6 #include <sys/types.h>
8 /* SunOS 4.x Blargh.... */
9 #ifndef NULL
10 #define NULL ((void*)0)
11 #endif
14 * Warning: proplist.h #defines BOOL which will clash with the
15 * typedef BOOL in Xmd.h
16 * proplist.h should use Bool (which is a #define in Xlib.h) instead.
19 #include <proplist.h>
22 #ifndef WMAX
23 # define WMAX(a,b) ((a)>(b) ? (a) : (b))
24 #endif
25 #ifndef WMIN
26 # define WMIN(a,b) ((a)<(b) ? (a) : (b))
27 #endif
30 #if (!defined (__GNUC__) || __GNUC__ < 2 || \
31 __GNUC_MINOR__ < (defined (__cplusplus) ? 6 : 4))
32 #define __ASSERT_FUNCTION ((char *) 0)
33 #else
34 #define __ASSERT_FUNCTION __PRETTY_FUNCTION__
35 #endif
38 #ifdef NDEBUG
40 #define wassertr(expr) {}
41 #define wassertrv(expr, val) {}
43 #else /* !NDEBUG */
45 #ifdef DEBUG
47 #include <assert.h>
49 #define wassertr(expr) assert(expr)
51 #define wassertrv(expr, val) assert(expr)
53 #else /* !DEBUG */
55 #define wassertr(expr) \
56 if (!(expr)) { \
57 wwarning("%s line %i (%s): assertion %s failed",\
58 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
59 return;\
62 #define wassertrv(expr, val) \
63 if (!(expr)) { \
64 wwarning("%s line %i (%s): assertion %s failed",\
65 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
66 return (val);\
68 #endif /* !DEBUG */
70 #endif /* !NDEBUG */
73 #ifdef __cplusplus
74 extern "C" {
75 #endif /* __cplusplus */
78 typedef enum {
79 WMPostWhenIdle = 1,
80 WMPostASAP = 2,
81 WMPostNow = 3
82 } WMPostingStyle;
85 typedef enum {
86 WNCNone = 0,
87 WNCOnName = 1,
88 WNCOnSender = 2
89 } WMNotificationCoalescing;
92 /* The possible states for connections */
93 typedef enum {
94 WCNotConnected=0,
95 WCListening,
96 WCInProgress,
97 WCFailed,
98 WCConnected,
99 WCTimedOut,
100 WCDied,
101 WCClosed
102 } WMConnectionState;
105 /* The possible states for connection timeouts */
106 typedef enum {
107 WCTNone=0,
108 WCTWhileOpening,
109 WCTWhileSending
110 } WMConnectionTimeoutState;
114 enum {
115 WBNotFound = INT_MIN, /* element was not found in WMBag */
116 WANotFound = -1 /* element was not found in WMArray */
120 typedef struct W_Array WMArray;
121 typedef struct W_Bag WMBag;
122 typedef struct W_Data WMData;
123 typedef struct W_HashTable WMHashTable;
124 typedef struct W_UserDefaults WMUserDefaults;
125 typedef struct W_Notification WMNotification;
126 typedef struct W_NotificationQueue WMNotificationQueue;
127 typedef struct W_Host WMHost;
128 typedef struct W_Connection WMConnection;
132 typedef int WMCompareDataProc(const void *item1, const void *item2);
134 typedef void WMFreeDataProc(void *data);
136 /* Used by WMBag or WMArray for matching data */
137 typedef int WMMatchDataProc(void *item, void *cdata);
142 typedef struct {
143 int position;
144 int count;
145 } WMRange;
149 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
150 typedef struct {
151 void *table;
152 void *nextItem;
153 int index;
154 } WMHashEnumerator;
157 typedef struct {
158 /* NULL is pointer hash */
159 unsigned (*hash)(const void *);
160 /* NULL is pointer compare */
161 Bool (*keyIsEqual)(const void *, const void *);
162 /* NULL does nothing */
163 void* (*retainKey)(const void *);
164 /* NULL does nothing */
165 void (*releaseKey)(const void *);
166 } WMHashTableCallbacks;
169 typedef void *WMBagIterator;
172 #if 0
173 typedef struct {
174 char character; /* the escape character */
175 char *value; /* value to place */
176 } WMSEscapes;
177 #endif
180 /* The connection callbacks */
181 typedef struct ConnectionDelegate {
182 void *data;
184 void (*didCatchException)(struct ConnectionDelegate *self,
185 WMConnection *cPtr);
187 void (*didDie)(struct ConnectionDelegate *self, WMConnection *cPtr);
189 void (*didInitialize)(struct ConnectionDelegate *self, WMConnection *cPtr);
191 void (*didReceiveInput)(struct ConnectionDelegate *self, WMConnection *cPtr);
193 void (*didTimeout)(struct ConnectionDelegate *self, WMConnection *cPtr);
195 } ConnectionDelegate;
198 typedef void WMNotificationObserverAction(void *observerData,
199 WMNotification *notification);
203 /*......................................................................*/
205 typedef void (waborthandler)(int);
207 waborthandler* wsetabort(waborthandler*);
210 /* don't free the returned string */
211 char* wstrerror(int errnum);
213 void wmessage(const char *msg, ...);
214 void wwarning(const char *msg, ...);
215 void wfatal(const char *msg, ...);
216 void wsyserror(const char *msg, ...);
217 void wsyserrorwithcode(int error, const char *msg, ...);
219 char* wfindfile(char *paths, char *file);
221 char* wfindfileinlist(char **path_list, char *file);
223 char* wfindfileinarray(proplist_t array, char *file);
225 char* wexpandpath(char *path);
227 /* don't free the returned string */
228 char* wgethomedir();
230 void* wmalloc(size_t size);
231 void* wrealloc(void *ptr, size_t newsize);
232 void wfree(void *ptr);
235 void wrelease(void *ptr);
236 void* wretain(void *ptr);
238 char* wstrdup(char *str);
240 /* Concatenate str1 with str2 and return that in a newly malloc'ed string.
241 * str1 and str2 can be any strings including static and constant strings.
242 * str1 and str2 will not be modified.
243 * Free the returned string when you're done with it. */
244 char* wstrconcat(char *str1, char *str2);
246 /* This will append src to dst, and return dst. dst MUST be either NULL
247 * or a string that was a result of a dynamic allocation (malloc, realloc
248 * wmalloc, wrealloc, ...). dst CANNOT be a static or a constant string!
249 * Modifies dst (no new string is created except if dst==NULL in which case
250 * it is equivalent to calling wstrdup(src) ).
251 * The returned address may be different from the original address of dst,
252 * so always assign the returned address to avoid dangling pointers. */
253 char* wstrappend(char *dst, char *src);
256 void wtokensplit(char *command, char ***argv, int *argc);
258 char* wtokennext(char *word, char **next);
260 char* wtokenjoin(char **list, int count);
262 void wtokenfree(char **tokens, int count);
264 char* wtrimspace(char *s);
268 char* wusergnusteppath();
270 char* wdefaultspathfordomain(char *domain);
272 void wusleep(unsigned int microsec);
274 #if 0
275 int wsprintesc(char *buffer, int length, char *format, WMSEscapes **escapes,
276 int count);
277 #endif
279 /*......................................................................*/
281 /* This function is used _only_ if you create a NON-GUI program.
282 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
283 * This function will handle all input/timer/idle events, then return.
286 void WHandleEvents();
288 /*......................................................................*/
291 WMHashTable* WMCreateHashTable(WMHashTableCallbacks callbacks);
293 void WMFreeHashTable(WMHashTable *table);
295 void WMResetHashTable(WMHashTable *table);
297 const void* WMHashGet(WMHashTable *table, const void *key);
299 /* put data in table, replacing already existing data and returning
300 * the old value */
301 const void* WMHashInsert(WMHashTable *table, const void *key, const void *data);
303 void WMHashRemove(WMHashTable *table, const void *key);
305 /* warning: do not manipulate the table while using these functions */
306 WMHashEnumerator WMEnumerateHashTable(WMHashTable *table);
308 const void* WMNextHashEnumeratorItem(WMHashEnumerator *enumerator);
310 unsigned WMCountHashTable(WMHashTable *table);
315 /* some predefined callback sets */
317 extern const WMHashTableCallbacks WMIntHashCallbacks;
318 /* sizeof(keys) are <= sizeof(void*) */
320 extern const WMHashTableCallbacks WMStringHashCallbacks;
321 /* keys are strings. Strings will be copied with wstrdup()
322 * and freed with wfree() */
324 extern const WMHashTableCallbacks WMStringPointerHashCallbacks;
325 /* keys are strings, bug they are not copied */
328 /*......................................................................*/
331 * WMArray use an array to store the elements.
332 * Item indexes may be only positive integer numbers.
333 * The array cannot contain holes in it.
335 * Pros:
336 * Fast [O(1)] access to elements
337 * Fast [O(1)] push/pop
339 * Cons:
340 * A little slower [O(n)] for insertion/deletion of elements that
341 * aren't in the end
344 WMArray* WMCreateArray(int initialSize);
346 WMArray* WMCreateArrayWithDestructor(int initialSize, WMFreeDataProc *destructor);
348 WMArray* WMCreateArrayWithArray(WMArray *array);
350 #define WMDuplicateArray(array) WMCreateArrayWithArray(array)
352 void WMEmptyArray(WMArray *array);
354 void WMFreeArray(WMArray *array);
356 int WMGetArrayItemCount(WMArray *array);
358 /* appends other to array. other remains unchanged */
359 void WMAppendArray(WMArray *array, WMArray *other);
361 /* add will place the element at the end of the array */
362 void WMAddToArray(WMArray *array, void *item);
364 #define WMPushInArray(array, item) WMAddToArray(array, item)
366 /* insert will increment the index of elements after it by 1 */
367 void WMInsertInArray(WMArray *array, int index, void *item);
369 /* replace and set will return the old item WITHOUT calling the
370 * destructor on it even if its available. Free the returned item yourself.
372 void* WMReplaceInArray(WMArray *array, int index, void *item);
374 #define WMSetInArray(array, index, item) WMReplaceInArray(array, index, item)
376 /* delete and remove will remove the elements and cause the elements
377 * after them to decrement their indexes by 1. Also will call the
378 * destructor on the deleted element if there's one available.
380 int WMDeleteFromArray(WMArray *array, int index);
382 int WMRemoveFromArray(WMArray *array, void *item);
384 void* WMGetFromArray(WMArray *array, int index);
386 #define WMGetFirstInArray(array, item) WMFindInArray(array, NULL, item)
388 /* pop will return the last element from the array, also removing it
389 * from the array. The destructor is NOT called, even if available.
390 * Free the returned element if needed by yourself
392 void* WMPopFromArray(WMArray *array);
394 int WMFindInArray(WMArray *array, WMMatchDataProc *match, void *cdata);
396 int WMCountInArray(WMArray *array, void *item);
398 /* comparer must return:
399 * < 0 if a < b
400 * > 0 if a > b
401 * = 0 if a = b
403 void WMSortArray(WMArray *array, WMCompareDataProc *comparer);
405 void WMMapArray(WMArray *array, void (*function)(void*, void*), void *data);
407 WMArray* WMGetSubarrayWithRange(WMArray* array, WMRange aRange);
410 /*..........................................................................*/
413 * Tree bags use a red-black tree for storage.
414 * Item indexes may be any integer number.
416 * Pros:
417 * O(lg n) insertion/deletion/search
418 * Good for large numbers of elements with sparse indexes
420 * Cons:
421 * O(lg n) insertion/deletion/search
422 * Slow for storing small numbers of elements
425 #define WMCreateBag(size) WMCreateTreeBag()
427 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
429 WMBag* WMCreateTreeBag(void);
431 WMBag* WMCreateTreeBagWithDestructor(WMFreeDataProc *destructor);
433 int WMGetBagItemCount(WMBag *bag);
435 void WMAppendBag(WMBag *bag, WMBag *other);
437 void WMPutInBag(WMBag *bag, void *item);
439 /* insert will increment the index of elements after it by 1 */
440 void WMInsertInBag(WMBag *bag, int index, void *item);
442 /* this is slow */
443 /* erase will remove the element from the bag,
444 * but will keep the index of the other elements unchanged */
445 int WMEraseFromBag(WMBag *bag, int index);
447 /* delete and remove will remove the elements and cause the elements
448 * after them to decrement their indexes by 1 */
449 int WMDeleteFromBag(WMBag *bag, int index);
451 int WMRemoveFromBag(WMBag *bag, void *item);
453 void* WMGetFromBag(WMBag *bag, int index);
455 void* WMReplaceInBag(WMBag *bag, int index, void *item);
457 #define WMSetInBag(bag, index, item) WMReplaceInBag(bag, index, item)
459 /* comparer must return:
460 * < 0 if a < b
461 * > 0 if a > b
462 * = 0 if a = b
464 void WMSortBag(WMBag *bag, WMCompareDataProc *comparer);
466 void WMEmptyBag(WMBag *bag);
468 void WMFreeBag(WMBag *bag);
470 void WMMapBag(WMBag *bag, void (*function)(void*, void*), void *data);
472 int WMGetFirstInBag(WMBag *bag, void *item);
474 int WMCountInBag(WMBag *bag, void *item);
476 int WMFindInBag(WMBag *bag, WMMatchDataProc *match, void *cdata);
478 void* WMBagFirst(WMBag *bag, WMBagIterator *ptr);
480 void* WMBagLast(WMBag *bag, WMBagIterator *ptr);
482 void* WMBagNext(WMBag *bag, WMBagIterator *ptr);
484 void* WMBagPrevious(WMBag *bag, WMBagIterator *ptr);
486 void* WMBagIteratorAtIndex(WMBag *bag, int index, WMBagIterator *ptr);
488 int WMBagIndexForIterator(WMBag *bag, WMBagIterator ptr);
491 #define WM_ITERATE_BAG(bag, var, i) \
492 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
493 var = WMBagNext(bag, &(i)))
495 #define WM_ETARETI_BAG(bag, var, i) \
496 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
497 var = WMBagPrevious(bag, &(i)))
501 /*-------------------------------------------------------------------------*/
503 /* WMData handling */
505 /* Creating/destroying data */
507 WMData* WMCreateDataWithCapacity(unsigned capacity);
509 WMData* WMCreateDataWithLength(unsigned length);
511 WMData* WMCreateDataWithBytes(void *bytes, unsigned length);
513 /* destructor is a function called to free the data when releasing the data
514 * object, or NULL if no freeing of data is necesary. */
515 WMData* WMCreateDataWithBytesNoCopy(void *bytes, unsigned length,
516 WMFreeDataProc *destructor);
518 WMData* WMCreateDataWithData(WMData *aData);
520 WMData* WMRetainData(WMData *aData);
522 void WMReleaseData(WMData *aData);
524 /* Adjusting capacity */
526 void WMSetDataCapacity(WMData *aData, unsigned capacity);
528 void WMSetDataLength(WMData *aData, unsigned length);
530 void WMIncreaseDataLengthBy(WMData *aData, unsigned extraLength);
532 /* Accessing data */
534 const void* WMDataBytes(WMData *aData);
536 void WMGetDataBytes(WMData *aData, void *buffer);
538 void WMGetDataBytesWithLength(WMData *aData, void *buffer, unsigned length);
540 void WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange);
542 WMData* WMGetSubdataWithRange(WMData *aData, WMRange aRange);
544 /* Testing data */
546 Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
548 unsigned WMGetDataLength(WMData *aData);
550 /* Adding data */
552 void WMAppendDataBytes(WMData *aData, void *bytes, unsigned length);
554 void WMAppendData(WMData *aData, WMData *anotherData);
556 /* Modifying data */
558 void WMReplaceDataBytesInRange(WMData *aData, WMRange aRange, void *bytes);
560 void WMResetDataBytesInRange(WMData *aData, WMRange aRange);
562 void WMSetData(WMData *aData, WMData *anotherData);
565 void WMSetDataFormat(WMData *aData, unsigned format);
567 unsigned WMGetDataFormat(WMData *aData);
568 /* Storing data */
571 /*--------------------------------------------------------------------------*/
574 WMNotification* WMCreateNotification(const char *name, void *object, void *clientData);
576 void WMReleaseNotification(WMNotification *notification);
578 WMNotification* WMRetainNotification(WMNotification *notification);
580 void* WMGetNotificationClientData(WMNotification *notification);
582 void* WMGetNotificationObject(WMNotification *notification);
584 const char* WMGetNotificationName(WMNotification *notification);
587 void WMAddNotificationObserver(WMNotificationObserverAction *observerAction,
588 void *observer, const char *name, void *object);
590 void WMPostNotification(WMNotification *notification);
592 void WMRemoveNotificationObserver(void *observer);
594 void WMRemoveNotificationObserverWithName(void *observer, const char *name,
595 void *object);
597 void WMPostNotificationName(const char *name, void *object, void *clientData);
599 WMNotificationQueue* WMGetDefaultNotificationQueue(void);
601 WMNotificationQueue* WMCreateNotificationQueue(void);
603 void WMDequeueNotificationMatching(WMNotificationQueue *queue,
604 WMNotification *notification,
605 unsigned mask);
607 void WMEnqueueNotification(WMNotificationQueue *queue,
608 WMNotification *notification,
609 WMPostingStyle postingStyle);
611 void WMEnqueueCoalesceNotification(WMNotificationQueue *queue,
612 WMNotification *notification,
613 WMPostingStyle postingStyle,
614 unsigned coalesceMask);
617 /*......................................................................*/
619 WMUserDefaults* WMGetStandardUserDefaults(void);
621 WMUserDefaults* WMGetDefaultsFromPath(char *path);
623 void WMSynchronizeUserDefaults(WMUserDefaults *database);
625 void WMSaveUserDefaults(WMUserDefaults *database);
627 void WMEnableUDPeriodicSynchronization(WMUserDefaults *database, Bool enable);
629 /* Returns a PLArray with all keys in the user defaults database.
630 * Free the returned array with PLRelease() when no longer needed,
631 * but do not free the elements of the array! They're just references. */
632 proplist_t WMGetUDAllKeys(WMUserDefaults *database);
634 proplist_t WMGetUDObjectForKey(WMUserDefaults *database, char *defaultName);
636 void WMSetUDObjectForKey(WMUserDefaults *database, proplist_t object,
637 char *defaultName);
639 void WMRemoveUDObjectForKey(WMUserDefaults *database, char *defaultName);
641 char* WMGetUDStringForKey(WMUserDefaults *database, char *defaultName);
643 int WMGetUDIntegerForKey(WMUserDefaults *database, char *defaultName);
645 float WMGetUDFloatForKey(WMUserDefaults *database, char *defaultName);
647 Bool WMGetUDBoolForKey(WMUserDefaults *database, char *defaultName);
649 void WMSetUDStringForKey(WMUserDefaults *database, char *value,
650 char *defaultName);
652 void WMSetUDIntegerForKey(WMUserDefaults *database, int value,
653 char *defaultName);
655 void WMSetUDFloatForKey(WMUserDefaults *database, float value,
656 char *defaultName);
658 void WMSetUDBoolForKey(WMUserDefaults *database, Bool value,
659 char *defaultName);
661 proplist_t WMGetUDSearchList(WMUserDefaults *database);
663 void WMSetUDSearchList(WMUserDefaults *database, proplist_t list);
665 extern char *WMUserDefaultsDidChangeNotification;
668 /*-------------------------------------------------------------------------*/
670 /* WMHost: host handling */
672 WMHost* WMGetCurrentHost();
674 WMHost* WMGetHostWithName(char* name);
676 WMHost* WMGetHostWithAddress(char* address);
678 WMHost* WMRetainHost(WMHost *hPtr);
680 void WMReleaseHost(WMHost *hPtr);
683 * Host cache management
684 * If enabled, only one object representing each host will be created, and
685 * a shared instance will be returned by all methods that return a host.
686 * Enabled by default.
688 void WMSetHostCacheEnabled(Bool flag);
690 Bool WMIsHostCacheEnabled();
692 void WMFlushHostCache();
695 * Compare hosts: Hosts are equal if they share at least one address
697 Bool WMIsHostEqualToHost(WMHost* hPtr, WMHost* anotherHost);
700 * Host names.
701 * WMGetHostName() will return first name (official) if a host has several.
702 * WMGetHostNames() will return a R/O WMArray with all the names of the host.
704 char* WMGetHostName(WMHost* hPtr);
706 /* The returned array is R/O. Do not modify it, and do not free it! */
707 WMArray* WMGetHostNames(WMHost* hPtr);
710 * Host addresses.
711 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
712 * WMGetHostAddress() will return an arbitrary address if a host has several.
713 * WMGetHostAddresses() will return a R/O WMArray with all addresses of the host.
715 char* WMGetHostAddress(WMHost* hPtr);
717 /* The returned array is R/O. Do not modify it, and do not free it! */
718 WMArray* WMGetHostAddresses(WMHost* hPtr);
721 /*-------------------------------------------------------------------------*/
723 /* WMConnection functions */
725 WMConnection* WMCreateConnectionAsServerAtAddress(char *host, char *service,
726 char *protocol);
728 WMConnection* WMCreateConnectionToAddress(char *host, char *service,
729 char *protocol);
731 WMConnection* WMCreateConnectionToAddressAndNotify(char *host, char *service,
732 char *protocol);
734 void WMCloseConnection(WMConnection *cPtr);
736 void WMDestroyConnection(WMConnection *cPtr);
738 WMConnection* WMAcceptConnection(WMConnection *listener);
740 /* Release the returned data! */
741 WMData* WMGetConnectionAvailableData(WMConnection *cPtr);
743 int WMSendConnectionData(WMConnection *cPtr, WMData *data);
745 Bool WMEnqueueConnectionData(WMConnection *cPtr, WMData *data);
747 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
749 void WMSetConnectionDelegate(WMConnection *cPtr, ConnectionDelegate *delegate);
751 /* Connection info */
753 char* WMGetConnectionAddress(WMConnection *cPtr);
755 char* WMGetConnectionService(WMConnection *cPtr);
757 char* WMGetConnectionProtocol(WMConnection *cPtr);
759 Bool WMSetConnectionNonBlocking(WMConnection *cPtr, Bool flag);
761 Bool WMSetConnectionCloseOnExec(WMConnection *cPtr, Bool flag);
763 void* WMGetConnectionClientData(WMConnection *cPtr);
765 void WMSetConnectionClientData(WMConnection *cPtr, void *data);
767 unsigned int WMGetConnectionFlags(WMConnection *cPtr);
769 void WMSetConnectionFlags(WMConnection *cPtr, unsigned int flags);
771 int WMGetConnectionSocket(WMConnection *cPtr);
773 WMConnectionState WMGetConnectionState(WMConnection *cPtr);
775 WMConnectionTimeoutState WMGetConnectionTimeoutState(WMConnection *cPtr);
777 WMArray* WMGetConnectionUnsentData(WMConnection *cPtr);
779 #define WMGetConnectionQueuedData(cPtr) WMGetConnectionUnsentData(cPtr)
783 * Passing timeout==0 in the SetTimeout functions below, will reset that
784 * timeout to its default value.
787 /* The default timeout inherited by all WMConnection operations, if none set */
788 void WMSetConnectionDefaultTimeout(unsigned int timeout);
790 /* Global timeout for all WMConnection objects, for opening a new connection */
791 void WMSetConnectionOpenTimeout(unsigned int timeout);
793 /* Connection specific timeout for sending out data */
794 void WMSetConnectionSendTimeout(WMConnection *cPtr, unsigned int timeout);
797 /* Global variables */
799 extern int WCErrorCode;
802 /*-------------------------------------------------------------------------*/
806 #ifdef __cplusplus
808 #endif /* __cplusplus */
811 #endif