Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Fl_Signal.h

00001 /*
00002  * $Id: Fl_Signal.h,v 1.4 2003/07/03 20:36:35 laza2000 Exp $
00003  *
00004  * Extended Fast Light Toolkit (EFLTK)
00005  * Copyright (C) 2002-2003 by EDE-Team
00006  * WWW: http://www.sourceforge.net/projects/ede
00007  *
00008  * Fast Light Toolkit (FLTK)
00009  * Copyright (C) 1998-2003 by Bill Spitzak and others.
00010  * WWW: http://www.fltk.org
00011  *
00012  * This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00013  * version 2. See COPYING for details.
00014  *
00015  * Author : Mikko Lahteenmaki
00016  * Email  : mikko@fltk.net
00017  *
00018  * Please report all bugs and problems to "efltk-bugs@fltk.net"
00019  *
00020  */
00021 
00022 #ifndef _FL_SIGNAL_H_
00023 #define _FL_SIGNAL_H_
00024 
00025 #include <stdlib.h>
00026 
00027 typedef struct Fl_Signal_Func_Struct {
00028     Fl_Signal_Callback *func;   //pointer to callback
00029     void *obj_ptr;              //pointer to receiver object
00030     int event;                  //event we accept
00031     Fl_Signal_Func_Struct *next;//pointer to next function in signal
00032 } Fl_Signal_Func;
00033 
00034 // Fl_Signal structure.
00035 // Handles multiple callbacks.
00036 typedef struct FL_API Fl_Signal_Struct
00037 {
00038     Fl_Signal_Struct()  { first = 0; }
00039     ~Fl_Signal_Struct() { disconnect_all(); }
00040 
00041     // Remove all connected callbacks
00042     void disconnect_all() {
00043         for(Fl_Signal_Func *cb = first; cb;) { 
00044             Fl_Signal_Func *delcb=cb; cb = cb->next; free(delcb); 
00045         }
00046         first = 0;
00047     }
00048     
00049     // Connect callback 'func' with class object pointer 'obj_ptr'
00050     // If 'obj_ptr' is NULL, func is assumed to be normal (e)FLTK style callback function.
00051     // Called by FL_CONNECT_* macros internally.
00052     void connect(Fl_Signal_Callback *func, void *obj_ptr, int event=0) {
00053         Fl_Signal_Func *cb = (Fl_Signal_Func *)malloc(sizeof(Fl_Signal_Func));
00054         cb->next = first;       
00055         first = cb;
00056         cb->event   = event;
00057         cb->func    = func;
00058         cb->obj_ptr = obj_ptr;
00059     }
00060 
00061     // Disconnect callback 'func' with object pointer 'obj_ptr'
00062     void disconnect(Fl_Signal_Callback *func, void *obj_ptr, int event=0) {
00063         for(Fl_Signal_Func **p = &first; *p;) {
00064             Fl_Signal_Func *cb = *p;
00065             if(cb->func == func && cb->obj_ptr == obj_ptr) {
00066                 *p = cb->next;
00067                 free(cb);               
00068             } else {
00069                 p = &(cb->next);
00070             }
00071         }
00072     }
00073     
00074     // Call all connected callbacks. 
00075     // That is - emit signal    
00076     int emit(int event, void *caller, void *user_data, void *opt_data) {
00077         int cnt = 0;
00078         for(Fl_Signal_Func *cb = first; cb; cb=cb->next) {
00079             if(cb->event!=event) continue;
00080             cnt++;
00081             //void *caller, void *user_data, int event, void *opt_data, void *receiver 
00082             if(cb->obj_ptr) cb->func(caller, user_data, event, opt_data, cb->obj_ptr);
00083             else            cb->func(caller, user_data, event, opt_data, 0);
00084         }
00085         return cnt; // return 0 if no event handlers were found
00086     }
00087 
00088 private:
00089     Fl_Signal_Func *first;
00090 } Fl_Signal;
00091 
00092 // All slot types:
00093 // void slot(CallerClass *caller, int event, type user_data, type opt_data)
00094 // void slot(CallerClass *caller, int event, type opt_data)
00095 // void slot(CallerClass *caller, type user_data, type opt_data)
00096 // void slot(CallerClass *caller, type opt_data)
00097 // void slot(CallerClass *caller, type user_data)
00098 // void slot(CallerClass *caller, int event)
00099 // void slot(CallerClass *caller)
00100 // void slot()
00101 
00102 //void slot(CallerClass *caller, int event, void *opt_data, void *user_data)
00103 #define DEFSLOT_CEUO(CallerClass, ReceiverClass, CB, userdata_type, opt_type) \
00104 static inline void cb_i_##CB(void *caller, void *user_data, int event, void *opt_data, void *receiver) { \
00105     ((ReceiverClass*)receiver)->CB((CallerClass*)caller, event, (userdata_type)user_data, (opt_type)opt_data); \
00106 }
00107 
00108 //void slot(CallerClass *caller, int event, void *opt_data)
00109 #define DEFSLOT_CEO(CallerClass, ReceiverClass, CB, opt_type) \
00110 static inline void cb_i_##CB(void *caller, void *, int event, void *opt_data, void *receiver) { \
00111     ((ReceiverClass*)receiver)->CB((CallerClass*)caller, event, (opt_type)opt_data); \
00112 }
00113 
00114 //void slot(CallerClass *caller, void *user_data, void *opt_data)
00115 #define DEFSLOT_CUO(CallerClass, ReceiverClass, CB, userdata_type, opt_type) \
00116 static inline void cb_i_##CB(void *caller, void *user_data, int, void *opt_data, void *receiver) { \
00117     ((ReceiverClass*)receiver)->CB((CallerClass*)caller, (userdata_type)user_data, (opt_type)opt_data); \
00118 }
00119 
00120 //void slot(CallerClass *caller, void *opt_data)
00121 #define DEFSLOT_CO(CallerClass, ReceiverClass, CB, opt_type) \
00122 static inline void cb_i_##CB(void *caller, void *, int, void *opt_data, void *receiver) { \
00123     ((ReceiverClass*)receiver)->CB((CallerClass*)caller, (opt_type)opt_data); \
00124 }
00125 
00126 //void slot(CallerClass *caller, void *user_data)
00127 #define DEFSLOT_CU(CallerClass, ReceiverClass, CB, userdata_type) \
00128 static inline void cb_i_##CB(void *caller, void *user_data, int, void *, void *receiver) { \
00129     ((ReceiverClass*)receiver)->CB((CallerClass*)caller, (userdata_type)user_data); \
00130 }
00131 
00132 //void slot(CallerClass *caller, int event)
00133 #define DEFSLOT_CE(CallerClass, ReceiverClass, CB) \
00134 static inline void cb_i_##CB(void *caller, void *, int event, void *, void *receiver) { \
00135     ((ReceiverClass*)receiver)->CB((CallerClass*)caller, event); \
00136 }
00137 
00138 //void slot(CallerClass *caller)
00139 #define DEFSLOT_C(CallerClass, ReceiverClass, CB) \
00140 static inline void cb_i_##CB(void *caller, void *, int, void *, void *receiver) { \
00141     ((ReceiverClass*)receiver)->CB((CallerClass*)caller); \
00142 }
00143 
00144 //void slot(CallerClass *caller)
00145 #define DEFSLOT_O(CallerClass, ReceiverClass, CB, opt_type) \
00146 static inline void cb_i_##CB(void *, void *, int, void *opt_data, void *receiver) { \
00147     ((ReceiverClass*)receiver)->CB((opt_type)opt_data); \
00148 }
00149 
00150 //void slot()
00151 #define DEFSLOT(CallerClass, ReceiverClass, CB) \
00152 static inline void cb_i_##CB(void *, void *, int, void *, void *receiver) { \
00153     ((ReceiverClass*)receiver)->CB(); \
00154 }
00155 
00156 // Member function slot
00157 #define SLOT(Receiver, CB) Receiver, (Fl_Signal_Callback*)(Receiver)->cb_i_##CB
00158 
00159 // Static function slot
00160 #define STATIC_SLOT(CB) 0, (Fl_Signal_Callback*)CB
00161 
00162 #endif

Generated on Thu Jul 31 15:33:45 2003 for eFLTK by doxygen1.2.15