]> git.saurik.com Git - wxWidgets.git/blob - src/richtext/richtextstyles.cpp
VC6 compilation fix: using access declaration can only be used with immediate base...
[wxWidgets.git] / src / richtext / richtextstyles.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextstyles.cpp
3 // Purpose: Style management for wxRichTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2005-09-30
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_RICHTEXT
20
21 #include "wx/richtext/richtextstyles.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/filename.h"
28 #include "wx/clipbrd.h"
29 #include "wx/wfstream.h"
30 #include "wx/settings.h"
31
32 #include "wx/richtext/richtextctrl.h"
33
34 IMPLEMENT_CLASS(wxRichTextStyleDefinition, wxObject)
35 IMPLEMENT_CLASS(wxRichTextCharacterStyleDefinition, wxRichTextStyleDefinition)
36 IMPLEMENT_CLASS(wxRichTextParagraphStyleDefinition, wxRichTextStyleDefinition)
37 IMPLEMENT_CLASS(wxRichTextListStyleDefinition, wxRichTextParagraphStyleDefinition)
38
39 /*!
40 * A definition
41 */
42
43 void wxRichTextStyleDefinition::Copy(const wxRichTextStyleDefinition& def)
44 {
45 m_name = def.m_name;
46 m_baseStyle = def.m_baseStyle;
47 m_style = def.m_style;
48 m_description = def.m_description;
49 }
50
51 bool wxRichTextStyleDefinition::Eq(const wxRichTextStyleDefinition& def) const
52 {
53 return (m_name == def.m_name && m_baseStyle == def.m_baseStyle && m_style == def.m_style);
54 }
55
56 /// Gets the style combined with the base style
57 wxTextAttr wxRichTextStyleDefinition::GetStyleMergedWithBase(const wxRichTextStyleSheet* sheet) const
58 {
59 if (!m_baseStyle.IsEmpty())
60 {
61 wxRichTextStyleDefinition* baseStyle = sheet->FindStyle(m_baseStyle);
62 if (baseStyle)
63 {
64 wxTextAttr baseAttr = baseStyle->GetStyleMergedWithBase(sheet);
65 baseAttr.Apply(m_style, NULL);
66 return baseAttr;
67 }
68 }
69 return m_style;
70 }
71
72 /*!
73 * Paragraph style definition
74 */
75
76 void wxRichTextParagraphStyleDefinition::Copy(const wxRichTextParagraphStyleDefinition& def)
77 {
78 wxRichTextStyleDefinition::Copy(def);
79
80 m_nextStyle = def.m_nextStyle;
81 }
82
83 bool wxRichTextParagraphStyleDefinition::operator ==(const wxRichTextParagraphStyleDefinition& def) const
84 {
85 return (Eq(def) && m_nextStyle == def.m_nextStyle);
86 }
87
88 /*!
89 * List style definition
90 */
91
92 void wxRichTextListStyleDefinition::Copy(const wxRichTextListStyleDefinition& def)
93 {
94 wxRichTextParagraphStyleDefinition::Copy(def);
95
96 int i;
97 for (i = 0; i < 10; i++)
98 m_levelStyles[i] = def.m_levelStyles[i];
99 }
100
101 bool wxRichTextListStyleDefinition::operator ==(const wxRichTextListStyleDefinition& def) const
102 {
103 if (!Eq(def))
104 return false;
105 int i;
106 for (i = 0; i < 10; i++)
107 if (!(m_levelStyles[i] == def.m_levelStyles[i]))
108 return false;
109
110 return true;
111 }
112
113 /// Sets/gets the attributes for the given level
114 void wxRichTextListStyleDefinition::SetLevelAttributes(int i, const wxTextAttr& attr)
115 {
116 wxASSERT( (i >= 0 && i < 10) );
117 if (i >= 0 && i < 10)
118 m_levelStyles[i] = attr;
119 }
120
121 const wxTextAttr* wxRichTextListStyleDefinition::GetLevelAttributes(int i) const
122 {
123 wxASSERT( (i >= 0 && i < 10) );
124 if (i >= 0 && i < 10)
125 return & m_levelStyles[i];
126 else
127 return NULL;
128 }
129
130 wxTextAttr* wxRichTextListStyleDefinition::GetLevelAttributes(int i)
131 {
132 wxASSERT( (i >= 0 && i < 10) );
133 if (i >= 0 && i < 10)
134 return & m_levelStyles[i];
135 else
136 return NULL;
137 }
138
139 /// Convenience function for setting the major attributes for a list level specification
140 void wxRichTextListStyleDefinition::SetAttributes(int i, int leftIndent, int leftSubIndent, int bulletStyle, const wxString& bulletSymbol)
141 {
142 wxASSERT( (i >= 0 && i < 10) );
143 if (i >= 0 && i < 10)
144 {
145 wxTextAttr attr;
146
147 attr.SetBulletStyle(bulletStyle);
148 attr.SetLeftIndent(leftIndent, leftSubIndent);
149
150 if (!bulletSymbol.IsEmpty())
151 {
152 if (bulletStyle & wxTEXT_ATTR_BULLET_STYLE_SYMBOL)
153 attr.SetBulletText(bulletSymbol);
154 else
155 attr.SetBulletName(bulletSymbol);
156 }
157
158 m_levelStyles[i] = attr;
159 }
160 }
161
162 /// Finds the level corresponding to the given indentation
163 int wxRichTextListStyleDefinition::FindLevelForIndent(int indent) const
164 {
165 int i;
166 for (i = 0; i < 10; i++)
167 {
168 if (indent < m_levelStyles[i].GetLeftIndent())
169 {
170 if (i > 0)
171 return i - 1;
172 else
173 return 0;
174 }
175 }
176 return 9;
177 }
178
179 /// Combine the list style with a paragraph style, using the given indent (from which
180 /// an appropriate level is found)
181 wxTextAttr wxRichTextListStyleDefinition::CombineWithParagraphStyle(int indent, const wxTextAttr& paraStyle, wxRichTextStyleSheet* styleSheet)
182 {
183 int listLevel = FindLevelForIndent(indent);
184
185 wxTextAttr attr(*GetLevelAttributes(listLevel));
186 int oldLeftIndent = attr.GetLeftIndent();
187 int oldLeftSubIndent = attr.GetLeftSubIndent();
188
189 // First apply the overall paragraph style, if any
190 if (styleSheet)
191 attr.Apply(GetStyleMergedWithBase(styleSheet));
192 else
193 attr.Apply(GetStyle());
194
195 // Then apply paragraph style, e.g. from paragraph style definition
196 attr.Apply(paraStyle);
197
198 // We override the indents according to the list definition
199 attr.SetLeftIndent(oldLeftIndent, oldLeftSubIndent);
200
201 return attr;
202 }
203
204 /// Combine the base and list style, using the given indent (from which
205 /// an appropriate level is found)
206 wxTextAttr wxRichTextListStyleDefinition::GetCombinedStyle(int indent, wxRichTextStyleSheet* styleSheet)
207 {
208 int listLevel = FindLevelForIndent(indent);
209 return GetCombinedStyleForLevel(listLevel, styleSheet);
210 }
211
212 /// Combine the base and list style, using the given indent (from which
213 /// an appropriate level is found)
214 wxTextAttr wxRichTextListStyleDefinition::GetCombinedStyleForLevel(int listLevel, wxRichTextStyleSheet* styleSheet)
215 {
216 wxTextAttr attr(*GetLevelAttributes(listLevel));
217 int oldLeftIndent = attr.GetLeftIndent();
218 int oldLeftSubIndent = attr.GetLeftSubIndent();
219
220 // Apply the overall paragraph style, if any
221 if (styleSheet)
222 attr.Apply(GetStyleMergedWithBase(styleSheet));
223 else
224 attr.Apply(GetStyle());
225
226 // We override the indents according to the list definition
227 attr.SetLeftIndent(oldLeftIndent, oldLeftSubIndent);
228
229 return attr;
230 }
231
232 /// Is this a numbered list?
233 bool wxRichTextListStyleDefinition::IsNumbered(int i) const
234 {
235 return (0 != (GetLevelAttributes(i)->GetFlags() &
236 (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER|wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER|
237 wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER|wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER)));
238 }
239
240 /*!
241 * The style manager
242 */
243
244 IMPLEMENT_CLASS(wxRichTextStyleSheet, wxObject)
245
246 wxRichTextStyleSheet::~wxRichTextStyleSheet()
247 {
248 DeleteStyles();
249
250 if (m_nextSheet)
251 m_nextSheet->m_previousSheet = m_previousSheet;
252
253 if (m_previousSheet)
254 m_previousSheet->m_nextSheet = m_nextSheet;
255
256 m_previousSheet = NULL;
257 m_nextSheet = NULL;
258 }
259
260 /// Initialisation
261 void wxRichTextStyleSheet::Init()
262 {
263 m_previousSheet = NULL;
264 m_nextSheet = NULL;
265 }
266
267 /// Add a definition to one of the style lists
268 bool wxRichTextStyleSheet::AddStyle(wxList& list, wxRichTextStyleDefinition* def)
269 {
270 if (!list.Find(def))
271 list.Append(def);
272 return true;
273 }
274
275 /// Remove a style
276 bool wxRichTextStyleSheet::RemoveStyle(wxList& list, wxRichTextStyleDefinition* def, bool deleteStyle)
277 {
278 wxList::compatibility_iterator node = list.Find(def);
279 if (node)
280 {
281 wxRichTextStyleDefinition* def = (wxRichTextStyleDefinition*) node->GetData();
282 list.Erase(node);
283 if (deleteStyle)
284 delete def;
285 return true;
286 }
287 else
288 return false;
289 }
290
291 /// Remove a style
292 bool wxRichTextStyleSheet::RemoveStyle(wxRichTextStyleDefinition* def, bool deleteStyle)
293 {
294 if (RemoveParagraphStyle(def, deleteStyle))
295 return true;
296 if (RemoveCharacterStyle(def, deleteStyle))
297 return true;
298 if (RemoveListStyle(def, deleteStyle))
299 return true;
300 return false;
301 }
302
303 /// Find a definition by name
304 wxRichTextStyleDefinition* wxRichTextStyleSheet::FindStyle(const wxList& list, const wxString& name, bool recurse) const
305 {
306 for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
307 {
308 wxRichTextStyleDefinition* def = (wxRichTextStyleDefinition*) node->GetData();
309 if (def->GetName().Lower() == name.Lower())
310 return def;
311 }
312
313 if (m_nextSheet && recurse)
314 return m_nextSheet->FindStyle(list, name, recurse);
315
316 return NULL;
317 }
318
319 /// Delete all styles
320 void wxRichTextStyleSheet::DeleteStyles()
321 {
322 WX_CLEAR_LIST(wxList, m_characterStyleDefinitions);
323 WX_CLEAR_LIST(wxList, m_paragraphStyleDefinitions);
324 WX_CLEAR_LIST(wxList, m_listStyleDefinitions);
325 }
326
327 /// Insert into list of style sheets
328 bool wxRichTextStyleSheet::InsertSheet(wxRichTextStyleSheet* before)
329 {
330 m_previousSheet = before->m_previousSheet;
331 m_nextSheet = before;
332
333 before->m_previousSheet = this;
334 return true;
335 }
336
337 /// Append to list of style sheets
338 bool wxRichTextStyleSheet::AppendSheet(wxRichTextStyleSheet* after)
339 {
340 wxRichTextStyleSheet* last = after;
341 while (last && last->m_nextSheet)
342 {
343 last = last->m_nextSheet;
344 }
345
346 if (last)
347 {
348 m_previousSheet = last;
349 last->m_nextSheet = this;
350
351 return true;
352 }
353 else
354 return false;
355 }
356
357 /// Unlink from the list of style sheets
358 void wxRichTextStyleSheet::Unlink()
359 {
360 if (m_previousSheet)
361 m_previousSheet->m_nextSheet = m_nextSheet;
362 if (m_nextSheet)
363 m_nextSheet->m_previousSheet = m_previousSheet;
364
365 m_previousSheet = NULL;
366 m_nextSheet = NULL;
367 }
368
369 /// Add a definition to the character style list
370 bool wxRichTextStyleSheet::AddCharacterStyle(wxRichTextCharacterStyleDefinition* def)
371 {
372 def->GetStyle().SetCharacterStyleName(def->GetName());
373 return AddStyle(m_characterStyleDefinitions, def);
374 }
375
376 /// Add a definition to the paragraph style list
377 bool wxRichTextStyleSheet::AddParagraphStyle(wxRichTextParagraphStyleDefinition* def)
378 {
379 def->GetStyle().SetParagraphStyleName(def->GetName());
380 return AddStyle(m_paragraphStyleDefinitions, def);
381 }
382
383 /// Add a definition to the list style list
384 bool wxRichTextStyleSheet::AddListStyle(wxRichTextListStyleDefinition* def)
385 {
386 def->GetStyle().SetListStyleName(def->GetName());
387 return AddStyle(m_listStyleDefinitions, def);
388 }
389
390 /// Add a definition to the appropriate style list
391 bool wxRichTextStyleSheet::AddStyle(wxRichTextStyleDefinition* def)
392 {
393 wxRichTextListStyleDefinition* listDef = wxDynamicCast(def, wxRichTextListStyleDefinition);
394 if (listDef)
395 return AddListStyle(listDef);
396
397 wxRichTextParagraphStyleDefinition* paraDef = wxDynamicCast(def, wxRichTextParagraphStyleDefinition);
398 if (paraDef)
399 return AddParagraphStyle(paraDef);
400
401 wxRichTextCharacterStyleDefinition* charDef = wxDynamicCast(def, wxRichTextCharacterStyleDefinition);
402 if (charDef)
403 return AddCharacterStyle(charDef);
404
405 return false;
406 }
407
408 /// Find any definition by name
409 wxRichTextStyleDefinition* wxRichTextStyleSheet::FindStyle(const wxString& name, bool recurse) const
410 {
411 wxRichTextListStyleDefinition* listDef = FindListStyle(name, recurse);
412 if (listDef)
413 return listDef;
414
415 wxRichTextParagraphStyleDefinition* paraDef = FindParagraphStyle(name, recurse);
416 if (paraDef)
417 return paraDef;
418
419 wxRichTextCharacterStyleDefinition* charDef = FindCharacterStyle(name, recurse);
420 if (charDef)
421 return charDef;
422
423 return NULL;
424 }
425
426 /// Copy
427 void wxRichTextStyleSheet::Copy(const wxRichTextStyleSheet& sheet)
428 {
429 DeleteStyles();
430
431 wxList::compatibility_iterator node;
432
433 for (node = sheet.m_characterStyleDefinitions.GetFirst(); node; node = node->GetNext())
434 {
435 wxRichTextCharacterStyleDefinition* def = (wxRichTextCharacterStyleDefinition*) node->GetData();
436 AddCharacterStyle(new wxRichTextCharacterStyleDefinition(*def));
437 }
438
439 for (node = sheet.m_paragraphStyleDefinitions.GetFirst(); node; node = node->GetNext())
440 {
441 wxRichTextParagraphStyleDefinition* def = (wxRichTextParagraphStyleDefinition*) node->GetData();
442 AddParagraphStyle(new wxRichTextParagraphStyleDefinition(*def));
443 }
444
445 for (node = sheet.m_listStyleDefinitions.GetFirst(); node; node = node->GetNext())
446 {
447 wxRichTextListStyleDefinition* def = (wxRichTextListStyleDefinition*) node->GetData();
448 AddListStyle(new wxRichTextListStyleDefinition(*def));
449 }
450
451 SetName(sheet.GetName());
452 SetDescription(sheet.GetDescription());
453 }
454
455 /// Equality
456 bool wxRichTextStyleSheet::operator==(const wxRichTextStyleSheet& WXUNUSED(sheet)) const
457 {
458 // TODO
459 return false;
460 }
461
462
463 #if wxUSE_HTML
464 /*!
465 * wxRichTextStyleListBox: a listbox to display styles.
466 */
467
468 IMPLEMENT_CLASS(wxRichTextStyleListBox, wxHtmlListBox)
469
470 BEGIN_EVENT_TABLE(wxRichTextStyleListBox, wxHtmlListBox)
471 EVT_LEFT_DOWN(wxRichTextStyleListBox::OnLeftDown)
472 EVT_LEFT_DCLICK(wxRichTextStyleListBox::OnLeftDoubleClick)
473 EVT_IDLE(wxRichTextStyleListBox::OnIdle)
474 END_EVENT_TABLE()
475
476 wxRichTextStyleListBox::wxRichTextStyleListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos,
477 const wxSize& size, long style)
478 {
479 Init();
480 Create(parent, id, pos, size, style);
481 }
482
483 bool wxRichTextStyleListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
484 const wxSize& size, long style)
485 {
486 return wxHtmlListBox::Create(parent, id, pos, size, style);
487 }
488
489 wxRichTextStyleListBox::~wxRichTextStyleListBox()
490 {
491 }
492
493 /// Returns the HTML for this item
494 wxString wxRichTextStyleListBox::OnGetItem(size_t n) const
495 {
496 if (!GetStyleSheet())
497 return wxEmptyString;
498
499 wxRichTextStyleDefinition* def = GetStyle(n);
500 if (def)
501 return CreateHTML(def);
502
503 return wxEmptyString;
504 }
505
506 // Get style for index
507 wxRichTextStyleDefinition* wxRichTextStyleListBox::GetStyle(size_t i) const
508 {
509 if (!GetStyleSheet())
510 return NULL;
511
512 if (i >= m_styleNames.GetCount() /* || i < 0 */ )
513 return NULL;
514
515 return GetStyleSheet()->FindStyle(m_styleNames[i]);
516 }
517
518 /// Updates the list
519 void wxRichTextStyleListBox::UpdateStyles()
520 {
521 if (GetStyleSheet())
522 {
523 SetSelection(wxNOT_FOUND);
524
525 m_styleNames.Clear();
526
527 size_t i;
528 if (GetStyleType() == wxRICHTEXT_STYLE_ALL || GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH)
529 {
530 for (i = 0; i < GetStyleSheet()->GetParagraphStyleCount(); i++)
531 m_styleNames.Add(GetStyleSheet()->GetParagraphStyle(i)->GetName());
532 }
533 if (GetStyleType() == wxRICHTEXT_STYLE_ALL || GetStyleType() == wxRICHTEXT_STYLE_CHARACTER)
534 {
535 for (i = 0; i < GetStyleSheet()->GetCharacterStyleCount(); i++)
536 m_styleNames.Add(GetStyleSheet()->GetCharacterStyle(i)->GetName());
537 }
538 if (GetStyleType() == wxRICHTEXT_STYLE_ALL || GetStyleType() == wxRICHTEXT_STYLE_LIST)
539 {
540 for (i = 0; i < GetStyleSheet()->GetListStyleCount(); i++)
541 m_styleNames.Add(GetStyleSheet()->GetListStyle(i)->GetName());
542 }
543
544 m_styleNames.Sort();
545 SetItemCount(m_styleNames.GetCount());
546
547 Refresh();
548
549 if (GetItemCount() > 0)
550 {
551 SetSelection(0);
552 SendSelectedEvent();
553 }
554 }
555 }
556
557 // Get index for style name
558 int wxRichTextStyleListBox::GetIndexForStyle(const wxString& name) const
559 {
560 return m_styleNames.Index(name);
561 }
562
563 /// Set selection for string
564 int wxRichTextStyleListBox::SetStyleSelection(const wxString& name)
565 {
566 int i = GetIndexForStyle(name);
567 if (i > -1)
568 SetSelection(i);
569 return i;
570 }
571
572 // Convert a colour to a 6-digit hex string
573 static wxString ColourToHexString(const wxColour& col)
574 {
575 wxString hex;
576
577 hex += wxDecToHex(col.Red());
578 hex += wxDecToHex(col.Green());
579 hex += wxDecToHex(col.Blue());
580
581 return hex;
582 }
583
584 /// Creates a suitable HTML fragment for a definition
585 wxString wxRichTextStyleListBox::CreateHTML(wxRichTextStyleDefinition* def) const
586 {
587 // TODO: indicate list format for list style types
588
589 wxString str;
590
591 bool isCentred = false;
592
593 wxTextAttr attr(def->GetStyleMergedWithBase(GetStyleSheet()));
594
595 if (attr.HasAlignment() && attr.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
596 isCentred = true;
597
598 if (isCentred)
599 str << wxT("<center>");
600
601
602 str << wxT("<table><tr>");
603
604 if (attr.GetLeftIndent() > 0)
605 {
606 wxClientDC dc((wxWindow*) this);
607
608 str << wxT("<td width=") << wxMin(50, (ConvertTenthsMMToPixels(dc, attr.GetLeftIndent())/2)) << wxT("></td>");
609 }
610
611 if (isCentred)
612 str << wxT("<td nowrap align=\"center\">");
613 else
614 str << wxT("<td nowrap>");
615
616 #ifdef __WXMSW__
617 int size = 3;
618 #else
619 int size = 4;
620 #endif
621
622 int stdFontSize = 12;
623 int thisFontSize = ((attr.GetFlags() & wxTEXT_ATTR_FONT_SIZE) != 0) ? attr.GetFontSize() : stdFontSize;
624
625 if (thisFontSize < stdFontSize)
626 size ++;
627 else if (thisFontSize > stdFontSize)
628 size --;
629
630 str += wxT("<font");
631
632 str << wxT(" size=") << size;
633
634 if (!attr.GetFontFaceName().IsEmpty())
635 str << wxT(" face=\"") << attr.GetFontFaceName() << wxT("\"");
636
637 if (attr.GetTextColour().Ok())
638 str << wxT(" color=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\"");
639
640 str << wxT(">");
641
642 bool hasBold = false;
643 bool hasItalic = false;
644 bool hasUnderline = false;
645
646 if (attr.GetFontWeight() == wxBOLD)
647 hasBold = true;
648 if (attr.GetFontStyle() == wxITALIC)
649 hasItalic = true;
650 if (attr.GetFontUnderlined())
651 hasUnderline = true;
652
653 if (hasBold)
654 str << wxT("<b>");
655 if (hasItalic)
656 str << wxT("<i>");
657 if (hasUnderline)
658 str << wxT("<u>");
659
660 str += def->GetName();
661
662 if (hasUnderline)
663 str << wxT("</u>");
664 if (hasItalic)
665 str << wxT("</i>");
666 if (hasBold)
667 str << wxT("</b>");
668
669 if (isCentred)
670 str << wxT("</centre>");
671
672 str << wxT("</font>");
673
674 str << wxT("</td></tr></table>");
675
676 if (isCentred)
677 str << wxT("</center>");
678
679 return str;
680 }
681
682 // Convert units in tends of a millimetre to device units
683 int wxRichTextStyleListBox::ConvertTenthsMMToPixels(wxDC& dc, int units) const
684 {
685 int ppi = dc.GetPPI().x;
686
687 // There are ppi pixels in 254.1 "1/10 mm"
688
689 double pixels = ((double) units * (double)ppi) / 254.1;
690
691 return (int) pixels;
692 }
693
694 void wxRichTextStyleListBox::OnLeftDown(wxMouseEvent& event)
695 {
696 wxVListBox::OnLeftDown(event);
697
698 int item = HitTest(event.GetPosition());
699 if (item != wxNOT_FOUND && GetApplyOnSelection())
700 ApplyStyle(item);
701 }
702
703 void wxRichTextStyleListBox::OnLeftDoubleClick(wxMouseEvent& event)
704 {
705 wxVListBox::OnLeftDown(event);
706
707 int item = HitTest(event.GetPosition());
708 if (item != wxNOT_FOUND && !GetApplyOnSelection())
709 ApplyStyle(item);
710 }
711
712 /// Helper for listbox and combo control
713 wxString wxRichTextStyleListBox::GetStyleToShowInIdleTime(wxRichTextCtrl* ctrl, wxRichTextStyleType styleType)
714 {
715 int adjustedCaretPos = ctrl->GetAdjustedCaretPosition(ctrl->GetCaretPosition());
716
717 wxString styleName;
718
719 wxTextAttr attr;
720 ctrl->GetStyle(adjustedCaretPos, attr);
721
722 // Take into account current default style just chosen by user
723 if (ctrl->IsDefaultStyleShowing())
724 {
725 wxRichTextApplyStyle(attr, ctrl->GetDefaultStyleEx());
726
727 if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_CHARACTER) &&
728 !attr.GetCharacterStyleName().IsEmpty())
729 styleName = attr.GetCharacterStyleName();
730 else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_PARAGRAPH) &&
731 !attr.GetParagraphStyleName().IsEmpty())
732 styleName = attr.GetParagraphStyleName();
733 else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_LIST) &&
734 !attr.GetListStyleName().IsEmpty())
735 styleName = attr.GetListStyleName();
736 }
737 else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_CHARACTER) &&
738 !attr.GetCharacterStyleName().IsEmpty())
739 {
740 styleName = attr.GetCharacterStyleName();
741 }
742 else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_PARAGRAPH) &&
743 !attr.GetParagraphStyleName().IsEmpty())
744 {
745 styleName = attr.GetParagraphStyleName();
746 }
747 else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_LIST) &&
748 !attr.GetListStyleName().IsEmpty())
749 {
750 styleName = attr.GetListStyleName();
751 }
752
753 return styleName;
754 }
755
756 /// Auto-select from style under caret in idle time
757 void wxRichTextStyleListBox::OnIdle(wxIdleEvent& event)
758 {
759 if (CanAutoSetSelection() && GetRichTextCtrl() && IsShownOnScreen() && wxWindow::FindFocus() != this)
760 {
761 wxString styleName = GetStyleToShowInIdleTime(GetRichTextCtrl(), GetStyleType());
762
763 int sel = GetSelection();
764 if (!styleName.IsEmpty())
765 {
766 // Don't do the selection if it's already set
767 if (sel == GetIndexForStyle(styleName))
768 return;
769
770 SetStyleSelection(styleName);
771 }
772 else if (sel != -1)
773 SetSelection(-1);
774 }
775 event.Skip();
776 }
777
778 /// Do selection
779 void wxRichTextStyleListBox::ApplyStyle(int item)
780 {
781 if ( item != wxNOT_FOUND )
782 {
783 wxRichTextStyleDefinition* def = GetStyle(item);
784 if (def && GetRichTextCtrl())
785 {
786 GetRichTextCtrl()->ApplyStyle(def);
787 GetRichTextCtrl()->SetFocus();
788 }
789 }
790 }
791
792 /*!
793 * wxRichTextStyleListCtrl class: manages a listbox and a choice control to
794 * switch shown style types
795 */
796
797 IMPLEMENT_CLASS(wxRichTextStyleListCtrl, wxControl)
798
799 BEGIN_EVENT_TABLE(wxRichTextStyleListCtrl, wxControl)
800 EVT_CHOICE(wxID_ANY, wxRichTextStyleListCtrl::OnChooseType)
801 EVT_SIZE(wxRichTextStyleListCtrl::OnSize)
802 END_EVENT_TABLE()
803
804 wxRichTextStyleListCtrl::wxRichTextStyleListCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos,
805 const wxSize& size, long style)
806 {
807 Init();
808 Create(parent, id, pos, size, style);
809 }
810
811 bool wxRichTextStyleListCtrl::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
812 const wxSize& size, long style)
813 {
814 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
815 style |= wxBORDER_THEME;
816
817 wxControl::Create(parent, id, pos, size, style);
818
819 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
820 if (size != wxDefaultSize)
821 SetInitialSize(size);
822
823 bool showSelector = ((style & wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR) == 0);
824
825 wxBorder listBoxStyle;
826 if (showSelector)
827 listBoxStyle = wxBORDER_THEME;
828 else
829 listBoxStyle = wxBORDER_NONE;
830
831 m_styleListBox = new wxRichTextStyleListBox(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, listBoxStyle);
832
833 wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL);
834
835 if (showSelector)
836 {
837 wxArrayString choices;
838 choices.Add(_("All styles"));
839 choices.Add(_("Paragraph styles"));
840 choices.Add(_("Character styles"));
841 choices.Add(_("List styles"));
842
843 m_styleChoice = new wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, choices);
844
845 boxSizer->Add(m_styleListBox, 1, wxALL|wxEXPAND, 5);
846 boxSizer->Add(m_styleChoice, 0, wxLEFT|wxRIGHT|wxBOTTOM|wxEXPAND, 5);
847 }
848 else
849 {
850 boxSizer->Add(m_styleListBox, 1, wxALL|wxEXPAND, 0);
851 }
852
853 SetSizer(boxSizer);
854 Layout();
855
856 m_dontUpdate = true;
857
858 if (m_styleChoice)
859 {
860 int i = StyleTypeToIndex(m_styleListBox->GetStyleType());
861 m_styleChoice->SetSelection(i);
862 }
863
864 m_dontUpdate = false;
865
866 return true;
867 }
868
869 wxRichTextStyleListCtrl::~wxRichTextStyleListCtrl()
870 {
871
872 }
873
874 /// React to style type choice
875 void wxRichTextStyleListCtrl::OnChooseType(wxCommandEvent& event)
876 {
877 if (event.GetEventObject() != m_styleChoice)
878 event.Skip();
879 else
880 {
881 if (m_dontUpdate)
882 return;
883
884 wxRichTextStyleListBox::wxRichTextStyleType styleType = StyleIndexToType(event.GetSelection());
885 m_styleListBox->SetStyleType(styleType);
886 }
887 }
888
889 /// Lay out the controls
890 void wxRichTextStyleListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
891 {
892 if (GetAutoLayout())
893 Layout();
894 }
895
896 /// Get the choice index for style type
897 int wxRichTextStyleListCtrl::StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType)
898 {
899 if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL)
900 {
901 return 0;
902 }
903 else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH)
904 {
905 return 1;
906 }
907 else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER)
908 {
909 return 2;
910 }
911 else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST)
912 {
913 return 3;
914 }
915 return 0;
916 }
917
918 /// Get the style type for choice index
919 wxRichTextStyleListBox::wxRichTextStyleType wxRichTextStyleListCtrl::StyleIndexToType(int i)
920 {
921 if (i == 1)
922 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH;
923 else if (i == 2)
924 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER;
925 else if (i == 3)
926 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST;
927
928 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL;
929 }
930
931 /// Associates the control with a style manager
932 void wxRichTextStyleListCtrl::SetStyleSheet(wxRichTextStyleSheet* styleSheet)
933 {
934 if (m_styleListBox)
935 m_styleListBox->SetStyleSheet(styleSheet);
936 }
937
938 wxRichTextStyleSheet* wxRichTextStyleListCtrl::GetStyleSheet() const
939 {
940 if (m_styleListBox)
941 return m_styleListBox->GetStyleSheet();
942 else
943 return NULL;
944 }
945
946 /// Associates the control with a wxRichTextCtrl
947 void wxRichTextStyleListCtrl::SetRichTextCtrl(wxRichTextCtrl* ctrl)
948 {
949 if (m_styleListBox)
950 m_styleListBox->SetRichTextCtrl(ctrl);
951 }
952
953 wxRichTextCtrl* wxRichTextStyleListCtrl::GetRichTextCtrl() const
954 {
955 if (m_styleListBox)
956 return m_styleListBox->GetRichTextCtrl();
957 else
958 return NULL;
959 }
960
961 /// Set/get the style type to display
962 void wxRichTextStyleListCtrl::SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType)
963 {
964 if ( !m_styleListBox )
965 return;
966
967 m_styleListBox->SetStyleType(styleType);
968
969 m_dontUpdate = true;
970
971 if (m_styleChoice)
972 {
973 int i = StyleTypeToIndex(m_styleListBox->GetStyleType());
974 m_styleChoice->SetSelection(i);
975 }
976
977 m_dontUpdate = false;
978 }
979
980 wxRichTextStyleListBox::wxRichTextStyleType wxRichTextStyleListCtrl::GetStyleType() const
981 {
982 if (m_styleListBox)
983 return m_styleListBox->GetStyleType();
984 else
985 return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL;
986 }
987
988 /// Updates the style list box
989 void wxRichTextStyleListCtrl::UpdateStyles()
990 {
991 if (m_styleListBox)
992 m_styleListBox->UpdateStyles();
993 }
994
995 #if wxUSE_COMBOCTRL
996
997 /*!
998 * Style drop-down for a wxComboCtrl
999 */
1000
1001
1002 BEGIN_EVENT_TABLE(wxRichTextStyleComboPopup, wxRichTextStyleListBox)
1003 EVT_MOTION(wxRichTextStyleComboPopup::OnMouseMove)
1004 EVT_LEFT_DOWN(wxRichTextStyleComboPopup::OnMouseClick)
1005 END_EVENT_TABLE()
1006
1007 bool wxRichTextStyleComboPopup::Create( wxWindow* parent )
1008 {
1009 int borderStyle = GetDefaultBorder();
1010 if (borderStyle == wxBORDER_SUNKEN)
1011 borderStyle = wxBORDER_SIMPLE;
1012
1013 return wxRichTextStyleListBox::Create(parent, wxID_ANY,
1014 wxPoint(0,0), wxDefaultSize,
1015 borderStyle);
1016 }
1017
1018 void wxRichTextStyleComboPopup::SetStringValue( const wxString& s )
1019 {
1020 m_value = SetStyleSelection(s);
1021 }
1022
1023 wxString wxRichTextStyleComboPopup::GetStringValue() const
1024 {
1025 int sel = m_value;
1026 if (sel > -1)
1027 {
1028 wxRichTextStyleDefinition* def = GetStyle(sel);
1029 if (def)
1030 return def->GetName();
1031 }
1032 return wxEmptyString;
1033 }
1034
1035 //
1036 // Popup event handlers
1037 //
1038
1039 // Mouse hot-tracking
1040 void wxRichTextStyleComboPopup::OnMouseMove(wxMouseEvent& event)
1041 {
1042 // Move selection to cursor if it is inside the popup
1043
1044 int itemHere = wxRichTextStyleListBox::HitTest(event.GetPosition());
1045 if ( itemHere >= 0 )
1046 {
1047 wxRichTextStyleListBox::SetSelection(itemHere);
1048 m_itemHere = itemHere;
1049 }
1050 event.Skip();
1051 }
1052
1053 // On mouse left, set the value and close the popup
1054 void wxRichTextStyleComboPopup::OnMouseClick(wxMouseEvent& WXUNUSED(event))
1055 {
1056 if (m_itemHere >= 0)
1057 m_value = m_itemHere;
1058
1059 // Ordering is important, so we don't dismiss this popup accidentally
1060 // by setting the focus elsewhere e.g. in ApplyStyle
1061 Dismiss();
1062
1063 if (m_itemHere >= 0)
1064 wxRichTextStyleListBox::ApplyStyle(m_itemHere);
1065 }
1066
1067 /*!
1068 * wxRichTextStyleComboCtrl
1069 * A combo for applying styles.
1070 */
1071
1072 IMPLEMENT_CLASS(wxRichTextStyleComboCtrl, wxComboCtrl)
1073
1074 BEGIN_EVENT_TABLE(wxRichTextStyleComboCtrl, wxComboCtrl)
1075 EVT_IDLE(wxRichTextStyleComboCtrl::OnIdle)
1076 END_EVENT_TABLE()
1077
1078 bool wxRichTextStyleComboCtrl::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
1079 const wxSize& size, long style)
1080 {
1081 if (!wxComboCtrl::Create(parent, id, wxEmptyString, pos, size, style))
1082 return false;
1083
1084 SetPopupMaxHeight(400);
1085
1086 m_stylePopup = new wxRichTextStyleComboPopup;
1087
1088 SetPopupControl(m_stylePopup);
1089
1090 return true;
1091 }
1092
1093 /// Auto-select from style under caret in idle time
1094
1095 // TODO: must be able to show italic, bold, combinations
1096 // in style box. Do we have a concept of automatic, temporary
1097 // styles that are added whenever we wish to show a style
1098 // that doesn't exist already? E.g. "Bold, Italic, Underline".
1099 // Word seems to generate these things on the fly.
1100 // If there's a named style already, it uses e.g. Heading1 + Bold, Italic
1101 // If you unembolden text in a style that has bold, it uses the
1102 // term "Not bold".
1103 // TODO: order styles alphabetically. This means indexes can change,
1104 // so need a different way to specify selections, i.e. by name.
1105
1106 void wxRichTextStyleComboCtrl::OnIdle(wxIdleEvent& event)
1107 {
1108 event.Skip();
1109
1110 if ( !m_stylePopup )
1111 return;
1112
1113 wxRichTextCtrl * const richtext = GetRichTextCtrl();
1114 if ( !richtext )
1115 return;
1116
1117 if ( !IsPopupShown() && IsShownOnScreen() && wxWindow::FindFocus() != this )
1118 {
1119 wxString styleName =
1120 wxRichTextStyleListBox::GetStyleToShowInIdleTime(richtext, m_stylePopup->GetStyleType());
1121
1122 wxString currentValue = GetValue();
1123 if (!styleName.IsEmpty())
1124 {
1125 // Don't do the selection if it's already set
1126 if (currentValue == styleName)
1127 return;
1128
1129 SetValue(styleName);
1130 }
1131 else if (!currentValue.IsEmpty())
1132 SetValue(wxEmptyString);
1133 }
1134 }
1135
1136 #endif
1137 // wxUSE_COMBOCTRL
1138
1139 #endif
1140 // wxUSE_HTML
1141
1142 #endif
1143 // wxUSE_RICHTEXT