Merge in from trunk r67662 to r64801
[wxWidgets.git] / src / common / textcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textcmn.cpp
3 // Purpose: implementation of platform-independent functions of wxTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 13.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/event.h"
25 #endif // WX_PRECOMP
26
27 #if wxUSE_TEXTCTRL
28
29 #include "wx/textctrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif // WX_PRECOMP
35
36 #include "wx/ffile.h"
37
38 extern WXDLLEXPORT_DATA(const char) wxTextCtrlNameStr[] = "text";
39
40 // ----------------------------------------------------------------------------
41 // macros
42 // ----------------------------------------------------------------------------
43
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))
47
48 // ============================================================================
49 // implementation
50 // ============================================================================
51
52 // ----------------------------------------------------------------------------
53 // XTI
54 // ----------------------------------------------------------------------------
55
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)
66
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)
74
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)
84
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 )
102
103 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl, wxControl, "wx/textctrl.h")
104
105 wxBEGIN_PROPERTIES_TABLE(wxTextCtrl)
106 wxEVENT_PROPERTY( TextUpdated, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent )
107 wxEVENT_PROPERTY( TextEnter, wxEVT_COMMAND_TEXT_ENTER, wxCommandEvent )
108
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"))
113
114 wxPROPERTY_FLAGS( WindowStyle, wxTextCtrlStyle, long, SetWindowStyleFlag, \
115 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
116 wxT("Helpstring"), wxT("group")) // style
117 wxEND_PROPERTIES_TABLE()
118
119 wxEMPTY_HANDLERS_TABLE(wxTextCtrl)
120
121 wxCONSTRUCTOR_6( wxTextCtrl, wxWindow*, Parent, wxWindowID, Id, \
122 wxString, Value, wxPoint, Position, wxSize, Size, \
123 long, WindowStyle)
124
125
126 IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent, wxCommandEvent)
127
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 );
132
133 IMPLEMENT_ABSTRACT_CLASS(wxTextCtrlBase, wxControl)
134
135 // ============================================================================
136 // wxTextAttr implementation
137 // ============================================================================
138
139 wxTextAttr::wxTextAttr(const wxColour& colText,
140 const wxColour& colBack,
141 const wxFont& font,
142 wxTextAttrAlignment alignment): m_textAlignment(alignment), m_colText(colText), m_colBack(colBack)
143 {
144 Init();
145
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;
150
151 GetFontAttributes(font);
152 }
153
154 // Initialisation
155 void wxTextAttr::Init()
156 {
157 m_textAlignment = wxTEXT_ALIGNMENT_DEFAULT;
158 m_flags = 0;
159 m_leftIndent = 0;
160 m_leftSubIndent = 0;
161 m_rightIndent = 0;
162
163 m_fontSize = 12;
164 m_fontStyle = wxFONTSTYLE_NORMAL;
165 m_fontWeight = wxFONTWEIGHT_NORMAL;
166 m_fontUnderlined = false;
167 m_fontEncoding = wxFONTENCODING_DEFAULT;
168 m_fontFamily = wxFONTFAMILY_DEFAULT;
169
170 m_paragraphSpacingAfter = 0;
171 m_paragraphSpacingBefore = 0;
172 m_lineSpacing = 0;
173 m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
174 m_textEffects = wxTEXT_ATTR_EFFECT_NONE;
175 m_textEffectFlags = wxTEXT_ATTR_EFFECT_NONE;
176 m_outlineLevel = 0;
177 m_bulletNumber = 0;
178 }
179
180 // Copy
181 void wxTextAttr::Copy(const wxTextAttr& attr)
182 {
183 m_colText = attr.m_colText;
184 m_colBack = attr.m_colBack;
185 m_textAlignment = attr.m_textAlignment;
186 m_leftIndent = attr.m_leftIndent;
187 m_leftSubIndent = attr.m_leftSubIndent;
188 m_rightIndent = attr.m_rightIndent;
189 m_tabs = attr.m_tabs;
190 m_flags = attr.m_flags;
191
192 m_fontSize = attr.m_fontSize;
193 m_fontStyle = attr.m_fontStyle;
194 m_fontWeight = attr.m_fontWeight;
195 m_fontUnderlined = attr.m_fontUnderlined;
196 m_fontFaceName = attr.m_fontFaceName;
197 m_fontEncoding = attr.m_fontEncoding;
198 m_fontFamily = attr.m_fontFamily;
199 m_textEffects = attr.m_textEffects;
200 m_textEffectFlags = attr.m_textEffectFlags;
201
202 m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
203 m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
204 m_lineSpacing = attr.m_lineSpacing;
205 m_characterStyleName = attr.m_characterStyleName;
206 m_paragraphStyleName = attr.m_paragraphStyleName;
207 m_listStyleName = attr.m_listStyleName;
208 m_bulletStyle = attr.m_bulletStyle;
209 m_bulletNumber = attr.m_bulletNumber;
210 m_bulletText = attr.m_bulletText;
211 m_bulletFont = attr.m_bulletFont;
212 m_bulletName = attr.m_bulletName;
213 m_outlineLevel = attr.m_outlineLevel;
214
215 m_urlTarget = attr.m_urlTarget;
216 }
217
218 // operators
219 void wxTextAttr::operator= (const wxTextAttr& attr)
220 {
221 Copy(attr);
222 }
223
224 // Equality test
225 bool wxTextAttr::operator== (const wxTextAttr& attr) const
226 {
227 return GetFlags() == attr.GetFlags() &&
228
229 GetTextColour() == attr.GetTextColour() &&
230 GetBackgroundColour() == attr.GetBackgroundColour() &&
231
232 GetAlignment() == attr.GetAlignment() &&
233 GetLeftIndent() == attr.GetLeftIndent() &&
234 GetLeftSubIndent() == attr.GetLeftSubIndent() &&
235 GetRightIndent() == attr.GetRightIndent() &&
236 TabsEq(GetTabs(), attr.GetTabs()) &&
237
238 GetParagraphSpacingAfter() == attr.GetParagraphSpacingAfter() &&
239 GetParagraphSpacingBefore() == attr.GetParagraphSpacingBefore() &&
240 GetLineSpacing() == attr.GetLineSpacing() &&
241 GetCharacterStyleName() == attr.GetCharacterStyleName() &&
242 GetParagraphStyleName() == attr.GetParagraphStyleName() &&
243 GetListStyleName() == attr.GetListStyleName() &&
244
245 GetBulletStyle() == attr.GetBulletStyle() &&
246 GetBulletText() == attr.GetBulletText() &&
247 GetBulletNumber() == attr.GetBulletNumber() &&
248 GetBulletFont() == attr.GetBulletFont() &&
249 GetBulletName() == attr.GetBulletName() &&
250
251 GetTextEffects() == attr.GetTextEffects() &&
252 GetTextEffectFlags() == attr.GetTextEffectFlags() &&
253
254 GetOutlineLevel() == attr.GetOutlineLevel() &&
255
256 GetFontSize() == attr.GetFontSize() &&
257 GetFontStyle() == attr.GetFontStyle() &&
258 GetFontWeight() == attr.GetFontWeight() &&
259 GetFontUnderlined() == attr.GetFontUnderlined() &&
260 GetFontFaceName() == attr.GetFontFaceName() &&
261 GetFontEncoding() == attr.GetFontEncoding() &&
262 GetFontFamily() == attr.GetFontFamily() &&
263
264 GetURL() == attr.GetURL();
265 }
266
267 // Partial equality test. Only returns false if an attribute doesn't match.
268 bool wxTextAttr::EqPartial(const wxTextAttr& attr) const
269 {
270 int flags = attr.GetFlags();
271
272 if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && GetTextColour() != attr.GetTextColour())
273 return false;
274
275 if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && GetBackgroundColour() != attr.GetBackgroundColour())
276 return false;
277
278 if ((flags & wxTEXT_ATTR_FONT_FACE) &&
279 GetFontFaceName() != attr.GetFontFaceName())
280 return false;
281
282 if ((flags & wxTEXT_ATTR_FONT_SIZE) &&
283 GetFontSize() != attr.GetFontSize())
284 return false;
285
286 if ((flags & wxTEXT_ATTR_FONT_WEIGHT) &&
287 GetFontWeight() != attr.GetFontWeight())
288 return false;
289
290 if ((flags & wxTEXT_ATTR_FONT_ITALIC) &&
291 GetFontStyle() != attr.GetFontStyle())
292 return false;
293
294 if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) &&
295 GetFontUnderlined() != attr.GetFontUnderlined())
296 return false;
297
298 if ((flags & wxTEXT_ATTR_FONT_ENCODING) &&
299 GetFontEncoding() != attr.GetFontEncoding())
300 return false;
301
302 if ((flags & wxTEXT_ATTR_FONT_FAMILY) &&
303 GetFontFamily() != attr.GetFontFamily())
304 return false;
305
306 if ((flags & wxTEXT_ATTR_URL) && GetURL() != attr.GetURL())
307 return false;
308
309 if ((flags & wxTEXT_ATTR_ALIGNMENT) && GetAlignment() != attr.GetAlignment())
310 return false;
311
312 if ((flags & wxTEXT_ATTR_LEFT_INDENT) &&
313 ((GetLeftIndent() != attr.GetLeftIndent()) || (GetLeftSubIndent() != attr.GetLeftSubIndent())))
314 return false;
315
316 if ((flags & wxTEXT_ATTR_RIGHT_INDENT) &&
317 (GetRightIndent() != attr.GetRightIndent()))
318 return false;
319
320 if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) &&
321 (GetParagraphSpacingAfter() != attr.GetParagraphSpacingAfter()))
322 return false;
323
324 if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) &&
325 (GetParagraphSpacingBefore() != attr.GetParagraphSpacingBefore()))
326 return false;
327
328 if ((flags & wxTEXT_ATTR_LINE_SPACING) &&
329 (GetLineSpacing() != attr.GetLineSpacing()))
330 return false;
331
332 if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) &&
333 (GetCharacterStyleName() != attr.GetCharacterStyleName()))
334 return false;
335
336 if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) &&
337 (GetParagraphStyleName() != attr.GetParagraphStyleName()))
338 return false;
339
340 if ((flags & wxTEXT_ATTR_LIST_STYLE_NAME) &&
341 (GetListStyleName() != attr.GetListStyleName()))
342 return false;
343
344 if ((flags & wxTEXT_ATTR_BULLET_STYLE) &&
345 (GetBulletStyle() != attr.GetBulletStyle()))
346 return false;
347
348 if ((flags & wxTEXT_ATTR_BULLET_NUMBER) &&
349 (GetBulletNumber() != attr.GetBulletNumber()))
350 return false;
351
352 if ((flags & wxTEXT_ATTR_BULLET_TEXT) &&
353 (GetBulletText() != attr.GetBulletText()) &&
354 (GetBulletFont() != attr.GetBulletFont()))
355 return false;
356
357 if ((flags & wxTEXT_ATTR_BULLET_NAME) &&
358 (GetBulletName() != attr.GetBulletName()))
359 return false;
360
361 if ((flags & wxTEXT_ATTR_TABS) &&
362 !TabsEq(GetTabs(), attr.GetTabs()))
363 return false;
364
365 if ((flags & wxTEXT_ATTR_PAGE_BREAK) &&
366 (HasPageBreak() != attr.HasPageBreak()))
367 return false;
368
369 if (flags & wxTEXT_ATTR_EFFECTS)
370 {
371 if (HasTextEffects() != attr.HasTextEffects())
372 return false;
373 if (!BitlistsEqPartial(GetTextEffects(), attr.GetTextEffects(), attr.GetTextEffectFlags()))
374 return false;
375 }
376
377 if ((flags & wxTEXT_ATTR_OUTLINE_LEVEL) &&
378 (GetOutlineLevel() != attr.GetOutlineLevel()))
379 return false;
380
381 return true;
382 }
383
384 // Create font from font attributes.
385 wxFont wxTextAttr::GetFont() const
386 {
387 if ( !HasFont() )
388 return wxNullFont;
389
390 int fontSize = 10;
391 if (HasFontSize())
392 fontSize = GetFontSize();
393
394 int fontStyle = wxNORMAL;
395 if (HasFontItalic())
396 fontStyle = GetFontStyle();
397
398 int fontWeight = wxNORMAL;
399 if (HasFontWeight())
400 fontWeight = GetFontWeight();
401
402 bool underlined = false;
403 if (HasFontUnderlined())
404 underlined = GetFontUnderlined();
405
406 wxString fontFaceName;
407 if (HasFontFaceName())
408 fontFaceName = GetFontFaceName();
409
410 wxFontEncoding encoding = wxFONTENCODING_DEFAULT;
411 if (HasFontEncoding())
412 encoding = GetFontEncoding();
413
414 wxFontFamily fontFamily = wxFONTFAMILY_DEFAULT;
415 if (HasFontFamily())
416 fontFamily = GetFontFamily();
417
418 wxFont font(fontSize, fontFamily, fontStyle, fontWeight, underlined, fontFaceName, encoding);
419 return font;
420 }
421
422 // Get attributes from font.
423 bool wxTextAttr::GetFontAttributes(const wxFont& font, int flags)
424 {
425 if (!font.IsOk())
426 return false;
427
428 if (flags & wxTEXT_ATTR_FONT_SIZE)
429 m_fontSize = font.GetPointSize();
430
431 if (flags & wxTEXT_ATTR_FONT_ITALIC)
432 m_fontStyle = font.GetStyle();
433
434 if (flags & wxTEXT_ATTR_FONT_WEIGHT)
435 m_fontWeight = font.GetWeight();
436
437 if (flags & wxTEXT_ATTR_FONT_UNDERLINE)
438 m_fontUnderlined = font.GetUnderlined();
439
440 if (flags & wxTEXT_ATTR_FONT_FACE)
441 m_fontFaceName = font.GetFaceName();
442
443 if (flags & wxTEXT_ATTR_FONT_ENCODING)
444 m_fontEncoding = font.GetEncoding();
445
446 if (flags & wxTEXT_ATTR_FONT_FAMILY)
447 {
448 // wxFont might not know its family, avoid setting m_fontFamily to an
449 // invalid value and rather pretend that we don't have any font family
450 // information at all in this case
451 const wxFontFamily fontFamily = font.GetFamily();
452 if ( fontFamily == wxFONTFAMILY_UNKNOWN )
453 flags &= ~wxTEXT_ATTR_FONT_FAMILY;
454 else
455 m_fontFamily = fontFamily;
456 }
457
458 m_flags |= flags;
459
460 return true;
461 }
462
463 // Resets bits in destination so new attributes aren't merged with mutually exclusive ones
464 static bool wxResetIncompatibleBits(const int mask, const int srcFlags, int& destFlags, int& destBits)
465 {
466 if ((srcFlags & mask) && (destFlags & mask))
467 {
468 destBits &= ~mask;
469 destFlags &= ~mask;
470 }
471
472 return true;
473 }
474
475 bool wxTextAttr::Apply(const wxTextAttr& style, const wxTextAttr* compareWith)
476 {
477 wxTextAttr& destStyle = (*this);
478
479 if (style.HasFontWeight())
480 {
481 if (!(compareWith && compareWith->HasFontWeight() && compareWith->GetFontWeight() == style.GetFontWeight()))
482 destStyle.SetFontWeight(style.GetFontWeight());
483 }
484
485 if (style.HasFontSize())
486 {
487 if (!(compareWith && compareWith->HasFontSize() && compareWith->GetFontSize() == style.GetFontSize()))
488 destStyle.SetFontSize(style.GetFontSize());
489 }
490
491 if (style.HasFontItalic())
492 {
493 if (!(compareWith && compareWith->HasFontItalic() && compareWith->GetFontStyle() == style.GetFontStyle()))
494 destStyle.SetFontStyle(style.GetFontStyle());
495 }
496
497 if (style.HasFontUnderlined())
498 {
499 if (!(compareWith && compareWith->HasFontUnderlined() && compareWith->GetFontUnderlined() == style.GetFontUnderlined()))
500 destStyle.SetFontUnderlined(style.GetFontUnderlined());
501 }
502
503 if (style.HasFontFaceName())
504 {
505 if (!(compareWith && compareWith->HasFontFaceName() && compareWith->GetFontFaceName() == style.GetFontFaceName()))
506 destStyle.SetFontFaceName(style.GetFontFaceName());
507 }
508
509 if (style.HasFontEncoding())
510 {
511 if (!(compareWith && compareWith->HasFontEncoding() && compareWith->GetFontEncoding() == style.GetFontEncoding()))
512 destStyle.SetFontEncoding(style.GetFontEncoding());
513 }
514
515 if (style.HasFontFamily())
516 {
517 if (!(compareWith && compareWith->HasFontFamily() && compareWith->GetFontFamily() == style.GetFontFamily()))
518 destStyle.SetFontFamily(style.GetFontFamily());
519 }
520
521 if (style.GetTextColour().IsOk() && style.HasTextColour())
522 {
523 if (!(compareWith && compareWith->HasTextColour() && compareWith->GetTextColour() == style.GetTextColour()))
524 destStyle.SetTextColour(style.GetTextColour());
525 }
526
527 if (style.GetBackgroundColour().IsOk() && style.HasBackgroundColour())
528 {
529 if (!(compareWith && compareWith->HasBackgroundColour() && compareWith->GetBackgroundColour() == style.GetBackgroundColour()))
530 destStyle.SetBackgroundColour(style.GetBackgroundColour());
531 }
532
533 if (style.HasAlignment())
534 {
535 if (!(compareWith && compareWith->HasAlignment() && compareWith->GetAlignment() == style.GetAlignment()))
536 destStyle.SetAlignment(style.GetAlignment());
537 }
538
539 if (style.HasTabs())
540 {
541 if (!(compareWith && compareWith->HasTabs() && TabsEq(compareWith->GetTabs(), style.GetTabs())))
542 destStyle.SetTabs(style.GetTabs());
543 }
544
545 if (style.HasLeftIndent())
546 {
547 if (!(compareWith && compareWith->HasLeftIndent() && compareWith->GetLeftIndent() == style.GetLeftIndent()
548 && compareWith->GetLeftSubIndent() == style.GetLeftSubIndent()))
549 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
550 }
551
552 if (style.HasRightIndent())
553 {
554 if (!(compareWith && compareWith->HasRightIndent() && compareWith->GetRightIndent() == style.GetRightIndent()))
555 destStyle.SetRightIndent(style.GetRightIndent());
556 }
557
558 if (style.HasParagraphSpacingAfter())
559 {
560 if (!(compareWith && compareWith->HasParagraphSpacingAfter() && compareWith->GetParagraphSpacingAfter() == style.GetParagraphSpacingAfter()))
561 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
562 }
563
564 if (style.HasParagraphSpacingBefore())
565 {
566 if (!(compareWith && compareWith->HasParagraphSpacingBefore() && compareWith->GetParagraphSpacingBefore() == style.GetParagraphSpacingBefore()))
567 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
568 }
569
570 if (style.HasLineSpacing())
571 {
572 if (!(compareWith && compareWith->HasLineSpacing() && compareWith->GetLineSpacing() == style.GetLineSpacing()))
573 destStyle.SetLineSpacing(style.GetLineSpacing());
574 }
575
576 if (style.HasCharacterStyleName())
577 {
578 if (!(compareWith && compareWith->HasCharacterStyleName() && compareWith->GetCharacterStyleName() == style.GetCharacterStyleName()))
579 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
580 }
581
582 if (style.HasParagraphStyleName())
583 {
584 if (!(compareWith && compareWith->HasParagraphStyleName() && compareWith->GetParagraphStyleName() == style.GetParagraphStyleName()))
585 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
586 }
587
588 if (style.HasListStyleName())
589 {
590 if (!(compareWith && compareWith->HasListStyleName() && compareWith->GetListStyleName() == style.GetListStyleName()))
591 destStyle.SetListStyleName(style.GetListStyleName());
592 }
593
594 if (style.HasBulletStyle())
595 {
596 if (!(compareWith && compareWith->HasBulletStyle() && compareWith->GetBulletStyle() == style.GetBulletStyle()))
597 destStyle.SetBulletStyle(style.GetBulletStyle());
598 }
599
600 if (style.HasBulletText())
601 {
602 if (!(compareWith && compareWith->HasBulletText() && compareWith->GetBulletText() == style.GetBulletText()))
603 {
604 destStyle.SetBulletText(style.GetBulletText());
605 destStyle.SetBulletFont(style.GetBulletFont());
606 }
607 }
608
609 if (style.HasBulletNumber())
610 {
611 if (!(compareWith && compareWith->HasBulletNumber() && compareWith->GetBulletNumber() == style.GetBulletNumber()))
612 destStyle.SetBulletNumber(style.GetBulletNumber());
613 }
614
615 if (style.HasBulletName())
616 {
617 if (!(compareWith && compareWith->HasBulletName() && compareWith->GetBulletName() == style.GetBulletName()))
618 destStyle.SetBulletName(style.GetBulletName());
619 }
620
621 if (style.HasURL())
622 {
623 if (!(compareWith && compareWith->HasURL() && compareWith->GetURL() == style.GetURL()))
624 destStyle.SetURL(style.GetURL());
625 }
626
627 if (style.HasPageBreak())
628 {
629 if (!(compareWith && compareWith->HasPageBreak()))
630 destStyle.SetPageBreak();
631 }
632
633 if (style.HasTextEffects())
634 {
635 if (!(compareWith && compareWith->HasTextEffects() && compareWith->GetTextEffects() == style.GetTextEffects()))
636 {
637 int destBits = destStyle.GetTextEffects();
638 int destFlags = destStyle.GetTextEffectFlags();
639
640 int srcBits = style.GetTextEffects();
641 int srcFlags = style.GetTextEffectFlags();
642
643 // Reset incompatible bits in the destination
644 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_SUPERSCRIPT|wxTEXT_ATTR_EFFECT_SUBSCRIPT), srcFlags, destFlags, destBits);
645 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_CAPITALS|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS), srcFlags, destFlags, destBits);
646 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_STRIKETHROUGH|wxTEXT_ATTR_EFFECT_DOUBLE_STRIKETHROUGH), srcFlags, destFlags, destBits);
647
648 CombineBitlists(destBits, srcBits, destFlags, srcFlags);
649
650 destStyle.SetTextEffects(destBits);
651 destStyle.SetTextEffectFlags(destFlags);
652 }
653 }
654
655 if (style.HasOutlineLevel())
656 {
657 if (!(compareWith && compareWith->HasOutlineLevel() && compareWith->GetOutlineLevel() == style.GetOutlineLevel()))
658 destStyle.SetOutlineLevel(style.GetOutlineLevel());
659 }
660
661 return true;
662 }
663
664 /* static */
665 wxTextAttr wxTextAttr::Combine(const wxTextAttr& attr,
666 const wxTextAttr& attrDef,
667 const wxTextCtrlBase *text)
668 {
669 wxFont font;
670 if (attr.HasFont())
671 font = attr.GetFont();
672
673 if ( !font.IsOk() )
674 {
675 if (attrDef.HasFont())
676 font = attrDef.GetFont();
677
678 if ( text && !font.IsOk() )
679 font = text->GetFont();
680 }
681
682 wxColour colFg = attr.GetTextColour();
683 if ( !colFg.IsOk() )
684 {
685 colFg = attrDef.GetTextColour();
686
687 if ( text && !colFg.IsOk() )
688 colFg = text->GetForegroundColour();
689 }
690
691 wxColour colBg = attr.GetBackgroundColour();
692 if ( !colBg.IsOk() )
693 {
694 colBg = attrDef.GetBackgroundColour();
695
696 if ( text && !colBg.IsOk() )
697 colBg = text->GetBackgroundColour();
698 }
699
700 wxTextAttr newAttr(colFg, colBg, font);
701
702 if (attr.HasAlignment())
703 newAttr.SetAlignment(attr.GetAlignment());
704 else if (attrDef.HasAlignment())
705 newAttr.SetAlignment(attrDef.GetAlignment());
706
707 if (attr.HasTabs())
708 newAttr.SetTabs(attr.GetTabs());
709 else if (attrDef.HasTabs())
710 newAttr.SetTabs(attrDef.GetTabs());
711
712 if (attr.HasLeftIndent())
713 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
714 else if (attrDef.HasLeftIndent())
715 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
716
717 if (attr.HasRightIndent())
718 newAttr.SetRightIndent(attr.GetRightIndent());
719 else if (attrDef.HasRightIndent())
720 newAttr.SetRightIndent(attrDef.GetRightIndent());
721
722 return newAttr;
723 }
724
725 /// Compare tabs
726 bool wxTextAttr::TabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
727 {
728 if (tabs1.GetCount() != tabs2.GetCount())
729 return false;
730
731 size_t i;
732 for (i = 0; i < tabs1.GetCount(); i++)
733 {
734 if (tabs1[i] != tabs2[i])
735 return false;
736 }
737 return true;
738 }
739
740 // Remove attributes
741 bool wxTextAttr::RemoveStyle(wxTextAttr& destStyle, const wxTextAttr& style)
742 {
743 int flags = style.GetFlags();
744 int destFlags = destStyle.GetFlags();
745
746 destStyle.SetFlags(destFlags & ~flags);
747
748 return true;
749 }
750
751 /// Combine two bitlists, specifying the bits of interest with separate flags.
752 bool wxTextAttr::CombineBitlists(int& valueA, int valueB, int& flagsA, int flagsB)
753 {
754 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
755 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
756 // indicate it.
757
758 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
759 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
760 int valueA2 = ~(~valueB & flagsB) & valueA;
761
762 // Now combine the 1 bits.
763 int valueA3 = (valueB & flagsB) | valueA2;
764
765 valueA = valueA3;
766 flagsA = (flagsA | flagsB);
767
768 return true;
769 }
770
771 /// Compare two bitlists
772 bool wxTextAttr::BitlistsEqPartial(int valueA, int valueB, int flags)
773 {
774 int relevantBitsA = valueA & flags;
775 int relevantBitsB = valueB & flags;
776 return (relevantBitsA != relevantBitsB);
777 }
778
779 /// Split into paragraph and character styles
780 bool wxTextAttr::SplitParaCharStyles(const wxTextAttr& style, wxTextAttr& parStyle, wxTextAttr& charStyle)
781 {
782 wxTextAttr defaultCharStyle1(style);
783 wxTextAttr defaultParaStyle1(style);
784 defaultCharStyle1.SetFlags(defaultCharStyle1.GetFlags()&wxTEXT_ATTR_CHARACTER);
785 defaultParaStyle1.SetFlags(defaultParaStyle1.GetFlags()&wxTEXT_ATTR_PARAGRAPH);
786
787 charStyle.Apply(defaultCharStyle1);
788 parStyle.Apply(defaultParaStyle1);
789
790 return true;
791 }
792
793 // apply styling to text range
794 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
795 const wxTextAttr& WXUNUSED(style))
796 {
797 // to be implemented in derived classes
798 return false;
799 }
800
801 // get the styling at the given position
802 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style))
803 {
804 // to be implemented in derived classes
805 return false;
806 }
807
808 // change default text attributes
809 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr& style)
810 {
811 // keep the old attributes if the new style doesn't specify them unless the
812 // new style is empty - then reset m_defaultStyle (as there is no other way
813 // to do it)
814 if ( style.IsDefault() )
815 m_defaultStyle = style;
816 else
817 m_defaultStyle = wxTextAttr::Combine(style, m_defaultStyle, this);
818
819 return true;
820 }
821
822 // ----------------------------------------------------------------------------
823 // file IO functions
824 // ----------------------------------------------------------------------------
825
826 bool wxTextAreaBase::DoLoadFile(const wxString& filename, int WXUNUSED(fileType))
827 {
828 #if wxUSE_FFILE
829 wxFFile file(filename);
830 if ( file.IsOpened() )
831 {
832 wxString text;
833 if ( file.ReadAll(&text) )
834 {
835 SetValue(text);
836
837 return true;
838 }
839 }
840 #endif // wxUSE_FFILE
841
842 return false;
843 }
844
845 bool wxTextCtrlBase::DoLoadFile(const wxString& filename, int fileType)
846 {
847 if ( wxTextAreaBase::DoLoadFile(filename, fileType) )
848 {
849 DiscardEdits();
850 m_filename = filename;
851 return true;
852 }
853 wxLogError(_("File couldn't be loaded."));
854 return false;
855 }
856
857 bool wxTextAreaBase::DoSaveFile(const wxString& filename, int WXUNUSED(fileType))
858 {
859 #if wxUSE_FFILE
860 wxFFile file(filename, wxT("w"));
861 return file.IsOpened() && file.Write(GetValue(), *wxConvCurrent);
862 #else
863 return false;
864 #endif // wxUSE_FFILE
865 }
866
867 bool wxTextAreaBase::SaveFile(const wxString& filename, int fileType)
868 {
869 wxString filenameToUse = filename.empty() ? m_filename : filename;
870 if ( filenameToUse.empty() )
871 {
872 // what kind of message to give? is it an error or a program bug?
873 wxLogDebug(wxT("Can't save textctrl to file without filename."));
874
875 return false;
876 }
877
878 return DoSaveFile(filenameToUse, fileType);
879 }
880
881 bool wxTextCtrlBase::DoSaveFile(const wxString& filename, int fileType)
882 {
883 if ( wxTextAreaBase::DoSaveFile(filename, fileType) )
884 {
885 // if it worked, save for future calls
886 m_filename = filename;
887
888 // it's not modified any longer
889 DiscardEdits();
890
891 return true;
892 }
893 return false;
894 }
895
896 // ----------------------------------------------------------------------------
897 // stream-like insertion operator
898 // ----------------------------------------------------------------------------
899
900 wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
901 {
902 AppendText(s);
903 return *TEXTCTRL(this);
904 }
905
906 wxTextCtrl& wxTextCtrlBase::operator<<(double d)
907 {
908 return *this << wxString::Format("%.2f", d);
909 }
910
911 wxTextCtrl& wxTextCtrlBase::operator<<(int i)
912 {
913 return *this << wxString::Format("%d", i);
914 }
915
916 wxTextCtrl& wxTextCtrlBase::operator<<(long l)
917 {
918 return *this << wxString::Format("%ld", l);
919 }
920
921 // ----------------------------------------------------------------------------
922 // streambuf methods implementation
923 // ----------------------------------------------------------------------------
924
925 #if wxHAS_TEXT_WINDOW_STREAM
926
927 int wxTextCtrlBase::overflow(int c)
928 {
929 AppendText((wxChar)c);
930
931 // return something different from EOF
932 return 0;
933 }
934
935 #endif // wxHAS_TEXT_WINDOW_STREAM
936
937 // ----------------------------------------------------------------------------
938 // emulating key presses
939 // ----------------------------------------------------------------------------
940
941 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent& event)
942 {
943 // we have a native implementation for Win32 and so don't need this one
944 #ifndef __WIN32__
945 wxChar ch = 0;
946 int keycode = event.GetKeyCode();
947 switch ( keycode )
948 {
949 case WXK_NUMPAD0:
950 case WXK_NUMPAD1:
951 case WXK_NUMPAD2:
952 case WXK_NUMPAD3:
953 case WXK_NUMPAD4:
954 case WXK_NUMPAD5:
955 case WXK_NUMPAD6:
956 case WXK_NUMPAD7:
957 case WXK_NUMPAD8:
958 case WXK_NUMPAD9:
959 ch = (wxChar)(wxT('0') + keycode - WXK_NUMPAD0);
960 break;
961
962 case WXK_MULTIPLY:
963 case WXK_NUMPAD_MULTIPLY:
964 ch = wxT('*');
965 break;
966
967 case WXK_ADD:
968 case WXK_NUMPAD_ADD:
969 ch = wxT('+');
970 break;
971
972 case WXK_SUBTRACT:
973 case WXK_NUMPAD_SUBTRACT:
974 ch = wxT('-');
975 break;
976
977 case WXK_DECIMAL:
978 case WXK_NUMPAD_DECIMAL:
979 ch = wxT('.');
980 break;
981
982 case WXK_DIVIDE:
983 case WXK_NUMPAD_DIVIDE:
984 ch = wxT('/');
985 break;
986
987 case WXK_DELETE:
988 case WXK_NUMPAD_DELETE:
989 // delete the character at cursor
990 {
991 const long pos = GetInsertionPoint();
992 if ( pos < GetLastPosition() )
993 Remove(pos, pos + 1);
994 }
995 break;
996
997 case WXK_BACK:
998 // delete the character before the cursor
999 {
1000 const long pos = GetInsertionPoint();
1001 if ( pos > 0 )
1002 Remove(pos - 1, pos);
1003 }
1004 break;
1005
1006 default:
1007 #if wxUSE_UNICODE
1008 if ( event.GetUnicodeKey() )
1009 {
1010 ch = event.GetUnicodeKey();
1011 }
1012 else
1013 #endif
1014 if ( keycode < 256 && keycode >= 0 && wxIsprint(keycode) )
1015 {
1016 // FIXME this is not going to work for non letters...
1017 if ( !event.ShiftDown() )
1018 {
1019 keycode = wxTolower(keycode);
1020 }
1021
1022 ch = (wxChar)keycode;
1023 }
1024 else
1025 {
1026 ch = wxT('\0');
1027 }
1028 }
1029
1030 if ( ch )
1031 {
1032 WriteText(ch);
1033
1034 return true;
1035 }
1036 #else // __WIN32__
1037 wxUnusedVar(event);
1038 #endif // !__WIN32__/__WIN32__
1039
1040 return false;
1041 }
1042
1043 // do the window-specific processing after processing the update event
1044 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
1045 {
1046 // call inherited, but skip the wxControl's version, and call directly the
1047 // wxWindow's one instead, because the only reason why we are overriding this
1048 // function is that we want to use SetValue() instead of wxControl::SetLabel()
1049 wxWindowBase::DoUpdateWindowUI(event);
1050
1051 // update text
1052 if ( event.GetSetText() )
1053 {
1054 if ( event.GetText() != GetValue() )
1055 SetValue(event.GetText());
1056 }
1057 }
1058
1059 // ----------------------------------------------------------------------------
1060 // hit testing
1061 // ----------------------------------------------------------------------------
1062
1063 wxTextCtrlHitTestResult
1064 wxTextAreaBase::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
1065 {
1066 // implement in terms of the other overload as the native ports typically
1067 // can get the position and not (x, y) pair directly (although wxUniv
1068 // directly gets x and y -- and so overrides this method as well)
1069 long pos;
1070 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
1071
1072 if ( rc != wxTE_HT_UNKNOWN )
1073 {
1074 PositionToXY(pos, x, y);
1075 }
1076
1077 return rc;
1078 }
1079
1080 wxTextCtrlHitTestResult
1081 wxTextAreaBase::HitTest(const wxPoint& WXUNUSED(pt), long * WXUNUSED(pos)) const
1082 {
1083 // not implemented
1084 return wxTE_HT_UNKNOWN;
1085 }
1086
1087 #else // !wxUSE_TEXTCTRL
1088
1089 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
1090 // controls (wxComboBox and wxSpinCtrl)
1091
1092 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent );
1093
1094 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL