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