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