Allow handling EVT_UPDATE_UI for wxID_UNDO/REDO at wxDocument level.
[wxWidgets.git] / src / common / textcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textcmn.cpp
3 // Purpose: implementation of platform-independent functions of wxTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 13.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/event.h"
25 #endif // WX_PRECOMP
26
27 #if wxUSE_TEXTCTRL
28
29 #include "wx/textctrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif // WX_PRECOMP
35
36 #include "wx/ffile.h"
37
38 extern WXDLLEXPORT_DATA(const char) wxTextCtrlNameStr[] = "text";
39
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
52 // ----------------------------------------------------------------------------
53 // XTI
54 // ----------------------------------------------------------------------------
55
56 wxDEFINE_FLAGS( wxTextCtrlStyle )
57 wxBEGIN_FLAGS( wxTextCtrlStyle )
58 // new style border flags, we put them first to
59 // use them for streaming out
60 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
61 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
62 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
63 wxFLAGS_MEMBER(wxBORDER_RAISED)
64 wxFLAGS_MEMBER(wxBORDER_STATIC)
65 wxFLAGS_MEMBER(wxBORDER_NONE)
66
67 // old style border flags
68 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
69 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
70 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
71 wxFLAGS_MEMBER(wxRAISED_BORDER)
72 wxFLAGS_MEMBER(wxSTATIC_BORDER)
73 wxFLAGS_MEMBER(wxBORDER)
74
75 // standard window styles
76 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
77 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
78 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
79 wxFLAGS_MEMBER(wxWANTS_CHARS)
80 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
81 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
82 wxFLAGS_MEMBER(wxVSCROLL)
83 wxFLAGS_MEMBER(wxHSCROLL)
84
85 wxFLAGS_MEMBER(wxTE_PROCESS_ENTER)
86 wxFLAGS_MEMBER(wxTE_PROCESS_TAB)
87 wxFLAGS_MEMBER(wxTE_MULTILINE)
88 wxFLAGS_MEMBER(wxTE_PASSWORD)
89 wxFLAGS_MEMBER(wxTE_READONLY)
90 wxFLAGS_MEMBER(wxHSCROLL)
91 wxFLAGS_MEMBER(wxTE_RICH)
92 wxFLAGS_MEMBER(wxTE_RICH2)
93 wxFLAGS_MEMBER(wxTE_AUTO_URL)
94 wxFLAGS_MEMBER(wxTE_NOHIDESEL)
95 wxFLAGS_MEMBER(wxTE_LEFT)
96 wxFLAGS_MEMBER(wxTE_CENTRE)
97 wxFLAGS_MEMBER(wxTE_RIGHT)
98 wxFLAGS_MEMBER(wxTE_DONTWRAP)
99 wxFLAGS_MEMBER(wxTE_CHARWRAP)
100 wxFLAGS_MEMBER(wxTE_WORDWRAP)
101 wxEND_FLAGS( wxTextCtrlStyle )
102
103 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl, wxControl, "wx/textctrl.h")
104
105 wxBEGIN_PROPERTIES_TABLE(wxTextCtrl)
106 wxEVENT_PROPERTY( TextUpdated, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent )
107 wxEVENT_PROPERTY( TextEnter, wxEVT_COMMAND_TEXT_ENTER, wxCommandEvent )
108
109 wxPROPERTY( Font, wxFont, SetFont, GetFont , wxEMPTY_PARAMETER_VALUE, \
110 0 /*flags*/, wxT("Helpstring"), wxT("group") )
111 wxPROPERTY( Value, wxString, SetValue, GetValue, wxString(), \
112 0 /*flags*/, wxT("Helpstring"), wxT("group"))
113
114 wxPROPERTY_FLAGS( WindowStyle, wxTextCtrlStyle, long, SetWindowStyleFlag, \
115 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
116 wxT("Helpstring"), wxT("group")) // style
117 wxEND_PROPERTIES_TABLE()
118
119 wxEMPTY_HANDLERS_TABLE(wxTextCtrl)
120
121 wxCONSTRUCTOR_6( wxTextCtrl, wxWindow*, Parent, wxWindowID, Id, \
122 wxString, Value, wxPoint, Position, wxSize, Size, \
123 long, WindowStyle)
124
125
126 IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent, wxCommandEvent)
127
128 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent );
129 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_ENTER, wxCommandEvent );
130 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_URL, wxTextUrlEvent );
131 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_MAXLEN, wxCommandEvent );
132
133 IMPLEMENT_ABSTRACT_CLASS(wxTextCtrlBase, wxControl)
134
135 // ============================================================================
136 // wxTextAttr implementation
137 // ============================================================================
138
139 wxTextAttr::wxTextAttr(const wxColour& colText,
140 const wxColour& colBack,
141 const wxFont& font,
142 wxTextAttrAlignment alignment): m_textAlignment(alignment), m_colText(colText), m_colBack(colBack)
143 {
144 Init();
145
146 if (m_colText.IsOk()) m_flags |= wxTEXT_ATTR_TEXT_COLOUR;
147 if (m_colBack.IsOk()) m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR;
148 if (alignment != wxTEXT_ALIGNMENT_DEFAULT)
149 m_flags |= wxTEXT_ATTR_ALIGNMENT;
150
151 GetFontAttributes(font);
152 }
153
154 // Initialisation
155 void wxTextAttr::Init()
156 {
157 m_textAlignment = wxTEXT_ALIGNMENT_DEFAULT;
158 m_flags = 0;
159 m_leftIndent = 0;
160 m_leftSubIndent = 0;
161 m_rightIndent = 0;
162
163 m_fontSize = 12;
164 m_fontStyle = wxFONTSTYLE_NORMAL;
165 m_fontWeight = wxFONTWEIGHT_NORMAL;
166 m_fontUnderlined = false;
167 m_fontStrikethrough = false;
168 m_fontEncoding = wxFONTENCODING_DEFAULT;
169 m_fontFamily = wxFONTFAMILY_DEFAULT;
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
182 void 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;
197 m_fontStrikethrough = attr.m_fontStrikethrough;
198 m_fontFaceName = attr.m_fontFaceName;
199 m_fontEncoding = attr.m_fontEncoding;
200 m_fontFamily = attr.m_fontFamily;
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
221 void wxTextAttr::operator= (const wxTextAttr& attr)
222 {
223 Copy(attr);
224 }
225
226 // Equality test
227 bool 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() &&
264 GetFontFamily() == attr.GetFontFamily() &&
265
266 GetURL() == attr.GetURL();
267 }
268
269 // Partial equality test. Only returns false if an attribute doesn't match.
270 bool wxTextAttr::EqPartial(const wxTextAttr& attr) const
271 {
272 int flags = attr.GetFlags();
273
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
304 if ((flags & wxTEXT_ATTR_FONT_FAMILY) &&
305 GetFontFamily() != attr.GetFontFamily())
306 return false;
307
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.
387 wxFont wxTextAttr::GetFont() const
388 {
389 if ( !HasFont() )
390 return wxNullFont;
391
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
408 bool strikethrough = false;
409 if ( HasFontStrikethrough() )
410 strikethrough = GetFontStrikethrough();
411
412 wxString fontFaceName;
413 if (HasFontFaceName())
414 fontFaceName = GetFontFaceName();
415
416 wxFontEncoding encoding = wxFONTENCODING_DEFAULT;
417 if (HasFontEncoding())
418 encoding = GetFontEncoding();
419
420 wxFontFamily fontFamily = wxFONTFAMILY_DEFAULT;
421 if (HasFontFamily())
422 fontFamily = GetFontFamily();
423
424 wxFont font(fontSize, fontFamily, fontStyle, fontWeight, underlined, fontFaceName, encoding);
425 if ( strikethrough )
426 font.SetStrikethrough( true );
427 return font;
428 }
429
430 // Get attributes from font.
431 bool wxTextAttr::GetFontAttributes(const wxFont& font, int flags)
432 {
433 if (!font.IsOk())
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
448 if (flags & wxTEXT_ATTR_FONT_STRIKETHROUGH)
449 m_fontStrikethrough = font.GetStrikethrough();
450
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
457 if (flags & wxTEXT_ATTR_FONT_FAMILY)
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 }
468
469 m_flags |= flags;
470
471 return true;
472 }
473
474 // Resets bits in destination so new attributes aren't merged with mutually exclusive ones
475 static 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
486 bool 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
514 if (style.HasFontStrikethrough())
515 {
516 if (!(compareWith && compareWith->HasFontStrikethrough() && compareWith->GetFontStrikethrough() == style.GetFontStrikethrough()))
517 destStyle.SetFontStrikethrough(style.GetFontStrikethrough());
518 }
519
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
532 if (style.HasFontFamily())
533 {
534 if (!(compareWith && compareWith->HasFontFamily() && compareWith->GetFontFamily() == style.GetFontFamily()))
535 destStyle.SetFontFamily(style.GetFontFamily());
536 }
537
538 if (style.GetTextColour().IsOk() && style.HasTextColour())
539 {
540 if (!(compareWith && compareWith->HasTextColour() && compareWith->GetTextColour() == style.GetTextColour()))
541 destStyle.SetTextColour(style.GetTextColour());
542 }
543
544 if (style.GetBackgroundColour().IsOk() && style.HasBackgroundColour())
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
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
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;
679 }
680
681 /* static */
682 wxTextAttr wxTextAttr::Combine(const wxTextAttr& attr,
683 const wxTextAttr& attrDef,
684 const wxTextCtrlBase *text)
685 {
686 wxFont font;
687 if (attr.HasFont())
688 font = attr.GetFont();
689
690 if ( !font.IsOk() )
691 {
692 if (attrDef.HasFont())
693 font = attrDef.GetFont();
694
695 if ( text && !font.IsOk() )
696 font = text->GetFont();
697 }
698
699 wxColour colFg = attr.GetTextColour();
700 if ( !colFg.IsOk() )
701 {
702 colFg = attrDef.GetTextColour();
703
704 if ( text && !colFg.IsOk() )
705 colFg = text->GetForegroundColour();
706 }
707
708 wxColour colBg = attr.GetBackgroundColour();
709 if ( !colBg.IsOk() )
710 {
711 colBg = attrDef.GetBackgroundColour();
712
713 if ( text && !colBg.IsOk() )
714 colBg = text->GetBackgroundColour();
715 }
716
717 wxTextAttr newAttr(colFg, colBg, font);
718
719 if (attr.HasAlignment())
720 newAttr.SetAlignment(attr.GetAlignment());
721 else if (attrDef.HasAlignment())
722 newAttr.SetAlignment(attrDef.GetAlignment());
723
724 if (attr.HasTabs())
725 newAttr.SetTabs(attr.GetTabs());
726 else if (attrDef.HasTabs())
727 newAttr.SetTabs(attrDef.GetTabs());
728
729 if (attr.HasLeftIndent())
730 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
731 else if (attrDef.HasLeftIndent())
732 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
733
734 if (attr.HasRightIndent())
735 newAttr.SetRightIndent(attr.GetRightIndent());
736 else if (attrDef.HasRightIndent())
737 newAttr.SetRightIndent(attrDef.GetRightIndent());
738
739 return newAttr;
740 }
741
742 /// Compare tabs
743 bool wxTextAttr::TabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
744 {
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
758 bool 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.
769 bool 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
789 bool wxTextAttr::BitlistsEqPartial(int valueA, int valueB, int flags)
790 {
791 int relevantBitsA = valueA & flags;
792 int relevantBitsB = valueB & flags;
793 return relevantBitsA == relevantBitsB;
794 }
795
796 /// Split into paragraph and character styles
797 bool 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 }
809
810 // apply styling to text range
811 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
812 const wxTextAttr& WXUNUSED(style))
813 {
814 // to be implemented in derived classes
815 return false;
816 }
817
818 // get the styling at the given position
819 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style))
820 {
821 // to be implemented in derived classes
822 return false;
823 }
824
825 // change default text attributes
826 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr& style)
827 {
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);
835
836 return true;
837 }
838
839 // ----------------------------------------------------------------------------
840 // file IO functions
841 // ----------------------------------------------------------------------------
842
843 bool wxTextAreaBase::DoLoadFile(const wxString& filename, int WXUNUSED(fileType))
844 {
845 #if wxUSE_FFILE
846 wxFFile file(filename);
847 if ( file.IsOpened() )
848 {
849 wxString text;
850 if ( file.ReadAll(&text) )
851 {
852 SetValue(text);
853
854 return true;
855 }
856 }
857 #endif // wxUSE_FFILE
858
859 return false;
860 }
861
862 bool wxTextCtrlBase::DoLoadFile(const wxString& filename, int fileType)
863 {
864 if ( wxTextAreaBase::DoLoadFile(filename, fileType) )
865 {
866 DiscardEdits();
867 m_filename = filename;
868 return true;
869 }
870 wxLogError(_("File couldn't be loaded."));
871 return false;
872 }
873
874 bool wxTextAreaBase::DoSaveFile(const wxString& filename, int WXUNUSED(fileType))
875 {
876 #if wxUSE_FFILE
877 wxFFile file(filename, wxT("w"));
878 return file.IsOpened() && file.Write(GetValue(), *wxConvCurrent);
879 #else
880 return false;
881 #endif // wxUSE_FFILE
882 }
883
884 bool wxTextAreaBase::SaveFile(const wxString& filename, int fileType)
885 {
886 wxString filenameToUse = filename.empty() ? m_filename : filename;
887 if ( filenameToUse.empty() )
888 {
889 // what kind of message to give? is it an error or a program bug?
890 wxLogDebug(wxT("Can't save textctrl to file without filename."));
891
892 return false;
893 }
894
895 return DoSaveFile(filenameToUse, fileType);
896 }
897
898 bool wxTextCtrlBase::DoSaveFile(const wxString& filename, int fileType)
899 {
900 if ( wxTextAreaBase::DoSaveFile(filename, fileType) )
901 {
902 // if it worked, save for future calls
903 m_filename = filename;
904
905 // it's not modified any longer
906 DiscardEdits();
907
908 return true;
909 }
910 return false;
911 }
912
913 // ----------------------------------------------------------------------------
914 // stream-like insertion operator
915 // ----------------------------------------------------------------------------
916
917 wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
918 {
919 AppendText(s);
920 return *TEXTCTRL(this);
921 }
922
923 wxTextCtrl& wxTextCtrlBase::operator<<(double d)
924 {
925 return *this << wxString::Format("%.2f", d);
926 }
927
928 wxTextCtrl& wxTextCtrlBase::operator<<(int i)
929 {
930 return *this << wxString::Format("%d", i);
931 }
932
933 wxTextCtrl& wxTextCtrlBase::operator<<(long l)
934 {
935 return *this << wxString::Format("%ld", l);
936 }
937
938 // ----------------------------------------------------------------------------
939 // streambuf methods implementation
940 // ----------------------------------------------------------------------------
941
942 #if wxHAS_TEXT_WINDOW_STREAM
943
944 int wxTextCtrlBase::overflow(int c)
945 {
946 AppendText((wxChar)c);
947
948 // return something different from EOF
949 return 0;
950 }
951
952 #endif // wxHAS_TEXT_WINDOW_STREAM
953
954 // ----------------------------------------------------------------------------
955 // emulating key presses
956 // ----------------------------------------------------------------------------
957
958 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent& event)
959 {
960 bool handled = false;
961 // we have a native implementation for Win32 and so don't need this one
962 #ifndef __WIN32__
963 wxChar ch = 0;
964 int keycode = event.GetKeyCode();
965
966 long from, to;
967 GetSelection(&from,&to);
968 long insert = GetInsertionPoint();
969 long last = GetLastPosition();
970
971 // catch arrow left and right
972
973 switch ( keycode )
974 {
975 case WXK_LEFT:
976 if ( event.ShiftDown() )
977 SetSelection( (from > 0 ? from - 1 : 0) , to );
978 else
979 {
980 if ( from != to )
981 insert = from;
982 else if ( insert > 0 )
983 insert -= 1;
984 SetInsertionPoint( insert );
985 }
986 handled = true;
987 break;
988 case WXK_RIGHT:
989 if ( event.ShiftDown() )
990 SetSelection( from, (to < last ? to + 1 : last) );
991 else
992 {
993 if ( from != to )
994 insert = to;
995 else if ( insert < last )
996 insert += 1;
997 SetInsertionPoint( insert );
998 }
999 handled = true;
1000 break;
1001 case WXK_NUMPAD0:
1002 case WXK_NUMPAD1:
1003 case WXK_NUMPAD2:
1004 case WXK_NUMPAD3:
1005 case WXK_NUMPAD4:
1006 case WXK_NUMPAD5:
1007 case WXK_NUMPAD6:
1008 case WXK_NUMPAD7:
1009 case WXK_NUMPAD8:
1010 case WXK_NUMPAD9:
1011 ch = (wxChar)(wxT('0') + keycode - WXK_NUMPAD0);
1012 break;
1013
1014 case WXK_MULTIPLY:
1015 case WXK_NUMPAD_MULTIPLY:
1016 ch = wxT('*');
1017 break;
1018
1019 case WXK_ADD:
1020 case WXK_NUMPAD_ADD:
1021 ch = wxT('+');
1022 break;
1023
1024 case WXK_SUBTRACT:
1025 case WXK_NUMPAD_SUBTRACT:
1026 ch = wxT('-');
1027 break;
1028
1029 case WXK_DECIMAL:
1030 case WXK_NUMPAD_DECIMAL:
1031 ch = wxT('.');
1032 break;
1033
1034 case WXK_DIVIDE:
1035 case WXK_NUMPAD_DIVIDE:
1036 ch = wxT('/');
1037 break;
1038
1039 case WXK_DELETE:
1040 case WXK_NUMPAD_DELETE:
1041 // delete the character at cursor
1042 {
1043 const long pos = GetInsertionPoint();
1044 if ( pos < GetLastPosition() )
1045 Remove(pos, pos + 1);
1046 handled = true;
1047 }
1048 break;
1049
1050 case WXK_BACK:
1051 // delete the character before the cursor
1052 {
1053 const long pos = GetInsertionPoint();
1054 if ( pos > 0 )
1055 Remove(pos - 1, pos);
1056 handled = true;
1057 }
1058 break;
1059
1060 default:
1061 #if wxUSE_UNICODE
1062 if ( event.GetUnicodeKey() )
1063 {
1064 ch = event.GetUnicodeKey();
1065 }
1066 else
1067 #endif
1068 if ( keycode < 256 && keycode >= 0 && wxIsprint(keycode) )
1069 {
1070 // FIXME this is not going to work for non letters...
1071 if ( !event.ShiftDown() )
1072 {
1073 keycode = wxTolower(keycode);
1074 }
1075
1076 ch = (wxChar)keycode;
1077 }
1078 else
1079 {
1080 ch = wxT('\0');
1081 }
1082 }
1083
1084 if ( ch )
1085 {
1086 WriteText(ch);
1087
1088 handled = true;
1089 }
1090 #else // __WIN32__
1091 wxUnusedVar(event);
1092 #endif // !__WIN32__/__WIN32__
1093
1094 return handled;
1095 }
1096
1097 // do the window-specific processing after processing the update event
1098 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
1099 {
1100 // call inherited, but skip the wxControl's version, and call directly the
1101 // wxWindow's one instead, because the only reason why we are overriding this
1102 // function is that we want to use SetValue() instead of wxControl::SetLabel()
1103 wxWindowBase::DoUpdateWindowUI(event);
1104
1105 // update text
1106 if ( event.GetSetText() )
1107 {
1108 if ( event.GetText() != GetValue() )
1109 SetValue(event.GetText());
1110 }
1111 }
1112
1113 // ----------------------------------------------------------------------------
1114 // hit testing
1115 // ----------------------------------------------------------------------------
1116
1117 wxTextCtrlHitTestResult
1118 wxTextAreaBase::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
1119 {
1120 // implement in terms of the other overload as the native ports typically
1121 // can get the position and not (x, y) pair directly (although wxUniv
1122 // directly gets x and y -- and so overrides this method as well)
1123 long pos;
1124 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
1125
1126 if ( rc != wxTE_HT_UNKNOWN )
1127 {
1128 PositionToXY(pos, x, y);
1129 }
1130
1131 return rc;
1132 }
1133
1134 wxTextCtrlHitTestResult
1135 wxTextAreaBase::HitTest(const wxPoint& WXUNUSED(pt), long * WXUNUSED(pos)) const
1136 {
1137 // not implemented
1138 return wxTE_HT_UNKNOWN;
1139 }
1140
1141 wxPoint wxTextAreaBase::PositionToCoords(long pos) const
1142 {
1143 wxCHECK_MSG( IsValidPosition(pos), wxDefaultPosition,
1144 wxS("Position argument out of range.") );
1145
1146 return DoPositionToCoords(pos);
1147 }
1148
1149 wxPoint wxTextAreaBase::DoPositionToCoords(long WXUNUSED(pos)) const
1150 {
1151 return wxDefaultPosition;
1152 }
1153
1154 #else // !wxUSE_TEXTCTRL
1155
1156 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
1157 // controls (wxComboBox and wxSpinCtrl)
1158
1159 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent );
1160
1161 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL