Hello,

While studying code of the FvwmWinList module I found that truncation
code isn't very well written:

1) Truncated string saved only if 'TruncateLeft' option is set. If not,
string recalculated each time.

2) If 'TruncateLeft' isn't set, during truncation only length of the
string is updated, end of string '\0' marker position isn't moved.

3) If 'TruncateLeft' is set string can be truncated up to zero
characters (only '\0' is left); if it isn't set string can only be
truncated up to one character.

Attached patch:

- fixes first two issues.
- independently of 'TruncateLeft' setting, allows string truncation up
  to zero characters (only '\0' is left).
- considerably shortens the code (can be shortened even more, but I
  followed module author's coding style) and makes it more easy to
  understand.
- removes useless comments about converting module code to C++.

Hope you'll find my changes useful.

Bye

-- 
Serge Koksharov, Free Software user & supporter
GPG public key ID: 0x3D330896 (pgp.mit.edu)
Key fingerprint: 5BC4 0475 CB03 6A31 0076  82C2 C240 72F0 3D33 0896
diff -Naur fvwmCVS-orig/modules/ChangeLog fvwmCVS-fixed/modules/ChangeLog
--- fvwmCVS-orig/modules/ChangeLog      2006-08-01 12:38:42.000000000 +0400
+++ fvwmCVS-fixed/modules/ChangeLog     2006-08-01 14:25:42.000000000 +0400
@@ -1,3 +1,13 @@
+2006-08-01  Serge Koksharov  <gentoosiast dog yandex dot ru>
+
+       * modules/FvwmWinList/ButtonArray.c (DoButton):
+       improved truncation code.
+       
+       * modules/FvwmWinList/ButtonArray.c:
+       * modules/FvwmWinList/List.c:
+       * modules/FvwmWinList/List.h:
+       removed useless comments about converting module code to C++.
+
 2006-07-16  Scott Smedley  <[EMAIL PROTECTED]>
 
        * FvwmTabs/FvwmTabs.in:
diff -Naur fvwmCVS-orig/modules/FvwmWinList/ButtonArray.c 
fvwmCVS-fixed/modules/FvwmWinList/ButtonArray.c
--- fvwmCVS-orig/modules/FvwmWinList/ButtonArray.c      2006-08-01 
12:38:41.000000000 +0400
+++ fvwmCVS-fixed/modules/FvwmWinList/ButtonArray.c     2006-08-01 
14:20:57.000000000 +0400
@@ -10,7 +10,6 @@
  * whatsoever. Use this program at your own risk. Permission to use this
  * program for any purpose is given, as long as the copyright is kept intact.
  *
- *  Things to do:  Convert to C++  (In Progress)
  */
 
 /* This program is free software; you can redistribute it and/or modify
@@ -517,33 +516,24 @@
                }
                else
                {
-                       if (TruncateLeft)
+                       while (*string &&
+                               (newx + FlocaleTextWidth(
+                                       FButtonFont, string, len)
+                               + 2 * button->reliefwidth +
+                               INNER_MARGIN) > w)
                        {
-                               /* move the ptr up until the rest fits */
-                               while (*string &&
-                                      (newx + FlocaleTextWidth(
-                                              FButtonFont, string,
-                                              strlen(string))
-                                       + 2 * button->reliefwidth +
-                                       INNER_MARGIN) > w)
+                               len--;
+                               if (TruncateLeft)
                                {
                                        string++;
-                                       len--;
                                }
-                               button->truncatewidth = w;
-                               button->truncate_title = string;
-                       }
-                       else
-                       {
-                               while ((len > 1) &&
-                                      (newx + FlocaleTextWidth(
-                                              FButtonFont, string, len)
-                                       + 2 * button->reliefwidth +
-                                       INNER_MARGIN) > w)
+                               else
                                {
-                                       len--;
+                                       *(string + len) = '\0';
                                }
                        }
+                       button->truncatewidth = w;
+                       button->truncate_title = string;
                }
        }
        FwinString->str = string;
diff -Naur fvwmCVS-orig/modules/FvwmWinList/List.c 
fvwmCVS-fixed/modules/FvwmWinList/List.c
--- fvwmCVS-orig/modules/FvwmWinList/List.c     2006-08-01 12:38:41.000000000 
+0400
+++ fvwmCVS-fixed/modules/FvwmWinList/List.c    2006-08-01 12:55:53.000000000 
+0400
@@ -10,7 +10,6 @@
  * whatsoever. Use this program at your own risk. Permission to use this
  * program for any purpose is given, as long as the copyright is kept intact.
  *
- *  Things to do:  Convert to C++  (In Progress)
  */
 
 /* This program is free software; you can redistribute it and/or modify
diff -Naur fvwmCVS-orig/modules/FvwmWinList/List.h 
fvwmCVS-fixed/modules/FvwmWinList/List.h
--- fvwmCVS-orig/modules/FvwmWinList/List.h     2006-08-01 12:38:41.000000000 
+0400
+++ fvwmCVS-fixed/modules/FvwmWinList/List.h    2006-08-01 12:55:57.000000000 
+0400
@@ -10,7 +10,6 @@
  * whatsoever. Use this program at your own risk. Permission to use this
  * program for any purpose is given, as long as the copyright is kept intact.
  *
- *  Things to do:  Convert to C++  (In Progress)
  */
 
 /* Structure definitions */

Attachment: pgpHmNzgYRpDX.pgp
Description: PGP signature

Reply via email to