swk

static widget kit
git clone git://git.suckless.org/swk
Log | Files | Refs | README | LICENSE

swk.h (4146B)


      1 /* See LICENSE file for copyright and license details. */
      2 
      3 #define IS_SCROLLBOX(b) (b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0)
      4 #define SWK_BOX_NEWLINE(x) { .data=(void*)(size_t)x, .r.w=-1, .r.h=-1, .cb = swk_filler }
      5 #define SWK_BOX_VFILL(x) { .data=(void*)(size_t)x, .r.w=-1, .r.h=-2, .cb = swk_filler }
      6 #define SWK_HIT(r,p) (p.x>=r.x && p.x<(r.x+r.w) && p.y>=r.y && p.y<(r.y+r.h))
      7 
      8 typedef enum { EVoid, EClick, EMotion, EKey, EExpose, EQuit, ELast } SwkEventType;
      9 typedef enum { Shift=1, Ctrl=2, Alt=4, Meta=8 } SwkKeyMod;
     10 typedef enum { ColorFG, ColorBG, ColorHI, ColorTF, ColorCC, ColorLast } Palete;
     11 typedef enum { KDel=8, KSupr=127, KUp=0xe0, KDown=0xe1, KLeft=0xe2, KRight=0xe3 } SwkKeyCode;
     12 
     13 typedef struct SwkBox SwkBox;
     14 typedef struct SwkEvent SwkEvent;
     15 typedef struct SwkWindow SwkWindow;
     16 typedef void (*SwkEventCallback)(SwkEvent *e);
     17 typedef void (*SwkKeyCallback)();
     18 
     19 typedef struct {
     20 	int x;
     21 	int y;
     22 } Point;
     23 
     24 typedef struct {
     25 	int x;
     26 	int y;
     27 	int w;
     28 	int h;
     29 } Rect;
     30 
     31 typedef struct {
     32 	int button;
     33 	int modmask;
     34 	Point point;
     35 } Click;
     36 
     37 typedef struct {
     38 	int modmask;
     39 	int keycode;
     40 } Key;
     41 
     42 typedef struct {
     43 	int modmask;
     44 	int keycode;
     45 	SwkKeyCallback cb;
     46 } SwkKeyBind;
     47 
     48 struct SwkEvent {
     49 	SwkEventType type;
     50 	SwkBox *box;
     51 	SwkWindow *win;
     52 	union {
     53 		Click click;
     54 		Point motion;
     55 		Key key;
     56 		Rect expose;
     57 		int rows;
     58 	} data;
     59 }; 
     60 
     61 struct SwkBox {
     62 	Rect r;
     63 	SwkEventCallback cb;
     64 	char *text;
     65 	void *data;
     66 	int scroll;
     67 };
     68 
     69 struct SwkWindow {
     70 	char *title;
     71 	SwkEventCallback cb;
     72 	Rect r;
     73 	SwkBox *boxes[2];
     74 	int col;
     75 	int colpos;
     76 	/* internal use */
     77 	SwkBox *box;
     78 	SwkEvent _e;
     79 };
     80 
     81 typedef struct {
     82 	char name[32];
     83 	int ref;
     84 	void *data;
     85 	int bpp;
     86 	int w;
     87 	int h;
     88 	void *priv;
     89 	void *pub;
     90 } SwkImage;
     91 
     92 int swk_use(SwkWindow *w);
     93 void swk_update();
     94 void swk_exit();
     95 void swk_fit();
     96 void swk_loop();
     97 SwkEvent *swk_next_event();
     98 void swk_handle_event(SwkEvent *e);
     99 
    100 void swk_focus_first();
    101 void swk_focus_next();
    102 void swk_focus_prev();
    103 void swk_focus_activate();
    104 void swk_scroll_up();
    105 void swk_scroll_down();
    106 void swk_fontsize_increase();
    107 void swk_fontsize_decrease();
    108 void swk_column_move_left();
    109 void swk_column_move_right();
    110 
    111 void swk_button(SwkEvent *e);
    112 void swk_bigbutton(SwkEvent *e);
    113 void swk_label(SwkEvent *e);
    114 void swk_entry(SwkEvent *e);
    115 void swk_password(SwkEvent *e);
    116 void swk_filler(SwkEvent *e);
    117 void swk_option(SwkEvent *e);
    118 void swk_separator(SwkEvent *e);
    119 void swk_progress(SwkEvent *e);
    120 void swk_image(SwkEvent *e);
    121 void swk_sketch(SwkEvent *e);
    122 
    123 /* graphic backend */
    124 
    125 int swk_gi_init(SwkWindow *w);
    126 void swk_gi_exit();
    127 SwkEvent *swk_gi_event(SwkWindow *w, int dowait);
    128 int swk_gi_update(SwkWindow *w);
    129 int swk_gi_fontsize(int sz);
    130 
    131 /* FIXME: don't these need SwkWindow *w state, to avoid static'ness? */
    132 void swk_gi_clear();
    133 void swk_gi_flip();
    134 
    135 void swk_gi_line(int x1, int y1, int x2, int y2, int color);
    136 void swk_gi_fill(Rect r, int color, int lil);
    137 void swk_gi_rect(Rect r, int color);
    138 void swk_gi_text(Rect r, const char *t);
    139 
    140 /* images */
    141 void swk_gi_img(Rect r, void *img);
    142 void* swk_gi_img_new(int w, int h, int color);
    143 void* swk_gi_img_load(const char *str);
    144 void swk_gi_img_free(void *s);
    145 void swk_gi_img_set(void *img, int x, int y, int color);
    146 int swk_gi_img_get(void *img, int x, int y);
    147 
    148 /* text.c */
    149 typedef struct {
    150 	const char *otext;
    151 	int cur;
    152 	int xcur;
    153 	int ycur;
    154 	char *text;
    155 	int len;
    156 	int size;
    157 	int yscroll;
    158 	int sel[2];
    159 	int selmode;
    160 } Text;
    161 
    162 int text_rowcount(Text *t);
    163 int text_rowoff(Text *t, int row);
    164 int text_rowcol(Text *t, int off, int *col);
    165 int text_off(Text *t, int col, int row);
    166 char * text_sub(Text *t, int col, int row, int rows);
    167 void text_init(Text *t, const char *text);
    168 void text_set(Text *t, const char *text);
    169 char * text_get(Text *t, int from, int to);
    170 void text_sync(Text *t);
    171 void text_cur(Text *t, int num, int dir);
    172 void text_ins(Text *t, const char *str, int app);
    173 void text_insc(Text *t, char ch, int app);
    174 void text_del(Text *t, int num, int dir);
    175 void text_sel(Text *t, int begin, int end);
    176 void text_sel_mode(Text *t, int enable);
    177 
    178 /* text.c widgets */
    179 void swk_text(SwkEvent *e);
    180 SwkImage *img_open(const char *str);
    181 void img_free(SwkImage *img);