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"
28 #define BASELINESTRETCH 12
30 #define VAR(x) cerr << #x"=" << x << endl;
31 #define DBG_POINT(p) cerr << #p << ": " << p.x << ',' << p.y << endl
32 #define TRACE(f) cerr << #f":" << endl;
35 static const char *_t
[] = { "invalid", "text", "cmd", "icon",
39 wxLayoutObjectBase::Debug(void)
42 cerr
<< _t
[GetType()] << ": size=" << GetSize(&bl
).x
<< ","
43 << GetSize(&bl
).y
<< " bl=" << bl
;
47 //-------------------------- wxLayoutObjectText
49 wxLayoutObjectText::wxLayoutObjectText(const String
&txt
)
58 wxLayoutObjectText::GetSize(CoordType
*baseLine
) const
60 if(baseLine
) *baseLine
= m_BaseLine
;
61 return wxPoint(m_Width
, m_Height
);
65 wxLayoutObjectText::Draw(wxDC
&dc
, wxPoint position
, CoordType baseLine
,
69 dc
.GetTextExtent(Str(m_Text
),&m_Width
, &m_Height
, &descent
);
70 //FIXME: wxGTK does not set descent to a descent value yet.
72 descent
= (2*m_Height
)/10; // crude fix
73 m_BaseLine
= m_Height
- descent
;
74 position
.y
+= baseLine
-m_BaseLine
;
76 dc
.DrawText(Str(m_Text
),position
.x
,position
.y
);
77 # ifdef WXLAYOUT_DEBUG
78 // dc.DrawRectangle(position.x, position.y, m_Width, m_Height);
84 wxLayoutObjectText::Debug(void)
86 wxLayoutObjectBase::Debug();
87 cerr
<< " `" << m_Text
<< '\'';
91 //-------------------------- wxLayoutObjectIcon
93 wxLayoutObjectIcon::wxLayoutObjectIcon(wxIcon
*icon
)
99 wxLayoutObjectIcon::Draw(wxDC
&dc
, wxPoint position
, CoordType baseLine
,
102 position
.y
+= baseLine
- m_Icon
->GetHeight();
104 dc
.DrawIcon(m_Icon
,position
.x
,position
.y
);
108 wxLayoutObjectIcon::GetSize(CoordType
*baseLine
) const
111 *baseLine
= m_Icon
->GetHeight();
112 return wxPoint(m_Icon
->GetWidth(), m_Icon
->GetHeight());
115 //-------------------------- wxLayoutObjectCmd
118 wxLayoutObjectCmd::wxLayoutObjectCmd(int size
, int family
, int style
, int
119 weight
, bool underline
,
120 wxColour
const *fg
, wxColour
const *bg
)
123 m_font
= new wxFont(size
,family
,style
,weight
,underline
);
128 wxLayoutObjectCmd::~wxLayoutObjectCmd()
134 wxLayoutObjectCmd::GetStyle(void) const
136 wxLayoutStyleInfo
*si
= new wxLayoutStyleInfo();
139 si
->size
= m_font
->GetPointSize();
140 si
->family
= m_font
->GetFamily();
141 si
->style
= m_font
->GetStyle();
142 si
->underline
= m_font
->GetUnderlined();
143 si
->weight
= m_font
->GetWeight();
145 si
->fg_red
= m_ColourFG
->Red();
146 si
->fg_green
= m_ColourFG
->Green();
147 si
->fg_blue
= m_ColourFG
->Blue();
148 si
->bg_red
= m_ColourBG
->Red();
149 si
->bg_green
= m_ColourBG
->Green();
150 si
->bg_blue
= m_ColourBG
->Blue();
156 wxLayoutObjectCmd::Draw(wxDC
&dc
, wxPoint position
, CoordType lineHeight
,
160 // this get called even when draw==false, so that recalculation
161 // uses right font sizes
164 dc
.SetTextForeground(*m_ColourFG
);
166 dc
.SetTextBackground(*m_ColourBG
);
169 //-------------------------- wxwxLayoutList
171 wxLayoutList::wxLayoutList()
173 m_DefaultSetting
= NULL
;
177 wxLayoutList::~wxLayoutList()
180 delete m_DefaultSetting
;
185 wxLayoutList::LineBreak(void)
187 Insert(new wxLayoutObjectLineBreak
);
188 m_CursorPosition
.x
= 0; m_CursorPosition
.y
++;
192 wxLayoutList::SetFont(int family
, int size
, int style
, int weight
,
193 int underline
, wxColour
const *fg
,
196 if(family
!= -1) m_FontFamily
= family
;
197 if(size
!= -1) m_FontPtSize
= size
;
198 if(style
!= -1) m_FontStyle
= style
;
199 if(weight
!= -1) m_FontWeight
= weight
;
200 if(underline
!= -1) m_FontUnderline
= underline
;
202 if(fg
!= NULL
) m_ColourFG
= fg
;
203 if(bg
!= NULL
) m_ColourBG
= bg
;
206 new wxLayoutObjectCmd(m_FontPtSize
,m_FontFamily
,m_FontStyle
,m_FontWeight
,m_FontUnderline
,
207 m_ColourFG
, m_ColourBG
));
211 wxLayoutList::SetFont(int family
, int size
, int style
, int weight
,
212 int underline
, char const *fg
, char const *bg
)
219 cfg
= wxTheColourDatabase
->FindColour(fg
);
221 cbg
= wxTheColourDatabase
->FindColour(bg
);
223 SetFont(family
,size
,style
,weight
,underline
,cfg
,cbg
);
227 /// for access by wxLayoutWindow:
229 wxLayoutList::GetSize(CoordType
*max_x
, CoordType
*max_y
,
230 CoordType
*lineHeight
)
232 wxASSERT(max_x
); wxASSERT(max_y
); wxASSERT(lineHeight
);
235 *lineHeight
= m_LineHeight
;
239 wxLayoutList::Draw(wxDC
&dc
, bool findObject
, wxPoint
const &findCoords
)
241 wxLayoutObjectList::iterator i
;
243 // in case we need to look for an object
244 wxLayoutObjectBase
*foundObject
= NULL
;
246 // first object in current line
247 wxLayoutObjectList::iterator headOfLine
;
249 // do we need to recalculate current line?
250 bool recalculate
= false;
252 // do we calculate or draw? Either true or false.
254 // drawing parameters:
255 wxPoint position
= wxPoint(0,0);
256 wxPoint position_HeadOfLine
;
257 CoordType baseLine
= m_FontPtSize
;
258 CoordType baseLineSkip
= (BASELINESTRETCH
* baseLine
)/10;
260 // we trace the objects' cursor positions so we can draw the cursor
261 wxPoint cursor
= wxPoint(0,0);
262 // the cursor position inside the object
263 CoordType cursorOffset
= 0;
264 // object under cursor
265 wxLayoutObjectList::iterator cursorObject
= FindCurrentObject(&cursorOffset
);
267 // queried from each object:
268 wxPoint size
= wxPoint(0,0);
269 CoordType objBaseLine
= baseLine
;
270 wxLayoutObjectType type
;
273 wxLayoutObjectText
*tobj
= NULL
;
275 VAR(findObject
); VAR(findCoords
.x
); VAR(findCoords
.y
);
276 // if the cursorobject is a cmd, we need to find the first
278 while(cursorObject
!= end()
279 && (*cursorObject
)->GetType() == WXLO_TYPE_CMD
)
282 headOfLine
= begin();
283 position_HeadOfLine
= position
;
285 // setting up the default:
286 dc
.SetTextForeground( *wxBLACK
);
287 dc
.SetFont( *wxNORMAL_FONT
);
290 m_DefaultSetting
->Draw(dc
,wxPoint(0,0),0,true);
292 // we calculate everything for drawing a line, then rewind to the
293 // begin of line and actually draw it
301 type
= (*i
)->GetType();
303 // to initialise sizes of objects, we need to call Draw
304 (*i
)->Draw(dc
, position
, baseLine
, draw
);
306 // update coordinates for next object:
307 size
= (*i
)->GetSize(&objBaseLine
);
308 if(findObject
&& draw
) // we need to look for an object
310 if(findCoords
.y
>= position
.y
311 && findCoords
.y
<= position
.y
+size
.y
312 && findCoords
.x
>= position
.x
313 && findCoords
.x
<= position
.x
+size
.x
)
316 findObject
= false; // speeds things up
320 if(m_Editable
&& draw
&& i
== cursorObject
)
322 if(type
== WXLO_TYPE_TEXT
) // special treatment
324 long descent
= 0l; long width
, height
;
325 tobj
= (wxLayoutObjectText
*)*i
;
326 String str
= tobj
->GetText();
327 VAR(m_CursorPosition
.x
); VAR(cursor
.x
);
328 str
= str
.substr(0, cursorOffset
);
330 dc
.GetTextExtent(Str(str
), &width
,&height
, &descent
);
332 VAR(width
); VAR(descent
);
333 dc
.DrawLine(position
.x
+width
,
334 position
.y
+(baseLineSkip
-height
),
335 position
.x
+width
, position
.y
+baseLineSkip
);
339 if(type
== WXLO_TYPE_LINEBREAK
)
340 dc
.DrawLine(0, position
.y
+baseLineSkip
, 0, position
.y
+2*baseLineSkip
);
346 dc
.DrawLine(position
.x
, position
.y
, position
.x
, position
.y
+baseLineSkip
);
348 dc
.DrawLine(position
.x
, position
.y
, position
.x
, position
.y
+size
.y
);
351 dc
.DrawRectangle(position
.x
, position
.y
, size
.x
, size
.y
);
356 // calculate next object's position:
357 position
.x
+= size
.x
;
359 // do we need to increase the line's height?
360 if(size
.y
> baseLineSkip
)
362 baseLineSkip
= size
.y
;
365 if(objBaseLine
> baseLine
)
367 baseLine
= objBaseLine
;
371 // now check whether we have finished handling this line:
372 if(type
== WXLO_TYPE_LINEBREAK
|| i
== tail())
374 if(recalculate
) // do this line again
376 position
.x
= position_HeadOfLine
.x
;
381 if(! draw
) // finished calculating sizes
382 { // do this line again, this time drawing it
383 position
= position_HeadOfLine
;
388 else // we have drawn a line, so continue calculating next one
392 if(position
.x
+size
.x
> m_MaxX
)
393 m_MaxX
= position
.x
+size
.x
;
394 // is it a linebreak?
395 if(type
== WXLO_TYPE_LINEBREAK
|| i
== tail())
398 position
.y
+= baseLineSkip
;
399 baseLine
= m_FontPtSize
;
400 objBaseLine
= baseLine
; // not all objects set it
401 baseLineSkip
= (BASELINESTRETCH
* baseLine
)/10;
404 position_HeadOfLine
= position
;
412 #ifdef WXLAYOUT_DEBUG
414 wxLayoutList::Debug(void)
417 wxLayoutObjectList::iterator i
;
420 "------------------------debug start-------------------------" << endl
;
421 for(i
= begin(); i
!= end(); i
++)
427 "-----------------------debug end----------------------------"
429 // show current object:
431 << m_CursorPosition
.x
<< ','
432 << m_CursorPosition
.y
;
434 i
= FindCurrentObject(&offs
);
435 cerr
<< " line length: " << GetLineLength(i
) << " ";
438 cerr
<< "<<no object found>>" << endl
;
439 return; // FIXME we should set cursor position to maximum allowed
442 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
444 cerr
<< " \"" << ((wxLayoutObjectText
*)(*i
))->GetText() << "\", offs: "
448 cerr
<< ' ' << _t
[(*i
)->GetType()] << endl
;
453 /******************** editing stuff ********************/
455 wxLayoutObjectList::iterator
456 wxLayoutList::FindObjectCursor(wxPoint
const &cpos
, CoordType
*offset
)
458 wxPoint cursor
= wxPoint(0,0); // runs along the objects
460 wxLayoutObjectList::iterator i
;
462 #ifdef WXLAYOUT_DEBUG
463 cerr
<< "Looking for object at " << cpos
.x
<< ',' << cpos
.y
<<
466 for(i
= begin(); i
!= end() && cursor
.y
<= cpos
.y
; i
++)
469 if((*i
)->GetType() == WXLO_TYPE_LINEBREAK
)
471 if(cpos
.y
== cursor
.y
)
475 *offset
= (*i
)->CountPositions();
478 cursor
.x
= 0; cursor
.y
++;
481 cursor
.x
+= (width
= (*i
)->CountPositions());
482 if(cursor
.y
== cpos
.y
&& (cursor
.x
> cpos
.x
||
483 ((*i
)->GetType() != WXLO_TYPE_CMD
&& cursor
.x
== cpos
.x
))
487 *offset
= cpos
.x
-(cursor
.x
-width
); // 0==cursor before
489 #ifdef WXLAYOUT_DEBUG
490 cerr
<< " found object at " << cursor
.x
-width
<< ',' <<
491 cursor
.y
<< ", type:" << _t
[(*i
)->GetType()] <<endl
;
496 #ifdef WXLAYOUT_DEBUG
497 cerr
<< " not found" << endl
;
499 return end(); // not found
502 wxLayoutObjectList::iterator
503 wxLayoutList::FindCurrentObject(CoordType
*offset
)
505 wxLayoutObjectList::iterator obj
= end();
507 obj
= FindObjectCursor(m_CursorPosition
, offset
);
508 if(obj
== end()) // not ideal yet
511 if(obj
!= end()) // tail really exists
512 *offset
= (*obj
)->CountPositions(); // at the end of it
518 wxLayoutList::MoveCursor(int dx
, int dy
)
520 CoordType offs
, lineLength
;
521 wxLayoutObjectList::iterator i
;
524 if(dy
> 0 && m_CursorPosition
.y
< m_MaxLine
)
525 m_CursorPosition
.y
+= dy
;
526 else if(dy
< 0 && m_CursorPosition
.y
> 0)
527 m_CursorPosition
.y
+= dy
; // dy is negative
528 if(m_CursorPosition
.y
< 0)
529 m_CursorPosition
.y
= 0;
530 else if (m_CursorPosition
.y
> m_MaxLine
)
531 m_CursorPosition
.y
= m_MaxLine
;
535 i
= FindCurrentObject(&offs
);
536 lineLength
= GetLineLength(i
);
537 if(m_CursorPosition
.x
< lineLength
)
539 m_CursorPosition
.x
++;
545 if(m_CursorPosition
.y
< m_MaxLine
)
547 m_CursorPosition
.y
++;
548 m_CursorPosition
.x
= 0;
552 break; // cannot move there
557 if(m_CursorPosition
.x
> 0)
559 m_CursorPosition
.x
--;
564 if(m_CursorPosition
.y
> 0)
566 m_CursorPosition
.y
--;
567 m_CursorPosition
.x
= 0;
568 i
= FindCurrentObject(&offs
);
569 lineLength
= GetLineLength(i
);
570 m_CursorPosition
.x
= lineLength
;
575 break; // cannot move left any more
579 i
= FindCurrentObject(&offs
);
580 lineLength
= GetLineLength(i
);
581 if(m_CursorPosition
.x
> lineLength
)
582 m_CursorPosition
.x
= lineLength
;
584 #ifdef WXLAYOUT_DEBUG
585 i
= FindCurrentObject(&offs
);
587 << m_CursorPosition
.x
<< ','
588 << m_CursorPosition
.y
;
592 cerr
<< "<<no object found>>" << endl
;
593 return; // FIXME we should set cursor position to maximum allowed
596 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
598 cerr
<< " \"" << ((wxLayoutObjectText
*)(*i
))->GetText() << "\", offs: "
602 cerr
<< ' ' << _t
[(*i
)->GetType()] << endl
;
607 wxLayoutList::Delete(CoordType count
)
617 wxLayoutObjectList::iterator i
;
621 i
= FindCurrentObject(&offs
);
624 #ifdef WXLAYOUT_DEBUG
625 cerr
<< "trying to delete: " << _t
[(*i
)->GetType()] << endl
;
627 if((*i
)->GetType() == WXLO_TYPE_LINEBREAK
)
629 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
631 wxLayoutObjectText
*tobj
= (wxLayoutObjectText
*)*i
;
632 len
= tobj
->CountPositions();
633 // If we find the end of a text object, this means that we
634 // have to delete from the object following it.
638 if((*i
)->GetType() == WXLO_TYPE_TEXT
)
640 offs
= 0; // delete from begin of next string
641 tobj
= (wxLayoutObjectText
*)*i
;
642 len
= tobj
->CountPositions();
650 if(len
<= count
) // delete this object
659 tobj
->GetText().erase(offs
,len
);
660 return; // we are done
663 else // delete the object
665 len
= (*i
)->CountPositions();
666 erase(i
); // after this, i is the iterator for the following object
673 while(count
&& i
!= end());
677 wxLayoutList::Insert(wxLayoutObjectBase
*obj
)
681 wxLayoutObjectList::iterator i
= FindCurrentObject(&offs
);
689 // do we have to split a text object?
690 if((*i
)->GetType() == WXLO_TYPE_TEXT
&& offs
!= 0 && offs
!= (*i
)->CountPositions())
692 wxLayoutObjectText
*tobj
= (wxLayoutObjectText
*) *i
;
693 #ifdef WXLAYOUT_DEBUG
694 cerr
<< "text: '" << tobj
->GetText() << "'" << endl
;
697 String left
= tobj
->GetText().substr(0,offs
); // get part before cursor
699 tobj
->GetText() = tobj
->GetText().substr(offs
,(*i
)->CountPositions()-offs
); // keeps the right half
700 VAR(tobj
->GetText());
702 insert(i
,new wxLayoutObjectText(left
)); // inserts before
706 wxLayoutObjectList::iterator j
= i
; // we want to apend after this object
714 m_CursorPosition
.x
+= obj
->CountPositions();
715 if(obj
->GetType() == WXLO_TYPE_LINEBREAK
)
720 wxLayoutList::Insert(String
const &text
)
722 wxLayoutObjectText
*tobj
= NULL
;
729 wxLayoutObjectList::iterator i
= FindCurrentObject(&offs
);
731 if(i
!= end() && (*i
)->GetType() == WXLO_TYPE_TEXT
)
732 { // insert into an existing text object:
733 TRACE(inserting into existing object
);
734 tobj
= (wxLayoutObjectText
*)*i
;
736 tobj
->GetText().insert(offs
,text
);
738 else // check whether the previous object is text:
740 wxLayoutObjectList::iterator j
= i
;
742 TRACE(checking previous object
);
743 if(0 && j
!= end() && (*j
)->GetType() == WXLO_TYPE_TEXT
)
745 tobj
= (wxLayoutObjectText
*)*i
;
747 tobj
->GetText()+=text
;
749 else // insert a new text object
751 TRACE(creating
new object
);
752 Insert(new wxLayoutObjectText(text
)); //FIXME not too optimal, slow
753 return; // position gets incremented in Insert(obj)
756 m_CursorPosition
.x
+= strlen(text
.c_str());
760 wxLayoutList::GetLineLength(wxLayoutObjectList::iterator i
)
767 // search backwards for beginning of line:
768 while(i
!= begin() && (*i
)->GetType() != WXLO_TYPE_LINEBREAK
)
770 if((*i
)->GetType() == WXLO_TYPE_LINEBREAK
)
772 // now we can start counting:
773 while(i
!= end() && (*i
)->GetType() != WXLO_TYPE_LINEBREAK
)
775 len
+= (*i
)->CountPositions();
782 wxLayoutList::Clear(int family
, int size
, int style
, int weight
,
783 int underline
, char const *fg
, char const *bg
)
785 wxLayoutObjectList::iterator i
= begin();
787 while(i
!= end()) // == while valid
792 m_FontUnderline
= false;
793 m_FontFamily
= family
;
795 m_FontWeight
= weight
;
796 m_ColourFG
= wxTheColourDatabase
->FindColour(fg
);
797 m_ColourBG
= wxTheColourDatabase
->FindColour(bg
);
799 m_Position
= wxPoint(0,0);
800 m_CursorPosition
= wxPoint(0,0);
802 m_LineHeight
= (BASELINESTRETCH
*m_FontPtSize
)/10;
803 m_MaxX
= 0; m_MaxY
= 0;
806 delete m_DefaultSetting
;
807 m_DefaultSetting
= new
808 wxLayoutObjectCmd(m_FontPtSize
,m_FontFamily
,m_FontStyle
,
809 m_FontWeight
,m_FontUnderline
,
810 m_ColourFG
, m_ColourBG
);