1 /*-*- c++ -*-********************************************************
2 * wxLayoutList.h - a formatted text rendering engine for wxWidgets *
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 wxWidgets 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,
91 /// text object, containing normal text
94 /// command object, containing font or colour changes
97 /// icon object, any kind of image
100 /// a linebreak, does not exist as an object
104 /// Type used for coordinates in drawing. Must be signed.
105 typedef long CoordType
;
107 // Forward declarations.
110 class wxLayoutObject
;
112 class WXDLLEXPORT wxCaret
;
113 class WXDLLEXPORT wxColour
;
114 class WXDLLEXPORT wxDC
;
115 class WXDLLEXPORT wxFont
;
117 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
119 The wxLayout objects which make up the lines.
121 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
123 /** The base class defining the interface to each object which can be
124 part of the layout. Each object needs to draw itself and calculate
133 /** This structure can be used to contain data associated with the
135 It is refcounted, so the caller has to do a DecRef() on it
140 UserData() { m_refcount
= 1; }
141 inline void IncRef() { m_refcount
++; }
142 inline void DecRef() { m_refcount
--; if(m_refcount
== 0) delete this;}
143 inline void SetLabel(const wxString
&l
) { m_label
= l
; }
144 inline const wxString
& GetLabel() const { return m_label
; }
149 virtual ~UserData() { wxASSERT(m_refcount
== 0); }
150 /// prevents gcc from generating stupid warnings
151 friend class dummy_UserData
;
154 /// return the type of this object
155 virtual wxLayoutObjectType
GetType() const { return WXLO_TYPE_INVALID
; }
156 /** Calculates the size of an object.
157 @param dc the wxDC to draw on
158 @param llist the wxLayoutList
160 virtual void Layout(wxDC
&dc
, wxLayoutList
*llist
) = 0;
163 @param dc the wxDC to draw on
164 @param coords where to draw the baseline of the object.
165 @param wxllist pointer to wxLayoutList
166 @param begin if !=-1, from which position on to highlight it
167 @param end if begin !=-1, how many positions to highlight it
169 virtual void Draw(wxDC
& /* dc */,
170 wxPoint
const & /* coords */,
171 wxLayoutList
*WXUNUSED(wxllist
),
172 CoordType
WXUNUSED(begin
) = -1,
173 CoordType
WXUNUSED(end
) = -1) { }
175 /** Calculates and returns the size of the object.
176 @param top where to store height above baseline
177 @param bottom where to store height below baseline
178 @return the size of the object's box in pixels
180 virtual wxPoint
GetSize(CoordType
* top
, CoordType
*bottom
) const
181 { *top
= 0; *bottom
= 0; return wxPoint(0,0); }
183 /// Return just the width of the object on the screen.
184 virtual CoordType
GetWidth() const { return 0; }
186 /// returns the number of cursor positions occupied by this object
187 virtual CoordType
GetLength() const { return 1; }
189 /** Returns the cursor offset relating to the screen x position
190 relative to begin of object.
191 @param dc the wxDC to use for calculations
192 @param xpos relative x position from head of object
193 @return cursor coordinate offset
195 virtual CoordType
GetOffsetScreen( wxDC
& WXUNUSED(dc
),
196 CoordType
WXUNUSED(xpos
) ) const
202 wxLayoutObject() { m_UserData
= NULL
; }
204 /// delete the user data
205 virtual ~wxLayoutObject() { if(m_UserData
) m_UserData
->DecRef(); }
207 #ifdef WXLAYOUT_DEBUG
208 virtual wxString
DebugDump() const;
211 /** Tells the object about some user data. This data is associated
212 with the object and will be deleted at destruction time.
213 It is reference counted.
215 void SetUserData(UserData
*data
)
218 m_UserData
->DecRef();
223 m_UserData
->IncRef();
226 /** Return the user data.
227 Increments the object's reference count. When no longer needed,
228 caller must call DecRef() on the pointer returned.
230 UserData
* GetUserData() const { if(m_UserData
) m_UserData
->IncRef(); return m_UserData
; }
232 /** Makes a copy of this object.
234 virtual wxLayoutObject
*Copy() = 0;
236 /** Clipboard support function. Read and write objects to
239 /// Writes the object to the string.
240 virtual void Write(wxString
&ostr
) = 0;
243 @param str stream to read from, will bee changed
244 @return true on success
246 static wxLayoutObject
*Read(wxString
&istr
);
249 /// returns true if the object is shown on the screen (i.e. not cmd object)
250 bool IsVisibleObject() const { return GetType() != WXLO_TYPE_CMD
; }
253 /// optional data for application's use
254 UserData
*m_UserData
;
255 #if defined (M_BASEDIR) && defined (DEBUG)
256 MOBJECT_NAME(wxLayoutObject
)
260 /// Define a list type of wxLayoutObject pointers.
261 KBLIST_DEFINE(wxLayoutObjectList
, wxLayoutObject
);
263 /// The iterator type.
264 typedef wxLayoutObjectList::iterator wxLOiterator
;
266 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
270 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
271 /** This class implements a wxLayoutObject holding plain text.
273 class wxLayoutObjectText
: public wxLayoutObject
276 wxLayoutObjectText(const wxString
&txt
= wxEmptyString
);
278 virtual wxLayoutObjectType
GetType() const { return WXLO_TYPE_TEXT
; }
279 virtual void Layout(wxDC
&dc
, wxLayoutList
*llist
);
280 virtual void Draw(wxDC
&dc
, wxPoint
const &coords
,
281 wxLayoutList
*wxllist
,
282 CoordType begin
= -1,
285 /** Calculates and returns the size of the object.
286 @param top where to store height above baseline
287 @param bottom where to store height below baseline
288 @return the size of the object's box in pixels
290 virtual wxPoint
GetSize(CoordType
* top
, CoordType
*bottom
) const;
292 /// Return just the width of the object on the screen.
293 virtual CoordType
GetWidth() const { return m_Width
; }
295 /** Returns the cursor offset relating to the screen x position
296 relative to begin of object.
297 @param dc the wxDC to use for calculations
298 @param xpos relative x position from head of object
299 @return cursor coordinate offset
301 virtual CoordType
GetOffsetScreen(wxDC
&dc
, CoordType xpos
) const;
303 virtual void Write(wxString
&ostr
);
304 static wxLayoutObjectText
*Read(wxString
&istr
);
306 #ifdef WXLAYOUT_DEBUG
307 virtual wxString
DebugDump() const;
310 virtual CoordType
GetLength() const { return wxStrlen(m_Text
.c_str()); }
313 wxString
& GetText() { return m_Text
; }
315 void SetText(wxString
const &text
) { m_Text
= text
; }
316 /** Makes a copy of this object.
318 virtual wxLayoutObject
*Copy();
323 /// size of the box containing text
324 long m_Width
, m_Height
;
326 /// Height above baseline.
329 /// Height below baseline.
333 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
337 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
338 /** This class implements a wxLayoutObject holding a graphic.
340 class wxLayoutObjectIcon
: public wxLayoutObject
343 wxLayoutObjectIcon(wxBitmap
*icon
= NULL
);
344 wxLayoutObjectIcon(wxBitmap
const &icon
);
346 ~wxLayoutObjectIcon() { if(m_Icon
) delete m_Icon
; }
348 virtual wxLayoutObjectType
GetType() const { return WXLO_TYPE_ICON
; }
349 virtual void Layout(wxDC
&dc
, wxLayoutList
*llist
);
350 virtual void Draw(wxDC
&dc
, wxPoint
const &coords
,
351 wxLayoutList
*wxllist
,
352 CoordType begin
= -1,
355 /** Calculates and returns the size of the object.
356 @param top where to store height above baseline
357 @param bottom where to store height below baseline
358 @return the size of the object's box in pixels
360 virtual wxPoint
GetSize(CoordType
* top
, CoordType
*bottom
) const;
362 /// Return just the width of the object on the screen.
363 virtual CoordType
GetWidth() const { return m_Icon
->GetWidth(); }
365 // return a pointer to the icon
366 wxBitmap
*GetIcon() const { return m_Icon
; }
368 /** Makes a copy of this object.
370 virtual wxLayoutObject
*Copy();
371 virtual void Write(wxString
&ostr
);
372 static wxLayoutObjectIcon
*Read(wxString
&istr
);
378 /** This structure holds all formatting information.
380 struct wxLayoutStyleInfo
382 wxLayoutStyleInfo(int ifamily
= -1,
388 wxColour
*bg
= NULL
);
390 wxLayoutStyleInfo
& operator=(const wxLayoutStyleInfo
&right
);
392 wxColour
& GetBGColour() { return m_bg
; }
394 /// Font change parameters.
395 int size
, family
, style
, weight
, underline
;
399 int m_fg_valid
, m_bg_valid
; // bool, but must be int!
403 class wxFontCacheEntry
406 wxFontCacheEntry(int family
, int size
, int style
, int weight
,
409 m_Family
= family
; m_Size
= size
; m_Style
= style
;
410 m_Weight
= weight
; m_Underline
= underline
;
411 m_Font
= new wxFont(m_Size
, m_Family
,
412 m_Style
, m_Weight
, m_Underline
);
415 bool Matches(int family
, int size
, int style
, int weight
,
416 bool underline
) const
418 return size
== m_Size
&& family
== m_Family
419 && style
== m_Style
&& weight
== m_Weight
420 && underline
== m_Underline
;
423 wxFont
& GetFont() { return *m_Font
; }
432 // VZ: I wonder why it doesn't use wxLayoutStyleInfo instead of those?
433 int m_Family
, m_Size
, m_Style
, m_Weight
;
437 KBLIST_DEFINE(wxFCEList
, wxFontCacheEntry
);
442 wxFont
& GetFont(int family
, int size
, int style
, int weight
,
445 wxFont
& GetFont(wxLayoutStyleInfo
const &si
)
447 return GetFont(si
.family
, si
.size
, si
.style
, si
.weight
,
452 wxFCEList m_FontList
;
455 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
459 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
460 /** This class implements a wxLayoutObject holding style change commands.
462 class wxLayoutObjectCmd
: public wxLayoutObject
465 virtual wxLayoutObjectType
GetType() const { return WXLO_TYPE_CMD
; }
466 virtual void Layout(wxDC
&dc
, wxLayoutList
*llist
);
467 virtual void Draw(wxDC
&dc
, wxPoint
const &coords
,
468 wxLayoutList
*wxllist
,
469 CoordType begin
= -1,
472 wxLayoutObjectCmd(int family
= -1,
478 wxColour
*bg
= NULL
);
480 wxLayoutObjectCmd(const wxLayoutStyleInfo
&si
);
481 ~wxLayoutObjectCmd();
482 /** Stores the current style in the styleinfo structure */
483 wxLayoutStyleInfo
* GetStyle() const;
484 /** Makes a copy of this object.
486 virtual wxLayoutObject
*Copy();
487 virtual void Write(wxString
&ostr
);
488 static wxLayoutObjectCmd
*Read(wxString
&istr
);
491 wxLayoutStyleInfo
*m_StyleInfo
;
494 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
496 The wxLayoutLine object
498 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
500 /** This class represents a single line of objects to be displayed.
501 It knows its height and total size and whether it needs to be
503 It has pointers to its first and next line so it can automatically
504 update them as needed.
510 @param prev pointer to previous line or NULL
511 @param next pointer to following line or NULL
512 @param llist pointer to layout list
514 wxLayoutLine(wxLayoutLine
*prev
, wxLayoutList
*llist
);
515 /** This function inserts a new object at cursor position xpos.
516 @param xpos where to insert new object
517 @param obj the object to insert
518 @return true if that xpos existed and the object was inserted
520 bool Insert(CoordType xpos
, wxLayoutObject
*obj
);
522 /** This function inserts text at cursor position xpos.
523 @param xpos where to insert
524 @param text the text to insert
525 @return true if that xpos existed and the object was inserted
527 bool Insert(CoordType xpos
, const wxString
& text
);
529 /** This function appends an object to the line.
530 @param obj the object to insert
532 void Append(wxLayoutObject
* obj
)
535 m_ObjectList
.push_back(obj
);
536 m_Length
+= obj
->GetLength();
539 /** This function prepends an object to the line. */
540 void Prepend(wxLayoutObject
* obj
)
543 m_ObjectList
.push_front(obj
);
544 m_Length
+= obj
->GetLength();
547 /** This function appens the next line to this, i.e. joins the two
550 void MergeNextLine(wxLayoutList
*llist
);
552 /** This function deletes npos cursor positions from position xpos.
553 @param xpos where to delete
554 @param npos how many positions
555 @return number of positions still to be deleted
557 CoordType
Delete(CoordType xpos
, CoordType npos
);
559 /** This function breaks the line at a given cursor position.
560 @param xpos where to break it
561 @return pointer to the new line object replacing the old one
563 wxLayoutLine
*Break(CoordType xpos
, wxLayoutList
*llist
);
565 /** This function wraps the line: breaks it at a suitable point
566 and merges it with the next.
568 @return true if broken
570 bool Wrap(CoordType wrapmargin
, wxLayoutList
*llist
);
572 /** Deletes the next word from this position, including leading
574 This function does not delete over font changes, i.e. a word
575 with formatting instructions in the middle of it is treated as
576 two (three actually!) words. In fact, if the cursor is on a non-text object, that
577 one is treated as a word.
578 @param xpos from where to delete
579 @return true if a word was deleted
581 bool DeleteWord(CoordType npos
);
583 /** Finds a suitable position left to the given column to break the
585 @param column we want to break the line to the left of this
586 @return column for breaking line or -1 if no suitable location found
588 CoordType
GetWrapPosition(CoordType column
);
590 /** Finds the object which covers the cursor position xpos in this
592 @param xpos the column number
593 @param offset where to store the difference between xpos and
595 @return iterator to the object or iterator to NULL
597 wxLayoutObjectList::iterator
FindObject(CoordType xpos
, CoordType
600 /** Finds the object which covers the screen position xpos in this
602 @param dc the wxDC to use for calculations
603 @param llist the layout list to which this line belongs
604 @param xpos the screen x coordinate
605 @param offset where to store the difference between xpos and
607 @return iterator to the object or iterator to NULL
609 wxLayoutObjectList::iterator
FindObjectScreen(wxDC
&dc
,
613 bool *found
= NULL
) const ;
615 /** Finds text in this line.
616 @param needle the text to find
617 @param xpos the position where to start the search
618 @return the cursoor coord where it was found or -1
620 CoordType
FindText(const wxString
&needle
, CoordType xpos
= 0) const;
622 /** Get the first object in the list. This is used by the wxlparser
623 functions to export the list.
624 @return iterator to the first object
626 wxLayoutObjectList::iterator
GetFirstObject() const
628 return m_ObjectList
.begin();
631 /// Get the last object in the list.
632 wxLayoutObjectList::iterator
GetLastObject() const
634 return m_ObjectList
.tail();
637 /** Deletes this line, returns pointer to next line.
638 @param update If true, update all following lines.
640 wxLayoutLine
*DeleteLine(bool update
, wxLayoutList
*llist
);
642 /**@name Cursor Management */
644 /** Return the line number of this line.
645 @return the line number
647 inline CoordType
GetLineNumber() const { return m_LineNumber
; }
649 /** Return the length of the line.
650 @return line length in cursor positions
652 inline CoordType
GetLength() const { return m_Length
; }
655 /**@name Drawing and Layout */
657 /** Draws the line on a wxDC.
658 @param dc the wxDC to draw on
659 @param llist the wxLayoutList
660 @param offset an optional offset to shift printout
664 const wxPoint
&offset
= wxPoint(0,0)) const;
666 /** Recalculates the positions of objects and the height of the
668 @param dc the wxDC to draw on
669 @param llist th e wxLayoutList
670 @param cursorPos if not NULL, set cursor screen position in there
671 @param cursorSize if not cursorPos != NULL, set cursor size in there
672 @param cursorStyle if non NULL where to store styleinfo for cursor pos
673 @param cx if cursorPos != NULL, the cursor x position
674 @param suppressStyleUpdate FALSe normally, only to suppress updating of m_StyleInfo
676 void Layout(wxDC
&dc
,
678 wxPoint
*cursorPos
= NULL
,
679 wxPoint
*cursorSize
= NULL
,
680 wxLayoutStyleInfo
*cursorStyle
= NULL
,
682 bool suppressStyleUpdate
= false);
684 /** This function finds an object belonging to a given cursor
685 position. It assumes that Layout() has been called before.
686 @param dc the wxDC to use for calculations
687 @param xpos screen x position
688 @param found if non-NULL set to false if we return the last
689 object before the cursor, to true if we really have an object
691 @return pointer to the object
693 wxLayoutObject
* FindObjectScreen(wxDC
&dc
,
697 /** This sets the style info for the beginning of this line.
698 @param si styleinfo structure
700 void ApplyStyle(const wxLayoutStyleInfo
&si
)
701 { m_StyleInfo
= si
; }
705 /**@name List traversal */
707 /// Returns pointer to next line.
708 wxLayoutLine
*GetNextLine() const { return m_Next
; }
710 /// Returns pointer to previous line.
711 wxLayoutLine
*GetPreviousLine() const { return m_Previous
; }
713 /// Sets the link to the next line.
714 void SetNext(wxLayoutLine
*next
)
715 { m_Next
= next
; if(next
) next
->m_Previous
= this; }
717 /// Sets the link to the previous line.
718 void SetPrevious(wxLayoutLine
*previous
)
719 { m_Previous
= previous
; if(previous
) previous
->m_Next
= this; }
722 /// Returns the position of this line on the canvas.
723 wxPoint
GetPosition() const { return m_Position
; }
725 /// Returns the height of this line.
726 CoordType
GetHeight() const { return m_Height
; }
728 /// Returns the width of this line.
729 CoordType
GetWidth() const { return m_Width
; }
731 /// Recalculates the position of this line on the canvas.
732 wxPoint
RecalculatePosition(wxLayoutList
*llist
);
734 /** Copies the contents of this line to another wxLayoutList
735 @param llist the wxLayoutList destination
736 @param from x cursor coordinate where to start
737 @param to x cursor coordinate where to stop, -1 for end of line
739 void Copy(wxLayoutList
*llist
,
743 #ifdef WXLAYOUT_DEBUG
746 wxLayoutStyleInfo
const & GetStyleInfo() const { return m_StyleInfo
; }
748 /// Returns dirty state
749 bool IsDirty() const { return m_Dirty
; }
751 /** Marks this line as diry.
752 @param left xpos from where it is dirty or -1 for all
754 void MarkDirty(CoordType left
= -1)
758 if ( m_updateLeft
== -1 || left
< m_updateLeft
)
763 if(m_Next
) m_Next
->MarkDirty();
766 /// Reset the dirty flag
767 void MarkClean() { m_Dirty
= false; m_updateLeft
= -1; }
770 /// Destructor is private. Use DeleteLine() to remove it.
773 /**@name Functions to let the lines synchronise with each other. */
775 /** Sets the height of this line. Will mark following lines as
777 @param height new height
779 void SetHeight( CoordType height
, wxLayoutList
* WXUNUSED(llist
) )
781 m_Height
= height
; MarkDirty();
784 /** Updates the line numbers. */
790 CoordType m_LineNumber
;
792 /// The line length in cursor positions.
795 /// The total height of the line.
798 /// The total width of the line on screen.
801 /// The baseline for drawing objects
802 CoordType m_BaseLine
;
804 /// The position on the canvas.
807 /// The list of objects
808 wxLayoutObjectList m_ObjectList
;
810 /// Have we been changed since the last layout?
813 /// The coordinate of the left boundary of the update rectangle (if m_Dirty)
814 CoordType m_updateLeft
;
816 /// Pointer to previous line if it exists.
817 wxLayoutLine
*m_Previous
;
819 /// Pointer to next line if it exists.
820 wxLayoutLine
*m_Next
;
822 /// A StyleInfo structure, holding the current settings.
823 wxLayoutStyleInfo m_StyleInfo
;
825 /// Just to suppress gcc compiler warnings.
829 wxLayoutLine(const wxLayoutLine
&);
833 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
835 The wxLayoutList object
837 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
838 /** The wxLayoutList is a list of wxLayoutLine objects. It provides a
839 higher level of abstraction for the text and can generally be considered
840 as representing "the text".
851 #ifdef WXLAYOUT_USE_CARET
852 /// give us the pointer to the caret to use
853 void SetCaret(wxCaret
*caret
) { m_caret
= caret
; }
854 #endif // WXLAYOUT_USE_CARET
857 void Clear(int family
= wxROMAN
,
858 int size
=WXLO_DEFAULTFONTSIZE
,
865 /// Empty: clear the list but leave font settings.
868 /** Enable or disable auto-formatting. Normally, while editing this
869 should be enabled which is the default. While
870 inserting/deleting lots of text, it makes sense to temporarily
872 @param enable true to enable, false to disable
875 void SetAutoFormatting(bool enable
= true)
876 { m_AutoFormat
= enable
; }
878 /**@name Cursor Management */
880 /** Set new cursor position.
881 @param p new position
882 @return bool if it could be set
884 bool MoveCursorTo(wxPoint
const &p
);
886 /** Move cursor up or down.
888 @return bool if it could be moved
890 bool MoveCursorVertically(int n
);
892 /** Move cursor left or right.
893 @param n = number of positions to move
894 @return bool if it could be moved
896 bool MoveCursorHorizontally(int n
);
898 /** Move cursor to the left or right counting in words
899 @param n = number of positions in words
900 @param untilNext: puts the cursor at the start of the next word if true,
901 leaves it at the end of the current one otherwise
902 @return bool if it could be moved
904 bool MoveCursorWord(int n
, bool untilNext
= true);
906 /// Move cursor to end of line.
907 void MoveCursorToEndOfLine()
909 wxASSERT(m_CursorLine
);
910 MoveCursorHorizontally(m_CursorLine
->GetLength()-m_CursorPos
.x
);
913 /// Move cursor to the start of line.
914 void MoveCursorToBeginOfLine()
915 { MoveCursorHorizontally(-m_CursorPos
.x
); }
917 /// get the number of lines in the list
918 size_t GetNumLines() const { return m_numLines
; }
920 /// Returns current cursor position.
921 const wxPoint
&GetCursorPos(wxDC
& WXUNUSED(dc
) ) const
922 { return m_CursorPos
; }
923 const wxPoint
&GetCursorPos() const { return m_CursorPos
; }
924 wxLayoutLine
* GetCursorLine() { return m_CursorLine
; }
926 /// move cursor to the end of text
927 void MoveCursorToEnd()
929 MoveCursorTo(wxPoint(0, GetNumLines() - 1));
930 MoveCursorToEndOfLine();
935 /**@name Editing functions.
936 All of these functions return true on success and false on
939 /// Insert text at current cursor position.
940 bool Insert(wxString
const &text
);
942 /// Insert some other object at current cursor position.
943 bool Insert(wxLayoutObject
*obj
);
945 /// Inserts objects at current cursor positions
946 bool Insert(wxLayoutList
*llist
);
948 /// Inserts a linebreak at current cursor position.
950 /** Wraps the current line. Searches to the left of the cursor to
951 break the line. Does nothing if the cursor position is before
952 the break position parameter.
953 @param column the break position for the line, maximum length
954 @return true if line got broken
956 bool WrapLine(CoordType column
);
958 /** Wraps the complete buffer.
959 @param column the break position for the line, maximum length
960 @return true if line got broken
962 bool WrapAll(CoordType column
);
963 /** This function deletes npos cursor positions.
964 @param npos how many positions
965 @return true if everything got deleted
967 bool Delete(CoordType npos
);
969 /** Delete the next n lines.
970 @param n how many lines to delete
971 @return how many it could not delete
973 int DeleteLines(int n
);
975 /// Delete to end of line.
976 void DeleteToEndOfLine()
978 wxASSERT(m_CursorLine
);
979 Delete(m_CursorLine
->GetLength()-m_CursorPos
.x
);
982 /// Delete to begin of line.
983 void DeleteToBeginOfLine()
985 wxASSERT(m_CursorLine
);
986 CoordType n
= m_CursorPos
.x
;
987 #ifdef WXLAYOUT_DEBUG
988 wxASSERT(MoveCursorHorizontally(-n
));
990 MoveCursorHorizontally(-n
);
995 /** Delete the next word.
999 wxASSERT(m_CursorLine
);
1000 m_CursorLine
->DeleteWord(m_CursorPos
.x
);
1005 /** Finds text in this list.
1006 @param needle the text to find
1007 @param cpos the position where to start the search
1008 @return the cursor coord where it was found or (-1,-1)
1010 wxPoint
FindText(const wxString
&needle
, const wxPoint
&cpos
= wxPoint(0,0)) const;
1012 /**@name Formatting options */
1014 /// sets font parameters
1015 void SetFont(int family
, int size
, int style
,
1016 int weight
, int underline
,
1020 /// sets font parameters, colours by name
1021 void SetFont(int family
=-1, int size
= -1, int style
=-1,
1022 int weight
=-1, int underline
= -1,
1023 wxChar
const *fg
= NULL
,
1024 wxChar
const *bg
= NULL
);
1026 /// changes to the next larger font size
1027 inline void SetFontLarger()
1028 { SetFont(-1,(12*m_CurrentStyleInfo
.size
)/10); }
1030 /// changes to the next smaller font size
1031 inline void SetFontSmaller()
1032 { SetFont(-1,(10*m_CurrentStyleInfo
.size
)/12); }
1035 inline void SetFontFamily(int family
) { SetFont(family
); }
1038 inline void SetFontSize(int size
) { SetFont(-1,size
); }
1041 inline void SetFontStyle(int style
) { SetFont(-1,-1,style
); }
1044 inline void SetFontWeight(int weight
) { SetFont(-1,-1,-1,weight
); }
1046 /// toggle underline flag
1047 inline void SetFontUnderline(bool ul
) { SetFont(-1,-1,-1,-1,(int)ul
); }
1049 /// set font colours by name
1050 inline void SetFontColour(wxChar
const *fg
, wxChar
const *bg
= NULL
)
1051 { SetFont(-1,-1,-1,-1,-1,fg
,bg
); }
1053 /// set font colours by colour
1054 inline void SetFontColour(wxColour
*fg
, wxColour
*bg
= NULL
)
1055 { SetFont(-1,-1,-1,-1,-1,fg
,bg
); }
1058 Returns a pointer to the default settings.
1059 This is only valid temporarily and should not be stored
1061 @return the default settings of the list
1063 wxLayoutStyleInfo
&GetDefaultStyleInfo() { return m_DefaultStyleInfo
; }
1064 wxLayoutStyleInfo
&GetStyleInfo() { return m_CurrentStyleInfo
; }
1065 const wxLayoutStyleInfo
&GetStyleInfo() const { return m_CurrentStyleInfo
; }
1066 const wxLayoutStyleInfo
&GetCursorStyleInfo() const { return m_CursorStyleInfo
; }
1068 /// is the current font underlined?
1069 bool IsFontUnderlined() const { return GetCursorStyleInfo().underline
!= 0; }
1071 /// is the current font bold?
1072 bool IsFontBold() const { return GetCursorStyleInfo().weight
== wxBOLD
; }
1074 /// is the current font italic?
1075 bool IsFontItalic() const { return GetCursorStyleInfo().style
== wxITALIC
; }
1077 /// set underline if it was off, turn it off if it was on
1078 void ToggleFontUnderline()
1079 { SetFontUnderline(!IsFontUnderlined()); }
1081 /// make font bold if it was normal or make it normal if it was bold
1082 void ToggleFontWeight()
1083 { SetFontWeight(IsFontBold() ? wxNORMAL
: wxBOLD
); }
1085 /// make font italic if it was normal or make it normal if it was italic
1086 void ToggleFontItalics()
1087 { SetFontStyle(IsFontItalic() ? wxNORMAL
: wxITALIC
); }
1093 /** Draws the complete list on a wxDC.
1094 @param dc the wxDC to draw on
1095 @param offset an optional offset to shift printout
1096 @param top optional y coordinate where to start drawing
1097 @param bottom optional y coordinate where to stop drawing
1098 @param clipStrictly if set, do not draw objects which reach
1099 beyond "bottom". Set this when printing.
1102 const wxPoint
&offset
= wxPoint(0,0),
1103 CoordType top
= -1, CoordType bottom
= -1,
1104 bool clipStrictly
= false);
1106 /** Calculates new layout for the list, like Draw() but does not
1108 @param dc the wxDC to draw on
1109 @param bottom optional y coordinate where to stop calculating
1110 @param forceAll force re-layout of all lines
1111 @param cpos Can hold a cursorposition, and will be overwritten
1112 with the corresponding DC position.
1113 @param csize Will hold the cursor size relating to cpos.
1115 void Layout(wxDC
&dc
, CoordType bottom
= -1, bool forceAll
= false,
1116 wxPoint
*cpos
= NULL
,
1117 wxPoint
*csize
= NULL
);
1119 /** Ensure that the whole list will be recalculate on the next call
1120 to Layout() or Draw().
1121 @param redrawAll true or false to reset it
1123 void ForceTotalLayout(bool redrawAll
= true)
1124 { m_ReLayoutAll
= redrawAll
; }
1126 /** Returns the screen coordinates relating to a given cursor
1127 position and the size of the cursor at that position.
1128 @param dc for which to calculate it
1129 @param cpos Cursor position to look for.
1130 @param csize If non-NULL, will be set to the cursor size.
1131 @return The cursor position on the DC.
1133 wxPoint
GetScreenPos(wxDC
&dc
, const wxPoint
&cpos
, wxPoint
*csize
= NULL
);
1135 /** Calculates new sizes for everything in the list, like Layout()
1136 but this is needed after the list got changed.
1137 @param dc the wxDC to draw on
1138 @param bottom optional y coordinate where to stop calculating
1140 void Recalculate(wxDC
&dc
, CoordType bottom
= -1);
1142 /** Returns the size of the list in screen coordinates.
1143 The return value only makes sense after the list has been
1145 @return a wxPoint holding the maximal x/y coordinates used for
1148 wxPoint
GetSize() const;
1150 /** Returns the cursor position on the screen.
1152 wxPoint
GetCursorScreenPos() const;
1154 /** Draws the cursor.
1155 @param active If true, draw a bold cursor to mark window as
1157 @param translate optional translation of cursor coords on screen
1159 void DrawCursor(wxDC
&dc
,
1161 const wxPoint
& translate
= wxPoint(0,0));
1163 /** This function finds an object belonging to a given screen
1164 position. It assumes that Layout() has been called before.
1165 @param pos screen position
1166 @param cursorPos if non NULL, store cursor position in there
1167 @param found if used, set this to true if we really found an
1168 object, to false if we had to take the object near to it
1169 @return pointer to the object
1171 wxLayoutObject
* FindObjectScreen(wxDC
&dc
,
1173 wxPoint
*cursorPos
= NULL
,
1174 bool *found
= NULL
);
1176 /** Called by the objects to update the update rectangle.
1177 @param x horizontal coordinate to include in rectangle
1178 @param y vertical coordinate to include in rectangle
1180 void SetUpdateRect(CoordType x
, CoordType y
);
1182 /** Called by the objects to update the update rectangle.
1183 @param p a point to include in it
1185 void SetUpdateRect(const wxPoint
&p
)
1186 { SetUpdateRect(p
.x
,p
.y
); }
1188 /// adds the cursor position to the update rectangle
1189 void AddCursorPosToUpdateRect()
1191 #ifndef WXLAYOUT_USE_CARET
1192 SetUpdateRect(m_CursorScreenPos
);
1193 SetUpdateRect(m_CursorScreenPos
+m_CursorSize
);
1194 //#else - the caret will take care of refreshing itself
1195 #endif // !WXLAYOUT_USE_CARET
1198 /// Invalidates the update rectangle.
1199 void InvalidateUpdateRect() { m_UpdateRectValid
= false; }
1201 /// Returns the update rectangle.
1202 const wxRect
*GetUpdateRect() const { return &m_UpdateRect
; }
1205 /// get the current cursor size
1206 const wxPoint
& GetCursorSize() const { return m_CursorSize
; }
1208 /**@name For exporting one object after another. */
1210 /** Returns a pointer to the first line in the list. */
1211 wxLayoutLine
*GetFirstLine()
1213 wxASSERT(m_FirstLine
);
1218 /// Begin selecting text
1219 void StartSelection(const wxPoint
& cpos
= wxPoint(-1,-1),
1220 const wxPoint
& spos
= wxPoint(-1,-1));
1222 // Continue selecting text
1223 void ContinueSelection(const wxPoint
& cpos
= wxPoint(-1,-1),
1224 const wxPoint
& spos
= wxPoint(-1,-1));
1226 /// End selecting text.
1227 void EndSelection(const wxPoint
& cpos
= wxPoint(-1,-1),
1228 const wxPoint
& spos
= wxPoint(-1,-1));
1230 /// Discard the current selection
1231 void DiscardSelection();
1233 /// Are we still selecting text?
1234 bool IsSelecting() const;
1236 /// Is the given point (text coords) selected?
1237 bool IsSelected(const wxPoint
&cursor
) const;
1239 /// Do we have a non null selection?
1240 bool HasSelection() const
1241 { return m_Selection
.m_valid
|| m_Selection
.m_selecting
; }
1243 /** Return the selection as a wxLayoutList.
1244 @param invalidate if true, the selection will be invalidated after this and can no longer be used.
1245 @return Another layout list object holding the selection, must be freed by caller
1247 wxLayoutList
*GetSelection(class wxLayoutDataObject
*wxldo
= NULL
, bool invalidate
= true);
1249 /// Delete selected bit
1250 void DeleteSelection();
1252 wxLayoutList
*Copy(const wxPoint
&from
= wxPoint(0,0),
1253 const wxPoint
&to
= wxPoint(-1,-1));
1255 /// starts highlighting of text for selections
1256 void StartHighlighting(wxDC
&dc
);
1258 /// ends highlighting of text for selections
1259 void EndHighlighting(wxDC
&dc
);
1261 /** Tests whether this layout line is selected and needs
1263 @param line to test for
1264 @param from set to first cursorpos to be highlighted (for returncode == -1)
1265 @param to set to last cursorpos to be highlighted (for returncode == -1)
1266 @return 0 = not selected, 1 = fully selected, -1 = partially
1269 int IsSelected(const wxLayoutLine
*line
, CoordType
*from
, CoordType
*to
);
1271 void ApplyStyle(wxLayoutStyleInfo
const &si
, wxDC
&dc
);
1272 #ifdef WXLAYOUT_DEBUG
1276 // for wxLayoutLine usage only
1277 void IncNumLines() { m_numLines
++; }
1278 void DecNumLines() { m_numLines
--; }
1280 /// get the line by number
1281 wxLayoutLine
*GetLine(CoordType index
) const;
1283 /** Reads objects from a string and inserts them. Returns NULL if
1284 string is empty or a linebreak was found.
1285 @param istr stream to read from, will bee changed
1287 void Read(wxString
&istr
);
1291 void InternalClear();
1293 /// The list of lines.
1294 wxLayoutLine
*m_FirstLine
;
1296 /// The number of lines in the list (store instead recalculating for speed)
1299 /// The update rectangle which needs to be refreshed:
1300 wxRect m_UpdateRect
;
1302 /// Is the update rectangle valid?
1303 bool m_UpdateRectValid
;
1305 /// Shall we auto-format?
1308 /// Shall we re-layout everything?
1311 /**@name Cursor Management */
1313 /// Where the text cursor (column,line) is.
1314 wxPoint m_CursorPos
;
1316 /// Where the cursor should be drawn.
1317 wxPoint m_CursorScreenPos
;
1319 /// The line where the cursor is.
1320 wxLayoutLine
*m_CursorLine
;
1322 /// The size of the cursor.
1323 wxPoint m_CursorSize
;
1325 /// Has the cursor moved (is m_CursorScreenPos up to date)?
1328 #ifdef WXLAYOUT_USE_CARET
1331 #endif // WXLAYOUT_USE_CARET
1334 /// selection.state and begin/end coordinates
1337 Selection() { m_valid
= m_selecting
= m_discarded
= false; }
1341 bool m_discarded
; // may be true only until the next redraw
1343 // returns true if we already have the screen coordinates of the
1344 // selection start and end
1345 bool HasValidScreenCoords() const
1346 { return m_ScreenA
.x
!= -1 && m_ScreenB
.x
!= -1; }
1348 // the start and end of the selection coordinates in pixels
1349 wxPoint m_ScreenA
, m_ScreenB
;
1351 // these coordinates are in text positions, not in pixels
1352 wxPoint m_CursorA
, m_CursorB
;
1354 /** @name Font parameters. */
1356 /// this object manages the fonts for us
1357 wxFontCache m_FontCache
;
1359 /// the default setting:
1360 wxLayoutStyleInfo m_DefaultStyleInfo
;
1362 /// the current setting:
1363 wxLayoutStyleInfo m_CurrentStyleInfo
;
1365 /// the current setting:
1366 wxLayoutStyleInfo m_CursorStyleInfo
;
1370 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1372 The wxLayoutDataObject for exporting data to the clipboard in our
1375 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1376 class wxLayoutDataObject
: public wxCustomDataObject
1379 wxLayoutDataObject()
1381 SetFormat(wxT("application/wxlayoutlist"));
1384 // type safe wrappers
1385 void SetLayoutData(const wxString
& text
)
1386 { SetData(text
.length() + 1, text
.c_str()); }
1388 const wxChar
*GetLayoutData() const { return (const wxChar
*)GetData(); }
1391 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1393 The wxLayoutPrintout object for printing within the wxWidgets print
1396 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1397 /** This class implements a wxPrintout for printing a wxLayoutList within
1398 the wxWidgets printing framework.
1400 class wxLayoutPrintout
: public wxPrintout
1404 @param llist pointer to the wxLayoutList to be printed
1405 @param title title for PS file or windows
1407 wxLayoutPrintout(wxLayoutList
*llist
,
1408 wxString
const & title
=
1409 _T("wxLayout Printout"));
1412 ~wxLayoutPrintout(){};
1414 /** Function which prints the n-th page.
1415 @param page the page number to print
1416 @return bool true if we are not at end of document yet
1418 bool OnPrintPage(int page
);
1419 /** Checks whether page exists in document.
1420 @param page number of page
1421 @return true if page exists
1423 bool HasPage(int page
);
1425 /** Gets called from wxWidgets to find out which pages are existing.
1426 I'm not totally sure about the parameters though.
1427 @param minPage the first page in the document
1428 @param maxPage the last page in the document
1429 @param selPageFrom the first page to be printed
1430 @param selPageTo the last page to be printed
1432 void GetPageInfo(int *minPage
, int *maxPage
,
1433 int *selPageFrom
, int *selPageTo
);
1435 /** This little function scales the DC so that the printout has
1436 roughly the same size as the output on screen.
1437 @param dc the wxDC to scale
1438 @return the scale that was applied
1440 float ScaleDC(wxDC
*dc
);
1443 virtual void DrawHeader(wxDC &dc, wxPoint topleft, wxPoint bottomright, int pageno);
1447 /// The list to print.
1448 wxLayoutList
*m_llist
;
1450 /// Title for PS file or window.
1453 /// The real paper size.
1454 int m_PageHeight
, m_PageWidth
;
1456 /// How much we actually print per page.
1457 int m_PrintoutHeight
;
1459 /// How many pages we need to print.
1462 /// Top left corner where we start printing.