AUTO_LT_SYNC
[tore.git] / pytx / txobj.c
blobdfc9cbfe6fe880517cdf739dffbcbb61fba498a7
1 #include <panel.h>
2 #include <ncurses.h>
3 #include <assert.h>
4 #include <string.h>
5 #include "txops.h"
6 #include "txobj.h"
7 #include "txlist.h"
8 #include "txbox.h"
10 TXObj *tx_obj_focus;
11 TXObj *tx_obj_root;
13 static int getc_cb(TXObj *obj) {
14 return wgetch(obj->win);
17 void tx_obj_size_cb_set(TXObj *obj, size_fn size_cb) {
18 obj->size_cb = size_cb;
21 void tx_obj_idle_cb_set(TXObj *obj, obj_fn idle_cb) {
22 obj->idle_cb = idle_cb;
25 void tx_obj_getc_cb_set(TXObj *obj, getc_fn getc_cb) {
26 obj->getc_cb = getc_cb;
29 void tx_obj_rsze_cb_set(TXObj *obj, obj_fn rsze_cb) {
30 obj->rsze_cb = rsze_cb;
33 /* apply cb to a box then each object in the box else apply cb to itself */
34 void tx_obj_map(TXObj *obj, obj_fn cb) {
35 TXObj *entry;
36 if (obj->type == TX_BOX) {
37 cb(obj);
38 TX_EXT(TXBox, box);
39 list_for_each_entry(entry, &box->node, node) {
40 tx_obj_map(entry, cb);
42 return;
44 cb(obj);
47 /* setup background window for border and static text, ie titles */
48 void tx_obj_setup(TXObj *obj) {
49 if (obj->bwin) {
50 wresize(obj->bwin, obj->h, obj->w);
51 mvwin(obj->bwin, obj->y, obj->x);
52 return;
54 obj->bwin = newwin(obj->h, obj->w, obj->y, obj->x);
55 obj->bpanel = new_panel(obj->bwin);
58 void tx_obj_size_set(TXObj *obj, int x, int y, int w, int h) {
59 obj->x = x; obj->y = y;
60 obj->w = w; obj->h = h;
63 void tx_obj_border(TXObj *obj) {
64 tx_box(obj->bwin);
65 tx_obj_size_set(obj, obj->x+1, obj->y+1, obj->w-2, obj->h-2);
68 /* callback funcs */
69 void tx_obj_idle(TXObj *obj) {
70 if (obj->idle_cb) obj->idle_cb(obj);
73 void tx_obj_draw(TXObj *obj) {
74 obj->draw_cb(obj);
75 update_panels();
76 doupdate();
79 void tx_obj_resize(TXObj *obj) {
80 if (obj->flags & TX_OBJ_ROOT) {
81 tx_obj_size_set(obj, 0, 0, COLS, LINES);
82 /* this allows the object to set the size if root is not in a box*/
83 if (obj->type != TX_BOX) obj->size_cb(obj, obj->h, obj->w);
86 /* setup back window */
87 tx_obj_setup(obj);
88 /* draw border on back window */
89 if (obj->flags & TX_OBJ_BORDER) tx_obj_border(obj);
91 /* let object routines do whatever needs to be done */
93 /* setup win object and draw on background, ie titles */
94 if (obj->init_cb) obj->init_cb(obj);
96 /* user defined routines, ie setup columns */
97 if (obj->rsze_cb) obj->rsze_cb(obj);
100 void tx_obj_reset(TXObj *obj) {
101 /* lets start fresh */
102 getmaxyx(obj->bwin, obj->h, obj->w);
103 getbegyx(obj->bwin, obj->y, obj->x);
104 tx_obj_resize(obj);
107 /* object flag manipluation */
108 void tx_obj_flag_set(TXObj *obj, int flag, int set) {
109 if (set) {
110 obj->flags |= flag;
111 return;
113 obj->flags &= ~flag;
116 int tx_obj_flag_get(TXObj *obj, int flag) {
117 return obj->flags & flag;
120 int tx_obj_flag_toggle(TXObj *obj, int flag) {
121 int set = (obj->flags & flag) ? 0 : 1;
122 tx_obj_flag_set(obj, flag, set);
123 return set;
126 void tx_obj_operate(TXObj *obj) {
127 halfdelay(10);
128 //timeout(0);
129 int key;
130 while (1) {
131 switch(key = obj->getc_cb(obj)) {
132 case 'q': return;
133 case EMERGENCY: tx_free(); return;
134 case ERR: tx_obj_map(tx_obj_root, tx_obj_idle); break;
135 case KEY_RESIZE: tx_obj_map(tx_obj_root, tx_obj_resize); break;
136 default: if (obj->oper_cb) obj->oper_cb(obj, key); break;
141 int tx_obj_init(TXObj *obj, int type, int flags) {
142 memset(obj, 0, sizeof(TXObj));
144 obj->flags = flags;
145 obj->type = type;
147 obj->getc_cb = getc_cb;
149 switch (type) {
150 case TX_LIST: tx_list_new(obj); break;
151 case TX_BOX: tx_box_init(obj); break;
152 default: return 0; break;
155 /* make inital object fill the screen */
156 if (flags & TX_OBJ_ROOT)
157 tx_obj_root = obj;
159 if (flags & TX_OBJ_FOCUS)
160 tx_obj_focus = obj;
162 return 1;