changeset 3204:369bdd72f5e6

Renamed text and icon drawing functions and moved text stuff to subtle/text.c
author unexist
date Tue, 22 May 2012 23:15:06 +0200
parents 793b1315b386
children 25e264312f90
files src/shared/shared.c src/shared/shared.h src/subtlext/window.c
diffstat 3 files changed, 72 insertions(+), 300 deletions(-) [+]
line wrap: on
line diff
--- a/src/shared/shared.c	Sat May 12 20:49:31 2012 +0200
+++ b/src/shared/shared.c	Tue May 22 23:15:06 2012 +0200
@@ -365,241 +365,9 @@
   XDeleteProperty(disp, win, prop);
 } /* }}} */
 
-/* Text */
-
- /** subSharedTextNew {{{
-  * @brief Parse text
-  * @return New #SubText
-  **/
-
-SubText *
-subSharedTextNew(void)
-{
-  return TEXT(subSharedMemoryAlloc(1, sizeof(SubText)));
-} /* }}} */
-
- /** subSharedTextParse {{{
-  * @brief Parse text
-  * @param[in]  disp  Display
-  * @param[in]  f     A #SubFont
-  * @param[in]  t     A #SubText
-  * @param[in]  text  String to parse
-  **/
-
-int
-subSharedTextParse(Display *disp,
-  SubFont *f,
-  SubText *t,
-  char *text)
-{
-  int i = 0, left = 0, right = 0;
-  char *tok = NULL;
-  long color = -1, pixmap = 0;
-  SubTextItem *item = NULL;
-
-  assert(f && t);
-
-  t->width = 0;
-
-  /* Split and iterate over tokens */
-  while((tok = strsep(&text, SEPARATOR)))
-    {
-      if('#' == *tok) color = strtol(tok + 1, NULL, 0); ///< Color
-      else if('\0' != *tok) ///< Text or icon
-        {
-          /* Re-use items to save alloc cycles */
-          if(i < t->nitems && (item = ITEM(t->items[i])))
-            {
-              if(!(item->flags & (SUB_TEXT_BITMAP|SUB_TEXT_PIXMAP)) &&
-                  item->data.string)
-                free(item->data.string);
-
-              item->flags &= ~(SUB_TEXT_EMPTY|SUB_TEXT_BITMAP|SUB_TEXT_PIXMAP);
-            }
-          else if((item = ITEM(subSharedMemoryAlloc(1, sizeof(SubTextItem)))))
-            {
-              /* Add icon to array */
-              t->items = (SubTextItem **)subSharedMemoryRealloc(t->items,
-                (t->nitems + 1) * sizeof(SubTextItem *));
-              t->items[(t->nitems)++] = item;
-            }
-
-          /* Get geometry of bitmap/pixmap */
-          if(('!' == *tok || '&' == *tok) &&
-              (pixmap = strtol(tok + 1, NULL, 0)))
-            {
-              XRectangle geometry = { 0 };
-
-              subSharedPropertyGeometry(disp, pixmap, &geometry);
-
-              item->flags    |= ('!' == *tok ? SUB_TEXT_BITMAP :
-                SUB_TEXT_PIXMAP);
-              item->data.num  = pixmap;
-              item->width     = geometry.width;
-              item->height    = geometry.height;
-
-              /* Add spacing and check if icon is first */
-              t->width += item->width + (0 == i ? 3 : 6);
-
-              item->color = color;
-            }
-          else ///< Ordinary text
-            {
-              item->data.string = strdup(tok);
-              item->width       = subSharedTextWidth(disp, f, tok,
-                strlen(tok), &left, &right, False);
-
-              /* Remove left bearing from first text item */
-              t->width += item->width - (0 == i ? left : 0);
-
-              item->color = color;
-            }
-
-          i++;
-        }
-    }
-
-  /* Mark other items a clean */
-  for(; i < t->nitems; i++)
-    ITEM(t->items[i])->flags |= SUB_TEXT_EMPTY;
-
-  /* Fix spacing of last item */
-  if(item)
-    {
-      if(item->flags & (SUB_TEXT_BITMAP|SUB_TEXT_PIXMAP))
-        t->width -= 2;
-      else
-        {
-          t->width    -= right;
-          item->width -= right;
-        }
-    }
-
-  return t->width;
-} /* }}} */
+/* Draw */
 
