columnify.h

00001 // columnify.h    -*-c++-*-
00002 //
00003 //  Copyright 2000, 2005 Daniel Burrows
00004 //  
00005 //  This program is free software; you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; see the file COPYING.  If not, write to
00017 //  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018 //  Boston, MA 02111-1307, USA.
00019 //
00020 //  Support for creating column-formatted strings.  Columns which exceed their
00021 // size limit are truncated.  Columns are collected into 'groups', each of
00022 // which starts at a particular location.  This allows things such as
00023 // right-justification of text.  If one column group overlaps another, the
00024 // conflict is resolved by adjusting the column which is situated farthest to
00025 // the right (by moving it farther to the right).
00026 //
00027 //  Right-justification is now handled (somewhat hackily) by columnify; to
00028 // use it, set the start_off to something negative.  The group will be aligned
00029 // so the the right edge of the column is start_off+1 characters from the
00030 // right-hand screen edge -- so to align something exactly with the right-hand
00031 // edge of the screen, set start_off to -1.
00032 
00033 #ifndef COLUMNIFY_H
00034 #define COLUMNIFY_H
00035 
00036 #include <list>
00037 #include <string>
00038 #include <cwidget/generic/util/eassert.h>
00039 
00040 namespace cwidget
00041 {
00042   struct column_disposition
00043   {
00044     std::wstring text;  // The contents of the columns
00045     int minx;          // The minimum x value to start this column at (useful for
00046     // indenting stuff in trees)
00047 
00048     column_disposition(const std::wstring &_text, int _minx):text(_text), minx(_minx) {}
00049 
00051     column_disposition(const std::string &_text, int _minx,
00052                        const char *encoding=NULL);
00053   };
00054 
00055   struct column
00056   {
00057     column_disposition info;
00058     int width;
00059     bool expand, shrink;
00060 
00061     column(const column_disposition &_info, int _width, bool _expand, bool _shrink)
00062       :info(_info), width(_width), expand(_expand), shrink(_shrink)
00063     {
00064       eassert(_width>=0);
00065     }
00066   };
00067 
00068   typedef std::list<column> column_list;
00069 
00070   typedef std::list<column> layout;
00071 
00072   /* \return a string formatted as requested.  The string will be no
00073    * wider than width columns.  I could probably use printf-style
00074    * strings, but with the groups it would probably turn into a major
00075    * pain..
00076    */
00077   std::wstring columnify(const layout &format, int width);
00078 }
00079 
00080 #endif

Generated on Wed Jan 28 07:23:52 2009 for cwidget by  doxygen 1.5.6