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