00001 #ifndef __OBSERVER_H_
00002 #define __OBSERVER_H_
00003
00004 #include "mb_tools.h"
00005
00006 typedef struct _event event_t;
00007 typedef struct _observer observer_t;
00008 typedef struct _subject subject_t;
00009 typedef struct _mouse_event mouse_event_t;
00010 typedef struct _ob_factory ob_factory_t;
00011 typedef void (*evt_handler)(event_t *event, void *arg);
00012
00013 struct _event {
00014 int type;
00015 subject_t *tgt, *cur_tgt;
00016 };
00017
00018
00019
00020
00021
00022 struct _observer {
00023 evt_handler hdr;
00024 void *arg;
00025 observer_t *next;
00026 };
00027
00028
00029
00030
00031
00032
00033
00034 struct _subject {
00035 int obj_type;
00036 void *obj;
00037 int flags;
00038 STAILQ(observer_t) observers;
00039 };
00040
00041 #define SUBF_STOP_PROPAGATE 0x1
00042 #define SUBF_BUSY 0x2
00043 #define SUBF_FREE 0x4
00044
00045 enum {OBJT_GEO, OBJT_COORD, OBJT_KB, OBJT_PROGM, OBJT_RDMAN};
00046
00047 struct _mouse_event {
00048 event_t event;
00049 int x, y;
00050 unsigned int but_state;
00051 unsigned int button;
00052 };
00053
00054 #define MOUSE_BUT1 0x1
00055 #define MOUSE_BUT2 0x2
00056 #define MOUSE_BUT3 0x4
00057
00058
00059
00060
00061
00062
00063 struct _ob_factory {
00064 subject_t *(*subject_alloc)(ob_factory_t *factory);
00065 void (*subject_free)(ob_factory_t *factory, subject_t *subject);
00066 observer_t *(*observer_alloc)(ob_factory_t *factory);
00067 void (*observer_free)(ob_factory_t *factory, observer_t *observer);
00068
00069 subject_t *(*get_parent_subject)(ob_factory_t *factory,
00070 subject_t *cur_subject);
00071 };
00072
00073 enum {EVT_MOUSE_OVER, EVT_MOUSE_OUT, EVT_MOUSE_MOVE,
00074 EVT_MOUSE_BUT_PRESS, EVT_MOUSE_BUT_RELEASE,
00075 EVT_KB_PRESS, EVT_KB_RELEASE, EVT_PROGM_COMPLETE,
00076 EVT_RDMAN_REDRAW };
00077
00078 extern subject_t *subject_new(ob_factory_t *factory,
00079 void *obj, int obj_type);
00080 extern void subject_free(ob_factory_t *factory, subject_t *subject);
00081 extern void subject_notify(ob_factory_t *factory,
00082 subject_t *subject, event_t *evt);
00083 extern observer_t *subject_add_observer(ob_factory_t *factory,
00084 subject_t *subject,
00085 evt_handler hdr, void *arg);
00086 extern void subject_remove_observer(ob_factory_t *factory,
00087 subject_t *subject,
00088 observer_t *observer);
00089
00090
00091 #endif