Re: Word wrap bug. - scratch that previous one




here it is again for the first time ;-)

gchar *soft_word_wrap (gchar *text, guint len)
{
   /* wraps text without breaking up `words`
    * allowing `len` chars per line */
   gchar *curpos, *lastspc, *lastbrk;

   for (curpos = lastspc = lastbrk = text; *curpos; curpos++)
   {
      if (*curpos == ' ')
         lastspc = curpos;
      else
         if (*curpos == '\n')
         {
            lastbrk = curpos;
            lastspc = lastbrk;
         }
      if ( (curpos - lastbrk) >= len)
      {
         *lastspc = '\n';
         lastbrk = lastspc;
      }
   }

   return text;
}

gchar *hard_word_wrap (gchar *text, guint len)
{
   /* wraps text at `len` chars per line whether it breaks up a word or not
    * returns a pointer to an allocated string - be sure to free this
memory later!! */
   gchar *curpos, *lastbrk;
   gchar *buffer, *buffpos;
   guint bufflen;

   bufflen = strlen(text) + 1;
   buffer = g_malloc0(bufflen);

   buffpos = buffer;
   for (curpos = lastbrk = text; *curpos; curpos++)
   {
      *buffpos++ = *curpos;
      if (*curpos == '\n')
         lastbrk = curpos;
      if ( (curpos - lastbrk) >= len)
      {
         bufflen++;
         buffer = g_realloc(buffer, bufflen);
         *buffpos++ = '\n';
         lastbrk = curpos;
      }
   }

   return buffer;
}

#define word_wrap (a, b) hard_word_wrap (soft_word_wrap((a), (b)), (b))

void send_body_wrap (Body *body, GtkText *text)
{
   body->buffer = word_wrap(gtk_editable_get_chars (GTK_EDITABLE(text), 0,
-1), balsa_app.wraplength);
}

-- 
Spruce: Uhh, could you keep that dog away from me? I just had my roots
cleaned.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]