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