From 0ce0d1a3525e6a38aeb37185801dfa079209480b Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sun, 28 Mar 2010 03:36:56 +0200 Subject: [PATCH] Draw: Add Imlib2 support to draw image (optional feature, work only if have imlib2): statustext block to draw image: \i[x;y;width;height;imagepath.extension]\ --- CMakeLists.txt | 13 +++++++++++++ src/config.h.in | 3 +++ src/draw.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/status.c | 43 ++++++++++++++++++++++++++++++++++++++++++- src/structs.h | 6 ++++++ src/wmfs.h | 14 ++++++++++++++ 6 files changed, 120 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fdcfb8..0255b8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,6 +106,8 @@ pkg_check_modules(WMFS_REQUIRED REQUIRED freetype2 xft) +# Optional dependencies check + # Check for xinerama pkg_check_modules(HAVE_XINERAMA xinerama) if(HAVE_XINERAMA_FOUND) @@ -124,6 +126,17 @@ else() set(WMFS_HAVE_XRANDR "") endif() +# Check for Imlib +pkg_check_modules(HAVE_IMLIB imlib2) +if(HAVE_IMLIB_FOUND) + set(WMFS_HAVE_IMLIB "#define HAVE_IMLIB") + set(LIBRARIES_TO_LINK ${LIBRARIES_TO_LINK} Imlib2) +else() + set(WMFS_HAVE_IMLIB "") +endif() + + + target_link_libraries(wmfs ${LIBRARIES_TO_LINK}) # Messages diff --git a/src/config.h.in b/src/config.h.in index af98c95..e409351 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -41,7 +41,10 @@ #define WMFS_COMPILE_FLAGS "@WMFS_COMPILE_FLAGS@" #define WMFS_LINKED_LIBS "@WMFS_LINKED_LIBS@" #define XDG_CONFIG_DIR "@XDG_CONFIG_DIR@" + +/* Optional dependencies */ @WMFS_HAVE_XINERAMA@ @WMFS_HAVE_XRANDR@ +@WMFS_HAVE_IMLIB@ #endif /* CONFIG_H */ diff --git a/src/draw.c b/src/draw.c index 30fa14e..e017f03 100644 --- a/src/draw.c +++ b/src/draw.c @@ -85,6 +85,48 @@ draw_rectangle(Drawable dr, int x, int y, uint w, uint h, uint color) return; } +#ifdef HAVE_IMLIB +/** Draw an image in a drawable + * \param dr Drawable + * \param x X position + * \param y Y position + * \param name Path of the XPM +*/ +void +draw_image(Drawable dr, int x, int y, int w, int h, char *name) +{ + Imlib_Image image; + + if(!name) + return; + + imlib_set_cache_size(2048 * 1024); + imlib_context_set_display(dpy); + imlib_context_set_visual(DefaultVisual(dpy, DefaultScreen(dpy))); + imlib_context_set_colormap(DefaultColormap(dpy, DefaultScreen(dpy))); + imlib_context_set_drawable(dr); + + image = imlib_load_image(name); + imlib_context_set_image(image); + + if(w <= 0) + w = imlib_image_get_width(); + + if(h <= 0) + h = imlib_image_get_height(); + + if(image) + { + imlib_render_image_on_drawable_at_size(x, y, w, h); + imlib_free_image(); + } + else + warnx("Can't draw image: '%s'", name); + + return; +} +#endif /* HAVE_IMLIB */ + /** Calculates the text's size relatively to the font * \param text Text string * \return final text width diff --git a/src/status.c b/src/status.c index 47a5c2a..a6fa48b 100644 --- a/src/status.c +++ b/src/status.c @@ -80,6 +80,33 @@ statustext_text(StatusText *s, char *str) return n; } +#ifdef HAVE_IMLIB +/** Check images blocks in str and return properties + * --> \i[x;y;w;h;name]\ + *\param im StatusImage pointer, image properties + *\param str String + *\return n Lenght of i + */ +int +statustext_image(StatusImage *im, char *str) +{ + char as; + int n, i, j, k; + + for(i = j = n = 0; i < strlen(str); ++i, ++j) + /* %512[^]] */ + if(sscanf(&str[i], "\\i[%d;%d;%d;%d;%512[^]]]%c", &im[n].x, &im[n].y, &im[n].w, &im[n].h, im[n].name, &as) == 6 + && as == '\\') + for(++n, ++i, --j; str[i] != as || str[i - 1] != ']'; ++i); + else if(j != i) + str[j] = str[i]; + + for(k = j; k < i; str[k++] = 0); + + return n; +} +#endif /* HAVE_IMLIB */ + /** Draw normal text and colored normal text * --> \#color\ text in color *\param sc Screen @@ -158,10 +185,18 @@ statustext_handle(int sc, char *str) infobar[sc].statustext = _strdup(str); len = ((strlen(str) > MAXSTATUS) ? MAXSTATUS : strlen(str)); - /* Store rectangles & located text properties. */ + /* Store rectangles, located text & images properties. */ nr = statustext_rectangle(r, str); ns = statustext_text(s, str); +#ifdef HAVE_IMLIB + int ni; + StatusImage im[128]; + + ni = statustext_image(im, str); +#endif /* HAVE_IMLIB */ + + /* Draw normal text (and possibly colored with \#color\ blocks) */ statustext_normal(sc, str); @@ -173,6 +208,12 @@ statustext_handle(int sc, char *str) for(i = 0; i < ns; ++i) draw_text(infobar[sc].bar->dr, s[i].x, s[i].y, s[i].color, 0, s[i].text); +#ifdef HAVE_IMLIB + /* Draw images with stored properties. */ + for(i = 0; i < ni; ++i) + draw_image(infobar[sc].bar->dr, im[i].x, im[i].y, im[i].w, im[i].h, im[i].name); +#endif /* HAVE_IMLIB */ + barwin_refresh(infobar[sc].bar); free(lastst); diff --git a/src/structs.h b/src/structs.h index 4ee9ff9..870f16c 100644 --- a/src/structs.h +++ b/src/structs.h @@ -419,6 +419,12 @@ typedef struct char text[512]; } StatusText; +typedef struct +{ + uint x, y, w, h; + char name[512]; +} StatusImage; + /* Config.c struct */ typedef struct { diff --git a/src/wmfs.h b/src/wmfs.h index a776b5b..6192312 100644 --- a/src/wmfs.h +++ b/src/wmfs.h @@ -68,6 +68,10 @@ #include #endif /* HAVE_XRANDR */ +#ifdef HAVE_IMLIB +#include +#endif /* HAVE_IMLIB */ + /* MACRO */ #define ButtonMask (ButtonPressMask | ButtonReleaseMask | ButtonMotionMask) #define MouseMask (ButtonMask | PointerMotionMask) @@ -127,6 +131,11 @@ void barwin_refresh(BarWindow *bw); /* draw.c */ void draw_text(Drawable d, int x, int y, char* fg, int pad, char *str); void draw_rectangle(Drawable dr, int x, int y, uint w, uint h, uint color); + +#ifdef HAVE_IMLIB +void draw_image(Drawable dr, int x, int y, int w, int h, char *name); +#endif /* HAVE_IMLIB */ + ushort textw(const char *text); /* infobar.c */ @@ -290,6 +299,11 @@ void uicb_screen_prev_sel(uicb_t); /* status.c */ int statustext_rectangle(StatusRec *r, char *str); int statustext_text(StatusText *s, char *str); + +#ifdef HAVE_IMLIB +int statustext_image(StatusImage *im, char *str); +#endif /* HAVE_IMLIB */ + void statustext_normal(int sc, char *str); void statustext_handle(int sc, char *str);