Browse Source

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]\

Martin Duquesnoy 14 years ago
parent
commit
0ce0d1a352
6 changed files with 120 additions and 1 deletions
  1. 13 0
      CMakeLists.txt
  2. 3 0
      src/config.h.in
  3. 42 0
      src/draw.c
  4. 42 1
      src/status.c
  5. 6 0
      src/structs.h
  6. 14 0
      src/wmfs.h

+ 13 - 0
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

+ 3 - 0
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 */

+ 42 - 0
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

+ 42 - 1
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);

+ 6 - 0
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
 {

+ 14 - 0
src/wmfs.h

@@ -68,6 +68,10 @@
 #include <X11/extensions/Xrandr.h>
 #endif /* HAVE_XRANDR */
 
+#ifdef HAVE_IMLIB
+#include <Imlib2.h>
+#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);