]> git.saurik.com Git - wxWidgets.git/blob - src/common/textcmn.cpp
Add support for stricken-through fonts.
[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_fontEncoding = wxFONTENCODING_DEFAULT;
168 m_fontFamily = wxFONTFAMILY_DEFAULT;
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
181 void 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;
198 m_fontFamily = attr.m_fontFamily;
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
219 void wxTextAttr::operator= (const wxTextAttr& attr)
220 {
221 Copy(attr);
222 }
223
224 // Equality test
225 bool 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() &&
262 GetFontFamily() == attr.GetFontFamily() &&
263
264 GetURL() == attr.GetURL();
265 }
266
267 // Partial equality test. Only returns false if an attribute doesn't match.
268 bool wxTextAttr::EqPartial(const wxTextAttr& attr) const
269 {
270 int flags = attr.GetFlags();
271
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
302 if ((flags & wxTEXT_ATTR_FONT_FAMILY) &&
303 GetFontFamily() != attr.GetFontFamily())
304 return false;
305
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.
385 wxFont wxTextAttr::GetFont() const
386 {
387 if ( !HasFont() )
388 return wxNullFont;
389
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 bool strikethrough = false;
407 if ( HasFontStrikethrough() )
408 strikethrough = GetFontStrikethrough();
409
410 wxString fontFaceName;
411 if (HasFontFaceName())
412 fontFaceName = GetFontFaceName();
413
414 wxFontEncoding encoding = wxFONTENCODING_DEFAULT;
415 if (HasFontEncoding())
416 encoding = GetFontEncoding();
417
418 wxFontFamily fontFamily = wxFONTFAMILY_DEFAULT;
419 if (HasFontFamily())
420 fontFamily = GetFontFamily();
421
422 wxFont font(fontSize, fontFamily, fontStyle, fontWeight, underlined, fontFaceName, encoding);
423 if ( strikethrough )
424 font.SetStrikethrough( true );
425 return font;
426 }
427
428 // Get attributes from font.
429 bool wxTextAttr::GetFontAttributes(const wxFont& font, int flags)
430 {
431 if (!font.IsOk())
432 return false;
433
434 if (flags & wxTEXT_ATTR_FONT_SIZE)
435 m_fontSize = font.GetPointSize();
436
437 if (flags & wxTEXT_ATTR_FONT_ITALIC)
438 m_fontStyle = font.GetStyle();
439
440 if (flags & wxTEXT_ATTR_FONT_WEIGHT)
441 m_fontWeight = font.GetWeight();
442
443 if (flags & wxTEXT_ATTR_FONT_UNDERLINE)
444 m_fontUnderlined = font.GetUnderlined();
445
446 if (flags & wxTEXT_ATTR_FONT_STRIKETHROUGH)
447 m_fontStrikethrough = font.GetStrikethrough();
448
449 if (flags & wxTEXT_ATTR_FONT_FACE)
450 m_fontFaceName = font.GetFaceName();
451
452 if (flags & wxTEXT_ATTR_FONT_ENCODING)
453 m_fontEncoding = font.GetEncoding();
454
455 if (flags & wxTEXT_ATTR_FONT_FAMILY)
456 {
457 // wxFont might not know its family, avoid setting m_fontFamily to an
458 // invalid value and rather pretend that we don't have any font family
459 // information at all in this case
460 const wxFontFamily fontFamily = font.GetFamily();
461 if ( fontFamily == wxFONTFAMILY_UNKNOWN )
462 flags &= ~wxTEXT_ATTR_FONT_FAMILY;
463 else
464 m_fontFamily = fontFamily;
465 }
466
467 m_flags |= flags;
468
469 return true;
470 }
471
472 // Resets bits in destination so new attributes aren't merged with mutually exclusive ones
473 static bool wxResetIncompatibleBits(const int mask, const int srcFlags, int& destFlags, int& destBits)
474 {
475 if ((srcFlags & mask) && (destFlags & mask))
476 {
477 destBits &= ~mask;
478 destFlags &= ~mask;
479 }
480
481 return true;
482 }
483
484 bool wxTextAttr::Apply(const wxTextAttr& style, const wxTextAttr* compareWith)
485 {
486 wxTextAttr& destStyle = (*this);
487
488 if (style.HasFontWeight())
489 {
490 if (!(compareWith && compareWith->HasFontWeight() && compareWith->GetFontWeight() == style.GetFontWeight()))
491 destStyle.SetFontWeight(style.GetFontWeight());
492 }
493
494 if (style.HasFontSize())
495 {
496 if (!(compareWith && compareWith->HasFontSize() && compareWith->GetFontSize() == style.GetFontSize()))
497 destStyle.SetFontSize(style.GetFontSize());
498 }
499
500 if (style.HasFontItalic())
501 {
502 if (!(compareWith && compareWith->HasFontItalic() && compareWith->GetFontStyle() == style.GetFontStyle()))
503 destStyle.SetFontStyle(style.GetFontStyle());
504 }
505
506 if (style.HasFontUnderlined())
507 {
508 if (!(compareWith && compareWith->HasFontUnderlined() && compareWith->GetFontUnderlined() == style.GetFontUnderlined()))
509 destStyle.SetFontUnderlined(style.GetFontUnderlined());
510 }
511
512 if (style.HasFontStrikethrough())
513 {
514 if (!(compareWith && compareWith->HasFontStrikethrough() && compareWith->GetFontStrikethrough() == style.GetFontStrikethrough()))
515 destStyle.SetFontStrikethrough(style.GetFontStrikethrough());
516 }
517
518 if (style.HasFontFaceName())
519 {
520 if (!(compareWith && compareWith->HasFontFaceName() && compareWith->GetFontFaceName() == style.GetFontFaceName()))
521 destStyle.SetFontFaceName(style.GetFontFaceName());
522 }
523
524 if (style.HasFontEncoding())
525 {
526 if (!(compareWith && compareWith->HasFontEncoding() && compareWith->GetFontEncoding() == style.GetFontEncoding()))
527 destStyle.SetFontEncoding(style.GetFontEncoding());
528 }
529
530 if (style.HasFontFamily())
531 {
532 if (!(compareWith && compareWith->HasFontFamily() && compareWith->GetFontFamily() == style.GetFontFamily()))
533 destStyle.SetFontFamily(style.GetFontFamily());
534 }
535
536 if (style.GetTextColour().IsOk() && style.HasTextColour())
537 {
538 if (!(compareWith && compareWith->HasTextColour() && compareWith->GetTextColour() == style.GetTextColour()))
539 destStyle.SetTextColour(style.GetTextColour());
540 }
541
542 if (style.GetBackgroundColour().IsOk() && style.HasBackgroundColour())
543 {
544 if (!(compareWith && compareWith->HasBackgroundColour() && compareWith->GetBackgroundColour() == style.GetBackgroundColour()))
545 destStyle.SetBackgroundColour(style.GetBackgroundColour());
546 }
547
548 if (style.HasAlignment())
549 {
550 if (!(compareWith && compareWith->HasAlignment() && compareWith->GetAlignment() == style.GetAlignment()))
551 destStyle.SetAlignment(style.GetAlignment());
552 }
553
554 if (style.HasTabs())
555 {
556 if (!(compareWith && compareWith->HasTabs() && TabsEq(compareWith->GetTabs(), style.GetTabs())))
557 destStyle.SetTabs(style.GetTabs());
558 }
559
560 if (style.HasLeftIndent())
561 {
562 if (!(compareWith && compareWith->HasLeftIndent() && compareWith->GetLeftIndent() == style.GetLeftIndent()
563 && compareWith->GetLeftSubIndent() == style.GetLeftSubIndent()))
564 destStyle.SetLeftIndent(style.GetLeftIndent(), style.GetLeftSubIndent());
565 }
566
567 if (style.HasRightIndent())
568 {
569 if (!(compareWith && compareWith->HasRightIndent() && compareWith->GetRightIndent() == style.GetRightIndent()))
570 destStyle.SetRightIndent(style.GetRightIndent());
571 }
572
573 if (style.HasParagraphSpacingAfter())
574 {
575 if (!(compareWith && compareWith->HasParagraphSpacingAfter() && compareWith->GetParagraphSpacingAfter() == style.GetParagraphSpacingAfter()))
576 destStyle.SetParagraphSpacingAfter(style.GetParagraphSpacingAfter());
577 }
578
579 if (style.HasParagraphSpacingBefore())
580 {
581 if (!(compareWith && compareWith->HasParagraphSpacingBefore() && compareWith->GetParagraphSpacingBefore() == style.GetParagraphSpacingBefore()))
582 destStyle.SetParagraphSpacingBefore(style.GetParagraphSpacingBefore());
583 }
584
585 if (style.HasLineSpacing())
586 {
587 if (!(compareWith && compareWith->HasLineSpacing() && compareWith->GetLineSpacing() == style.GetLineSpacing()))
588 destStyle.SetLineSpacing(style.GetLineSpacing());
589 }
590
591 if (style.HasCharacterStyleName())
592 {
593 if (!(compareWith && compareWith->HasCharacterStyleName() && compareWith->GetCharacterStyleName() == style.GetCharacterStyleName()))
594 destStyle.SetCharacterStyleName(style.GetCharacterStyleName());
595 }
596
597 if (style.HasParagraphStyleName())
598 {
599 if (!(compareWith && compareWith->HasParagraphStyleName() && compareWith->GetParagraphStyleName() == style.GetParagraphStyleName()))
600 destStyle.SetParagraphStyleName(style.GetParagraphStyleName());
601 }
602
603 if (style.HasListStyleName())
604 {
605 if (!(compareWith && compareWith->HasListStyleName() && compareWith->GetListStyleName() == style.GetListStyleName()))
606 destStyle.SetListStyleName(style.GetListStyleName());
607 }
608
609 if (style.HasBulletStyle())
610 {
611 if (!(compareWith && compareWith->HasBulletStyle() && compareWith->GetBulletStyle() == style.GetBulletStyle()))
612 destStyle.SetBulletStyle(style.GetBulletStyle());
613 }
614
615 if (style.HasBulletText())
616 {
617 if (!(compareWith && compareWith->HasBulletText() && compareWith->GetBulletText() == style.GetBulletText()))
618 {
619 destStyle.SetBulletText(style.GetBulletText());
620 destStyle.SetBulletFont(style.GetBulletFont());
621 }
622 }
623
624 if (style.HasBulletNumber())
625 {
626 if (!(compareWith && compareWith->HasBulletNumber() && compareWith->GetBulletNumber() == style.GetBulletNumber()))
627 destStyle.SetBulletNumber(style.GetBulletNumber());
628 }
629
630 if (style.HasBulletName())
631 {
632 if (!(compareWith && compareWith->HasBulletName() && compareWith->GetBulletName() == style.GetBulletName()))
633 destStyle.SetBulletName(style.GetBulletName());
634 }
635
636 if (style.HasURL())
637 {
638 if (!(compareWith && compareWith->HasURL() && compareWith->GetURL() == style.GetURL()))
639 destStyle.SetURL(style.GetURL());
640 }
641
642 if (style.HasPageBreak())
643 {
644 if (!(compareWith && compareWith->HasPageBreak()))
645 destStyle.SetPageBreak();
646 }
647
648 if (style.HasTextEffects())
649 {
650 if (!(compareWith && compareWith->HasTextEffects() && compareWith->GetTextEffects() == style.GetTextEffects()))
651 {
652 int destBits = destStyle.GetTextEffects();
653 int destFlags = destStyle.GetTextEffectFlags();
654
655 int srcBits = style.GetTextEffects();
656 int srcFlags = style.GetTextEffectFlags();
657
658 // Reset incompatible bits in the destination
659 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_SUPERSCRIPT|wxTEXT_ATTR_EFFECT_SUBSCRIPT), srcFlags, destFlags, destBits);
660 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_CAPITALS|wxTEXT_ATTR_EFFECT_SMALL_CAPITALS), srcFlags, destFlags, destBits);
661 wxResetIncompatibleBits((wxTEXT_ATTR_EFFECT_STRIKETHROUGH|wxTEXT_ATTR_EFFECT_DOUBLE_STRIKETHROUGH), srcFlags, destFlags, destBits);
662
663 CombineBitlists(destBits, srcBits, destFlags, srcFlags);
664
665 destStyle.SetTextEffects(destBits);
666 destStyle.SetTextEffectFlags(destFlags);
667 }
668 }
669
670 if (style.HasOutlineLevel())
671 {
672 if (!(compareWith && compareWith->HasOutlineLevel() && compareWith->GetOutlineLevel() == style.GetOutlineLevel()))
673 destStyle.SetOutlineLevel(style.GetOutlineLevel());
674 }
675
676 return true;
677 }
678
679 /* static */
680 wxTextAttr wxTextAttr::Combine(const wxTextAttr& attr,
681 const wxTextAttr& attrDef,
682 const wxTextCtrlBase *text)
683 {
684 wxFont font;
685 if (attr.HasFont())
686 font = attr.GetFont();
687
688 if ( !font.IsOk() )
689 {
690 if (attrDef.HasFont())
691 font = attrDef.GetFont();
692
693 if ( text && !font.IsOk() )
694 font = text->GetFont();
695 }
696
697 wxColour colFg = attr.GetTextColour();
698 if ( !colFg.IsOk() )
699 {
700 colFg = attrDef.GetTextColour();
701
702 if ( text && !colFg.IsOk() )
703 colFg = text->GetForegroundColour();
704 }
705
706 wxColour colBg = attr.GetBackgroundColour();
707 if ( !colBg.IsOk() )
708 {
709 colBg = attrDef.GetBackgroundColour();
710
711 if ( text && !colBg.IsOk() )
712 colBg = text->GetBackgroundColour();
713 }
714
715 wxTextAttr newAttr(colFg, colBg, font);
716
717 if (attr.HasAlignment())
718 newAttr.SetAlignment(attr.GetAlignment());
719 else if (attrDef.HasAlignment())
720 newAttr.SetAlignment(attrDef.GetAlignment());
721
722 if (attr.HasTabs())
723 newAttr.SetTabs(attr.GetTabs());
724 else if (attrDef.HasTabs())
725 newAttr.SetTabs(attrDef.GetTabs());
726
727 if (attr.HasLeftIndent())
728 newAttr.SetLeftIndent(attr.GetLeftIndent(), attr.GetLeftSubIndent());
729 else if (attrDef.HasLeftIndent())
730 newAttr.SetLeftIndent(attrDef.GetLeftIndent(), attr.GetLeftSubIndent());
731
732 if (attr.HasRightIndent())
733 newAttr.SetRightIndent(attr.GetRightIndent());
734 else if (attrDef.HasRightIndent())
735 newAttr.SetRightIndent(attrDef.GetRightIndent());
736
737 return newAttr;
738 }
739
740 /// Compare tabs
741 bool wxTextAttr::TabsEq(const wxArrayInt& tabs1, const wxArrayInt& tabs2)
742 {
743 if (tabs1.GetCount() != tabs2.GetCount())
744 return false;
745
746 size_t i;
747 for (i = 0; i < tabs1.GetCount(); i++)
748 {
749 if (tabs1[i] != tabs2[i])
750 return false;
751 }
752 return true;
753 }
754
755 // Remove attributes
756 bool wxTextAttr::RemoveStyle(wxTextAttr& destStyle, const wxTextAttr& style)
757 {
758 int flags = style.GetFlags();
759 int destFlags = destStyle.GetFlags();
760
761 destStyle.SetFlags(destFlags & ~flags);
762
763 return true;
764 }
765
766 /// Combine two bitlists, specifying the bits of interest with separate flags.
767 bool wxTextAttr::CombineBitlists(int& valueA, int valueB, int& flagsA, int flagsB)
768 {
769 // We want to apply B's bits to A, taking into account each's flags which indicate which bits
770 // are to be taken into account. A zero in B's bits should reset that bit in A but only if B's flags
771 // indicate it.
772
773 // First, reset the 0 bits from B. We make a mask so we're only dealing with B's zero
774 // bits at this point, ignoring any 1 bits in B or 0 bits in B that are not relevant.
775 int valueA2 = ~(~valueB & flagsB) & valueA;
776
777 // Now combine the 1 bits.
778 int valueA3 = (valueB & flagsB) | valueA2;
779
780 valueA = valueA3;
781 flagsA = (flagsA | flagsB);
782
783 return true;
784 }
785
786 /// Compare two bitlists
787 bool wxTextAttr::BitlistsEqPartial(int valueA, int valueB, int flags)
788 {
789 int relevantBitsA = valueA & flags;
790 int relevantBitsB = valueB & flags;
791 return relevantBitsA == relevantBitsB;
792 }
793
794 /// Split into paragraph and character styles
795 bool wxTextAttr::SplitParaCharStyles(const wxTextAttr& style, wxTextAttr& parStyle, wxTextAttr& charStyle)
796 {
797 wxTextAttr defaultCharStyle1(style);
798 wxTextAttr defaultParaStyle1(style);
799 defaultCharStyle1.SetFlags(defaultCharStyle1.GetFlags()&wxTEXT_ATTR_CHARACTER);
800 defaultParaStyle1.SetFlags(defaultParaStyle1.GetFlags()&wxTEXT_ATTR_PARAGRAPH);
801
802 charStyle.Apply(defaultCharStyle1);
803 parStyle.Apply(defaultParaStyle1);
804
805 return true;
806 }
807
808 // apply styling to text range
809 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
810 const wxTextAttr& WXUNUSED(style))
811 {
812 // to be implemented in derived classes
813 return false;
814 }
815
816 // get the styling at the given position
817 bool wxTextCtrlBase::GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style))
818 {
819 // to be implemented in derived classes
820 return false;
821 }
822
823 // change default text attributes
824 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr& style)
825 {
826 // keep the old attributes if the new style doesn't specify them unless the
827 // new style is empty - then reset m_defaultStyle (as there is no other way
828 // to do it)
829 if ( style.IsDefault() )
830 m_defaultStyle = style;
831 else
832 m_defaultStyle = wxTextAttr::Combine(style, m_defaultStyle, this);
833
834 return true;
835 }
836
837 // ----------------------------------------------------------------------------
838 // file IO functions
839 // ----------------------------------------------------------------------------
840
841 bool wxTextAreaBase::DoLoadFile(const wxString& filename, int WXUNUSED(fileType))
842 {
843 #if wxUSE_FFILE
844 wxFFile file(filename);
845 if ( file.IsOpened() )
846 {
847 wxString text;
848 if ( file.ReadAll(&text) )
849 {
850 SetValue(text);
851
852 return true;
853 }
854 }
855 #endif // wxUSE_FFILE
856
857 return false;
858 }
859
860 bool wxTextCtrlBase::DoLoadFile(const wxString& filename, int fileType)
861 {
862 if ( wxTextAreaBase::DoLoadFile(filename, fileType) )
863 {
864 DiscardEdits();
865 m_filename = filename;
866 return true;
867 }
868 wxLogError(_("File couldn't be loaded."));
869 return false;
870 }
871
872 bool wxTextAreaBase::DoSaveFile(const wxString& filename, int WXUNUSED(fileType))
873 {
874 #if wxUSE_FFILE
875 wxFFile file(filename, wxT("w"));
876 return file.IsOpened() && file.Write(GetValue(), *wxConvCurrent);
877 #else
878 return false;
879 #endif // wxUSE_FFILE
880 }
881
882 bool wxTextAreaBase::SaveFile(const wxString& filename, int fileType)
883 {
884 wxString filenameToUse = filename.empty() ? m_filename : filename;
885 if ( filenameToUse.empty() )
886 {
887 // what kind of message to give? is it an error or a program bug?
888 wxLogDebug(wxT("Can't save textctrl to file without filename."));
889
890 return false;
891 }
892
893 return DoSaveFile(filenameToUse, fileType);
894 }
895
896 bool wxTextCtrlBase::DoSaveFile(const wxString& filename, int fileType)
897 {
898 if ( wxTextAreaBase::DoSaveFile(filename, fileType) )
899 {
900 // if it worked, save for future calls
901 m_filename = filename;
902
903 // it's not modified any longer
904 DiscardEdits();
905
906 return true;
907 }
908 return false;
909 }
910
911 // ----------------------------------------------------------------------------
912 // stream-like insertion operator
913 // ----------------------------------------------------------------------------
914
915 wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
916 {
917 AppendText(s);
918 return *TEXTCTRL(this);
919 }
920
921 wxTextCtrl& wxTextCtrlBase::operator<<(double d)
922 {
923 return *this << wxString::Format("%.2f", d);
924 }
925
926 wxTextCtrl& wxTextCtrlBase::operator<<(int i)
927 {
928 return *this << wxString::Format("%d", i);
929 }
930
931 wxTextCtrl& wxTextCtrlBase::operator<<(long l)
932 {
933 return *this << wxString::Format("%ld", l);
934 }
935
936 // ----------------------------------------------------------------------------
937 // streambuf methods implementation
938 // ----------------------------------------------------------------------------
939
940 #if wxHAS_TEXT_WINDOW_STREAM
941
942 int wxTextCtrlBase::overflow(int c)
943 {
944 AppendText((wxChar)c);
945
946 // return something different from EOF
947 return 0;
948 }
949
950 #endif // wxHAS_TEXT_WINDOW_STREAM
951
952 // ----------------------------------------------------------------------------
953 // emulating key presses
954 // ----------------------------------------------------------------------------
955
956 bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent& event)
957 {
958 bool handled = false;
959 // we have a native implementation for Win32 and so don't need this one
960 #ifndef __WIN32__
961 wxChar ch = 0;
962 int keycode = event.GetKeyCode();
963
964 long from, to;
965 GetSelection(&from,&to);
966 long insert = GetInsertionPoint();
967 long last = GetLastPosition();
968
969 // catch arrow left and right
970
971 switch ( keycode )
972 {
973 case WXK_LEFT:
974 if ( event.ShiftDown() )
975 SetSelection( (from > 0 ? from - 1 : 0) , to );
976 else
977 {
978 if ( from != to )
979 insert = from;
980 else if ( insert > 0 )
981 insert -= 1;
982 SetInsertionPoint( insert );
983 }
984 handled = true;
985 break;
986 case WXK_RIGHT:
987 if ( event.ShiftDown() )
988 SetSelection( from, (to < last ? to + 1 : last) );
989 else
990 {
991 if ( from != to )
992 insert = to;
993 else if ( insert < last )
994 insert += 1;
995 SetInsertionPoint( insert );
996 }
997 handled = true;
998 break;
999 case WXK_NUMPAD0:
1000 case WXK_NUMPAD1:
1001 case WXK_NUMPAD2:
1002 case WXK_NUMPAD3:
1003 case WXK_NUMPAD4:
1004 case WXK_NUMPAD5:
1005 case WXK_NUMPAD6:
1006 case WXK_NUMPAD7:
1007 case WXK_NUMPAD8:
1008 case WXK_NUMPAD9:
1009 ch = (wxChar)(wxT('0') + keycode - WXK_NUMPAD0);
1010 break;
1011
1012 case WXK_MULTIPLY:
1013 case WXK_NUMPAD_MULTIPLY:
1014 ch = wxT('*');
1015 break;
1016
1017 case WXK_ADD:
1018 case WXK_NUMPAD_ADD:
1019 ch = wxT('+');
1020 break;
1021
1022 case WXK_SUBTRACT:
1023 case WXK_NUMPAD_SUBTRACT:
1024 ch = wxT('-');
1025 break;
1026
1027 case WXK_DECIMAL:
1028 case WXK_NUMPAD_DECIMAL:
1029 ch = wxT('.');
1030 break;
1031
1032 case WXK_DIVIDE:
1033 case WXK_NUMPAD_DIVIDE:
1034 ch = wxT('/');
1035 break;
1036
1037 case WXK_DELETE:
1038 case WXK_NUMPAD_DELETE:
1039 // delete the character at cursor
1040 {
1041 const long pos = GetInsertionPoint();
1042 if ( pos < GetLastPosition() )
1043 Remove(pos, pos + 1);
1044 handled = true;
1045 }
1046 break;
1047
1048 case WXK_BACK:
1049 // delete the character before the cursor
1050 {
1051 const long pos = GetInsertionPoint();
1052 if ( pos > 0 )
1053 Remove(pos - 1, pos);
1054 handled = true;
1055 }
1056 break;
1057
1058 default:
1059 #if wxUSE_UNICODE
1060 if ( event.GetUnicodeKey() )
1061 {
1062 ch = event.GetUnicodeKey();
1063 }
1064 else
1065 #endif
1066 if ( keycode < 256 && keycode >= 0 && wxIsprint(keycode) )
1067 {
1068 // FIXME this is not going to work for non letters...
1069 if ( !event.ShiftDown() )
1070 {
1071 keycode = wxTolower(keycode);
1072 }
1073
1074 ch = (wxChar)keycode;
1075 }
1076 else
1077 {
1078 ch = wxT('\0');
1079 }
1080 }
1081
1082 if ( ch )
1083 {
1084 WriteText(ch);
1085
1086 handled = true;
1087 }
1088 #else // __WIN32__
1089 wxUnusedVar(event);
1090 #endif // !__WIN32__/__WIN32__
1091
1092 return handled;
1093 }
1094
1095 // do the window-specific processing after processing the update event
1096 void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
1097 {
1098 // call inherited, but skip the wxControl's version, and call directly the
1099 // wxWindow's one instead, because the only reason why we are overriding this
1100 // function is that we want to use SetValue() instead of wxControl::SetLabel()
1101 wxWindowBase::DoUpdateWindowUI(event);
1102
1103 // update text
1104 if ( event.GetSetText() )
1105 {
1106 if ( event.GetText() != GetValue() )
1107 SetValue(event.GetText());
1108 }
1109 }
1110
1111 // ----------------------------------------------------------------------------
1112 // hit testing
1113 // ----------------------------------------------------------------------------
1114
1115 wxTextCtrlHitTestResult
1116 wxTextAreaBase::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const
1117 {
1118 // implement in terms of the other overload as the native ports typically
1119 // can get the position and not (x, y) pair directly (although wxUniv
1120 // directly gets x and y -- and so overrides this method as well)
1121 long pos;
1122 wxTextCtrlHitTestResult rc = HitTest(pt, &pos);
1123
1124 if ( rc != wxTE_HT_UNKNOWN )
1125 {
1126 PositionToXY(pos, x, y);
1127 }
1128
1129 return rc;
1130 }
1131
1132 wxTextCtrlHitTestResult
1133 wxTextAreaBase::HitTest(const wxPoint& WXUNUSED(pt), long * WXUNUSED(pos)) const
1134 {
1135 // not implemented
1136 return wxTE_HT_UNKNOWN;
1137 }
1138
1139 wxPoint wxTextAreaBase::PositionToCoords(long pos) const
1140 {
1141 wxCHECK_MSG( IsValidPosition(pos), wxDefaultPosition,
1142 wxS("Position argument out of range.") );
1143
1144 return DoPositionToCoords(pos);
1145 }
1146
1147 wxPoint wxTextAreaBase::DoPositionToCoords(long WXUNUSED(pos)) const
1148 {
1149 return wxDefaultPosition;
1150 }
1151
1152 #else // !wxUSE_TEXTCTRL
1153
1154 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
1155 // controls (wxComboBox and wxSpinCtrl)
1156
1157 wxDEFINE_EVENT( wxEVT_COMMAND_TEXT_UPDATED, wxCommandEvent );
1158
1159 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL