]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxllist.h
84a1e1c7c647807e9bb9ac34e01ce1a3ab1b047e
[wxWidgets.git] / user / wxLayout / wxllist.h
1 /*-*- c++ -*-********************************************************
2 * wxLayoutList.h - a formatted text rendering engine for wxWindows *
3 * *
4 * (C) 1999 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 #include "wx/dataobj.h"
25
26 // skip the following defines if embedded in M application
27 #ifndef M_BASEDIR
28 # define WXMENU_LAYOUT_LCLICK 1111
29 # define WXMENU_LAYOUT_RCLICK 1112
30 # define WXMENU_LAYOUT_DBLCLICK 1113
31 #endif
32
33 // do not enable debug mode within Mahogany
34 #if defined(__WXDEBUG__) && ! defined(M_BASEDIR)
35 # define WXLAYOUT_DEBUG
36 #endif
37
38 #ifdef WXLAYOUT_DEBUG
39 # define WXLO_TRACE(x) wxLogDebug(x)
40 #else
41 # define WXLO_TRACE(x)
42 #endif
43
44 #define WXLO_DEBUG_URECT 0
45
46 #ifndef WXLO_DEFAULTFONTSIZE
47 # define WXLO_DEFAULTFONTSIZE 12
48 #endif
49
50 #ifdef __WXMSW__
51 # define WXLO_BITMAP_FORMAT wxBITMAP_TYPE_BMP
52 #else
53 # define WXLO_BITMAP_FORMAT wxBITMAP_TYPE_PNG
54 #endif
55
56
57 /// Types of currently supported layout objects.
58 enum wxLayoutObjectType
59 {
60 /// illegal object type, should never appear
61 WXLO_TYPE_INVALID = 0,
62 /// text object, containing normal text
63 WXLO_TYPE_TEXT,
64 /// command object, containing font or colour changes
65 WXLO_TYPE_CMD,
66 /// icon object, any kind of image
67 WXLO_TYPE_ICON
68 };
69
70 /// Type used for coordinates in drawing. Must be signed.
71 typedef long CoordType;
72
73 // Forward declarations.
74 class wxLayoutList;
75 class wxLayoutObject;
76 class wxDC;
77 class wxColour;
78 class wxFont;
79
80
81 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
82
83 The wxLayout objects which make up the lines.
84
85 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
86
87 /** The base class defining the interface to each object which can be
88 part of the layout. Each object needs to draw itself and calculate
89 its size.
90 */
91 class wxLayoutObject
92 {
93 public:
94 /** This structure can be used to contain data associated with the
95 object.
96 It is refcounted, so the caller has to do a DecRef() on it
97 instead of a delete.
98 */
99 struct UserData
100 {
101 UserData() { m_refcount = 1; }
102 inline void IncRef(void) { m_refcount++; }
103 inline void DecRef(void) { m_refcount--; if(m_refcount == 0) delete this;}
104 inline void SetLabel(const wxString &l) { m_label = l; }
105 inline const wxString & GetLabel(void) const { return m_label; }
106 private:
107 int m_refcount;
108 wxString m_label;
109 protected:
110 virtual ~UserData() { wxASSERT(m_refcount == 0); }
111 /// prevents gcc from generating stupid warnings
112 friend class dummy_UserData;
113 };
114
115 /// return the type of this object
116 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_INVALID; }
117 /** Calculates the size of an object.
118 @param dc the wxDC to draw on
119 @param llist the wxLayoutList
120 */
121 virtual void Layout(wxDC &dc, class wxLayoutList *llist) = 0;
122
123 /** Draws an object.
124 @param dc the wxDC to draw on
125 @param coords where to draw the baseline of the object.
126 @param wxllist pointer to wxLayoutList
127 @param begin if !=-1, from which position on to highlight it
128 @param end if begin !=-1, how many positions to highlight it
129 */
130 virtual void Draw(wxDC & /* dc */,
131 wxPoint const & /* coords */,
132 class wxLayoutList *wxllist,
133 CoordType begin = -1,
134 CoordType end = -1) { }
135
136 /** Calculates and returns the size of the object.
137 @param top where to store height above baseline
138 @param bottom where to store height below baseline
139 @return the size of the object's box in pixels
140 */
141 virtual wxPoint GetSize(CoordType * top, CoordType *bottom) const
142 { *top = 0; *bottom = 0; return wxPoint(0,0); }
143
144 /// Return just the width of the object on the screen.
145 virtual CoordType GetWidth(void) const { return 0; }
146 /// returns the number of cursor positions occupied by this object
147 virtual CoordType GetLength(void) const { return 1; }
148 /** Returns the cursor offset relating to the screen x position
149 relative to begin of object.
150 @param dc the wxDC to use for calculations
151 @param xpos relative x position from head of object
152 @return cursor coordinate offset
153 */
154 virtual CoordType GetOffsetScreen(wxDC &dc, CoordType xpos) const { return 0; }
155
156 /// constructor
157 wxLayoutObject() { m_UserData = NULL; }
158 /// delete the user data
159 virtual ~wxLayoutObject() { if(m_UserData) m_UserData->DecRef(); }
160
161 #ifdef WXLAYOUT_DEBUG
162 virtual void Debug(void);
163 #endif
164
165 /** Tells the object about some user data. This data is associated
166 with the object and will be deleted at destruction time.
167 It is reference counted.
168 */
169 void SetUserData(UserData *data)
170 {
171 if(m_UserData)
172 m_UserData->DecRef();
173 m_UserData = data;
174 if(m_UserData)
175 m_UserData->IncRef();
176 }
177
178 /** Return the user data.
179 Increments the object's reference count. When no longer needed,
180 caller must call DecRef() on the pointer returned.
181 */
182 UserData * GetUserData(void) const { if(m_UserData) m_UserData->IncRef(); return m_UserData; }
183
184 /** Makes a copy of this object.
185 */
186 virtual wxLayoutObject *Copy(void) = 0;
187
188 /** Clipboard support function. Read and write objects to
189 strings. */
190 //@{
191 /// Writes the object to the string.
192 virtual void Write(wxString &ostr) = 0;
193 /** Reads an object.
194 @param str stream to read from, will bee changed
195 @return true on success
196 */
197 static wxLayoutObject *Read(wxString &istr);
198 //@}
199 protected:
200 /// optional data for application's use
201 UserData *m_UserData;
202 };
203
204 /// Define a list type of wxLayoutObject pointers.
205 KBLIST_DEFINE(wxLayoutObjectList, wxLayoutObject);
206
207 /// An illegal iterator to save typing.
208 #define NULLIT (wxLayoutObjectList::iterator(NULL))
209 /// The iterator type.
210 #define wxLOiterator wxLayoutObjectList::iterator
211
212 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
213
214 wxLayoutObjectText
215
216 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
217 /** This class implements a wxLayoutObject holding plain text.
218 */
219 class wxLayoutObjectText : public wxLayoutObject
220 {
221 public:
222 wxLayoutObjectText(const wxString &txt = "");
223
224 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_TEXT; }
225 virtual void Layout(wxDC &dc, class wxLayoutList *llist);
226 virtual void Draw(wxDC &dc, wxPoint const &coords,
227 class wxLayoutList *wxllist,
228 CoordType begin = -1,
229 CoordType end = -1);
230 /** Calculates and returns the size of the object.
231 @param top where to store height above baseline
232 @param bottom where to store height below baseline
233 @return the size of the object's box in pixels
234 */
235 virtual wxPoint GetSize(CoordType * top, CoordType *bottom) const;
236 /// Return just the width of the object on the screen.
237 virtual CoordType GetWidth(void) const { return m_Width; }
238 /** Returns the cursor offset relating to the screen x position
239 relative to begin of object.
240 @param dc the wxDC to use for calculations
241 @param xpos relative x position from head of object
242 @return cursor coordinate offset
243 */
244 virtual CoordType GetOffsetScreen(wxDC &dc, CoordType xpos) const;
245
246 virtual void Write(wxString &ostr);
247 static wxLayoutObjectText *Read(wxString &istr);
248
249 #ifdef WXLAYOUT_DEBUG
250 virtual void Debug(void);
251 #endif
252
253 virtual CoordType GetLength(void) const { return strlen(m_Text.c_str()); }
254
255 // for editing:
256 wxString & GetText(void) { return m_Text; }
257 void SetText(wxString const &text) { m_Text = text; }
258 /** Makes a copy of this object.
259 */
260 virtual wxLayoutObject *Copy(void);
261 private:
262 wxString m_Text;
263 /// size of the box containing text
264 long m_Width, m_Height;
265 /// Height above baseline.
266 long m_Top;
267 /// Height below baseline.
268 long m_Bottom;
269 };
270
271 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
272
273 wxLayoutObjectIcon
274
275 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
276 /** This class implements a wxLayoutObject holding a graphic.
277 */
278 class wxLayoutObjectIcon : public wxLayoutObject
279 {
280 public:
281 wxLayoutObjectIcon(wxBitmap *icon = NULL);
282 wxLayoutObjectIcon(wxBitmap const &icon);
283
284 ~wxLayoutObjectIcon() { if(m_Icon) delete m_Icon; }
285
286 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_ICON; }
287 virtual void Layout(wxDC &dc, class wxLayoutList *llist);
288 virtual void Draw(wxDC &dc, wxPoint const &coords,
289 class wxLayoutList *wxllist,
290 CoordType begin = -1,
291 CoordType end = -1);
292
293 /** Calculates and returns the size of the object.
294 @param top where to store height above baseline
295 @param bottom where to store height below baseline
296 @return the size of the object's box in pixels
297 */
298 virtual wxPoint GetSize(CoordType * top, CoordType *bottom) const;
299 /// Return just the width of the object on the screen.
300 virtual CoordType GetWidth(void) const { return m_Icon->GetWidth(); }
301 // return a pointer to the icon
302 wxBitmap *GetIcon(void) const { return m_Icon; }
303 /** Makes a copy of this object.
304 */
305 virtual wxLayoutObject *Copy(void);
306 virtual void Write(wxString &ostr);
307 static wxLayoutObjectIcon *Read(wxString &istr);
308 private:
309 wxBitmap *m_Icon;
310 };
311
312 /** This structure holds all formatting information. Members which are
313 undefined (for a CmdObject this means: no change), are set to -1.
314 */
315 struct wxLayoutStyleInfo
316 {
317 wxLayoutStyleInfo(int ifamily = -1,
318 int isize = -1,
319 int istyle = -1,
320 int iweight = -1,
321 int iul = -1,
322 wxColour *fg = NULL,
323 wxColour *bg = NULL);
324 wxColour & GetBGColour()
325 {
326 return m_bg;
327 }
328 wxLayoutStyleInfo & operator=(const wxLayoutStyleInfo &right);
329 /// Font change parameters.
330 int size, family, style, weight, underline;
331 /// Colours
332 wxColour m_bg, m_fg;
333 int m_fg_valid, m_bg_valid; // bool, but must be int!
334 };
335
336
337 class wxFontCacheEntry
338 {
339 public:
340 wxFontCacheEntry(int family, int size, int style, int weight,
341 bool underline)
342 {
343 m_Family = family; m_Size = size; m_Style = style;
344 m_Weight = weight; m_Underline = underline;
345 m_Font = new wxFont(m_Size, m_Family,
346 m_Style, m_Weight, m_Underline);
347 }
348 bool Matches(int family, int size, int style, int weight,
349 bool underline) const
350 {
351 return size == m_Size && family == m_Family
352 && style == m_Style && weight == m_Weight
353 && underline == m_Underline;
354 }
355 wxFont & GetFont(void) { return *m_Font; }
356 ~wxFontCacheEntry()
357 {
358 delete m_Font;
359 }
360 private:
361 wxFont *m_Font;
362 int m_Family, m_Size, m_Style, m_Weight;
363 bool m_Underline;
364 };
365
366 KBLIST_DEFINE(wxFCEList, wxFontCacheEntry);
367
368 class wxFontCache
369 {
370 public:
371 wxFont & GetFont(int family, int size, int style, int weight,
372 bool underline);
373 wxFont & GetFont(wxLayoutStyleInfo const &si)
374 {
375 return GetFont(si.family, si.size, si.style, si.weight, si.underline);
376 }
377 private:
378 wxFCEList m_FontList;
379 };
380
381 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
382
383 wxLayoutObjectCmd
384
385 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
386 /** This class implements a wxLayoutObject holding style change commands.
387 */
388 class wxLayoutObjectCmd : public wxLayoutObject
389 {
390 public:
391 virtual wxLayoutObjectType GetType(void) const { return WXLO_TYPE_CMD; }
392 virtual void Layout(wxDC &dc, class wxLayoutList *llist);
393 virtual void Draw(wxDC &dc, wxPoint const &coords,
394 class wxLayoutList *wxllist,
395 CoordType begin = -1,
396 CoordType end = -1);
397 wxLayoutObjectCmd(int family = -1,
398 int size = -1,
399 int style = -1,
400 int weight = -1,
401 int underline = -1,
402 wxColour *fg = NULL,
403 wxColour *bg = NULL);
404 ~wxLayoutObjectCmd();
405 /** Stores the current style in the styleinfo structure */
406 wxLayoutStyleInfo * GetStyle(void) const;
407 /** Makes a copy of this object.
408 */
409 virtual wxLayoutObject *Copy(void);
410 virtual void Write(wxString &ostr);
411 static wxLayoutObjectCmd *Read(wxString &istr);
412 private:
413 wxLayoutStyleInfo *m_StyleInfo;
414 };
415
416 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
417
418 The wxLayoutLine object
419
420 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
421
422 /// forward declaration
423 class wxLayoutList;
424
425 /** This class represents a single line of objects to be displayed.
426 It knows its height and total size and whether it needs to be
427 redrawn or not.
428 It has pointers to its first and next line so it can automatically
429 update them as needed.
430 */
431 class wxLayoutLine
432 {
433 public:
434 /** Constructor.
435 @param prev pointer to previous line or NULL
436 @param next pointer to following line or NULL
437 @param llist pointer to layout list
438 */
439 wxLayoutLine(wxLayoutLine *prev, wxLayoutList *llist);
440 /** This function inserts a new object at cursor position xpos.
441 @param xpos where to insert new object
442 @param obj the object to insert
443 @return true if that xpos existed and the object was inserted
444 */
445 bool Insert(CoordType xpos, wxLayoutObject *obj);
446
447 /** This function inserts text at cursor position xpos.
448 @param xpos where to insert
449 @param text the text to insert
450 @return true if that xpos existed and the object was inserted
451 */
452 bool Insert(CoordType xpos, wxString text);
453
454 /** This function appends an object to the line.
455 @param obj the object to insert
456 */
457 void Append(wxLayoutObject * obj)
458 {
459 wxASSERT(obj);
460 m_ObjectList.push_back(obj);
461 m_Length += obj->GetLength();
462 }
463
464 /** This function appens the next line to this, i.e. joins the two
465 lines into one.
466 */
467 void MergeNextLine(wxLayoutList *llist);
468
469 /** This function deletes npos cursor positions from position xpos.
470 @param xpos where to delete
471 @param npos how many positions
472 @return number of positions still to be deleted
473 */
474 CoordType Delete(CoordType xpos, CoordType npos);
475
476 /** This function breaks the line at a given cursor position.
477 @param xpos where to break it
478 @return pointer to the new line object replacing the old one
479 */
480 wxLayoutLine *Break(CoordType xpos, wxLayoutList *llist);
481
482 /** Deletes the next word from this position, including leading
483 whitespace.
484 This function does not delete over font changes, i.e. a word
485 with formatting instructions in the middle of it is treated as
486 two (three actually!) words. In fact, if the cursor is on a non-text object, that
487 one is treated as a word.
488 @param xpos from where to delete
489 @return true if a word was deleted
490 */
491 bool DeleteWord(CoordType npos);
492
493 /** Finds a suitable position left to the given column to break the
494 line.
495 @param column we want to break the line to the left of this
496 @return column for breaking line or -1 if no suitable location found
497 */
498 CoordType GetWrapPosition(CoordType column);
499
500 /** Finds the object which covers the cursor position xpos in this
501 line.
502 @param xpos the column number
503 @param offset where to store the difference between xpos and
504 the object's head
505 @return iterator to the object or NULLIT
506 */
507 wxLayoutObjectList::iterator FindObject(CoordType xpos, CoordType
508 *offset) const ;
509
510 /** Finds the object which covers the screen position xpos in this
511 line.
512 @param dc the wxDC to use for calculations
513 @param xpos the screen x coordinate
514 @param offset where to store the difference between xpos and
515 the object's head
516 @return iterator to the object or NULLIT
517 */
518 wxLayoutObjectList::iterator FindObjectScreen(wxDC &dc,
519 CoordType xpos,
520 CoordType *offset,
521 bool *found = NULL) const ;
522
523 /** Finds text in this line.
524 @param needle the text to find
525 @param xpos the position where to start the search
526 @return the cursoor coord where it was found or -1
527 */
528 CoordType FindText(const wxString &needle, CoordType xpos = 0) const;
529
530 /** Get the first object in the list. This is used by the wxlparser
531 functions to export the list.
532 @return iterator to the first object
533 */
534 wxLayoutObjectList::iterator GetFirstObject(void)
535 {
536 return m_ObjectList.begin();
537 }
538
539 /** Deletes this line, returns pointer to next line.
540 @param update If true, update all following lines.
541 */
542 wxLayoutLine *DeleteLine(bool update, wxLayoutList *llist);
543
544 /**@name Cursor Management */
545 //@{
546 /** Return the line number of this line.
547 @return the line number
548 */
549 inline CoordType GetLineNumber(void) const { return m_LineNumber; }
550 /** Return the length of the line.
551 @return line lenght in cursor positions
552 */
553 inline CoordType GetLength(void) const { return m_Length; }
554 //@}
555
556 /**@name Drawing and Layout */
557 //@{
558 /** Draws the line on a wxDC.
559 @param dc the wxDC to draw on
560 @param llist the wxLayoutList
561 @param offset an optional offset to shift printout
562 */
563 void Draw(wxDC &dc,
564 wxLayoutList *llist,
565 const wxPoint &offset = wxPoint(0,0)) const;
566
567 /** Recalculates the positions of objects and the height of the
568 line.
569 @param dc the wxDC to draw on
570 @param llist th e wxLayoutList
571 @param cursorPos if not NULL, set cursor screen position in there
572 @param cursorSize if not cursorPos != NULL, set cursor size in there
573 @param cx if cursorPos != NULL, the cursor x position
574 */
575 void Layout(wxDC &dc,
576 wxLayoutList *llist,
577 wxPoint *cursorPos = NULL,
578 wxPoint *cursorSize = NULL,
579 int cx = 0);
580 /** This function finds an object belonging to a given cursor
581 position. It assumes that Layout() has been called before.
582 @param dc the wxDC to use for calculations
583 @param xpos screen x position
584 @param found if non-NULL set to false if we return the last
585 object before the cursor, to true if we really have an object
586 for that position
587 @return pointer to the object
588 */
589 wxLayoutObject * FindObjectScreen(wxDC &dc, CoordType xpos, bool
590 *found = NULL);
591
592 //@}
593
594 /**@name List traversal */
595 //@{
596 /// Returns pointer to next line.
597 wxLayoutLine *GetNextLine(void) const { return m_Next; }
598 /// Returns pointer to previous line.
599 wxLayoutLine *GetPreviousLine(void) const { return m_Previous; }
600 /// Sets the link to the next line.
601 void SetNext(wxLayoutLine *next)
602 { m_Next = next; if(next) next->m_Previous = this; }
603 /// Sets the link to the previous line.
604 void SetPrevious(wxLayoutLine *previous)
605 { m_Previous = previous; if(previous) previous->m_Next = this; }
606 //@}
607
608 /// Returns the position of this line on the canvas.
609 wxPoint GetPosition(void) const { return m_Position; }
610 /// Returns the height of this line.
611 CoordType GetHeight(void) const { return m_Height; }
612 /// Returns the width of this line.
613 CoordType GetWidth(void) const { return m_Width; }
614 /** This will recalculate the position and size of this line.
615 If called recursively it will abort if the position of an
616 object is unchanged, assuming that none of the following
617 objects need to move.
618 @param recurse if greater 0 then it will be used as the
619 minimum(!) recursion level, continue with all lines till the end of
620 the list or until the coordinates no longer changed.
621 */
622 void RecalculatePositions(int recurse, wxLayoutList *llist);
623 /// Recalculates the position of this line on the canvas.
624 wxPoint RecalculatePosition(wxLayoutList *llist);
625
626 /** Copies the contents of this line to another wxLayoutList
627 @param llist the wxLayoutList destination
628 @param from x cursor coordinate where to start
629 @param to x cursor coordinate where to stop, -1 for end of line
630 */
631 void Copy(wxLayoutList *llist,
632 CoordType from = 0,
633 CoordType to = -1);
634
635 #ifdef WXLAYOUT_DEBUG
636 void Debug(void);
637 #endif
638 wxLayoutStyleInfo &GetStyleInfo() { return m_StyleInfo; }
639
640 /// Returns dirty state
641 bool IsDirty(void) const { return m_Dirty; }
642 /// Marks line as diry.
643 void MarkDirty(void) { m_Dirty = true; }
644 private:
645 /// Destructor is private. Use DeleteLine() to remove it.
646 ~wxLayoutLine();
647
648 /**@name Functions to let the lines synchronise with each other. */
649 //@{
650 /** Sets the height of this line. Will mark following lines as
651 dirty.
652 @param height new height
653 */
654 void SetHeight(CoordType height, wxLayoutList *llist)
655 { m_Height = height; RecalculatePositions(true, llist); }
656
657 /** Moves the linenumbers one on, because a line has been inserted
658 or deleted.
659 @param delta either +1 or -1
660 */
661 void MoveLines(int delta)
662 {
663 m_LineNumber += delta;
664 if(m_Next) m_Next->MoveLines(delta);
665 }
666 //@}
667 private:
668 /// The line number.
669 CoordType m_LineNumber;
670 /// The line length in cursor positions.
671 CoordType m_Length;
672 /// The total height of the line.
673 CoordType m_Height;
674 /// The total width of the line on screen.
675 CoordType m_Width;
676 /// The baseline for drawing objects
677 CoordType m_BaseLine;
678 /// The position on the canvas.
679 wxPoint m_Position;
680 /// The list of objects
681 wxLayoutObjectList m_ObjectList;
682 /// Have we been changed since the last layout?
683 bool m_Dirty;
684 /// Pointer to previous line if it exists.
685 wxLayoutLine *m_Previous;
686 /// Pointer to next line if it exists.
687 wxLayoutLine *m_Next;
688 /// A StyleInfo structure, holding the current settings.
689 wxLayoutStyleInfo m_StyleInfo;
690 /// Just to suppress gcc compiler warnings.
691 friend class dummy;
692 private:
693 wxLayoutLine(const wxLayoutLine &);
694 };
695
696
697 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
698
699 The wxLayoutList object
700
701 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
702 /** The wxLayoutList is a list of wxLayoutLine objects. It provides a
703 higher level of abstraction for the text and can generally be considered
704 as representing "the text".
705 */
706 class wxLayoutList
707 {
708 public:
709 /// Constructor.
710 wxLayoutList();
711 /// Destructor.
712 ~wxLayoutList();
713
714 /// Clear the list.
715 void Clear(int family = wxROMAN,
716 int size=WXLO_DEFAULTFONTSIZE,
717 int style=wxNORMAL,
718 int weight=wxNORMAL,
719 int underline=0,
720 wxColour *fg=NULL,
721 wxColour *bg=NULL);
722 /// Empty: clear the list but leave font settings.
723 void Empty(void);
724
725 /**@name Cursor Management */
726 //@{
727 /** Set new cursor position.
728 @param p new position
729 @return bool if it could be set
730 */
731 bool MoveCursorTo(wxPoint const &p);
732 /** Move cursor up or down.
733 @param n
734 @return bool if it could be moved
735 */
736 bool MoveCursorVertically(int n);
737 /** Move cursor left or right.
738 @param n
739 @return bool if it could be moved
740 */
741 bool MoveCursorHorizontally(int n);
742
743 /// Move cursor to end of line.
744 void MoveCursorToEndOfLine(void)
745 {
746 wxASSERT(m_CursorLine);
747 MoveCursorHorizontally(m_CursorLine->GetLength()-m_CursorPos.x);
748 }
749
750 /// Move cursor to begin of line.
751 void MoveCursorToBeginOfLine(void)
752 { MoveCursorHorizontally(-m_CursorPos.x); }
753
754 /// Returns current cursor position.
755 const wxPoint &GetCursorPos(wxDC &dc) const { return m_CursorPos; }
756 const wxPoint &GetCursorPos() const { return m_CursorPos; }
757
758 //@}
759
760 /**@name Editing functions.
761 All of these functions return true on success and false on
762 failure. */
763 //@{
764 /// Insert text at current cursor position.
765 bool Insert(wxString const &text);
766 /// Insert some other object at current cursor position.
767 bool Insert(wxLayoutObject *obj);
768 /// Inserts objects at current cursor positions
769 bool Insert(wxLayoutList *llist);
770
771 /// Inserts a linebreak at current cursor position.
772 bool LineBreak(void);
773 /** Wraps the current line. Searches to the left of the cursor to
774 break the line. Does nothing if the cursor position is before
775 the break position parameter.
776 @param column the break position for the line, maximum length
777 @return true if line got broken
778 */
779 bool WrapLine(CoordType column);
780 /** This function deletes npos cursor positions.
781 @param npos how many positions
782 @return true if everything got deleted
783 */
784 bool Delete(CoordType npos);
785
786 /** Delete the next n lines.
787 @param n how many lines to delete
788 @return how many it could not delete
789 */
790 int DeleteLines(int n);
791
792 /// Delete to end of line.
793 void DeleteToEndOfLine(void)
794 {
795 wxASSERT(m_CursorLine);
796 Delete(m_CursorLine->GetLength()-m_CursorPos.x);
797 }
798 /// Delete to begin of line.
799 void DeleteToBeginOfLine(void)
800 {
801 wxASSERT(m_CursorLine);
802 CoordType n = m_CursorPos.x;
803 #ifdef WXLAYOUT_DEBUG
804 wxASSERT(MoveCursorHorizontally(-n));
805 #else
806 MoveCursorHorizontally(-n);
807 #endif
808 Delete(n);
809 }
810
811 /** Delete the next word.
812 */
813 void DeleteWord(void)
814 {
815 wxASSERT(m_CursorLine);
816 m_CursorLine->DeleteWord(m_CursorPos.x);
817 }
818
819 //@}
820
821 /** Finds text in this list.
822 @param needle the text to find
823 @param cpos the position where to start the search
824 @return the cursoor coord where it was found or (-1,-1)
825 */
826 wxPoint FindText(const wxString &needle, const wxPoint &cpos = wxPoint(0,0)) const;
827
828 /**@name Formatting options */
829 //@{
830 /// sets font parameters
831 void SetFont(int family, int size, int style,
832 int weight, int underline,
833 wxColour *fg,
834 wxColour *bg);
835 /// sets font parameters, colours by name
836 void SetFont(int family=-1, int size = -1, int style=-1,
837 int weight=-1, int underline = -1,
838 char const *fg = NULL,
839 char const *bg = NULL);
840 /// changes to the next larger font size
841 inline void SetFontLarger(void)
842 { SetFont(-1,(12*m_CurrentSetting.size)/10); }
843 /// changes to the next smaller font size
844 inline void SetFontSmaller(void)
845 { SetFont(-1,(10*m_CurrentSetting.size)/12); }
846
847 /// set font family
848 inline void SetFontFamily(int family) { SetFont(family); }
849 /// set font size
850 inline void SetFontSize(int size) { SetFont(-1,size); }
851 /// set font style
852 inline void SetFontStyle(int style) { SetFont(-1,-1,style); }
853 /// set font weight
854 inline void SetFontWeight(int weight) { SetFont(-1,-1,-1,weight); }
855 /// toggle underline flag
856 inline void SetFontUnderline(bool ul) { SetFont(-1,-1,-1,-1,(int)ul); }
857 /// set font colours by name
858 inline void SetFontColour(char const *fg, char const *bg = NULL)
859 { SetFont(-1,-1,-1,-1,-1,fg,bg); }
860 /// set font colours by colour
861 inline void SetFontColour(wxColour *fg, wxColour *bg = NULL)
862 { SetFont(-1,-1,-1,-1,-1,fg,bg); }
863
864
865 /**
866 Returns a pointer to the default settings.
867 This is only valid temporarily and should not be stored
868 anywhere.
869 @return the default settings of the list
870 */
871 wxLayoutStyleInfo *GetDefaults(void) { return &m_DefaultSetting ; }
872 wxLayoutStyleInfo *GetStyleInfo(void) { return &m_CurrentSetting ; }
873 //@}
874
875 /**@name Drawing */
876 //@{
877 /** Draws the complete list on a wxDC.
878 @param dc the wxDC to draw on
879 @param offset an optional offset to shift printout
880 @param top optional y coordinate where to start drawing
881 @param bottom optional y coordinate where to stop drawing
882 */
883 void Draw(wxDC &dc,
884 const wxPoint &offset = wxPoint(0,0),
885 CoordType top = -1, CoordType bottom = -1);
886
887 /** Calculates new layout for the list, like Draw() but does not
888 actually draw it.
889 @param dc the wxDC to draw on
890 @param bottom optional y coordinate where to stop calculating
891 @param forceAll force re-layout of all lines
892 */
893 void Layout(wxDC &dc, CoordType bottom = -1, bool forceAll = false);
894
895 /** Calculates new sizes for everything in the list, like Layout()
896 but this is needed after the list got changed.
897 @param dc the wxDC to draw on
898 @param bottom optional y coordinate where to stop calculating
899 */
900 void Recalculate(wxDC &dc, CoordType bottom = -1);
901
902 /** Returns the size of the list in screen coordinates.
903 The return value only makes sense after the list has been
904 drawn.
905 @return a wxPoint holding the maximal x/y coordinates used for
906 drawing
907 */
908 wxPoint GetSize(void) const;
909
910 /** Returns the cursor position on the screen.
911 @return cursor position in pixels
912 */
913 wxPoint GetCursorScreenPos(wxDC &dc);
914
915 /** Draws the cursor.
916 @param active If true, draw a bold cursor to mark window as
917 active.
918 @param translate optional translation of cursor coords on screen
919 */
920 void DrawCursor(wxDC &dc,
921 bool active = true,
922 const wxPoint & translate = wxPoint(0,0));
923
924 /** This function finds an object belonging to a given screen
925 position. It assumes that Layout() has been called before.
926 @param pos screen position
927 @param cursorPos if non NULL, store cursor position in there
928 @param found if used, set this to true if we really found an
929 object, to false if we had to take the object near to it
930 @return pointer to the object
931 */
932 wxLayoutObject * FindObjectScreen(wxDC &dc,
933 wxPoint const pos,
934 wxPoint *cursorPos = NULL,
935 bool *found = NULL);
936
937 /** Called by the objects to update the update rectangle.
938 @param x horizontal coordinate to include in rectangle
939 @param y vertical coordinate to include in rectangle
940 */
941 void SetUpdateRect(CoordType x, CoordType y);
942 /** Called by the objects to update the update rectangle.
943 @param p a point to include in it
944 */
945 inline void SetUpdateRect(const wxPoint &p)
946 { SetUpdateRect(p.x,p.y); }
947 /// Invalidates the update rectangle.
948 void InvalidateUpdateRect(void) { m_UpdateRectValid = false; }
949 /// Returns the update rectangle.
950 const wxRect *GetUpdateRect(void) const { return &m_UpdateRect; }
951 //@}
952
953 /**@name For exporting one object after another. */
954 //@{
955 /** Returns a pointer to the first line in the list. */
956 wxLayoutLine *GetFirstLine(void)
957 {
958 wxASSERT(m_FirstLine);
959 return m_FirstLine;
960 }
961 //@}
962
963 /// Begin selecting text.
964 void StartSelection(wxPoint cpos = wxPoint(-1,-1));
965 // Continue selecting text
966 void ContinueSelection(wxPoint cpos = wxPoint(-1,-1));
967 /// End selecting text.
968 void EndSelection(wxPoint cpos = wxPoint(-1,-1));
969 /// Are we still selecting text?
970 bool IsSelecting(void);
971 bool IsSelected(const wxPoint &cursor);
972
973 /** Return the selection as a wxLayoutList.
974 @param invalidate if true, the selection will be invalidated after this and can no longer be used.
975 @return Another layout list object holding the selection, must be freed by caller
976 */
977 wxLayoutList *GetSelection(class wxLayoutDataObject *wxldo = NULL, bool invalidate = TRUE);
978 /// Delete selected bit
979 void DeleteSelection(void);
980
981 wxLayoutList *Copy(const wxPoint &from = wxPoint(0,0),
982 const wxPoint &to = wxPoint(-1,-1));
983
984 /// starts highlighting of text for selections
985 void StartHighlighting(wxDC &dc);
986 /// ends highlighting of text for selections
987 void EndHighlighting(wxDC &dc);
988
989 /** Tests whether this layout line is selected and needs
990 highlighting.
991 @param line to test for
992 @param from set to first cursorpos to be highlighted (for returncode == -1)
993 @param to set to last cursorpos to be highlighted (for returncode == -1)
994 @return 0 = not selected, 1 = fully selected, -1 = partially
995 selected
996
997 */
998 int IsSelected(const wxLayoutLine *line, CoordType *from, CoordType *to);
999
1000 void ApplyStyle(wxLayoutStyleInfo *si, wxDC &dc);
1001 #ifdef WXLAYOUT_DEBUG
1002 void Debug(void);
1003 #endif
1004 private:
1005 /// Clear the list.
1006 void InternalClear(void);
1007 /** Calculates the cursor position on the screen.
1008 */
1009 void UpdateCursorScreenPos(wxDC &dc);
1010
1011 /// The list of lines.
1012 wxLayoutLine *m_FirstLine;
1013 /// The update rectangle which needs to be refreshed:
1014 wxRect m_UpdateRect;
1015 /// Is the update rectangle valid?
1016 bool m_UpdateRectValid;
1017 /**@name Cursor Management */
1018 //@{
1019 /// Where the text cursor (column,line) is.
1020 wxPoint m_CursorPos;
1021 /// The size of the cursor.
1022 wxPoint m_CursorSize;
1023 /// Where the cursor should be drawn.
1024 wxPoint m_CursorScreenPos;
1025 /// The line where the cursor is.
1026 wxLayoutLine *m_CursorLine;
1027 //@}
1028
1029 /// A structure for the selection.
1030 struct Selection
1031 {
1032 Selection() { m_valid = false; m_selecting = false; }
1033 bool m_valid;
1034 bool m_selecting;
1035 wxPoint m_CursorA, m_CursorB;
1036 } m_Selection;
1037 /** @name Font parameters. */
1038 //@{
1039 /// this object manages the fonts for us
1040 wxFontCache m_FontCache;
1041 /// the default setting:
1042 wxLayoutStyleInfo m_DefaultSetting;
1043 /// the current setting:
1044 wxLayoutStyleInfo m_CurrentSetting;
1045 //@}
1046 };
1047
1048
1049 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1050
1051 The wxLayoutDataObject for exporting data to the clipboard in our
1052 own format.
1053
1054 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1055 class wxLayoutDataObject : public wxPrivateDataObject
1056 {
1057 public:
1058 wxLayoutDataObject(void)
1059 {
1060 SetId("application/wxlayoutlist");
1061 //m_format.SetAtom((GdkAtom) 222222);
1062 }
1063 };
1064
1065 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1066
1067 The wxLayoutPrintout object for printing within the wxWindows print
1068 framework.
1069
1070 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1071 /** This class implements a wxPrintout for printing a wxLayoutList within
1072 the wxWindows printing framework.
1073 */
1074 class wxLayoutPrintout: public wxPrintout
1075 {
1076 public:
1077 /** Constructor.
1078 @param llist pointer to the wxLayoutList to be printed
1079 @param title title for PS file or windows
1080 */
1081 wxLayoutPrintout(wxLayoutList *llist,
1082 wxString const & title =
1083 "wxLayout Printout");
1084 /// Destructor.
1085 ~wxLayoutPrintout();
1086
1087 /** Function which prints the n-th page.
1088 @param page the page number to print
1089 @return bool true if we are not at end of document yet
1090 */
1091 bool OnPrintPage(int page);
1092 /** Checks whether page exists in document.
1093 @param page number of page
1094 @return true if page exists
1095 */
1096 bool HasPage(int page);
1097
1098 /** Gets called from wxWindows to find out which pages are existing.
1099 I'm not totally sure about the parameters though.
1100 @param minPage the first page in the document
1101 @param maxPage the last page in the document
1102 @param selPageFrom the first page to be printed
1103 @param selPageTo the last page to be printed
1104 */
1105 void GetPageInfo(int *minPage, int *maxPage,
1106 int *selPageFrom, int *selPageTo);
1107 protected:
1108 /** This little function scales the DC so that the printout has
1109 roughly the same size as the output on screen.
1110 @param dc the wxDC to scale
1111 @return the scale that was applied
1112 */
1113 float ScaleDC(wxDC *dc);
1114
1115 /* no longer used
1116 virtual void DrawHeader(wxDC &dc, wxPoint topleft, wxPoint bottomright, int pageno);
1117 */
1118 private:
1119 /// The list to print.
1120 wxLayoutList *m_llist;
1121 /// Title for PS file or window.
1122 wxString m_title;
1123 /// The real paper size.
1124 int m_PageHeight, m_PageWidth;
1125 /// How much we actually print per page.
1126 int m_PrintoutHeight;
1127 /// How many pages we need to print.
1128 int m_NumOfPages;
1129 /// Top left corner where we start printing.
1130 wxPoint m_Offset;
1131 };
1132
1133
1134 #endif // WXLLIST_H