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 extern WXDLLEXPORT_DATA(const char) wxTextCtrlNameStr
[] = "text";
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // we don't have any objects of type wxTextCtrlBase in the program, only
45 // wxTextCtrl, so this cast is safe
46 #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 wxDEFINE_FLAGS( wxTextCtrlStyle
)
57 wxBEGIN_FLAGS( wxTextCtrlStyle
)
58 // new style border flags, we put them first to
59 // use them for streaming out
60 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
61 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
62 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
63 wxFLAGS_MEMBER(wxBORDER_RAISED
)
64 wxFLAGS_MEMBER(wxBORDER_STATIC
)
65 wxFLAGS_MEMBER(wxBORDER_NONE
)
67 // old style border flags
68 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
69 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
70 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
71 wxFLAGS_MEMBER(wxRAISED_BORDER
)
72 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
73 wxFLAGS_MEMBER(wxBORDER
)
75 // standard window styles
76 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
77 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
78 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
79 wxFLAGS_MEMBER(wxWANTS_CHARS
)
80 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
81 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
82 wxFLAGS_MEMBER(wxVSCROLL
)
83 wxFLAGS_MEMBER(wxHSCROLL
)
85 wxFLAGS_MEMBER(wxTE_PROCESS_ENTER
)
86 wxFLAGS_MEMBER(wxTE_PROCESS_TAB
)
87 wxFLAGS_MEMBER(wxTE_MULTILINE
)
88 wxFLAGS_MEMBER(wxTE_PASSWORD
)
89 wxFLAGS_MEMBER(wxTE_READONLY
)
90 wxFLAGS_MEMBER(wxHSCROLL
)
91 wxFLAGS_MEMBER(wxTE_RICH
)
92 wxFLAGS_MEMBER(wxTE_RICH2
)
93 wxFLAGS_MEMBER(wxTE_AUTO_URL
)
94 wxFLAGS_MEMBER(wxTE_NOHIDESEL
)
95 wxFLAGS_MEMBER(wxTE_LEFT
)
96 wxFLAGS_MEMBER(wxTE_CENTRE
)
97 wxFLAGS_MEMBER(wxTE_RIGHT
)
98 wxFLAGS_MEMBER(wxTE_DONTWRAP
)
99 wxFLAGS_MEMBER(wxTE_CHARWRAP
)
100 wxFLAGS_MEMBER(wxTE_WORDWRAP
)
101 wxEND_FLAGS( wxTextCtrlStyle
)
103 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl
, wxControl
, "wx/textctrl.h")
105 wxBEGIN_PROPERTIES_TABLE(wxTextCtrl
)
106 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
107 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
109 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, wxEMPTY_PARAMETER_VALUE
, \
110 0 /*flags*/, wxT("Helpstring"), wxT("group") )
111 wxPROPERTY( Value
, wxString
, SetValue
, GetValue
, wxString(), \
112 0 /*flags*/, wxT("Helpstring"), wxT("group"))
114 wxPROPERTY_FLAGS( WindowStyle
, wxTextCtrlStyle
, long, SetWindowStyleFlag
, \
115 GetWindowStyleFlag
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
116 wxT("Helpstring"), wxT("group")) // style
117 wxEND_PROPERTIES_TABLE()
119 wxEMPTY_HANDLERS_TABLE(wxTextCtrl
)
121 wxCONSTRUCTOR_6( wxTextCtrl
, wxWindow
*, Parent
, wxWindowID
, Id
, \
122 wxString
, Value
, wxPoint
, Position
, wxSize
, Size
, \
126 IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent
, wxCommandEvent
)
128 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
);
129 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
);
130 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_URL
, wxTextUrlEvent
);
131 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_MAXLEN
, wxCommandEvent
);
133 IMPLEMENT_ABSTRACT_CLASS(wxTextCtrlBase
, wxControl
)
135 // ============================================================================
136 // wxTextAttr implementation
137 // ============================================================================
139 wxTextAttr::wxTextAttr(const wxColour
& colText
,
140 const wxColour
& colBack
,
142 wxTextAttrAlignment alignment
): m_textAlignment(alignment
), m_colText(colText
), m_colBack(colBack
)
146 if (m_colText
.IsOk()) m_flags
|= wxTEXT_ATTR_TEXT_COLOUR
;
147 if (m_colBack
.IsOk()) m_flags
|= wxTEXT_ATTR_BACKGROUND_COLOUR
;
148 if (alignment
!= wxTEXT_ALIGNMENT_DEFAULT
)
149 m_flags
|= wxTEXT_ATTR_ALIGNMENT
;
151 GetFontAttributes(font
);
155 void wxTextAttr::Init()
157 m_textAlignment
= wxTEXT_ALIGNMENT_DEFAULT
;
164 m_fontStyle
= wxFONTSTYLE_NORMAL
;
165 m_fontWeight
= wxFONTWEIGHT_NORMAL
;
166 m_fontUnderlined
= false;
167 m_fontStrikethrough
= false;
168 m_fontEncoding
= wxFONTENCODING_DEFAULT
;
169 m_fontFamily
= wxFONTFAMILY_DEFAULT
;
171 m_paragraphSpacingAfter
= 0;
172 m_paragraphSpacingBefore
= 0;
174 m_bulletStyle
= wxTEXT_ATTR_BULLET_STYLE_NONE
;
175 m_textEffects
= wxTEXT_ATTR_EFFECT_NONE
;
176 m_textEffectFlags
= wxTEXT_ATTR_EFFECT_NONE
;
182 void wxTextAttr::Copy(const wxTextAttr
& attr
)
184 m_colText
= attr
.m_colText
;
185 m_colBack
= attr
.m_colBack
;
186 m_textAlignment
= attr
.m_textAlignment
;
187 m_leftIndent
= attr
.m_leftIndent
;
188 m_leftSubIndent
= attr
.m_leftSubIndent
;
189 m_rightIndent
= attr
.m_rightIndent
;
190 m_tabs
= attr
.m_tabs
;
191 m_flags
= attr
.m_flags
;
193 m_fontSize
= attr
.m_fontSize
;
194 m_fontStyle
= attr
.m_fontStyle
;
195 m_fontWeight
= attr
.m_fontWeight
;
196 m_fontUnderlined
= attr
.m_fontUnderlined
;
197 m_fontStrikethrough
= attr
.m_fontStrikethrough
;
198 m_fontFaceName
= attr
.m_fontFaceName
;
199 m_fontEncoding
= attr
.m_fontEncoding
;
200 m_fontFamily
= attr
.m_fontFamily
;
201 m_textEffects
= attr
.m_textEffects
;
202 m_textEffectFlags
= attr
.m_textEffectFlags
;
204 m_paragraphSpacingAfter
= attr
.m_paragraphSpacingAfter
;
205 m_paragraphSpacingBefore
= attr
.m_paragraphSpacingBefore
;
206 m_lineSpacing
= attr
.m_lineSpacing
;
207 m_characterStyleName
= attr
.m_characterStyleName
;
208 m_paragraphStyleName
= attr
.m_paragraphStyleName
;
209 m_listStyleName
= attr
.m_listStyleName
;
210 m_bulletStyle
= attr
.m_bulletStyle
;
211 m_bulletNumber
= attr
.m_bulletNumber
;
212 m_bulletText
= attr
.m_bulletText
;
213 m_bulletFont
= attr
.m_bulletFont
;
214 m_bulletName
= attr
.m_bulletName
;
215 m_outlineLevel
= attr
.m_outlineLevel
;
217 m_urlTarget
= attr
.m_urlTarget
;
221 void wxTextAttr::operator= (const wxTextAttr
& attr
)
227 bool wxTextAttr::operator== (const wxTextAttr
& attr
) const
229 return GetFlags() == attr
.GetFlags() &&
231 GetTextColour() == attr
.GetTextColour() &&
232 GetBackgroundColour() == attr
.GetBackgroundColour() &&
234 GetAlignment() == attr
.GetAlignment() &&
235 GetLeftIndent() == attr
.GetLeftIndent() &&
236 GetLeftSubIndent() == attr
.GetLeftSubIndent() &&
237 GetRightIndent() == attr
.GetRightIndent() &&
238 TabsEq(GetTabs(), attr
.GetTabs()) &&
240 GetParagraphSpacingAfter() == attr
.GetParagraphSpacingAfter() &&
241 GetParagraphSpacingBefore() == attr
.GetParagraphSpacingBefore() &&
242 GetLineSpacing() == attr
.GetLineSpacing() &&
243 GetCharacterStyleName() == attr
.GetCharacterStyleName() &&
244 GetParagraphStyleName() == attr
.GetParagraphStyleName() &&
245 GetListStyleName() == attr
.GetListStyleName() &&
247 GetBulletStyle() == attr
.GetBulletStyle() &&
248 GetBulletText() == attr
.GetBulletText() &&
249 GetBulletNumber() == attr
.GetBulletNumber() &&
250 GetBulletFont() == attr
.GetBulletFont() &&
251 GetBulletName() == attr
.GetBulletName() &&
253 GetTextEffects() == attr
.GetTextEffects() &&
254 GetTextEffectFlags() == attr
.GetTextEffectFlags() &&
256 GetOutlineLevel() == attr
.GetOutlineLevel() &&
258 GetFontSize() == attr
.GetFontSize() &&
259 GetFontStyle() == attr
.GetFontStyle() &&
260 GetFontWeight() == attr
.GetFontWeight() &&
261 GetFontUnderlined() == attr
.GetFontUnderlined() &&
262 GetFontFaceName() == attr
.GetFontFaceName() &&
263 GetFontEncoding() == attr
.GetFontEncoding() &&
264 GetFontFamily() == attr
.GetFontFamily() &&
266 GetURL() == attr
.GetURL();
269 // Partial equality test. Only returns false if an attribute doesn't match.
270 bool wxTextAttr::EqPartial(const wxTextAttr
& attr
, bool weakTest
) const
272 int flags
= attr
.GetFlags();
275 ((!HasTextColour() && attr
.HasTextColour()) ||
276 (!HasBackgroundColour() && attr
.HasBackgroundColour()) ||
277 (!HasFontFaceName() && attr
.HasFontFaceName()) ||
278 (!HasFontSize() && attr
.HasFontSize()) ||
279 (!HasFontWeight() && attr
.HasFontWeight()) ||
280 (!HasFontItalic() && attr
.HasFontItalic()) ||
281 (!HasFontUnderlined() && attr
.HasFontUnderlined()) ||
282 (!HasFontStrikethrough() && attr
.HasFontStrikethrough()) ||
283 (!HasFontEncoding() && attr
.HasFontEncoding()) ||
284 (!HasFontFamily() && attr
.HasFontFamily()) ||
285 (!HasURL() && attr
.HasURL()) ||
286 (!HasAlignment() && attr
.HasAlignment()) ||
287 (!HasLeftIndent() && attr
.HasLeftIndent()) ||
288 (!HasParagraphSpacingAfter() && attr
.HasParagraphSpacingAfter()) ||
289 (!HasParagraphSpacingBefore() && attr
.HasParagraphSpacingBefore()) ||
290 (!HasLineSpacing() && attr
.HasLineSpacing()) ||
291 (!HasCharacterStyleName() && attr
.HasCharacterStyleName()) ||
292 (!HasParagraphStyleName() && attr
.HasParagraphStyleName()) ||
293 (!HasListStyleName() && attr
.HasListStyleName()) ||
294 (!HasBulletStyle() && attr
.HasBulletStyle()) ||
295 (!HasBulletNumber() && attr
.HasBulletNumber()) ||
296 (!HasBulletText() && attr
.HasBulletText()) ||
297 (!HasBulletName() && attr
.HasBulletName()) ||
298 (!HasTabs() && attr
.HasTabs()) ||
299 (!HasTextEffects() && attr
.HasTextEffects()) ||
300 (!HasOutlineLevel() && attr
.HasOutlineLevel())))
305 if (HasTextColour() && attr
.HasTextColour() && GetTextColour() != attr
.GetTextColour())
308 if (HasBackgroundColour() && attr
.HasBackgroundColour() && GetBackgroundColour() != attr
.GetBackgroundColour())
311 if (HasFontFaceName() && attr
.HasFontFaceName() && GetFontFaceName() != attr
.GetFontFaceName())
314 // This checks whether the two objects have the same font size dimension (px versus pt)
315 if (HasFontSize() && attr
.HasFontSize() && (flags
& wxTEXT_ATTR_FONT
) != (GetFlags() & wxTEXT_ATTR_FONT
))
318 if (HasFontPointSize() && attr
.HasFontPointSize() && GetFontSize() != attr
.GetFontSize())
321 if (HasFontPixelSize() && attr
.HasFontPixelSize() && GetFontSize() != attr
.GetFontSize())
324 if (HasFontWeight() && attr
.HasFontWeight() && GetFontWeight() != attr
.GetFontWeight())
327 if (HasFontItalic() && attr
.HasFontItalic() && GetFontStyle() != attr
.GetFontStyle())
330 if (HasFontUnderlined() && attr
.HasFontUnderlined() && GetFontUnderlined() != attr
.GetFontUnderlined())
333 if (HasFontStrikethrough() && attr
.HasFontStrikethrough() && GetFontStrikethrough() != attr
.GetFontStrikethrough())
336 if (HasFontEncoding() && attr
.HasFontEncoding() && GetFontEncoding() != attr
.GetFontEncoding())
339 if (HasFontFamily() && attr
.HasFontFamily() && GetFontFamily() != attr
.GetFontFamily())
342 if (HasURL() && attr
.HasURL() && GetURL() != attr
.GetURL())
345 if (HasAlignment() && attr
.HasAlignment() && GetAlignment() != attr
.GetAlignment())
348 if (HasLeftIndent() && attr
.HasLeftIndent() &&
349 ((GetLeftIndent() != attr
.GetLeftIndent()) || (GetLeftSubIndent() != attr
.GetLeftSubIndent())))
352 if (HasRightIndent() && attr
.HasRightIndent() && (GetRightIndent() != attr
.GetRightIndent()))
355 if (HasParagraphSpacingAfter() && attr
.HasParagraphSpacingAfter() &&
356 (GetParagraphSpacingAfter() != attr
.GetParagraphSpacingAfter()))
359 if (HasParagraphSpacingBefore() && attr
.HasParagraphSpacingBefore() &&
360 (GetParagraphSpacingBefore() != attr
.GetParagraphSpacingBefore()))
363 if (HasLineSpacing() && attr
.HasLineSpacing() && (GetLineSpacing() != attr
.GetLineSpacing()))
366 if (HasCharacterStyleName() && attr
.HasCharacterStyleName() && (GetCharacterStyleName() != attr
.GetCharacterStyleName()))
369 if (HasParagraphStyleName() && attr
.HasParagraphStyleName() && (GetParagraphStyleName() != attr
.GetParagraphStyleName()))
372 if (HasListStyleName() && attr
.HasListStyleName() && (GetListStyleName() != attr
.GetListStyleName()))
375 if (HasBulletStyle() && attr
.HasBulletStyle() && (GetBulletStyle() != attr
.GetBulletStyle()))
378 if (HasBulletNumber() && attr
.HasBulletNumber() && (GetBulletNumber() != attr
.GetBulletNumber()))
381 if (HasBulletText() && attr
.HasBulletText() &&
382 (GetBulletText() != attr
.GetBulletText()) &&
383 (GetBulletFont() != attr
.GetBulletFont()))
386 if (HasBulletName() && attr
.HasBulletName() && (GetBulletName() != attr
.GetBulletName()))
389 if (HasTabs() && attr
.HasTabs() && !TabsEq(GetTabs(), attr
.GetTabs()))
392 if ((HasPageBreak() != attr
.HasPageBreak()))
395 if (HasTextEffects() && attr
.HasTextEffects())
397 if (!BitlistsEqPartial(GetTextEffects(), attr
.GetTextEffects(), attr
.GetTextEffectFlags()))
401 if (HasOutlineLevel() && attr
.HasOutlineLevel() && (GetOutlineLevel() != attr
.GetOutlineLevel()))
407 // Create font from font attributes.
408 wxFont
wxTextAttr::GetFont() const
415 fontSize
= GetFontSize();
417 wxFontStyle fontStyle
= wxFONTSTYLE_NORMAL
;
419 fontStyle
= GetFontStyle();
421 wxFontWeight fontWeight
= wxFONTWEIGHT_NORMAL
;
423 fontWeight
= GetFontWeight();
425 bool underlined
= false;
426 if (HasFontUnderlined())
427 underlined
= GetFontUnderlined();
429 bool strikethrough
= false;
430 if (HasFontStrikethrough())
431 strikethrough
= GetFontStrikethrough();
433 wxString fontFaceName
;
434 if (HasFontFaceName())
435 fontFaceName
= GetFontFaceName();
437 wxFontEncoding encoding
= wxFONTENCODING_DEFAULT
;
438 if (HasFontEncoding())
439 encoding
= GetFontEncoding();
441 wxFontFamily fontFamily
= wxFONTFAMILY_DEFAULT
;
443 fontFamily
= GetFontFamily();
445 if (HasFontPixelSize())
447 wxFont
font(wxSize(0, fontSize
), fontFamily
, fontStyle
, fontWeight
, underlined
, fontFaceName
, encoding
);
449 font
.SetStrikethrough(true);
454 wxFont
font(fontSize
, fontFamily
, fontStyle
, fontWeight
, underlined
, fontFaceName
, encoding
);
456 font
.SetStrikethrough(true);
461 // Get attributes from font.
462 bool wxTextAttr::GetFontAttributes(const wxFont
& font
, int flags
)
467 // If we pass both pixel and point size attributes, this is an indication
468 // to choose the most appropriate units.
469 if ((flags
& wxTEXT_ATTR_FONT
) == wxTEXT_ATTR_FONT
)
471 if (font
.IsUsingSizeInPixels())
473 m_fontSize
= font
.GetPixelSize().y
;
474 flags
&= ~wxTEXT_ATTR_FONT_POINT_SIZE
;
478 m_fontSize
= font
.GetPointSize();
479 flags
&= ~wxTEXT_ATTR_FONT_PIXEL_SIZE
;
482 else if (flags
& wxTEXT_ATTR_FONT_POINT_SIZE
)
484 m_fontSize
= font
.GetPointSize();
485 flags
&= ~wxTEXT_ATTR_FONT_PIXEL_SIZE
;
487 else if (flags
& wxTEXT_ATTR_FONT_PIXEL_SIZE
)
489 m_fontSize
= font
.GetPixelSize().y
;
492 if (flags
& wxTEXT_ATTR_FONT_ITALIC
)
493 m_fontStyle
= font
.GetStyle();
495 if (flags
& wxTEXT_ATTR_FONT_WEIGHT
)
496 m_fontWeight
= font
.GetWeight();
498 if (flags
& wxTEXT_ATTR_FONT_UNDERLINE
)
499 m_fontUnderlined
= font
.GetUnderlined();
501 if (flags
& wxTEXT_ATTR_FONT_STRIKETHROUGH
)
502 m_fontStrikethrough
= font
.GetStrikethrough();
504 if (flags
& wxTEXT_ATTR_FONT_FACE
)
505 m_fontFaceName
= font
.GetFaceName();
507 if (flags
& wxTEXT_ATTR_FONT_ENCODING
)
508 m_fontEncoding
= font
.GetEncoding();
510 if (flags
& wxTEXT_ATTR_FONT_FAMILY
)
512 // wxFont might not know its family, avoid setting m_fontFamily to an
513 // invalid value and rather pretend that we don't have any font family
514 // information at all in this case
515 const wxFontFamily fontFamily
= font
.GetFamily();
516 if ( fontFamily
== wxFONTFAMILY_UNKNOWN
)
517 flags
&= ~wxTEXT_ATTR_FONT_FAMILY
;
519 m_fontFamily
= fontFamily
;
527 // Resets bits in destination so new attributes aren't merged with mutually exclusive ones
528 static bool wxResetIncompatibleBits(const int mask
, const int srcFlags
, int& destFlags
, int& destBits
)
530 if ((srcFlags
& mask
) && (destFlags
& mask
))
539 bool wxTextAttr::Apply(const wxTextAttr
& style
, const wxTextAttr
* compareWith
)
541 wxTextAttr
& destStyle
= (*this);
543 if (style
.HasFontWeight())
545 if (!(compareWith
&& compareWith
->HasFontWeight() && compareWith
->GetFontWeight() == style
.GetFontWeight()))
546 destStyle
.SetFontWeight(style
.GetFontWeight());
549 if (style
.HasFontPointSize())
551 if (!(compareWith
&& compareWith
->HasFontPointSize() && compareWith
->GetFontSize() == style
.GetFontSize()))
552 destStyle
.SetFontPointSize(style
.GetFontSize());
554 else if (style
.HasFontPixelSize())
556 if (!(compareWith
&& compareWith
->HasFontPixelSize() && compareWith
->GetFontSize() == style
.GetFontSize()))
557 destStyle
.SetFontPixelSize(style
.GetFontSize());
560 if (style
.HasFontItalic())
562 if (!(compareWith
&& compareWith
->HasFontItalic() && compareWith
->GetFontStyle() == style
.GetFontStyle()))
563 destStyle
.SetFontStyle(style
.GetFontStyle());
566 if (style
.HasFontUnderlined())
568 if (!(compareWith
&& compareWith
->HasFontUnderlined() && compareWith
->GetFontUnderlined() == style
.GetFontUnderlined()))
569 destStyle
.SetFontUnderlined(style
.GetFontUnderlined());
572 if (style
.HasFontStrikethrough())
574 if (!(compareWith
&& compareWith
->HasFontStrikethrough() && compareWith
->GetFontStrikethrough() == style
.GetFontStrikethrough()))
575 destStyle
.SetFontStrikethrough(style
.GetFontStrikethrough());
578 if (style
.HasFontFaceName())
580 if (!(compareWith
&& compareWith
->HasFontFaceName() && compareWith
->GetFontFaceName() == style
.GetFontFaceName()))
581 destStyle
.SetFontFaceName(style
.GetFontFaceName());
584 if (style
.HasFontEncoding())
586 if (!(compareWith
&& compareWith
->HasFontEncoding() && compareWith
->GetFontEncoding() == style
.GetFontEncoding()))
587 destStyle
.SetFontEncoding(style
.GetFontEncoding());
590 if (style
.HasFontFamily())
592 if (!(compareWith
&& compareWith
->HasFontFamily() && compareWith
->GetFontFamily() == style
.GetFontFamily()))
593 destStyle
.SetFontFamily(style
.GetFontFamily());
596 if (style
.GetTextColour().IsOk() && style
.HasTextColour())
598 if (!(compareWith
&& compareWith
->HasTextColour() && compareWith
->GetTextColour() == style
.GetTextColour()))
599 destStyle
.SetTextColour(style
.GetTextColour());
602 if (style
.GetBackgroundColour().IsOk() && style
.HasBackgroundColour())
604 if (!(compareWith
&& compareWith
->HasBackgroundColour() && compareWith
->GetBackgroundColour() == style
.GetBackgroundColour()))
605 destStyle
.SetBackgroundColour(style
.GetBackgroundColour());
608 if (style
.HasAlignment())
610 if (!(compareWith
&& compareWith
->HasAlignment() && compareWith
->GetAlignment() == style
.GetAlignment()))
611 destStyle
.SetAlignment(style
.GetAlignment());
616 if (!(compareWith
&& compareWith
->HasTabs() && TabsEq(compareWith
->GetTabs(), style
.GetTabs())))
617 destStyle
.SetTabs(style
.GetTabs());
620 if (style
.HasLeftIndent())
622 if (!(compareWith
&& compareWith
->HasLeftIndent() && compareWith
->GetLeftIndent() == style
.GetLeftIndent()
623 && compareWith
->GetLeftSubIndent() == style
.GetLeftSubIndent()))
624 destStyle
.SetLeftIndent(style
.GetLeftIndent(), style
.GetLeftSubIndent());
627 if (style
.HasRightIndent())
629 if (!(compareWith
&& compareWith
->HasRightIndent() && compareWith
->GetRightIndent() == style
.GetRightIndent()))
630 destStyle
.SetRightIndent(style
.GetRightIndent());
633 if (style
.HasParagraphSpacingAfter())
635 if (!(compareWith
&& compareWith
->HasParagraphSpacingAfter() && compareWith
->GetParagraphSpacingAfter() == style
.GetParagraphSpacingAfter()))
636 destStyle
.SetParagraphSpacingAfter(style
.GetParagraphSpacingAfter());
639 if (style
.HasParagraphSpacingBefore())
641 if (!(compareWith
&& compareWith
->HasParagraphSpacingBefore() && compareWith
->GetParagraphSpacingBefore() == style
.GetParagraphSpacingBefore()))
642 destStyle
.SetParagraphSpacingBefore(style
.GetParagraphSpacingBefore());
645 if (style
.HasLineSpacing())
647 if (!(compareWith
&& compareWith
->HasLineSpacing() && compareWith
->GetLineSpacing() == style
.GetLineSpacing()))
648 destStyle
.SetLineSpacing(style
.GetLineSpacing());
651 if (style
.HasCharacterStyleName())
653 if (!(compareWith
&& compareWith
->HasCharacterStyleName() && compareWith
->GetCharacterStyleName() == style
.GetCharacterStyleName()))
654 destStyle
.SetCharacterStyleName(style
.GetCharacterStyleName());
657 if (style
.HasParagraphStyleName())
659 if (!(compareWith
&& compareWith
->HasParagraphStyleName() && compareWith
->GetParagraphStyleName() == style
.GetParagraphStyleName()))
660 destStyle
.SetParagraphStyleName(style
.GetParagraphStyleName());
663 if (style
.HasListStyleName())
665 if (!(compareWith
&& compareWith
->HasListStyleName() && compareWith
->GetListStyleName() == style
.GetListStyleName()))
666 destStyle
.SetListStyleName(style
.GetListStyleName());
669 if (style
.HasBulletStyle())
671 if (!(compareWith
&& compareWith
->HasBulletStyle() && compareWith
->GetBulletStyle() == style
.GetBulletStyle()))
672 destStyle
.SetBulletStyle(style
.GetBulletStyle());
675 if (style
.HasBulletText())
677 if (!(compareWith
&& compareWith
->HasBulletText() && compareWith
->GetBulletText() == style
.GetBulletText()))
679 destStyle
.SetBulletText(style
.GetBulletText());
680 destStyle
.SetBulletFont(style
.GetBulletFont());
684 if (style
.HasBulletNumber())
686 if (!(compareWith
&& compareWith
->HasBulletNumber() && compareWith
->GetBulletNumber() == style
.GetBulletNumber()))
687 destStyle
.SetBulletNumber(style
.GetBulletNumber());
690 if (style
.HasBulletName())
692 if (!(compareWith
&& compareWith
->HasBulletName() && compareWith
->GetBulletName() == style
.GetBulletName()))
693 destStyle
.SetBulletName(style
.GetBulletName());
698 if (!(compareWith
&& compareWith
->HasURL() && compareWith
->GetURL() == style
.GetURL()))
699 destStyle
.SetURL(style
.GetURL());
702 if (style
.HasPageBreak())
704 if (!(compareWith
&& compareWith
->HasPageBreak()))
705 destStyle
.SetPageBreak();
708 if (style
.HasTextEffects())
710 if (!(compareWith
&& compareWith
->HasTextEffects() && compareWith
->GetTextEffects() == style
.GetTextEffects()))
712 int destBits
= destStyle
.GetTextEffects();
713 int destFlags
= destStyle
.GetTextEffectFlags();
715 int srcBits
= style
.GetTextEffects();
716 int srcFlags
= style
.GetTextEffectFlags();
718 // Reset incompatible bits in the destination
719 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_SUPERSCRIPT
|wxTEXT_ATTR_EFFECT_SUBSCRIPT
), srcFlags
, destFlags
, destBits
);
720 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_CAPITALS
|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS
), srcFlags
, destFlags
, destBits
);
721 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_STRIKETHROUGH
|wxTEXT_ATTR_EFFECT_DOUBLE_STRIKETHROUGH
), srcFlags
, destFlags
, destBits
);
723 CombineBitlists(destBits
, srcBits
, destFlags
, srcFlags
);
725 destStyle
.SetTextEffects(destBits
);
726 destStyle
.SetTextEffectFlags(destFlags
);
730 if (style
.HasOutlineLevel())
732 if (!(compareWith
&& compareWith
->HasOutlineLevel() && compareWith
->GetOutlineLevel() == style
.GetOutlineLevel()))
733 destStyle
.SetOutlineLevel(style
.GetOutlineLevel());
740 wxTextAttr
wxTextAttr::Combine(const wxTextAttr
& attr
,
741 const wxTextAttr
& attrDef
,
742 const wxTextCtrlBase
*text
)
746 font
= attr
.GetFont();
750 if (attrDef
.HasFont())
751 font
= attrDef
.GetFont();
753 if ( text
&& !font
.IsOk() )
754 font
= text
->GetFont();
757 wxColour colFg
= attr
.GetTextColour();
760 colFg
= attrDef
.GetTextColour();
762 if ( text
&& !colFg
.IsOk() )
763 colFg
= text
->GetForegroundColour();
766 wxColour colBg
= attr
.GetBackgroundColour();
769 colBg
= attrDef
.GetBackgroundColour();
771 if ( text
&& !colBg
.IsOk() )
772 colBg
= text
->GetBackgroundColour();
775 wxTextAttr
newAttr(colFg
, colBg
, font
);
777 if (attr
.HasAlignment())
778 newAttr
.SetAlignment(attr
.GetAlignment());
779 else if (attrDef
.HasAlignment())
780 newAttr
.SetAlignment(attrDef
.GetAlignment());
783 newAttr
.SetTabs(attr
.GetTabs());
784 else if (attrDef
.HasTabs())
785 newAttr
.SetTabs(attrDef
.GetTabs());
787 if (attr
.HasLeftIndent())
788 newAttr
.SetLeftIndent(attr
.GetLeftIndent(), attr
.GetLeftSubIndent());
789 else if (attrDef
.HasLeftIndent())
790 newAttr
.SetLeftIndent(attrDef
.GetLeftIndent(), attr
.GetLeftSubIndent());
792 if (attr
.HasRightIndent())
793 newAttr
.SetRightIndent(attr
.GetRightIndent());
794 else if (attrDef
.HasRightIndent())
795 newAttr
.SetRightIndent(attrDef
.GetRightIndent());
801 bool wxTextAttr::TabsEq(const wxArrayInt
& tabs1
, const wxArrayInt
& tabs2
)
803 if (tabs1
.GetCount() != tabs2
.GetCount())
807 for (i
= 0; i
< tabs1
.GetCount(); i
++)
809 if (tabs1
[i
] != tabs2
[i
])
816 bool wxTextAttr::RemoveStyle(wxTextAttr
& destStyle
, const wxTextAttr
& style
)
818 int flags
= style
.GetFlags();
819 int destFlags
= destStyle
.GetFlags();
821 destStyle
.SetFlags(destFlags
& ~flags
);
826 /// Combine two bitlists, specifying the bits of interest with separate flags.
827 bool wxTextAttr::CombineBitlists(int& valueA
, int valueB
, int& flagsA
, int flagsB
)
829 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
830 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
833 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
834 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
835 int valueA2
= ~(~valueB
& flagsB
) & valueA
;
837 // Now combine the 1 bits.
838 int valueA3
= (valueB
& flagsB
) | valueA2
;
841 flagsA
= (flagsA
| flagsB
);
846 /// Compare two bitlists
847 bool wxTextAttr::BitlistsEqPartial(int valueA
, int valueB
, int flags
)
849 int relevantBitsA
= valueA
& flags
;
850 int relevantBitsB
= valueB
& flags
;
851 return relevantBitsA
== relevantBitsB
;
854 /// Split into paragraph and character styles
855 bool wxTextAttr::SplitParaCharStyles(const wxTextAttr
& style
, wxTextAttr
& parStyle
, wxTextAttr
& charStyle
)
857 wxTextAttr
defaultCharStyle1(style
);
858 wxTextAttr
defaultParaStyle1(style
);
859 defaultCharStyle1
.SetFlags(defaultCharStyle1
.GetFlags()&wxTEXT_ATTR_CHARACTER
);
860 defaultParaStyle1
.SetFlags(defaultParaStyle1
.GetFlags()&wxTEXT_ATTR_PARAGRAPH
);
862 charStyle
.Apply(defaultCharStyle1
);
863 parStyle
.Apply(defaultParaStyle1
);
868 // apply styling to text range
869 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start
), long WXUNUSED(end
),
870 const wxTextAttr
& WXUNUSED(style
))
872 // to be implemented in derived classes
876 // get the styling at the given position
877 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position
), wxTextAttr
& WXUNUSED(style
))
879 // to be implemented in derived classes
883 // change default text attributes
884 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr
& style
)
886 // keep the old attributes if the new style doesn't specify them unless the
887 // new style is empty - then reset m_defaultStyle (as there is no other way
889 if ( style
.IsDefault() )
890 m_defaultStyle
= style
;
892 m_defaultStyle
= wxTextAttr::Combine(style
, m_defaultStyle
, this);
897 // ----------------------------------------------------------------------------
899 // ----------------------------------------------------------------------------
901 bool wxTextAreaBase::DoLoadFile(const wxString
& filename
, int WXUNUSED(fileType
))
904 wxFFile
file(filename
);
905 if ( file
.IsOpened() )
908 if ( file
.ReadAll(&text
) )
913 m_filename
= filename
;
918 #endif // wxUSE_FFILE
920 wxLogError(_("File couldn't be loaded."));
925 bool wxTextAreaBase::DoSaveFile(const wxString
& filename
, int WXUNUSED(fileType
))
928 wxFFile
file(filename
, wxT("w"));
929 if ( file
.IsOpened() && file
.Write(GetValue(), *wxConvCurrent
) )
931 // if it worked, save for future calls
932 m_filename
= filename
;
934 // it's not modified any longer
939 #endif // wxUSE_FFILE
944 bool wxTextAreaBase::SaveFile(const wxString
& filename
, int fileType
)
946 wxString filenameToUse
= filename
.empty() ? m_filename
: filename
;
947 if ( filenameToUse
.empty() )
949 // what kind of message to give? is it an error or a program bug?
950 wxLogDebug(wxT("Can't save textctrl to file without filename."));
955 return DoSaveFile(filenameToUse
, fileType
);
958 // ----------------------------------------------------------------------------
959 // stream-like insertion operator
960 // ----------------------------------------------------------------------------
962 wxTextCtrl
& wxTextCtrlBase::operator<<(const wxString
& s
)
965 return *TEXTCTRL(this);
968 wxTextCtrl
& wxTextCtrlBase::operator<<(double d
)
970 return *this << wxString::Format("%.2f", d
);
973 wxTextCtrl
& wxTextCtrlBase::operator<<(int i
)
975 return *this << wxString::Format("%d", i
);
978 wxTextCtrl
& wxTextCtrlBase::operator<<(long l
)
980 return *this << wxString::Format("%ld", l
);
983 // ----------------------------------------------------------------------------
984 // streambuf methods implementation
985 // ----------------------------------------------------------------------------
987 #if wxHAS_TEXT_WINDOW_STREAM
989 int wxTextCtrlBase::overflow(int c
)
991 AppendText((wxChar
)c
);
993 // return something different from EOF
997 #endif // wxHAS_TEXT_WINDOW_STREAM
999 // ----------------------------------------------------------------------------
1000 // emulating key presses
1001 // ----------------------------------------------------------------------------
1003 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent
& event
)
1005 bool handled
= false;
1006 // we have a native implementation for Win32 and so don't need this one
1009 int keycode
= event
.GetKeyCode();
1012 GetSelection(&from
,&to
);
1013 long insert
= GetInsertionPoint();
1014 long last
= GetLastPosition();
1016 // catch arrow left and right
1021 if ( event
.ShiftDown() )
1022 SetSelection( (from
> 0 ? from
- 1 : 0) , to
);
1027 else if ( insert
> 0 )
1029 SetInsertionPoint( insert
);
1034 if ( event
.ShiftDown() )
1035 SetSelection( from
, (to
< last
? to
+ 1 : last
) );
1040 else if ( insert
< last
)
1042 SetInsertionPoint( insert
);
1056 ch
= (wxChar
)(wxT('0') + keycode
- WXK_NUMPAD0
);
1060 case WXK_NUMPAD_MULTIPLY
:
1065 case WXK_NUMPAD_ADD
:
1070 case WXK_NUMPAD_SUBTRACT
:
1075 case WXK_NUMPAD_DECIMAL
:
1080 case WXK_NUMPAD_DIVIDE
:
1085 case WXK_NUMPAD_DELETE
:
1086 // delete the character at cursor
1088 const long pos
= GetInsertionPoint();
1089 if ( pos
< GetLastPosition() )
1090 Remove(pos
, pos
+ 1);
1096 // delete the character before the cursor
1098 const long pos
= GetInsertionPoint();
1100 Remove(pos
- 1, pos
);
1107 if ( event
.GetUnicodeKey() )
1109 ch
= event
.GetUnicodeKey();
1113 if ( keycode
< 256 && keycode
>= 0 && wxIsprint(keycode
) )
1115 // FIXME this is not going to work for non letters...
1116 if ( !event
.ShiftDown() )
1118 keycode
= wxTolower(keycode
);
1121 ch
= (wxChar
)keycode
;
1137 #endif // !__WIN32__/__WIN32__
1142 // ----------------------------------------------------------------------------
1143 // Other miscellaneous stuff
1144 // ----------------------------------------------------------------------------
1146 bool wxTextCtrlBase::SetHint(const wxString
& hint
)
1148 wxCHECK_MSG( IsSingleLine(), false,
1149 wxS("Hints can only be set for single line text controls") );
1151 return wxTextEntry::SetHint(hint
);
1154 // do the window-specific processing after processing the update event
1155 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
1157 // call inherited, but skip the wxControl's version, and call directly the
1158 // wxWindow's one instead, because the only reason why we are overriding this
1159 // function is that we want to use SetValue() instead of wxControl::SetLabel()
1160 wxWindowBase::DoUpdateWindowUI(event
);
1163 if ( event
.GetSetText() )
1165 if ( event
.GetText() != GetValue() )
1166 SetValue(event
.GetText());
1170 // ----------------------------------------------------------------------------
1172 // ----------------------------------------------------------------------------
1174 wxTextCtrlHitTestResult
1175 wxTextAreaBase::HitTest(const wxPoint
& pt
, wxTextCoord
*x
, wxTextCoord
*y
) const
1177 // implement in terms of the other overload as the native ports typically
1178 // can get the position and not (x, y) pair directly (although wxUniv
1179 // directly gets x and y -- and so overrides this method as well)
1181 wxTextCtrlHitTestResult rc
= HitTest(pt
, &pos
);
1183 if ( rc
!= wxTE_HT_UNKNOWN
)
1185 PositionToXY(pos
, x
, y
);
1191 wxTextCtrlHitTestResult
1192 wxTextAreaBase::HitTest(const wxPoint
& WXUNUSED(pt
), long * WXUNUSED(pos
)) const
1195 return wxTE_HT_UNKNOWN
;
1198 wxPoint
wxTextAreaBase::PositionToCoords(long pos
) const
1200 wxCHECK_MSG( IsValidPosition(pos
), wxDefaultPosition
,
1201 wxS("Position argument out of range.") );
1203 return DoPositionToCoords(pos
);
1206 wxPoint
wxTextAreaBase::DoPositionToCoords(long WXUNUSED(pos
)) const
1208 return wxDefaultPosition
;
1211 #else // !wxUSE_TEXTCTRL
1213 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
1214 // controls (wxComboBox and wxSpinCtrl)
1216 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
);
1218 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL