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