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