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