cwidget  0.5.17
fragment.h
1 // fragment.h -*-c++-*-
2 //
3 // Copyright (C) 2004-2005, 2007 Daniel Burrows
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; see the file COPYING. If not, write to
17 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 // Boston, MA 02111-1307, USA.
19 //
20 // Fragments are pieces of text that live in a text_layout widget.
21 // See widgets/text_layout.h for details.
22 
23 #ifndef FRAGMENT_H
24 #define FRAGMENT_H
25 
26 #include "fragment_contents.h"
27 
28 #include <cwidget/style.h>
29 
30 #include <string>
31 #include <vector>
32 
33 namespace cwidget
34 {
37  class fragment
38  {
39  public:
58  virtual fragment_contents layout(size_t firstw,
59  size_t w,
60  const style &st)=0;
61 
71  virtual size_t max_width(size_t first_indent,
72  size_t rest_indent) const=0;
73 
81  virtual size_t trailing_width(size_t first_indent,
82  size_t rest_indent) const=0;
83 
85  virtual bool final_newline() const=0;
86 
88  virtual ~fragment();
89  };
90 
91  // Factory methods to avoid cluttering the .h file:
92 
101  fragment *text_fragment(const std::wstring &s);
102 
112  fragment *text_fragment(const std::wstring &s,
113  const style &st);
114 
123  fragment *text_fragment(const std::string &s,
124  const char *encoding=NULL);
125 
129  fragment *text_fragment(const std::string &s,
130  const style &st,
131  const char *encoding=NULL);
132 
141  inline fragment *text_fragment(const char *s,
142  const style &st=style())
143  {
144  return text_fragment(std::string(s), st);
145  }
146 
149 
158  const style &st);
159 
173  fragment *sequence_fragment(const std::vector<fragment *> &fragments);
174 
185  fragment *sequence_fragment(fragment *f, ...);
186 
195  fragment *join_fragments(const std::vector<fragment *> &fragments,
196  const std::wstring &between);
197 
211  fragment *flowbox(fragment *contents);
212 
225  fragment *fillbox(fragment *contents);
226 
238  fragment *hardwrapbox(fragment *contents);
239 
251  fragment *clipbox(fragment *contents);
252 
267  fragment *indentbox(size_t firstindent, size_t restindent, fragment *contents);
268 
277  fragment *dropbox(fragment *header, fragment *contents);
278 
281  {
286 
293 
298  size_t width;
299 
300  enum align {top, center, bottom};
301 
308  align vert_align;
309 
320  std::vector<fragment *>lines;
321 
323  fragment_column_entry(bool _proportional,
324  bool _expandable,
325  size_t _width, align _vert_align,
326  fragment *f)
327  :proportional(_proportional),
328  expandable(_expandable),
329  width(_width),
330  vert_align(_vert_align)
331  {
332  lines.push_back(f);
333  }
334 
335  fragment_column_entry(bool _proportional,
336  bool _expandable,
337  size_t _width, align _vert_align,
338  const std::vector<fragment *> &_lines)
339  :proportional(_proportional),
340  expandable(_expandable),
341  width(_width),
342  vert_align(_vert_align),
343  lines(_lines)
344  {
345  }
346 
348  :proportional(false), width(0), vert_align(top)
349  {
350  }
351  };
352 
364  fragment *fragment_columns(const std::vector<fragment_column_entry> &columns);
365 
393  fragment *fragf(const char *format, ...);
394 }
395 
396 #endif
bool expandable
If proportional is false and the fragment is not NULL, then setting this to true means that the width...
Definition: fragment.h:292
A "style" is a setting to be applied to a display element (widget, text, etc).
Definition: style.h:51
This class represents the formatted contents of a fragment.
Definition: fragment_contents.h:30
fragment * newline_fragment()
Create a fragment which simply produces a newline wherever it occurs.
Definition: fragment.cc:165
fragment * fillbox(fragment *contents)
Create a fillbox.
Definition: fragment.cc:775
std::vector< fragment * > lines
The vertical components of this column.
Definition: fragment.h:320
fragment * clipbox(fragment *contents)
Create a clipbox.
Definition: fragment.cc:916
virtual size_t trailing_width(size_t first_indent, size_t rest_indent) const =0
Stores information on a single column of fragments.
Definition: fragment.h:280
The namespace containing everything defined by cwidget.
Definition: columnify.cc:26
virtual ~fragment()
Nothing to do in the base class.
Definition: fragment.cc:36
bool proportional
If true, this column is allocated space proportionally; otherwise, its width is exactly what is speci...
Definition: fragment.h:285
align vert_align
The vertical alignment of the column.
Definition: fragment.h:308
fragment * hardwrapbox(fragment *contents)
Create a hardwrapbox.
Definition: fragment.cc:853
fragment * dropbox(fragment *header, fragment *contents)
Indent a paragraph, placing the given text on the first line.
Definition: fragment.cc:989
fragment * flowbox(fragment *contents)
Create a flowbox.
Definition: fragment.cc:600
virtual bool final_newline() const =0
fragment * style_fragment(fragment *f, const style &st)
Create a fragment which alters the style of its contents.
Definition: fragment.cc:204
Definition: center.h:14
virtual size_t max_width(size_t first_indent, size_t rest_indent) const =0
fragment * fragf(const char *format,...)
A printf-alike for fragments.
Definition: fragment.cc:1358
size_t width
If proportional is true, this is a number giving the relative size of this column compared to other p...
Definition: fragment.h:298
fragment_column_entry(bool _proportional, bool _expandable, size_t _width, align _vert_align, fragment *f)
Create a fragment column that has a single line.
Definition: fragment.h:323
virtual fragment_contents layout(size_t firstw, size_t w, const style &st)=0
Return all the lines of this fragment, given the "shape" of the fragment.
A fragment represents a logical unit of text.
Definition: fragment.h:37
fragment * indentbox(size_t firstindent, size_t restindent, fragment *contents)
Create an indentbox.
Definition: fragment.cc:982