- /** subSharedTextRender {{{
-  * @brief Render text
-  * @param[in]  disp  Display
-  * @param[in]  gc    GC
-  * @param[in]  f     A #SubFont
-  * @param[in]  win   A #Window
-  * @param[in]  x     X position
-  * @param[in]  y     Y position
-  * @param[in]  fg    Foreground color
-  * @param[in]  icon  Icon color
-  * @param[in]  bg    Background color
-  * @param[in]  t     A #SubText
-  **/
-
-void
-subSharedTextRender(Display *disp,
-  GC gc,
-  SubFont *f,
-  Window win,
-  int x,
-  int y,
-  long fg,
-  long icon,
-  long bg,
-  SubText *t)
-{
-  int i, width = x;
-
-  assert(t);
-
-  /* Render text items */
-  for(i = 0; i < t->nitems; i++)
-    {
-      SubTextItem *item = ITEM(t->items[i]);
-
-      if(item->flags & SUB_TEXT_EMPTY) ///< Empty text
-        {
-          break; ///< Break loop
-        }
-      else if(item->flags & (SUB_TEXT_BITMAP|SUB_TEXT_PIXMAP)) ///< Icons
-        {
-          int icony = 0, dx = (0 == i) ? 0 : 3; ///< Add spacing when icon isn't first
-
-          icony = item->height > f->height ?
-            y - f->y - ((item->height - f->height) / 2): y - item->height;
-
-          subSharedTextIconDraw(disp, gc, win, width + dx, icony,
-            item->width, item->height, (-1 == item->color) ? icon : item->color,
-            bg, (Pixmap)item->data.num, (item->flags & SUB_TEXT_BITMAP));
-
-          /* Add spacing when icon isn't last */
-          width += item->width + dx + (i != t->nitems - 1 ? 3 : 0);
-        }
-      else ///< Text
-        {
-          subSharedTextDraw(disp, gc, f, win, width, y,
-            (-1 == item->color) ? fg : item->color, bg, 
-            item->data.string, strlen(item->data.string));
-
-          width += item->width;
-        }
-    }
-} /* }}} */
-
- /** subSharedTextWidth {{{
-  * @brief Get width of the smallest enclosing box
-  * @param[in]     disp    Display
-  * @param[in]     text    The text
-  * @param[in]     len     Length of the string
-  * @param[inout]  left    Left bearing
-  * @param[inout]  right   Right bearing
-  * @param[in]     center  Center text
-  * @return Width of the box
-  **/
-
-int
-subSharedTextWidth(Display *disp,
-  SubFont *f,
-  const char *text,
-  int len,
-  int *left,
-  int *right,
-  int center)
-{
-  int width = 0, lbearing = 0, rbearing = 0;
-
-  assert(f);
-
-  /* Get text extents based on font */
-  if(text && 0 < len)
-    {
-#ifdef HAVE_X11_XFT_XFT_H
-      if(f->xft) ///< XFT
-        {
-          XGlyphInfo extents;
-
-          XftTextExtentsUtf8(disp, f->xft, (XftChar8 *)text, len, &extents);
-
-          width    = extents.xOff;
-          lbearing = extents.x;
-        }
-      else ///< XFS
-#endif /* HAVE_X11_XFT_XFT_H */
-        {
-          XRectangle overall_ink = { 0 }, overall_logical = { 0 };
-
-          XmbTextExtents(f->xfs, text, len,
-            &overall_ink, &overall_logical);
-
-          width    = overall_logical.width;
-          lbearing = overall_logical.x;
-        }
-
-      /* Get left and right spacing */
-      if(left)  *left  = lbearing;
-      if(right) *right = rbearing;
-    }
-
-  return center ? width - abs(lbearing - rbearing) : width;
-} /* }}} */
-
- /** subSharedTextDraw {{{
+ /** subSharedDrawString {{{
   * @brief Draw text
   * @param[in]  disp  Display
   * @param[in]  gc    GC
@@ -614,7 +382,7 @@
   **/
 
 void
-subSharedTextDraw(Display *disp,
+subSharedDrawString(Display *disp,
   GC gc,
   SubFont *f,
   Window win,
@@ -661,7 +429,7 @@
     }
 } /* }}} */
 
- /** subSharedTextIconDraw {{{
+ /** subSharedDrawIcon {{{
   * @brief Draw text
   * @param[in]  disp    Display
   * @param[in]  gc      GC
@@ -677,7 +445,7 @@
   **/
 
 void
-subSharedTextIconDraw(Display *disp,
+subSharedDrawIcon(Display *disp,
   GC gc,
   Window win,
   int x,
@@ -704,33 +472,6 @@
 #endif /* HAVE_X11_XPM_H */
 } /* }}} */
 
- /** subSharedTextFree {{{
-  * @brief Free text
-  * @param[in]  t  A #SubText
-  **/
-
-void
-subSharedTextFree(SubText *t)
-{
-  int i;
-
-  assert(t);
-
-  for(i = 0; i < t->nitems; i++)
-    {
-      SubTextItem *item = (SubTextItem *)t->items[i];
-
-      if(!(item->flags & (SUB_TEXT_BITMAP|SUB_TEXT_PIXMAP)) &&
-          item->data.string)
-        free(item->data.string);
-
-      free(t->items[i]);
-    }
-
-  free(t->items);
-  free(t);
-} /* }}} */
-
 /* Font */
 
  /** subSharedFontNew {{{
@@ -951,6 +692,63 @@
   return pid;
 } /* }}} */
 
+ /** subSharedStringWidth {{{
+  * @brief Get width of the smallest enclosing box
+  * @param[in]     disp    Display
+  * @param[in]     text    The text
+  * @param[in]     len     Length of the string
+  * @param[inout]  left    Left bearing
+  * @param[inout]  right   Right bearing
+  * @param[in]     center  Center text
+  * @return Width of the box
+  **/
+
+int
+subSharedStringWidth(Display *disp,
+  SubFont *f,
+  const char *text,
+  int len,
+  int *left,
+  int *right,
+  int center)
+{
+  int width = 0, lbearing = 0, rbearing = 0;
+
+  assert(f);
+
+  /* Get text extents based on font */
+  if(text && 0 < len)
+    {
+#ifdef HAVE_X11_XFT_XFT_H
+      if(f->xft) ///< XFT
+        {
+          XGlyphInfo extents;
+
+          XftTextExtentsUtf8(disp, f->xft, (XftChar8 *)text, len, &extents);
+
+          width    = extents.xOff;
+          lbearing = extents.x;
+        }
+      else ///< XFS
+#endif /* HAVE_X11_XFT_XFT_H */
+        {
+          XRectangle overall_ink = { 0 }, overall_logical = { 0 };
+
+          XmbTextExtents(f->xfs, text, len,
+            &overall_ink, &overall_logical);
+
+          width    = overall_logical.width;
+          lbearing = overall_logical.x;
+        }
+
+      /* Get left and right spacing */
+      if(left)  *left  = lbearing;
+      if(right) *right = rbearing;
+    }
+
+  return center ? width - abs(lbearing - rbearing) : width;
+} /* }}} */
+
 #ifndef SUBTLE
 
  /** subSharedMessage {{{
--- a/src/shared/shared.h	Sat May 12 20:49:31 2012 +0200
+++ b/src/shared/shared.h	Tue May 22 23:15:06 2012 +0200
@@ -68,8 +68,8 @@
 #define SUB_EWMH_BORDERLESS (1L << 8)                             ///< EWMH fixed flag
 #define SUB_EWMH_VISIBLE    (1L << 9)                             ///< EWMH visible flag
 #define SUB_EWMH_HIDDEN     (1L << 10)                            ///< EWMH hidden flag
-#define SUB_EWMH_HORZ       (1L << 12)                            ///< EWMH horizontal flag
-#define SUB_EWMH_VERT       (1L << 11)                            ///< EWMH vertical flag
+#define SUB_EWMH_HORZ       (1L << 11)                            ///< EWMH horizontal flag
+#define SUB_EWMH_VERT       (1L << 12)                            ///< EWMH vertical flag
 
 /* Match types flags */
 #define SUB_MATCH_NAME      (1L << 0)                             ///< Match SUBTLE_NAME
@@ -79,11 +79,6 @@
 #define SUB_MATCH_ROLE      (1L << 4)                             ///< Match window role
 #define SUB_MATCH_PID       (1L << 5)                             ///< Match pid
 #define SUB_MATCH_EXACT     (1L << 6)                             ///< Match exact string
-
-/* Text flags */
-#define SUB_TEXT_EMPTY      (1L << 0)                             ///< Empty text
-#define SUB_TEXT_BITMAP     (1L << 1)                             ///< Text bitmap
-#define SUB_TEXT_PIXMAP     (1L << 2)                             ///< Text pixmap
 /* }}} */
 
 /* Typedefs {{{ */
@@ -93,20 +88,6 @@
   char          *string;                                          ///< Data string
 } SubData; /* }}} */
 
-typedef struct subtextitem_t /* {{{ */
-{
-  int             flags, width, height;                           ///< Text flags, width, height
-  long            color;                                          ///< Text color
-
-  union subdata_t data;                                           ///< Text data
-} SubTextItem; /* }}} */
-
-typedef struct subtext_t /* {{{ */
-{
-  struct subtextitem_t **items;                                   ///< Item text items
-  int                  flags, nitems, width;                      ///< Item flags, count, width
-} SubText; /* }}} */
-
 typedef struct subfont_t /* {{{ */
 {
   int      y, height;                                             ///< Font y, height
@@ -154,20 +135,11 @@
   Atom prop);                                                     ///< Delete window property
 /* }}} */
 
-/* Text {{{ */
-SubText *subSharedTextNew(void);                                  ///< Create new text
-int subSharedTextParse(Display *disp, SubFont *f,
-  SubText *t, char *text);                                        ///< Parse text
-void subSharedTextRender(Display *disp, GC gc, SubFont *f,
-  Window win, int x, int y, long fg, long icon,
-  long bg, SubText *t);                                           ///< Render text
-int subSharedTextWidth(Display *disp, SubFont *f,
-  const char *text, int len, int *left, int *right, int center);  ///< Get text width
-void subSharedTextFree(SubText *t);                               ///< Free text
-void subSharedTextIconDraw(Display *disp, GC gc, Window win,
+/* Draw {{{ */
+void subSharedDrawIcon(Display *disp, GC gc, Window win,
   int x, int y, int width, int height, long fg, long bg,
   Pixmap pixmap, int bitmap);                                     ///< Draw icons
-void subSharedTextDraw(Display *disp, GC gc, SubFont *f,
+void subSharedDrawString(Display *disp, GC gc, SubFont *f,
   Window win, int x, int y, long fg, long bg,
   const char *text, int len);                                     ///< Draw text
 /* }}} */
@@ -182,6 +154,8 @@
 KeySym subSharedParseKey(Display *disp, const char *key,
   unsigned int *code, unsigned int *state, int *mouse);           ///< Parse keys
 pid_t subSharedSpawn(char *cmd);                                  ///< Spawn command
+int subSharedStringWidth(Display *disp, SubFont *f,
+  const char *text, int len, int *left, int *right, int center);  ///< Get text width
 /* }}} */
 
 #ifndef SUBTLE
--- a/src/subtlext/window.c	Sat May 12 20:49:31 2012 +0200
+++ b/src/subtlext/window.c	Tue May 22 23:15:06 2012 +0200
@@ -602,7 +602,7 @@
 
   Data_Get_Struct(self, SubtlextWindow, w);
   if(w && w->font && T_STRING == rb_type(string))
-    ret = INT2FIX(subSharedTextWidth(display, w->font,
+    ret = INT2FIX(subSharedStringWidth(display, w->font,
       RSTRING_PTR(string), RSTRING_LEN(string), NULL, NULL, False));
 
   return ret;
@@ -1094,7 +1094,7 @@
       /* Parse colors */
       if(!NIL_P(color)) lcolor = subColorPixel(color, Qnil, Qnil, NULL);
 
-      subSharedTextDraw(display, w->gc, w->font, w->win, FIX2INT(x),
+      subSharedDrawString(display, w->gc, w->font, w->win, FIX2INT(x),
         FIX2INT(y), lcolor, w->bg, RSTRING_PTR(text), RSTRING_LEN(text));
     }
 
@@ -1147,7 +1147,7 @@
       pixmap = rb_iv_get(icon, "@pixmap");
       bitmap = Qtrue == subIconAskBitmap(icon) ? True : False;
 
-      subSharedTextIconDraw(display, w->gc, w->win, FIX2INT(x),
+      subSharedDrawIcon(display, w->gc, w->win, FIX2INT(x),
         FIX2INT(y), FIX2INT(width), FIX2INT(height), lfg, lbg,
         NUM2LONG(pixmap), bitmap);
     }