]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxllist.h
Added more makefiles; fixed some samples for Cygwin
[wxWidgets.git] / user / wxLayout / wxllist.h
1 /*-*- c++ -*-********************************************************
2 * wxLayoutList.h - a formatted text rendering engine for wxWindows *
3 * *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8
9
10 #ifndef WXLLIST_H
11 #define WXLLIST_H
12
13 #ifdef __GNUG__
14 # pragma interface "wxllist.h"
15 #endif
16
17 #include "kbList.h"
18
19 #include "wx/wx.h"
20 #include "wx/print.h"
21 #include "wx/printdlg.h"
22 #include "wx/generic/printps.h"
23 #include "wx/generic/prntdlgg.h"
24
25 // skip the following defines if embedded in M application
26 #ifdef M_BASEDIR
27 # ifdef DEBUG
28 # define WXLAYOUT_DEBUG
29 # endif
30 #else
31 // for testing only:
32 # define WXLAYOUT_DEBUG
33 // The wxLayout classes can be compiled with std::string instead of wxString
34 //# define USE_STD_STRING
35 # define WXMENU_LAYOUT_LCLICK 1111
36 # define WXMENU_LAYOUT_RCLICK 1112
37 # define WXMENU_LAYOUT_DBLCLICK 1113
38 #endif
39
40 #ifdef USE_STD_STRING
41 # include <string>
42 typedef std::string String;
43 # define Str(str)(str.c_str())
44 #else
45 typedef wxString String;
46 # define Str(str) str
47 #endif
48
49 #define WXLO_DEFAULTFONTSIZE 12
50
51 /// Types of currently supported layout objects.
52 enum wxLayoutObjectType
53 { WXLO_TYPE_INVALID = 0, WXLO_TYPE_TEXT, WXLO_TYPE_CMD, WXLO_TYPE_ICON, WXLO_TYPE_LINEBREAK };
54
55 /// Type used for coordinates in drawing.
56 typedef long CoordType;
57
58 class wxLayoutList;
59 class wxLayoutObjectBase;
60
61 class wxDC;
62 class wxColour;
63 class wxFont;
64
65 /** The base class defining the interface to each object which can be
66 part of the layout. Each object needs to draw itself and calculate
67 its size.
68 */
69 class wxLayoutObjectBase
70 {
71 public:
72 struct UserData
73 {
74 virtual ~UserData() { }
75 };
76
77 /// return the type of this object
78 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_INVALID; } ;
79 /** Calculates the position etc of an object.
80 @param dc the wxDC to draw on
81 @param position where to draw the top left corner
82 @param baseLine the baseline for alignment, from top of box
83 */
84 virtual void Layout(wxDC & dc,
85 wxPoint position,
86 CoordType baseLine)
87 { m_Position = position; }
88
89 /** Draws an object.
90 @param dc the wxDC to draw on
91 @param translation to be added to coordinates
92 */
93 virtual void Draw(wxDC & dc, wxPoint const &translate) {}
94
95 /** Calculates and returns the size of the object.
96 @param baseLine pointer where to store the baseline position of
97 this object (i.e. the height from the top of the box to the
98 baseline)
99 @return the size of the object's box in pixels
100 */
101 virtual wxPoint GetSize(CoordType * baseLine = NULL) const
102 { return wxPoint(0,0); }
103
104 /** Calculates and returns the position of the object.
105 @return the size of the object's box in pixels
106 */
107 virtual wxPoint GetPosition(void) const { return m_Position; }
108
109 /// returns the number of cursor positions occupied by this object
110 virtual CoordType CountPositions(void) const { return 1; }
111
112 /// constructor
113 wxLayoutObjectBase() { m_UserData = NULL; }
114 /// delete the user data
115 virtual ~wxLayoutObjectBase() { if(m_UserData) delete m_UserData; }
116
117 #ifdef WXLAYOUT_DEBUG
118 virtual void Debug(void);
119 #endif
120
121 /// query whether coordinates have changed since last drawing
122 virtual bool IsDirty(void) const { return true; }
123
124 /** Tells the object about some user data. This data is associated
125 with the object and will be deleted at destruction time.
126 */
127 void SetUserData(UserData *data) { m_UserData = data; }
128 /** Return the user data. */
129 void * GetUserData(void) const { return m_UserData; }
130
131 private:
132 /// optional data for application's use
133 UserData *m_UserData;
134 protected:
135 wxPoint m_Position;
136 };
137
138 /// Define a list type of wxLayoutObjectBase pointers.
139 KBLIST_DEFINE(wxLayoutObjectList, wxLayoutObjectBase);
140
141
142 /// object for text block
143 class wxLayoutObjectText : public wxLayoutObjectBase
144 {
145 public:
146 wxLayoutObjectText(const String &txt);
147
148 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_TEXT; }
149 virtual void Layout(wxDC &dc, wxPoint position, CoordType
150 baseLine);
151
152 virtual void Draw(wxDC &dc, wxPoint const &translate);
153 /** This returns the height and in baseLine the position of the
154 text's baseline within it's box. This is needed to properly
155 align text objects.
156 */
157 virtual wxPoint GetSize(CoordType *baseLine = NULL) const;
158
159 #ifdef WXLAYOUT_DEBUG
160 virtual void Debug(void);
161 #endif
162
163 virtual CoordType CountPositions(void) const { return strlen(m_Text.c_str()); }
164 virtual bool IsDirty(void) const { return m_IsDirty; }
165
166 // for editing:
167 String & GetText(void) { return m_Text; }
168 void SetText(String const &text) { m_Text = text; }
169
170 private:
171 String m_Text;
172 /// size of the box containing text
173 long m_Width, m_Height;
174 /// the position of the baseline counted from the top of the box
175 long m_BaseLine;
176 /// coordinates have changed
177 bool m_IsDirty;
178 };
179
180 /// icon/pictures:
181 class wxLayoutObjectIcon : public wxLayoutObjectBase
182 {
183 public:
184 wxLayoutObjectIcon(wxIcon *icon);
185 wxLayoutObjectIcon(wxIcon const &icon);
186
187 ~wxLayoutObjectIcon() { delete m_Icon; }
188
189 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_ICON; }
190 virtual void Layout(wxDC &dc, wxPoint position, CoordType baseLine);
191 virtual void Draw(wxDC &dc, wxPoint const &translate);
192
193 virtual wxPoint GetSize(CoordType *baseLine = NULL) const;
194 virtual bool IsDirty(void) const { return m_IsDirty; }
195
196 private:
197 wxIcon *m_Icon;
198 /// coordinates have changed
199 bool m_IsDirty;
200 };
201
202 /// for export to html:
203 struct wxLayoutStyleInfo
204 {
205 int size, family, style, weight;
206 bool underline;
207 unsigned fg_red, fg_green, fg_blue;
208 unsigned bg_red, bg_green, bg_blue;
209 };
210
211 /// pseudo-object executing a formatting command in Draw()
212 class wxLayoutObjectCmd : public wxLayoutObjectBase
213 {
214 public:
215 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_CMD; }
216 virtual void Draw(wxDC &dc, wxPoint const &translate);
217 virtual void Layout(wxDC &dc, wxPoint position, CoordType baseLine);
218 wxLayoutObjectCmd(int size, int family, int style, int weight,
219 bool underline,
220 wxColour const *fg, wxColour const *bg);
221 ~wxLayoutObjectCmd();
222 /// caller must free pointer:
223 wxLayoutStyleInfo *GetStyle(void) const ;
224 /// return the background colour for setting colour of window
225 wxColour const *GetBGColour(void) const { return m_ColourBG; }
226 private:
227 /// the font to use
228 wxFont *m_font;
229 /// foreground colour
230 wxColour const *m_ColourFG;
231 /// background colour
232 wxColour const *m_ColourBG;
233 };
234
235 /// this object doesn't do anything at all
236 class wxLayoutObjectLineBreak : public wxLayoutObjectBase
237 {
238 public:
239 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_LINEBREAK; }
240 };
241
242
243 class wxLayoutPrintout;
244
245 class wxLayoutMargins
246 {
247 public:
248 wxLayoutMargins() { top = left = 0; bottom = right = -1; }
249 int top;
250 int left;
251 int bottom;
252 int right;
253 };
254
255 /**
256 This class provides a high level abstraction to the wxFText
257 classes.
258 It handles most of the character events with its own callback
259 functions, providing an editing ability. All events which cannot be
260 handled get passed to the parent window's handlers.
261 */
262 class wxLayoutList : public wxLayoutObjectList
263 {
264 public:
265 wxLayoutList();
266
267 /// Destructor.
268 ~wxLayoutList();
269
270 /// adds an object:
271 void AddObject(wxLayoutObjectBase *obj);
272 /// adds a text object
273 void AddText(String const &txt);
274 /// adds a line break
275 void LineBreak(void);
276 /// sets font parameters
277 void SetFont(int family, int size, int style,
278 int weight, int underline,
279 wxColour const *fg,
280 wxColour const *bg);
281 /// sets font parameters, colours by name
282 void SetFont(int family=-1, int size = -1, int style=-1,
283 int weight=-1, int underline = -1,
284 char const *fg = NULL,
285 char const *bg = NULL);
286 /// changes to the next larger font size
287 inline void SetFontLarger(void)
288 { SetFont(-1,(12*m_FontPtSize)/10); }
289 /// changes to the next smaller font size
290 inline void SetFontSmaller(void)
291 { SetFont(-1,(10*m_FontPtSize)/12); }
292
293 /// set font family
294 inline void SetFontFamily(int family) { SetFont(family); }
295 /// set font size
296 inline void SetFontSize(int size) { SetFont(-1,size); }
297 /// set font style
298 inline void SetFontStyle(int style) { SetFont(-1,-1,style); }
299 /// set font weight
300 inline void SetFontWeight(int weight) { SetFont(-1,-1,-1,weight); }
301 /// toggle underline flag
302 inline void SetFontUnderline(bool ul) { SetFont(-1,-1,-1,-1,(int)ul); }
303 /// set font colours by name
304 inline void SetFontColour(char const *fg, char const *bg = NULL) { SetFont(-1,-1,-1,-1,-1,fg,bg); }
305
306 /** Sets the wrap margin in cursor positions.
307 @param n the wrap margin, -1 to disable auto wrap
308 */
309 void SetWrapMargin(long n = -1);
310
311 /// Wraps the current line if word wrap is enabled.
312 void WrapLine(void);
313
314 /** Re-layouts the list on a DC.
315 @param dc the dc to layout for
316 @param margins if not NULL, use these top and left margins
317 */
318 void Layout(wxDC &dc, wxLayoutMargins *margins = NULL);
319
320 /** Draw the list on a given DC.
321 @param dc the dc to layout for
322 @param fromLine the first graphics line from where to draw
323 @param toLine the last line at which to draw
324 @param start if != iterator(NULL) start drawing from here
325 */
326 void Draw(wxDC &dc,
327 CoordType fromLine = -1,
328 CoordType toLine = -1,
329 iterator start = iterator(NULL),
330 wxPoint const &translate = wxPoint(0,0));
331
332 /** Deletes at least to the end of line and redraws */
333 void EraseAndDraw(wxDC &dc, iterator start = iterator(NULL),
334 wxPoint const &translate = wxPoint(0,0));
335
336 /** Finds the object occupying a certain screen position.
337 @return pointer to wxLayoutObjectBase or NULL if not found
338 */
339 wxLayoutObjectBase *Find(wxPoint coords) const;
340
341 #ifdef WXLAYOUT_DEBUG
342 void Debug(void);
343 void ShowCurrentObject();
344 #endif
345
346 /// dirty?
347 bool IsDirty() const { return m_bModified; }
348 bool CursorMoved(void) const { return m_CursorMoved; }
349
350 /// called after the contents is saved, for example
351 void ResetDirty() { m_bModified = FALSE; }
352
353 /// for access by wxLayoutWindow:
354 void GetSize(CoordType *max_x, CoordType *max_y,
355 CoordType *lineHeight);
356
357
358 /**@name Functionality for editing */
359 //@{
360 /// set list editable or read only
361 void SetEditable(bool editable = true) { m_Editable = editable; }
362 /// return true if list is editable
363 bool IsEditable(void) const { return m_Editable; }
364 /// move cursor, returns true if it could move to the desired position
365 bool MoveCursor(int dx = 0, int dy = 0);
366 void SetCursor(wxPoint const &p);
367 void DrawCursor(wxDC &dc, bool erase = false,wxPoint const &translate = wxPoint(0,0));
368 /// Get current cursor position cursor coords
369 wxPoint GetCursor(void) const { return m_CursorPos; }
370 /// Gets graphical coordinates of cursor
371 wxPoint GetCursorCoords(void) const { return m_CursorCoords; }
372
373 /// delete one or more cursor positions
374 void Delete(CoordType count = 1);
375 void Insert(String const &text);
376 void Insert(wxLayoutObjectBase *obj);
377 void Clear(int family = wxROMAN, int size=WXLO_DEFAULTFONTSIZE, int style=wxNORMAL, int weight=wxNORMAL,
378 int underline=0, char const *fg="black", char const *bg="white");
379
380 /// return a pointer to the default settings (dangerous, why?) FIXME:
381 wxLayoutObjectCmd const *GetDefaults(void) const { return m_DefaultSetting ; }
382
383 /// returns the iterator for the object under the cursor
384 wxLayoutObjectList::iterator GetCurrentObject(CoordType *offset =
385 NULL)
386 { if(offset) *offset = m_CursorOffset; return m_CursorObject; }
387
388 // get the length of the line with the object pointed to by i, offs
389 // only used to decide whether we are before or after linebreak
390 CoordType GetLineLength(wxLayoutObjectList::iterator i,
391 CoordType offs = 0);
392 wxLayoutPrintout *MakePrintout(wxString const &name);
393
394 /// Return maximum X,Y coordinates
395 wxPoint GetSize(void) const { return wxPoint(m_MaxX, m_MaxY); }
396
397 /// calculates current cursor coordinates, called in Layout()
398 void CalculateCursor(wxDC &dc);
399
400 /// toggle normal/bold cursor
401 void SetBoldCursor(bool bold = true)
402 { m_boldCursor = bold; m_CursorMoved = true;}
403 //@}
404 protected:
405 /// font parameters:
406 int m_FontFamily, m_FontStyle, m_FontWeight;
407 int m_FontPtSize;
408 bool m_FontUnderline;
409 /// colours:
410 wxColour const * m_ColourFG;
411 wxColour const * m_ColourBG;
412 /// the default setting:
413 wxLayoutObjectCmd *m_DefaultSetting;
414
415 /// needs recalculation?
416 bool m_dirty;
417 /// cursor moved
418 bool m_CursorMoved;
419
420 /// needs saving (i.e., was modified?)
421 bool m_bModified;
422
423 // the currently updated line:
424 /// where do we draw next:
425 wxPoint m_Position;
426 /// the height of the current line:
427 CoordType m_LineHeight;
428 /// maximum drawn x position so far
429 CoordType m_MaxX;
430 /// maximum drawn y position:
431 CoordType m_MaxY;
432
433 //---- this is needed for editing:
434 /// where is the text cursor (column,line):
435 wxPoint m_CursorPos;
436 /// where to draw the cursor
437 wxPoint m_CursorCoords;
438 /// how large to draw it
439 wxPoint m_CursorSize;
440 /// object iterator for current cursor position:
441 iterator m_CursorObject;
442 /// position of cursor within m_CursorObject:
443 CoordType m_CursorOffset;
444
445 /// to store content overwritten by cursor
446 wxMemoryDC m_CursorMemDC;
447
448 /// which is the last line
449 CoordType m_MaxLine;
450 /// can we edit it?
451 bool m_Editable;
452 /// find the object to the cursor position and returns the offset
453 /// in there
454 wxLayoutObjectList::iterator FindObjectCursor(wxPoint *cpos, CoordType *offset = NULL);
455 /// get the wrap margin
456 inline long GetWrapMargin(void) const { return m_WrapMargin; }
457 /// do we do wrapping?
458 inline bool DoWordWrap(void) const { return m_WrapMargin != -1; }
459 private:
460 /// Resets the font settings etc to default values
461 void ResetSettings(wxDC &dc);
462 /// remembers the last cursor position for which FindObjectCursor was called
463 wxPoint m_FoundCursor;
464 /// remembers the iterator to the object related to m_FoundCursor
465 wxLayoutObjectList::iterator m_FoundIterator;
466 /// the wrap margin
467 long m_WrapMargin;
468 /// draw a bold cursor?
469 bool m_boldCursor;
470 };
471
472 class wxLayoutPrintout: public wxPrintout
473 {
474 public:
475 wxLayoutPrintout(wxLayoutList &llist, wxString const & title =
476 "wxLayout Printout");
477 bool OnPrintPage(int page);
478 bool HasPage(int page);
479 bool OnBeginDocument(int startPage, int endPage);
480 void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int
481 *selPageTo);
482 void OnPreparePrinting(void);
483 protected:
484 virtual void DrawHeader(wxDC &dc, wxPoint topleft, wxPoint bottomright, int pageno);
485
486 private:
487 wxLayoutList *m_llist;
488 wxString m_title;
489 int m_PageHeight, m_PageWidth;
490 // how much we actually print per page
491 int m_PrintoutHeight;
492 wxLayoutMargins m_Margins;
493 int m_NumOfPages;
494 };
495
496 #endif // WXLLIST_H