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. Only returns false if an attribute doesn't match.
192 bool wxTextAttr::EqPartial(const wxTextAttr
& attr
) const
194 int flags
= attr
.GetFlags();
196 if ((flags
& wxTEXT_ATTR_TEXT_COLOUR
) && GetTextColour() != attr
.GetTextColour())
199 if ((flags
& wxTEXT_ATTR_BACKGROUND_COLOUR
) && GetBackgroundColour() != attr
.GetBackgroundColour())
202 if ((flags
& wxTEXT_ATTR_FONT_FACE
) &&
203 GetFontFaceName() != attr
.GetFontFaceName())
206 if ((flags
& wxTEXT_ATTR_FONT_SIZE
) &&
207 GetFontSize() != attr
.GetFontSize())
210 if ((flags
& wxTEXT_ATTR_FONT_WEIGHT
) &&
211 GetFontWeight() != attr
.GetFontWeight())
214 if ((flags
& wxTEXT_ATTR_FONT_ITALIC
) &&
215 GetFontStyle() != attr
.GetFontStyle())
218 if ((flags
& wxTEXT_ATTR_FONT_UNDERLINE
) &&
219 GetFontUnderlined() != attr
.GetFontUnderlined())
222 if ((flags
& wxTEXT_ATTR_FONT_ENCODING
) &&
223 GetFontEncoding() != attr
.GetFontEncoding())
226 if ((flags
& wxTEXT_ATTR_FONT_FAMILY
) &&
227 GetFontFamily() != attr
.GetFontFamily())
230 if ((flags
& wxTEXT_ATTR_URL
) && GetURL() != attr
.GetURL())
233 if ((flags
& wxTEXT_ATTR_ALIGNMENT
) && GetAlignment() != attr
.GetAlignment())
236 if ((flags
& wxTEXT_ATTR_LEFT_INDENT
) &&
237 ((GetLeftIndent() != attr
.GetLeftIndent()) || (GetLeftSubIndent() != attr
.GetLeftSubIndent())))
240 if ((flags
& wxTEXT_ATTR_RIGHT_INDENT
) &&
241 (GetRightIndent() != attr
.GetRightIndent()))
244 if ((flags
& wxTEXT_ATTR_PARA_SPACING_AFTER
) &&
245 (GetParagraphSpacingAfter() != attr
.GetParagraphSpacingAfter()))
248 if ((flags
& wxTEXT_ATTR_PARA_SPACING_BEFORE
) &&
249 (GetParagraphSpacingBefore() != attr
.GetParagraphSpacingBefore()))
252 if ((flags
& wxTEXT_ATTR_LINE_SPACING
) &&
253 (GetLineSpacing() != attr
.GetLineSpacing()))
256 if ((flags
& wxTEXT_ATTR_CHARACTER_STYLE_NAME
) &&
257 (GetCharacterStyleName() != attr
.GetCharacterStyleName()))
260 if ((flags
& wxTEXT_ATTR_PARAGRAPH_STYLE_NAME
) &&
261 (GetParagraphStyleName() != attr
.GetParagraphStyleName()))
264 if ((flags
& wxTEXT_ATTR_LIST_STYLE_NAME
) &&
265 (GetListStyleName() != attr
.GetListStyleName()))
268 if ((flags
& wxTEXT_ATTR_BULLET_STYLE
) &&
269 (GetBulletStyle() != attr
.GetBulletStyle()))
272 if ((flags
& wxTEXT_ATTR_BULLET_NUMBER
) &&
273 (GetBulletNumber() != attr
.GetBulletNumber()))
276 if ((flags
& wxTEXT_ATTR_BULLET_TEXT
) &&
277 (GetBulletText() != attr
.GetBulletText()) &&
278 (GetBulletFont() != attr
.GetBulletFont()))
281 if ((flags
& wxTEXT_ATTR_BULLET_NAME
) &&
282 (GetBulletName() != attr
.GetBulletName()))
285 if ((flags
& wxTEXT_ATTR_TABS
) &&
286 !TabsEq(GetTabs(), attr
.GetTabs()))
289 if ((flags
& wxTEXT_ATTR_PAGE_BREAK
) &&
290 (HasPageBreak() != attr
.HasPageBreak()))
293 if (flags
& wxTEXT_ATTR_EFFECTS
)
295 if (HasTextEffects() != attr
.HasTextEffects())
297 if (!BitlistsEqPartial(GetTextEffects(), attr
.GetTextEffects(), attr
.GetTextEffectFlags()))
301 if ((flags
& wxTEXT_ATTR_OUTLINE_LEVEL
) &&
302 (GetOutlineLevel() != attr
.GetOutlineLevel()))
308 // Create font from font attributes.
309 wxFont
wxTextAttr::GetFont() const
316 fontSize
= GetFontSize();
318 int fontStyle
= wxNORMAL
;
320 fontStyle
= GetFontStyle();
322 int fontWeight
= wxNORMAL
;
324 fontWeight
= GetFontWeight();
326 bool underlined
= false;
327 if (HasFontUnderlined())
328 underlined
= GetFontUnderlined();
330 wxString fontFaceName
;
331 if (HasFontFaceName())
332 fontFaceName
= GetFontFaceName();
334 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
;
335 if (HasFontEncoding())
336 encoding
= GetFontEncoding();
338 wxFontFamily fontFamily
= wxFONTFAMILY_DEFAULT
;
340 fontFamily
= GetFontFamily();
342 wxFont
font(fontSize
, fontFamily
, fontStyle
, fontWeight
, underlined
, fontFaceName
, encoding
);
346 // Get attributes from font.
347 bool wxTextAttr::GetFontAttributes(const wxFont
& font
, int flags
)
352 if (flags
& wxTEXT_ATTR_FONT_SIZE
)
353 m_fontSize
= font
.GetPointSize();
355 if (flags
& wxTEXT_ATTR_FONT_ITALIC
)
356 m_fontStyle
= font
.GetStyle();
358 if (flags
& wxTEXT_ATTR_FONT_WEIGHT
)
359 m_fontWeight
= font
.GetWeight();
361 if (flags
& wxTEXT_ATTR_FONT_UNDERLINE
)
362 m_fontUnderlined
= font
.GetUnderlined();
364 if (flags
& wxTEXT_ATTR_FONT_FACE
)
365 m_fontFaceName
= font
.GetFaceName();
367 if (flags
& wxTEXT_ATTR_FONT_ENCODING
)
368 m_fontEncoding
= font
.GetEncoding();
370 if (flags
& wxTEXT_ATTR_FONT_FAMILY
)
372 // wxFont might not know its family, avoid setting m_fontFamily to an
373 // invalid value and rather pretend that we don't have any font family
374 // information at all in this case
375 const wxFontFamily fontFamily
= font
.GetFamily();
376 if ( fontFamily
== wxFONTFAMILY_UNKNOWN
)
377 flags
&= ~wxTEXT_ATTR_FONT_FAMILY
;
379 m_fontFamily
= fontFamily
;
387 // Resets bits in destination so new attributes aren't merged with mutually exclusive ones
388 static bool wxResetIncompatibleBits(const int mask
, const int srcFlags
, int& destFlags
, int& destBits
)
390 if ((srcFlags
& mask
) && (destFlags
& mask
))
399 bool wxTextAttr::Apply(const wxTextAttr
& style
, const wxTextAttr
* compareWith
)
401 wxTextAttr
& destStyle
= (*this);
403 if (style
.HasFontWeight())
405 if (!(compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight()))
406 destStyle
.SetFontWeight(style
.GetFontWeight());
409 if (style
.HasFontSize())
411 if (!(compareWith
&& compareWith
->HasFontSize() && compareWith
->GetFontSize() == style
.GetFontSize()))
412 destStyle
.SetFontSize(style
.GetFontSize());
415 if (style
.HasFontItalic())
417 if (!(compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle()))
418 destStyle
.SetFontStyle(style
.GetFontStyle());
421 if (style
.HasFontUnderlined())
423 if (!(compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined()))
424 destStyle
.SetFontUnderlined(style
.GetFontUnderlined());
427 if (style
.HasFontFaceName())
429 if (!(compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName()))
430 destStyle
.SetFontFaceName(style
.GetFontFaceName());
433 if (style
.HasFontEncoding())
435 if (!(compareWith
&& compareWith
->HasFontEncoding() && compareWith
->GetFontEncoding() == style
.GetFontEncoding()))
436 destStyle
.SetFontEncoding(style
.GetFontEncoding());
439 if (style
.HasFontFamily())
441 if (!(compareWith
&& compareWith
->HasFontFamily() && compareWith
->GetFontFamily() == style
.GetFontFamily()))
442 destStyle
.SetFontFamily(style
.GetFontFamily());
445 if (style
.GetTextColour().Ok() && style
.HasTextColour())
447 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
448 destStyle
.SetTextColour(style
.GetTextColour());
451 if (style
.GetBackgroundColour().Ok() && style
.HasBackgroundColour())
453 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
454 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
457 if (style
.HasAlignment())
459 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
460 destStyle
.SetAlignment(style
.GetAlignment());
465 if (!(compareWith
&& compareWith
->HasTabs() && TabsEq(compareWith
->GetTabs(), style
.GetTabs())))
466 destStyle
.SetTabs(style
.GetTabs());
469 if (style
.HasLeftIndent())
471 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
472 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
473 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
476 if (style
.HasRightIndent())
478 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
479 destStyle
.SetRightIndent(style
.GetRightIndent());
482 if (style
.HasParagraphSpacingAfter())
484 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
485 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
488 if (style
.HasParagraphSpacingBefore())
490 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
491 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
494 if (style
.HasLineSpacing())
496 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
497 destStyle
.SetLineSpacing(style
.GetLineSpacing());
500 if (style
.HasCharacterStyleName())
502 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
503 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
506 if (style
.HasParagraphStyleName())
508 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
509 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
512 if (style
.HasListStyleName())
514 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
515 destStyle
.SetListStyleName(style
.GetListStyleName());
518 if (style
.HasBulletStyle())
520 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
521 destStyle
.SetBulletStyle(style
.GetBulletStyle());
524 if (style
.HasBulletText())
526 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
528 destStyle
.SetBulletText(style
.GetBulletText());
529 destStyle
.SetBulletFont(style
.GetBulletFont());
533 if (style
.HasBulletNumber())
535 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
536 destStyle
.SetBulletNumber(style
.GetBulletNumber());
539 if (style
.HasBulletName())
541 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
542 destStyle
.SetBulletName(style
.GetBulletName());
547 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
548 destStyle
.SetURL(style
.GetURL());
551 if (style
.HasPageBreak())
553 if (!(compareWith
&& compareWith
->HasPageBreak()))
554 destStyle
.SetPageBreak();
557 if (style
.HasTextEffects())
559 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
561 int destBits
= destStyle
.GetTextEffects();
562 int destFlags
= destStyle
.GetTextEffectFlags();
564 int srcBits
= style
.GetTextEffects();
565 int srcFlags
= style
.GetTextEffectFlags();
567 // Reset incompatible bits in the destination
568 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_SUPERSCRIPT
|wxTEXT_ATTR_EFFECT_SUBSCRIPT
), srcFlags
, destFlags
, destBits
);
569 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_CAPITALS
|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS
), srcFlags
, destFlags
, destBits
);
570 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_DOUBLE_STRIKETHROUGH
), srcFlags
, destFlags
, destBits
);
572 CombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
574 destStyle
.SetTextEffects(destBits
);
575 destStyle
.SetTextEffectFlags(destFlags
);
579 if (style
.HasOutlineLevel())
581 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
582 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
589 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
590 const wxTextAttr
& attrDef
,
591 const wxTextCtrlBase
*text
)
595 font
= attr
.GetFont();
599 if (attrDef
.HasFont())
600 font
= attrDef
.GetFont();
602 if ( text
&& !font
.Ok() )
603 font
= text
->GetFont();
606 wxColour colFg
= attr
.GetTextColour();
609 colFg
= attrDef
.GetTextColour();
611 if ( text
&& !colFg
.Ok() )
612 colFg
= text
->GetForegroundColour();
615 wxColour colBg
= attr
.GetBackgroundColour();
618 colBg
= attrDef
.GetBackgroundColour();
620 if ( text
&& !colBg
.Ok() )
621 colBg
= text
->GetBackgroundColour();
624 wxTextAttr
newAttr(colFg
, colBg
, font
);
626 if (attr
.HasAlignment())
627 newAttr
.SetAlignment(attr
.GetAlignment());
628 else if (attrDef
.HasAlignment())
629 newAttr
.SetAlignment(attrDef
.GetAlignment());
632 newAttr
.SetTabs(attr
.GetTabs());
633 else if (attrDef
.HasTabs())
634 newAttr
.SetTabs(attrDef
.GetTabs());
636 if (attr
.HasLeftIndent())
637 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
638 else if (attrDef
.HasLeftIndent())
639 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
641 if (attr
.HasRightIndent())
642 newAttr
.SetRightIndent(attr
.GetRightIndent());
643 else if (attrDef
.HasRightIndent())
644 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
650 bool wxTextAttr::TabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
652 if (tabs1
.GetCount() != tabs2
.GetCount())
656 for (i
= 0; i
< tabs1
.GetCount(); i
++)
658 if (tabs1
[i
] != tabs2
[i
])
665 bool wxTextAttr::RemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
667 int flags
= style
.GetFlags();
668 int destFlags
= destStyle
.GetFlags();
670 destStyle
.SetFlags(destFlags
& ~flags
);
675 /// Combine two bitlists, specifying the bits of interest with separate flags.
676 bool wxTextAttr::CombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
678 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
679 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
682 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
683 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
684 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
686 // Now combine the 1 bits.
687 int valueA3
= (valueB
& flagsB
) | valueA2
;
690 flagsA
= (flagsA
| flagsB
);
695 /// Compare two bitlists
696 bool wxTextAttr::BitlistsEqPartial(int valueA
, int valueB
, int flags
)
698 int relevantBitsA
= valueA
& flags
;
699 int relevantBitsB
= valueB
& flags
;
700 return (relevantBitsA
!= relevantBitsB
);
703 /// Split into paragraph and character styles
704 bool wxTextAttr::SplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
706 wxTextAttr
defaultCharStyle1(style
);
707 wxTextAttr
defaultParaStyle1(style
);
708 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
709 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
711 charStyle
.Apply(defaultCharStyle1
);
712 parStyle
.Apply(defaultParaStyle1
);
717 // apply styling to text range
718 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
719 const wxTextAttr
& WXUNUSED(style
))
721 // to be implemented in derived classes
725 // get the styling at the given position
726 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position
), wxTextAttr
& WXUNUSED(style
))
728 // to be implemented in derived classes
732 // change default text attributes
733 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
735 // keep the old attributes if the new style doesn't specify them unless the
736 // new style is empty - then reset m_defaultStyle (as there is no other way
738 if ( style
.IsDefault() )
739 m_defaultStyle
= style
;
741 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
746 // ----------------------------------------------------------------------------
748 // ----------------------------------------------------------------------------
750 bool wxTextAreaBase::DoLoadFile(const wxString
& filename
, int WXUNUSED(fileType
))
753 wxFFile
file(filename
);
754 if ( file
.IsOpened() )
757 if ( file
.ReadAll(&text
) )
764 #endif // wxUSE_FFILE
769 bool wxTextCtrlBase::DoLoadFile(const wxString
& filename
, int fileType
)
771 if ( wxTextAreaBase::DoLoadFile(filename
, fileType
) )
774 m_filename
= filename
;
777 wxLogError(_("File couldn't be loaded."));
781 bool wxTextAreaBase::DoSaveFile(const wxString
& filename
, int WXUNUSED(fileType
))
784 wxFFile
file(filename
, wxT("w"));
785 return file
.IsOpened() && file
.Write(GetValue(), *wxConvCurrent
);
788 #endif // wxUSE_FFILE
791 bool wxTextAreaBase::SaveFile(const wxString
& filename
, int fileType
)
793 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
794 if ( filenameToUse
.empty() )
796 // what kind of message to give? is it an error or a program bug?
797 wxLogDebug(wxT("Can't save textctrl to file without filename."));
802 return DoSaveFile(filenameToUse
, fileType
);
805 bool wxTextCtrlBase::DoSaveFile(const wxString
& filename
, int fileType
)
807 if ( wxTextAreaBase::DoSaveFile(filename
, fileType
) )
809 // if it worked, save for future calls
810 m_filename
= filename
;
812 // it's not modified any longer
820 // ----------------------------------------------------------------------------
821 // stream-like insertion operator
822 // ----------------------------------------------------------------------------
824 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
827 return *TEXTCTRL(this);
830 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
832 return *this << wxString::Format("%.2f", d
);
835 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
837 return *this << wxString::Format("%d", i
);
840 wxTextCtrl
& wxTextCtrlBase::operator<<(long l
)
842 return *this << wxString::Format("%ld", l
);
845 // ----------------------------------------------------------------------------
846 // streambuf methods implementation
847 // ----------------------------------------------------------------------------
849 #if wxHAS_TEXT_WINDOW_STREAM
851 int wxTextCtrlBase::overflow(int c
)
853 AppendText((wxChar
)c
);
855 // return something different from EOF
859 #endif // wxHAS_TEXT_WINDOW_STREAM
861 // ----------------------------------------------------------------------------
862 // emulating key presses
863 // ----------------------------------------------------------------------------
865 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
867 // we have a native implementation for Win32 and so don't need this one
870 int keycode
= event
.GetKeyCode();
883 ch
= (wxChar
)(wxT('0') + keycode
- WXK_NUMPAD0
);
887 case WXK_NUMPAD_MULTIPLY
:
897 case WXK_NUMPAD_SUBTRACT
:
902 case WXK_NUMPAD_DECIMAL
:
907 case WXK_NUMPAD_DIVIDE
:
912 case WXK_NUMPAD_DELETE
:
913 // delete the character at cursor
915 const long pos
= GetInsertionPoint();
916 if ( pos
< GetLastPosition() )
917 Remove(pos
, pos
+ 1);
922 // delete the character before the cursor
924 const long pos
= GetInsertionPoint();
926 Remove(pos
- 1, pos
);
932 if ( event
.GetUnicodeKey() )
934 ch
= event
.GetUnicodeKey();
938 if ( keycode
< 256 && keycode
>= 0 && wxIsprint(keycode
) )
940 // FIXME this is not going to work for non letters...
941 if ( !event
.ShiftDown() )
943 keycode
= wxTolower(keycode
);
946 ch
= (wxChar
)keycode
;
962 #endif // !__WIN32__/__WIN32__
967 // do the window-specific processing after processing the update event
968 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
970 // call inherited, but skip the wxControl's version, and call directly the
971 // wxWindow's one instead, because the only reason why we are overriding this
972 // function is that we want to use SetValue() instead of wxControl::SetLabel()
973 wxWindowBase::DoUpdateWindowUI(event
);
976 if ( event
.GetSetText() )
978 if ( event
.GetText() != GetValue() )
979 SetValue(event
.GetText());
983 // ----------------------------------------------------------------------------
985 // ----------------------------------------------------------------------------
987 wxTextCtrlHitTestResult
988 wxTextAreaBase::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
990 // implement in terms of the other overload as the native ports typically
991 // can get the position and not (x, y) pair directly (although wxUniv
992 // directly gets x and y -- and so overrides this method as well)
994 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
996 if ( rc
!= wxTE_HT_UNKNOWN
)
998 PositionToXY(pos
, x
, y
);
1004 wxTextCtrlHitTestResult
1005 wxTextAreaBase::HitTest(const wxPoint
& WXUNUSED(pt
), long * WXUNUSED(pos
)) const
1008 return wxTE_HT_UNKNOWN
;
1011 #else // !wxUSE_TEXTCTRL
1013 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
1014 // controls (wxComboBox and wxSpinCtrl)
1016 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
);
1018 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL