]>
Commit | Line | Data |
---|---|---|
5d7836c4 | 1 | ///////////////////////////////////////////////////////////////////////////// |
61399247 | 2 | // Name: src/richtext/richtextstyles.cpp |
5d7836c4 JS |
3 | // Purpose: Style management for wxRichTextCtrl |
4 | // Author: Julian Smart | |
61399247 | 5 | // Modified by: |
5d7836c4 | 6 | // Created: 2005-09-30 |
61399247 | 7 | // RCS-ID: $Id$ |
5d7836c4 JS |
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__ | |
fe5aa22c | 16 | #pragma hdrstop |
5d7836c4 JS |
17 | #endif |
18 | ||
b01ca8b6 JS |
19 | #if wxUSE_RICHTEXT |
20 | ||
21 | #include "wx/richtext/richtextstyles.h" | |
22 | ||
5d7836c4 | 23 | #ifndef WX_PRECOMP |
fe5aa22c | 24 | #include "wx/wx.h" |
5d7836c4 JS |
25 | #endif |
26 | ||
5d7836c4 JS |
27 | #include "wx/filename.h" |
28 | #include "wx/clipbrd.h" | |
29 | #include "wx/wfstream.h" | |
38f833b1 | 30 | #include "wx/settings.h" |
5d7836c4 | 31 | |
5d7836c4 JS |
32 | #include "wx/richtext/richtextctrl.h" |
33 | ||
34 | IMPLEMENT_CLASS(wxRichTextStyleDefinition, wxObject) | |
35 | IMPLEMENT_CLASS(wxRichTextCharacterStyleDefinition, wxRichTextStyleDefinition) | |
36 | IMPLEMENT_CLASS(wxRichTextParagraphStyleDefinition, wxRichTextStyleDefinition) | |
38f833b1 | 37 | IMPLEMENT_CLASS(wxRichTextListStyleDefinition, wxRichTextParagraphStyleDefinition) |
603f702b | 38 | IMPLEMENT_CLASS(wxRichTextBoxStyleDefinition, wxRichTextStyleDefinition) |
5d7836c4 | 39 | |
fe5aa22c JS |
40 | /*! |
41 | * A definition | |
42 | */ | |
43 | ||
44 | void wxRichTextStyleDefinition::Copy(const wxRichTextStyleDefinition& def) | |
45 | { | |
46 | m_name = def.m_name; | |
47 | m_baseStyle = def.m_baseStyle; | |
48 | m_style = def.m_style; | |
42688aea | 49 | m_description = def.m_description; |
c6182d48 | 50 | m_properties = def.m_properties; |
fe5aa22c JS |
51 | } |
52 | ||
53 | bool wxRichTextStyleDefinition::Eq(const wxRichTextStyleDefinition& def) const | |
54 | { | |
c6182d48 | 55 | return (m_name == def.m_name && m_baseStyle == def.m_baseStyle && m_style == def.m_style && m_properties == def.m_properties); |
fe5aa22c JS |
56 | } |
57 | ||
336d8ae9 | 58 | /// Gets the style combined with the base style |
24777478 | 59 | wxRichTextAttr wxRichTextStyleDefinition::GetStyleMergedWithBase(const wxRichTextStyleSheet* sheet) const |
336d8ae9 | 60 | { |
519884a0 JS |
61 | if (m_baseStyle.IsEmpty()) |
62 | return m_style; | |
ce00f59b | 63 | |
80a46597 VZ |
64 | bool isParaStyle = IsKindOf(wxCLASSINFO(wxRichTextParagraphStyleDefinition)); |
65 | bool isCharStyle = IsKindOf(wxCLASSINFO(wxRichTextCharacterStyleDefinition)); | |
66 | bool isListStyle = IsKindOf(wxCLASSINFO(wxRichTextListStyleDefinition)); | |
67 | bool isBoxStyle = IsKindOf(wxCLASSINFO(wxRichTextBoxStyleDefinition)); | |
6d531430 | 68 | |
519884a0 JS |
69 | // Collect the styles, detecting loops |
70 | wxArrayString styleNames; | |
71 | wxList styles; | |
72 | const wxRichTextStyleDefinition* def = this; | |
73 | while (def) | |
336d8ae9 | 74 | { |
519884a0 JS |
75 | styles.Insert((wxObject*) def); |
76 | styleNames.Add(def->GetName()); | |
ce00f59b | 77 | |
519884a0 JS |
78 | wxString baseStyleName = def->GetBaseStyle(); |
79 | if (!baseStyleName.IsEmpty() && styleNames.Index(baseStyleName) == wxNOT_FOUND) | |
6d531430 JS |
80 | { |
81 | if (isParaStyle) | |
82 | def = sheet->FindParagraphStyle(baseStyleName); | |
83 | else if (isCharStyle) | |
84 | def = sheet->FindCharacterStyle(baseStyleName); | |
85 | else if (isListStyle) | |
86 | def = sheet->FindListStyle(baseStyleName); | |
87 | else if (isBoxStyle) | |
88 | def = sheet->FindBoxStyle(baseStyleName); | |
89 | else | |
90 | def = sheet->FindStyle(baseStyleName); | |
91 | } | |
519884a0 JS |
92 | else |
93 | def = NULL; | |
336d8ae9 | 94 | } |
ce00f59b | 95 | |
519884a0 JS |
96 | wxRichTextAttr attr; |
97 | wxList::compatibility_iterator node = styles.GetFirst(); | |
98 | while (node) | |
99 | { | |
100 | wxRichTextStyleDefinition* def = (wxRichTextStyleDefinition*) node->GetData(); | |
101 | attr.Apply(def->GetStyle(), NULL); | |
102 | node = node->GetNext(); | |
103 | } | |
ce00f59b | 104 | |
519884a0 | 105 | return attr; |
336d8ae9 VZ |
106 | } |
107 | ||
fe5aa22c JS |
108 | /*! |
109 | * Paragraph style definition | |
110 | */ | |
111 | ||
112 | void wxRichTextParagraphStyleDefinition::Copy(const wxRichTextParagraphStyleDefinition& def) | |
113 | { | |
114 | wxRichTextStyleDefinition::Copy(def); | |
115 | ||
116 | m_nextStyle = def.m_nextStyle; | |
117 | } | |
118 | ||
119 | bool wxRichTextParagraphStyleDefinition::operator ==(const wxRichTextParagraphStyleDefinition& def) const | |
120 | { | |
121 | return (Eq(def) && m_nextStyle == def.m_nextStyle); | |
122 | } | |
123 | ||
603f702b JS |
124 | /*! |
125 | * Box style definition | |
126 | */ | |
127 | ||
128 | void wxRichTextBoxStyleDefinition::Copy(const wxRichTextBoxStyleDefinition& def) | |
129 | { | |
130 | wxRichTextStyleDefinition::Copy(def); | |
131 | } | |
132 | ||
133 | bool wxRichTextBoxStyleDefinition::operator ==(const wxRichTextBoxStyleDefinition& def) const | |
134 | { | |
135 | return (Eq(def)); | |
136 | } | |
137 | ||
38f833b1 JS |
138 | /*! |
139 | * List style definition | |
140 | */ | |
141 | ||
142 | void wxRichTextListStyleDefinition::Copy(const wxRichTextListStyleDefinition& def) | |
143 | { | |
144 | wxRichTextParagraphStyleDefinition::Copy(def); | |
41a85215 | 145 | |
38f833b1 JS |
146 | int i; |
147 | for (i = 0; i < 10; i++) | |
148 | m_levelStyles[i] = def.m_levelStyles[i]; | |
149 | } | |
150 | ||
151 | bool wxRichTextListStyleDefinition::operator ==(const wxRichTextListStyleDefinition& def) const | |
152 | { | |
153 | if (!Eq(def)) | |
154 | return false; | |
155 | int i; | |
156 | for (i = 0; i < 10; i++) | |
157 | if (!(m_levelStyles[i] == def.m_levelStyles[i])) | |
158 | return false; | |
41a85215 | 159 | |
38f833b1 JS |
160 | return true; |
161 | } | |
162 | ||
163 | /// Sets/gets the attributes for the given level | |
24777478 | 164 | void wxRichTextListStyleDefinition::SetLevelAttributes(int i, const wxRichTextAttr& attr) |
38f833b1 JS |
165 | { |
166 | wxASSERT( (i >= 0 && i < 10) ); | |
167 | if (i >= 0 && i < 10) | |
168 | m_levelStyles[i] = attr; | |
169 | } | |
170 | ||
24777478 | 171 | const wxRichTextAttr* wxRichTextListStyleDefinition::GetLevelAttributes(int i) const |
38f833b1 JS |
172 | { |
173 | wxASSERT( (i >= 0 && i < 10) ); | |
174 | if (i >= 0 && i < 10) | |
175 | return & m_levelStyles[i]; | |
176 | else | |
177 | return NULL; | |
178 | } | |
179 | ||
24777478 | 180 | wxRichTextAttr* wxRichTextListStyleDefinition::GetLevelAttributes(int i) |
38f833b1 JS |
181 | { |
182 | wxASSERT( (i >= 0 && i < 10) ); | |
183 | if (i >= 0 && i < 10) | |
184 | return & m_levelStyles[i]; | |
185 | else | |
186 | return NULL; | |
187 | } | |
188 | ||
189 | /// Convenience function for setting the major attributes for a list level specification | |
190 | void wxRichTextListStyleDefinition::SetAttributes(int i, int leftIndent, int leftSubIndent, int bulletStyle, const wxString& bulletSymbol) | |
191 | { | |
192 | wxASSERT( (i >= 0 && i < 10) ); | |
193 | if (i >= 0 && i < 10) | |
194 | { | |
24777478 | 195 | wxRichTextAttr attr; |
41a85215 | 196 | |
38f833b1 JS |
197 | attr.SetBulletStyle(bulletStyle); |
198 | attr.SetLeftIndent(leftIndent, leftSubIndent); | |
199 | ||
200 | if (!bulletSymbol.IsEmpty()) | |
d2d0adc7 JS |
201 | { |
202 | if (bulletStyle & wxTEXT_ATTR_BULLET_STYLE_SYMBOL) | |
203 | attr.SetBulletText(bulletSymbol); | |
204 | else | |
205 | attr.SetBulletName(bulletSymbol); | |
206 | } | |
41a85215 WS |
207 | |
208 | m_levelStyles[i] = attr; | |
209 | } | |
38f833b1 JS |
210 | } |
211 | ||
212 | /// Finds the level corresponding to the given indentation | |
213 | int wxRichTextListStyleDefinition::FindLevelForIndent(int indent) const | |
214 | { | |
215 | int i; | |
216 | for (i = 0; i < 10; i++) | |
217 | { | |
218 | if (indent < m_levelStyles[i].GetLeftIndent()) | |
219 | { | |
220 | if (i > 0) | |
221 | return i - 1; | |
222 | else | |
223 | return 0; | |
224 | } | |
225 | } | |
226 | return 9; | |
227 | } | |
228 | ||
229 | /// Combine the list style with a paragraph style, using the given indent (from which | |
230 | /// an appropriate level is found) | |
24777478 | 231 | wxRichTextAttr wxRichTextListStyleDefinition::CombineWithParagraphStyle(int indent, const wxRichTextAttr& paraStyle, wxRichTextStyleSheet* styleSheet) |
38f833b1 JS |
232 | { |
233 | int listLevel = FindLevelForIndent(indent); | |
41a85215 | 234 | |
24777478 | 235 | wxRichTextAttr attr(*GetLevelAttributes(listLevel)); |
38f833b1 JS |
236 | int oldLeftIndent = attr.GetLeftIndent(); |
237 | int oldLeftSubIndent = attr.GetLeftSubIndent(); | |
238 | ||
41a85215 | 239 | // First apply the overall paragraph style, if any |
336d8ae9 VZ |
240 | if (styleSheet) |
241 | attr.Apply(GetStyleMergedWithBase(styleSheet)); | |
242 | else | |
243 | attr.Apply(GetStyle()); | |
38f833b1 JS |
244 | |
245 | // Then apply paragraph style, e.g. from paragraph style definition | |
336d8ae9 | 246 | attr.Apply(paraStyle); |
41a85215 | 247 | |
38f833b1 JS |
248 | // We override the indents according to the list definition |
249 | attr.SetLeftIndent(oldLeftIndent, oldLeftSubIndent); | |
41a85215 | 250 | |
38f833b1 JS |
251 | return attr; |
252 | } | |
253 | ||
254 | /// Combine the base and list style, using the given indent (from which | |
255 | /// an appropriate level is found) | |
24777478 | 256 | wxRichTextAttr wxRichTextListStyleDefinition::GetCombinedStyle(int indent, wxRichTextStyleSheet* styleSheet) |
38f833b1 JS |
257 | { |
258 | int listLevel = FindLevelForIndent(indent); | |
336d8ae9 | 259 | return GetCombinedStyleForLevel(listLevel, styleSheet); |
38f833b1 JS |
260 | } |
261 | ||
262 | /// Combine the base and list style, using the given indent (from which | |
263 | /// an appropriate level is found) | |
24777478 | 264 | wxRichTextAttr wxRichTextListStyleDefinition::GetCombinedStyleForLevel(int listLevel, wxRichTextStyleSheet* styleSheet) |
38f833b1 | 265 | { |
24777478 | 266 | wxRichTextAttr attr(*GetLevelAttributes(listLevel)); |
38f833b1 JS |
267 | int oldLeftIndent = attr.GetLeftIndent(); |
268 | int oldLeftSubIndent = attr.GetLeftSubIndent(); | |
269 | ||
41a85215 | 270 | // Apply the overall paragraph style, if any |
336d8ae9 VZ |
271 | if (styleSheet) |
272 | attr.Apply(GetStyleMergedWithBase(styleSheet)); | |
273 | else | |
274 | attr.Apply(GetStyle()); | |
38f833b1 JS |
275 | |
276 | // We override the indents according to the list definition | |
277 | attr.SetLeftIndent(oldLeftIndent, oldLeftSubIndent); | |
41a85215 | 278 | |
38f833b1 JS |
279 | return attr; |
280 | } | |
281 | ||
282 | /// Is this a numbered list? | |
283 | bool wxRichTextListStyleDefinition::IsNumbered(int i) const | |
284 | { | |
285 | return (0 != (GetLevelAttributes(i)->GetFlags() & | |
286 | (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER|wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER| | |
287 | wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER|wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER))); | |
288 | } | |
289 | ||
5d7836c4 JS |
290 | /*! |
291 | * The style manager | |
292 | */ | |
293 | ||
294 | IMPLEMENT_CLASS(wxRichTextStyleSheet, wxObject) | |
295 | ||
38f833b1 JS |
296 | wxRichTextStyleSheet::~wxRichTextStyleSheet() |
297 | { | |
298 | DeleteStyles(); | |
299 | ||
300 | if (m_nextSheet) | |
301 | m_nextSheet->m_previousSheet = m_previousSheet; | |
41a85215 | 302 | |
38f833b1 JS |
303 | if (m_previousSheet) |
304 | m_previousSheet->m_nextSheet = m_nextSheet; | |
41a85215 | 305 | |
38f833b1 JS |
306 | m_previousSheet = NULL; |
307 | m_nextSheet = NULL; | |
308 | } | |
309 | ||
5d7836c4 JS |
310 | /// Initialisation |
311 | void wxRichTextStyleSheet::Init() | |
312 | { | |
38f833b1 JS |
313 | m_previousSheet = NULL; |
314 | m_nextSheet = NULL; | |
5d7836c4 JS |
315 | } |
316 | ||
317 | /// Add a definition to one of the style lists | |
318 | bool wxRichTextStyleSheet::AddStyle(wxList& list, wxRichTextStyleDefinition* def) | |
319 | { | |
320 | if (!list.Find(def)) | |
321 | list.Append(def); | |
322 | return true; | |
323 | } | |
324 | ||
325 | /// Remove a style | |
326 | bool wxRichTextStyleSheet::RemoveStyle(wxList& list, wxRichTextStyleDefinition* def, bool deleteStyle) | |
327 | { | |
09f14108 | 328 | wxList::compatibility_iterator node = list.Find(def); |
5d7836c4 JS |
329 | if (node) |
330 | { | |
331 | wxRichTextStyleDefinition* def = (wxRichTextStyleDefinition*) node->GetData(); | |
09f14108 | 332 | list.Erase(node); |
5d7836c4 JS |
333 | if (deleteStyle) |
334 | delete def; | |
335 | return true; | |
336 | } | |
337 | else | |
338 | return false; | |
339 | } | |
340 | ||
336d8ae9 VZ |
341 | /// Remove a style |
342 | bool wxRichTextStyleSheet::RemoveStyle(wxRichTextStyleDefinition* def, bool deleteStyle) | |
343 | { | |
344 | if (RemoveParagraphStyle(def, deleteStyle)) | |
345 | return true; | |
346 | if (RemoveCharacterStyle(def, deleteStyle)) | |
347 | return true; | |
348 | if (RemoveListStyle(def, deleteStyle)) | |
349 | return true; | |
603f702b JS |
350 | if (RemoveBoxStyle(def, deleteStyle)) |
351 | return true; | |
336d8ae9 VZ |
352 | return false; |
353 | } | |
354 | ||
5d7836c4 | 355 | /// Find a definition by name |
38f833b1 | 356 | wxRichTextStyleDefinition* wxRichTextStyleSheet::FindStyle(const wxList& list, const wxString& name, bool recurse) const |
5d7836c4 | 357 | { |
09f14108 | 358 | for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext()) |
5d7836c4 JS |
359 | { |
360 | wxRichTextStyleDefinition* def = (wxRichTextStyleDefinition*) node->GetData(); | |
23a9a87c | 361 | if (def->GetName() == name) |
5d7836c4 JS |
362 | return def; |
363 | } | |
41a85215 | 364 | |
38f833b1 JS |
365 | if (m_nextSheet && recurse) |
366 | return m_nextSheet->FindStyle(list, name, recurse); | |
367 | ||
61399247 | 368 | return NULL; |
5d7836c4 JS |
369 | } |
370 | ||
371 | /// Delete all styles | |
372 | void wxRichTextStyleSheet::DeleteStyles() | |
373 | { | |
374 | WX_CLEAR_LIST(wxList, m_characterStyleDefinitions); | |
375 | WX_CLEAR_LIST(wxList, m_paragraphStyleDefinitions); | |
38f833b1 | 376 | WX_CLEAR_LIST(wxList, m_listStyleDefinitions); |
603f702b | 377 | WX_CLEAR_LIST(wxList, m_boxStyleDefinitions); |
38f833b1 JS |
378 | } |
379 | ||
380 | /// Insert into list of style sheets | |
381 | bool wxRichTextStyleSheet::InsertSheet(wxRichTextStyleSheet* before) | |
382 | { | |
383 | m_previousSheet = before->m_previousSheet; | |
384 | m_nextSheet = before; | |
41a85215 | 385 | |
38f833b1 JS |
386 | before->m_previousSheet = this; |
387 | return true; | |
388 | } | |
389 | ||
390 | /// Append to list of style sheets | |
391 | bool wxRichTextStyleSheet::AppendSheet(wxRichTextStyleSheet* after) | |
392 | { | |
393 | wxRichTextStyleSheet* last = after; | |
394 | while (last && last->m_nextSheet) | |
395 | { | |
396 | last = last->m_nextSheet; | |
397 | } | |
41a85215 | 398 | |
38f833b1 JS |
399 | if (last) |
400 | { | |
401 | m_previousSheet = last; | |
402 | last->m_nextSheet = this; | |
41a85215 | 403 | |
38f833b1 JS |
404 | return true; |
405 | } | |
406 | else | |
407 | return false; | |
408 | } | |
409 | ||
410 | /// Unlink from the list of style sheets | |
411 | void wxRichTextStyleSheet::Unlink() | |
412 | { | |
413 | if (m_previousSheet) | |
414 | m_previousSheet->m_nextSheet = m_nextSheet; | |
415 | if (m_nextSheet) | |
416 | m_nextSheet->m_previousSheet = m_previousSheet; | |
41a85215 | 417 | |
38f833b1 JS |
418 | m_previousSheet = NULL; |
419 | m_nextSheet = NULL; | |
5d7836c4 JS |
420 | } |
421 | ||
fe5aa22c JS |
422 | /// Add a definition to the character style list |
423 | bool wxRichTextStyleSheet::AddCharacterStyle(wxRichTextCharacterStyleDefinition* def) | |
424 | { | |
425 | def->GetStyle().SetCharacterStyleName(def->GetName()); | |
426 | return AddStyle(m_characterStyleDefinitions, def); | |
427 | } | |
428 | ||
429 | /// Add a definition to the paragraph style list | |
430 | bool wxRichTextStyleSheet::AddParagraphStyle(wxRichTextParagraphStyleDefinition* def) | |
431 | { | |
432 | def->GetStyle().SetParagraphStyleName(def->GetName()); | |
433 | return AddStyle(m_paragraphStyleDefinitions, def); | |
434 | } | |
435 | ||
38f833b1 JS |
436 | /// Add a definition to the list style list |
437 | bool wxRichTextStyleSheet::AddListStyle(wxRichTextListStyleDefinition* def) | |
438 | { | |
439 | def->GetStyle().SetListStyleName(def->GetName()); | |
440 | return AddStyle(m_listStyleDefinitions, def); | |
441 | } | |
442 | ||
603f702b JS |
443 | /// Add a definition to the box style list |
444 | bool wxRichTextStyleSheet::AddBoxStyle(wxRichTextBoxStyleDefinition* def) | |
445 | { | |
1199fbd4 | 446 | def->GetStyle().GetTextBoxAttr().SetBoxStyleName(def->GetName()); |
603f702b JS |
447 | return AddStyle(m_boxStyleDefinitions, def); |
448 | } | |
449 | ||
336d8ae9 VZ |
450 | /// Add a definition to the appropriate style list |
451 | bool wxRichTextStyleSheet::AddStyle(wxRichTextStyleDefinition* def) | |
452 | { | |
453 | wxRichTextListStyleDefinition* listDef = wxDynamicCast(def, wxRichTextListStyleDefinition); | |
454 | if (listDef) | |
455 | return AddListStyle(listDef); | |
456 | ||
457 | wxRichTextParagraphStyleDefinition* paraDef = wxDynamicCast(def, wxRichTextParagraphStyleDefinition); | |
458 | if (paraDef) | |
459 | return AddParagraphStyle(paraDef); | |
460 | ||
461 | wxRichTextCharacterStyleDefinition* charDef = wxDynamicCast(def, wxRichTextCharacterStyleDefinition); | |
462 | if (charDef) | |
463 | return AddCharacterStyle(charDef); | |
3706bae0 | 464 | |
603f702b JS |
465 | wxRichTextBoxStyleDefinition* boxDef = wxDynamicCast(def, wxRichTextBoxStyleDefinition); |
466 | if (boxDef) | |
467 | return AddBoxStyle(boxDef); | |
468 | ||
336d8ae9 VZ |
469 | return false; |
470 | } | |
471 | ||
472 | /// Find any definition by name | |
473 | wxRichTextStyleDefinition* wxRichTextStyleSheet::FindStyle(const wxString& name, bool recurse) const | |
474 | { | |
475 | wxRichTextListStyleDefinition* listDef = FindListStyle(name, recurse); | |
476 | if (listDef) | |
477 | return listDef; | |
478 | ||
479 | wxRichTextParagraphStyleDefinition* paraDef = FindParagraphStyle(name, recurse); | |
480 | if (paraDef) | |
481 | return paraDef; | |
482 | ||
483 | wxRichTextCharacterStyleDefinition* charDef = FindCharacterStyle(name, recurse); | |
484 | if (charDef) | |
485 | return charDef; | |
3706bae0 | 486 | |
603f702b JS |
487 | wxRichTextBoxStyleDefinition* boxDef = FindBoxStyle(name, recurse); |
488 | if (boxDef) | |
489 | return boxDef; | |
490 | ||
3706bae0 | 491 | return NULL; |
336d8ae9 VZ |
492 | } |
493 | ||
fe5aa22c JS |
494 | /// Copy |
495 | void wxRichTextStyleSheet::Copy(const wxRichTextStyleSheet& sheet) | |
496 | { | |
497 | DeleteStyles(); | |
498 | ||
499 | wxList::compatibility_iterator node; | |
500 | ||
501 | for (node = sheet.m_characterStyleDefinitions.GetFirst(); node; node = node->GetNext()) | |
502 | { | |
503 | wxRichTextCharacterStyleDefinition* def = (wxRichTextCharacterStyleDefinition*) node->GetData(); | |
504 | AddCharacterStyle(new wxRichTextCharacterStyleDefinition(*def)); | |
505 | } | |
506 | ||
507 | for (node = sheet.m_paragraphStyleDefinitions.GetFirst(); node; node = node->GetNext()) | |
508 | { | |
509 | wxRichTextParagraphStyleDefinition* def = (wxRichTextParagraphStyleDefinition*) node->GetData(); | |
510 | AddParagraphStyle(new wxRichTextParagraphStyleDefinition(*def)); | |
511 | } | |
38f833b1 JS |
512 | |
513 | for (node = sheet.m_listStyleDefinitions.GetFirst(); node; node = node->GetNext()) | |
514 | { | |
515 | wxRichTextListStyleDefinition* def = (wxRichTextListStyleDefinition*) node->GetData(); | |
516 | AddListStyle(new wxRichTextListStyleDefinition(*def)); | |
517 | } | |
3706bae0 | 518 | |
603f702b JS |
519 | for (node = sheet.m_boxStyleDefinitions.GetFirst(); node; node = node->GetNext()) |
520 | { | |
521 | wxRichTextBoxStyleDefinition* def = (wxRichTextBoxStyleDefinition*) node->GetData(); | |
522 | AddBoxStyle(new wxRichTextBoxStyleDefinition(*def)); | |
523 | } | |
524 | ||
42688aea JS |
525 | SetName(sheet.GetName()); |
526 | SetDescription(sheet.GetDescription()); | |
c6182d48 | 527 | m_properties = sheet.m_properties; |
fe5aa22c JS |
528 | } |
529 | ||
530 | /// Equality | |
531 | bool wxRichTextStyleSheet::operator==(const wxRichTextStyleSheet& WXUNUSED(sheet)) const | |
532 | { | |
533 | // TODO | |
534 | return false; | |
535 | } | |
536 | ||
537 | ||
5d7836c4 | 538 | #if wxUSE_HTML |
6d531430 JS |
539 | |
540 | // Functions for dealing with clashing names for different kinds of style. | |
541 | // Returns "P", "C", "L" or "B" (paragraph, character, list or box) for | |
542 | // style name | type. | |
543 | static wxString wxGetRichTextStyleType(const wxString& style) | |
544 | { | |
545 | return style.AfterLast(wxT('|')); | |
546 | } | |
547 | ||
548 | static wxString wxGetRichTextStyle(const wxString& style) | |
549 | { | |
550 | return style.BeforeLast(wxT('|')); | |
551 | } | |
552 | ||
553 | ||
5d7836c4 | 554 | /*! |
38f833b1 | 555 | * wxRichTextStyleListBox: a listbox to display styles. |
5d7836c4 JS |
556 | */ |
557 | ||
558 | IMPLEMENT_CLASS(wxRichTextStyleListBox, wxHtmlListBox) | |
559 | ||
560 | BEGIN_EVENT_TABLE(wxRichTextStyleListBox, wxHtmlListBox) | |
5d7836c4 | 561 | EVT_LEFT_DOWN(wxRichTextStyleListBox::OnLeftDown) |
38f833b1 | 562 | EVT_LEFT_DCLICK(wxRichTextStyleListBox::OnLeftDoubleClick) |
e637208a | 563 | EVT_IDLE(wxRichTextStyleListBox::OnIdle) |
5d7836c4 JS |
564 | END_EVENT_TABLE() |
565 | ||
566 | wxRichTextStyleListBox::wxRichTextStyleListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos, | |
e637208a | 567 | const wxSize& size, long style) |
5d7836c4 | 568 | { |
e637208a JS |
569 | Init(); |
570 | Create(parent, id, pos, size, style); | |
571 | } | |
572 | ||
573 | bool wxRichTextStyleListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, | |
574 | const wxSize& size, long style) | |
575 | { | |
576 | return wxHtmlListBox::Create(parent, id, pos, size, style); | |
5d7836c4 JS |
577 | } |
578 | ||
579 | wxRichTextStyleListBox::~wxRichTextStyleListBox() | |
580 | { | |
581 | } | |
582 | ||
583 | /// Returns the HTML for this item | |
584 | wxString wxRichTextStyleListBox::OnGetItem(size_t n) const | |
585 | { | |
586 | if (!GetStyleSheet()) | |
587 | return wxEmptyString; | |
41a85215 | 588 | |
38f833b1 JS |
589 | wxRichTextStyleDefinition* def = GetStyle(n); |
590 | if (def) | |
591 | return CreateHTML(def); | |
5d7836c4 | 592 | |
5d7836c4 JS |
593 | return wxEmptyString; |
594 | } | |
595 | ||
596 | // Get style for index | |
597 | wxRichTextStyleDefinition* wxRichTextStyleListBox::GetStyle(size_t i) const | |
598 | { | |
599 | if (!GetStyleSheet()) | |
600 | return NULL; | |
3706bae0 | 601 | |
8a8e9df8 | 602 | if (i >= m_styleNames.GetCount() /* || i < 0 */ ) |
486dd03a | 603 | return NULL; |
5d7836c4 | 604 | |
6d531430 JS |
605 | wxString styleType = wxGetRichTextStyleType(m_styleNames[i]); |
606 | wxString style = wxGetRichTextStyle(m_styleNames[i]); | |
607 | if (styleType == wxT("P")) | |
608 | return GetStyleSheet()->FindParagraphStyle(style); | |
609 | else if (styleType == wxT("C")) | |
610 | return GetStyleSheet()->FindCharacterStyle(style); | |
611 | else if (styleType == wxT("L")) | |
612 | return GetStyleSheet()->FindListStyle(style); | |
613 | else if (styleType == wxT("B")) | |
614 | return GetStyleSheet()->FindBoxStyle(style); | |
615 | else | |
616 | return GetStyleSheet()->FindStyle(style); | |
5d7836c4 JS |
617 | } |
618 | ||
619 | /// Updates the list | |
620 | void wxRichTextStyleListBox::UpdateStyles() | |
621 | { | |
622 | if (GetStyleSheet()) | |
623 | { | |
02aa812c JS |
624 | int oldSel = GetSelection(); |
625 | ||
dadd4f55 | 626 | SetSelection(wxNOT_FOUND); |
3706bae0 | 627 | |
486dd03a | 628 | m_styleNames.Clear(); |
3706bae0 | 629 | |
486dd03a JS |
630 | size_t i; |
631 | if (GetStyleType() == wxRICHTEXT_STYLE_ALL || GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH) | |
632 | { | |
633 | for (i = 0; i < GetStyleSheet()->GetParagraphStyleCount(); i++) | |
6d531430 | 634 | m_styleNames.Add(GetStyleSheet()->GetParagraphStyle(i)->GetName() + wxT("|P")); |
486dd03a JS |
635 | } |
636 | if (GetStyleType() == wxRICHTEXT_STYLE_ALL || GetStyleType() == wxRICHTEXT_STYLE_CHARACTER) | |
637 | { | |
638 | for (i = 0; i < GetStyleSheet()->GetCharacterStyleCount(); i++) | |
6d531430 | 639 | m_styleNames.Add(GetStyleSheet()->GetCharacterStyle(i)->GetName() + wxT("|C")); |
486dd03a JS |
640 | } |
641 | if (GetStyleType() == wxRICHTEXT_STYLE_ALL || GetStyleType() == wxRICHTEXT_STYLE_LIST) | |
642 | { | |
643 | for (i = 0; i < GetStyleSheet()->GetListStyleCount(); i++) | |
6d531430 | 644 | m_styleNames.Add(GetStyleSheet()->GetListStyle(i)->GetName() + wxT("|L")); |
486dd03a | 645 | } |
603f702b JS |
646 | if (GetStyleType() == wxRICHTEXT_STYLE_ALL || GetStyleType() == wxRICHTEXT_STYLE_BOX) |
647 | { | |
648 | for (i = 0; i < GetStyleSheet()->GetBoxStyleCount(); i++) | |
6d531430 | 649 | m_styleNames.Add(GetStyleSheet()->GetBoxStyle(i)->GetName() + wxT("|B")); |
603f702b | 650 | } |
3706bae0 | 651 | |
486dd03a JS |
652 | m_styleNames.Sort(); |
653 | SetItemCount(m_styleNames.GetCount()); | |
41a85215 | 654 | |
5d7836c4 | 655 | Refresh(); |
dadd4f55 | 656 | |
02aa812c JS |
657 | int newSel = -1; |
658 | if (oldSel >= 0 && oldSel < (int) GetItemCount()) | |
659 | newSel = oldSel; | |
660 | else if (GetItemCount() > 0) | |
661 | newSel = 0; | |
662 | ||
663 | if (newSel >= 0) | |
dadd4f55 | 664 | { |
02aa812c | 665 | SetSelection(newSel); |
dadd4f55 | 666 | SendSelectedEvent(); |
41a85215 | 667 | } |
5d7836c4 | 668 | } |
3ef12fd2 JS |
669 | else |
670 | { | |
671 | m_styleNames.Clear(); | |
672 | SetSelection(wxNOT_FOUND); | |
673 | SetItemCount(0); | |
674 | Refresh(); | |
675 | } | |
5d7836c4 JS |
676 | } |
677 | ||
e637208a JS |
678 | // Get index for style name |
679 | int wxRichTextStyleListBox::GetIndexForStyle(const wxString& name) const | |
680 | { | |
6d531430 JS |
681 | wxString s(name); |
682 | if (GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH) | |
683 | s += wxT("|P"); | |
684 | else if (GetStyleType() == wxRICHTEXT_STYLE_CHARACTER) | |
685 | s += wxT("|C"); | |
686 | else if (GetStyleType() == wxRICHTEXT_STYLE_LIST) | |
687 | s += wxT("|L"); | |
688 | else if (GetStyleType() == wxRICHTEXT_STYLE_BOX) | |
689 | s += wxT("|B"); | |
690 | else | |
691 | { | |
692 | if (m_styleNames.Index(s + wxT("|P")) != wxNOT_FOUND) | |
693 | s += wxT("|P"); | |
694 | else if (m_styleNames.Index(s + wxT("|C")) != wxNOT_FOUND) | |
695 | s += wxT("|C"); | |
696 | else if (m_styleNames.Index(s + wxT("|L")) != wxNOT_FOUND) | |
697 | s += wxT("|L"); | |
698 | else if (m_styleNames.Index(s + wxT("|B")) != wxNOT_FOUND) | |
699 | s += wxT("|B"); | |
700 | } | |
701 | return m_styleNames.Index(s); | |
e637208a JS |
702 | } |
703 | ||
704 | /// Set selection for string | |
705 | int wxRichTextStyleListBox::SetStyleSelection(const wxString& name) | |
706 | { | |
707 | int i = GetIndexForStyle(name); | |
708 | if (i > -1) | |
be8d847d | 709 | { |
e637208a | 710 | SetSelection(i); |
be8d847d | 711 | if (!IsVisible(i)) |
14722c43 | 712 | ScrollToRow(i); |
be8d847d | 713 | } |
e637208a JS |
714 | return i; |
715 | } | |
716 | ||
5d7836c4 JS |
717 | // Convert a colour to a 6-digit hex string |
718 | static wxString ColourToHexString(const wxColour& col) | |
719 | { | |
720 | wxString hex; | |
721 | ||
722 | hex += wxDecToHex(col.Red()); | |
723 | hex += wxDecToHex(col.Green()); | |
724 | hex += wxDecToHex(col.Blue()); | |
725 | ||
726 | return hex; | |
727 | } | |
728 | ||
729 | /// Creates a suitable HTML fragment for a definition | |
730 | wxString wxRichTextStyleListBox::CreateHTML(wxRichTextStyleDefinition* def) const | |
731 | { | |
38f833b1 JS |
732 | // TODO: indicate list format for list style types |
733 | ||
380a5dd8 JS |
734 | wxString str; |
735 | ||
736 | bool isCentred = false; | |
3706bae0 | 737 | |
24777478 | 738 | wxRichTextAttr attr(def->GetStyleMergedWithBase(GetStyleSheet())); |
380a5dd8 | 739 | |
336d8ae9 | 740 | if (attr.HasAlignment() && attr.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE) |
380a5dd8 JS |
741 | isCentred = true; |
742 | ||
743 | if (isCentred) | |
744 | str << wxT("<center>"); | |
745 | ||
3706bae0 | 746 | |
380a5dd8 | 747 | str << wxT("<table><tr>"); |
5d7836c4 | 748 | |
336d8ae9 | 749 | if (attr.GetLeftIndent() > 0) |
5d7836c4 JS |
750 | { |
751 | wxClientDC dc((wxWindow*) this); | |
752 | ||
336d8ae9 | 753 | str << wxT("<td width=") << wxMin(50, (ConvertTenthsMMToPixels(dc, attr.GetLeftIndent())/2)) << wxT("></td>"); |
5d7836c4 JS |
754 | } |
755 | ||
380a5dd8 JS |
756 | if (isCentred) |
757 | str << wxT("<td nowrap align=\"center\">"); | |
758 | else | |
759 | str << wxT("<td nowrap>"); | |
5d7836c4 | 760 | |
dadd4f55 | 761 | #ifdef __WXMSW__ |
e2d0875a | 762 | int size = 2; |
dadd4f55 | 763 | #else |
e2d0875a | 764 | int size = 3; |
dadd4f55 JS |
765 | #endif |
766 | ||
03647350 | 767 | // Guess a standard font size |
e2d0875a JS |
768 | int stdFontSize = 0; |
769 | ||
770 | // First see if we have a default/normal style to base the size on | |
771 | wxString normalTranslated(_("normal")); | |
772 | wxString defaultTranslated(_("default")); | |
773 | size_t i; | |
6d531430 | 774 | for (i = 0; i < GetStyleSheet()->GetParagraphStyleCount(); i++) |
e2d0875a | 775 | { |
6d531430 JS |
776 | wxRichTextStyleDefinition* d = GetStyleSheet()->GetParagraphStyle(i); |
777 | wxString name = d->GetName().Lower(); | |
03647350 | 778 | if (name.Find(wxT("normal")) != wxNOT_FOUND || name.Find(normalTranslated) != wxNOT_FOUND || |
6d531430 | 779 | name.Find(wxT("default")) != wxNOT_FOUND || name.Find(defaultTranslated) != wxNOT_FOUND) |
03647350 | 780 | { |
6d531430 | 781 | wxRichTextAttr attr2(d->GetStyleMergedWithBase(GetStyleSheet())); |
32423dd8 | 782 | if (attr2.HasFontPointSize()) |
03647350 | 783 | { |
6d531430 JS |
784 | stdFontSize = attr2.GetFontSize(); |
785 | break; | |
03647350 VZ |
786 | } |
787 | } | |
788 | } | |
e2d0875a JS |
789 | |
790 | if (stdFontSize == 0) | |
791 | { | |
792 | // Look at sizes up to 20 points, and see which is the most common | |
793 | wxArrayInt sizes; | |
794 | size_t maxSize = 20; | |
795 | for (i = 0; i <= maxSize; i++) | |
796 | sizes.Add(0); | |
797 | for (i = 0; i < m_styleNames.GetCount(); i++) | |
798 | { | |
6d531430 | 799 | wxRichTextStyleDefinition* d = GetStyle(i); |
e2d0875a JS |
800 | if (d) |
801 | { | |
802 | wxRichTextAttr attr2(d->GetStyleMergedWithBase(GetStyleSheet())); | |
32423dd8 | 803 | if (attr2.HasFontPointSize()) |
e2d0875a JS |
804 | { |
805 | if (attr2.GetFontSize() <= (int) maxSize) | |
806 | sizes[attr2.GetFontSize()] ++; | |
807 | } | |
808 | } | |
809 | } | |
810 | int mostCommonSize = 0; | |
811 | for (i = 0; i <= maxSize; i++) | |
812 | { | |
813 | if (sizes[i] > mostCommonSize) | |
814 | mostCommonSize = i; | |
03647350 | 815 | } |
e2d0875a JS |
816 | if (mostCommonSize > 0) |
817 | stdFontSize = mostCommonSize; | |
818 | } | |
819 | ||
820 | if (stdFontSize == 0) | |
03647350 | 821 | stdFontSize = 12; |
e2d0875a | 822 | |
32423dd8 | 823 | int thisFontSize = attr.HasFontPointSize() ? attr.GetFontSize() : stdFontSize; |
5d7836c4 | 824 | |
dadd4f55 | 825 | if (thisFontSize < stdFontSize) |
dadd4f55 | 826 | size --; |
e2d0875a JS |
827 | else if (thisFontSize > stdFontSize) |
828 | size ++; | |
5d7836c4 JS |
829 | |
830 | str += wxT("<font"); | |
831 | ||
832 | str << wxT(" size=") << size; | |
833 | ||
336d8ae9 VZ |
834 | if (!attr.GetFontFaceName().IsEmpty()) |
835 | str << wxT(" face=\"") << attr.GetFontFaceName() << wxT("\""); | |
5d7836c4 | 836 | |
a1b806b9 | 837 | if (attr.GetTextColour().IsOk()) |
336d8ae9 | 838 | str << wxT(" color=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\""); |
5d7836c4 JS |
839 | |
840 | str << wxT(">"); | |
841 | ||
842 | bool hasBold = false; | |
843 | bool hasItalic = false; | |
844 | bool hasUnderline = false; | |
845 | ||
43f4e852 | 846 | if (attr.GetFontWeight() == wxFONTWEIGHT_BOLD) |
5d7836c4 | 847 | hasBold = true; |
43f4e852 | 848 | if (attr.GetFontStyle() == wxFONTSTYLE_ITALIC) |
5d7836c4 | 849 | hasItalic = true; |
336d8ae9 | 850 | if (attr.GetFontUnderlined()) |
5d7836c4 JS |
851 | hasUnderline = true; |
852 | ||
853 | if (hasBold) | |
854 | str << wxT("<b>"); | |
855 | if (hasItalic) | |
856 | str << wxT("<i>"); | |
857 | if (hasUnderline) | |
858 | str << wxT("<u>"); | |
859 | ||
860 | str += def->GetName(); | |
861 | ||
862 | if (hasUnderline) | |
863 | str << wxT("</u>"); | |
864 | if (hasItalic) | |
865 | str << wxT("</i>"); | |
866 | if (hasBold) | |
867 | str << wxT("</b>"); | |
868 | ||
380a5dd8 JS |
869 | if (isCentred) |
870 | str << wxT("</centre>"); | |
871 | ||
5d7836c4 JS |
872 | str << wxT("</font>"); |
873 | ||
380a5dd8 JS |
874 | str << wxT("</td></tr></table>"); |
875 | ||
876 | if (isCentred) | |
877 | str << wxT("</center>"); | |
878 | ||
5d7836c4 JS |
879 | return str; |
880 | } | |
881 | ||
882 | // Convert units in tends of a millimetre to device units | |
883 | int wxRichTextStyleListBox::ConvertTenthsMMToPixels(wxDC& dc, int units) const | |
884 | { | |
885 | int ppi = dc.GetPPI().x; | |
886 | ||
887 | // There are ppi pixels in 254.1 "1/10 mm" | |
888 | ||
889 | double pixels = ((double) units * (double)ppi) / 254.1; | |
890 | ||
891 | return (int) pixels; | |
892 | } | |
893 | ||
5d7836c4 JS |
894 | void wxRichTextStyleListBox::OnLeftDown(wxMouseEvent& event) |
895 | { | |
896 | wxVListBox::OnLeftDown(event); | |
897 | ||
6eff83b8 | 898 | int item = VirtualHitTest(event.GetPosition().y); |
86015e55 JS |
899 | if (item != wxNOT_FOUND && GetApplyOnSelection()) |
900 | ApplyStyle(item); | |
e637208a | 901 | } |
5d7836c4 | 902 | |
38f833b1 | 903 | void wxRichTextStyleListBox::OnLeftDoubleClick(wxMouseEvent& event) |
e637208a | 904 | { |
38f833b1 | 905 | wxVListBox::OnLeftDown(event); |
fe5aa22c | 906 | |
6eff83b8 | 907 | int item = VirtualHitTest(event.GetPosition().y); |
38f833b1 JS |
908 | if (item != wxNOT_FOUND && !GetApplyOnSelection()) |
909 | ApplyStyle(item); | |
910 | } | |
e637208a | 911 | |
38f833b1 JS |
912 | /// Helper for listbox and combo control |
913 | wxString wxRichTextStyleListBox::GetStyleToShowInIdleTime(wxRichTextCtrl* ctrl, wxRichTextStyleType styleType) | |
914 | { | |
915 | int adjustedCaretPos = ctrl->GetAdjustedCaretPosition(ctrl->GetCaretPosition()); | |
e637208a | 916 | |
38f833b1 JS |
917 | wxString styleName; |
918 | ||
24777478 | 919 | wxRichTextAttr attr; |
3f239e37 JS |
920 | ctrl->GetStyle(adjustedCaretPos, attr); |
921 | ||
38f833b1 JS |
922 | // Take into account current default style just chosen by user |
923 | if (ctrl->IsDefaultStyleShowing()) | |
924 | { | |
3706bae0 JS |
925 | wxRichTextApplyStyle(attr, ctrl->GetDefaultStyleEx()); |
926 | ||
38f833b1 | 927 | if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_CHARACTER) && |
3706bae0 JS |
928 | !attr.GetCharacterStyleName().IsEmpty()) |
929 | styleName = attr.GetCharacterStyleName(); | |
38f833b1 | 930 | else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_PARAGRAPH) && |
3706bae0 JS |
931 | !attr.GetParagraphStyleName().IsEmpty()) |
932 | styleName = attr.GetParagraphStyleName(); | |
38f833b1 | 933 | else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_LIST) && |
3706bae0 JS |
934 | !attr.GetListStyleName().IsEmpty()) |
935 | styleName = attr.GetListStyleName(); | |
603f702b JS |
936 | // TODO: when we have a concept of focused object (text box), we'll |
937 | // use the paragraph style name of the focused object as the frame style name. | |
938 | #if 0 | |
939 | else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_BOX) && | |
940 | !attr.GetBoxStyleName().IsEmpty()) | |
941 | styleName = attr.GetBoxStyleName(); | |
942 | #endif | |
38f833b1 | 943 | } |
3f239e37 JS |
944 | else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_CHARACTER) && |
945 | !attr.GetCharacterStyleName().IsEmpty()) | |
38f833b1 | 946 | { |
3f239e37 | 947 | styleName = attr.GetCharacterStyleName(); |
38f833b1 | 948 | } |
3f239e37 JS |
949 | else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_PARAGRAPH) && |
950 | !attr.GetParagraphStyleName().IsEmpty()) | |
38f833b1 | 951 | { |
3f239e37 | 952 | styleName = attr.GetParagraphStyleName(); |
38f833b1 | 953 | } |
3f239e37 JS |
954 | else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_LIST) && |
955 | !attr.GetListStyleName().IsEmpty()) | |
38f833b1 | 956 | { |
3f239e37 | 957 | styleName = attr.GetListStyleName(); |
38f833b1 | 958 | } |
41a85215 | 959 | |
38f833b1 JS |
960 | return styleName; |
961 | } | |
962 | ||
963 | /// Auto-select from style under caret in idle time | |
964 | void wxRichTextStyleListBox::OnIdle(wxIdleEvent& event) | |
965 | { | |
3570a1c6 | 966 | if (CanAutoSetSelection() && GetRichTextCtrl() && IsShownOnScreen() && wxWindow::FindFocus() != this) |
38f833b1 JS |
967 | { |
968 | wxString styleName = GetStyleToShowInIdleTime(GetRichTextCtrl(), GetStyleType()); | |
61399247 | 969 | |
e637208a JS |
970 | int sel = GetSelection(); |
971 | if (!styleName.IsEmpty()) | |
972 | { | |
973 | // Don't do the selection if it's already set | |
974 | if (sel == GetIndexForStyle(styleName)) | |
975 | return; | |
5d7836c4 | 976 | |
e637208a JS |
977 | SetStyleSelection(styleName); |
978 | } | |
979 | else if (sel != -1) | |
980 | SetSelection(-1); | |
981 | } | |
982 | event.Skip(); | |
983 | } | |
5d7836c4 | 984 | |
e637208a | 985 | /// Do selection |
86015e55 | 986 | void wxRichTextStyleListBox::ApplyStyle(int item) |
e637208a JS |
987 | { |
988 | if ( item != wxNOT_FOUND ) | |
989 | { | |
990 | wxRichTextStyleDefinition* def = GetStyle(item); | |
991 | if (def && GetRichTextCtrl()) | |
992 | { | |
993 | GetRichTextCtrl()->ApplyStyle(def); | |
994 | GetRichTextCtrl()->SetFocus(); | |
5d7836c4 JS |
995 | } |
996 | } | |
997 | } | |
998 | ||
38f833b1 JS |
999 | /*! |
1000 | * wxRichTextStyleListCtrl class: manages a listbox and a choice control to | |
1001 | * switch shown style types | |
1002 | */ | |
1003 | ||
1004 | IMPLEMENT_CLASS(wxRichTextStyleListCtrl, wxControl) | |
1005 | ||
1006 | BEGIN_EVENT_TABLE(wxRichTextStyleListCtrl, wxControl) | |
1007 | EVT_CHOICE(wxID_ANY, wxRichTextStyleListCtrl::OnChooseType) | |
1008 | EVT_SIZE(wxRichTextStyleListCtrl::OnSize) | |
1009 | END_EVENT_TABLE() | |
1010 | ||
1011 | wxRichTextStyleListCtrl::wxRichTextStyleListCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, | |
1012 | const wxSize& size, long style) | |
5d7836c4 | 1013 | { |
38f833b1 JS |
1014 | Init(); |
1015 | Create(parent, id, pos, size, style); | |
5d7836c4 JS |
1016 | } |
1017 | ||
38f833b1 JS |
1018 | bool wxRichTextStyleListCtrl::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, |
1019 | const wxSize& size, long style) | |
5d7836c4 | 1020 | { |
2fce6547 JS |
1021 | if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT) |
1022 | style |= wxBORDER_THEME; | |
1023 | ||
38f833b1 | 1024 | wxControl::Create(parent, id, pos, size, style); |
41a85215 | 1025 | |
38f833b1 | 1026 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
dadd4f55 | 1027 | if (size != wxDefaultSize) |
170acdc9 | 1028 | SetInitialSize(size); |
41a85215 | 1029 | |
dadd4f55 | 1030 | bool showSelector = ((style & wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR) == 0); |
41a85215 | 1031 | |
2fce6547 JS |
1032 | wxBorder listBoxStyle; |
1033 | if (showSelector) | |
1034 | listBoxStyle = wxBORDER_THEME; | |
1035 | else | |
1036 | listBoxStyle = wxBORDER_NONE; | |
1037 | ||
1038 | m_styleListBox = new wxRichTextStyleListBox(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, listBoxStyle); | |
dadd4f55 JS |
1039 | |
1040 | wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL); | |
41a85215 | 1041 | |
dadd4f55 | 1042 | if (showSelector) |
41a85215 | 1043 | { |
dadd4f55 JS |
1044 | wxArrayString choices; |
1045 | choices.Add(_("All styles")); | |
1046 | choices.Add(_("Paragraph styles")); | |
1047 | choices.Add(_("Character styles")); | |
1048 | choices.Add(_("List styles")); | |
603f702b | 1049 | choices.Add(_("Box styles")); |
41a85215 | 1050 | |
dadd4f55 | 1051 | m_styleChoice = new wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, choices); |
41a85215 | 1052 | |
dadd4f55 | 1053 | boxSizer->Add(m_styleListBox, 1, wxALL|wxEXPAND, 5); |
a047aff2 | 1054 | boxSizer->Add(m_styleChoice, 0, wxLEFT|wxRIGHT|wxBOTTOM|wxEXPAND, 5); |
dadd4f55 JS |
1055 | } |
1056 | else | |
1057 | { | |
1058 | boxSizer->Add(m_styleListBox, 1, wxALL|wxEXPAND, 0); | |
1059 | } | |
41a85215 | 1060 | |
dadd4f55 JS |
1061 | SetSizer(boxSizer); |
1062 | Layout(); | |
38f833b1 JS |
1063 | |
1064 | m_dontUpdate = true; | |
dadd4f55 JS |
1065 | |
1066 | if (m_styleChoice) | |
41a85215 | 1067 | { |
dadd4f55 JS |
1068 | int i = StyleTypeToIndex(m_styleListBox->GetStyleType()); |
1069 | m_styleChoice->SetSelection(i); | |
1070 | } | |
41a85215 | 1071 | |
38f833b1 | 1072 | m_dontUpdate = false; |
41a85215 | 1073 | |
38f833b1 JS |
1074 | return true; |
1075 | } | |
1076 | ||
1077 | wxRichTextStyleListCtrl::~wxRichTextStyleListCtrl() | |
1078 | { | |
41a85215 | 1079 | |
38f833b1 JS |
1080 | } |
1081 | ||
1082 | /// React to style type choice | |
1083 | void wxRichTextStyleListCtrl::OnChooseType(wxCommandEvent& event) | |
1084 | { | |
1085 | if (event.GetEventObject() != m_styleChoice) | |
1086 | event.Skip(); | |
1087 | else | |
1088 | { | |
1089 | if (m_dontUpdate) | |
1090 | return; | |
41a85215 | 1091 | |
38f833b1 | 1092 | wxRichTextStyleListBox::wxRichTextStyleType styleType = StyleIndexToType(event.GetSelection()); |
02aa812c | 1093 | m_styleListBox->SetSelection(-1); |
38f833b1 JS |
1094 | m_styleListBox->SetStyleType(styleType); |
1095 | } | |
1096 | } | |
1097 | ||
1098 | /// Lay out the controls | |
1099 | void wxRichTextStyleListCtrl::OnSize(wxSizeEvent& WXUNUSED(event)) | |
1100 | { | |
1101 | if (GetAutoLayout()) | |
1102 | Layout(); | |
1103 | } | |
1104 | ||
1105 | /// Get the choice index for style type | |
1106 | int wxRichTextStyleListCtrl::StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType) | |
1107 | { | |
1108 | if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL) | |
1109 | { | |
1110 | return 0; | |
1111 | } | |
1112 | else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH) | |
1113 | { | |
1114 | return 1; | |
1115 | } | |
1116 | else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER) | |
1117 | { | |
1118 | return 2; | |
1119 | } | |
1120 | else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST) | |
1121 | { | |
1122 | return 3; | |
1123 | } | |
603f702b JS |
1124 | else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_BOX) |
1125 | { | |
1126 | return 4; | |
1127 | } | |
38f833b1 JS |
1128 | return 0; |
1129 | } | |
1130 | ||
1131 | /// Get the style type for choice index | |
1132 | wxRichTextStyleListBox::wxRichTextStyleType wxRichTextStyleListCtrl::StyleIndexToType(int i) | |
1133 | { | |
1134 | if (i == 1) | |
1135 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH; | |
1136 | else if (i == 2) | |
1137 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER; | |
1138 | else if (i == 3) | |
1139 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST; | |
603f702b JS |
1140 | else if (i == 4) |
1141 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_BOX; | |
38f833b1 JS |
1142 | |
1143 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL; | |
1144 | } | |
1145 | ||
1146 | /// Associates the control with a style manager | |
1147 | void wxRichTextStyleListCtrl::SetStyleSheet(wxRichTextStyleSheet* styleSheet) | |
1148 | { | |
1149 | if (m_styleListBox) | |
1150 | m_styleListBox->SetStyleSheet(styleSheet); | |
1151 | } | |
1152 | ||
1153 | wxRichTextStyleSheet* wxRichTextStyleListCtrl::GetStyleSheet() const | |
1154 | { | |
1155 | if (m_styleListBox) | |
1156 | return m_styleListBox->GetStyleSheet(); | |
1157 | else | |
1158 | return NULL; | |
1159 | } | |
1160 | ||
1161 | /// Associates the control with a wxRichTextCtrl | |
1162 | void wxRichTextStyleListCtrl::SetRichTextCtrl(wxRichTextCtrl* ctrl) | |
1163 | { | |
1164 | if (m_styleListBox) | |
1165 | m_styleListBox->SetRichTextCtrl(ctrl); | |
1166 | } | |
1167 | ||
1168 | wxRichTextCtrl* wxRichTextStyleListCtrl::GetRichTextCtrl() const | |
1169 | { | |
1170 | if (m_styleListBox) | |
1171 | return m_styleListBox->GetRichTextCtrl(); | |
1172 | else | |
1173 | return NULL; | |
1174 | } | |
1175 | ||
1176 | /// Set/get the style type to display | |
1177 | void wxRichTextStyleListCtrl::SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType) | |
1178 | { | |
cd5dea50 VZ |
1179 | if ( !m_styleListBox ) |
1180 | return; | |
1181 | ||
1182 | m_styleListBox->SetStyleType(styleType); | |
38f833b1 JS |
1183 | |
1184 | m_dontUpdate = true; | |
1185 | ||
1186 | if (m_styleChoice) | |
41a85215 | 1187 | { |
38f833b1 JS |
1188 | int i = StyleTypeToIndex(m_styleListBox->GetStyleType()); |
1189 | m_styleChoice->SetSelection(i); | |
1190 | } | |
41a85215 | 1191 | |
38f833b1 JS |
1192 | m_dontUpdate = false; |
1193 | } | |
1194 | ||
1195 | wxRichTextStyleListBox::wxRichTextStyleType wxRichTextStyleListCtrl::GetStyleType() const | |
1196 | { | |
1197 | if (m_styleListBox) | |
1198 | return m_styleListBox->GetStyleType(); | |
1199 | else | |
1200 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL; | |
1201 | } | |
1202 | ||
1203 | /// Updates the style list box | |
1204 | void wxRichTextStyleListCtrl::UpdateStyles() | |
1205 | { | |
1206 | if (m_styleListBox) | |
41a85215 | 1207 | m_styleListBox->UpdateStyles(); |
5d7836c4 | 1208 | } |
5d7836c4 | 1209 | |
e637208a JS |
1210 | #if wxUSE_COMBOCTRL |
1211 | ||
1212 | /*! | |
1213 | * Style drop-down for a wxComboCtrl | |
1214 | */ | |
1215 | ||
1216 | ||
1217 | BEGIN_EVENT_TABLE(wxRichTextStyleComboPopup, wxRichTextStyleListBox) | |
1218 | EVT_MOTION(wxRichTextStyleComboPopup::OnMouseMove) | |
1219 | EVT_LEFT_DOWN(wxRichTextStyleComboPopup::OnMouseClick) | |
1220 | END_EVENT_TABLE() | |
1221 | ||
a047aff2 JS |
1222 | bool wxRichTextStyleComboPopup::Create( wxWindow* parent ) |
1223 | { | |
44cc96a8 | 1224 | int borderStyle = GetDefaultBorder(); |
9b794421 JS |
1225 | if (borderStyle == wxBORDER_SUNKEN || borderStyle == wxBORDER_NONE) |
1226 | borderStyle = wxBORDER_THEME; | |
a047aff2 JS |
1227 | |
1228 | return wxRichTextStyleListBox::Create(parent, wxID_ANY, | |
1229 | wxPoint(0,0), wxDefaultSize, | |
1230 | borderStyle); | |
1231 | } | |
1232 | ||
e637208a JS |
1233 | void wxRichTextStyleComboPopup::SetStringValue( const wxString& s ) |
1234 | { | |
1235 | m_value = SetStyleSelection(s); | |
1236 | } | |
1237 | ||
1238 | wxString wxRichTextStyleComboPopup::GetStringValue() const | |
1239 | { | |
1240 | int sel = m_value; | |
1241 | if (sel > -1) | |
1242 | { | |
1243 | wxRichTextStyleDefinition* def = GetStyle(sel); | |
1244 | if (def) | |
1245 | return def->GetName(); | |
1246 | } | |
1247 | return wxEmptyString; | |
1248 | } | |
1249 | ||
1250 | // | |
1251 | // Popup event handlers | |
1252 | // | |
1253 | ||
1254 | // Mouse hot-tracking | |
1255 | void wxRichTextStyleComboPopup::OnMouseMove(wxMouseEvent& event) | |
1256 | { | |
1257 | // Move selection to cursor if it is inside the popup | |
1258 | ||
6eff83b8 | 1259 | int itemHere = wxRichTextStyleListBox::VirtualHitTest(event.GetPosition().y); |
e637208a JS |
1260 | if ( itemHere >= 0 ) |
1261 | { | |
1262 | wxRichTextStyleListBox::SetSelection(itemHere); | |
1263 | m_itemHere = itemHere; | |
1264 | } | |
1265 | event.Skip(); | |
1266 | } | |
1267 | ||
1268 | // On mouse left, set the value and close the popup | |
1269 | void wxRichTextStyleComboPopup::OnMouseClick(wxMouseEvent& WXUNUSED(event)) | |
1270 | { | |
1271 | if (m_itemHere >= 0) | |
1272 | m_value = m_itemHere; | |
1273 | ||
1274 | // Ordering is important, so we don't dismiss this popup accidentally | |
86015e55 | 1275 | // by setting the focus elsewhere e.g. in ApplyStyle |
e637208a JS |
1276 | Dismiss(); |
1277 | ||
1278 | if (m_itemHere >= 0) | |
86015e55 | 1279 | wxRichTextStyleListBox::ApplyStyle(m_itemHere); |
e637208a JS |
1280 | } |
1281 | ||
1282 | /*! | |
1283 | * wxRichTextStyleComboCtrl | |
1284 | * A combo for applying styles. | |
1285 | */ | |
1286 | ||
1287 | IMPLEMENT_CLASS(wxRichTextStyleComboCtrl, wxComboCtrl) | |
1288 | ||
1289 | BEGIN_EVENT_TABLE(wxRichTextStyleComboCtrl, wxComboCtrl) | |
1290 | EVT_IDLE(wxRichTextStyleComboCtrl::OnIdle) | |
1291 | END_EVENT_TABLE() | |
1292 | ||
1293 | bool wxRichTextStyleComboCtrl::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, | |
1294 | const wxSize& size, long style) | |
1295 | { | |
1296 | if (!wxComboCtrl::Create(parent, id, wxEmptyString, pos, size, style)) | |
1297 | return false; | |
1298 | ||
1299 | SetPopupMaxHeight(400); | |
1300 | ||
1301 | m_stylePopup = new wxRichTextStyleComboPopup; | |
1302 | ||
1303 | SetPopupControl(m_stylePopup); | |
1304 | ||
1305 | return true; | |
1306 | } | |
1307 | ||
1308 | /// Auto-select from style under caret in idle time | |
1309 | ||
1310 | // TODO: must be able to show italic, bold, combinations | |
1311 | // in style box. Do we have a concept of automatic, temporary | |
1312 | // styles that are added whenever we wish to show a style | |
1313 | // that doesn't exist already? E.g. "Bold, Italic, Underline". | |
1314 | // Word seems to generate these things on the fly. | |
1315 | // If there's a named style already, it uses e.g. Heading1 + Bold, Italic | |
1316 | // If you unembolden text in a style that has bold, it uses the | |
1317 | // term "Not bold". | |
1318 | // TODO: order styles alphabetically. This means indexes can change, | |
1319 | // so need a different way to specify selections, i.e. by name. | |
1320 | ||
1321 | void wxRichTextStyleComboCtrl::OnIdle(wxIdleEvent& event) | |
1322 | { | |
ecb7235d VZ |
1323 | event.Skip(); |
1324 | ||
1325 | if ( !m_stylePopup ) | |
1326 | return; | |
1327 | ||
1328 | wxRichTextCtrl * const richtext = GetRichTextCtrl(); | |
1329 | if ( !richtext ) | |
1330 | return; | |
1331 | ||
3570a1c6 | 1332 | if ( !IsPopupShown() && IsShownOnScreen() && wxWindow::FindFocus() != this ) |
e637208a | 1333 | { |
ecb7235d VZ |
1334 | wxString styleName = |
1335 | wxRichTextStyleListBox::GetStyleToShowInIdleTime(richtext, m_stylePopup->GetStyleType()); | |
41a85215 | 1336 | |
e637208a JS |
1337 | wxString currentValue = GetValue(); |
1338 | if (!styleName.IsEmpty()) | |
1339 | { | |
1340 | // Don't do the selection if it's already set | |
1341 | if (currentValue == styleName) | |
1342 | return; | |
1343 | ||
1344 | SetValue(styleName); | |
1345 | } | |
1346 | else if (!currentValue.IsEmpty()) | |
1347 | SetValue(wxEmptyString); | |
1348 | } | |
e637208a JS |
1349 | } |
1350 | ||
1351 | #endif | |
1352 | // wxUSE_COMBOCTRL | |
1353 | ||
5d7836c4 JS |
1354 | #endif |
1355 | // wxUSE_HTML | |
1356 | ||
1357 | #endif | |
1358 | // wxUSE_RICHTEXT |