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