]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextformatdlg.cpp
include vms_jackets.h directly from wx/platform.h instead of doing it before wx/wxpre...
[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"
1f71a770
JS
60// Digital Mars can't cope with this much code
61#ifndef __DMC__
62 #include "richtextliststylepage.cpp"
63#endif
97ff49b3 64#include "richtextstylepage.cpp"
ee5c8f01 65#endif
97ff49b3 66
61c006ec 67#if 0 // def __WXMAC__
c730fa07 68#define wxRICHTEXT_USE_TOOLBOOK 1
97ff49b3 69#else
c730fa07 70#define wxRICHTEXT_USE_TOOLBOOK 0
97ff49b3
JS
71#endif
72
dadd4f55
JS
73bool wxRichTextFormattingDialog::sm_showToolTips = false;
74
97ff49b3
JS
75IMPLEMENT_CLASS(wxRichTextFormattingDialog, wxPropertySheetDialog)
76
77BEGIN_EVENT_TABLE(wxRichTextFormattingDialog, wxPropertySheetDialog)
2b741a39 78 EVT_BOOKCTRL_PAGE_CHANGED(wxID_ANY, wxRichTextFormattingDialog::OnTabChanged)
97ff49b3
JS
79END_EVENT_TABLE()
80
81wxRichTextFormattingDialogFactory* wxRichTextFormattingDialog::ms_FormattingDialogFactory = NULL;
82
83void wxRichTextFormattingDialog::Init()
84{
85 m_imageList = NULL;
86 m_styleDefinition = NULL;
87 m_styleSheet = NULL;
88}
89
90wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
91{
92 delete m_imageList;
93 delete m_styleDefinition;
94}
95
96bool 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,
528e5cf8 106 style | (int)wxPlatform::IfNot(wxOS_WINDOWS_CE, resizeBorder)
97ff49b3
JS
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
118bool 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)
127bool 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
133bool 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
142bool 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
154bool 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
165bool 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
177bool 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
2b741a39 184void wxRichTextFormattingDialog::OnTabChanged(wxBookCtrlEvent& event)
97ff49b3
JS
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
1807a1f3
JS
209/// Respond to help command
210void 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
97ff49b3
JS
221void 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
233bool 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;
61c006ec 240 bool selected = false;
97ff49b3
JS
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);
61c006ec
JS
252 dialog->GetBookCtrl()->AddPage(panel, title, !selected, imageIndex);
253 selected = true;
41a85215 254
1807a1f3 255 dialog->AddPageId(pageId);
97ff49b3
JS
256 }
257 }
258 }
2b741a39 259
97ff49b3
JS
260 return true;
261}
262
263/// Create a page, given a page identifier
264wxPanel* 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 }
1f71a770 296#ifndef __DMC__
dadd4f55
JS
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 }
1f71a770 303#endif
97ff49b3
JS
304 else
305 return NULL;
306}
307
308/// Enumerate all available page identifiers
309int 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,
dadd4f55
JS
316 wxRICHTEXT_FORMAT_TABS,
317 wxRICHTEXT_FORMAT_LIST_STYLE };
97ff49b3 318
dadd4f55 319 if (i < 0 || i > 5)
97ff49b3
JS
320 return -1;
321
322 return pages[i];
323}
324
325/// Get the number of available page identifiers
326int wxRichTextFormattingDialogFactory::GetPageIdCount() const
327{
1f71a770
JS
328#ifdef __DMC__
329 return 5;
330#else
dadd4f55 331 return 6;
1f71a770 332#endif
97ff49b3
JS
333}
334
335/// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
336bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog* dialog)
337{
d0cf4953
VZ
338#if wxRICHTEXT_USE_TOOLBOOK
339 int sheetStyle = wxPROPSHEET_SHRINKTOFIT;
97ff49b3 340#ifdef __WXMAC__
d0cf4953 341 sheetStyle |= wxPROPSHEET_BUTTONTOOLBOOK;
97ff49b3 342#else
d0cf4953 343 sheetStyle |= wxPROPSHEET_TOOLBOOK;
97ff49b3
JS
344#endif
345
d0cf4953
VZ
346 dialog->SetSheetStyle(sheetStyle);
347 dialog->SetSheetInnerBorder(0);
348 dialog->SetSheetOuterBorder(0);
c730fa07
JS
349#else
350 wxUnusedVar(dialog);
d0cf4953 351#endif // wxRICHTEXT_USE_TOOLBOOK
97ff49b3
JS
352
353 return true;
354}
355
356/// Create the main dialog buttons
357bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog* dialog)
358{
97ff49b3
JS
359 int flags = wxOK|wxCANCEL;
360#ifndef __WXWINCE__
dadd4f55
JS
361 if (dialog->GetWindowStyleFlag() & wxRICHTEXT_FORMAT_HELP_BUTTON)
362 flags |= wxHELP;
97ff49b3
JS
363#endif
364
d0cf4953
VZ
365 // If using a toolbook, also follow Mac style and don't create buttons
366#if !wxRICHTEXT_USE_TOOLBOOK
367 dialog->CreateButtons(flags);
368#endif
97ff49b3
JS
369
370 return true;
371}
372
373/*
374 * Module to initialise and clean up handlers
375 */
376
377class wxRichTextFormattingDialogModule: public wxModule
378{
379DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule)
380public:
381 wxRichTextFormattingDialogModule() {}
47b378bd
VS
382 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory); return true; }
383 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL); }
97ff49b3
JS
384};
385
386IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule, wxModule)
387
388/*
389 * Font preview control
390 */
391
392BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl, wxWindow)
393 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint)
394END_EVENT_TABLE()
395
396void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
397{
398 wxPaintDC dc(this);
399
400 wxSize size = GetSize();
401 wxFont font = GetFont();
402
403 if ( font.Ok() )
404 {
405 dc.SetFont(font);
406 // Calculate vertical and horizontal centre
0bc58f95 407 wxCoord w = 0, h = 0;
97ff49b3
JS
408
409 wxString text(_("ABCDEFGabcdefg12345"));
42688aea
JS
410 if (GetTextEffects() & wxTEXT_ATTR_EFFECT_CAPITALS)
411 text.MakeUpper();
97ff49b3
JS
412
413 dc.GetTextExtent( text, &w, &h);
414 int cx = wxMax(2, (size.x/2) - (w/2));
415 int cy = wxMax(2, (size.y/2) - (h/2));
416
417 dc.SetTextForeground(GetForegroundColour());
418 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
419 dc.DrawText(text, cx, cy);
c730fa07 420
42688aea
JS
421 if (GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH)
422 {
423 dc.SetPen(wxPen(GetForegroundColour(), 1));
424 dc.DrawLine(cx, (int) (cy + h/2 + 0.5), cx + w, (int) (cy + h/2 + 0.5));
425 }
c730fa07 426
97ff49b3
JS
427 dc.DestroyClippingRegion();
428 }
429}
430
431// Helper for pages to get the top-level dialog
432wxRichTextFormattingDialog* wxRichTextFormattingDialog::GetDialog(wxWindow* win)
433{
434 wxWindow* p = win->GetParent();
435 while (p && !p->IsKindOf(CLASSINFO(wxRichTextFormattingDialog)))
436 p = p->GetParent();
437 wxRichTextFormattingDialog* dialog = wxDynamicCast(p, wxRichTextFormattingDialog);
438 return dialog;
439}
440
441
442// Helper for pages to get the attributes
443wxTextAttrEx* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow* win)
444{
445 wxRichTextFormattingDialog* dialog = GetDialog(win);
446 if (dialog)
447 return & dialog->GetAttributes();
448 else
449 return NULL;
450}
451
452// Helper for pages to get the style
453wxRichTextStyleDefinition* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow* win)
454{
455 wxRichTextFormattingDialog* dialog = GetDialog(win);
456 if (dialog)
457 return dialog->GetStyleDefinition();
458 else
459 return NULL;
460}
461
462/*
463 * A control for displaying a small preview of a colour or bitmap
464 */
465
466BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl, wxControl)
467 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent)
468END_EVENT_TABLE()
469
470IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl, wxControl)
471
472wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style):
473 wxControl(parent, id, pos, size, style)
474{
475 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
476 SetBackgroundStyle(wxBG_STYLE_COLOUR);
477}
478
479wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
480{
481}
482
483void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent& event)
484{
485 if (event.LeftDown())
486 {
487 wxWindow* parent = GetParent();
488 while (parent != NULL && !parent->IsKindOf(CLASSINFO(wxDialog)) && !parent->IsKindOf(CLASSINFO(wxFrame)))
489 parent = parent->GetParent();
490
491 wxColourData data;
492 data.SetChooseFull(true);
493 data.SetColour(m_colour);
2b741a39 494#if wxUSE_COLOURDLG
97ff49b3
JS
495 wxColourDialog *dialog = new wxColourDialog(parent, &data);
496 // Crashes on wxMac (no m_peer)
497#ifndef __WXMAC__
498 dialog->SetTitle(_("Background colour"));
499#endif
500 if (dialog->ShowModal() == wxID_OK)
501 {
502 wxColourData retData = dialog->GetColourData();
503 m_colour = retData.GetColour();
504 SetBackgroundColour(m_colour);
505 }
506 dialog->Destroy();
2b741a39 507#endif // wxUSE_COLOURDLG
97ff49b3
JS
508 Refresh();
509
510 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
511 GetEventHandler()->ProcessEvent(event);
512 }
513}
514
515#if wxUSE_HTML
516
517/*!
518 * wxRichTextFontListBox class declaration
519 * A listbox to display styles.
520 */
521
522IMPLEMENT_CLASS(wxRichTextFontListBox, wxHtmlListBox)
523
524BEGIN_EVENT_TABLE(wxRichTextFontListBox, wxHtmlListBox)
525END_EVENT_TABLE()
526
527wxRichTextFontListBox::wxRichTextFontListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos,
528 const wxSize& size, long style)
529{
530 Init();
531 Create(parent, id, pos, size, style);
532}
533
534bool wxRichTextFontListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
535 const wxSize& size, long style)
536{
537 return wxHtmlListBox::Create(parent, id, pos, size, style);
538}
539
540wxRichTextFontListBox::~wxRichTextFontListBox()
541{
542}
543
544/// Returns the HTML for this item
545wxString wxRichTextFontListBox::OnGetItem(size_t n) const
546{
547 if (m_faceNames.GetCount() == 0)
548 return wxEmptyString;
549
550 wxString str = CreateHTML(m_faceNames[n]);
551 return str;
552}
553
554/// Get font name for index
555wxString wxRichTextFontListBox::GetFaceName(size_t i) const
556{
557 return m_faceNames[i];
558}
559
560/// Set selection for string, returning the index.
561int wxRichTextFontListBox::SetFaceNameSelection(const wxString& name)
562{
563 int i = m_faceNames.Index(name);
564 SetSelection(i);
565
566 return i;
567}
568
569/// Updates the font list
570void wxRichTextFontListBox::UpdateFonts()
571{
dadd4f55 572 wxArrayString facenames = wxRichTextCtrl::GetAvailableFontNames();
97ff49b3
JS
573 m_faceNames = facenames;
574 m_faceNames.Sort();
575
576 SetItemCount(m_faceNames.GetCount());
577 Refresh();
578}
579
580#if 0
581// Convert a colour to a 6-digit hex string
582static wxString ColourToHexString(const wxColour& col)
583{
584 wxString hex;
585
586 hex += wxDecToHex(col.Red());
587 hex += wxDecToHex(col.Green());
588 hex += wxDecToHex(col.Blue());
589
590 return hex;
591}
592#endif
593
594/// Creates a suitable HTML fragment for a definition
595wxString wxRichTextFontListBox::CreateHTML(const wxString& facename) const
596{
597 wxString str = wxT("<font");
598
599 str << wxT(" size=\"+2\"");;
600
601 if (!facename.IsEmpty() && facename != _("(none)"))
602 str << wxT(" face=\"") << facename << wxT("\"");
603/*
604 if (def->GetStyle().GetTextColour().Ok())
605 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
606*/
607
608 str << wxT(">");
609
610 bool hasBold = false;
611
612 if (hasBold)
613 str << wxT("<b>");
614
615 str += facename;
616
617 if (hasBold)
618 str << wxT("</b>");
619
620 str << wxT("</font>");
621
622 return str;
623}
624
625#endif
626 // wxUSE_HTML
627
628
629#endif
630 // wxUSE_RICHTEXT