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