]> git.saurik.com Git - wxWidgets.git/blame - src/common/textcmn.cpp
don't compile in wx hash code unless we really use it (#9532:12)
[wxWidgets.git] / src / common / textcmn.cpp
CommitLineData
a1b82138 1///////////////////////////////////////////////////////////////////////////////
d5da0ce7 2// Name: src/common/textcmn.cpp
a1b82138
VZ
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$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
a1b82138
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
1e6feb95 15
a1b82138
VZ
16// for compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
d5da0ce7
WS
23#ifndef WX_PRECOMP
24 #include "wx/event.h"
25#endif // WX_PRECOMP
26
1e6feb95
VZ
27#if wxUSE_TEXTCTRL
28
d5da0ce7
WS
29#include "wx/textctrl.h"
30
a1b82138 31#ifndef WX_PRECOMP
0efe5ba7
VZ
32 #include "wx/intl.h"
33 #include "wx/log.h"
a1b82138
VZ
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
c57e3339
VZ
50IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent, wxCommandEvent)
51
52DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED)
53DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_ENTER)
54DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_URL)
d7eee191 55DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_MAXLEN)
c57e3339 56
9d112688
JS
57IMPLEMENT_ABSTRACT_CLASS(wxTextCtrlBase, wxControl)
58
44cc96a8
JS
59// ============================================================================
60// wxTextAttr implementation
61// ============================================================================
4bc1afd5 62
e00a5d3c
JS
63wxTextAttr::wxTextAttr(const wxColour& colText,
64 const wxColour& colBack,
65 const wxFont& font,
44cc96a8 66 wxTextAttrAlignment alignment): m_textAlignment(alignment), m_colText(colText), m_colBack(colBack)
e00a5d3c 67{
44cc96a8
JS
68 Init();
69
e00a5d3c
JS
70 if (m_colText.Ok()) m_flags |= wxTEXT_ATTR_TEXT_COLOUR;
71 if (m_colBack.Ok()) m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR;
e00a5d3c
JS
72 if (alignment != wxTEXT_ALIGNMENT_DEFAULT)
73 m_flags |= wxTEXT_ATTR_ALIGNMENT;
44cc96a8
JS
74
75 GetFontAttributes(font);
e00a5d3c
JS
76}
77
44cc96a8 78// Initialisation
e00a5d3c
JS
79void wxTextAttr::Init()
80{
81 m_textAlignment = wxTEXT_ALIGNMENT_DEFAULT;
82 m_flags = 0;
83 m_leftIndent = 0;
89b67477 84 m_leftSubIndent = 0;
e00a5d3c 85 m_rightIndent = 0;
44cc96a8
JS
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
104void 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
141void wxTextAttr::operator= (const wxTextAttr& attr)
142{
143 Copy(attr);
144}
145
146// Equality test
147bool 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
189bool 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.
1fee6e25 300wxFont wxTextAttr::GetFont() const
44cc96a8 301{
1fee6e25
VZ
302 if ( !HasFont() )
303 return wxNullFont;
304
44cc96a8
JS
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.
337bool 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
c4076626
JS
360 m_flags |= flags;
361
44cc96a8
JS
362 return true;
363}
364
d1e5be0e
JS
365// Resets bits in destination so new attributes aren't merged with mutually exclusive ones
366static bool wxResetIncompatibleBits(const int mask, const int srcFlags, int& destFlags, int& destBits)
367{
368 if ((srcFlags & mask) && (destFlags & mask))
369 {
370 destBits &= ~mask;
371 destFlags &= ~mask;
372 }
373
374 return true;
375}
376
44cc96a8
JS
377bool wxTextAttr::Apply(const wxTextAttr& style, const wxTextAttr* compareWith)
378{
379 wxTextAttr& destStyle = (*this);
380
381 if (style.HasFontWeight())
382 {
383 if (!(compareWith && compareWith->HasFontWeight() && compareWith->GetFontWeight() == style.GetFontWeight()))
384 destStyle.SetFontWeight(style.GetFontWeight());
385 }
386
387 if (style.HasFontSize())
388 {
389 if (!(compareWith && compareWith->HasFontSize() && compareWith->GetFontSize() == style.GetFontSize()))
390 destStyle.SetFontSize(style.GetFontSize());
391 }
392
393 if (style.HasFontItalic())
394 {
395 if (!(compareWith && compareWith->HasFontItalic() && compareWith->GetFontStyle() == style.GetFontStyle()))
396 destStyle.SetFontStyle(style.GetFontStyle());
397 }
398
399 if (style.HasFontUnderlined())
400 {
401 if (!(compareWith && compareWith->HasFontUnderlined() && compareWith->GetFontUnderlined() == style.GetFontUnderlined()))
402 destStyle.SetFontUnderlined(style.GetFontUnderlined());
403 }
404
405 if (style.HasFontFaceName())
406 {
407 if (!(compareWith && compareWith->HasFontFaceName() && compareWith->GetFontFaceName() == style.GetFontFaceName()))
408 destStyle.SetFontFaceName(style.GetFontFaceName());
409 }
410
411 if (style.HasFontEncoding())
412 {
413 if (!(compareWith && compareWith->HasFontEncoding() && compareWith->GetFontEncoding() == style.GetFontEncoding()))
414 destStyle.SetFontEncoding(style.GetFontEncoding());
415 }
416
417 if (style.GetTextColour().Ok() && style.HasTextColour())
418 {
419 if (!(compareWith && compareWith->HasTextColour() && compareWith->GetTextColour() == style.GetTextColour()))
420 destStyle.SetTextColour(style.GetTextColour());
421 }
422
423 if (style.GetBackgroundColour().Ok() && style.HasBackgroundColour())
424 {
425 if (!(compareWith && compareWith->HasBackgroundColour() && compareWith->GetBackgroundColour() == style.GetBackgroundColour()))
426 destStyle.SetBackgroundColour(style.GetBackgroundColour());
427 }
428
429 if (style.HasAlignment())
430 {
431 if (!(compareWith && compareWith->HasAlignment() && compareWith->GetAlignment() == style.GetAlignment()))
432 destStyle.SetAlignment(style.GetAlignment());
433 }
434
435 if (style.HasTabs())
436 {
437 if (!(compareWith && compareWith->HasTabs() && TabsEq(compareWith->GetTabs(), style.GetTabs())))
438 destStyle.SetTabs(style.GetTabs());
439 }
440
441 if (style.HasLeftIndent())
442 {
443 if (!(compareWith && compareWith->HasLeftIndent() && compareWith->GetLeftIndent() == style.GetLeftIndent()
444 && compareWith->GetLeftSubIndent() == style.GetLeftSubIndent()))
445 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
446 }
447
448 if (style.HasRightIndent())
449 {
450 if (!(compareWith && compareWith->HasRightIndent() && compareWith->GetRightIndent() == style.GetRightIndent()))
451 destStyle.SetRightIndent(style.GetRightIndent());
452 }
453
454 if (style.HasParagraphSpacingAfter())
455 {
456 if (!(compareWith && compareWith->HasParagraphSpacingAfter() && compareWith->GetParagraphSpacingAfter() == style.GetParagraphSpacingAfter()))
457 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
458 }
459
460 if (style.HasParagraphSpacingBefore())
461 {
462 if (!(compareWith && compareWith->HasParagraphSpacingBefore() && compareWith->GetParagraphSpacingBefore() == style.GetParagraphSpacingBefore()))
463 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
464 }
465
466 if (style.HasLineSpacing())
467 {
468 if (!(compareWith && compareWith->HasLineSpacing() && compareWith->GetLineSpacing() == style.GetLineSpacing()))
469 destStyle.SetLineSpacing(style.GetLineSpacing());
470 }
471
472 if (style.HasCharacterStyleName())
473 {
474 if (!(compareWith && compareWith->HasCharacterStyleName() && compareWith->GetCharacterStyleName() == style.GetCharacterStyleName()))
475 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
476 }
477
478 if (style.HasParagraphStyleName())
479 {
480 if (!(compareWith && compareWith->HasParagraphStyleName() && compareWith->GetParagraphStyleName() == style.GetParagraphStyleName()))
481 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
482 }
483
484 if (style.HasListStyleName())
485 {
486 if (!(compareWith && compareWith->HasListStyleName() && compareWith->GetListStyleName() == style.GetListStyleName()))
487 destStyle.SetListStyleName(style.GetListStyleName());
488 }
489
490 if (style.HasBulletStyle())
491 {
492 if (!(compareWith && compareWith->HasBulletStyle() && compareWith->GetBulletStyle() == style.GetBulletStyle()))
493 destStyle.SetBulletStyle(style.GetBulletStyle());
494 }
495
496 if (style.HasBulletText())
497 {
498 if (!(compareWith && compareWith->HasBulletText() && compareWith->GetBulletText() == style.GetBulletText()))
499 {
500 destStyle.SetBulletText(style.GetBulletText());
501 destStyle.SetBulletFont(style.GetBulletFont());
502 }
503 }
504
505 if (style.HasBulletNumber())
506 {
507 if (!(compareWith && compareWith->HasBulletNumber() && compareWith->GetBulletNumber() == style.GetBulletNumber()))
508 destStyle.SetBulletNumber(style.GetBulletNumber());
509 }
510
511 if (style.HasBulletName())
512 {
513 if (!(compareWith && compareWith->HasBulletName() && compareWith->GetBulletName() == style.GetBulletName()))
514 destStyle.SetBulletName(style.GetBulletName());
515 }
516
517 if (style.HasURL())
518 {
519 if (!(compareWith && compareWith->HasURL() && compareWith->GetURL() == style.GetURL()))
520 destStyle.SetURL(style.GetURL());
521 }
522
523 if (style.HasPageBreak())
524 {
525 if (!(compareWith && compareWith->HasPageBreak()))
526 destStyle.SetPageBreak();
527 }
528
529 if (style.HasTextEffects())
530 {
531 if (!(compareWith && compareWith->HasTextEffects() && compareWith->GetTextEffects() == style.GetTextEffects()))
532 {
533 int destBits = destStyle.GetTextEffects();
534 int destFlags = destStyle.GetTextEffectFlags();
535
536 int srcBits = style.GetTextEffects();
537 int srcFlags = style.GetTextEffectFlags();
538
d1e5be0e
JS
539 // Reset incompatible bits in the destination
540 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_SUPERSCRIPT|wxTEXT_ATTR_EFFECT_SUBSCRIPT), srcFlags, destFlags, destBits);
541 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_CAPITALS|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS), srcFlags, destFlags, destBits);
542 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_STRIKETHROUGH|wxTEXT_ATTR_EFFECT_DOUBLE_STRIKETHROUGH), srcFlags, destFlags, destBits);
543
44cc96a8
JS
544 CombineBitlists(destBits, srcBits, destFlags, srcFlags);
545
546 destStyle.SetTextEffects(destBits);
547 destStyle.SetTextEffectFlags(destFlags);
548 }
549 }
550
551 if (style.HasOutlineLevel())
552 {
553 if (!(compareWith && compareWith->HasOutlineLevel() && compareWith->GetOutlineLevel() == style.GetOutlineLevel()))
554 destStyle.SetOutlineLevel(style.GetOutlineLevel());
555 }
556
557 return true;
e00a5d3c
JS
558}
559
eda40bfc
VZ
560/* static */
561wxTextAttr wxTextAttr::Combine(const wxTextAttr& attr,
562 const wxTextAttr& attrDef,
563 const wxTextCtrlBase *text)
564{
7c3c990e
JS
565 wxFont font;
566 if (attr.HasFont())
567 font = attr.GetFont();
568
eda40bfc
VZ
569 if ( !font.Ok() )
570 {
7c3c990e
JS
571 if (attrDef.HasFont())
572 font = attrDef.GetFont();
eda40bfc
VZ
573
574 if ( text && !font.Ok() )
575 font = text->GetFont();
576 }
577
578 wxColour colFg = attr.GetTextColour();
579 if ( !colFg.Ok() )
580 {
581 colFg = attrDef.GetTextColour();
582
583 if ( text && !colFg.Ok() )
584 colFg = text->GetForegroundColour();
585 }
586
587 wxColour colBg = attr.GetBackgroundColour();
588 if ( !colBg.Ok() )
589 {
590 colBg = attrDef.GetBackgroundColour();
591
592 if ( text && !colBg.Ok() )
593 colBg = text->GetBackgroundColour();
594 }
595
e00a5d3c 596 wxTextAttr newAttr(colFg, colBg, font);
cb719f2e 597
e00a5d3c
JS
598 if (attr.HasAlignment())
599 newAttr.SetAlignment(attr.GetAlignment());
600 else if (attrDef.HasAlignment())
601 newAttr.SetAlignment(attrDef.GetAlignment());
cb719f2e 602
e00a5d3c
JS
603 if (attr.HasTabs())
604 newAttr.SetTabs(attr.GetTabs());
605 else if (attrDef.HasTabs())
606 newAttr.SetTabs(attrDef.GetTabs());
cb719f2e 607
e00a5d3c 608 if (attr.HasLeftIndent())
89b67477 609 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
e00a5d3c 610 else if (attrDef.HasLeftIndent())
89b67477 611 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
cb719f2e 612
e00a5d3c
JS
613 if (attr.HasRightIndent())
614 newAttr.SetRightIndent(attr.GetRightIndent());
615 else if (attrDef.HasRightIndent())
cb719f2e
WS
616 newAttr.SetRightIndent(attrDef.GetRightIndent());
617
e00a5d3c 618 return newAttr;
eda40bfc
VZ
619}
620
44cc96a8
JS
621/// Compare tabs
622bool wxTextAttr::TabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
e00a5d3c 623{
44cc96a8
JS
624 if (tabs1.GetCount() != tabs2.GetCount())
625 return false;
626
627 size_t i;
628 for (i = 0; i < tabs1.GetCount(); i++)
629 {
630 if (tabs1[i] != tabs2[i])
631 return false;
632 }
633 return true;
634}
635
636// Remove attributes
637bool wxTextAttr::RemoveStyle(wxTextAttr& destStyle, const wxTextAttr& style)
638{
639 int flags = style.GetFlags();
640 int destFlags = destStyle.GetFlags();
641
642 destStyle.SetFlags(destFlags & ~flags);
643
644 return true;
645}
646
647/// Combine two bitlists, specifying the bits of interest with separate flags.
648bool wxTextAttr::CombineBitlists(int& valueA, int valueB, int& flagsA, int flagsB)
649{
650 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
651 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
652 // indicate it.
653
654 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
655 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
656 int valueA2 = ~(~valueB & flagsB) & valueA;
657
658 // Now combine the 1 bits.
659 int valueA3 = (valueB & flagsB) | valueA2;
660
661 valueA = valueA3;
662 flagsA = (flagsA | flagsB);
663
664 return true;
665}
666
667/// Compare two bitlists
668bool wxTextAttr::BitlistsEqPartial(int valueA, int valueB, int flags)
669{
670 int relevantBitsA = valueA & flags;
671 int relevantBitsB = valueB & flags;
672 return (relevantBitsA != relevantBitsB);
e00a5d3c
JS
673}
674
44cc96a8
JS
675/// Split into paragraph and character styles
676bool wxTextAttr::SplitParaCharStyles(const wxTextAttr& style, wxTextAttr& parStyle, wxTextAttr& charStyle)
677{
678 wxTextAttr defaultCharStyle1(style);
679 wxTextAttr defaultParaStyle1(style);
680 defaultCharStyle1.SetFlags(defaultCharStyle1.GetFlags()&wxTEXT_ATTR_CHARACTER);
681 defaultParaStyle1.SetFlags(defaultParaStyle1.GetFlags()&wxTEXT_ATTR_PARAGRAPH);
682
683 charStyle.Apply(defaultCharStyle1);
684 parStyle.Apply(defaultParaStyle1);
685
686 return true;
687}
e00a5d3c 688
4bc1afd5
VZ
689// apply styling to text range
690bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
691 const wxTextAttr& WXUNUSED(style))
692{
0ec1179b 693 // to be implemented in derived classes
cb719f2e 694 return false;
4bc1afd5
VZ
695}
696
e00a5d3c
JS
697// get the styling at the given position
698bool wxTextCtrlBase::GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style))
699{
0ec1179b 700 // to be implemented in derived classes
cb719f2e 701 return false;
e00a5d3c
JS
702}
703
4bc1afd5 704// change default text attributes
eda40bfc 705bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr& style)
4bc1afd5 706{
c598f225
VZ
707 // keep the old attributes if the new style doesn't specify them unless the
708 // new style is empty - then reset m_defaultStyle (as there is no other way
709 // to do it)
710 if ( style.IsDefault() )
711 m_defaultStyle = style;
712 else
713 m_defaultStyle = wxTextAttr::Combine(style, m_defaultStyle, this);
eda40bfc 714
cb719f2e 715 return true;
4bc1afd5
VZ
716}
717
a1b82138
VZ
718// ----------------------------------------------------------------------------
719// file IO functions
720// ----------------------------------------------------------------------------
721
3306aec1 722bool wxTextCtrlBase::DoLoadFile(const wxString& filename, int WXUNUSED(fileType))
a1b82138 723{
1e6feb95 724#if wxUSE_FFILE
a1b82138
VZ
725 wxFFile file(filename);
726 if ( file.IsOpened() )
727 {
728 wxString text;
729 if ( file.ReadAll(&text) )
730 {
731 SetValue(text);
732
733 DiscardEdits();
734
735 m_filename = filename;
736
cb719f2e 737 return true;
a1b82138
VZ
738 }
739 }
740
741 wxLogError(_("File couldn't be loaded."));
1e6feb95 742#endif // wxUSE_FFILE
a1b82138 743
cb719f2e 744 return false;
a1b82138
VZ
745}
746
0ec1179b 747bool wxTextAreaBase::SaveFile(const wxString& filename, int fileType)
a1b82138 748{
7d8268a1 749 wxString filenameToUse = filename.empty() ? m_filename : filename;
64dcc269 750 if ( filenameToUse.empty() )
a1b82138
VZ
751 {
752 // what kind of message to give? is it an error or a program bug?
223d09f6 753 wxLogDebug(wxT("Can't save textctrl to file without filename."));
a1b82138 754
cb719f2e 755 return false;
a1b82138
VZ
756 }
757
3306aec1
JS
758 return DoSaveFile(filenameToUse, fileType);
759}
760
761bool wxTextCtrlBase::DoSaveFile(const wxString& filename, int WXUNUSED(fileType))
762{
1e6feb95 763#if wxUSE_FFILE
3306aec1 764 wxFFile file(filename, _T("w"));
a1b82138
VZ
765 if ( file.IsOpened() && file.Write(GetValue()) )
766 {
3306aec1
JS
767 // if it worked, save for future calls
768 m_filename = filename;
ee2ec18e 769
a1b82138
VZ
770 // it's not modified any longer
771 DiscardEdits();
772
cb719f2e 773 return true;
a1b82138 774 }
64dcc269 775#endif // wxUSE_FFILE
a1b82138
VZ
776
777 wxLogError(_("The text couldn't be saved."));
778
cb719f2e 779 return false;
a1b82138
VZ
780}
781
782// ----------------------------------------------------------------------------
783// stream-like insertion operator
784// ----------------------------------------------------------------------------
785
786wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
787{
788 AppendText(s);
789 return *TEXTCTRL(this);
790}
791
a1b82138
VZ
792wxTextCtrl& wxTextCtrlBase::operator<<(double d)
793{
9e50ed28 794 return *this << wxString::Format("%.2f", d);
a1b82138
VZ
795}
796
797wxTextCtrl& wxTextCtrlBase::operator<<(int i)
798{
9e50ed28 799 return *this << wxString::Format("%d", i);
a1b82138
VZ
800}
801
9e50ed28 802wxTextCtrl& wxTextCtrlBase::operator<<(long l)
a1b82138 803{
9e50ed28 804 return *this << wxString::Format("%ld", l);
a1b82138
VZ
805}
806
807// ----------------------------------------------------------------------------
808// streambuf methods implementation
809// ----------------------------------------------------------------------------
810
15cdc341 811#if wxHAS_TEXT_WINDOW_STREAM
a1b82138 812
d73e6791 813int wxTextCtrlBase::overflow(int c)
a1b82138 814{
d73e6791 815 AppendText((wxChar)c);
a1b82138 816
d73e6791 817 // return something different from EOF
a1b82138
VZ
818 return 0;
819}
820
15cdc341 821#endif // wxHAS_TEXT_WINDOW_STREAM
a1b82138 822
94af7d45
VZ
823// ----------------------------------------------------------------------------
824// emulating key presses
825// ----------------------------------------------------------------------------
826
827bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent& event)
828{
0ec1179b
VZ
829 // we have a native implementation for Win32 and so don't need this one
830#ifndef __WIN32__
02a48e3b 831 wxChar ch = 0;
94af7d45
VZ
832 int keycode = event.GetKeyCode();
833 switch ( keycode )
834 {
835 case WXK_NUMPAD0:
836 case WXK_NUMPAD1:
837 case WXK_NUMPAD2:
838 case WXK_NUMPAD3:
839 case WXK_NUMPAD4:
840 case WXK_NUMPAD5:
841 case WXK_NUMPAD6:
842 case WXK_NUMPAD7:
843 case WXK_NUMPAD8:
844 case WXK_NUMPAD9:
7a893a31 845 ch = (wxChar)(_T('0') + keycode - WXK_NUMPAD0);
94af7d45
VZ
846 break;
847
848 case WXK_MULTIPLY:
849 case WXK_NUMPAD_MULTIPLY:
850 ch = _T('*');
851 break;
852
853 case WXK_ADD:
854 case WXK_NUMPAD_ADD:
855 ch = _T('+');
856 break;
857
858 case WXK_SUBTRACT:
859 case WXK_NUMPAD_SUBTRACT:
860 ch = _T('-');
861 break;
862
863 case WXK_DECIMAL:
864 case WXK_NUMPAD_DECIMAL:
865 ch = _T('.');
866 break;
867
868 case WXK_DIVIDE:
869 case WXK_NUMPAD_DIVIDE:
870 ch = _T('/');
871 break;
872
cd916794
VZ
873 case WXK_DELETE:
874 case WXK_NUMPAD_DELETE:
875 // delete the character at cursor
876 {
7d8268a1
WS
877 const long pos = GetInsertionPoint();
878 if ( pos < GetLastPosition() )
cd916794
VZ
879 Remove(pos, pos + 1);
880 }
881 break;
882
883 case WXK_BACK:
884 // delete the character before the cursor
885 {
886 const long pos = GetInsertionPoint();
887 if ( pos > 0 )
888 Remove(pos - 1, pos);
889 }
890 break;
891
94af7d45 892 default:
c4d25c01
VS
893#if wxUSE_UNICODE
894 if ( event.GetUnicodeKey() )
895 {
896 ch = event.GetUnicodeKey();
897 }
898 else
899#endif
401eb3de 900 if ( keycode < 256 && keycode >= 0 && wxIsprint(keycode) )
94af7d45
VZ
901 {
902 // FIXME this is not going to work for non letters...
903 if ( !event.ShiftDown() )
904 {
401eb3de 905 keycode = wxTolower(keycode);
94af7d45
VZ
906 }
907
908 ch = (wxChar)keycode;
909 }
910 else
911 {
912 ch = _T('\0');
913 }
914 }
915
916 if ( ch )
917 {
918 WriteText(ch);
919
cb719f2e 920 return true;
94af7d45 921 }
c8fe7fda
VZ
922#else // __WIN32__
923 wxUnusedVar(event);
924#endif // !__WIN32__/__WIN32__
94af7d45 925
0ec1179b 926 return false;
18414479
VZ
927}
928
e39af974
JS
929// do the window-specific processing after processing the update event
930void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
931{
a3a4105d
VZ
932 // call inherited, but skip the wxControl's version, and call directly the
933 // wxWindow's one instead, because the only reason why we are overriding this
934 // function is that we want to use SetValue() instead of wxControl::SetLabel()
935 wxWindowBase::DoUpdateWindowUI(event);
cb719f2e 936
a3a4105d 937 // update text
e39af974
JS
938 if ( event.GetSetText() )
939 {
940 if ( event.GetText() != GetValue() )
941 SetValue(event.GetText());
cb719f2e 942 }
e39af974
JS
943}
944
efe66bbc
VZ
945// ----------------------------------------------------------------------------
946// hit testing
947// ----------------------------------------------------------------------------
948
692c9b86 949wxTextCtrlHitTestResult
0ec1179b 950wxTextAreaBase::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
692c9b86
VZ
951{
952 // implement in terms of the other overload as the native ports typically
953 // can get the position and not (x, y) pair directly (although wxUniv
954 // directly gets x and y -- and so overrides this method as well)
955 long pos;
956 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
957
958 if ( rc != wxTE_HT_UNKNOWN )
959 {
960 PositionToXY(pos, x, y);
961 }
962
963 return rc;
964}
965
efe66bbc 966wxTextCtrlHitTestResult
0ec1179b 967wxTextAreaBase::HitTest(const wxPoint& WXUNUSED(pt), long * WXUNUSED(pos)) const
efe66bbc
VZ
968{
969 // not implemented
970 return wxTE_HT_UNKNOWN;
971}
e39af974 972
ee2ec18e
VZ
973// ----------------------------------------------------------------------------
974// events
975// ----------------------------------------------------------------------------
976
0ec1179b 977/* static */
fa2f57be 978bool wxTextCtrlBase::SendTextUpdatedEvent(wxWindow *win)
ee2ec18e 979{
0ec1179b 980 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, win->GetId());
ee2ec18e
VZ
981
982 // do not do this as it could be very inefficient if the text control
983 // contains a lot of text and we're not using ref-counted wxString
984 // implementation -- instead, event.GetString() will query the control for
985 // its current text if needed
0ec1179b 986 //event.SetString(win->GetValue());
ee2ec18e 987
0ec1179b 988 event.SetEventObject(win);
fa2f57be 989 return win->GetEventHandler()->ProcessEvent(event);
ee2ec18e
VZ
990}
991
ad0bae85
VZ
992#else // !wxUSE_TEXTCTRL
993
994// define this one even if !wxUSE_TEXTCTRL because it is also used by other
995// controls (wxComboBox and wxSpinCtrl)
ad0bae85
VZ
996
997DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED)
998
999#endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL