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"
29 #include <wx/postscrp.h>
32 #define BASELINESTRETCH 12
34 #define VAR(x) cerr << #x"=" << x << endl;
35 #define DBG_POINT(p) cerr << #p << ": " << p.x << ',' << p.y << endl
36 #define TRACE(f) cerr << #f":" << endl;
39 static const char *_t
[] = { "invalid", "text", "cmd", "icon",
43 wxLayoutObjectBase::Debug(void)
46 cerr
<< _t
[GetType()] << ": size=" << GetSize(&bl
).x
<< ","
47 << GetSize(&bl
).y
<< " bl=" << bl
;
51 //-------------------------- wxLayoutObjectText
53 wxLayoutObjectText::wxLayoutObjectText(const String
&txt
)
62 wxLayoutObjectText::GetSize(CoordType
*baseLine
) const
64 if(baseLine
) *baseLine
= m_BaseLine
;
65 return wxPoint(m_Width
, m_Height
);
69 wxLayoutObjectText::Draw(wxDC
&dc
, wxPoint position
, CoordType baseLine
,
73 dc
.GetTextExtent(Str(m_Text
),&m_Width
, &m_Height
, &descent
);
74 //FIXME: wxGTK does not set descent to a descent value yet.
76 descent
= (2*m_Height
)/10; // crude fix
77 m_BaseLine
= m_Height
- descent
;
78 position
.y
+= baseLine
-m_BaseLine
;
80 dc
.DrawText(Str(m_Text
),position
.x
,position
.y
);
81 # ifdef WXLAYOUT_DEBUG
82 // dc.DrawRectangle(position.x, position.y, m_Width, m_Height);
88 wxLayoutObjectText::Debug(void)
90 wxLayoutObjectBase::Debug();
91 cerr
<< " `" << m_Text
<< '\'';
95 //-------------------------- wxLayoutObjectIcon
97 wxLayoutObjectIcon::wxLayoutObjectIcon(wxIcon
*icon
)
103 wxLayoutObjectIcon::Draw(wxDC
&dc
, wxPoint position
, CoordType baseLine
,
106 position
.y
+= baseLine
- m_Icon
->GetHeight();
108 dc
.DrawIcon(m_Icon
,position
.x
,position
.y
);
112 wxLayoutObjectIcon::GetSize(CoordType
*baseLine
) const
115 *baseLine
= m_Icon
->GetHeight();
116 return wxPoint(m_Icon
->GetWidth(), m_Icon
->GetHeight());
119 //-------------------------- wxLayoutObjectCmd
122 wxLayoutObjectCmd::wxLayoutObjectCmd(int size
, int family
, int style
, int
123 weight
, bool underline
,
124 wxColour
const *fg
, wxColour
const *bg
)
127 m_font
= new wxFont(size
,family
,style
,weight
,underline
);
132 wxLayoutObjectCmd::~wxLayoutObjectCmd()
138 wxLayoutObjectCmd::GetStyle(void) const
140 wxLayoutStyleInfo
*si
= new wxLayoutStyleInfo();
143 si
->size
= m_font
->GetPointSize();
144 si
->family
= m_font
->GetFamily();
145 si
->style
= m_font
->GetStyle();
146 si
->underline
= m_font
->GetUnderlined();
147 si
->weight
= m_font
->GetWeight();
149 si
->fg_red
= m_ColourFG
->Red();
150 si
->fg_green
= m_ColourFG
->Green();
151 si
->fg_blue
= m_ColourFG
->Blue();
152 si
->bg_red
= m_ColourBG
->Red();
153 si
->bg_green
= m_ColourBG
->Green();
154 si
->bg_blue
= m_ColourBG
->Blue();
160 wxLayoutObjectCmd::Draw(wxDC
&dc
, wxPoint position
, CoordType lineHeight
,
164 // this get called even when draw==false, so that recalculation
165 // uses right font sizes
168 dc
.SetTextForeground(*m_ColourFG
);
170 dc
.SetTextBackground(*m_ColourBG
);
173 //-------------------------- wxwxLayoutList
175 wxLayoutList::wxLayoutList()
177 m_DefaultSetting
= NULL
;
181 wxLayoutList::~wxLayoutList()
184 delete m_DefaultSetting
;
189 wxLayoutList::LineBreak(void)
191 Insert(new wxLayoutObjectLineBreak
);
192 m_CursorPosition
.x
= 0; m_CursorPosition
.y
++;
196 wxLayoutList::SetFont(int family
, int size
, int style
, int weight
,
197 int underline
, wxColour
const *fg
,
200 if(family
!= -1) m_FontFamily
= family
;
201 if(size
!= -1) m_FontPtSize
= size
;
202 if(style
!= -1) m_FontStyle
= style
;
203 if(weight
!= -1) m_FontWeight
= weight
;
204 if(underline
!= -1) m_FontUnderline
= underline
;
206 if(fg
!= NULL
) m_ColourFG
= fg
;
207 if(bg
!= NULL
) m_ColourBG
= bg
;
210 new wxLayoutObjectCmd(m_FontPtSize
,m_FontFamily
,m_FontStyle
,m_FontWeight
,m_FontUnderline
,
211 m_ColourFG
, m_ColourBG
));
215 wxLayoutList::SetFont(int family
, int size
, int style
, int weight
,
216 int underline
, char const *fg
, char const *bg
)
223 cfg
= wxTheColourDatabase
->FindColour(fg
);
225 cbg
= wxTheColourDatabase
->FindColour(bg
);
227 SetFont(family
,size
,style
,weight
,underline
,cfg
,cbg
);
231 /// for access by wxLayoutWindow:
233 wxLayoutList::GetSize(CoordType
*max_x
, CoordType
*max_y
,
234 CoordType
*lineHeight
)
236 wxASSERT(max_x
); wxASSERT(max_y
); wxASSERT(lineHeight
);
239 *lineHeight
= m_LineHeight
;
243 wxLayoutList::Draw(wxDC
&dc
, bool findObject
, wxPoint
const &findCoords
)
245 wxLayoutObjectList::iterator i
;
247 // in case we need to look for an object
248 wxLayoutObjectBase
*foundObject
= NULL
;
250 // first object in current line
251 wxLayoutObjectList::iterator headOfLine
;
253 // do we need to recalculate current line?
254 bool recalculate
= false;
256 // do we calculate or draw? Either true or false.
258 // drawing parameters:
259 wxPoint position
= wxPoint(0,0);
260 wxPoint position_HeadOfLine
;
261 CoordType baseLine
= m_FontPtSize
;
262 CoordType baseLineSkip
= (BASELINESTRETCH
* baseLine
)/10;
264 // we trace the objects' cursor positions so we can draw the cursor
265 wxPoint cursor
= wxPoint(0,0);
266 // the cursor position inside the object
267 CoordType cursorOffset
= 0;
268 // object under cursor
269 wxLayoutObjectList::iterator cursorObject
= FindCurrentObject(&cursorOffset
);
271 // queried from each object:
272 wxPoint size
= wxPoint(0,0);
273 CoordType objBaseLine
= baseLine
;
274 wxLayoutObjectType type
;
277 wxLayoutObjectText
*tobj
= NULL
;
280 // this is needed for printing to a printer:
281 // only interesting for printer/PS output
282 int pageWidth
, pageHeight
; //GetSize() still needs int at the moment
285 int top
, bottom
, left
, right
;
290 dc
.IsKindOf(CLASSINFO(wxPrinterDC
)) ||
292 dc
.IsKindOf(CLASSINFO(wxPostScriptDC
)))
294 VAR(wxThePrintSetupData
);
296 dc
.GetSize(&pageWidth
, &pageHeight
);
297 dc
.StartDoc(_("Printing..."));
299 margins
.top
= (1*pageHeight
)/10; // 10%
300 margins
.bottom
= (9*pageHeight
)/10; // 90%
301 margins
.left
= (1*pageWidth
)/10;
302 margins
.right
= (9*pageWidth
)/10;
306 margins
.top
= 0; margins
.left
= 0;
310 position
.y
= margins
.right
;
311 position
.x
= margins
.left
;
313 VAR(findObject
); VAR(findCoords
.x
); VAR(findCoords
.y
);
314 // if the cursorobject is a cmd, we need to find the first
316 while(cursorObject
!= end()
317 && (*cursorObject
)->GetType() == WXLO_TYPE_CMD
)
320 headOfLine
= begin();
321 position_HeadOfLine
= position
;
323 // setting up the default:
324 dc
.SetTextForeground( *wxBLACK
);
325 dc
.SetTextBackground( *wxWHITE
);
326 dc
.SetBackgroundMode( wxSOLID
); // to enable setting of text background
327 dc
.SetFont( *wxNORMAL_FONT
);
330 //FIXME: who frees the brush, how long does it need to exist?
332 m_DefaultSetting
->Draw(dc
,wxPoint(0,0),0,true);
334 // we calculate everything for drawing a line, then rewind to the
335 // begin of line and actually draw it
343 type
= (*i
)->GetType();
345 // to initialise sizes of objects, we need to call Draw
346 (*i
)->Draw(dc
, position
, baseLine
, draw
);
348 // update coordinates for next object:
349 size
= (*i
)->GetSize(&objBaseLine
);
350 if(findObject
&& draw
) // we need to look for an object
352 if(findCoords
.y
>= position
.y
353 && findCoords
.y
<= position
.y
+size
.y
354 && findCoords
.x
>= position
.x
355 && findCoords
.x
<= position
.x
+size
.x
)
358 findObject
= false; // speeds things up
362 if(m_Editable
&& draw
&& i
== cursorObject
)
364 if(type
== WXLO_TYPE_TEXT
) // special treatment
366 long descent
= 0l; long width
, height
;
367 tobj
= (wxLayoutObjectText
*)*i
;
368 String str
= tobj
->GetText();
369 VAR(m_CursorPosition
.x
); VAR(cursor
.x
);
370 str
= str
.substr(0, cursorOffset
);
372 dc
.GetTextExtent(Str(str
), &width
,&height
, &descent
);
374 VAR(width
); VAR(descent
);
375 dc
.DrawLine(position
.x
+width
,
376 position
.y
+(baseLineSkip
-height
),
377 position
.x
+width
, position
.y
+baseLineSkip
);
381 if(type
== WXLO_TYPE_LINEBREAK
)
382 dc
.DrawLine(0, position
.y
+baseLineSkip
, 0, position
.y
+2*baseLineSkip
);
388 dc
.DrawLine(position
.x
, position
.y
, position
.x
, position
.y
+baseLineSkip
);
390 dc
.DrawLine(position
.x
, position
.y
, position
.x
, position
.y
+size
.y
);
393 dc
.DrawRectangle(position
.x
, position
.y
, size
.x
, size
.y
);
398 // calculate next object's position:
399 position
.x
+= size
.x
;
401 // do we need to increase the line's height?
402 if(size
.y
> baseLineSkip
)
404 baseLineSkip
= size
.y
;
407 if(objBaseLine
> baseLine
)
409 baseLine
= objBaseLine
;
413 // now check whether we have finished handling this line:
414 if(type
== WXLO_TYPE_LINEBREAK
|| i
== tail())
416 if(recalculate
) // do this line again
418 position
.x
= position_HeadOfLine
.x
;
423 if(! draw
) // finished calculating sizes
425 // if the this line needs to go onto a new page, we need
426 // to change pages before drawing it:
427 if(margins
.bottom
!= -1 && position
.y
> margins
.bottom
)
430 position_HeadOfLine
.y
= margins
.top
;
433 // do this line again, this time drawing it
434 position
= position_HeadOfLine
;
439 else // we have drawn a line, so continue calculating next one
443 if(position
.x
+size
.x
> m_MaxX
)
444 m_MaxX
= position
.x
+size
.x
;
445 // is it a linebreak?
446 if(type
== WXLO_TYPE_LINEBREAK
|| i
== tail())
448 position
.x
= margins
.left
;
449 position
.y
+= baseLineSkip
;
450 baseLine
= m_FontPtSize
;
451 objBaseLine
= baseLine
; // not all objects set it
452 baseLineSkip
= (BASELINESTRETCH
* baseLine
)/10;
455 position_HeadOfLine
= position
;
464 #ifdef WXLAYOUT_DEBUG
466 wxLayoutList::Debug(void)
469 wxLayoutObjectList::iterator i
;
472 "------------------------debug start-------------------------" << endl
;
473 for(i
= begin(); i
!= end(); i
++)
479 "-----------------------debug end----------------------------"
481 // show current object:
483 << m_CursorPosition
.x
<< ','
484 << m_CursorPosition
.y
;
486 i
= FindCurrentObject(&offs
);
487 cerr
<< " line length: " << GetLineLength(i
) << " ";
490 cerr
<< "<<no object found>>" << endl
;
491 return; // FIXME we should set cursor position to maximum allowed
494 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
496 cerr
<< " \"" << ((wxLayoutObjectText
*)(*i
))->GetText() << "\", offs: "
500 cerr
<< ' ' << _t
[(*i
)->GetType()] << endl
;
505 /******************** editing stuff ********************/
507 wxLayoutObjectList::iterator
508 wxLayoutList::FindObjectCursor(wxPoint
const &cpos
, CoordType
*offset
)
510 wxPoint cursor
= wxPoint(0,0); // runs along the objects
512 wxLayoutObjectList::iterator i
;
514 #ifdef WXLAYOUT_DEBUG
515 cerr
<< "Looking for object at " << cpos
.x
<< ',' << cpos
.y
<<
518 for(i
= begin(); i
!= end() && cursor
.y
<= cpos
.y
; i
++)
521 if((*i
)->GetType() == WXLO_TYPE_LINEBREAK
)
523 if(cpos
.y
== cursor
.y
)
527 *offset
= (*i
)->CountPositions();
530 cursor
.x
= 0; cursor
.y
++;
533 cursor
.x
+= (width
= (*i
)->CountPositions());
534 if(cursor
.y
== cpos
.y
&& (cursor
.x
> cpos
.x
||
535 ((*i
)->GetType() != WXLO_TYPE_CMD
&& cursor
.x
== cpos
.x
))
539 *offset
= cpos
.x
-(cursor
.x
-width
); // 0==cursor before
541 #ifdef WXLAYOUT_DEBUG
542 cerr
<< " found object at " << cursor
.x
-width
<< ',' <<
543 cursor
.y
<< ", type:" << _t
[(*i
)->GetType()] <<endl
;
548 #ifdef WXLAYOUT_DEBUG
549 cerr
<< " not found" << endl
;
551 return end(); // not found
554 wxLayoutObjectList::iterator
555 wxLayoutList::FindCurrentObject(CoordType
*offset
)
557 wxLayoutObjectList::iterator obj
= end();
559 obj
= FindObjectCursor(m_CursorPosition
, offset
);
560 if(obj
== end()) // not ideal yet
563 if(obj
!= end()) // tail really exists
564 *offset
= (*obj
)->CountPositions(); // at the end of it
570 wxLayoutList::MoveCursor(int dx
, int dy
)
572 CoordType offs
, lineLength
;
573 wxLayoutObjectList::iterator i
;
576 if(dy
> 0 && m_CursorPosition
.y
< m_MaxLine
)
577 m_CursorPosition
.y
+= dy
;
578 else if(dy
< 0 && m_CursorPosition
.y
> 0)
579 m_CursorPosition
.y
+= dy
; // dy is negative
580 if(m_CursorPosition
.y
< 0)
581 m_CursorPosition
.y
= 0;
582 else if (m_CursorPosition
.y
> m_MaxLine
)
583 m_CursorPosition
.y
= m_MaxLine
;
587 i
= FindCurrentObject(&offs
);
588 lineLength
= GetLineLength(i
);
589 if(m_CursorPosition
.x
< lineLength
)
591 m_CursorPosition
.x
++;
597 if(m_CursorPosition
.y
< m_MaxLine
)
599 m_CursorPosition
.y
++;
600 m_CursorPosition
.x
= 0;
604 break; // cannot move there
609 if(m_CursorPosition
.x
> 0)
611 m_CursorPosition
.x
--;
616 if(m_CursorPosition
.y
> 0)
618 m_CursorPosition
.y
--;
619 m_CursorPosition
.x
= 0;
620 i
= FindCurrentObject(&offs
);
621 lineLength
= GetLineLength(i
);
622 m_CursorPosition
.x
= lineLength
;
627 break; // cannot move left any more
631 i
= FindCurrentObject(&offs
);
632 lineLength
= GetLineLength(i
);
633 if(m_CursorPosition
.x
> lineLength
)
634 m_CursorPosition
.x
= lineLength
;
636 #ifdef WXLAYOUT_DEBUG
637 i
= FindCurrentObject(&offs
);
639 << m_CursorPosition
.x
<< ','
640 << m_CursorPosition
.y
;
644 cerr
<< "<<no object found>>" << endl
;
645 return; // FIXME we should set cursor position to maximum allowed
648 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
650 cerr
<< " \"" << ((wxLayoutObjectText
*)(*i
))->GetText() << "\", offs: "
654 cerr
<< ' ' << _t
[(*i
)->GetType()] << endl
;
659 wxLayoutList::Delete(CoordType count
)
669 wxLayoutObjectList::iterator i
;
673 i
= FindCurrentObject(&offs
);
676 #ifdef WXLAYOUT_DEBUG
677 cerr
<< "trying to delete: " << _t
[(*i
)->GetType()] << endl
;
679 if((*i
)->GetType() == WXLO_TYPE_LINEBREAK
)
681 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
683 wxLayoutObjectText
*tobj
= (wxLayoutObjectText
*)*i
;
684 len
= tobj
->CountPositions();
685 // If we find the end of a text object, this means that we
686 // have to delete from the object following it.
690 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
692 offs
= 0; // delete from begin of next string
693 tobj
= (wxLayoutObjectText
*)*i
;
694 len
= tobj
->CountPositions();
702 if(len
<= count
) // delete this object
711 tobj
->GetText().erase(offs
,len
);
712 return; // we are done
715 else // delete the object
717 len
= (*i
)->CountPositions();
718 erase(i
); // after this, i is the iterator for the following object
725 while(count
&& i
!= end());
729 wxLayoutList::Insert(wxLayoutObjectBase
*obj
)
733 wxLayoutObjectList::iterator i
= FindCurrentObject(&offs
);
741 // do we have to split a text object?
742 if((*i
)->GetType() == WXLO_TYPE_TEXT
&& offs
!= 0 && offs
!= (*i
)->CountPositions())
744 wxLayoutObjectText
*tobj
= (wxLayoutObjectText
*) *i
;
745 #ifdef WXLAYOUT_DEBUG
746 cerr
<< "text: '" << tobj
->GetText() << "'" << endl
;
749 String left
= tobj
->GetText().substr(0,offs
); // get part before cursor
751 tobj
->GetText() = tobj
->GetText().substr(offs
,(*i
)->CountPositions()-offs
); // keeps the right half
752 VAR(tobj
->GetText());
754 insert(i
,new wxLayoutObjectText(left
)); // inserts before
758 wxLayoutObjectList::iterator j
= i
; // we want to apend after this object
766 m_CursorPosition
.x
+= obj
->CountPositions();
767 if(obj
->GetType() == WXLO_TYPE_LINEBREAK
)
772 wxLayoutList::Insert(String
const &text
)
774 wxLayoutObjectText
*tobj
= NULL
;
781 wxLayoutObjectList::iterator i
= FindCurrentObject(&offs
);
783 if(i
!= end() && (*i
)->GetType() == WXLO_TYPE_TEXT
)
784 { // insert into an existing text object:
785 TRACE(inserting into existing object
);
786 tobj
= (wxLayoutObjectText
*)*i
;
788 tobj
->GetText().insert(offs
,text
);
790 else // check whether the previous object is text:
792 wxLayoutObjectList::iterator j
= i
;
794 TRACE(checking previous object
);
795 if(0 && j
!= end() && (*j
)->GetType() == WXLO_TYPE_TEXT
)
797 tobj
= (wxLayoutObjectText
*)*i
;
799 tobj
->GetText()+=text
;
801 else // insert a new text object
803 TRACE(creating
new object
);
804 Insert(new wxLayoutObjectText(text
)); //FIXME not too optimal, slow
805 return; // position gets incremented in Insert(obj)
808 m_CursorPosition
.x
+= strlen(text
.c_str());
812 wxLayoutList::GetLineLength(wxLayoutObjectList::iterator i
)
819 // search backwards for beginning of line:
820 while(i
!= begin() && (*i
)->GetType() != WXLO_TYPE_LINEBREAK
)
822 if((*i
)->GetType() == WXLO_TYPE_LINEBREAK
)
824 // now we can start counting:
825 while(i
!= end() && (*i
)->GetType() != WXLO_TYPE_LINEBREAK
)
827 len
+= (*i
)->CountPositions();
834 wxLayoutList::Clear(int family
, int size
, int style
, int weight
,
835 int underline
, char const *fg
, char const *bg
)
837 wxLayoutObjectList::iterator i
= begin();
839 while(i
!= end()) // == while valid
844 m_FontUnderline
= false;
845 m_FontFamily
= family
;
847 m_FontWeight
= weight
;
848 m_ColourFG
= wxTheColourDatabase
->FindColour(fg
);
849 m_ColourBG
= wxTheColourDatabase
->FindColour(bg
);
851 m_Position
= wxPoint(0,0);
852 m_CursorPosition
= wxPoint(0,0);
854 m_LineHeight
= (BASELINESTRETCH
*m_FontPtSize
)/10;
855 m_MaxX
= 0; m_MaxY
= 0;
859 delete m_DefaultSetting
;
860 m_DefaultSetting
= new
861 wxLayoutObjectCmd(m_FontPtSize
,m_FontFamily
,m_FontStyle
,
862 m_FontWeight
,m_FontUnderline
,
863 m_ColourFG
, m_ColourBG
);