1 /*-*- c++ -*-********************************************************
2 * wxLayoutList.h - a formatted text rendering engine for wxWindows *
4 * (C) 1999-2000 by Karsten Ballüder (ballueder@gmx.net) *
7 *******************************************************************/
14 # pragma interface "wxllist.h"
21 #include <wx/printdlg.h>
22 #include <wx/generic/printps.h>
23 #include <wx/generic/prntdlgg.h>
24 #include <wx/dataobj.h>
26 // skip the following defines if embedded in M application
28 # define WXMENU_LAYOUT_LCLICK 1111
29 # define WXMENU_LAYOUT_RCLICK 1112
30 # define WXMENU_LAYOUT_DBLCLICK 1113
31 #else // for Mahogany only
35 // use the wxWindows caret class instead of home grown cursor whenever possible
37 # undef WXLAYOUT_USE_CARET
38 # define WXLAYOUT_USE_CARET 1
41 // do not enable debug mode within Mahogany unless in debug mode
42 #if defined(__WXDEBUG__) && (( ! defined(M_BASEDIR) )|| defined(DEBUG))
43 # define WXLAYOUT_DEBUG
47 # define WXLO_TRACE(x) wxLogDebug(x)
48 // activate profiling: # define WXLO_PROFILE
50 # define WXLO_TRACE(x)
53 /* Some profiling code: */
54 #if defined (WXLO_PROFILE)
58 # define WXLO_TIMER_DEFINE(x) static struct timeval x
59 # define WXLO_TIMER_START(x) gettimeofday(&x,NULL)
60 # define WXLO_TIMER_STOP(x) { struct timeval y; \
61 gettimeofday(&y,NULL); \
62 x.tv_sec -= y.tv_sec; x.tv_usec -= y.tv_usec; }
63 # define WXLO_TIMER_PRINT(x) wxLogDebug("Timer " #x " elapsed: %ld", \
64 (long)(x.tv_sec * -1000 - x.tv_usec));
66 # define WXLO_TIMER_DEFINE(x)
67 # define WXLO_TIMER_START(x)
68 # define WXLO_TIMER_STOP(x)
69 # define WXLO_TIMER_PRINT(x)
73 #define WXLO_DEBUG_URECT 0
75 #ifndef WXLO_DEFAULTFONTSIZE
76 # define WXLO_DEFAULTFONTSIZE 12
80 # define WXLO_BITMAP_FORMAT wxBITMAP_TYPE_BMP
82 # define WXLO_BITMAP_FORMAT wxBITMAP_TYPE_PNG
85 /// Types of currently supported layout objects.
86 enum wxLayoutObjectType
88 /// illegal object type, should never appear
89 WXLO_TYPE_INVALID
= 0,
90 /// text object, containing normal text
92 /// command object, containing font or colour changes
94 /// icon object, any kind of image
96 /// a linebreak, does not exist as an object
100 /// Type used for coordinates in drawing. Must be signed.
101 typedef long CoordType
;
103 // Forward declarations.
106 class wxLayoutObject
;
108 class WXDLLEXPORT wxCaret
;
109 class WXDLLEXPORT wxColour
;
110 class WXDLLEXPORT wxDC
;
111 class WXDLLEXPORT wxFont
;
113 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
115 The wxLayout objects which make up the lines.
117 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
119 /** The base class defining the interface to each object which can be
120 part of the layout. Each object needs to draw itself and calculate
129 /** This structure can be used to contain data associated with the
131 It is refcounted, so the caller has to do a DecRef() on it
136 UserData() { m_refcount
= 1; }
137 inline void IncRef(void) { m_refcount
++; }
138 inline void DecRef(void) { m_refcount
--; if(m_refcount
== 0) delete this;}
139 inline void SetLabel(const wxString
&l
) { m_label
= l
; }
140 inline const wxString
& GetLabel(void) const { return m_label
; }
145 virtual ~UserData() { wxASSERT(m_refcount
== 0); }
146 /// prevents gcc from generating stupid warnings
147 friend class dummy_UserData
;
150 /// return the type of this object
151 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_INVALID
; }
152 /** Calculates the size of an object.
153 @param dc the wxDC to draw on
154 @param llist the wxLayoutList
156 virtual void Layout(wxDC
&dc
, wxLayoutList
*llist
) = 0;
159 @param dc the wxDC to draw on
160 @param coords where to draw the baseline of the object.
161 @param wxllist pointer to wxLayoutList
162 @param begin if !=-1, from which position on to highlight it
163 @param end if begin !=-1, how many positions to highlight it
165 virtual void Draw(wxDC
& /* dc */,
166 wxPoint
const & /* coords */,
167 wxLayoutList
*wxllist
,
168 CoordType begin
= -1,
169 CoordType end
= -1) { }
171 /** Calculates and returns the size of the object.
172 @param top where to store height above baseline
173 @param bottom where to store height below baseline
174 @return the size of the object's box in pixels
176 virtual wxPoint
GetSize(CoordType
* top
, CoordType
*bottom
) const
177 { *top
= 0; *bottom
= 0; return wxPoint(0,0); }
179 /// Return just the width of the object on the screen.
180 virtual CoordType
GetWidth(void) const { return 0; }
181 /// returns the number of cursor positions occupied by this object
182 virtual CoordType
GetLength(void) const { return 1; }
183 /** Returns the cursor offset relating to the screen x position
184 relative to begin of object.
185 @param dc the wxDC to use for calculations
186 @param xpos relative x position from head of object
187 @return cursor coordinate offset
189 virtual CoordType
GetOffsetScreen(wxDC
&dc
, CoordType xpos
) const { return 0; }
192 wxLayoutObject() { m_UserData
= NULL
; }
193 /// delete the user data
194 virtual ~wxLayoutObject() { if(m_UserData
) m_UserData
->DecRef(); }
196 #ifdef WXLAYOUT_DEBUG
197 virtual wxString
DebugDump(void) const;
200 /** Tells the object about some user data. This data is associated
201 with the object and will be deleted at destruction time.
202 It is reference counted.
204 void SetUserData(UserData
*data
)
207 m_UserData
->DecRef();
210 m_UserData
->IncRef();
213 /** Return the user data.
214 Increments the object's reference count. When no longer needed,
215 caller must call DecRef() on the pointer returned.
217 UserData
* GetUserData(void) const { if(m_UserData
) m_UserData
->IncRef(); return m_UserData
; }
219 /** Makes a copy of this object.
221 virtual wxLayoutObject
*Copy(void) = 0;
223 /** Clipboard support function. Read and write objects to
226 /// Writes the object to the string.
227 virtual void Write(wxString
&ostr
) = 0;
229 @param str stream to read from, will bee changed
230 @return true on success
232 static wxLayoutObject
*Read(wxString
&istr
);
235 /// returns TRUE if the object is shown on the screen (i.e. not cmd object)
236 bool IsVisibleObject() const { return GetType() != WXLO_TYPE_CMD
; }
239 /// optional data for application's use
240 UserData
*m_UserData
;
241 #if defined (M_BASEDIR) && defined (DEBUG)
242 MOBJECT_NAME(wxLayoutObject
)
246 /// Define a list type of wxLayoutObject pointers.
247 KBLIST_DEFINE(wxLayoutObjectList
, wxLayoutObject
);
249 /// An illegal iterator to save typing.
250 #define NULLIT (wxLayoutObjectList::iterator(NULL))
251 /// The iterator type.
252 typedef wxLayoutObjectList::iterator wxLOiterator
;
254 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
258 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
259 /** This class implements a wxLayoutObject holding plain text.
261 class wxLayoutObjectText
: public wxLayoutObject
264 wxLayoutObjectText(const wxString
&txt
= "");
266 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_TEXT
; }
267 virtual void Layout(wxDC
&dc
, wxLayoutList
*llist
);
268 virtual void Draw(wxDC
&dc
, wxPoint
const &coords
,
269 wxLayoutList
*wxllist
,
270 CoordType begin
= -1,
272 /** Calculates and returns the size of the object.
273 @param top where to store height above baseline
274 @param bottom where to store height below baseline
275 @return the size of the object's box in pixels
277 virtual wxPoint
GetSize(CoordType
* top
, CoordType
*bottom
) const;
278 /// Return just the width of the object on the screen.
279 virtual CoordType
GetWidth(void) const { return m_Width
; }
280 /** Returns the cursor offset relating to the screen x position
281 relative to begin of object.
282 @param dc the wxDC to use for calculations
283 @param xpos relative x position from head of object
284 @return cursor coordinate offset
286 virtual CoordType
GetOffsetScreen(wxDC
&dc
, CoordType xpos
) const;
288 virtual void Write(wxString
&ostr
);
289 static wxLayoutObjectText
*Read(wxString
&istr
);
291 #ifdef WXLAYOUT_DEBUG
292 virtual wxString
DebugDump(void) const;
295 virtual CoordType
GetLength(void) const { return strlen(m_Text
.c_str()); }
298 wxString
& GetText(void) { return m_Text
; }
299 void SetText(wxString
const &text
) { m_Text
= text
; }
300 /** Makes a copy of this object.
302 virtual wxLayoutObject
*Copy(void);
305 /// size of the box containing text
306 long m_Width
, m_Height
;
307 /// Height above baseline.
309 /// Height below baseline.
313 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
317 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
318 /** This class implements a wxLayoutObject holding a graphic.
320 class wxLayoutObjectIcon
: public wxLayoutObject
323 wxLayoutObjectIcon(wxBitmap
*icon
= NULL
);
324 wxLayoutObjectIcon(wxBitmap
const &icon
);
326 ~wxLayoutObjectIcon() { if(m_Icon
) delete m_Icon
; }
328 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_ICON
; }
329 virtual void Layout(wxDC
&dc
, wxLayoutList
*llist
);
330 virtual void Draw(wxDC
&dc
, wxPoint
const &coords
,
331 wxLayoutList
*wxllist
,
332 CoordType begin
= -1,
335 /** Calculates and returns the size of the object.
336 @param top where to store height above baseline
337 @param bottom where to store height below baseline
338 @return the size of the object's box in pixels
340 virtual wxPoint
GetSize(CoordType
* top
, CoordType
*bottom
) const;
341 /// Return just the width of the object on the screen.
342 virtual CoordType
GetWidth(void) const { return m_Icon
->GetWidth(); }
343 // return a pointer to the icon
344 wxBitmap
*GetIcon(void) const { return m_Icon
; }
345 /** Makes a copy of this object.
347 virtual wxLayoutObject
*Copy(void);
348 virtual void Write(wxString
&ostr
);
349 static wxLayoutObjectIcon
*Read(wxString
&istr
);
354 /** This structure holds all formatting information.
356 struct wxLayoutStyleInfo
358 wxLayoutStyleInfo(int ifamily
= -1,
364 wxColour
*bg
= NULL
);
365 wxLayoutStyleInfo
& operator=(const wxLayoutStyleInfo
&right
);
367 wxColour
& GetBGColour() { return m_bg
; }
369 /// Font change parameters.
370 int size
, family
, style
, weight
, underline
;
373 int m_fg_valid
, m_bg_valid
; // bool, but must be int!
377 class wxFontCacheEntry
380 wxFontCacheEntry(int family
, int size
, int style
, int weight
,
383 m_Family
= family
; m_Size
= size
; m_Style
= style
;
384 m_Weight
= weight
; m_Underline
= underline
;
385 m_Font
= new wxFont(m_Size
, m_Family
,
386 m_Style
, m_Weight
, m_Underline
);
388 bool Matches(int family
, int size
, int style
, int weight
,
389 bool underline
) const
391 return size
== m_Size
&& family
== m_Family
392 && style
== m_Style
&& weight
== m_Weight
393 && underline
== m_Underline
;
395 wxFont
& GetFont(void) { return *m_Font
; }
403 // VZ: I wonder why it doesn't use wxLayoutStyleInfo instead of those?
404 int m_Family
, m_Size
, m_Style
, m_Weight
;
408 KBLIST_DEFINE(wxFCEList
, wxFontCacheEntry
);
413 wxFont
& GetFont(int family
, int size
, int style
, int weight
,
415 wxFont
& GetFont(wxLayoutStyleInfo
const &si
)
417 return GetFont(si
.family
, si
.size
, si
.style
, si
.weight
,
421 wxFCEList m_FontList
;
424 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
428 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
429 /** This class implements a wxLayoutObject holding style change commands.
431 class wxLayoutObjectCmd
: public wxLayoutObject
434 virtual wxLayoutObjectType
GetType(void) const { return WXLO_TYPE_CMD
; }
435 virtual void Layout(wxDC
&dc
, wxLayoutList
*llist
);
436 virtual void Draw(wxDC
&dc
, wxPoint
const &coords
,
437 wxLayoutList
*wxllist
,
438 CoordType begin
= -1,
440 wxLayoutObjectCmd(int family
= -1,
446 wxColour
*bg
= NULL
);
447 wxLayoutObjectCmd(const wxLayoutStyleInfo
&si
);
448 ~wxLayoutObjectCmd();
449 /** Stores the current style in the styleinfo structure */
450 wxLayoutStyleInfo
* GetStyle(void) const;
451 /** Makes a copy of this object.
453 virtual wxLayoutObject
*Copy(void);
454 virtual void Write(wxString
&ostr
);
455 static wxLayoutObjectCmd
*Read(wxString
&istr
);
457 wxLayoutStyleInfo
*m_StyleInfo
;
460 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
462 The wxLayoutLine object
464 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
466 /** This class represents a single line of objects to be displayed.
467 It knows its height and total size and whether it needs to be
469 It has pointers to its first and next line so it can automatically
470 update them as needed.
476 @param prev pointer to previous line or NULL
477 @param next pointer to following line or NULL
478 @param llist pointer to layout list
480 wxLayoutLine(wxLayoutLine
*prev
, wxLayoutList
*llist
);
481 /** This function inserts a new object at cursor position xpos.
482 @param xpos where to insert new object
483 @param obj the object to insert
484 @return true if that xpos existed and the object was inserted
486 bool Insert(CoordType xpos
, wxLayoutObject
*obj
);
488 /** This function inserts text at cursor position xpos.
489 @param xpos where to insert
490 @param text the text to insert
491 @return true if that xpos existed and the object was inserted
493 bool Insert(CoordType xpos
, const wxString
& text
);
495 /** This function appends an object to the line.
496 @param obj the object to insert
498 void Append(wxLayoutObject
* obj
)
501 m_ObjectList
.push_back(obj
);
502 m_Length
+= obj
->GetLength();
505 /** This function prepends an object to the line. */
506 void Prepend(wxLayoutObject
* obj
)
509 m_ObjectList
.push_front(obj
);
510 m_Length
+= obj
->GetLength();
513 /** This function appens the next line to this, i.e. joins the two
516 void MergeNextLine(wxLayoutList
*llist
);
518 /** This function deletes npos cursor positions from position xpos.
519 @param xpos where to delete
520 @param npos how many positions
521 @return number of positions still to be deleted
523 CoordType
Delete(CoordType xpos
, CoordType npos
);
525 /** This function breaks the line at a given cursor position.
526 @param xpos where to break it
527 @return pointer to the new line object replacing the old one
529 wxLayoutLine
*Break(CoordType xpos
, wxLayoutList
*llist
);
531 /** This function wraps the line: breaks it at a suitable point
532 and merges it with the next.
534 @return TRUE if broken
536 bool Wrap(CoordType wrapmargin
, wxLayoutList
*llist
);
538 /** Deletes the next word from this position, including leading
540 This function does not delete over font changes, i.e. a word
541 with formatting instructions in the middle of it is treated as
542 two (three actually!) words. In fact, if the cursor is on a non-text object, that
543 one is treated as a word.
544 @param xpos from where to delete
545 @return true if a word was deleted
547 bool DeleteWord(CoordType npos
);
549 /** Finds a suitable position left to the given column to break the
551 @param column we want to break the line to the left of this
552 @return column for breaking line or -1 if no suitable location found
554 CoordType
GetWrapPosition(CoordType column
);
556 /** Finds the object which covers the cursor position xpos in this
558 @param xpos the column number
559 @param offset where to store the difference between xpos and
561 @return iterator to the object or NULLIT
563 wxLayoutObjectList::iterator
FindObject(CoordType xpos
, CoordType
566 /** Finds the object which covers the screen position xpos in this
568 @param dc the wxDC to use for calculations
569 @param llist the layout list to which this line belongs
570 @param xpos the screen x coordinate
571 @param offset where to store the difference between xpos and
573 @return iterator to the object or NULLIT
575 wxLayoutObjectList::iterator
FindObjectScreen(wxDC
&dc
,
579 bool *found
= NULL
) const ;
581 /** Finds text in this line.
582 @param needle the text to find
583 @param xpos the position where to start the search
584 @return the cursoor coord where it was found or -1
586 CoordType
FindText(const wxString
&needle
, CoordType xpos
= 0) const;
588 /** Get the first object in the list. This is used by the wxlparser
589 functions to export the list.
590 @return iterator to the first object
592 wxLayoutObjectList::iterator
GetFirstObject(void) const
594 return m_ObjectList
.begin();
597 /** Get the last object in the list.
599 wxLayoutObjectList::iterator
GetLastObject(void) const
601 return m_ObjectList
.tail();
604 /** Deletes this line, returns pointer to next line.
605 @param update If true, update all following lines.
607 wxLayoutLine
*DeleteLine(bool update
, wxLayoutList
*llist
);
609 /**@name Cursor Management */
611 /** Return the line number of this line.
612 @return the line number
614 inline CoordType
GetLineNumber(void) const { return m_LineNumber
; }
615 /** Return the length of the line.
616 @return line lenght in cursor positions
618 inline CoordType
GetLength(void) const { return m_Length
; }
621 /**@name Drawing and Layout */
623 /** Draws the line on a wxDC.
624 @param dc the wxDC to draw on
625 @param llist the wxLayoutList
626 @param offset an optional offset to shift printout
630 const wxPoint
&offset
= wxPoint(0,0)) const;
632 /** Recalculates the positions of objects and the height of the
634 @param dc the wxDC to draw on
635 @param llist th e wxLayoutList
636 @param cursorPos if not NULL, set cursor screen position in there
637 @param cursorSize if not cursorPos != NULL, set cursor size in there
638 @param cursorStyle if non NULL where to store styleinfo for cursor pos
639 @param cx if cursorPos != NULL, the cursor x position
640 @param suppressStyleUpdate FALSe normally, only to suppress updating of m_StyleInfo
642 void Layout(wxDC
&dc
,
644 wxPoint
*cursorPos
= NULL
,
645 wxPoint
*cursorSize
= NULL
,
646 wxLayoutStyleInfo
*cursorStyle
= NULL
,
648 bool suppressStyleUpdate
= FALSE
);
649 /** This function finds an object belonging to a given cursor
650 position. It assumes that Layout() has been called before.
651 @param dc the wxDC to use for calculations
652 @param xpos screen x position
653 @param found if non-NULL set to false if we return the last
654 object before the cursor, to true if we really have an object
656 @return pointer to the object
658 wxLayoutObject
* FindObjectScreen(wxDC
&dc
,
661 /** This sets the style info for the beginning of this line.
662 @param si styleinfo structure
664 void ApplyStyle(const wxLayoutStyleInfo
&si
)
665 { m_StyleInfo
= si
; }
669 /**@name List traversal */
671 /// Returns pointer to next line.
672 wxLayoutLine
*GetNextLine(void) const { return m_Next
; }
673 /// Returns pointer to previous line.
674 wxLayoutLine
*GetPreviousLine(void) const { return m_Previous
; }
675 /// Sets the link to the next line.
676 void SetNext(wxLayoutLine
*next
)
677 { m_Next
= next
; if(next
) next
->m_Previous
= this; }
678 /// Sets the link to the previous line.
679 void SetPrevious(wxLayoutLine
*previous
)
680 { m_Previous
= previous
; if(previous
) previous
->m_Next
= this; }
683 /// Returns the position of this line on the canvas.
684 wxPoint
GetPosition(void) const { return m_Position
; }
685 /// Returns the height of this line.
686 CoordType
GetHeight(void) const { return m_Height
; }
687 /// Returns the width of this line.
688 CoordType
GetWidth(void) const { return m_Width
; }
689 /// Recalculates the position of this line on the canvas.
690 wxPoint
RecalculatePosition(wxLayoutList
*llist
);
692 /** Copies the contents of this line to another wxLayoutList
693 @param llist the wxLayoutList destination
694 @param from x cursor coordinate where to start
695 @param to x cursor coordinate where to stop, -1 for end of line
697 void Copy(wxLayoutList
*llist
,
701 #ifdef WXLAYOUT_DEBUG
702 void Debug(void) const;
704 wxLayoutStyleInfo
const & GetStyleInfo() const { return m_StyleInfo
; }
706 /// Returns dirty state
707 bool IsDirty(void) const { return m_Dirty
; }
708 /** Marks this line as diry.
709 @param left xpos from where it is dirty or -1 for all
711 void MarkDirty(CoordType left
= -1)
715 if ( m_updateLeft
== -1 || left
< m_updateLeft
)
720 if(m_Next
) m_Next
->MarkDirty();
722 /// Reset the dirty flag
723 void MarkClean() { m_Dirty
= false; m_updateLeft
= -1; }
726 /// Destructor is private. Use DeleteLine() to remove it.
729 /**@name Functions to let the lines synchronise with each other. */
731 /** Sets the height of this line. Will mark following lines as
733 @param height new height
735 void SetHeight(CoordType height
, wxLayoutList
*llist
)
736 { m_Height
= height
; MarkDirty(); }
738 /** Updates the line numbers. */
743 CoordType m_LineNumber
;
744 /// The line length in cursor positions.
746 /// The total height of the line.
748 /// The total width of the line on screen.
750 /// The baseline for drawing objects
751 CoordType m_BaseLine
;
752 /// The position on the canvas.
754 /// The list of objects
755 wxLayoutObjectList m_ObjectList
;
756 /// Have we been changed since the last layout?
758 /// The coordinate of the left boundary of the update rectangle (if m_Dirty)
759 CoordType m_updateLeft
;
760 /// Pointer to previous line if it exists.
761 wxLayoutLine
*m_Previous
;
762 /// Pointer to next line if it exists.
763 wxLayoutLine
*m_Next
;
764 /// A StyleInfo structure, holding the current settings.
765 wxLayoutStyleInfo m_StyleInfo
;
766 /// Just to suppress gcc compiler warnings.
769 wxLayoutLine(const wxLayoutLine
&);
773 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
775 The wxLayoutList object
777 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
778 /** The wxLayoutList is a list of wxLayoutLine objects. It provides a
779 higher level of abstraction for the text and can generally be considered
780 as representing "the text".
790 #ifdef WXLAYOUT_USE_CARET
791 /// give us the pointer to the caret to use
792 void SetCaret(wxCaret
*caret
) { m_caret
= caret
; }
793 #endif // WXLAYOUT_USE_CARET
796 void Clear(int family
= wxROMAN
,
797 int size
=WXLO_DEFAULTFONTSIZE
,
803 /// Empty: clear the list but leave font settings.
806 /** Enable or disable auto-formatting. Normally, while editing this
807 should be enabled which is the default. While
808 inserting/deleting lots of text, it makes sense to temporarily
810 @param enable TRUE to enable, FALSE to disable
812 void SetAutoFormatting(bool enable
= TRUE
)
813 { m_AutoFormat
= enable
; }
814 /**@name Cursor Management */
816 /** Set new cursor position.
817 @param p new position
818 @return bool if it could be set
820 bool MoveCursorTo(wxPoint
const &p
);
821 /** Move cursor up or down.
823 @return bool if it could be moved
825 bool MoveCursorVertically(int n
);
826 /** Move cursor left or right.
827 @param n = number of positions to move
828 @return bool if it could be moved
830 bool MoveCursorHorizontally(int n
);
831 /** Move cursor to the left or right counting in words
832 @param n = number of positions in words
833 @param untilNext: puts the cursor at the start of the next word if true,
834 leaves it at the end of the current one otherwise
835 @return bool if it could be moved
837 bool MoveCursorWord(int n
, bool untilNext
= true);
839 /// Move cursor to end of line.
840 void MoveCursorToEndOfLine(void)
842 wxASSERT(m_CursorLine
);
843 MoveCursorHorizontally(m_CursorLine
->GetLength()-m_CursorPos
.x
);
846 /// Move cursor to the start of line.
847 void MoveCursorToBeginOfLine(void)
848 { MoveCursorHorizontally(-m_CursorPos
.x
); }
850 /// get the number of lines in the list
851 size_t GetNumLines() const { return m_numLines
; }
853 /// Returns current cursor position.
854 const wxPoint
&GetCursorPos(wxDC
&dc
) const { return m_CursorPos
; }
855 const wxPoint
&GetCursorPos() const { return m_CursorPos
; }
856 wxLayoutLine
* GetCursorLine(void) { return m_CursorLine
; }
858 /// move cursor to the end of text
859 void MoveCursorToEnd(void)
861 MoveCursorTo(wxPoint(0, GetNumLines() - 1));
862 MoveCursorToEndOfLine();
867 /**@name Editing functions.
868 All of these functions return true on success and false on
871 /// Insert text at current cursor position.
872 bool Insert(wxString
const &text
);
873 /// Insert some other object at current cursor position.
874 bool Insert(wxLayoutObject
*obj
);
875 /// Inserts objects at current cursor positions
876 bool Insert(wxLayoutList
*llist
);
878 /// Inserts a linebreak at current cursor position.
879 bool LineBreak(void);
880 /** Wraps the current line. Searches to the left of the cursor to
881 break the line. Does nothing if the cursor position is before
882 the break position parameter.
883 @param column the break position for the line, maximum length
884 @return true if line got broken
886 bool WrapLine(CoordType column
);
888 /** Wraps the complete buffer.
889 @param column the break position for the line, maximum length
890 @return true if line got broken
892 bool WrapAll(CoordType column
);
893 /** This function deletes npos cursor positions.
894 @param npos how many positions
895 @return true if everything got deleted
897 bool Delete(CoordType npos
);
899 /** Delete the next n lines.
900 @param n how many lines to delete
901 @return how many it could not delete
903 int DeleteLines(int n
);
905 /// Delete to end of line.
906 void DeleteToEndOfLine(void)
908 wxASSERT(m_CursorLine
);
909 Delete(m_CursorLine
->GetLength()-m_CursorPos
.x
);
911 /// Delete to begin of line.
912 void DeleteToBeginOfLine(void)
914 wxASSERT(m_CursorLine
);
915 CoordType n
= m_CursorPos
.x
;
916 #ifdef WXLAYOUT_DEBUG
917 wxASSERT(MoveCursorHorizontally(-n
));
919 MoveCursorHorizontally(-n
);
924 /** Delete the next word.
926 void DeleteWord(void)
928 wxASSERT(m_CursorLine
);
929 m_CursorLine
->DeleteWord(m_CursorPos
.x
);
934 /** Finds text in this list.
935 @param needle the text to find
936 @param cpos the position where to start the search
937 @return the cursor coord where it was found or (-1,-1)
939 wxPoint
FindText(const wxString
&needle
, const wxPoint
&cpos
= wxPoint(0,0)) const;
941 /**@name Formatting options */
943 /// sets font parameters
944 void SetFont(int family
, int size
, int style
,
945 int weight
, int underline
,
948 /// sets font parameters, colours by name
949 void SetFont(int family
=-1, int size
= -1, int style
=-1,
950 int weight
=-1, int underline
= -1,
951 char const *fg
= NULL
,
952 char const *bg
= NULL
);
953 /// changes to the next larger font size
954 inline void SetFontLarger(void)
955 { SetFont(-1,(12*m_CurrentStyleInfo
.size
)/10); }
956 /// changes to the next smaller font size
957 inline void SetFontSmaller(void)
958 { SetFont(-1,(10*m_CurrentStyleInfo
.size
)/12); }
961 inline void SetFontFamily(int family
) { SetFont(family
); }
963 inline void SetFontSize(int size
) { SetFont(-1,size
); }
965 inline void SetFontStyle(int style
) { SetFont(-1,-1,style
); }
967 inline void SetFontWeight(int weight
) { SetFont(-1,-1,-1,weight
); }
968 /// toggle underline flag
969 inline void SetFontUnderline(bool ul
) { SetFont(-1,-1,-1,-1,(int)ul
); }
970 /// set font colours by name
971 inline void SetFontColour(char const *fg
, char const *bg
= NULL
)
972 { SetFont(-1,-1,-1,-1,-1,fg
,bg
); }
973 /// set font colours by colour
974 inline void SetFontColour(wxColour
*fg
, wxColour
*bg
= NULL
)
975 { SetFont(-1,-1,-1,-1,-1,fg
,bg
); }
978 Returns a pointer to the default settings.
979 This is only valid temporarily and should not be stored
981 @return the default settings of the list
983 wxLayoutStyleInfo
&GetDefaultStyleInfo(void) { return m_DefaultStyleInfo
; }
984 wxLayoutStyleInfo
&GetStyleInfo(void) { return m_CurrentStyleInfo
; }
985 const wxLayoutStyleInfo
&GetStyleInfo(void) const { return m_CurrentStyleInfo
; }
986 const wxLayoutStyleInfo
&GetCursorStyleInfo(void) const { return m_CursorStyleInfo
; }
988 /// is the current font underlined?
989 bool IsFontUnderlined() const { return GetCursorStyleInfo().underline
!= 0; }
990 /// is the current font bold?
991 bool IsFontBold() const { return GetCursorStyleInfo().weight
== wxBOLD
; }
992 /// is the current font italic?
993 bool IsFontItalic() const { return GetCursorStyleInfo().style
== wxITALIC
; }
995 /// set underline if it was off, turn it off if it was on
996 void ToggleFontUnderline()
997 { SetFontUnderline(!IsFontUnderlined()); }
999 /// make font bold if it was normal or make it normal if it was bold
1000 void ToggleFontWeight()
1001 { SetFontWeight(IsFontBold() ? wxNORMAL
: wxBOLD
); }
1003 /// make font italic if it was normal or make it normal if it was italic
1004 void ToggleFontItalics()
1005 { SetFontStyle(IsFontItalic() ? wxNORMAL
: wxITALIC
); }
1011 /** Draws the complete list on a wxDC.
1012 @param dc the wxDC to draw on
1013 @param offset an optional offset to shift printout
1014 @param top optional y coordinate where to start drawing
1015 @param bottom optional y coordinate where to stop drawing
1016 @param clipStrictly if set, do not draw objects which reach
1017 beyond "bottom". Set this when printing.
1020 const wxPoint
&offset
= wxPoint(0,0),
1021 CoordType top
= -1, CoordType bottom
= -1,
1022 bool clipStrictly
= false);
1024 /** Calculates new layout for the list, like Draw() but does not
1026 @param dc the wxDC to draw on
1027 @param bottom optional y coordinate where to stop calculating
1028 @param forceAll force re-layout of all lines
1029 @param cpos Can hold a cursorposition, and will be overwritten
1030 with the corresponding DC position.
1031 @param csize Will hold the cursor size relating to cpos.
1033 void Layout(wxDC
&dc
, CoordType bottom
= -1, bool forceAll
= false,
1034 wxPoint
*cpos
= NULL
,
1035 wxPoint
*csize
= NULL
);
1037 /** Ensure that the whole list will be recalculate on the next call
1038 to Layout() or Draw().
1039 @param redrawAll TRUE or FALSE to reset it
1041 void ForceTotalLayout(bool redrawAll
= TRUE
)
1042 { m_ReLayoutAll
= redrawAll
; }
1044 /** Returns the screen coordinates relating to a given cursor
1045 position and the size of the cursor at that position.
1046 @param dc for which to calculate it
1047 @param cpos Cursor position to look for.
1048 @param csize If non-NULL, will be set to the cursor size.
1049 @return The cursor position on the DC.
1051 wxPoint
GetScreenPos(wxDC
&dc
, const wxPoint
&cpos
, wxPoint
*csize
= NULL
);
1053 /** Calculates new sizes for everything in the list, like Layout()
1054 but this is needed after the list got changed.
1055 @param dc the wxDC to draw on
1056 @param bottom optional y coordinate where to stop calculating
1058 void Recalculate(wxDC
&dc
, CoordType bottom
= -1);
1060 /** Returns the size of the list in screen coordinates.
1061 The return value only makes sense after the list has been
1063 @return a wxPoint holding the maximal x/y coordinates used for
1066 wxPoint
GetSize(void) const;
1068 /** Returns the cursor position on the screen.
1070 wxPoint
GetCursorScreenPos(void) const;
1072 /** Draws the cursor.
1073 @param active If true, draw a bold cursor to mark window as
1075 @param translate optional translation of cursor coords on screen
1077 void DrawCursor(wxDC
&dc
,
1079 const wxPoint
& translate
= wxPoint(0,0));
1081 /** This function finds an object belonging to a given screen
1082 position. It assumes that Layout() has been called before.
1083 @param pos screen position
1084 @param cursorPos if non NULL, store cursor position in there
1085 @param found if used, set this to true if we really found an
1086 object, to false if we had to take the object near to it
1087 @return pointer to the object
1089 wxLayoutObject
* FindObjectScreen(wxDC
&dc
,
1091 wxPoint
*cursorPos
= NULL
,
1092 bool *found
= NULL
);
1094 /** Called by the objects to update the update rectangle.
1095 @param x horizontal coordinate to include in rectangle
1096 @param y vertical coordinate to include in rectangle
1098 void SetUpdateRect(CoordType x
, CoordType y
);
1099 /** Called by the objects to update the update rectangle.
1100 @param p a point to include in it
1102 void SetUpdateRect(const wxPoint
&p
)
1103 { SetUpdateRect(p
.x
,p
.y
); }
1104 /// adds the cursor position to the update rectangle
1105 void AddCursorPosToUpdateRect()
1107 #ifndef WXLAYOUT_USE_CARET
1108 SetUpdateRect(m_CursorScreenPos
);
1109 SetUpdateRect(m_CursorScreenPos
+m_CursorSize
);
1110 //#else - the caret will take care of refreshing itself
1111 #endif // !WXLAYOUT_USE_CARET
1113 /// Invalidates the update rectangle.
1114 void InvalidateUpdateRect(void) { m_UpdateRectValid
= false; }
1115 /// Returns the update rectangle.
1116 const wxRect
*GetUpdateRect(void) const { return &m_UpdateRect
; }
1119 /// get the current cursor size
1120 const wxPoint
& GetCursorSize() const { return m_CursorSize
; }
1122 /**@name For exporting one object after another. */
1124 /** Returns a pointer to the first line in the list. */
1125 wxLayoutLine
*GetFirstLine(void)
1127 wxASSERT(m_FirstLine
);
1132 /// Begin selecting text
1133 void StartSelection(const wxPoint
& cpos
= wxPoint(-1,-1),
1134 const wxPoint
& spos
= wxPoint(-1,-1));
1135 // Continue selecting text
1136 void ContinueSelection(const wxPoint
& cpos
= wxPoint(-1,-1),
1137 const wxPoint
& spos
= wxPoint(-1,-1));
1138 /// End selecting text.
1139 void EndSelection(const wxPoint
& cpos
= wxPoint(-1,-1),
1140 const wxPoint
& spos
= wxPoint(-1,-1));
1141 /// Discard the current selection
1142 void DiscardSelection();
1143 /// Are we still selecting text?
1144 bool IsSelecting(void) const;
1145 /// Is the given point (text coords) selected?
1146 bool IsSelected(const wxPoint
&cursor
) const;
1147 /// Do we have a non null selection?
1148 bool HasSelection() const
1149 { return m_Selection
.m_valid
|| m_Selection
.m_selecting
; }
1151 /** Return the selection as a wxLayoutList.
1152 @param invalidate if true, the selection will be invalidated after this and can no longer be used.
1153 @return Another layout list object holding the selection, must be freed by caller
1155 wxLayoutList
*GetSelection(class wxLayoutDataObject
*wxldo
= NULL
, bool invalidate
= TRUE
);
1156 /// Delete selected bit
1157 void DeleteSelection(void);
1159 wxLayoutList
*Copy(const wxPoint
&from
= wxPoint(0,0),
1160 const wxPoint
&to
= wxPoint(-1,-1));
1162 /// starts highlighting of text for selections
1163 void StartHighlighting(wxDC
&dc
);
1164 /// ends highlighting of text for selections
1165 void EndHighlighting(wxDC
&dc
);
1167 /** Tests whether this layout line is selected and needs
1169 @param line to test for
1170 @param from set to first cursorpos to be highlighted (for returncode == -1)
1171 @param to set to last cursorpos to be highlighted (for returncode == -1)
1172 @return 0 = not selected, 1 = fully selected, -1 = partially
1176 int IsSelected(const wxLayoutLine
*line
, CoordType
*from
, CoordType
*to
);
1178 void ApplyStyle(wxLayoutStyleInfo
const &si
, wxDC
&dc
);
1179 #ifdef WXLAYOUT_DEBUG
1183 // for wxLayoutLine usage only
1184 void IncNumLines() { m_numLines
++; }
1185 void DecNumLines() { m_numLines
--; }
1187 /// get the line by number
1188 wxLayoutLine
*GetLine(CoordType index
) const;
1190 /** Reads objects from a string and inserts them. Returns NULL if
1191 string is empty or a linebreak was found.
1192 @param istr stream to read from, will bee changed
1194 void Read(wxString
&istr
);
1198 void InternalClear(void);
1200 /// The list of lines.
1201 wxLayoutLine
*m_FirstLine
;
1202 /// The number of lines in the list (store instead recalculating for speed)
1205 /// The update rectangle which needs to be refreshed:
1206 wxRect m_UpdateRect
;
1207 /// Is the update rectangle valid?
1208 bool m_UpdateRectValid
;
1210 /// Shall we auto-format?
1212 /// Shall we re-layout everything?
1214 /**@name Cursor Management */
1216 /// Where the text cursor (column,line) is.
1217 wxPoint m_CursorPos
;
1218 /// Where the cursor should be drawn.
1219 wxPoint m_CursorScreenPos
;
1220 /// The line where the cursor is.
1221 wxLayoutLine
*m_CursorLine
;
1222 /// The size of the cursor.
1223 wxPoint m_CursorSize
;
1224 /// Has the cursor moved (is m_CursorScreenPos up to date)?
1226 #ifdef WXLAYOUT_USE_CARET
1229 #endif // WXLAYOUT_USE_CARET
1232 /// selection.state and begin/end coordinates
1235 Selection() { m_valid
= m_selecting
= m_discarded
= false; }
1239 bool m_discarded
; // may be TRUE only until the next redraw
1241 // returns true if we already have the screen coordinates of the
1242 // selection start and end
1243 bool HasValidScreenCoords() const
1244 { return m_ScreenA
.x
!= -1 && m_ScreenB
.x
!= -1; }
1246 // the start and end of the selection coordinates in pixels
1247 wxPoint m_ScreenA
, m_ScreenB
;
1249 // these coordinates are in text positions, not in pixels
1250 wxPoint m_CursorA
, m_CursorB
;
1252 /** @name Font parameters. */
1254 /// this object manages the fonts for us
1255 wxFontCache m_FontCache
;
1256 /// the default setting:
1257 wxLayoutStyleInfo m_DefaultStyleInfo
;
1258 /// the current setting:
1259 wxLayoutStyleInfo m_CurrentStyleInfo
;
1260 /// the current setting:
1261 wxLayoutStyleInfo m_CursorStyleInfo
;
1265 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1267 The wxLayoutDataObject for exporting data to the clipboard in our
1270 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1271 class wxLayoutDataObject
: public wxCustomDataObject
1274 wxLayoutDataObject()
1276 SetFormat("application/wxlayoutlist");
1279 // type safe wrappers
1280 void SetLayoutData(const wxString
& text
)
1281 { SetData(text
.length() + 1, text
.c_str()); }
1282 const char *GetLayoutData() const { return (const char *)GetData(); }
1285 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1287 The wxLayoutPrintout object for printing within the wxWindows print
1290 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1291 /** This class implements a wxPrintout for printing a wxLayoutList within
1292 the wxWindows printing framework.
1294 class wxLayoutPrintout
: public wxPrintout
1298 @param llist pointer to the wxLayoutList to be printed
1299 @param title title for PS file or windows
1301 wxLayoutPrintout(wxLayoutList
*llist
,
1302 wxString
const & title
=
1303 "wxLayout Printout");
1305 ~wxLayoutPrintout();
1307 /** Function which prints the n-th page.
1308 @param page the page number to print
1309 @return bool true if we are not at end of document yet
1311 bool OnPrintPage(int page
);
1312 /** Checks whether page exists in document.
1313 @param page number of page
1314 @return true if page exists
1316 bool HasPage(int page
);
1318 /** Gets called from wxWindows to find out which pages are existing.
1319 I'm not totally sure about the parameters though.
1320 @param minPage the first page in the document
1321 @param maxPage the last page in the document
1322 @param selPageFrom the first page to be printed
1323 @param selPageTo the last page to be printed
1325 void GetPageInfo(int *minPage
, int *maxPage
,
1326 int *selPageFrom
, int *selPageTo
);
1328 /** This little function scales the DC so that the printout has
1329 roughly the same size as the output on screen.
1330 @param dc the wxDC to scale
1331 @return the scale that was applied
1333 float ScaleDC(wxDC
*dc
);
1336 virtual void DrawHeader(wxDC &dc, wxPoint topleft, wxPoint bottomright, int pageno);
1339 /// The list to print.
1340 wxLayoutList
*m_llist
;
1341 /// Title for PS file or window.
1343 /// The real paper size.
1344 int m_PageHeight
, m_PageWidth
;
1345 /// How much we actually print per page.
1346 int m_PrintoutHeight
;
1347 /// How many pages we need to print.
1349 /// Top left corner where we start printing.