]> git.saurik.com Git - wxWidgets.git/blame - src/richtext/richtextstyledlg.cpp
API change: a single SELECTION_CHANGED not lots of SELECT and UNSELECT events
[wxWidgets.git] / src / richtext / richtextstyledlg.cpp
CommitLineData
dadd4f55 1/////////////////////////////////////////////////////////////////////////////
41a85215
WS
2// Name: src/richtext/richtextstyledlg.cpp
3// Purpose:
dadd4f55 4// Author: Julian Smart
41a85215 5// Modified by:
dadd4f55 6// Created: 10/5/2006 12:05:31 PM
41a85215 7// RCS-ID: $Id$
dadd4f55 8// Copyright: (c) Julian Smart
41a85215 9// Licence: wxWindows licence
dadd4f55
JS
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
5c6e84b6
JS
19#if wxUSE_RICHTEXT
20
dadd4f55
JS
21#ifndef WX_PRECOMP
22#include "wx/wx.h"
23#endif
24
dadd4f55
JS
25#include "wx/richtext/richtextstyledlg.h"
26#include "wx/richtext/richtextformatdlg.h"
27
dadd4f55
JS
28/*!
29 * wxRichTextStyleOrganiserDialog type definition
30 */
31
32bool wxRichTextStyleOrganiserDialog::sm_showToolTips = false;
33
34IMPLEMENT_DYNAMIC_CLASS( wxRichTextStyleOrganiserDialog, wxDialog )
35
36/*!
37 * wxRichTextStyleOrganiserDialog event table definition
38 */
39
40BEGIN_EVENT_TABLE( wxRichTextStyleOrganiserDialog, wxDialog )
41
42 EVT_LISTBOX(wxID_ANY, wxRichTextStyleOrganiserDialog::OnListSelection)
43
44////@begin wxRichTextStyleOrganiserDialog event table entries
45 EVT_BUTTON( ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_CHAR, wxRichTextStyleOrganiserDialog::OnNewCharClick )
46 EVT_UPDATE_UI( ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_CHAR, wxRichTextStyleOrganiserDialog::OnNewCharUpdate )
47
48 EVT_BUTTON( ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_PARA, wxRichTextStyleOrganiserDialog::OnNewParaClick )
49 EVT_UPDATE_UI( ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_PARA, wxRichTextStyleOrganiserDialog::OnNewParaUpdate )
50
51 EVT_BUTTON( ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_LIST, wxRichTextStyleOrganiserDialog::OnNewListClick )
52 EVT_UPDATE_UI( ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_LIST, wxRichTextStyleOrganiserDialog::OnNewListUpdate )
53
54 EVT_BUTTON( ID_RICHTEXTSTYLEORGANISERDIALOG_APPLY, wxRichTextStyleOrganiserDialog::OnApplyClick )
55 EVT_UPDATE_UI( ID_RICHTEXTSTYLEORGANISERDIALOG_APPLY, wxRichTextStyleOrganiserDialog::OnApplyUpdate )
56
57 EVT_BUTTON( ID_RICHTEXTSTYLEORGANISERDIALOG_RENAME, wxRichTextStyleOrganiserDialog::OnRenameClick )
58 EVT_UPDATE_UI( ID_RICHTEXTSTYLEORGANISERDIALOG_RENAME, wxRichTextStyleOrganiserDialog::OnRenameUpdate )
59
60 EVT_BUTTON( ID_RICHTEXTSTYLEORGANISERDIALOG_EDIT, wxRichTextStyleOrganiserDialog::OnEditClick )
61 EVT_UPDATE_UI( ID_RICHTEXTSTYLEORGANISERDIALOG_EDIT, wxRichTextStyleOrganiserDialog::OnEditUpdate )
62
63 EVT_BUTTON( ID_RICHTEXTSTYLEORGANISERDIALOG_DELETE, wxRichTextStyleOrganiserDialog::OnDeleteClick )
64 EVT_UPDATE_UI( ID_RICHTEXTSTYLEORGANISERDIALOG_DELETE, wxRichTextStyleOrganiserDialog::OnDeleteUpdate )
65
66////@end wxRichTextStyleOrganiserDialog event table entries
67
68END_EVENT_TABLE()
69
70/*!
71 * wxRichTextStyleOrganiserDialog constructors
72 */
73
74wxRichTextStyleOrganiserDialog::wxRichTextStyleOrganiserDialog( )
75{
76 Init();
77}
78
79wxRichTextStyleOrganiserDialog::wxRichTextStyleOrganiserDialog( int flags, wxRichTextStyleSheet* sheet, wxRichTextCtrl* ctrl, wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
80{
81 Init();
82 Create(flags, sheet, ctrl, parent, id, caption, pos, size, style);
83}
84
85/*!
86 * Member initialisation for wxSymbolPickerDialog
87 */
88
89void wxRichTextStyleOrganiserDialog::Init()
90{
91 m_richTextStyleSheet = NULL;
92 m_richTextCtrl = NULL;
bcedac44 93 m_dontUpdate = false;
dadd4f55
JS
94 m_flags = 0;
95 m_restartNumbering = true;
41a85215 96
dadd4f55
JS
97////@begin wxRichTextStyleOrganiserDialog member initialisation
98 m_innerSizer = NULL;
99 m_buttonSizerParent = NULL;
100 m_stylesListBox = NULL;
101 m_previewCtrl = NULL;
102 m_buttonSizer = NULL;
103 m_newCharacter = NULL;
104 m_newParagraph = NULL;
105 m_newList = NULL;
106 m_applyStyle = NULL;
107 m_renameStyle = NULL;
108 m_editStyle = NULL;
109 m_deleteStyle = NULL;
110 m_closeButton = NULL;
111 m_bottomButtonSizer = NULL;
112 m_restartNumberingCtrl = NULL;
113 m_okButton = NULL;
114 m_cancelButton = NULL;
115////@end wxRichTextStyleOrganiserDialog member initialisation
116}
117
118/*!
119 * wxRichTextStyleOrganiserDialog creator
120 */
121
122bool wxRichTextStyleOrganiserDialog::Create( int flags, wxRichTextStyleSheet* sheet, wxRichTextCtrl* ctrl, wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
123{
124 m_richTextStyleSheet = sheet;
125 m_richTextCtrl = ctrl;
126 m_flags = flags;
41a85215 127
dadd4f55
JS
128////@begin wxRichTextStyleOrganiserDialog creation
129 SetExtraStyle(GetExtraStyle()|wxWS_EX_BLOCK_EVENTS|wxDIALOG_EX_CONTEXTHELP);
130 wxDialog::Create( parent, id, caption, pos, size, style );
131
132 CreateControls();
133 if (GetSizer())
134 {
135 GetSizer()->SetSizeHints(this);
136 }
137 Centre();
138////@end wxRichTextStyleOrganiserDialog creation
139 return true;
140}
141
142/*!
143 * Control creation for wxRichTextStyleOrganiserDialog
144 */
145
146void wxRichTextStyleOrganiserDialog::CreateControls()
41a85215 147{
dadd4f55
JS
148 bool hideTypeSelector = false;
149 wxRichTextStyleListBox::wxRichTextStyleType typesToShow = wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL;
41a85215 150
dadd4f55
JS
151 if ((m_flags & wxRICHTEXT_ORGANISER_SHOW_CHARACTER) != 0)
152 {
153 typesToShow = wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER;
154 hideTypeSelector = true;
155 }
156 else if ((m_flags & wxRICHTEXT_ORGANISER_SHOW_PARAGRAPH) != 0)
157 {
158 typesToShow = wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH;
159 hideTypeSelector = true;
160 }
161 else if ((m_flags & wxRICHTEXT_ORGANISER_SHOW_LIST) != 0)
162 {
163 typesToShow = wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST;
164 hideTypeSelector = true;
165 }
166 else
167 {
168 // wxRICHTEXT_ORGANISER_SHOW_ALL is implied if the other styles aren't included
169 }
41a85215 170
dadd4f55
JS
171 long listCtrlStyle = 0;
172 if (hideTypeSelector)
173 listCtrlStyle = wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR;
41a85215 174
dadd4f55
JS
175////@begin wxRichTextStyleOrganiserDialog content construction
176 wxRichTextStyleOrganiserDialog* itemDialog1 = this;
177
178 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
179 itemDialog1->SetSizer(itemBoxSizer2);
180
181 m_innerSizer = new wxBoxSizer(wxVERTICAL);
182 itemBoxSizer2->Add(m_innerSizer, 1, wxGROW|wxALL, 5);
183
184 m_buttonSizerParent = new wxBoxSizer(wxHORIZONTAL);
185 m_innerSizer->Add(m_buttonSizerParent, 1, wxGROW, 5);
186
187 wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxVERTICAL);
188 m_buttonSizerParent->Add(itemBoxSizer5, 1, wxGROW, 5);
189
190 wxStaticText* itemStaticText6 = new wxStaticText( itemDialog1, wxID_STATIC, _("&Styles:"), wxDefaultPosition, wxDefaultSize, 0 );
69ce77e2 191 itemBoxSizer5->Add(itemStaticText6, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP, 5);
dadd4f55
JS
192
193 m_stylesListBox = new wxRichTextStyleListCtrl( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_STYLES, wxDefaultPosition, wxSize(200, 350), wxSIMPLE_BORDER|listCtrlStyle );
194 m_stylesListBox->SetHelpText(_("The available styles."));
195 if (ShowToolTips())
196 m_stylesListBox->SetToolTip(_("The available styles."));
197 itemBoxSizer5->Add(m_stylesListBox, 1, wxGROW|wxALL, 5);
198
199 wxBoxSizer* itemBoxSizer8 = new wxBoxSizer(wxVERTICAL);
200 m_buttonSizerParent->Add(itemBoxSizer8, 0, wxGROW, 5);
201
202 wxStaticText* itemStaticText9 = new wxStaticText( itemDialog1, wxID_STATIC, _(" "), wxDefaultPosition, wxDefaultSize, 0 );
69ce77e2 203 itemBoxSizer8->Add(itemStaticText9, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP, 5);
dadd4f55
JS
204
205 m_previewCtrl = new wxRichTextCtrl( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_PREVIEW, wxEmptyString, wxDefaultPosition, wxSize(310, 200), wxSIMPLE_BORDER|wxTE_READONLY );
206 m_previewCtrl->SetHelpText(_("The style preview."));
207 if (ShowToolTips())
208 m_previewCtrl->SetToolTip(_("The style preview."));
209 itemBoxSizer8->Add(m_previewCtrl, 1, wxGROW|wxALL, 5);
210
211 m_buttonSizer = new wxBoxSizer(wxVERTICAL);
212 m_buttonSizerParent->Add(m_buttonSizer, 0, wxGROW, 5);
213
214 wxStaticText* itemStaticText12 = new wxStaticText( itemDialog1, wxID_STATIC, _(" "), wxDefaultPosition, wxDefaultSize, 0 );
69ce77e2 215 m_buttonSizer->Add(itemStaticText12, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP, 5);
dadd4f55
JS
216
217 m_newCharacter = new wxButton( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_CHAR, _("New &Character Style..."), wxDefaultPosition, wxDefaultSize, 0 );
218 m_newCharacter->SetHelpText(_("Click to create a new character style."));
219 if (ShowToolTips())
220 m_newCharacter->SetToolTip(_("Click to create a new character style."));
221 m_buttonSizer->Add(m_newCharacter, 0, wxGROW|wxALL, 5);
222
223 m_newParagraph = new wxButton( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_PARA, _("New &Paragraph Style..."), wxDefaultPosition, wxDefaultSize, 0 );
224 m_newParagraph->SetHelpText(_("Click to create a new paragraph style."));
225 if (ShowToolTips())
226 m_newParagraph->SetToolTip(_("Click to create a new paragraph style."));
227 m_buttonSizer->Add(m_newParagraph, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);
228
229 m_newList = new wxButton( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_LIST, _("New &List Style..."), wxDefaultPosition, wxDefaultSize, 0 );
230 m_newList->SetHelpText(_("Click to create a new list style."));
231 if (ShowToolTips())
232 m_newList->SetToolTip(_("Click to create a new list style."));
233 m_buttonSizer->Add(m_newList, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);
234
235 m_buttonSizer->Add(5, 5, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
236
237 m_applyStyle = new wxButton( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_APPLY, _("&Apply Style"), wxDefaultPosition, wxDefaultSize, 0 );
238 m_applyStyle->SetHelpText(_("Click to apply the selected style."));
239 if (ShowToolTips())
240 m_applyStyle->SetToolTip(_("Click to apply the selected style."));
241 m_buttonSizer->Add(m_applyStyle, 0, wxGROW|wxALL, 5);
242
243 m_renameStyle = new wxButton( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_RENAME, _("&Rename Style..."), wxDefaultPosition, wxDefaultSize, 0 );
244 m_renameStyle->SetHelpText(_("Click to rename the selected style."));
245 if (ShowToolTips())
246 m_renameStyle->SetToolTip(_("Click to rename the selected style."));
247 m_buttonSizer->Add(m_renameStyle, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);
248
249 m_editStyle = new wxButton( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_EDIT, _("&Edit Style..."), wxDefaultPosition, wxDefaultSize, 0 );
250 m_editStyle->SetHelpText(_("Click to edit the selected style."));
251 if (ShowToolTips())
252 m_editStyle->SetToolTip(_("Click to edit the selected style."));
253 m_buttonSizer->Add(m_editStyle, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);
254
255 m_deleteStyle = new wxButton( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_DELETE, _("&Delete Style..."), wxDefaultPosition, wxDefaultSize, 0 );
256 m_deleteStyle->SetHelpText(_("Click to delete the selected style."));
257 if (ShowToolTips())
258 m_deleteStyle->SetToolTip(_("Click to delete the selected style."));
259 m_buttonSizer->Add(m_deleteStyle, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5);
260
261 m_buttonSizer->Add(5, 5, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
262
263 m_closeButton = new wxButton( itemDialog1, wxID_CANCEL, _("Close"), wxDefaultPosition, wxDefaultSize, 0 );
264 m_closeButton->SetDefault();
265 m_closeButton->SetHelpText(_("Click to close this window."));
266 if (ShowToolTips())
267 m_closeButton->SetToolTip(_("Click to close this window."));
268 m_buttonSizer->Add(m_closeButton, 0, wxGROW|wxALL, 5);
269
270 m_bottomButtonSizer = new wxBoxSizer(wxHORIZONTAL);
271 m_innerSizer->Add(m_bottomButtonSizer, 0, wxGROW, 5);
272
273 m_restartNumberingCtrl = new wxCheckBox( itemDialog1, ID_RICHTEXTSTYLEORGANISERDIALOG_RESTART_NUMBERING, _("&Restart numbering"), wxDefaultPosition, wxDefaultSize, 0 );
274 m_restartNumberingCtrl->SetValue(false);
275 m_restartNumberingCtrl->SetHelpText(_("Check to restart numbering."));
276 if (ShowToolTips())
277 m_restartNumberingCtrl->SetToolTip(_("Check to restart numbering."));
278 m_bottomButtonSizer->Add(m_restartNumberingCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
279
280 m_bottomButtonSizer->Add(5, 5, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
281
282 m_okButton = new wxButton( itemDialog1, wxID_OK, _("OK"), wxDefaultPosition, wxDefaultSize, 0 );
283 m_okButton->SetDefault();
284 m_okButton->SetHelpText(_("Click to confirm your selection."));
285 if (ShowToolTips())
286 m_okButton->SetToolTip(_("Click to confirm your selection."));
287 m_bottomButtonSizer->Add(m_okButton, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
288
289 m_cancelButton = new wxButton( itemDialog1, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
290 m_cancelButton->SetHelpText(_("Click to cancel this window."));
291 if (ShowToolTips())
292 m_cancelButton->SetToolTip(_("Click to cancel this window."));
293 m_bottomButtonSizer->Add(m_cancelButton, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
294
295////@end wxRichTextStyleOrganiserDialog content construction
296
297 if ((m_flags & wxRICHTEXT_ORGANISER_CREATE_STYLES) == 0)
298 {
299 m_buttonSizer->Show(m_newCharacter, false);
300 m_buttonSizer->Show(m_newParagraph, false);
301 m_buttonSizer->Show(m_newList, false);
302 }
303 if ((m_flags & wxRICHTEXT_ORGANISER_DELETE_STYLES) == 0)
304 {
305 m_buttonSizer->Show(m_deleteStyle, false);
306 }
307 if ((m_flags & wxRICHTEXT_ORGANISER_APPLY_STYLES) == 0)
308 {
309 m_buttonSizer->Show(m_applyStyle, false);
310 }
311 if ((m_flags & wxRICHTEXT_ORGANISER_EDIT_STYLES) == 0)
312 {
313 m_buttonSizer->Show(m_editStyle, false);
314 }
315 if ((m_flags & wxRICHTEXT_ORGANISER_RENAME_STYLES) == 0)
316 {
317 m_buttonSizer->Show(m_renameStyle, false);
318 }
319 if ((m_flags & wxRICHTEXT_ORGANISER_RENUMBER) == 0)
320 {
321 m_bottomButtonSizer->Show(m_restartNumberingCtrl, false);
322 }
323
324 if ((m_flags & wxRICHTEXT_ORGANISER_OK_CANCEL) == 0)
325 {
326 m_bottomButtonSizer->Show(m_okButton, false);
327 m_bottomButtonSizer->Show(m_cancelButton, false);
328 }
329 else
330 {
331 m_buttonSizer->Show(m_closeButton, false);
332 }
333
334 // No buttons in the vertical group are shown, so hide the whole sizer
335 if ((m_flags & wxRICHTEXT_ORGANISER_ORGANISE) == 0)
336 {
337 m_buttonSizerParent->Show(m_buttonSizer, false);
338 }
41a85215 339
dadd4f55
JS
340 // No buttons in the horizontal group are shown, so hide the whole sizer
341 if ((m_flags & (wxRICHTEXT_ORGANISER_OK_CANCEL|wxRICHTEXT_ORGANISER_RENUMBER)) == 0)
342 {
343 m_innerSizer->Show(m_bottomButtonSizer, false);
344 }
41a85215 345
dadd4f55
JS
346 if (hideTypeSelector)
347 {
348 m_stylesListBox->GetStyleListBox()->SetStyleType(typesToShow);
349 }
41a85215 350
dadd4f55
JS
351 m_stylesListBox->SetStyleSheet(m_richTextStyleSheet);
352 m_stylesListBox->SetRichTextCtrl(m_richTextCtrl);
353 m_stylesListBox->UpdateStyles();
354 m_stylesListBox->GetStyleListBox()->SetAutoSetSelection(false); // stop idle-time auto selection
355 if (m_stylesListBox->GetStyleListBox()->GetItemCount() > 0)
356 {
357 m_stylesListBox->GetStyleListBox()->SetSelection(0);
358 ShowPreview();
359 }
360}
361
362/*!
363 * Should we show tooltips?
364 */
365
366/// Get selected style name or definition
367wxString wxRichTextStyleOrganiserDialog::GetSelectedStyle() const
368{
369 wxRichTextStyleDefinition* def = GetSelectedStyleDefinition();
370 if (def)
371 return def->GetName();
372 else
373 return wxEmptyString;
374}
375
376wxRichTextStyleDefinition* wxRichTextStyleOrganiserDialog::GetSelectedStyleDefinition() const
377{
378 int sel = m_stylesListBox->GetStyleListBox()->GetSelection();
379 return m_stylesListBox->GetStyleListBox()->GetStyle(sel);
380}
381
382
383/// Transfer data from/to window
384bool wxRichTextStyleOrganiserDialog::TransferDataFromWindow()
385{
386 if (!wxDialog::TransferDataFromWindow())
387 return false;
41a85215 388
dadd4f55 389 m_restartNumbering = m_restartNumberingCtrl->GetValue();
41a85215 390
dadd4f55
JS
391 return true;
392}
393
394bool wxRichTextStyleOrganiserDialog::TransferDataToWindow()
395{
396 if (!wxDialog::TransferDataToWindow())
397 return false;
41a85215 398
dadd4f55 399 m_restartNumberingCtrl->SetValue(m_restartNumbering);
41a85215 400
dadd4f55
JS
401 return true;
402}
403
404/// Show preview for given or selected preview
405void wxRichTextStyleOrganiserDialog::ShowPreview(int sel)
406{
407 static const wxChar* s_para1 = wxT("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. \
408Nullam ante sapien, vestibulum nonummy, pulvinar sed, luctus ut, lacus.");
409
410 static const wxChar* s_para2List = wxT("Duis pharetra consequat dui. Nullam vitae justo id mauris lobortis interdum.");
411
412 static const wxChar* s_para2 = wxT("Duis pharetra consequat dui. Cum sociis natoque penatibus \
413et magnis dis parturient montes, nascetur ridiculus mus. Nullam vitae justo id mauris lobortis interdum.");
414
415 static const wxChar* s_para3 = wxT("\nInteger convallis dolor at augue \
416iaculis malesuada. Donec bibendum ipsum ut ante porta fringilla.\n");
417
418 if (sel == -1)
419 {
420 sel = m_stylesListBox->GetStyleListBox()->GetSelection();
421 if (sel == -1)
41a85215 422 return;
dadd4f55
JS
423 }
424
425 wxRichTextStyleDefinition* def = m_stylesListBox->GetStyleListBox()->GetStyle(sel);
41a85215 426
dadd4f55 427 wxRichTextListStyleDefinition* listDef = wxDynamicCast(def, wxRichTextListStyleDefinition);
336d8ae9
VZ
428
429 wxTextAttrEx attr(def->GetStyleMergedWithBase(GetStyleSheet()));
dadd4f55
JS
430
431 wxFont font(m_previewCtrl->GetFont());
432 font.SetPointSize(9);
433 m_previewCtrl->SetFont(font);
434
435 wxTextAttrEx normalParaAttr;
436 normalParaAttr.SetFont(font);
437 normalParaAttr.SetTextColour(wxColour(wxT("LIGHT GREY")));
438
439 m_previewCtrl->Freeze();
41a85215 440 m_previewCtrl->Clear();
dadd4f55
JS
441
442 m_previewCtrl->BeginStyle(normalParaAttr);
443 m_previewCtrl->WriteText(s_para1);
444 m_previewCtrl->EndStyle();
41a85215 445
dadd4f55 446 m_previewCtrl->BeginStyle(attr);
41a85215 447
dadd4f55
JS
448 if (listDef)
449 {
d2d0adc7 450 long listStart = m_previewCtrl->GetInsertionPoint() + 1;
dadd4f55
JS
451 int i;
452 for (i = 0; i < 10; i++)
453 {
454 wxTextAttrEx levelAttr = * listDef->GetLevelAttributes(i);
455 levelAttr.SetBulletNumber(1);
456 m_previewCtrl->BeginStyle(levelAttr);
457 m_previewCtrl->WriteText(wxString::Format(wxT("\nList level %d. "), i+1) + s_para2List);
458 m_previewCtrl->EndStyle();
459 }
d2d0adc7
JS
460 long listEnd = m_previewCtrl->GetInsertionPoint();
461 m_previewCtrl->NumberList(wxRichTextRange(listStart, listEnd), listDef);
dadd4f55
JS
462 }
463 else
464 {
465 m_previewCtrl->WriteText(wxString(wxT("\n")) + s_para2);
466 }
41a85215 467
dadd4f55
JS
468 m_previewCtrl->EndStyle();
469
470 m_previewCtrl->BeginStyle(normalParaAttr);
471 m_previewCtrl->WriteText(s_para3);
472 m_previewCtrl->EndStyle();
473
474 m_previewCtrl->Thaw();
475}
41a85215 476
dadd4f55
JS
477/// Clears the preview
478void wxRichTextStyleOrganiserDialog::ClearPreview()
479{
5bbdfd5b 480 m_previewCtrl->Clear();
dadd4f55
JS
481}
482
483bool wxRichTextStyleOrganiserDialog::ApplyStyle(wxRichTextCtrl* ctrl)
484{
485 int sel = m_stylesListBox->GetStyleListBox()->GetSelection();
486 if (sel == -1)
487 return false;
488 if (!ctrl)
489 ctrl = GetRichTextCtrl();
490 if (!ctrl)
491 return false;
492 if (!ctrl->HasSelection())
493 return false;
dadd4f55 494
41a85215
WS
495
496 wxRichTextStyleDefinition* def = m_stylesListBox->GetStyleListBox()->GetStyle(sel);
dadd4f55
JS
497 wxRichTextListStyleDefinition* listDef = wxDynamicCast(def, wxRichTextListStyleDefinition);
498
499 if (listDef && m_restartNumberingCtrl->GetValue())
500 {
501 wxRichTextRange range = ctrl->GetSelectionRange();
502 return ctrl->SetListStyle(range, listDef, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_RENUMBER);
503 }
504 else
505 {
506 return ctrl->ApplyStyle(def);
507 }
508}
509
510/*!
511 * Get bitmap resources
512 */
513
514wxBitmap wxRichTextStyleOrganiserDialog::GetBitmapResource( const wxString& name )
515{
516 // Bitmap retrieval
517////@begin wxRichTextStyleOrganiserDialog bitmap retrieval
518 wxUnusedVar(name);
519 return wxNullBitmap;
520////@end wxRichTextStyleOrganiserDialog bitmap retrieval
521}
522
523/*!
524 * Get icon resources
525 */
526
527wxIcon wxRichTextStyleOrganiserDialog::GetIconResource( const wxString& name )
528{
529 // Icon retrieval
530////@begin wxRichTextStyleOrganiserDialog icon retrieval
531 wxUnusedVar(name);
532 return wxNullIcon;
533////@end wxRichTextStyleOrganiserDialog icon retrieval
534}
535
536/*!
537 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_APPLY
538 */
539
540void wxRichTextStyleOrganiserDialog::OnApplyClick( wxCommandEvent& WXUNUSED(event) )
541{
542 ApplyStyle();
543}
544
545/*!
546 * wxEVT_UPDATE_UI event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_APPLY
547 */
548
549void wxRichTextStyleOrganiserDialog::OnApplyUpdate( wxUpdateUIEvent& event )
550{
551 event.Enable(((GetFlags() & wxRICHTEXT_ORGANISER_APPLY_STYLES) != 0) && m_stylesListBox->GetStyleListBox()->GetSelection() != wxNOT_FOUND);
552}
553
554
555/*!
556 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_CHAR
557 */
558
559void wxRichTextStyleOrganiserDialog::OnNewCharClick( wxCommandEvent& WXUNUSED(event) )
560{
561 wxString styleName = wxGetTextFromUser(_("Enter a character style name"), _("New Style"));
562 if (!styleName.IsEmpty())
563 {
564 if (GetStyleSheet()->FindCharacterStyle(styleName))
565 {
566 wxMessageBox(_("Sorry, that name is taken. Please choose another."), _("New Style"), wxICON_EXCLAMATION|wxOK, this);
567 return;
568 }
41a85215 569
dadd4f55 570 wxRichTextCharacterStyleDefinition* style = new wxRichTextCharacterStyleDefinition(styleName);
41a85215 571
dadd4f55
JS
572 int pages = wxRICHTEXT_FORMAT_FONT|wxRICHTEXT_FORMAT_STYLE_EDITOR;
573 wxRichTextFormattingDialog formatDlg;
574 formatDlg.SetStyleDefinition(*style, GetStyleSheet());
575 formatDlg.Create(pages, this);
576
577 if (formatDlg.ShowModal() == wxID_OK)
578 {
579 wxRichTextCharacterStyleDefinition* charDef = wxDynamicCast(formatDlg.GetStyleDefinition(), wxRichTextCharacterStyleDefinition);
41a85215 580
dadd4f55 581 (*((wxRichTextCharacterStyleDefinition* ) style)) = (*charDef);
41a85215 582
dadd4f55
JS
583 GetStyleSheet()->AddCharacterStyle(style);
584
585 m_stylesListBox->UpdateStyles();
586 ShowPreview();
587 }
588 else
589 delete style;
41a85215 590 }
dadd4f55
JS
591}
592
593/*!
594 * wxEVT_UPDATE_UI event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_CHAR
595 */
596
597void wxRichTextStyleOrganiserDialog::OnNewCharUpdate( wxUpdateUIEvent& event )
598{
599 event.Enable((GetFlags() & wxRICHTEXT_ORGANISER_CREATE_STYLES) != 0);
600}
601
602
603/*!
604 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_PARA
605 */
606
607void wxRichTextStyleOrganiserDialog::OnNewParaClick( wxCommandEvent& WXUNUSED(event) )
608{
609 wxString styleName = wxGetTextFromUser(_("Enter a paragraph style name"), _("New Style"));
610 if (!styleName.IsEmpty())
611 {
612 if (GetStyleSheet()->FindParagraphStyle(styleName))
613 {
614 wxMessageBox(_("Sorry, that name is taken. Please choose another."), _("New Style"), wxICON_EXCLAMATION|wxOK, this);
615 return;
616 }
41a85215 617
dadd4f55 618 wxRichTextParagraphStyleDefinition* style = new wxRichTextParagraphStyleDefinition(styleName);
41a85215 619
dadd4f55
JS
620 int pages = wxRICHTEXT_FORMAT_STYLE_EDITOR|wxRICHTEXT_FORMAT_FONT|wxRICHTEXT_FORMAT_INDENTS_SPACING|wxRICHTEXT_FORMAT_TABS|wxRICHTEXT_FORMAT_BULLETS;
621 wxRichTextFormattingDialog formatDlg;
622 formatDlg.SetStyleDefinition(*style, GetStyleSheet());
623 formatDlg.Create(pages, this);
624
625 if (formatDlg.ShowModal() == wxID_OK)
626 {
627 wxRichTextParagraphStyleDefinition* paraDef = wxDynamicCast(formatDlg.GetStyleDefinition(), wxRichTextParagraphStyleDefinition);
41a85215 628
dadd4f55 629 (*((wxRichTextParagraphStyleDefinition* ) style)) = (*paraDef);
41a85215 630
dadd4f55
JS
631 GetStyleSheet()->AddParagraphStyle(style);
632
633 m_stylesListBox->UpdateStyles();
634 ShowPreview();
635 }
636 else
637 delete style;
41a85215 638 }
dadd4f55
JS
639}
640
641/*!
642 * wxEVT_UPDATE_UI event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_PARA
643 */
644
645void wxRichTextStyleOrganiserDialog::OnNewParaUpdate( wxUpdateUIEvent& event )
646{
647 event.Enable((GetFlags() & wxRICHTEXT_ORGANISER_CREATE_STYLES) != 0);
648}
649
650
651/*!
652 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_EDIT
653 */
654
655void wxRichTextStyleOrganiserDialog::OnEditClick( wxCommandEvent& WXUNUSED(event) )
656{
657 int sel = m_stylesListBox->GetStyleListBox()->GetSelection();
658 if (sel != wxNOT_FOUND)
659 {
660 wxRichTextStyleDefinition* def = m_stylesListBox->GetStyleListBox()->GetStyle(sel);
41a85215 661
dadd4f55 662 int pages = wxRICHTEXT_FORMAT_STYLE_EDITOR;
41a85215 663
dadd4f55
JS
664 if (def->IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition)))
665 {
666 pages |= wxRICHTEXT_FORMAT_FONT;
667 }
668 else if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
669 {
670 pages |= wxRICHTEXT_FORMAT_LIST_STYLE|wxRICHTEXT_FORMAT_FONT|wxRICHTEXT_FORMAT_INDENTS_SPACING;
671 }
672 else if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
673 {
674 pages |= wxRICHTEXT_FORMAT_FONT|wxRICHTEXT_FORMAT_INDENTS_SPACING|wxRICHTEXT_FORMAT_TABS|wxRICHTEXT_FORMAT_BULLETS;
675 }
41a85215 676
dadd4f55
JS
677 wxRichTextFormattingDialog formatDlg;
678 formatDlg.SetStyleDefinition(*def, GetStyleSheet());
679 formatDlg.Create(pages, this);
680
681 if (formatDlg.ShowModal() == wxID_OK)
682 {
683 wxRichTextParagraphStyleDefinition* paraDef = wxDynamicCast(formatDlg.GetStyleDefinition(), wxRichTextParagraphStyleDefinition);
684 wxRichTextCharacterStyleDefinition* charDef = wxDynamicCast(formatDlg.GetStyleDefinition(), wxRichTextCharacterStyleDefinition);
685 wxRichTextListStyleDefinition* listDef = wxDynamicCast(formatDlg.GetStyleDefinition(), wxRichTextListStyleDefinition);
41a85215 686
dadd4f55
JS
687 if (listDef)
688 {
689 (*((wxRichTextListStyleDefinition* ) def)) = (*listDef);
690 }
691 else if (paraDef)
692 {
693 (*((wxRichTextParagraphStyleDefinition* ) def)) = (*paraDef);
694 }
695 else
696 {
697 (*((wxRichTextCharacterStyleDefinition* ) def)) = (*charDef);
698 }
699
700 m_stylesListBox->UpdateStyles();
701 ShowPreview();
702 }
703 }
704}
705
706/*!
707 * wxEVT_UPDATE_UI event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_EDIT
708 */
709
710void wxRichTextStyleOrganiserDialog::OnEditUpdate( wxUpdateUIEvent& event )
711{
712 event.Enable(((GetFlags() & wxRICHTEXT_ORGANISER_EDIT_STYLES) != 0) && m_stylesListBox->GetStyleListBox()->GetSelection() != wxNOT_FOUND);
713}
714
715
716/*!
717 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_DELETE
718 */
719
720void wxRichTextStyleOrganiserDialog::OnDeleteClick( wxCommandEvent& WXUNUSED(event) )
721{
722 int sel = m_stylesListBox->GetStyleListBox()->GetSelection();
723 if (sel != wxNOT_FOUND)
724 {
725 wxRichTextStyleDefinition* def = m_stylesListBox->GetStyleListBox()->GetStyle(sel);
726 wxString name(def->GetName());
86501081 727 if (wxYES == wxMessageBox(wxString::Format(_("Delete style %s?"), name), _("Delete Style"), wxYES_NO|wxICON_QUESTION, this))
dadd4f55
JS
728 {
729 m_stylesListBox->GetStyleListBox()->SetItemCount(0);
41a85215 730
dadd4f55
JS
731 if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition)))
732 GetStyleSheet()->RemoveListStyle((wxRichTextListStyleDefinition*) def, true);
733 else if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition)))
734 GetStyleSheet()->RemoveParagraphStyle((wxRichTextParagraphStyleDefinition*) def, true);
735 else if (def->IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition)))
736 GetStyleSheet()->RemoveCharacterStyle((wxRichTextCharacterStyleDefinition*) def, true);
41a85215 737
dadd4f55
JS
738 m_stylesListBox->UpdateStyles();
739
740 if (m_stylesListBox->GetStyleListBox()->GetSelection() != -1)
741 ShowPreview();
742 else
743 ClearPreview();
744 }
745 }
746}
747
748/*!
749 * wxEVT_UPDATE_UI event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_DELETE
750 */
751
752void wxRichTextStyleOrganiserDialog::OnDeleteUpdate( wxUpdateUIEvent& event )
753{
754 event.Enable(((GetFlags() & wxRICHTEXT_ORGANISER_DELETE_STYLES) != 0) && m_stylesListBox->GetStyleListBox()->GetSelection() != wxNOT_FOUND);
755}
756
757/*!
758 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_LIST
759 */
760
761void wxRichTextStyleOrganiserDialog::OnNewListClick( wxCommandEvent& WXUNUSED(event) )
762{
763 wxString styleName = wxGetTextFromUser(_("Enter a list style name"), _("New Style"));
764 if (!styleName.IsEmpty())
765 {
766 if (GetStyleSheet()->FindListStyle(styleName))
767 {
768 wxMessageBox(_("Sorry, that name is taken. Please choose another."), _("New Style"), wxICON_EXCLAMATION|wxOK, this);
769 return;
770 }
41a85215 771
dadd4f55 772 wxRichTextListStyleDefinition* style = new wxRichTextListStyleDefinition(styleName);
41a85215 773
dadd4f55
JS
774 // Initialize the style to make it easier to edit
775 int i;
776 for (i = 0; i < 10; i++)
777 {
778 wxString bulletSymbol;
779 if (i == 0)
780 bulletSymbol = wxT("*");
781 else if (i == 1)
782 bulletSymbol = wxT("-");
783 else if (i == 2)
784 bulletSymbol = wxT("*");
785 else if (i == 3)
786 bulletSymbol = wxT("-");
787 else
788 bulletSymbol = wxT("*");
789
790 style->SetAttributes(i, (i+1)*60, 60, wxTEXT_ATTR_BULLET_STYLE_SYMBOL, bulletSymbol);
791 }
41a85215 792
dadd4f55
JS
793 int pages = wxRICHTEXT_FORMAT_LIST_STYLE|wxRICHTEXT_FORMAT_STYLE_EDITOR|wxRICHTEXT_FORMAT_FONT|wxRICHTEXT_FORMAT_INDENTS_SPACING;
794 wxRichTextFormattingDialog formatDlg;
795 formatDlg.SetStyleDefinition(*style, GetStyleSheet());
796 formatDlg.Create(pages, this);
797
798 if (formatDlg.ShowModal() == wxID_OK)
799 {
800 wxRichTextListStyleDefinition* listDef = wxDynamicCast(formatDlg.GetStyleDefinition(), wxRichTextListStyleDefinition);
41a85215 801
dadd4f55 802 (*((wxRichTextListStyleDefinition* ) style)) = (*listDef);
41a85215 803
dadd4f55
JS
804 GetStyleSheet()->AddListStyle(style);
805
806 m_stylesListBox->UpdateStyles();
807 ShowPreview();
808 }
809 else
810 delete style;
41a85215 811 }
dadd4f55
JS
812}
813
814/*!
815 * wxEVT_UPDATE_UI event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_NEW_LIST
816 */
817
818void wxRichTextStyleOrganiserDialog::OnNewListUpdate( wxUpdateUIEvent& event )
819{
820 event.Enable((GetFlags() & wxRICHTEXT_ORGANISER_CREATE_STYLES) != 0);
821}
822
823/*!
824 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_RENAME
825 */
826
827void wxRichTextStyleOrganiserDialog::OnRenameClick( wxCommandEvent& WXUNUSED(event) )
828{
5bbdfd5b
JS
829 int sel = m_stylesListBox->GetStyleListBox()->GetSelection();
830 if (sel == -1)
831 return;
832 wxRichTextStyleDefinition* def = m_stylesListBox->GetStyleListBox()->GetStyle(sel);
833 if (!def)
834 return;
835
836 wxString styleName = wxGetTextFromUser(_("Enter a new style name"), _("New Style"), def->GetName());
837 if (!styleName.IsEmpty())
838 {
839 if (styleName == def->GetName())
840 return;
841
842 if (GetStyleSheet()->FindParagraphStyle(styleName) || GetStyleSheet()->FindCharacterStyle(styleName) || GetStyleSheet()->FindListStyle(styleName))
843 {
844 wxMessageBox(_("Sorry, that name is taken. Please choose another."), _("New Style"), wxICON_EXCLAMATION|wxOK, this);
845 return;
846 }
847
848 def->SetName(styleName);
849 m_stylesListBox->UpdateStyles();
850 }
dadd4f55
JS
851}
852
853/*!
854 * wxEVT_UPDATE_UI event handler for ID_RICHTEXTSTYLEORGANISERDIALOG_RENAME
855 */
856
857void wxRichTextStyleOrganiserDialog::OnRenameUpdate( wxUpdateUIEvent& event )
858{
859 event.Enable(((GetFlags() & wxRICHTEXT_ORGANISER_RENAME_STYLES) != 0) && m_stylesListBox->GetStyleListBox()->GetSelection() != wxNOT_FOUND);
860}
861
862/// List selection
863void wxRichTextStyleOrganiserDialog::OnListSelection(wxCommandEvent& event)
864{
865 if (event.GetEventObject() == m_stylesListBox->GetStyleListBox())
866 ShowPreview();
867 else
868 event.Skip();
869}
5c6e84b6
JS
870
871#endif
872 // wxUSE_RICHTEXT
873