]> git.saurik.com Git - wxWidgets.git/blame - src/common/textcmn.cpp
fixing osx_cocoa
[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
f313deaa
PC
38extern WXDLLEXPORT_DATA(const char) wxTextCtrlNameStr[] = "text";
39
a1b82138
VZ
40// ----------------------------------------------------------------------------
41// macros
42// ----------------------------------------------------------------------------
43
44// we don't have any objects of type wxTextCtrlBase in the program, only
45// wxTextCtrl, so this cast is safe
46#define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
47
48// ============================================================================
49// implementation
50// ============================================================================
51
28953245
SC
52// ----------------------------------------------------------------------------
53// XTI
54// ----------------------------------------------------------------------------
55
56wxDEFINE_FLAGS( wxTextCtrlStyle )
57wxBEGIN_FLAGS( wxTextCtrlStyle )
58// new style border flags, we put them first to
59// use them for streaming out
60wxFLAGS_MEMBER(wxBORDER_SIMPLE)
61wxFLAGS_MEMBER(wxBORDER_SUNKEN)
62wxFLAGS_MEMBER(wxBORDER_DOUBLE)
63wxFLAGS_MEMBER(wxBORDER_RAISED)
64wxFLAGS_MEMBER(wxBORDER_STATIC)
65wxFLAGS_MEMBER(wxBORDER_NONE)
66
67// old style border flags
68wxFLAGS_MEMBER(wxSIMPLE_BORDER)
69wxFLAGS_MEMBER(wxSUNKEN_BORDER)
70wxFLAGS_MEMBER(wxDOUBLE_BORDER)
71wxFLAGS_MEMBER(wxRAISED_BORDER)
72wxFLAGS_MEMBER(wxSTATIC_BORDER)
73wxFLAGS_MEMBER(wxBORDER)
74
75// standard window styles
76wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
77wxFLAGS_MEMBER(wxCLIP_CHILDREN)
78wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
79wxFLAGS_MEMBER(wxWANTS_CHARS)
80wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
81wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
82wxFLAGS_MEMBER(wxVSCROLL)
83wxFLAGS_MEMBER(wxHSCROLL)
84
85wxFLAGS_MEMBER(wxTE_PROCESS_ENTER)
86wxFLAGS_MEMBER(wxTE_PROCESS_TAB)
87wxFLAGS_MEMBER(wxTE_MULTILINE)
88wxFLAGS_MEMBER(wxTE_PASSWORD)
89wxFLAGS_MEMBER(wxTE_READONLY)
90wxFLAGS_MEMBER(wxHSCROLL)
91wxFLAGS_MEMBER(wxTE_RICH)
92wxFLAGS_MEMBER(wxTE_RICH2)
93wxFLAGS_MEMBER(wxTE_AUTO_URL)
94wxFLAGS_MEMBER(wxTE_NOHIDESEL)
95wxFLAGS_MEMBER(wxTE_LEFT)
96wxFLAGS_MEMBER(wxTE_CENTRE)
97wxFLAGS_MEMBER(wxTE_RIGHT)
98wxFLAGS_MEMBER(wxTE_DONTWRAP)
99wxFLAGS_MEMBER(wxTE_CHARWRAP)
100wxFLAGS_MEMBER(wxTE_WORDWRAP)
101wxEND_FLAGS( wxTextCtrlStyle )
102
103wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl, wxControl, "wx/textctrl.h")
104
105wxBEGIN_PROPERTIES_TABLE(wxTextCtrl)
106wxEVENT_PROPERTY( TextUpdated, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent )
107wxEVENT_PROPERTY( TextEnter, wxEVT_COMMAND_TEXT_ENTER, wxCommandEvent )
108
109wxPROPERTY( Font, wxFont, SetFont, GetFont , wxEMPTY_PARAMETER_VALUE, \
110 0 /*flags*/, wxT("Helpstring"), wxT("group") )
111wxPROPERTY( Value, wxString, SetValue, GetValue, wxString(), \
112 0 /*flags*/, wxT("Helpstring"), wxT("group"))
113
114wxPROPERTY_FLAGS( WindowStyle, wxTextCtrlStyle, long, SetWindowStyleFlag, \
115 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
116 wxT("Helpstring"), wxT("group")) // style
117wxEND_PROPERTIES_TABLE()
118
119wxEMPTY_HANDLERS_TABLE(wxTextCtrl)
120
121wxCONSTRUCTOR_6( wxTextCtrl, wxWindow*, Parent, wxWindowID, Id, \
122 wxString, Value, wxPoint, Position, wxSize, Size, \
123 long, WindowStyle)
124
125
c57e3339
VZ
126IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent, wxCommandEvent)
127
9b11752c
VZ
128wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent );
129wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_ENTER, wxCommandEvent );
130wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_URL, wxTextUrlEvent );
131wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_MAXLEN, wxCommandEvent );
c57e3339 132
9d112688
JS
133IMPLEMENT_ABSTRACT_CLASS(wxTextCtrlBase, wxControl)
134
44cc96a8
JS
135// ============================================================================
136// wxTextAttr implementation
137// ============================================================================
4bc1afd5 138
e00a5d3c
JS
139wxTextAttr::wxTextAttr(const wxColour& colText,
140 const wxColour& colBack,
141 const wxFont& font,
44cc96a8 142 wxTextAttrAlignment alignment): m_textAlignment(alignment), m_colText(colText), m_colBack(colBack)
e00a5d3c 143{
44cc96a8
JS
144 Init();
145
a1b806b9
DS
146 if (m_colText.IsOk()) m_flags |= wxTEXT_ATTR_TEXT_COLOUR;
147 if (m_colBack.IsOk()) m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR;
e00a5d3c
JS
148 if (alignment != wxTEXT_ALIGNMENT_DEFAULT)
149 m_flags |= wxTEXT_ATTR_ALIGNMENT;
44cc96a8
JS
150
151 GetFontAttributes(font);
e00a5d3c
JS
152}
153
44cc96a8 154// Initialisation
e00a5d3c
JS
155void wxTextAttr::Init()
156{
157 m_textAlignment = wxTEXT_ALIGNMENT_DEFAULT;
158 m_flags = 0;
159 m_leftIndent = 0;
89b67477 160 m_leftSubIndent = 0;
e00a5d3c 161 m_rightIndent = 0;
44cc96a8
JS
162
163 m_fontSize = 12;
7d76fbd5
FM
164 m_fontStyle = wxFONTSTYLE_NORMAL;
165 m_fontWeight = wxFONTWEIGHT_NORMAL;
44cc96a8
JS
166 m_fontUnderlined = false;
167 m_fontEncoding = wxFONTENCODING_DEFAULT;
9c4cb611 168 m_fontFamily = wxFONTFAMILY_DEFAULT;
44cc96a8
JS
169
170 m_paragraphSpacingAfter = 0;
171 m_paragraphSpacingBefore = 0;
172 m_lineSpacing = 0;
173 m_bulletStyle = wxTEXT_ATTR_BULLET_STYLE_NONE;
174 m_textEffects = wxTEXT_ATTR_EFFECT_NONE;
175 m_textEffectFlags = wxTEXT_ATTR_EFFECT_NONE;
176 m_outlineLevel = 0;
177 m_bulletNumber = 0;
178}
179
180// Copy
181void wxTextAttr::Copy(const wxTextAttr& attr)
182{
183 m_colText = attr.m_colText;
184 m_colBack = attr.m_colBack;
185 m_textAlignment = attr.m_textAlignment;
186 m_leftIndent = attr.m_leftIndent;
187 m_leftSubIndent = attr.m_leftSubIndent;
188 m_rightIndent = attr.m_rightIndent;
189 m_tabs = attr.m_tabs;
190 m_flags = attr.m_flags;
191
192 m_fontSize = attr.m_fontSize;
193 m_fontStyle = attr.m_fontStyle;
194 m_fontWeight = attr.m_fontWeight;
195 m_fontUnderlined = attr.m_fontUnderlined;
196 m_fontFaceName = attr.m_fontFaceName;
197 m_fontEncoding = attr.m_fontEncoding;
9c4cb611 198 m_fontFamily = attr.m_fontFamily;
44cc96a8
JS
199 m_textEffects = attr.m_textEffects;
200 m_textEffectFlags = attr.m_textEffectFlags;
201
202 m_paragraphSpacingAfter = attr.m_paragraphSpacingAfter;
203 m_paragraphSpacingBefore = attr.m_paragraphSpacingBefore;
204 m_lineSpacing = attr.m_lineSpacing;
205 m_characterStyleName = attr.m_characterStyleName;
206 m_paragraphStyleName = attr.m_paragraphStyleName;
207 m_listStyleName = attr.m_listStyleName;
208 m_bulletStyle = attr.m_bulletStyle;
209 m_bulletNumber = attr.m_bulletNumber;
210 m_bulletText = attr.m_bulletText;
211 m_bulletFont = attr.m_bulletFont;
212 m_bulletName = attr.m_bulletName;
213 m_outlineLevel = attr.m_outlineLevel;
214
215 m_urlTarget = attr.m_urlTarget;
216}
217
218// operators
219void wxTextAttr::operator= (const wxTextAttr& attr)
220{
221 Copy(attr);
222}
223
224// Equality test
225bool wxTextAttr::operator== (const wxTextAttr& attr) const
226{
227 return GetFlags() == attr.GetFlags() &&
228
229 GetTextColour() == attr.GetTextColour() &&
230 GetBackgroundColour() == attr.GetBackgroundColour() &&
231
232 GetAlignment() == attr.GetAlignment() &&
233 GetLeftIndent() == attr.GetLeftIndent() &&
234 GetLeftSubIndent() == attr.GetLeftSubIndent() &&
235 GetRightIndent() == attr.GetRightIndent() &&
236 TabsEq(GetTabs(), attr.GetTabs()) &&
237
238 GetParagraphSpacingAfter() == attr.GetParagraphSpacingAfter() &&
239 GetParagraphSpacingBefore() == attr.GetParagraphSpacingBefore() &&
240 GetLineSpacing() == attr.GetLineSpacing() &&
241 GetCharacterStyleName() == attr.GetCharacterStyleName() &&
242 GetParagraphStyleName() == attr.GetParagraphStyleName() &&
243 GetListStyleName() == attr.GetListStyleName() &&
244
245 GetBulletStyle() == attr.GetBulletStyle() &&
246 GetBulletText() == attr.GetBulletText() &&
247 GetBulletNumber() == attr.GetBulletNumber() &&
248 GetBulletFont() == attr.GetBulletFont() &&
249 GetBulletName() == attr.GetBulletName() &&
250
251 GetTextEffects() == attr.GetTextEffects() &&
252 GetTextEffectFlags() == attr.GetTextEffectFlags() &&
253
254 GetOutlineLevel() == attr.GetOutlineLevel() &&
255
256 GetFontSize() == attr.GetFontSize() &&
257 GetFontStyle() == attr.GetFontStyle() &&
258 GetFontWeight() == attr.GetFontWeight() &&
259 GetFontUnderlined() == attr.GetFontUnderlined() &&
260 GetFontFaceName() == attr.GetFontFaceName() &&
261 GetFontEncoding() == attr.GetFontEncoding() &&
9c4cb611 262 GetFontFamily() == attr.GetFontFamily() &&
44cc96a8
JS
263
264 GetURL() == attr.GetURL();
265}
266
24777478
JS
267// Partial equality test. Only returns false if an attribute doesn't match.
268bool wxTextAttr::EqPartial(const wxTextAttr& attr) const
44cc96a8 269{
24777478
JS
270 int flags = attr.GetFlags();
271
44cc96a8
JS
272 if ((flags & wxTEXT_ATTR_TEXT_COLOUR) && GetTextColour() != attr.GetTextColour())
273 return false;
274
275 if ((flags & wxTEXT_ATTR_BACKGROUND_COLOUR) && GetBackgroundColour() != attr.GetBackgroundColour())
276 return false;
277
278 if ((flags & wxTEXT_ATTR_FONT_FACE) &&
279 GetFontFaceName() != attr.GetFontFaceName())
280 return false;
281
282 if ((flags & wxTEXT_ATTR_FONT_SIZE) &&
283 GetFontSize() != attr.GetFontSize())
284 return false;
285
286 if ((flags & wxTEXT_ATTR_FONT_WEIGHT) &&
287 GetFontWeight() != attr.GetFontWeight())
288 return false;
289
290 if ((flags & wxTEXT_ATTR_FONT_ITALIC) &&
291 GetFontStyle() != attr.GetFontStyle())
292 return false;
293
294 if ((flags & wxTEXT_ATTR_FONT_UNDERLINE) &&
295 GetFontUnderlined() != attr.GetFontUnderlined())
296 return false;
297
298 if ((flags & wxTEXT_ATTR_FONT_ENCODING) &&
299 GetFontEncoding() != attr.GetFontEncoding())
300 return false;
301
9c4cb611
JS
302 if ((flags & wxTEXT_ATTR_FONT_FAMILY) &&
303 GetFontFamily() != attr.GetFontFamily())
304 return false;
305
44cc96a8
JS
306 if ((flags & wxTEXT_ATTR_URL) && GetURL() != attr.GetURL())
307 return false;
308
309 if ((flags & wxTEXT_ATTR_ALIGNMENT) && GetAlignment() != attr.GetAlignment())
310 return false;
311
312 if ((flags & wxTEXT_ATTR_LEFT_INDENT) &&
313 ((GetLeftIndent() != attr.GetLeftIndent()) || (GetLeftSubIndent() != attr.GetLeftSubIndent())))
314 return false;
315
316 if ((flags & wxTEXT_ATTR_RIGHT_INDENT) &&
317 (GetRightIndent() != attr.GetRightIndent()))
318 return false;
319
320 if ((flags & wxTEXT_ATTR_PARA_SPACING_AFTER) &&
321 (GetParagraphSpacingAfter() != attr.GetParagraphSpacingAfter()))
322 return false;
323
324 if ((flags & wxTEXT_ATTR_PARA_SPACING_BEFORE) &&
325 (GetParagraphSpacingBefore() != attr.GetParagraphSpacingBefore()))
326 return false;
327
328 if ((flags & wxTEXT_ATTR_LINE_SPACING) &&
329 (GetLineSpacing() != attr.GetLineSpacing()))
330 return false;
331
332 if ((flags & wxTEXT_ATTR_CHARACTER_STYLE_NAME) &&
333 (GetCharacterStyleName() != attr.GetCharacterStyleName()))
334 return false;
335
336 if ((flags & wxTEXT_ATTR_PARAGRAPH_STYLE_NAME) &&
337 (GetParagraphStyleName() != attr.GetParagraphStyleName()))
338 return false;
339
340 if ((flags & wxTEXT_ATTR_LIST_STYLE_NAME) &&
341 (GetListStyleName() != attr.GetListStyleName()))
342 return false;
343
344 if ((flags & wxTEXT_ATTR_BULLET_STYLE) &&
345 (GetBulletStyle() != attr.GetBulletStyle()))
346 return false;
347
348 if ((flags & wxTEXT_ATTR_BULLET_NUMBER) &&
349 (GetBulletNumber() != attr.GetBulletNumber()))
350 return false;
351
352 if ((flags & wxTEXT_ATTR_BULLET_TEXT) &&
353 (GetBulletText() != attr.GetBulletText()) &&
354 (GetBulletFont() != attr.GetBulletFont()))
355 return false;
356
357 if ((flags & wxTEXT_ATTR_BULLET_NAME) &&
358 (GetBulletName() != attr.GetBulletName()))
359 return false;
360
361 if ((flags & wxTEXT_ATTR_TABS) &&
362 !TabsEq(GetTabs(), attr.GetTabs()))
363 return false;
364
365 if ((flags & wxTEXT_ATTR_PAGE_BREAK) &&
366 (HasPageBreak() != attr.HasPageBreak()))
367 return false;
368
369 if (flags & wxTEXT_ATTR_EFFECTS)
370 {
371 if (HasTextEffects() != attr.HasTextEffects())
372 return false;
373 if (!BitlistsEqPartial(GetTextEffects(), attr.GetTextEffects(), attr.GetTextEffectFlags()))
374 return false;
375 }
376
377 if ((flags & wxTEXT_ATTR_OUTLINE_LEVEL) &&
378 (GetOutlineLevel() != attr.GetOutlineLevel()))
379 return false;
380
381 return true;
382}
383
384// Create font from font attributes.
1fee6e25 385wxFont wxTextAttr::GetFont() const
44cc96a8 386{
1fee6e25
VZ
387 if ( !HasFont() )
388 return wxNullFont;
389
44cc96a8
JS
390 int fontSize = 10;
391 if (HasFontSize())
392 fontSize = GetFontSize();
393
394 int fontStyle = wxNORMAL;
395 if (HasFontItalic())
396 fontStyle = GetFontStyle();
397
398 int fontWeight = wxNORMAL;
399 if (HasFontWeight())
400 fontWeight = GetFontWeight();
401
402 bool underlined = false;
403 if (HasFontUnderlined())
404 underlined = GetFontUnderlined();
405
406 wxString fontFaceName;
407 if (HasFontFaceName())
408 fontFaceName = GetFontFaceName();
409
410 wxFontEncoding encoding = wxFONTENCODING_DEFAULT;
411 if (HasFontEncoding())
412 encoding = GetFontEncoding();
413
aa926768 414 wxFontFamily fontFamily = wxFONTFAMILY_DEFAULT;
9c4cb611
JS
415 if (HasFontFamily())
416 fontFamily = GetFontFamily();
417
418 wxFont font(fontSize, fontFamily, fontStyle, fontWeight, underlined, fontFaceName, encoding);
44cc96a8
JS
419 return font;
420}
421
422// Get attributes from font.
423bool wxTextAttr::GetFontAttributes(const wxFont& font, int flags)
424{
a1b806b9 425 if (!font.IsOk())
44cc96a8
JS
426 return false;
427
428 if (flags & wxTEXT_ATTR_FONT_SIZE)
429 m_fontSize = font.GetPointSize();
430
431 if (flags & wxTEXT_ATTR_FONT_ITALIC)
432 m_fontStyle = font.GetStyle();
433
434 if (flags & wxTEXT_ATTR_FONT_WEIGHT)
435 m_fontWeight = font.GetWeight();
436
437 if (flags & wxTEXT_ATTR_FONT_UNDERLINE)
438 m_fontUnderlined = font.GetUnderlined();
439
440 if (flags & wxTEXT_ATTR_FONT_FACE)
441 m_fontFaceName = font.GetFaceName();
442
443 if (flags & wxTEXT_ATTR_FONT_ENCODING)
444 m_fontEncoding = font.GetEncoding();
445
9c4cb611 446 if (flags & wxTEXT_ATTR_FONT_FAMILY)
aa926768
VZ
447 {
448 // wxFont might not know its family, avoid setting m_fontFamily to an
449 // invalid value and rather pretend that we don't have any font family
450 // information at all in this case
451 const wxFontFamily fontFamily = font.GetFamily();
452 if ( fontFamily == wxFONTFAMILY_UNKNOWN )
453 flags &= ~wxTEXT_ATTR_FONT_FAMILY;
454 else
455 m_fontFamily = fontFamily;
456 }
9c4cb611 457
c4076626
JS
458 m_flags |= flags;
459
44cc96a8
JS
460 return true;
461}
462
d1e5be0e
JS
463// Resets bits in destination so new attributes aren't merged with mutually exclusive ones
464static bool wxResetIncompatibleBits(const int mask, const int srcFlags, int& destFlags, int& destBits)
465{
466 if ((srcFlags & mask) && (destFlags & mask))
467 {
468 destBits &= ~mask;
469 destFlags &= ~mask;
470 }
471
472 return true;
473}
474
44cc96a8
JS
475bool wxTextAttr::Apply(const wxTextAttr& style, const wxTextAttr* compareWith)
476{
477 wxTextAttr& destStyle = (*this);
478
479 if (style.HasFontWeight())
480 {
481 if (!(compareWith && compareWith->HasFontWeight() && compareWith->GetFontWeight() == style.GetFontWeight()))
482 destStyle.SetFontWeight(style.GetFontWeight());
483 }
484
485 if (style.HasFontSize())
486 {
487 if (!(compareWith && compareWith->HasFontSize() && compareWith->GetFontSize() == style.GetFontSize()))
488 destStyle.SetFontSize(style.GetFontSize());
489 }
490
491 if (style.HasFontItalic())
492 {
493 if (!(compareWith && compareWith->HasFontItalic() && compareWith->GetFontStyle() == style.GetFontStyle()))
494 destStyle.SetFontStyle(style.GetFontStyle());
495 }
496
497 if (style.HasFontUnderlined())
498 {
499 if (!(compareWith && compareWith->HasFontUnderlined() && compareWith->GetFontUnderlined() == style.GetFontUnderlined()))
500 destStyle.SetFontUnderlined(style.GetFontUnderlined());
501 }
502
503 if (style.HasFontFaceName())
504 {
505 if (!(compareWith && compareWith->HasFontFaceName() && compareWith->GetFontFaceName() == style.GetFontFaceName()))
506 destStyle.SetFontFaceName(style.GetFontFaceName());
507 }
508
509 if (style.HasFontEncoding())
510 {
511 if (!(compareWith && compareWith->HasFontEncoding() && compareWith->GetFontEncoding() == style.GetFontEncoding()))
512 destStyle.SetFontEncoding(style.GetFontEncoding());
513 }
514
9c4cb611
JS
515 if (style.HasFontFamily())
516 {
517 if (!(compareWith && compareWith->HasFontFamily() && compareWith->GetFontFamily() == style.GetFontFamily()))
518 destStyle.SetFontFamily(style.GetFontFamily());
519 }
520
a1b806b9 521 if (style.GetTextColour().IsOk() && style.HasTextColour())
44cc96a8
JS
522 {
523 if (!(compareWith && compareWith->HasTextColour() && compareWith->GetTextColour() == style.GetTextColour()))
524 destStyle.SetTextColour(style.GetTextColour());
525 }
526
a1b806b9 527 if (style.GetBackgroundColour().IsOk() && style.HasBackgroundColour())
44cc96a8
JS
528 {
529 if (!(compareWith && compareWith->HasBackgroundColour() && compareWith->GetBackgroundColour() == style.GetBackgroundColour()))
530 destStyle.SetBackgroundColour(style.GetBackgroundColour());
531 }
532
533 if (style.HasAlignment())
534 {
535 if (!(compareWith && compareWith->HasAlignment() && compareWith->GetAlignment() == style.GetAlignment()))
536 destStyle.SetAlignment(style.GetAlignment());
537 }
538
539 if (style.HasTabs())
540 {
541 if (!(compareWith && compareWith->HasTabs() && TabsEq(compareWith->GetTabs(), style.GetTabs())))
542 destStyle.SetTabs(style.GetTabs());
543 }
544
545 if (style.HasLeftIndent())
546 {
547 if (!(compareWith && compareWith->HasLeftIndent() && compareWith->GetLeftIndent() == style.GetLeftIndent()
548 && compareWith->GetLeftSubIndent() == style.GetLeftSubIndent()))
549 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
550 }
551
552 if (style.HasRightIndent())
553 {
554 if (!(compareWith && compareWith->HasRightIndent() && compareWith->GetRightIndent() == style.GetRightIndent()))
555 destStyle.SetRightIndent(style.GetRightIndent());
556 }
557
558 if (style.HasParagraphSpacingAfter())
559 {
560 if (!(compareWith && compareWith->HasParagraphSpacingAfter() && compareWith->GetParagraphSpacingAfter() == style.GetParagraphSpacingAfter()))
561 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
562 }
563
564 if (style.HasParagraphSpacingBefore())
565 {
566 if (!(compareWith && compareWith->HasParagraphSpacingBefore() && compareWith->GetParagraphSpacingBefore() == style.GetParagraphSpacingBefore()))
567 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
568 }
569
570 if (style.HasLineSpacing())
571 {
572 if (!(compareWith && compareWith->HasLineSpacing() && compareWith->GetLineSpacing() == style.GetLineSpacing()))
573 destStyle.SetLineSpacing(style.GetLineSpacing());
574 }
575
576 if (style.HasCharacterStyleName())
577 {
578 if (!(compareWith && compareWith->HasCharacterStyleName() && compareWith->GetCharacterStyleName() == style.GetCharacterStyleName()))
579 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
580 }
581
582 if (style.HasParagraphStyleName())
583 {
584 if (!(compareWith && compareWith->HasParagraphStyleName() && compareWith->GetParagraphStyleName() == style.GetParagraphStyleName()))
585 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
586 }
587
588 if (style.HasListStyleName())
589 {
590 if (!(compareWith && compareWith->HasListStyleName() && compareWith->GetListStyleName() == style.GetListStyleName()))
591 destStyle.SetListStyleName(style.GetListStyleName());
592 }
593
594 if (style.HasBulletStyle())
595 {
596 if (!(compareWith && compareWith->HasBulletStyle() && compareWith->GetBulletStyle() == style.GetBulletStyle()))
597 destStyle.SetBulletStyle(style.GetBulletStyle());
598 }
599
600 if (style.HasBulletText())
601 {
602 if (!(compareWith && compareWith->HasBulletText() && compareWith->GetBulletText() == style.GetBulletText()))
603 {
604 destStyle.SetBulletText(style.GetBulletText());
605 destStyle.SetBulletFont(style.GetBulletFont());
606 }
607 }
608
609 if (style.HasBulletNumber())
610 {
611 if (!(compareWith && compareWith->HasBulletNumber() && compareWith->GetBulletNumber() == style.GetBulletNumber()))
612 destStyle.SetBulletNumber(style.GetBulletNumber());
613 }
614
615 if (style.HasBulletName())
616 {
617 if (!(compareWith && compareWith->HasBulletName() && compareWith->GetBulletName() == style.GetBulletName()))
618 destStyle.SetBulletName(style.GetBulletName());
619 }
620
621 if (style.HasURL())
622 {
623 if (!(compareWith && compareWith->HasURL() && compareWith->GetURL() == style.GetURL()))
624 destStyle.SetURL(style.GetURL());
625 }
626
627 if (style.HasPageBreak())
628 {
629 if (!(compareWith && compareWith->HasPageBreak()))
630 destStyle.SetPageBreak();
631 }
632
633 if (style.HasTextEffects())
634 {
635 if (!(compareWith && compareWith->HasTextEffects() && compareWith->GetTextEffects() == style.GetTextEffects()))
636 {
637 int destBits = destStyle.GetTextEffects();
638 int destFlags = destStyle.GetTextEffectFlags();
639
640 int srcBits = style.GetTextEffects();
641 int srcFlags = style.GetTextEffectFlags();
642
d1e5be0e
JS
643 // Reset incompatible bits in the destination
644 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_SUPERSCRIPT|wxTEXT_ATTR_EFFECT_SUBSCRIPT), srcFlags, destFlags, destBits);
645 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_CAPITALS|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS), srcFlags, destFlags, destBits);
646 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_STRIKETHROUGH|wxTEXT_ATTR_EFFECT_DOUBLE_STRIKETHROUGH), srcFlags, destFlags, destBits);
647
44cc96a8
JS
648 CombineBitlists(destBits, srcBits, destFlags, srcFlags);
649
650 destStyle.SetTextEffects(destBits);
651 destStyle.SetTextEffectFlags(destFlags);
652 }
653 }
654
655 if (style.HasOutlineLevel())
656 {
657 if (!(compareWith && compareWith->HasOutlineLevel() && compareWith->GetOutlineLevel() == style.GetOutlineLevel()))
658 destStyle.SetOutlineLevel(style.GetOutlineLevel());
659 }
660
661 return true;
e00a5d3c
JS
662}
663
eda40bfc
VZ
664/* static */
665wxTextAttr wxTextAttr::Combine(const wxTextAttr& attr,
666 const wxTextAttr& attrDef,
667 const wxTextCtrlBase *text)
668{
7c3c990e
JS
669 wxFont font;
670 if (attr.HasFont())
671 font = attr.GetFont();
672
a1b806b9 673 if ( !font.IsOk() )
eda40bfc 674 {
7c3c990e
JS
675 if (attrDef.HasFont())
676 font = attrDef.GetFont();
eda40bfc 677
a1b806b9 678 if ( text && !font.IsOk() )
eda40bfc
VZ
679 font = text->GetFont();
680 }
681
682 wxColour colFg = attr.GetTextColour();
a1b806b9 683 if ( !colFg.IsOk() )
eda40bfc
VZ
684 {
685 colFg = attrDef.GetTextColour();
686
a1b806b9 687 if ( text && !colFg.IsOk() )
eda40bfc
VZ
688 colFg = text->GetForegroundColour();
689 }
690
691 wxColour colBg = attr.GetBackgroundColour();
a1b806b9 692 if ( !colBg.IsOk() )
eda40bfc
VZ
693 {
694 colBg = attrDef.GetBackgroundColour();
695
a1b806b9 696 if ( text && !colBg.IsOk() )
eda40bfc
VZ
697 colBg = text->GetBackgroundColour();
698 }
699
e00a5d3c 700 wxTextAttr newAttr(colFg, colBg, font);
cb719f2e 701
e00a5d3c
JS
702 if (attr.HasAlignment())
703 newAttr.SetAlignment(attr.GetAlignment());
704 else if (attrDef.HasAlignment())
705 newAttr.SetAlignment(attrDef.GetAlignment());
cb719f2e 706
e00a5d3c
JS
707 if (attr.HasTabs())
708 newAttr.SetTabs(attr.GetTabs());
709 else if (attrDef.HasTabs())
710 newAttr.SetTabs(attrDef.GetTabs());
cb719f2e 711
e00a5d3c 712 if (attr.HasLeftIndent())
89b67477 713 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
e00a5d3c 714 else if (attrDef.HasLeftIndent())
89b67477 715 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
cb719f2e 716
e00a5d3c
JS
717 if (attr.HasRightIndent())
718 newAttr.SetRightIndent(attr.GetRightIndent());
719 else if (attrDef.HasRightIndent())
cb719f2e
WS
720 newAttr.SetRightIndent(attrDef.GetRightIndent());
721
e00a5d3c 722 return newAttr;
eda40bfc
VZ
723}
724
44cc96a8
JS
725/// Compare tabs
726bool wxTextAttr::TabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
e00a5d3c 727{
44cc96a8
JS
728 if (tabs1.GetCount() != tabs2.GetCount())
729 return false;
730
731 size_t i;
732 for (i = 0; i < tabs1.GetCount(); i++)
733 {
734 if (tabs1[i] != tabs2[i])
735 return false;
736 }
737 return true;
738}
739
740// Remove attributes
741bool wxTextAttr::RemoveStyle(wxTextAttr& destStyle, const wxTextAttr& style)
742{
743 int flags = style.GetFlags();
744 int destFlags = destStyle.GetFlags();
745
746 destStyle.SetFlags(destFlags & ~flags);
747
748 return true;
749}
750
751/// Combine two bitlists, specifying the bits of interest with separate flags.
752bool wxTextAttr::CombineBitlists(int& valueA, int valueB, int& flagsA, int flagsB)
753{
754 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
755 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
756 // indicate it.
757
758 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
759 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
760 int valueA2 = ~(~valueB & flagsB) & valueA;
761
762 // Now combine the 1 bits.
763 int valueA3 = (valueB & flagsB) | valueA2;
764
765 valueA = valueA3;
766 flagsA = (flagsA | flagsB);
767
768 return true;
769}
770
771/// Compare two bitlists
772bool wxTextAttr::BitlistsEqPartial(int valueA, int valueB, int flags)
773{
774 int relevantBitsA = valueA & flags;
775 int relevantBitsB = valueB & flags;
776 return (relevantBitsA != relevantBitsB);
e00a5d3c
JS
777}
778
44cc96a8
JS
779/// Split into paragraph and character styles
780bool wxTextAttr::SplitParaCharStyles(const wxTextAttr& style, wxTextAttr& parStyle, wxTextAttr& charStyle)
781{
782 wxTextAttr defaultCharStyle1(style);
783 wxTextAttr defaultParaStyle1(style);
784 defaultCharStyle1.SetFlags(defaultCharStyle1.GetFlags()&wxTEXT_ATTR_CHARACTER);
785 defaultParaStyle1.SetFlags(defaultParaStyle1.GetFlags()&wxTEXT_ATTR_PARAGRAPH);
786
787 charStyle.Apply(defaultCharStyle1);
788 parStyle.Apply(defaultParaStyle1);
789
790 return true;
791}
e00a5d3c 792
4bc1afd5
VZ
793// apply styling to text range
794bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
795 const wxTextAttr& WXUNUSED(style))
796{
0ec1179b 797 // to be implemented in derived classes
cb719f2e 798 return false;
4bc1afd5
VZ
799}
800
e00a5d3c
JS
801// get the styling at the given position
802bool wxTextCtrlBase::GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style))
803{
0ec1179b 804 // to be implemented in derived classes
cb719f2e 805 return false;
e00a5d3c
JS
806}
807
4bc1afd5 808// change default text attributes
eda40bfc 809bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr& style)
4bc1afd5 810{
c598f225
VZ
811 // keep the old attributes if the new style doesn't specify them unless the
812 // new style is empty - then reset m_defaultStyle (as there is no other way
813 // to do it)
814 if ( style.IsDefault() )
815 m_defaultStyle = style;
816 else
817 m_defaultStyle = wxTextAttr::Combine(style, m_defaultStyle, this);
eda40bfc 818
cb719f2e 819 return true;
4bc1afd5
VZ
820}
821
a1b82138
VZ
822// ----------------------------------------------------------------------------
823// file IO functions
824// ----------------------------------------------------------------------------
825
3396739d 826bool wxTextAreaBase::DoLoadFile(const wxString& filename, int WXUNUSED(fileType))
a1b82138 827{
1e6feb95 828#if wxUSE_FFILE
a1b82138
VZ
829 wxFFile file(filename);
830 if ( file.IsOpened() )
831 {
832 wxString text;
833 if ( file.ReadAll(&text) )
834 {
3396739d 835 SetValue(text);
a1b82138 836
cb719f2e 837 return true;
a1b82138
VZ
838 }
839 }
3396739d
VZ
840#endif // wxUSE_FFILE
841
842 return false;
843}
a1b82138 844
3396739d
VZ
845bool wxTextCtrlBase::DoLoadFile(const wxString& filename, int fileType)
846{
847 if ( wxTextAreaBase::DoLoadFile(filename, fileType) )
848 {
849 DiscardEdits();
850 m_filename = filename;
851 return true;
852 }
a1b82138 853 wxLogError(_("File couldn't be loaded."));
3396739d
VZ
854 return false;
855}
a1b82138 856
3396739d
VZ
857bool wxTextAreaBase::DoSaveFile(const wxString& filename, int WXUNUSED(fileType))
858{
859#if wxUSE_FFILE
860 wxFFile file(filename, wxT("w"));
861 return file.IsOpened() && file.Write(GetValue(), *wxConvCurrent);
862#else
cb719f2e 863 return false;
3396739d 864#endif // wxUSE_FFILE
a1b82138
VZ
865}
866
0ec1179b 867bool wxTextAreaBase::SaveFile(const wxString& filename, int fileType)
a1b82138 868{
7d8268a1 869 wxString filenameToUse = filename.empty() ? m_filename : filename;
64dcc269 870 if ( filenameToUse.empty() )
a1b82138
VZ
871 {
872 // what kind of message to give? is it an error or a program bug?
223d09f6 873 wxLogDebug(wxT("Can't save textctrl to file without filename."));
a1b82138 874
cb719f2e 875 return false;
a1b82138
VZ
876 }
877
3306aec1
JS
878 return DoSaveFile(filenameToUse, fileType);
879}
880
3396739d 881bool wxTextCtrlBase::DoSaveFile(const wxString& filename, int fileType)
3306aec1 882{
3396739d 883 if ( wxTextAreaBase::DoSaveFile(filename, fileType) )
a1b82138 884 {
3306aec1
JS
885 // if it worked, save for future calls
886 m_filename = filename;
ee2ec18e 887
a1b82138
VZ
888 // it's not modified any longer
889 DiscardEdits();
890
cb719f2e 891 return true;
a1b82138 892 }
cb719f2e 893 return false;
a1b82138
VZ
894}
895
896// ----------------------------------------------------------------------------
897// stream-like insertion operator
898// ----------------------------------------------------------------------------
899
900wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
901{
902 AppendText(s);
903 return *TEXTCTRL(this);
904}
905
a1b82138
VZ
906wxTextCtrl& wxTextCtrlBase::operator<<(double d)
907{
9e50ed28 908 return *this << wxString::Format("%.2f", d);
a1b82138
VZ
909}
910
911wxTextCtrl& wxTextCtrlBase::operator<<(int i)
912{
9e50ed28 913 return *this << wxString::Format("%d", i);
a1b82138
VZ
914}
915
9e50ed28 916wxTextCtrl& wxTextCtrlBase::operator<<(long l)
a1b82138 917{
9e50ed28 918 return *this << wxString::Format("%ld", l);
a1b82138
VZ
919}
920
921// ----------------------------------------------------------------------------
922// streambuf methods implementation
923// ----------------------------------------------------------------------------
924
15cdc341 925#if wxHAS_TEXT_WINDOW_STREAM
a1b82138 926
d73e6791 927int wxTextCtrlBase::overflow(int c)
a1b82138 928{
d73e6791 929 AppendText((wxChar)c);
a1b82138 930
d73e6791 931 // return something different from EOF
a1b82138
VZ
932 return 0;
933}
934
15cdc341 935#endif // wxHAS_TEXT_WINDOW_STREAM
a1b82138 936
94af7d45
VZ
937// ----------------------------------------------------------------------------
938// emulating key presses
939// ----------------------------------------------------------------------------
940
941bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent& event)
942{
0ec1179b
VZ
943 // we have a native implementation for Win32 and so don't need this one
944#ifndef __WIN32__
02a48e3b 945 wxChar ch = 0;
94af7d45
VZ
946 int keycode = event.GetKeyCode();
947 switch ( keycode )
948 {
949 case WXK_NUMPAD0:
950 case WXK_NUMPAD1:
951 case WXK_NUMPAD2:
952 case WXK_NUMPAD3:
953 case WXK_NUMPAD4:
954 case WXK_NUMPAD5:
955 case WXK_NUMPAD6:
956 case WXK_NUMPAD7:
957 case WXK_NUMPAD8:
958 case WXK_NUMPAD9:
9a83f860 959 ch = (wxChar)(wxT('0') + keycode - WXK_NUMPAD0);
94af7d45
VZ
960 break;
961
962 case WXK_MULTIPLY:
963 case WXK_NUMPAD_MULTIPLY:
9a83f860 964 ch = wxT('*');
94af7d45
VZ
965 break;
966
967 case WXK_ADD:
968 case WXK_NUMPAD_ADD:
9a83f860 969 ch = wxT('+');
94af7d45
VZ
970 break;
971
972 case WXK_SUBTRACT:
973 case WXK_NUMPAD_SUBTRACT:
9a83f860 974 ch = wxT('-');
94af7d45
VZ
975 break;
976
977 case WXK_DECIMAL:
978 case WXK_NUMPAD_DECIMAL:
9a83f860 979 ch = wxT('.');
94af7d45
VZ
980 break;
981
982 case WXK_DIVIDE:
983 case WXK_NUMPAD_DIVIDE:
9a83f860 984 ch = wxT('/');
94af7d45
VZ
985 break;
986
cd916794
VZ
987 case WXK_DELETE:
988 case WXK_NUMPAD_DELETE:
989 // delete the character at cursor
990 {
7d8268a1
WS
991 const long pos = GetInsertionPoint();
992 if ( pos < GetLastPosition() )
cd916794
VZ
993 Remove(pos, pos + 1);
994 }
995 break;
996
997 case WXK_BACK:
998 // delete the character before the cursor
999 {
1000 const long pos = GetInsertionPoint();
1001 if ( pos > 0 )
1002 Remove(pos - 1, pos);
1003 }
1004 break;
1005
94af7d45 1006 default:
c4d25c01
VS
1007#if wxUSE_UNICODE
1008 if ( event.GetUnicodeKey() )
1009 {
1010 ch = event.GetUnicodeKey();
1011 }
1012 else
1013#endif
401eb3de 1014 if ( keycode < 256 && keycode >= 0 && wxIsprint(keycode) )
94af7d45
VZ
1015 {
1016 // FIXME this is not going to work for non letters...
1017 if ( !event.ShiftDown() )
1018 {
401eb3de 1019 keycode = wxTolower(keycode);
94af7d45
VZ
1020 }
1021
1022 ch = (wxChar)keycode;
1023 }
1024 else
1025 {
9a83f860 1026 ch = wxT('\0');
94af7d45
VZ
1027 }
1028 }
1029
1030 if ( ch )
1031 {
1032 WriteText(ch);
1033
cb719f2e 1034 return true;
94af7d45 1035 }
c8fe7fda
VZ
1036#else // __WIN32__
1037 wxUnusedVar(event);
1038#endif // !__WIN32__/__WIN32__
94af7d45 1039
0ec1179b 1040 return false;
18414479
VZ
1041}
1042
e39af974
JS
1043// do the window-specific processing after processing the update event
1044void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
1045{
a3a4105d
VZ
1046 // call inherited, but skip the wxControl's version, and call directly the
1047 // wxWindow's one instead, because the only reason why we are overriding this
1048 // function is that we want to use SetValue() instead of wxControl::SetLabel()
1049 wxWindowBase::DoUpdateWindowUI(event);
cb719f2e 1050
a3a4105d 1051 // update text
e39af974
JS
1052 if ( event.GetSetText() )
1053 {
1054 if ( event.GetText() != GetValue() )
1055 SetValue(event.GetText());
cb719f2e 1056 }
e39af974
JS
1057}
1058
efe66bbc
VZ
1059// ----------------------------------------------------------------------------
1060// hit testing
1061// ----------------------------------------------------------------------------
1062
692c9b86 1063wxTextCtrlHitTestResult
0ec1179b 1064wxTextAreaBase::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
692c9b86
VZ
1065{
1066 // implement in terms of the other overload as the native ports typically
1067 // can get the position and not (x, y) pair directly (although wxUniv
1068 // directly gets x and y -- and so overrides this method as well)
1069 long pos;
1070 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
1071
1072 if ( rc != wxTE_HT_UNKNOWN )
1073 {
1074 PositionToXY(pos, x, y);
1075 }
1076
1077 return rc;
1078}
1079
efe66bbc 1080wxTextCtrlHitTestResult
0ec1179b 1081wxTextAreaBase::HitTest(const wxPoint& WXUNUSED(pt), long * WXUNUSED(pos)) const
efe66bbc
VZ
1082{
1083 // not implemented
1084 return wxTE_HT_UNKNOWN;
1085}
e39af974 1086
6ce83213
VZ
1087wxPoint wxTextAreaBase::PositionToCoords(long pos) const
1088{
1089 wxCHECK_MSG( IsValidPosition(pos), wxDefaultPosition,
1090 wxS("Position argument out of range.") );
1091
1092 return DoPositionToCoords(pos);
1093}
1094
1095wxPoint wxTextAreaBase::DoPositionToCoords(long WXUNUSED(pos)) const
1096{
1097 return wxDefaultPosition;
1098}
1099
ad0bae85
VZ
1100#else // !wxUSE_TEXTCTRL
1101
1102// define this one even if !wxUSE_TEXTCTRL because it is also used by other
1103// controls (wxComboBox and wxSpinCtrl)
ad0bae85 1104
9b11752c 1105wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent );
ad0bae85
VZ
1106
1107#endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL