]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextformatdlg.cpp
making sure the overlay moves with its parent on OSX
[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"
41#include "wx/fontenum.h"
42#include "wx/settings.h"
43#include "wx/module.h"
62b51bfb 44#include "wx/imaglist.h"
97ff49b3 45
97ff49b3
JS
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
61c006ec 55#if 0 // def __WXMAC__
97ff49b3
JS
56#define wxRICHTEXT_USE_TOOLBOOK true
57#else
58#define wxRICHTEXT_USE_TOOLBOOK false
59#endif
60
61IMPLEMENT_CLASS(wxRichTextFormattingDialog, wxPropertySheetDialog)
62
63BEGIN_EVENT_TABLE(wxRichTextFormattingDialog, wxPropertySheetDialog)
2b741a39 64 EVT_BOOKCTRL_PAGE_CHANGED(wxID_ANY, wxRichTextFormattingDialog::OnTabChanged)
97ff49b3
JS
65END_EVENT_TABLE()
66
67wxRichTextFormattingDialogFactory* wxRichTextFormattingDialog::ms_FormattingDialogFactory = NULL;
68
69void wxRichTextFormattingDialog::Init()
70{
71 m_imageList = NULL;
72 m_styleDefinition = NULL;
73 m_styleSheet = NULL;
74}
75
76wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
77{
78 delete m_imageList;
79 delete m_styleDefinition;
80}
81
82bool 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,
528e5cf8 92 style | (int)wxPlatform::IfNot(wxOS_WINDOWS_CE, resizeBorder)
97ff49b3
JS
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
104bool 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)
113bool 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
119bool 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
128bool 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
140bool 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
151bool 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
163bool 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
2b741a39 170void wxRichTextFormattingDialog::OnTabChanged(wxBookCtrlEvent& event)
97ff49b3
JS
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
195void wxRichTextFormattingDialog::SetFormattingDialogFactory(wxRichTextFormattingDialogFactory* factory)
196{
197 if (ms_FormattingDialogFactory)
198 delete ms_FormattingDialogFactory;
199 ms_FormattingDialogFactory = factory;
200}
201
202/*!
203 * Factory for formatting dialog
204 */
205
206/// Create all pages, under the dialog's book control, also calling AddPage
207bool wxRichTextFormattingDialogFactory::CreatePages(long pages, wxRichTextFormattingDialog* dialog)
208{
209 if (dialog->GetImageList())
210 dialog->GetBookCtrl()->SetImageList(dialog->GetImageList());
211
212 int availablePageCount = GetPageIdCount();
213 int i;
61c006ec 214 bool selected = false;
97ff49b3
JS
215 for (i = 0; i < availablePageCount; i ++)
216 {
217 int pageId = GetPageId(i);
218 if (pageId != -1 && (pages & pageId))
219 {
220 wxString title;
221 wxPanel* panel = CreatePage(pageId, title, dialog);
222 wxASSERT( panel != NULL );
223 if (panel)
224 {
225 int imageIndex = GetPageImage(pageId);
61c006ec
JS
226 dialog->GetBookCtrl()->AddPage(panel, title, !selected, imageIndex);
227 selected = true;
97ff49b3
JS
228 }
229 }
230 }
2b741a39 231
97ff49b3
JS
232 return true;
233}
234
235/// Create a page, given a page identifier
236wxPanel* wxRichTextFormattingDialogFactory::CreatePage(int page, wxString& title, wxRichTextFormattingDialog* dialog)
237{
238 if (page == wxRICHTEXT_FORMAT_STYLE_EDITOR)
239 {
240 wxRichTextStylePage* page = new wxRichTextStylePage(dialog->GetBookCtrl(), wxID_ANY);
241 title = _("Style");
242 return page;
243 }
244 else if (page == wxRICHTEXT_FORMAT_FONT)
245 {
246 wxRichTextFontPage* page = new wxRichTextFontPage(dialog->GetBookCtrl(), wxID_ANY);
247 title = _("Font");
248 return page;
249 }
250 else if (page == wxRICHTEXT_FORMAT_INDENTS_SPACING)
251 {
252 wxRichTextIndentsSpacingPage* page = new wxRichTextIndentsSpacingPage(dialog->GetBookCtrl(), wxID_ANY);
253 title = _("Indents && Spacing");
254 return page;
255 }
256 else if (page == wxRICHTEXT_FORMAT_TABS)
257 {
258 wxRichTextTabsPage* page = new wxRichTextTabsPage(dialog->GetBookCtrl(), wxID_ANY);
259 title = _("Tabs");
260 return page;
261 }
262 else if (page == wxRICHTEXT_FORMAT_BULLETS)
263 {
264 wxRichTextBulletsPage* page = new wxRichTextBulletsPage(dialog->GetBookCtrl(), wxID_ANY);
265 title = _("Bullets");
266 return page;
267 }
268 else
269 return NULL;
270}
271
272/// Enumerate all available page identifiers
273int wxRichTextFormattingDialogFactory::GetPageId(int i) const
274{
275 int pages[] = {
276 wxRICHTEXT_FORMAT_STYLE_EDITOR,
277 wxRICHTEXT_FORMAT_FONT,
278 wxRICHTEXT_FORMAT_INDENTS_SPACING,
279 wxRICHTEXT_FORMAT_BULLETS,
280 wxRICHTEXT_FORMAT_TABS };
281
282 if (i < 0 || i > 4)
283 return -1;
284
285 return pages[i];
286}
287
288/// Get the number of available page identifiers
289int wxRichTextFormattingDialogFactory::GetPageIdCount() const
290{
291 return 5;
292}
293
294/// Set the sheet style, called at the start of wxRichTextFormattingDialog::Create
295bool wxRichTextFormattingDialogFactory::SetSheetStyle(wxRichTextFormattingDialog* dialog)
296{
297 bool useToolBook = wxRICHTEXT_USE_TOOLBOOK;
298 if (useToolBook)
299 {
300 int sheetStyle = wxPROPSHEET_SHRINKTOFIT;
301#ifdef __WXMAC__
302 sheetStyle |= wxPROPSHEET_BUTTONTOOLBOOK;
303#else
304 sheetStyle |= wxPROPSHEET_TOOLBOOK;
305#endif
306
307 dialog->SetSheetStyle(sheetStyle);
308 dialog->SetSheetInnerBorder(0);
309 dialog->SetSheetOuterBorder(0);
310 }
311
312 return true;
313}
314
315/// Create the main dialog buttons
316bool wxRichTextFormattingDialogFactory::CreateButtons(wxRichTextFormattingDialog* dialog)
317{
318 bool useToolBook = wxRICHTEXT_USE_TOOLBOOK;
319
320 // If using a toolbook, also follow Mac style and don't create buttons
321 int flags = wxOK|wxCANCEL;
322#ifndef __WXWINCE__
323 flags |= wxHELP;
324#endif
325
326 if (!useToolBook)
327 dialog->CreateButtons(flags);
328
329 return true;
330}
331
332/*
333 * Module to initialise and clean up handlers
334 */
335
336class wxRichTextFormattingDialogModule: public wxModule
337{
338DECLARE_DYNAMIC_CLASS(wxRichTextFormattingDialogModule)
339public:
340 wxRichTextFormattingDialogModule() {}
341 bool OnInit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(new wxRichTextFormattingDialogFactory); return true; };
342 void OnExit() { wxRichTextFormattingDialog::SetFormattingDialogFactory(NULL); };
343};
344
345IMPLEMENT_DYNAMIC_CLASS(wxRichTextFormattingDialogModule, wxModule)
346
347/*
348 * Font preview control
349 */
350
351BEGIN_EVENT_TABLE(wxRichTextFontPreviewCtrl, wxWindow)
352 EVT_PAINT(wxRichTextFontPreviewCtrl::OnPaint)
353END_EVENT_TABLE()
354
355void wxRichTextFontPreviewCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
356{
357 wxPaintDC dc(this);
358
359 wxSize size = GetSize();
360 wxFont font = GetFont();
361
362 if ( font.Ok() )
363 {
364 dc.SetFont(font);
365 // Calculate vertical and horizontal centre
366 long w = 0, h = 0;
367
368 wxString text(_("ABCDEFGabcdefg12345"));
369
370 dc.GetTextExtent( text, &w, &h);
371 int cx = wxMax(2, (size.x/2) - (w/2));
372 int cy = wxMax(2, (size.y/2) - (h/2));
373
374 dc.SetTextForeground(GetForegroundColour());
375 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
376 dc.DrawText(text, cx, cy);
377 dc.DestroyClippingRegion();
378 }
379}
380
381// Helper for pages to get the top-level dialog
382wxRichTextFormattingDialog* wxRichTextFormattingDialog::GetDialog(wxWindow* win)
383{
384 wxWindow* p = win->GetParent();
385 while (p && !p->IsKindOf(CLASSINFO(wxRichTextFormattingDialog)))
386 p = p->GetParent();
387 wxRichTextFormattingDialog* dialog = wxDynamicCast(p, wxRichTextFormattingDialog);
388 return dialog;
389}
390
391
392// Helper for pages to get the attributes
393wxTextAttrEx* wxRichTextFormattingDialog::GetDialogAttributes(wxWindow* win)
394{
395 wxRichTextFormattingDialog* dialog = GetDialog(win);
396 if (dialog)
397 return & dialog->GetAttributes();
398 else
399 return NULL;
400}
401
402// Helper for pages to get the style
403wxRichTextStyleDefinition* wxRichTextFormattingDialog::GetDialogStyleDefinition(wxWindow* win)
404{
405 wxRichTextFormattingDialog* dialog = GetDialog(win);
406 if (dialog)
407 return dialog->GetStyleDefinition();
408 else
409 return NULL;
410}
411
412/*
413 * A control for displaying a small preview of a colour or bitmap
414 */
415
416BEGIN_EVENT_TABLE(wxRichTextColourSwatchCtrl, wxControl)
417 EVT_MOUSE_EVENTS(wxRichTextColourSwatchCtrl::OnMouseEvent)
418END_EVENT_TABLE()
419
420IMPLEMENT_CLASS(wxRichTextColourSwatchCtrl, wxControl)
421
422wxRichTextColourSwatchCtrl::wxRichTextColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style):
423 wxControl(parent, id, pos, size, style)
424{
425 SetColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
426 SetBackgroundStyle(wxBG_STYLE_COLOUR);
427}
428
429wxRichTextColourSwatchCtrl::~wxRichTextColourSwatchCtrl()
430{
431}
432
433void wxRichTextColourSwatchCtrl::OnMouseEvent(wxMouseEvent& event)
434{
435 if (event.LeftDown())
436 {
437 wxWindow* parent = GetParent();
438 while (parent != NULL && !parent->IsKindOf(CLASSINFO(wxDialog)) && !parent->IsKindOf(CLASSINFO(wxFrame)))
439 parent = parent->GetParent();
440
441 wxColourData data;
442 data.SetChooseFull(true);
443 data.SetColour(m_colour);
2b741a39 444#if wxUSE_COLOURDLG
97ff49b3
JS
445 wxColourDialog *dialog = new wxColourDialog(parent, &data);
446 // Crashes on wxMac (no m_peer)
447#ifndef __WXMAC__
448 dialog->SetTitle(_("Background colour"));
449#endif
450 if (dialog->ShowModal() == wxID_OK)
451 {
452 wxColourData retData = dialog->GetColourData();
453 m_colour = retData.GetColour();
454 SetBackgroundColour(m_colour);
455 }
456 dialog->Destroy();
2b741a39 457#endif // wxUSE_COLOURDLG
97ff49b3
JS
458 Refresh();
459
460 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
461 GetEventHandler()->ProcessEvent(event);
462 }
463}
464
465#if wxUSE_HTML
466
467/*!
468 * wxRichTextFontListBox class declaration
469 * A listbox to display styles.
470 */
471
472IMPLEMENT_CLASS(wxRichTextFontListBox, wxHtmlListBox)
473
474BEGIN_EVENT_TABLE(wxRichTextFontListBox, wxHtmlListBox)
475END_EVENT_TABLE()
476
477wxRichTextFontListBox::wxRichTextFontListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos,
478 const wxSize& size, long style)
479{
480 Init();
481 Create(parent, id, pos, size, style);
482}
483
484bool wxRichTextFontListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
485 const wxSize& size, long style)
486{
487 return wxHtmlListBox::Create(parent, id, pos, size, style);
488}
489
490wxRichTextFontListBox::~wxRichTextFontListBox()
491{
492}
493
494/// Returns the HTML for this item
495wxString wxRichTextFontListBox::OnGetItem(size_t n) const
496{
497 if (m_faceNames.GetCount() == 0)
498 return wxEmptyString;
499
500 wxString str = CreateHTML(m_faceNames[n]);
501 return str;
502}
503
504/// Get font name for index
505wxString wxRichTextFontListBox::GetFaceName(size_t i) const
506{
507 return m_faceNames[i];
508}
509
510/// Set selection for string, returning the index.
511int wxRichTextFontListBox::SetFaceNameSelection(const wxString& name)
512{
513 int i = m_faceNames.Index(name);
514 SetSelection(i);
515
516 return i;
517}
518
519/// Updates the font list
520void wxRichTextFontListBox::UpdateFonts()
521{
522 wxFontEnumerator enumerator;
523 enumerator.EnumerateFacenames();
524 wxArrayString facenames = enumerator.GetFacenames();
525 m_faceNames = facenames;
526 m_faceNames.Sort();
527
528 SetItemCount(m_faceNames.GetCount());
529 Refresh();
530}
531
532#if 0
533// Convert a colour to a 6-digit hex string
534static wxString ColourToHexString(const wxColour& col)
535{
536 wxString hex;
537
538 hex += wxDecToHex(col.Red());
539 hex += wxDecToHex(col.Green());
540 hex += wxDecToHex(col.Blue());
541
542 return hex;
543}
544#endif
545
546/// Creates a suitable HTML fragment for a definition
547wxString wxRichTextFontListBox::CreateHTML(const wxString& facename) const
548{
549 wxString str = wxT("<font");
550
551 str << wxT(" size=\"+2\"");;
552
553 if (!facename.IsEmpty() && facename != _("(none)"))
554 str << wxT(" face=\"") << facename << wxT("\"");
555/*
556 if (def->GetStyle().GetTextColour().Ok())
557 str << wxT(" color=\"#") << ColourToHexString(def->GetStyle().GetTextColour()) << wxT("\"");
558*/
559
560 str << wxT(">");
561
562 bool hasBold = false;
563
564 if (hasBold)
565 str << wxT("<b>");
566
567 str += facename;
568
569 if (hasBold)
570 str << wxT("</b>");
571
572 str << wxT("</font>");
573
574 return str;
575}
576
577#endif
578 // wxUSE_HTML
579
580
581#endif
582 // wxUSE_RICHTEXT