1 /*-*- c++ -*-********************************************************
2 * wxFTCanvas: a canvas for editing formatted text *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
7 *******************************************************************/
10 - each Object knows its size and how to draw itself
11 - the list is responsible for calculating positions
12 - the draw coordinates for each object are the top left corner
13 - coordinates only get calculated when things get redrawn
14 - during redraw each line gets iterated over twice, first just
15 calculating baselines and positions, second to actually draw it
16 - the cursor position is the position before an object, i.e. if the
17 buffer starts with a text-object, cursor 0,0 is just before the
22 #pragma implementation "wxllist.h"
25 // these two lines are for use in M:
27 //#include "gui/wxllist.h"
32 # include "iostream.h"
35 # include <wx/postscrp.h>
36 # include <wx/print.h>
39 #define BASELINESTRETCH 12
42 static const char *_t
[] = { "invalid", "text", "cmd", "icon",
46 wxLayoutObjectBase::Debug(void)
49 cerr
<< _t
[GetType()] << ": size=" << GetSize(&bl
).x
<< ","
50 << GetSize(&bl
).y
<< " bl=" << bl
;
53 # define WXL_VAR(x) cerr << #x"=" << x << endl;
54 # define WXL_DBG_POINT(p) cerr << #p << ": " << p.x << ',' << p.y << endl
55 # define WXL_TRACE(f) cerr << #f":" << endl;
58 # define WXL_DBG_POINT(p)
62 //-------------------------- wxLayoutObjectText
64 wxLayoutObjectText::wxLayoutObjectText(const String
&txt
)
73 wxLayoutObjectText::GetSize(CoordType
*baseLine
) const
75 if(baseLine
) *baseLine
= m_BaseLine
;
76 return wxPoint(m_Width
, m_Height
);
80 wxLayoutObjectText::Draw(wxDC
&dc
, wxPoint position
, CoordType baseLine
,
84 dc
.GetTextExtent(Str(m_Text
),&m_Width
, &m_Height
, &descent
);
85 //FIXME: wxGTK does not set descent to a descent value yet.
87 descent
= (2*m_Height
)/10; // crude fix
88 m_BaseLine
= m_Height
- descent
;
89 position
.y
+= baseLine
-m_BaseLine
;
91 dc
.DrawText(Str(m_Text
),position
.x
,position
.y
);
92 # ifdef WXLAYOUT_DEBUG
93 // dc.DrawRectangle(position.x, position.y, m_Width, m_Height);
99 wxLayoutObjectText::Debug(void)
101 wxLayoutObjectBase::Debug();
102 cerr
<< " `" << m_Text
<< '\'';
106 //-------------------------- wxLayoutObjectIcon
108 wxLayoutObjectIcon::wxLayoutObjectIcon(wxIcon
*icon
)
114 wxLayoutObjectIcon::Draw(wxDC
&dc
, wxPoint position
, CoordType baseLine
,
117 position
.y
+= baseLine
- m_Icon
->GetHeight();
119 dc
.DrawIcon(m_Icon
,position
.x
,position
.y
);
123 wxLayoutObjectIcon::GetSize(CoordType
*baseLine
) const
126 *baseLine
= m_Icon
->GetHeight();
127 return wxPoint(m_Icon
->GetWidth(), m_Icon
->GetHeight());
130 //-------------------------- wxLayoutObjectCmd
133 wxLayoutObjectCmd::wxLayoutObjectCmd(int size
, int family
, int style
, int
134 weight
, bool underline
,
135 wxColour
const *fg
, wxColour
const *bg
)
138 m_font
= new wxFont(size
,family
,style
,weight
,underline
);
143 wxLayoutObjectCmd::~wxLayoutObjectCmd()
149 wxLayoutObjectCmd::GetStyle(void) const
151 wxLayoutStyleInfo
*si
= new wxLayoutStyleInfo();
154 si
->size
= m_font
->GetPointSize();
155 si
->family
= m_font
->GetFamily();
156 si
->style
= m_font
->GetStyle();
157 si
->underline
= m_font
->GetUnderlined();
158 si
->weight
= m_font
->GetWeight();
160 si
->fg_red
= m_ColourFG
->Red();
161 si
->fg_green
= m_ColourFG
->Green();
162 si
->fg_blue
= m_ColourFG
->Blue();
163 si
->bg_red
= m_ColourBG
->Red();
164 si
->bg_green
= m_ColourBG
->Green();
165 si
->bg_blue
= m_ColourBG
->Blue();
171 wxLayoutObjectCmd::Draw(wxDC
&dc
, wxPoint position
, CoordType lineHeight
,
175 // this get called even when draw==false, so that recalculation
176 // uses right font sizes
179 dc
.SetTextForeground(*m_ColourFG
);
181 dc
.SetTextBackground(*m_ColourBG
);
184 //-------------------------- wxwxLayoutList
186 wxLayoutList::wxLayoutList()
188 m_DefaultSetting
= NULL
;
192 wxLayoutList::~wxLayoutList()
195 delete m_DefaultSetting
;
200 wxLayoutList::LineBreak(void)
202 Insert(new wxLayoutObjectLineBreak
);
203 m_CursorPosition
.x
= 0; m_CursorPosition
.y
++;
207 wxLayoutList::SetFont(int family
, int size
, int style
, int weight
,
208 int underline
, wxColour
const *fg
,
211 if(family
!= -1) m_FontFamily
= family
;
212 if(size
!= -1) m_FontPtSize
= size
;
213 if(style
!= -1) m_FontStyle
= style
;
214 if(weight
!= -1) m_FontWeight
= weight
;
215 if(underline
!= -1) m_FontUnderline
= underline
!= 0;
217 if(fg
!= NULL
) m_ColourFG
= fg
;
218 if(bg
!= NULL
) m_ColourBG
= bg
;
221 new wxLayoutObjectCmd(m_FontPtSize
,m_FontFamily
,m_FontStyle
,m_FontWeight
,m_FontUnderline
,
222 m_ColourFG
, m_ColourBG
));
226 wxLayoutList::SetFont(int family
, int size
, int style
, int weight
,
227 int underline
, char const *fg
, char const *bg
)
234 cfg
= wxTheColourDatabase
->FindColour(fg
);
236 cbg
= wxTheColourDatabase
->FindColour(bg
);
238 SetFont(family
,size
,style
,weight
,underline
,cfg
,cbg
);
242 /// for access by wxLayoutWindow:
244 wxLayoutList::GetSize(CoordType
*max_x
, CoordType
*max_y
,
245 CoordType
*lineHeight
)
247 wxASSERT(max_x
); wxASSERT(max_y
); wxASSERT(lineHeight
);
250 *lineHeight
= m_LineHeight
;
254 wxLayoutList::Draw(wxDC
&dc
, bool findObject
, wxPoint
const &findCoords
)
256 wxLayoutObjectList::iterator i
;
258 // in case we need to look for an object
259 wxLayoutObjectBase
*foundObject
= NULL
;
261 // first object in current line
262 wxLayoutObjectList::iterator headOfLine
;
264 // do we need to recalculate current line?
265 bool recalculate
= false;
267 // do we calculate or draw? Either true or false.
269 // drawing parameters:
270 wxPoint position
= wxPoint(0,0);
271 wxPoint position_HeadOfLine
;
272 CoordType baseLine
= m_FontPtSize
;
273 CoordType baseLineSkip
= (BASELINESTRETCH
* baseLine
)/10;
275 // where to draw the cursor
276 wxPoint cursorPosition
, cursorSize
;
278 // we trace the objects' cursor positions so we can draw the cursor
279 wxPoint cursor
= wxPoint(0,0);
280 // the cursor position inside the object
281 CoordType cursorOffset
= 0;
282 // object under cursor
283 wxLayoutObjectList::iterator cursorObject
= FindCurrentObject(&cursorOffset
);
285 // queried from each object:
286 wxPoint size
= wxPoint(0,0);
287 CoordType objBaseLine
= baseLine
;
288 wxLayoutObjectType type
;
291 wxLayoutObjectText
*tobj
= NULL
;
294 // this is needed for printing to a printer:
295 // only interesting for printer/PS output
296 int pageWidth
, pageHeight
; //GetSize() still needs int at the moment
299 int top
, bottom
, left
, right
;
304 dc
.IsKindOf(CLASSINFO(wxPrinterDC
)) ||
306 dc
.IsKindOf(CLASSINFO(wxPostScriptDC
)))
308 WXL_VAR(wxThePrintSetupData
);
310 dc
.GetSize(&pageWidth
, &pageHeight
);
311 dc
.StartDoc(_("Printing..."));
313 margins
.top
= (1*pageHeight
)/10; // 10%
314 margins
.bottom
= (9*pageHeight
)/10; // 90%
315 margins
.left
= (1*pageWidth
)/10;
316 margins
.right
= (9*pageWidth
)/10;
320 margins
.top
= 0; margins
.left
= 0;
324 position
.y
= margins
.right
;
325 position
.x
= margins
.left
;
327 WXL_VAR(findObject
); WXL_VAR(findCoords
.x
); WXL_VAR(findCoords
.y
);
328 // if the cursorobject is a cmd, we need to find the first
330 while(cursorObject
!= end()
331 && (*cursorObject
)->GetType() == WXLO_TYPE_CMD
)
334 headOfLine
= begin();
335 position_HeadOfLine
= position
;
337 // setting up the default:
338 dc
.SetTextForeground( *wxBLACK
);
339 dc
.SetTextBackground( *wxWHITE
);
340 dc
.SetBackgroundMode( wxSOLID
); // to enable setting of text background
341 dc
.SetFont( *wxNORMAL_FONT
);
344 //FIXME: who frees the brush, how long does it need to exist?
346 m_DefaultSetting
->Draw(dc
,wxPoint(0,0),0,true);
348 // we calculate everything for drawing a line, then rewind to the
349 // begin of line and actually draw it
357 type
= (*i
)->GetType();
359 // to initialise sizes of objects, we need to call Draw
360 (*i
)->Draw(dc
, position
, baseLine
, draw
);
362 // update coordinates for next object:
363 size
= (*i
)->GetSize(&objBaseLine
);
364 if(findObject
&& draw
) // we need to look for an object
366 if(findCoords
.y
>= position
.y
367 && findCoords
.y
<= position
.y
+size
.y
368 && findCoords
.x
>= position
.x
369 && findCoords
.x
<= position
.x
+size
.x
)
372 findObject
= false; // speeds things up
376 if(m_Editable
&& draw
&& i
== cursorObject
)
378 if(type
== WXLO_TYPE_TEXT
) // special treatment
380 long descent
= 0l; long width
, height
;
381 tobj
= (wxLayoutObjectText
*)*i
;
382 String str
= tobj
->GetText();
383 WXL_VAR(m_CursorPosition
.x
); WXL_VAR(cursor
.x
);
384 str
= str
.substr(0, cursorOffset
);
386 dc
.GetTextExtent(Str(str
), &width
,&height
, &descent
);
390 cursorPosition
= wxPoint(position
.x
+width
,
391 position
.y
+(baseLineSkip
-height
));
392 cursorSize
= wxPoint(1, height
);
393 //dc.DrawLine(position.x+width,
394 // position.y+(baseLineSkip-height),
395 // position.x+width, position.y+baseLineSkip);
399 if(type
== WXLO_TYPE_LINEBREAK
)
400 //dc.DrawLine(0, position.y+baseLineSkip, 0, position.y+2*baseLineSkip);
402 cursorPosition
= wxPoint(0, position
.y
);
403 cursorSize
= wxPoint(1,baseLineSkip
);
407 cursorPosition
= wxPoint(position
.x
, position
.y
);
408 cursorSize
= wxPoint(size
.x
> 0 ? size
.x
: 1,size
.y
> 0 ? size
.y
: baseLineSkip
);
413 // calculate next object's position:
414 position
.x
+= size
.x
;
415 if(position
.x
> m_MaxX
)
418 // do we need to increase the line's height?
419 if(size
.y
> baseLineSkip
)
421 baseLineSkip
= size
.y
;
424 if(objBaseLine
> baseLine
)
426 baseLine
= objBaseLine
;
430 // now check whether we have finished handling this line:
431 if(type
== WXLO_TYPE_LINEBREAK
|| i
== tail())
433 if(recalculate
) // do this line again
435 position
.x
= position_HeadOfLine
.x
;
440 if(! draw
) // finished calculating sizes
442 // if the this line needs to go onto a new page, we need
443 // to change pages before drawing it:
444 if(margins
.bottom
!= -1 && position
.y
> margins
.bottom
)
447 position_HeadOfLine
.y
= margins
.top
;
450 // do this line again, this time drawing it
451 position
= position_HeadOfLine
;
456 else // we have drawn a line, so continue calculating next one
460 // is it a linebreak?
461 if(type
== WXLO_TYPE_LINEBREAK
|| i
== tail())
463 position
.x
= margins
.left
;
464 position
.y
+= baseLineSkip
;
465 baseLine
= m_FontPtSize
;
466 objBaseLine
= baseLine
; // not all objects set it
467 baseLineSkip
= (BASELINESTRETCH
* baseLine
)/10;
470 position_HeadOfLine
= position
;
478 dc
.DrawRectangle(cursorPosition
.x
, cursorPosition
.y
,
479 cursorSize
.x
, cursorSize
.y
);
485 #ifdef WXLAYOUT_DEBUG
487 wxLayoutList::Debug(void)
490 wxLayoutObjectList::iterator i
;
493 "------------------------debug start-------------------------" << endl
;
494 for(i
= begin(); i
!= end(); i
++)
500 "-----------------------debug end----------------------------"
502 // show current object:
504 << m_CursorPosition
.x
<< ','
505 << m_CursorPosition
.y
;
507 i
= FindCurrentObject(&offs
);
508 cerr
<< " line length: " << GetLineLength(i
) << " ";
511 cerr
<< "<<no object found>>" << endl
;
512 return; // FIXME we should set cursor position to maximum allowed
515 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
517 cerr
<< " \"" << ((wxLayoutObjectText
*)(*i
))->GetText() << "\", offs: "
521 cerr
<< ' ' << _t
[(*i
)->GetType()] << endl
;
526 /******************** editing stuff ********************/
528 // don't change this, I know how to optimise this and will do it real
533 * Finds the object belonging to a given cursor position cpos and
534 * returns an iterator to that object and stores the relative cursor
535 * position in offset.
537 * For linebreaks, the offset can be 0=before or 1=after.
539 * If the cpos coordinates don't exist, they are modified.
542 wxLayoutObjectList::iterator
543 wxLayoutList::FindObjectCursor(wxPoint
*cpos
, CoordType
*offset
)
545 wxPoint object
= wxPoint(0,0); // runs along the objects
547 wxLayoutObjectList::iterator i
;
549 #ifdef WXLAYOUT_DEBUG
550 cerr
<< "Looking for object at " << cpos
->x
<< ',' << cpos
->y
<<
553 for(i
= begin(); i
!= end() && object
.y
<= cpos
->y
; i
++)
555 width
= (**i
).CountPositions();
556 if(cpos
->y
== object
.y
) // a possible candidate
558 if((**i
).GetType() ==WXLO_TYPE_LINEBREAK
)
560 if(cpos
->x
== object
.x
)
569 if(cpos
->x
>= object
.x
&& cpos
->x
<= object
.x
+width
) // overlap
571 if(offset
) *offset
= cpos
->x
-object
.x
;
572 #ifdef WXLAYOUT_DEBUG
573 cerr
<< " found object at " << object
.x
<< ',' <<
574 object
.y
<< ", type:" << _t
[(*i
)->GetType()] <<endl
;
579 // no overlap, increment coordinates
581 if((**i
).GetType() == WXLO_TYPE_LINEBREAK
)
587 #ifdef WXLAYOUT_DEBUG
588 cerr
<< " not found" << endl
;
590 // return last object, coordinates of that one:
594 if((**i
).GetType()==WXLO_TYPE_LINEBREAK
)
597 *offset
= (cpos
->x
> object
.x
) ? 1 : 0;
600 cpos
->x
= object
.x
; // would be the coordinate of next object
602 cpos
->x
+= width
; // last object's width
603 if(*offset
) *offset
= cpos
->x
-object
.x
;
604 return i
; // not found
607 wxLayoutObjectList::iterator
608 wxLayoutList::FindCurrentObject(CoordType
*offset
)
610 wxLayoutObjectList::iterator obj
= end();
612 obj
= FindObjectCursor(&m_CursorPosition
, offset
);
613 if(obj
== end()) // not ideal yet
616 if(obj
!= end()) // tail really exists
617 *offset
= (*obj
)->CountPositions(); // at the end of it
623 wxLayoutList::MoveCursor(int dx
, int dy
)
625 CoordType offs
, lineLength
;
626 wxLayoutObjectList::iterator i
;
629 if(dy
> 0 && m_CursorPosition
.y
< m_MaxLine
)
630 m_CursorPosition
.y
+= dy
;
631 else if(dy
< 0 && m_CursorPosition
.y
> 0)
632 m_CursorPosition
.y
+= dy
; // dy is negative
633 if(m_CursorPosition
.y
< 0)
634 m_CursorPosition
.y
= 0;
635 else if (m_CursorPosition
.y
> m_MaxLine
)
636 m_CursorPosition
.y
= m_MaxLine
;
640 i
= FindCurrentObject(&offs
);
641 lineLength
= GetLineLength(i
);
642 if(m_CursorPosition
.x
< lineLength
)
644 m_CursorPosition
.x
++;
650 if(m_CursorPosition
.y
< m_MaxLine
)
652 m_CursorPosition
.y
++;
653 m_CursorPosition
.x
= 0;
657 break; // cannot move there
662 if(m_CursorPosition
.x
> 0)
664 m_CursorPosition
.x
--;
669 if(m_CursorPosition
.y
> 0)
671 m_CursorPosition
.y
--;
672 m_CursorPosition
.x
= 0;
673 i
= FindCurrentObject(&offs
);
674 lineLength
= GetLineLength(i
);
675 m_CursorPosition
.x
= lineLength
;
680 break; // cannot move left any more
684 i
= FindCurrentObject(&offs
);
685 lineLength
= GetLineLength(i
);
686 if(m_CursorPosition
.x
> lineLength
)
687 m_CursorPosition
.x
= lineLength
;
689 #ifdef WXLAYOUT_DEBUG
690 i
= FindCurrentObject(&offs
);
692 << m_CursorPosition
.x
<< ','
693 << m_CursorPosition
.y
;
697 cerr
<< "<<no object found>>" << endl
;
698 return; // FIXME we should set cursor position to maximum allowed
701 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
703 cerr
<< " \"" << ((wxLayoutObjectText
*)(*i
))->GetText() << "\", offs: "
707 cerr
<< ' ' << _t
[(*i
)->GetType()] << endl
;
712 wxLayoutList::Delete(CoordType count
)
722 wxLayoutObjectList::iterator i
;
726 i
= FindCurrentObject(&offs
);
727 startover
: // ugly, but easiest way to do it
729 return; // we cannot delete anything more
731 /* Here we need to treat linebreaks differently.
732 If offs==0 we are before the linebreak, otherwise behind. */
733 if((*i
)->GetType() == WXLO_TYPE_LINEBREAK
)
740 continue; // we're done
742 else // delete the object behind the linebreak
744 i
++; // we increment and continue as normal
749 else if((*i
)->GetType() == WXLO_TYPE_TEXT
)
751 wxLayoutObjectText
*tobj
= (wxLayoutObjectText
*)*i
;
752 CoordType len
= tobj
->CountPositions();
753 // If we find the end of a text object, this means that we
754 // have to delete from the object following it.
761 else if(len
<= count
) // delete this object
770 tobj
->GetText().erase(offs
,len
);
771 return; // we are done
774 else // all other objects: delete the object
776 CoordType len
= (*i
)->CountPositions();
777 erase(i
); // after this, i is the iterator for the following object
778 count
= count
> len
? count
-= len
: 0;
781 while(count
&& i
!= end());
785 wxLayoutList::Insert(wxLayoutObjectBase
*obj
)
789 wxLayoutObjectList::iterator i
= FindCurrentObject(&offs
);
791 WXL_TRACE(Insert(obj
));
797 // do we have to split a text object?
798 else if((*i
)->GetType() == WXLO_TYPE_TEXT
&& offs
!= (*i
)->CountPositions())
800 wxLayoutObjectText
*tobj
= (wxLayoutObjectText
*) *i
;
801 #ifdef WXLAYOUT_DEBUG
802 cerr
<< "text: '" << tobj
->GetText() << "'" << endl
;
805 String left
= tobj
->GetText().substr(0,offs
); // get part before cursor
807 tobj
->GetText() = tobj
->GetText().substr(offs
,(*i
)->CountPositions()-offs
); // keeps the right half
808 WXL_VAR(tobj
->GetText());
810 insert(i
,new wxLayoutObjectText(left
)); // inserts before
814 // all other cases, append after object:
815 wxLayoutObjectList::iterator j
= i
; // we want to apend after this object
823 m_CursorPosition
.x
+= obj
->CountPositions();
824 if(obj
->GetType() == WXLO_TYPE_LINEBREAK
)
829 wxLayoutList::Insert(String
const &text
)
831 wxLayoutObjectText
*tobj
= NULL
;
832 WXL_TRACE(Insert(text
));
838 wxLayoutObjectList::iterator i
= FindCurrentObject(&offs
);
840 if(i
!= end() && (*i
)->GetType() == WXLO_TYPE_TEXT
)
841 { // insert into an existing text object:
842 WXL_TRACE(inserting into existing object
);
843 tobj
= (wxLayoutObjectText
*)*i
;
845 tobj
->GetText().insert(offs
,text
);
847 else // check whether the previous object is text:
849 wxLayoutObjectList::iterator j
= i
;
851 WXL_TRACE(checking previous object
);
852 if(0 && j
!= end() && (*j
)->GetType() == WXLO_TYPE_TEXT
)
854 tobj
= (wxLayoutObjectText
*)*i
;
856 tobj
->GetText()+=text
;
858 else // insert a new text object
860 WXL_TRACE(creating
new object
);
861 Insert(new wxLayoutObjectText(text
)); //FIXME not too optimal, slow
862 return; // position gets incremented in Insert(obj)
865 m_CursorPosition
.x
+= strlen(text
.c_str());
869 wxLayoutList::GetLineLength(wxLayoutObjectList::iterator i
)
876 // search backwards for beginning of line:
877 while(i
!= begin() && (*i
)->GetType() != WXLO_TYPE_LINEBREAK
)
879 if((*i
)->GetType() == WXLO_TYPE_LINEBREAK
)
881 // now we can start counting:
882 while(i
!= end() && (*i
)->GetType() != WXLO_TYPE_LINEBREAK
)
884 len
+= (*i
)->CountPositions();
891 wxLayoutList::Clear(int family
, int size
, int style
, int weight
,
892 int underline
, char const *fg
, char const *bg
)
894 wxLayoutObjectList::iterator i
= begin();
896 while(i
!= end()) // == while valid
901 m_FontUnderline
= false;
902 m_FontFamily
= family
;
904 m_FontWeight
= weight
;
905 m_ColourFG
= wxTheColourDatabase
->FindColour(fg
);
906 m_ColourBG
= wxTheColourDatabase
->FindColour(bg
);
908 if(! m_ColourFG
) m_ColourFG
= wxBLACK
;
909 if(! m_ColourBG
) m_ColourBG
= wxWHITE
;
911 m_Position
= wxPoint(0,0);
912 m_CursorPosition
= wxPoint(0,0);
914 m_LineHeight
= (BASELINESTRETCH
*m_FontPtSize
)/10;
915 m_MaxX
= 0; m_MaxY
= 0;
919 delete m_DefaultSetting
;
920 m_DefaultSetting
= new
921 wxLayoutObjectCmd(m_FontPtSize
,m_FontFamily
,m_FontStyle
,
922 m_FontWeight
,m_FontUnderline
,
923 m_ColourFG
, m_ColourBG
);