1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textcmn.cpp
3 // Purpose: implementation of platform-independent functions of wxTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/textctrl.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // we don't have any objects of type wxTextCtrlBase in the program, only
43 // wxTextCtrl, so this cast is safe
44 #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
46 // ============================================================================
48 // ============================================================================
50 IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent
, wxCommandEvent
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_ENTER
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_URL
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_MAXLEN
)
57 IMPLEMENT_ABSTRACT_CLASS(wxTextCtrlBase
, wxControl
)
59 // ============================================================================
60 // wxTextAttr implementation
61 // ============================================================================
63 wxTextAttr::wxTextAttr(const wxColour
& colText
,
64 const wxColour
& colBack
,
66 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
70 if (m_colText
.Ok()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
71 if (m_colBack
.Ok()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
72 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
73 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
75 GetFontAttributes(font
);
79 void wxTextAttr::Init()
81 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
88 m_fontStyle
= wxNORMAL
;
89 m_fontWeight
= wxNORMAL
;
90 m_fontUnderlined
= false;
91 m_fontEncoding
= wxFONTENCODING_DEFAULT
;
93 m_paragraphSpacingAfter
= 0;
94 m_paragraphSpacingBefore
= 0;
96 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
97 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
98 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
104 void wxTextAttr::Copy(const wxTextAttr
& attr
)
106 m_colText
= attr
.m_colText
;
107 m_colBack
= attr
.m_colBack
;
108 m_textAlignment
= attr
.m_textAlignment
;
109 m_leftIndent
= attr
.m_leftIndent
;
110 m_leftSubIndent
= attr
.m_leftSubIndent
;
111 m_rightIndent
= attr
.m_rightIndent
;
112 m_tabs
= attr
.m_tabs
;
113 m_flags
= attr
.m_flags
;
115 m_fontSize
= attr
.m_fontSize
;
116 m_fontStyle
= attr
.m_fontStyle
;
117 m_fontWeight
= attr
.m_fontWeight
;
118 m_fontUnderlined
= attr
.m_fontUnderlined
;
119 m_fontFaceName
= attr
.m_fontFaceName
;
120 m_fontEncoding
= attr
.m_fontEncoding
;
121 m_textEffects
= attr
.m_textEffects
;
122 m_textEffectFlags
= attr
.m_textEffectFlags
;
124 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
125 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
126 m_lineSpacing
= attr
.m_lineSpacing
;
127 m_characterStyleName
= attr
.m_characterStyleName
;
128 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
129 m_listStyleName
= attr
.m_listStyleName
;
130 m_bulletStyle
= attr
.m_bulletStyle
;
131 m_bulletNumber
= attr
.m_bulletNumber
;
132 m_bulletText
= attr
.m_bulletText
;
133 m_bulletFont
= attr
.m_bulletFont
;
134 m_bulletName
= attr
.m_bulletName
;
135 m_outlineLevel
= attr
.m_outlineLevel
;
137 m_urlTarget
= attr
.m_urlTarget
;
141 void wxTextAttr::operator= (const wxTextAttr
& attr
)
147 bool wxTextAttr::operator== (const wxTextAttr
& attr
) const
149 return GetFlags() == attr
.GetFlags() &&
151 GetTextColour() == attr
.GetTextColour() &&
152 GetBackgroundColour() == attr
.GetBackgroundColour() &&
154 GetAlignment() == attr
.GetAlignment() &&
155 GetLeftIndent() == attr
.GetLeftIndent() &&
156 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
157 GetRightIndent() == attr
.GetRightIndent() &&
158 TabsEq(GetTabs(), attr
.GetTabs()) &&
160 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
161 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
162 GetLineSpacing() == attr
.GetLineSpacing() &&
163 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
164 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
165 GetListStyleName() == attr
.GetListStyleName() &&
167 GetBulletStyle() == attr
.GetBulletStyle() &&
168 GetBulletText() == attr
.GetBulletText() &&
169 GetBulletNumber() == attr
.GetBulletNumber() &&
170 GetBulletFont() == attr
.GetBulletFont() &&
171 GetBulletName() == attr
.GetBulletName() &&
173 GetTextEffects() == attr
.GetTextEffects() &&
174 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
176 GetOutlineLevel() == attr
.GetOutlineLevel() &&
178 GetFontSize() == attr
.GetFontSize() &&
179 GetFontStyle() == attr
.GetFontStyle() &&
180 GetFontWeight() == attr
.GetFontWeight() &&
181 GetFontUnderlined() == attr
.GetFontUnderlined() &&
182 GetFontFaceName() == attr
.GetFontFaceName() &&
183 GetFontEncoding() == attr
.GetFontEncoding() &&
185 GetURL() == attr
.GetURL();
188 // Partial equality test taking flags into account
189 bool wxTextAttr::EqPartial(const wxTextAttr
& attr
, int flags
) const
191 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && GetTextColour() != attr
.GetTextColour())
194 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && GetBackgroundColour() != attr
.GetBackgroundColour())
197 if ((flags
& wxTEXT_ATTR_FONT_FACE
) &&
198 GetFontFaceName() != attr
.GetFontFaceName())
201 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) &&
202 GetFontSize() != attr
.GetFontSize())
205 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) &&
206 GetFontWeight() != attr
.GetFontWeight())
209 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) &&
210 GetFontStyle() != attr
.GetFontStyle())
213 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) &&
214 GetFontUnderlined() != attr
.GetFontUnderlined())
217 if ((flags
& wxTEXT_ATTR_FONT_ENCODING
) &&
218 GetFontEncoding() != attr
.GetFontEncoding())
221 if ((flags
& wxTEXT_ATTR_URL
) && GetURL() != attr
.GetURL())
224 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && GetAlignment() != attr
.GetAlignment())
227 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
228 ((GetLeftIndent() != attr
.GetLeftIndent()) || (GetLeftSubIndent() != attr
.GetLeftSubIndent())))
231 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
232 (GetRightIndent() != attr
.GetRightIndent()))
235 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
236 (GetParagraphSpacingAfter() != attr
.GetParagraphSpacingAfter()))
239 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
240 (GetParagraphSpacingBefore() != attr
.GetParagraphSpacingBefore()))
243 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
244 (GetLineSpacing() != attr
.GetLineSpacing()))
247 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
248 (GetCharacterStyleName() != attr
.GetCharacterStyleName()))
251 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
252 (GetParagraphStyleName() != attr
.GetParagraphStyleName()))
255 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
256 (GetListStyleName() != attr
.GetListStyleName()))
259 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
260 (GetBulletStyle() != attr
.GetBulletStyle()))
263 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
264 (GetBulletNumber() != attr
.GetBulletNumber()))
267 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
268 (GetBulletText() != attr
.GetBulletText()) &&
269 (GetBulletFont() != attr
.GetBulletFont()))
272 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
273 (GetBulletName() != attr
.GetBulletName()))
276 if ((flags
& wxTEXT_ATTR_TABS
) &&
277 !TabsEq(GetTabs(), attr
.GetTabs()))
280 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
281 (HasPageBreak() != attr
.HasPageBreak()))
284 if (flags
& wxTEXT_ATTR_EFFECTS
)
286 if (HasTextEffects() != attr
.HasTextEffects())
288 if (!BitlistsEqPartial(GetTextEffects(), attr
.GetTextEffects(), attr
.GetTextEffectFlags()))
292 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
293 (GetOutlineLevel() != attr
.GetOutlineLevel()))
299 // Create font from font attributes.
300 wxFont
wxTextAttr::GetFont() const
307 fontSize
= GetFontSize();
309 int fontStyle
= wxNORMAL
;
311 fontStyle
= GetFontStyle();
313 int fontWeight
= wxNORMAL
;
315 fontWeight
= GetFontWeight();
317 bool underlined
= false;
318 if (HasFontUnderlined())
319 underlined
= GetFontUnderlined();
321 wxString fontFaceName
;
322 if (HasFontFaceName())
323 fontFaceName
= GetFontFaceName();
325 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
;
326 if (HasFontEncoding())
327 encoding
= GetFontEncoding();
329 wxFont
font(fontSize
, wxDEFAULT
, fontStyle
, fontWeight
, underlined
, fontFaceName
, encoding
);
331 font
.SetNoAntiAliasing(true);
336 // Get attributes from font.
337 bool wxTextAttr::GetFontAttributes(const wxFont
& font
, int flags
)
342 if (flags
& wxTEXT_ATTR_FONT_SIZE
)
343 m_fontSize
= font
.GetPointSize();
345 if (flags
& wxTEXT_ATTR_FONT_ITALIC
)
346 m_fontStyle
= font
.GetStyle();
348 if (flags
& wxTEXT_ATTR_FONT_WEIGHT
)
349 m_fontWeight
= font
.GetWeight();
351 if (flags
& wxTEXT_ATTR_FONT_UNDERLINE
)
352 m_fontUnderlined
= font
.GetUnderlined();
354 if (flags
& wxTEXT_ATTR_FONT_FACE
)
355 m_fontFaceName
= font
.GetFaceName();
357 if (flags
& wxTEXT_ATTR_FONT_ENCODING
)
358 m_fontEncoding
= font
.GetEncoding();
365 // Resets bits in destination so new attributes aren't merged with mutually exclusive ones
366 static bool wxResetIncompatibleBits(const int mask
, const int srcFlags
, int& destFlags
, int& destBits
)
368 if ((srcFlags
& mask
) && (destFlags
& mask
))
377 bool wxTextAttr::Apply(const wxTextAttr
& style
, const wxTextAttr
* compareWith
)
379 wxTextAttr
& destStyle
= (*this);
381 if (style
.HasFontWeight())
383 if (!(compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight()))
384 destStyle
.SetFontWeight(style
.GetFontWeight());
387 if (style
.HasFontSize())
389 if (!(compareWith
&& compareWith
->HasFontSize() && compareWith
->GetFontSize() == style
.GetFontSize()))
390 destStyle
.SetFontSize(style
.GetFontSize());
393 if (style
.HasFontItalic())
395 if (!(compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle()))
396 destStyle
.SetFontStyle(style
.GetFontStyle());
399 if (style
.HasFontUnderlined())
401 if (!(compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined()))
402 destStyle
.SetFontUnderlined(style
.GetFontUnderlined());
405 if (style
.HasFontFaceName())
407 if (!(compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName()))
408 destStyle
.SetFontFaceName(style
.GetFontFaceName());
411 if (style
.HasFontEncoding())
413 if (!(compareWith
&& compareWith
->HasFontEncoding() && compareWith
->GetFontEncoding() == style
.GetFontEncoding()))
414 destStyle
.SetFontEncoding(style
.GetFontEncoding());
417 if (style
.GetTextColour().Ok() && style
.HasTextColour())
419 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
420 destStyle
.SetTextColour(style
.GetTextColour());
423 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
425 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
426 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
429 if (style
.HasAlignment())
431 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
432 destStyle
.SetAlignment(style
.GetAlignment());
437 if (!(compareWith
&& compareWith
->HasTabs() && TabsEq(compareWith
->GetTabs(), style
.GetTabs())))
438 destStyle
.SetTabs(style
.GetTabs());
441 if (style
.HasLeftIndent())
443 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
444 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
445 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
448 if (style
.HasRightIndent())
450 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
451 destStyle
.SetRightIndent(style
.GetRightIndent());
454 if (style
.HasParagraphSpacingAfter())
456 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
457 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
460 if (style
.HasParagraphSpacingBefore())
462 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
463 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
466 if (style
.HasLineSpacing())
468 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
469 destStyle
.SetLineSpacing(style
.GetLineSpacing());
472 if (style
.HasCharacterStyleName())
474 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
475 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
478 if (style
.HasParagraphStyleName())
480 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
481 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
484 if (style
.HasListStyleName())
486 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
487 destStyle
.SetListStyleName(style
.GetListStyleName());
490 if (style
.HasBulletStyle())
492 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
493 destStyle
.SetBulletStyle(style
.GetBulletStyle());
496 if (style
.HasBulletText())
498 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
500 destStyle
.SetBulletText(style
.GetBulletText());
501 destStyle
.SetBulletFont(style
.GetBulletFont());
505 if (style
.HasBulletNumber())
507 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
508 destStyle
.SetBulletNumber(style
.GetBulletNumber());
511 if (style
.HasBulletName())
513 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
514 destStyle
.SetBulletName(style
.GetBulletName());
519 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
520 destStyle
.SetURL(style
.GetURL());
523 if (style
.HasPageBreak())
525 if (!(compareWith
&& compareWith
->HasPageBreak()))
526 destStyle
.SetPageBreak();
529 if (style
.HasTextEffects())
531 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
533 int destBits
= destStyle
.GetTextEffects();
534 int destFlags
= destStyle
.GetTextEffectFlags();
536 int srcBits
= style
.GetTextEffects();
537 int srcFlags
= style
.GetTextEffectFlags();
539 // Reset incompatible bits in the destination
540 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_SUPERSCRIPT
|wxTEXT_ATTR_EFFECT_SUBSCRIPT
), srcFlags
, destFlags
, destBits
);
541 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_CAPITALS
|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS
), srcFlags
, destFlags
, destBits
);
542 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_DOUBLE_STRIKETHROUGH
), srcFlags
, destFlags
, destBits
);
544 CombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
546 destStyle
.SetTextEffects(destBits
);
547 destStyle
.SetTextEffectFlags(destFlags
);
551 if (style
.HasOutlineLevel())
553 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
554 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
561 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
562 const wxTextAttr
& attrDef
,
563 const wxTextCtrlBase
*text
)
567 font
= attr
.GetFont();
571 if (attrDef
.HasFont())
572 font
= attrDef
.GetFont();
574 if ( text
&& !font
.Ok() )
575 font
= text
->GetFont();
578 wxColour colFg
= attr
.GetTextColour();
581 colFg
= attrDef
.GetTextColour();
583 if ( text
&& !colFg
.Ok() )
584 colFg
= text
->GetForegroundColour();
587 wxColour colBg
= attr
.GetBackgroundColour();
590 colBg
= attrDef
.GetBackgroundColour();
592 if ( text
&& !colBg
.Ok() )
593 colBg
= text
->GetBackgroundColour();
596 wxTextAttr
newAttr(colFg
, colBg
, font
);
598 if (attr
.HasAlignment())
599 newAttr
.SetAlignment(attr
.GetAlignment());
600 else if (attrDef
.HasAlignment())
601 newAttr
.SetAlignment(attrDef
.GetAlignment());
604 newAttr
.SetTabs(attr
.GetTabs());
605 else if (attrDef
.HasTabs())
606 newAttr
.SetTabs(attrDef
.GetTabs());
608 if (attr
.HasLeftIndent())
609 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
610 else if (attrDef
.HasLeftIndent())
611 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
613 if (attr
.HasRightIndent())
614 newAttr
.SetRightIndent(attr
.GetRightIndent());
615 else if (attrDef
.HasRightIndent())
616 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
622 bool wxTextAttr::TabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
624 if (tabs1
.GetCount() != tabs2
.GetCount())
628 for (i
= 0; i
< tabs1
.GetCount(); i
++)
630 if (tabs1
[i
] != tabs2
[i
])
637 bool wxTextAttr::RemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
639 int flags
= style
.GetFlags();
640 int destFlags
= destStyle
.GetFlags();
642 destStyle
.SetFlags(destFlags
& ~flags
);
647 /// Combine two bitlists, specifying the bits of interest with separate flags.
648 bool wxTextAttr::CombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
650 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
651 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
654 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
655 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
656 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
658 // Now combine the 1 bits.
659 int valueA3
= (valueB
& flagsB
) | valueA2
;
662 flagsA
= (flagsA
| flagsB
);
667 /// Compare two bitlists
668 bool wxTextAttr::BitlistsEqPartial(int valueA
, int valueB
, int flags
)
670 int relevantBitsA
= valueA
& flags
;
671 int relevantBitsB
= valueB
& flags
;
672 return (relevantBitsA
!= relevantBitsB
);
675 /// Split into paragraph and character styles
676 bool wxTextAttr::SplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
678 wxTextAttr
defaultCharStyle1(style
);
679 wxTextAttr
defaultParaStyle1(style
);
680 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
681 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
683 charStyle
.Apply(defaultCharStyle1
);
684 parStyle
.Apply(defaultParaStyle1
);
689 // apply styling to text range
690 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
691 const wxTextAttr
& WXUNUSED(style
))
693 // to be implemented in derived classes
697 // get the styling at the given position
698 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position
), wxTextAttr
& WXUNUSED(style
))
700 // to be implemented in derived classes
704 // change default text attributes
705 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
707 // keep the old attributes if the new style doesn't specify them unless the
708 // new style is empty - then reset m_defaultStyle (as there is no other way
710 if ( style
.IsDefault() )
711 m_defaultStyle
= style
;
713 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
718 // ----------------------------------------------------------------------------
720 // ----------------------------------------------------------------------------
722 bool wxTextCtrlBase::DoLoadFile(const wxString
& filename
, int WXUNUSED(fileType
))
725 wxFFile
file(filename
);
726 if ( file
.IsOpened() )
729 if ( file
.ReadAll(&text
) )
735 m_filename
= filename
;
741 wxLogError(_("File couldn't be loaded."));
742 #endif // wxUSE_FFILE
747 bool wxTextAreaBase::SaveFile(const wxString
& filename
, int fileType
)
749 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
750 if ( filenameToUse
.empty() )
752 // what kind of message to give? is it an error or a program bug?
753 wxLogDebug(wxT("Can't save textctrl to file without filename."));
758 return DoSaveFile(filenameToUse
, fileType
);
761 bool wxTextCtrlBase::DoSaveFile(const wxString
& filename
, int WXUNUSED(fileType
))
764 wxFFile
file(filename
, _T("w"));
765 if ( file
.IsOpened() && file
.Write(GetValue()) )
767 // if it worked, save for future calls
768 m_filename
= filename
;
770 // it's not modified any longer
775 #endif // wxUSE_FFILE
777 wxLogError(_("The text couldn't be saved."));
782 // ----------------------------------------------------------------------------
783 // stream-like insertion operator
784 // ----------------------------------------------------------------------------
786 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
789 return *TEXTCTRL(this);
792 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
794 return *this << wxString::Format("%.2f", d
);
797 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
799 return *this << wxString::Format("%d", i
);
802 wxTextCtrl
& wxTextCtrlBase::operator<<(long l
)
804 return *this << wxString::Format("%ld", l
);
807 // ----------------------------------------------------------------------------
808 // streambuf methods implementation
809 // ----------------------------------------------------------------------------
811 #if wxHAS_TEXT_WINDOW_STREAM
813 int wxTextCtrlBase::overflow(int c
)
815 AppendText((wxChar
)c
);
817 // return something different from EOF
821 #endif // wxHAS_TEXT_WINDOW_STREAM
823 // ----------------------------------------------------------------------------
824 // emulating key presses
825 // ----------------------------------------------------------------------------
827 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
829 // we have a native implementation for Win32 and so don't need this one
832 int keycode
= event
.GetKeyCode();
845 ch
= (wxChar
)(_T('0') + keycode
- WXK_NUMPAD0
);
849 case WXK_NUMPAD_MULTIPLY
:
859 case WXK_NUMPAD_SUBTRACT
:
864 case WXK_NUMPAD_DECIMAL
:
869 case WXK_NUMPAD_DIVIDE
:
874 case WXK_NUMPAD_DELETE
:
875 // delete the character at cursor
877 const long pos
= GetInsertionPoint();
878 if ( pos
< GetLastPosition() )
879 Remove(pos
, pos
+ 1);
884 // delete the character before the cursor
886 const long pos
= GetInsertionPoint();
888 Remove(pos
- 1, pos
);
894 if ( event
.GetUnicodeKey() )
896 ch
= event
.GetUnicodeKey();
900 if ( keycode
< 256 && keycode
>= 0 && wxIsprint(keycode
) )
902 // FIXME this is not going to work for non letters...
903 if ( !event
.ShiftDown() )
905 keycode
= wxTolower(keycode
);
908 ch
= (wxChar
)keycode
;
924 #endif // !__WIN32__/__WIN32__
929 // do the window-specific processing after processing the update event
930 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
932 // call inherited, but skip the wxControl's version, and call directly the
933 // wxWindow's one instead, because the only reason why we are overriding this
934 // function is that we want to use SetValue() instead of wxControl::SetLabel()
935 wxWindowBase::DoUpdateWindowUI(event
);
938 if ( event
.GetSetText() )
940 if ( event
.GetText() != GetValue() )
941 SetValue(event
.GetText());
945 // ----------------------------------------------------------------------------
947 // ----------------------------------------------------------------------------
949 wxTextCtrlHitTestResult
950 wxTextAreaBase::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
952 // implement in terms of the other overload as the native ports typically
953 // can get the position and not (x, y) pair directly (although wxUniv
954 // directly gets x and y -- and so overrides this method as well)
956 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
958 if ( rc
!= wxTE_HT_UNKNOWN
)
960 PositionToXY(pos
, x
, y
);
966 wxTextCtrlHitTestResult
967 wxTextAreaBase::HitTest(const wxPoint
& WXUNUSED(pt
), long * WXUNUSED(pos
)) const
970 return wxTE_HT_UNKNOWN
;
973 // ----------------------------------------------------------------------------
975 // ----------------------------------------------------------------------------
978 bool wxTextCtrlBase::SendTextUpdatedEvent(wxWindow
*win
)
980 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId());
982 // do not do this as it could be very inefficient if the text control
983 // contains a lot of text and we're not using ref-counted wxString
984 // implementation -- instead, event.GetString() will query the control for
985 // its current text if needed
986 //event.SetString(win->GetValue());
988 event
.SetEventObject(win
);
989 return win
->GetEventHandler()->ProcessEvent(event
);
992 #else // !wxUSE_TEXTCTRL
994 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
995 // controls (wxComboBox and wxSpinCtrl)
997 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED
)
999 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL