replaced run-time tests for wxRICHTEXT_USE_TOOLBOOK with compile-time ones to avoid...
[wxWidgets.git] / src / richtext / richtextformatdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextformatdlg.cpp
3 // Purpose: Formatting dialog for wxRichTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2006-10-01
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/richtextformatdlg.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/listbox.h"
25 #include "wx/combobox.h"
26 #include "wx/textctrl.h"
27 #include "wx/sizer.h"
28 #include "wx/stattext.h"
29 #include "wx/statline.h"
30 #include "wx/radiobut.h"
31 #include "wx/icon.h"
32 #include "wx/bitmap.h"
33 #include "wx/dcclient.h"
34 #include "wx/frame.h"
35 #include "wx/checkbox.h"
36 #include "wx/button.h"
37 #endif // WX_PRECOMP
38
39 #include "wx/bookctrl.h"
40 #include "wx/colordlg.h"
41 #include "wx/settings.h"
42 #include "wx/module.h"
43 #include "wx/imaglist.h"
44
45 #include "wx/richtext/richtextctrl.h"
46 #include "wx/richtext/richtextstyles.h"
47
48 #ifdef __WXMAC__
49 #include "../../src/richtext/richtextfontpage.cpp"
50 #include "../../src/richtext/richtextindentspage.cpp"
51 #include "../../src/richtext/richtexttabspage.cpp"
52 #include "../../src/richtext/richtextbulletspage.cpp"
53 #include "../../src/richtext/richtextstylepage.cpp"
54 #include "../../src/richtext/richtextliststylepage.cpp"
55 #else
56 #include "richtextfontpage.cpp"
57 #include "richtextindentspage.cpp"
58 #include "richtexttabspage.cpp"
59 #include "richtextbulletspage.cpp"
60 // Digital Mars can't cope with this much code
61 #ifndef __DMC__
62 #include "richtextliststylepage.cpp"
63 #endif
64 #include "richtextstylepage.cpp"
65 #endif
66
67 #if 0 // def __WXMAC__
68 #define wxRICHTEXT_USE_TOOLBOOK true
69 #else
70 #define wxRICHTEXT_USE_TOOLBOOK false
71 #endif
72
73 bool wxRichTextFormattingDialog::sm_showToolTips = false;
74
75 IMPLEMENT_CLASS(wxRichTextFormattingDialog, wxPropertySheetDialog)
76
77 BEGIN_EVENT_TABLE(wxRichTextFormattingDialog, wxPropertySheetDialog)
78 EVT_BOOKCTRL_PAGE_CHANGED(wxID_ANY, wxRichTextFormattingDialog::OnTabChanged)
79 END_EVENT_TABLE()
80
81 wxRichTextFormattingDialogFactory* wxRichTextFormattingDialog::ms_FormattingDialogFactory = NULL;
82
83 void wxRichTextFormattingDialog::Init()
84 {
85 m_imageList = NULL;
86 m_styleDefinition = NULL;
87 m_styleSheet = NULL;
88 }
89
90 wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
91 {
92 delete m_imageList;
93 delete m_styleDefinition;
94 }
95
96 bool wxRichTextFormattingDialog::Create(long flags, wxWindow* parent, const wxString& title, wxWindowID id,
97 const wxPoint& pos, const wxSize& sz, long style)
98 {
99 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP|wxWS_EX_VALIDATE_RECURSIVELY);
100
101 int resizeBorder = wxRESIZE_BORDER;
102
103 GetFormattingDialogFactory()->SetSheetStyle(this);
104
105 wxPropertySheetDialog::Create(parent, id, title, pos, sz,
106 style | (int)wxPlatform::IfNot(wxOS_WINDOWS_CE, resizeBorder)
107 );
108
109 GetFormattingDialogFactory()->CreateButtons(this);
110 GetFormattingDialogFactory()->CreatePages(flags, this);
111
112 LayoutDialog();
113
114 return true;
115 }
116
117 /// Get attributes from the given range
118 bool wxRichTextFormattingDialog::GetStyle(wxRichTextCtrl* ctrl, const wxRichTextRange& range)
119 {
120 if (ctrl->GetBuffer().GetStyleForRange(range.ToInternal(), m_attributes))
121 return UpdateDisplay();
122 else
123 return false;
124 }
125
126 /// Apply attributes to the given range, only applying if necessary (wxRICHTEXT_SETSTYLE_OPTIMIZE)
127 bool wxRichTextFormattingDialog::ApplyStyle(wxRichTextCtrl* ctrl, const wxRichTextRange& range, int flags)
128 {
129 return ctrl->SetStyleEx(range, m_attributes, flags);
130 }
131
132 /// Set the attributes and optionally update the display
133 bool wxRichTextFormattingDialog::SetStyle(const wxTextAttrEx& style, bool update)
134 {
135 m_attributes = style;
136 if (update)
137 UpdateDisplay();
138 return true;
139 }
140
141 /// Set the style definition and optionally update the display
142 bool wxRichTextFormattingDialog::SetStyleDefinition(const wxRichTextStyleDefinition& styleDef, wxRichTextStyleSheet* sheet, bool update)
143 {
144 m_styleSheet = sheet;
145
146 if (m_styleDefinition)
147 delete m_styleDefinition;
148 m_styleDefinition = styleDef.Clone();
149
150 return SetStyle(m_styleDefinition->GetStyle(), update);
151 }
152
153 /// Transfers the data and from to the window
154 bool wxRichTextFormattingDialog::TransferDataToWindow()
155 {
156 if (m_styleDefinition)
157 m_attributes = m_styleDefinition->GetStyle();
158
159 if (!wxPropertySheetDialog::TransferDataToWindow())
160 return false;
161
162 return true;
163 }
164
165 bool wxRichTextFormattingDialog::TransferDataFromWindow()
166 {
167 if (!wxPropertySheetDialog::TransferDataFromWindow())
168 return false;
169
170 if (m_styleDefinition)
171 m_styleDefinition->GetStyle() = m_attributes;
172
173 return true;
174 }
175
176 /// Update the display
177 bool wxRichTextFormattingDialog::UpdateDisplay()
178 {
179 return TransferDataToWindow();
180 }
181
182 /// Apply the styles when a different tab is selected, so the previews are
183 /// up to date
184 void wxRichTextFormattingDialog::OnTabChanged(wxBookCtrlEvent& event)
185 {
186 if (GetBookCtrl() != event.GetEventObject())
187 {
188 event.Skip();
189 return;
190 }
191
192 int oldPageId = event.GetOldSelection();
193 if (oldPageId != -1)
194 {
195 wxWindow* page = GetBookCtrl()->GetPage(oldPageId);
196 if (page)
197 page->TransferDataFromWindow();
198 }
199
200 int pageId = event.GetSelection();
201 if (pageId != -1)
202 {
203 wxWindow* page = GetBookCtrl()->GetPage(pageId);
204 if (page)
205 page->TransferDataToWindow();
206 }
207 }
208
209 /// Respond to help command
210 void wxRichTextFormattingDialog::OnHelp(wxCommandEvent& event)
211 {
212 int selPage = GetBookCtrl()->GetSelection();
213 if (selPage != wxNOT_FOUND)
214 {
215 int pageId = m_pageIds[selPage];
216 if (!GetFormattingDialogFactory()->ShowHelp(pageId, this))
217 event.Skip();
218 }
219 }
220
221 void wxRichTextFormattingDialog::SetFormattingDialogFactory(wxRichTextFormattingDialogFactory* factory)
222 {
223 if (ms_FormattingDialogFactory)
224 delete ms_FormattingDialogFactory;
225 ms_FormattingDialogFactory = factory;
226 }
227
228 /*!
229 * Factory for formatting dialog
230 */
231
232 /// Create all pages, under the dialog's book control, also calling AddPage
233 bool wxRichTextFormattingDialogFactory::CreatePages(long pages, wxRichTextFormattingDialog* dialog)
234 {
235 if (dialog->GetImageList())
236 dialog->GetBookCtrl()->SetImageList(dialog->GetImageList());
237
238 int availablePageCount = GetPageIdCount();
239 int i;
240 bool selected = false;
241 for (i = 0; i < availablePageCount; i ++)
242 {
243 int pageId = GetPageId(i);
244 if (pageId != -1 && (pages & pageId))
245 {
246 wxString title;
247 wxPanel* panel = CreatePage(pageId, title, dialog);
248 wxASSERT( panel != NULL );
249 if (panel)
250 {
251 int imageIndex = GetPageImage(pageId);
252 dialog->GetBookCtrl()->AddPage(panel, title, !selected, imageIndex);
253 selected = true;
254
255 dialog->AddPageId(pageId);
256 }
257 }
258 }
259
260 return true;
261 }
262
263 /// Create a page, given a page identifier
264 wxPanel* wxRichTextFormattingDialogFactory::CreatePage(int page, wxString& title, wxRichTextFormattingDialog* dialog)
265 {
266 if (page == wxRICHTEXT_FORMAT_STYLE_EDITOR)
267 {
268 wxRichTextStylePage* page = new wxRichTextStylePage(dialog->GetBookCtrl(), wxID_ANY);
269 title = _("Style");
270 return page;
271 }
272 else if (page == wxRICHTEXT_FORMAT_FONT)
273 {
274 wxRichTextFontPage* page = new wxRichTextFontPage(dialog->GetBookCtrl(), wxID_ANY);
275 title = _("Font");
276 return page;
277 }
278 else if (page == wxRICHTEXT_FORMAT_INDENTS_SPACING)
279 {
280 wxRichTextIndentsSpacingPage* page = new wxRichTextIndentsSpacingPage(dialog->GetBookCtrl(), wxID_ANY);
281 title = _("Indents && Spacing");
282 return page;
283 }
284 else if (page == wxRICHTEXT_FORMAT_TABS)
285 {
286 wxRichTextTabsPage* page = new wxRichTextTabsPage(dialog->GetBookCtrl(), wxID_ANY);
287 title = _("Tabs");
288 return page;
289 }
290 else if (page == wxRICHTEXT_FORMAT_BULLETS)
291 {
292 wxRichTextBulletsPage* page = new wxRichTextBulletsPage(dialog->GetBookCtrl(), wxID_ANY);
293 title = _("Bullets");
294 return page;
295 }
296 #ifndef __DMC__
297 else if (page == wxRICHTEXT_FORMAT_LIST_STYLE)
298 {
299 wxRichTextListStylePage* page = new wxRichTextListStylePage(dialog->GetBookCtrl(), wxID_ANY);
300 title = _("List Style");
301 return page;
302 }
303 #endif
304 else
305 return NULL;
306 }
307
308 /// Enumerate all available page identifiers
309 int wxRichTextFormattingDialogFactory::GetPageId(int i) const
310 {
311 int pages[] = {
312 wxRICHTEXT_FORMAT_STYLE_EDITOR,
313 wxRICHTEXT_FORMAT_FONT,
314 wxRICHTEXT_FORMAT_INDENTS_SPACING,
315 wxRICHTEXT_FORMAT_BULLETS,
316 wxRICHTEXT_FORMAT_TABS,
317 wxRICHTEXT_FORMAT_LIST_STYLE };
318
319 if (i < 0 || i > 5)
320 return -1;
321
322 return pages[i];
323 }
324
325 /// Get the number of available page identifiers
326 int wxRichTextFormattingDialogFactory::GetPageIdCount() const
327 {
328 #ifdef __DMC__
329 return 5;
330 #else
331 return 6;
332 #endif
333 }
334
335 /// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
336 bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog* dialog)
337 {
338 #if wxRICHTEXT_USE_TOOLBOOK
339 int sheetStyle = wxPROPSHEET_SHRINKTOFIT;
340 #ifdef __WXMAC__
341 sheetStyle |= wxPROPSHEET_BUTTONTOOLBOOK;
342 #else
343 sheetStyle |= wxPROPSHEET_TOOLBOOK;
344 #endif
345
346 dialog->SetSheetStyle(sheetStyle);
347 dialog->SetSheetInnerBorder(0);
348 dialog->SetSheetOuterBorder(0);
349 #endif // wxRICHTEXT_USE_TOOLBOOK
350
351 return true;
352 }
353
354 /// Create the main dialog buttons
355 bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog* dialog)
356 {
357 int flags = wxOK|wxCANCEL;
358 #ifndef __WXWINCE__
359 if (dialog->GetWindowStyleFlag() & wxRICHTEXT_FORMAT_HELP_BUTTON)
360 flags |= wxHELP;
361 #endif
362
363 // If using a toolbook, also follow Mac style and don't create buttons
364 #if !wxRICHTEXT_USE_TOOLBOOK
365 dialog->CreateButtons(flags);
366 #endif
367
368 return true;
369 }
370
371 /*
372 * Module to initialise and clean up handlers
373 */
374
375 class wxRichTextFormattingDialogModule: public wxModule
376 {
377 DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule)
378 public:
379 wxRichTextFormattingDialogModule() {}
380 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory); return true; };
381 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL); };
382 };
383
384 IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule, wxModule)
385
386 /*
387 * Font preview control
388 */
389
390 BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl, wxWindow)
391 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint)
392 END_EVENT_TABLE()
393
394 void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
395 {
396 wxPaintDC dc(this);
397
398 wxSize size = GetSize();
399 wxFont font = GetFont();
400
401 if ( font.Ok() )
402 {
403 dc.SetFont(font);
404 // Calculate vertical and horizontal centre
405 long w = 0, h = 0;
406
407 wxString text(_("ABCDEFGabcdefg12345"));
408 if (GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS)
409 text.MakeUpper();
410
411 dc.GetTextExtent( text, &w, &h);
412 int cx = wxMax(2, (size.x/2) - (w/2));
413 int cy = wxMax(2, (size.y/2) - (h/2));
414
415 dc.SetTextForeground(GetForegroundColour());
416 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
417 dc.DrawText(text, cx, cy);
418
419 if (GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH)
420 {
421 dc.SetPen(wxPen(GetForegroundColour(), 1));
422 dc.DrawLine(cx, (int) (cy + h/2 + 0.5), cx + w, (int) (cy + h/2 + 0.5));
423 }
424
425 dc.DestroyClippingRegion();
426 }
427 }
428
429 // Helper for pages to get the top-level dialog
430 wxRichTextFormattingDialog* wxRichTextFormattingDialog::GetDialog(wxWindow* win)
431 {
432 wxWindow* p = win->GetParent();
433 while (p && !p->IsKindOf(CLASSINFO(wxRichTextFormattingDialog)))
434 p = p->GetParent();
435 wxRichTextFormattingDialog* dialog = wxDynamicCast(p, wxRichTextFormattingDialog);
436 return dialog;
437 }
438
439
440 // Helper for pages to get the attributes
441 wxTextAttrEx* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow* win)
442 {
443 wxRichTextFormattingDialog* dialog = GetDialog(win);
444 if (dialog)
445 return & dialog->GetAttributes();
446 else
447 return NULL;
448 }
449
450 // Helper for pages to get the style
451 wxRichTextStyleDefinition* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow* win)
452 {
453 wxRichTextFormattingDialog* dialog = GetDialog(win);
454 if (dialog)
455 return dialog->GetStyleDefinition();
456 else
457 return NULL;
458 }
459
460 /*
461 * A control for displaying a small preview of a colour or bitmap
462 */
463
464 BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl, wxControl)
465 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent)
466 END_EVENT_TABLE()
467
468 IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl, wxControl)
469
470 wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style):
471 wxControl(parent, id, pos, size, style)
472 {
473 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
474 SetBackgroundStyle(wxBG_STYLE_COLOUR);
475 }
476
477 wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
478 {
479 }
480
481 void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent& event)
482 {
483 if (event.LeftDown())
484 {
485 wxWindow* parent = GetParent();
486 while (parent != NULL && !parent->IsKindOf(CLASSINFO(wxDialog)) && !parent->IsKindOf(CLASSINFO(wxFrame)))
487 parent = parent->GetParent();
488
489 wxColourData data;
490 data.SetChooseFull(true);
491 data.SetColour(m_colour);
492 #if wxUSE_COLOURDLG
493 wxColourDialog *dialog = new wxColourDialog(parent, &data);
494 // Crashes on wxMac (no m_peer)
495 #ifndef __WXMAC__
496 dialog->SetTitle(_("Background colour"));
497 #endif
498 if (dialog->ShowModal() == wxID_OK)
499 {
500 wxColourData retData = dialog->GetColourData();
501 m_colour = retData.GetColour();
502 SetBackgroundColour(m_colour);
503 }
504 dialog->Destroy();
505 #endif // wxUSE_COLOURDLG
506 Refresh();
507
508 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
509 GetEventHandler()->ProcessEvent(event);
510 }
511 }
512
513 #if wxUSE_HTML
514
515 /*!
516 * wxRichTextFontListBox class declaration
517 * A listbox to display styles.
518 */
519
520 IMPLEMENT_CLASS(wxRichTextFontListBox, wxHtmlListBox)
521
522 BEGIN_EVENT_TABLE(wxRichTextFontListBox, wxHtmlListBox)
523 END_EVENT_TABLE()
524
525 wxRichTextFontListBox::wxRichTextFontListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos,
526 const wxSize& size, long style)
527 {
528 Init();
529 Create(parent, id, pos, size, style);
530 }
531
532 bool wxRichTextFontListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
533 const wxSize& size, long style)
534 {
535 return wxHtmlListBox::Create(parent, id, pos, size, style);
536 }
537
538 wxRichTextFontListBox::~wxRichTextFontListBox()
539 {
540 }
541
542 /// Returns the HTML for this item
543 wxString wxRichTextFontListBox::OnGetItem(size_t n) const
544 {
545 if (m_faceNames.GetCount() == 0)
546 return wxEmptyString;
547
548 wxString str = CreateHTML(m_faceNames[n]);
549 return str;
550 }
551
552 /// Get font name for index
553 wxString wxRichTextFontListBox::GetFaceName(size_t i) const
554 {
555 return m_faceNames[i];
556 }
557
558 /// Set selection for string, returning the index.
559 int wxRichTextFontListBox::SetFaceNameSelection(const wxString& name)
560 {
561 int i = m_faceNames.Index(name);
562 SetSelection(i);
563
564 return i;
565 }
566
567 /// Updates the font list
568 void wxRichTextFontListBox::UpdateFonts()
569 {
570 wxArrayString facenames = wxRichTextCtrl::GetAvailableFontNames();
571 m_faceNames = facenames;
572 m_faceNames.Sort();
573
574 SetItemCount(m_faceNames.GetCount());
575 Refresh();
576 }
577
578 #if 0
579 // Convert a colour to a 6-digit hex string
580 static wxString ColourToHexString(const wxColour& col)
581 {
582 wxString hex;
583
584 hex += wxDecToHex(col.Red());
585 hex += wxDecToHex(col.Green());
586 hex += wxDecToHex(col.Blue());
587
588 return hex;
589 }
590 #endif
591
592 /// Creates a suitable HTML fragment for a definition
593 wxString wxRichTextFontListBox::CreateHTML(const wxString& facename) const
594 {
595 wxString str = wxT("<font");
596
597 str << wxT(" size=\"+2\"");;
598
599 if (!facename.IsEmpty() && facename != _("(none)"))
600 str << wxT(" face=\"") << facename << wxT("\"");
601 /*
602 if (def->GetStyle().GetTextColour().Ok())
603 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
604 */
605
606 str << wxT(">");
607
608 bool hasBold = false;
609
610 if (hasBold)
611 str << wxT("<b>");
612
613 str += facename;
614
615 if (hasBold)
616 str << wxT("</b>");
617
618 str << wxT("</font>");
619
620 return str;
621 }
622
623 #endif
624 // wxUSE_HTML
625
626
627 #endif
628 // wxUSE_RICHTEXT