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