cwidget  0.5.16
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 
148  fragment *newline_fragment();
149 
157  fragment *style_fragment(fragment *f,
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 
347  fragment_column_entry()
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