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