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 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
);
54 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_URL
, wxTextUrlEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_MAXLEN
, wxCommandEvent
);
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
= wxFONTSTYLE_NORMAL
;
89 m_fontWeight
= wxFONTWEIGHT_NORMAL
;
90 m_fontUnderlined
= false;
91 m_fontEncoding
= wxFONTENCODING_DEFAULT
;
92 m_fontFamily
= wxFONTFAMILY_DEFAULT
;
94 m_paragraphSpacingAfter
= 0;
95 m_paragraphSpacingBefore
= 0;
97 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
98 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
99 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
105 void wxTextAttr::Copy(const wxTextAttr
& attr
)
107 m_colText
= attr
.m_colText
;
108 m_colBack
= attr
.m_colBack
;
109 m_textAlignment
= attr
.m_textAlignment
;
110 m_leftIndent
= attr
.m_leftIndent
;
111 m_leftSubIndent
= attr
.m_leftSubIndent
;
112 m_rightIndent
= attr
.m_rightIndent
;
113 m_tabs
= attr
.m_tabs
;
114 m_flags
= attr
.m_flags
;
116 m_fontSize
= attr
.m_fontSize
;
117 m_fontStyle
= attr
.m_fontStyle
;
118 m_fontWeight
= attr
.m_fontWeight
;
119 m_fontUnderlined
= attr
.m_fontUnderlined
;
120 m_fontFaceName
= attr
.m_fontFaceName
;
121 m_fontEncoding
= attr
.m_fontEncoding
;
122 m_fontFamily
= attr
.m_fontFamily
;
123 m_textEffects
= attr
.m_textEffects
;
124 m_textEffectFlags
= attr
.m_textEffectFlags
;
126 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
127 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
128 m_lineSpacing
= attr
.m_lineSpacing
;
129 m_characterStyleName
= attr
.m_characterStyleName
;
130 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
131 m_listStyleName
= attr
.m_listStyleName
;
132 m_bulletStyle
= attr
.m_bulletStyle
;
133 m_bulletNumber
= attr
.m_bulletNumber
;
134 m_bulletText
= attr
.m_bulletText
;
135 m_bulletFont
= attr
.m_bulletFont
;
136 m_bulletName
= attr
.m_bulletName
;
137 m_outlineLevel
= attr
.m_outlineLevel
;
139 m_urlTarget
= attr
.m_urlTarget
;
143 void wxTextAttr::operator= (const wxTextAttr
& attr
)
149 bool wxTextAttr::operator== (const wxTextAttr
& attr
) const
151 return GetFlags() == attr
.GetFlags() &&
153 GetTextColour() == attr
.GetTextColour() &&
154 GetBackgroundColour() == attr
.GetBackgroundColour() &&
156 GetAlignment() == attr
.GetAlignment() &&
157 GetLeftIndent() == attr
.GetLeftIndent() &&
158 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
159 GetRightIndent() == attr
.GetRightIndent() &&
160 TabsEq(GetTabs(), attr
.GetTabs()) &&
162 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
163 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
164 GetLineSpacing() == attr
.GetLineSpacing() &&
165 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
166 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
167 GetListStyleName() == attr
.GetListStyleName() &&
169 GetBulletStyle() == attr
.GetBulletStyle() &&
170 GetBulletText() == attr
.GetBulletText() &&
171 GetBulletNumber() == attr
.GetBulletNumber() &&
172 GetBulletFont() == attr
.GetBulletFont() &&
173 GetBulletName() == attr
.GetBulletName() &&
175 GetTextEffects() == attr
.GetTextEffects() &&
176 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
178 GetOutlineLevel() == attr
.GetOutlineLevel() &&
180 GetFontSize() == attr
.GetFontSize() &&
181 GetFontStyle() == attr
.GetFontStyle() &&
182 GetFontWeight() == attr
.GetFontWeight() &&
183 GetFontUnderlined() == attr
.GetFontUnderlined() &&
184 GetFontFaceName() == attr
.GetFontFaceName() &&
185 GetFontEncoding() == attr
.GetFontEncoding() &&
186 GetFontFamily() == attr
.GetFontFamily() &&
188 GetURL() == attr
.GetURL();
191 // Partial equality test taking flags into account
192 bool wxTextAttr::EqPartial(const wxTextAttr
& attr
, int flags
) const
194 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && GetTextColour() != attr
.GetTextColour())
197 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && GetBackgroundColour() != attr
.GetBackgroundColour())
200 if ((flags
& wxTEXT_ATTR_FONT_FACE
) &&
201 GetFontFaceName() != attr
.GetFontFaceName())
204 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) &&
205 GetFontSize() != attr
.GetFontSize())
208 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) &&
209 GetFontWeight() != attr
.GetFontWeight())
212 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) &&
213 GetFontStyle() != attr
.GetFontStyle())
216 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) &&
217 GetFontUnderlined() != attr
.GetFontUnderlined())
220 if ((flags
& wxTEXT_ATTR_FONT_ENCODING
) &&
221 GetFontEncoding() != attr
.GetFontEncoding())
224 if ((flags
& wxTEXT_ATTR_FONT_FAMILY
) &&
225 GetFontFamily() != attr
.GetFontFamily())
228 if ((flags
& wxTEXT_ATTR_URL
) && GetURL() != attr
.GetURL())
231 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && GetAlignment() != attr
.GetAlignment())
234 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
235 ((GetLeftIndent() != attr
.GetLeftIndent()) || (GetLeftSubIndent() != attr
.GetLeftSubIndent())))
238 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
239 (GetRightIndent() != attr
.GetRightIndent()))
242 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
243 (GetParagraphSpacingAfter() != attr
.GetParagraphSpacingAfter()))
246 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
247 (GetParagraphSpacingBefore() != attr
.GetParagraphSpacingBefore()))
250 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
251 (GetLineSpacing() != attr
.GetLineSpacing()))
254 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
255 (GetCharacterStyleName() != attr
.GetCharacterStyleName()))
258 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
259 (GetParagraphStyleName() != attr
.GetParagraphStyleName()))
262 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
263 (GetListStyleName() != attr
.GetListStyleName()))
266 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
267 (GetBulletStyle() != attr
.GetBulletStyle()))
270 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
271 (GetBulletNumber() != attr
.GetBulletNumber()))
274 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
275 (GetBulletText() != attr
.GetBulletText()) &&
276 (GetBulletFont() != attr
.GetBulletFont()))
279 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
280 (GetBulletName() != attr
.GetBulletName()))
283 if ((flags
& wxTEXT_ATTR_TABS
) &&
284 !TabsEq(GetTabs(), attr
.GetTabs()))
287 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
288 (HasPageBreak() != attr
.HasPageBreak()))
291 if (flags
& wxTEXT_ATTR_EFFECTS
)
293 if (HasTextEffects() != attr
.HasTextEffects())
295 if (!BitlistsEqPartial(GetTextEffects(), attr
.GetTextEffects(), attr
.GetTextEffectFlags()))
299 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
300 (GetOutlineLevel() != attr
.GetOutlineLevel()))
306 // Create font from font attributes.
307 wxFont
wxTextAttr::GetFont() const
314 fontSize
= GetFontSize();
316 int fontStyle
= wxNORMAL
;
318 fontStyle
= GetFontStyle();
320 int fontWeight
= wxNORMAL
;
322 fontWeight
= GetFontWeight();
324 bool underlined
= false;
325 if (HasFontUnderlined())
326 underlined
= GetFontUnderlined();
328 wxString fontFaceName
;
329 if (HasFontFaceName())
330 fontFaceName
= GetFontFaceName();
332 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
;
333 if (HasFontEncoding())
334 encoding
= GetFontEncoding();
336 wxFontFamily fontFamily
= wxFONTFAMILY_DEFAULT
;
338 fontFamily
= GetFontFamily();
340 wxFont
font(fontSize
, fontFamily
, fontStyle
, fontWeight
, underlined
, fontFaceName
, encoding
);
344 // Get attributes from font.
345 bool wxTextAttr::GetFontAttributes(const wxFont
& font
, int flags
)
350 if (flags
& wxTEXT_ATTR_FONT_SIZE
)
351 m_fontSize
= font
.GetPointSize();
353 if (flags
& wxTEXT_ATTR_FONT_ITALIC
)
354 m_fontStyle
= font
.GetStyle();
356 if (flags
& wxTEXT_ATTR_FONT_WEIGHT
)
357 m_fontWeight
= font
.GetWeight();
359 if (flags
& wxTEXT_ATTR_FONT_UNDERLINE
)
360 m_fontUnderlined
= font
.GetUnderlined();
362 if (flags
& wxTEXT_ATTR_FONT_FACE
)
363 m_fontFaceName
= font
.GetFaceName();
365 if (flags
& wxTEXT_ATTR_FONT_ENCODING
)
366 m_fontEncoding
= font
.GetEncoding();
368 if (flags
& wxTEXT_ATTR_FONT_FAMILY
)
370 // wxFont might not know its family, avoid setting m_fontFamily to an
371 // invalid value and rather pretend that we don't have any font family
372 // information at all in this case
373 const wxFontFamily fontFamily
= font
.GetFamily();
374 if ( fontFamily
== wxFONTFAMILY_UNKNOWN
)
375 flags
&= ~wxTEXT_ATTR_FONT_FAMILY
;
377 m_fontFamily
= fontFamily
;
385 // Resets bits in destination so new attributes aren't merged with mutually exclusive ones
386 static bool wxResetIncompatibleBits(const int mask
, const int srcFlags
, int& destFlags
, int& destBits
)
388 if ((srcFlags
& mask
) && (destFlags
& mask
))
397 bool wxTextAttr::Apply(const wxTextAttr
& style
, const wxTextAttr
* compareWith
)
399 wxTextAttr
& destStyle
= (*this);
401 if (style
.HasFontWeight())
403 if (!(compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight()))
404 destStyle
.SetFontWeight(style
.GetFontWeight());
407 if (style
.HasFontSize())
409 if (!(compareWith
&& compareWith
->HasFontSize() && compareWith
->GetFontSize() == style
.GetFontSize()))
410 destStyle
.SetFontSize(style
.GetFontSize());
413 if (style
.HasFontItalic())
415 if (!(compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle()))
416 destStyle
.SetFontStyle(style
.GetFontStyle());
419 if (style
.HasFontUnderlined())
421 if (!(compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined()))
422 destStyle
.SetFontUnderlined(style
.GetFontUnderlined());
425 if (style
.HasFontFaceName())
427 if (!(compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName()))
428 destStyle
.SetFontFaceName(style
.GetFontFaceName());
431 if (style
.HasFontEncoding())
433 if (!(compareWith
&& compareWith
->HasFontEncoding() && compareWith
->GetFontEncoding() == style
.GetFontEncoding()))
434 destStyle
.SetFontEncoding(style
.GetFontEncoding());
437 if (style
.HasFontFamily())
439 if (!(compareWith
&& compareWith
->HasFontFamily() && compareWith
->GetFontFamily() == style
.GetFontFamily()))
440 destStyle
.SetFontFamily(style
.GetFontFamily());
443 if (style
.GetTextColour().Ok() && style
.HasTextColour())
445 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
446 destStyle
.SetTextColour(style
.GetTextColour());
449 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
451 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
452 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
455 if (style
.HasAlignment())
457 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
458 destStyle
.SetAlignment(style
.GetAlignment());
463 if (!(compareWith
&& compareWith
->HasTabs() && TabsEq(compareWith
->GetTabs(), style
.GetTabs())))
464 destStyle
.SetTabs(style
.GetTabs());
467 if (style
.HasLeftIndent())
469 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
470 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
471 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
474 if (style
.HasRightIndent())
476 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
477 destStyle
.SetRightIndent(style
.GetRightIndent());
480 if (style
.HasParagraphSpacingAfter())
482 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
483 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
486 if (style
.HasParagraphSpacingBefore())
488 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
489 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
492 if (style
.HasLineSpacing())
494 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
495 destStyle
.SetLineSpacing(style
.GetLineSpacing());
498 if (style
.HasCharacterStyleName())
500 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
501 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
504 if (style
.HasParagraphStyleName())
506 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
507 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
510 if (style
.HasListStyleName())
512 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
513 destStyle
.SetListStyleName(style
.GetListStyleName());
516 if (style
.HasBulletStyle())
518 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
519 destStyle
.SetBulletStyle(style
.GetBulletStyle());
522 if (style
.HasBulletText())
524 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
526 destStyle
.SetBulletText(style
.GetBulletText());
527 destStyle
.SetBulletFont(style
.GetBulletFont());
531 if (style
.HasBulletNumber())
533 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
534 destStyle
.SetBulletNumber(style
.GetBulletNumber());
537 if (style
.HasBulletName())
539 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
540 destStyle
.SetBulletName(style
.GetBulletName());
545 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
546 destStyle
.SetURL(style
.GetURL());
549 if (style
.HasPageBreak())
551 if (!(compareWith
&& compareWith
->HasPageBreak()))
552 destStyle
.SetPageBreak();
555 if (style
.HasTextEffects())
557 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
559 int destBits
= destStyle
.GetTextEffects();
560 int destFlags
= destStyle
.GetTextEffectFlags();
562 int srcBits
= style
.GetTextEffects();
563 int srcFlags
= style
.GetTextEffectFlags();
565 // Reset incompatible bits in the destination
566 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_SUPERSCRIPT
|wxTEXT_ATTR_EFFECT_SUBSCRIPT
), srcFlags
, destFlags
, destBits
);
567 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_CAPITALS
|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS
), srcFlags
, destFlags
, destBits
);
568 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_DOUBLE_STRIKETHROUGH
), srcFlags
, destFlags
, destBits
);
570 CombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
572 destStyle
.SetTextEffects(destBits
);
573 destStyle
.SetTextEffectFlags(destFlags
);
577 if (style
.HasOutlineLevel())
579 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
580 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
587 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
588 const wxTextAttr
& attrDef
,
589 const wxTextCtrlBase
*text
)
593 font
= attr
.GetFont();
597 if (attrDef
.HasFont())
598 font
= attrDef
.GetFont();
600 if ( text
&& !font
.Ok() )
601 font
= text
->GetFont();
604 wxColour colFg
= attr
.GetTextColour();
607 colFg
= attrDef
.GetTextColour();
609 if ( text
&& !colFg
.Ok() )
610 colFg
= text
->GetForegroundColour();
613 wxColour colBg
= attr
.GetBackgroundColour();
616 colBg
= attrDef
.GetBackgroundColour();
618 if ( text
&& !colBg
.Ok() )
619 colBg
= text
->GetBackgroundColour();
622 wxTextAttr
newAttr(colFg
, colBg
, font
);
624 if (attr
.HasAlignment())
625 newAttr
.SetAlignment(attr
.GetAlignment());
626 else if (attrDef
.HasAlignment())
627 newAttr
.SetAlignment(attrDef
.GetAlignment());
630 newAttr
.SetTabs(attr
.GetTabs());
631 else if (attrDef
.HasTabs())
632 newAttr
.SetTabs(attrDef
.GetTabs());
634 if (attr
.HasLeftIndent())
635 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
636 else if (attrDef
.HasLeftIndent())
637 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
639 if (attr
.HasRightIndent())
640 newAttr
.SetRightIndent(attr
.GetRightIndent());
641 else if (attrDef
.HasRightIndent())
642 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
648 bool wxTextAttr::TabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
650 if (tabs1
.GetCount() != tabs2
.GetCount())
654 for (i
= 0; i
< tabs1
.GetCount(); i
++)
656 if (tabs1
[i
] != tabs2
[i
])
663 bool wxTextAttr::RemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
665 int flags
= style
.GetFlags();
666 int destFlags
= destStyle
.GetFlags();
668 destStyle
.SetFlags(destFlags
& ~flags
);
673 /// Combine two bitlists, specifying the bits of interest with separate flags.
674 bool wxTextAttr::CombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
676 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
677 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
680 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
681 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
682 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
684 // Now combine the 1 bits.
685 int valueA3
= (valueB
& flagsB
) | valueA2
;
688 flagsA
= (flagsA
| flagsB
);
693 /// Compare two bitlists
694 bool wxTextAttr::BitlistsEqPartial(int valueA
, int valueB
, int flags
)
696 int relevantBitsA
= valueA
& flags
;
697 int relevantBitsB
= valueB
& flags
;
698 return (relevantBitsA
!= relevantBitsB
);
701 /// Split into paragraph and character styles
702 bool wxTextAttr::SplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
704 wxTextAttr
defaultCharStyle1(style
);
705 wxTextAttr
defaultParaStyle1(style
);
706 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
707 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
709 charStyle
.Apply(defaultCharStyle1
);
710 parStyle
.Apply(defaultParaStyle1
);
715 // apply styling to text range
716 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
717 const wxTextAttr
& WXUNUSED(style
))
719 // to be implemented in derived classes
723 // get the styling at the given position
724 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position
), wxTextAttr
& WXUNUSED(style
))
726 // to be implemented in derived classes
730 // change default text attributes
731 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
733 // keep the old attributes if the new style doesn't specify them unless the
734 // new style is empty - then reset m_defaultStyle (as there is no other way
736 if ( style
.IsDefault() )
737 m_defaultStyle
= style
;
739 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
744 // ----------------------------------------------------------------------------
746 // ----------------------------------------------------------------------------
748 bool wxTextAreaBase::DoLoadFile(const wxString
& filename
, int WXUNUSED(fileType
))
751 wxFFile
file(filename
);
752 if ( file
.IsOpened() )
755 if ( file
.ReadAll(&text
) )
762 #endif // wxUSE_FFILE
767 bool wxTextCtrlBase::DoLoadFile(const wxString
& filename
, int fileType
)
769 if ( wxTextAreaBase::DoLoadFile(filename
, fileType
) )
772 m_filename
= filename
;
775 wxLogError(_("File couldn't be loaded."));
779 bool wxTextAreaBase::DoSaveFile(const wxString
& filename
, int WXUNUSED(fileType
))
782 wxFFile
file(filename
, wxT("w"));
783 return file
.IsOpened() && file
.Write(GetValue(), *wxConvCurrent
);
786 #endif // wxUSE_FFILE
789 bool wxTextAreaBase::SaveFile(const wxString
& filename
, int fileType
)
791 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
792 if ( filenameToUse
.empty() )
794 // what kind of message to give? is it an error or a program bug?
795 wxLogDebug(wxT("Can't save textctrl to file without filename."));
800 return DoSaveFile(filenameToUse
, fileType
);
803 bool wxTextCtrlBase::DoSaveFile(const wxString
& filename
, int fileType
)
805 if ( wxTextAreaBase::DoSaveFile(filename
, fileType
) )
807 // if it worked, save for future calls
808 m_filename
= filename
;
810 // it's not modified any longer
818 // ----------------------------------------------------------------------------
819 // stream-like insertion operator
820 // ----------------------------------------------------------------------------
822 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
825 return *TEXTCTRL(this);
828 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
830 return *this << wxString::Format("%.2f", d
);
833 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
835 return *this << wxString::Format("%d", i
);
838 wxTextCtrl
& wxTextCtrlBase::operator<<(long l
)
840 return *this << wxString::Format("%ld", l
);
843 // ----------------------------------------------------------------------------
844 // streambuf methods implementation
845 // ----------------------------------------------------------------------------
847 #if wxHAS_TEXT_WINDOW_STREAM
849 int wxTextCtrlBase::overflow(int c
)
851 AppendText((wxChar
)c
);
853 // return something different from EOF
857 #endif // wxHAS_TEXT_WINDOW_STREAM
859 // ----------------------------------------------------------------------------
860 // emulating key presses
861 // ----------------------------------------------------------------------------
863 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
865 // we have a native implementation for Win32 and so don't need this one
868 int keycode
= event
.GetKeyCode();
881 ch
= (wxChar
)(wxT('0') + keycode
- WXK_NUMPAD0
);
885 case WXK_NUMPAD_MULTIPLY
:
895 case WXK_NUMPAD_SUBTRACT
:
900 case WXK_NUMPAD_DECIMAL
:
905 case WXK_NUMPAD_DIVIDE
:
910 case WXK_NUMPAD_DELETE
:
911 // delete the character at cursor
913 const long pos
= GetInsertionPoint();
914 if ( pos
< GetLastPosition() )
915 Remove(pos
, pos
+ 1);
920 // delete the character before the cursor
922 const long pos
= GetInsertionPoint();
924 Remove(pos
- 1, pos
);
930 if ( event
.GetUnicodeKey() )
932 ch
= event
.GetUnicodeKey();
936 if ( keycode
< 256 && keycode
>= 0 && wxIsprint(keycode
) )
938 // FIXME this is not going to work for non letters...
939 if ( !event
.ShiftDown() )
941 keycode
= wxTolower(keycode
);
944 ch
= (wxChar
)keycode
;
960 #endif // !__WIN32__/__WIN32__
965 // do the window-specific processing after processing the update event
966 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
968 // call inherited, but skip the wxControl's version, and call directly the
969 // wxWindow's one instead, because the only reason why we are overriding this
970 // function is that we want to use SetValue() instead of wxControl::SetLabel()
971 wxWindowBase::DoUpdateWindowUI(event
);
974 if ( event
.GetSetText() )
976 if ( event
.GetText() != GetValue() )
977 SetValue(event
.GetText());
981 // ----------------------------------------------------------------------------
983 // ----------------------------------------------------------------------------
985 wxTextCtrlHitTestResult
986 wxTextAreaBase::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
988 // implement in terms of the other overload as the native ports typically
989 // can get the position and not (x, y) pair directly (although wxUniv
990 // directly gets x and y -- and so overrides this method as well)
992 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
994 if ( rc
!= wxTE_HT_UNKNOWN
)
996 PositionToXY(pos
, x
, y
);
1002 wxTextCtrlHitTestResult
1003 wxTextAreaBase::HitTest(const wxPoint
& WXUNUSED(pt
), long * WXUNUSED(pos
)) const
1006 return wxTE_HT_UNKNOWN
;
1009 // ----------------------------------------------------------------------------
1011 // ----------------------------------------------------------------------------
1014 bool wxTextCtrlBase::SendTextUpdatedEvent(wxWindow
*win
)
1016 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId());
1018 // do not do this as it could be very inefficient if the text control
1019 // contains a lot of text and we're not using ref-counted wxString
1020 // implementation -- instead, event.GetString() will query the control for
1021 // its current text if needed
1022 //event.SetString(win->GetValue());
1024 event
.SetEventObject(win
);
1025 return win
->GetEventHandler()->ProcessEvent(event
);
1028 #else // !wxUSE_TEXTCTRL
1030 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
1031 // controls (wxComboBox and wxSpinCtrl)
1033 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
);
1035 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL