[hackers] [swk] add null pointer check in putpixel() || pancake

From: <hg_AT_suckless.org>
Date: Thu, 6 May 2010 23:06:11 +0000 (UTC)

changeset: 34:c6a17f2bc350
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Fri May 07 00:59:57 2010 +0200
files: README gi_sdl.c swk.c swk.h test.c
description:
add null pointer check in putpixel()
add swk_gi_img_new() to get blank surface
display text cursor line in swk_entry
initial nonworking implementation of swk_sketch

diff -r 9d527f82d3fe -r c6a17f2bc350 README
--- a/README Thu May 06 10:18:56 2010 +0200
+++ b/README Fri May 07 00:59:57 2010 +0200
@@ -12,6 +12,7 @@
 
 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
 * scroll on focus
diff -r 9d527f82d3fe -r c6a17f2bc350 gi_sdl.c
--- a/gi_sdl.c Thu May 06 10:18:56 2010 +0200
+++ b/gi_sdl.c Fri May 07 00:59:57 2010 +0200
@@ -31,6 +31,7 @@
 
 static void putpixel(SDL_Surface *scr, int x, int y, Uint32 pixel) {
         Uint8 *p = getscrpoint(scr, x, y);
+ if(!p) return;
 #if BPP == 8
         *p = pixel;
 #elif BPP == 16
@@ -272,7 +273,6 @@
         free(ptr);
 }
 
-/* images */
 void
 swk_gi_img(Rect r, void *img) {
         SDL_Surface *s = (SDL_Surface*)img;
@@ -280,6 +280,12 @@
         if(s) SDL_BlitSurface(s, NULL, screen, &area);
 }
 
+/* image api */
+void*
+swk_gi_img_new(int w, int h, int color) {
+ return SDL_CreateRGBSurface(NULL, w, h, BPP, 0, 0, 0, 0);
+}
+
 void*
 swk_gi_img_load(const char *str) {
         return IMG_Load(str);
diff -r 9d527f82d3fe -r c6a17f2bc350 swk.c
--- a/swk.c Thu May 06 10:18:56 2010 +0200
+++ b/swk.c Fri May 07 00:59:57 2010 +0200
@@ -24,8 +24,7 @@
 void
 swk_update(SwkWindow *w) {
         char text[8];
- int roy, oy;
- int scroll = 0;
+ int roy, oy, scroll = 0;
         w->_e.type = EExpose;
         if(swk_gi_update(w)) {
                 SwkBox *b = w->boxes;
@@ -106,9 +105,8 @@
 }
 
 static void swk_fit_row(SwkWindow *w, SwkBox *a, SwkBox *b, int y) {
- int count, x = 0;
         SwkBox *btmp;
- count = 0;
+ int count = 0, x = 0;
         for(btmp=a; btmp<b; btmp++)
                 count++;
         if(count) {
@@ -136,9 +134,8 @@
 
 void
 swk_fit(SwkWindow *w) {
- int skip = 0;
- int x, y = 0;
         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;
@@ -288,9 +285,9 @@
         switch(e->type) {
         case EExpose:
                 r = e->box->r;
- r.w+=6;
+ r.w += 6;
                 swk_gi_text(r, e->box->text);
- r.w-=6;
+ r.w -= 6;
                 if(e->win->box == e->box)
                         swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI);
                 break;
@@ -301,9 +298,9 @@
 
 void
 swk_password(SwkEvent *e) {
+ char *str, *ptr;
         int len;
         Rect r;
- char *str, *ptr;
         switch(e->type) {
         case EExpose:
                 r = e->box->r;
@@ -354,6 +351,12 @@
         default:
                 swk_label(e);
                 break;
+ case EExpose:
+ // XXX: add support for cursor (handle arrow keys)
+ len = e->box->r.x+(strlen(e->box->text)*0.6);
+ swk_label(e);
+ swk_gi_line(len, e->box->r.y, 0, 1, ColorFG);
+ break;
         }
 }
 
@@ -473,3 +476,23 @@
                 break;
         }
 }
+
+void
+swk_sketch(SwkEvent *e) {
+ if(e->box->data == NULL)
+ e->box->data = swk_gi_img_new(e->box->r.w, e->box->r.h, ColorHI);
+ switch(e->type) {
+ case EClick:
+ swk_gi_img_set(e->box->data,
+ e->data.click.point.x, e->data.click.point.y,
+ ColorFG);
+ printf("CLICKED %p %d %d\n", e->box->data, e->data.click.point.x, e->data.click.point.y);
+ break;
+ case EExpose:
+ swk_gi_img(e->box->r, e->box->data);
+ swk_gi_rect(e->box->r, ColorFG);
+ break;
+ default:
+ break;
+ }
+}
diff -r 9d527f82d3fe -r c6a17f2bc350 swk.h
--- a/swk.h Thu May 06 10:18:56 2010 +0200
+++ b/swk.h Fri May 07 00:59:57 2010 +0200
@@ -103,6 +103,7 @@
 void swk_separator(SwkEvent *e);
 void swk_progress(SwkEvent *e);
 void swk_image(SwkEvent *e);
+void swk_sketch(SwkEvent *e);
 
 /* graphic backend */
 
@@ -124,6 +125,7 @@
 
 /* images */
 void swk_gi_img(Rect r, void *img);
+void* swk_gi_img_new(int w, int h, int color);
 void* swk_gi_img_load(const char *str);
 void swk_gi_img_free(void *s);
 void swk_gi_img_set(void *img, int x, int y, int color);
diff -r 9d527f82d3fe -r c6a17f2bc350 test.c
--- a/test.c Thu May 06 10:18:56 2010 +0200
+++ b/test.c Fri May 07 00:59:57 2010 +0200
@@ -91,6 +91,8 @@
         { .cb=swk_password, .text="1234", },
         SWK_BOX_NEWLINE(-1),
         { .cb=swk_filler, },
+ { .cb=swk_sketch },
+ SWK_BOX_NEWLINE(1),
         { .cb=swk_image, .text="image.png" },
         { .cb=swk_image, .text="image.png" },
         { .cb=swk_image, .text="image.png" },
Received on Thu May 06 2010 - 23:06:11 UTC

This archive was generated by hypermail 2.2.0 : Thu May 06 2010 - 23:12:03 UTC