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