[hackers] [swk] Single global SwkWindow || pancake

From: <hg_AT_suckless.org>
Date: Sat, 15 May 2010 11:30:33 +0000 (UTC)

changeset: 44:f76c0a387780
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Sat May 15 13:24:31 2010 +0200
files: TODO swk.c swk.h t/test.c t/ui.c
description:
Single global SwkWindow
w->running is moved outside window
swk_init is now named swk_use()

diff -r 50df6f6e2a6c -r f76c0a387780 TODO
--- a/TODO Sun May 09 04:37:48 2010 +0200
+++ b/TODO Sat May 15 13:24:31 2010 +0200
@@ -1,15 +1,25 @@
 TODO
 ====
+ * receive fine-grained x,y in Point? for sketch or imaging stuff is important
+ * support for clipboard (implemented in gi_ backend)
+ * simple way to define callback for buttons instead of reimplementing widget (?)
+ * full text editing widget (multiline, cursor support, text format, ...)
+ * scroll on focus (if focused widget is not in screen, just scroll there..)
+ * mouse bindings in config.h (which mouse bindings we need? wheel to scroll?)
+ * allow widgets to force its own size (height, width)
+ * add support to resize images (scaling)
+ * only focus buttons and entries (input widgets)
+ * add function to click button 1, 2, 3, ... // clicking from code..
+ * allow to load images from memory instead of files
+ * default image path (?)
+ * transitions (to change from one window to another)
 
-* receive fine-grained x,y in Point? for sketch or imaging stuff is important
-* support for clipboard (implemented in gi_ backend)
-* simple way to define callback for buttons instead of reimplementing widget (?)
-* full text editing widget (multiline, cursor support, text format, ...)
-* scroll on focus (if focused widget is not in screen, just scroll there..)
-* mouse bindings in config.h (which mouse bindings we need? wheel to scroll?)
-* allow widgets to force its own size (height, width)
-* add support to resize images (scaling)
-* only focus buttons and entries (input widgets)
-* add function to click button 1, 2, 3, ... // clicking from code..
-* allow to load images from memory instead of files
-* default image path (?)
+USABILITY GUIDELINE
+===================
+ * useful
+ * usable
+ * transparent
+ * either discoverable or well-documented
+ * reliable
+ * as simple as possible, and no simpler
+ * customizable
diff -r 50df6f6e2a6c -r f76c0a387780 swk.c
--- a/swk.c Sun May 09 04:37:48 2010 +0200
+++ b/swk.c Sat May 15 13:24:31 2010 +0200
@@ -5,24 +5,27 @@
 #include "swk.h"
 #include "config.h"
 
+static SwkWindow *w = NULL;
+static int running = 0;
+
 int
-swk_init(SwkWindow *w) {
- w->_e.win = w;
- if(w->box == NULL)
+swk_use(SwkWindow *win) {
+ w = win = win->_e.win = win;
+ if(win->box == NULL)
                 swk_focus_first(w);
         if(w->r.w == 0 || w->r.h == 0) {
                 w->r.w = WINWIDTH;
                 w->r.h = WINHEIGHT;
         }
- if(swk_gi_init(w)) {
- w->running = 1;
- swk_update(w);
- }
- return w->running;
+ if(!running && !swk_gi_init(w))
+ return 0;
+ running = 1;
+ swk_update(w);
+ return 1;
 }
 
 void
-swk_update(SwkWindow *w) {
+swk_update() {
         char text[8];
         int roy, oy, scroll = 0;
         w->_e.type = EExpose;
@@ -51,16 +54,16 @@
                         oy = b->r.y;
                 }
                 swk_gi_flip();
- } else w->running = 0;
+ } else running = 0;
 }
 
 void
-swk_exit(SwkWindow *w) {
- w->running = 0;
+swk_exit() {
+ running = 0;
 }
 
 void
-swk_loop(SwkWindow *w) {
+swk_loop() {
         SwkEvent *e;
         do {
                 if((e = swk_next_event(w)))
@@ -69,20 +72,20 @@
 }
 
 void
-swk_fontsize_increase(SwkWindow *w) {
+swk_fontsize_increase() {
         swk_gi_fontsize(1);
         swk_update(w);
 }
 
 void
-swk_fontsize_decrease(SwkWindow *w) {
+swk_fontsize_decrease() {
         swk_gi_fontsize(-1);
         swk_update(w);
 }
 
 
 static void
-setscrollbox(SwkWindow *w, int delta) {
+setscrollbox(int delta) {
         SwkBox *r = NULL;
         SwkBox *b = w->boxes;
         for(; b->cb; b++) {
@@ -95,16 +98,16 @@
 }
 
 void
-swk_scroll_up(SwkWindow *w) {
- setscrollbox(w, 2);
+swk_scroll_up() {
+ setscrollbox(2);
 }
 
 void
-swk_scroll_down(SwkWindow *w) {
- setscrollbox(w, -2);
+swk_scroll_down() {
+ setscrollbox(-2);
 }
 
-static void swk_fit_row(SwkWindow *w, SwkBox *a, SwkBox *b, int y) {
+static void swk_fit_row(SwkBox *a, SwkBox *b, int y) {
         SwkBox *btmp;
         int count = 0, x = 0;
         for(btmp=a; btmp<b; btmp++)
@@ -133,37 +136,37 @@
 }
 
 void
-swk_fit(SwkWindow *w) {
+swk_fit() {
         SwkBox *b, *b2;
         int x, y = 0, skip = 0;
         for(b=b2=w->boxes; b->cb; b++) {
                 if(b->r.w==-1 && b->r.h==-1) {
                         x = (int)(size_t)b->data;
- swk_fit_row(w, b2, b, y);
+ swk_fit_row(b2, b, y);
                         y += x-skip;
                         // vertical align //
- if(x<0) y+=w->r.h-countrows(b2);
+ if(x<0) y += w->r.h-countrows(b2);
                         b2 = b+1;
                 }
                 y += b->scroll;
         }
- swk_fit_row(w, b2, b, y);
+ swk_fit_row(b2, b, y);
 }
 
 int
-swk_has_event(SwkWindow *w) {
+swk_has_event() { // XXX: remove this useless wrap
         return swk_gi_has_event(w);
 }
 
 void
-swk_focus_activate(SwkWindow *w) {
+swk_focus_activate() {
         w->_e.box = w->box;
         w->_e.type = EClick;
 }
 
 SwkEvent *
-swk_next_event(SwkWindow *w) {
- if(w->running)
+swk_next_event() {
+ if(running)
                 return swk_gi_event(w, 1);
         w->_e.type = EQuit;
         w->_e.win = w;
@@ -239,7 +242,7 @@
 }
 
 void
-swk_focus_first(SwkWindow *w) {
+swk_focus_first() {
         w->box = w->boxes;
         while(w->box->cb == swk_filler)
                 w->box++;
@@ -248,7 +251,7 @@
 }
 
 void
-swk_focus_next(SwkWindow *w) {
+swk_focus_next() {
         w->box++;
         if(w->box->cb == NULL)
                 w->box = w->boxes;
@@ -259,7 +262,7 @@
 }
 
 void
-swk_focus_prev(SwkWindow *w) {
+swk_focus_prev() {
         if(w->box == w->boxes) {
                 while(w->box->cb)
                         w->box++;
diff -r 50df6f6e2a6c -r f76c0a387780 swk.h
--- a/swk.h Sun May 09 04:37:48 2010 +0200
+++ b/swk.h Sat May 15 13:24:31 2010 +0200
@@ -13,7 +13,7 @@
 typedef struct SwkEvent SwkEvent;
 typedef struct SwkWindow SwkWindow;
 typedef void (*SwkEventCallback)(SwkEvent *e);
-typedef void (*SwkKeyCallback)(SwkWindow *w);
+typedef void (*SwkKeyCallback)();
 
 typedef struct {
         int x;
@@ -67,7 +67,6 @@
 
 struct SwkWindow {
         char *title;
- int running;
         SwkEventCallback cb;
         Rect r;
         SwkBox *boxes;
@@ -76,23 +75,23 @@
         SwkEvent _e;
 };
 
-int swk_init(SwkWindow *w);
-void swk_update(SwkWindow *w);
-void swk_exit(SwkWindow *w);
-void swk_fit(SwkWindow *w);
-void swk_loop(SwkWindow *w);
-SwkEvent *swk_next_event(SwkWindow *w);
-int swk_has_event(SwkWindow *w);
+int swk_use(SwkWindow *w);
+void swk_update();
+void swk_exit();
+void swk_fit();
+void swk_loop();
+SwkEvent *swk_next_event();
+int swk_has_event();
 void swk_handle_event(SwkEvent *e);
 
-void swk_focus_first(SwkWindow *w);
-void swk_focus_next(SwkWindow *w);
-void swk_focus_prev(SwkWindow *w);
-void swk_focus_activate(SwkWindow *w);
-void swk_scroll_up(SwkWindow *w);
-void swk_scroll_down(SwkWindow *w);
-void swk_fontsize_increase(SwkWindow *w);
-void swk_fontsize_decrease(SwkWindow *w);
+void swk_focus_first();
+void swk_focus_next();
+void swk_focus_prev();
+void swk_focus_activate();
+void swk_scroll_up();
+void swk_scroll_down();
+void swk_fontsize_increase();
+void swk_fontsize_decrease();
 
 void swk_button(SwkEvent *e);
 void swk_label(SwkEvent *e);
diff -r 50df6f6e2a6c -r f76c0a387780 t/test.c
--- a/t/test.c Sun May 09 04:37:48 2010 +0200
+++ b/t/test.c Sat May 15 13:24:31 2010 +0200
@@ -152,8 +152,8 @@
         .cancel=cb
 */
         };
- if(!swk_init(&w))
+ if(!swk_use(&w))
                 return 1;
- swk_loop(&w);
+ swk_loop();
         return 0;
 }
diff -r 50df6f6e2a6c -r f76c0a387780 t/ui.c
--- a/t/ui.c Sun May 09 04:37:48 2010 +0200
+++ b/t/ui.c Sat May 15 13:24:31 2010 +0200
@@ -173,8 +173,7 @@
                 }
                 text++;
         }
- w->running = 1;
- swk_init(w);
+ swk_use(w);
         return w;
 }
 
@@ -191,7 +190,7 @@
 
 int main() {
         w = swk_ui(UI);
- if(!w||!swk_init(w))
+ if(!w||!swk_use(w))
                 return 1;
         swk_loop(w);
         return 0;
Received on Sat May 15 2010 - 11:30:33 UTC

This archive was generated by hypermail 2.2.0 : Sat May 15 2010 - 11:36:04 UTC