]>
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) |
5d7836c4 | 38 | |
fe5aa22c JS |
39 | /*! |
40 | * A definition | |
41 | */ | |
42 | ||
43 | void wxRichTextStyleDefinition::Copy(const wxRichTextStyleDefinition& def) | |
44 | { | |
45 | m_name = def.m_name; | |
46 | m_baseStyle = def.m_baseStyle; | |
47 | m_style = def.m_style; | |
42688aea | 48 | m_description = def.m_description; |
fe5aa22c JS |
49 | } |
50 | ||
51 | bool wxRichTextStyleDefinition::Eq(const wxRichTextStyleDefinition& def) const | |
52 | { | |
53 | return (m_name == def.m_name && m_baseStyle == def.m_baseStyle && m_style == def.m_style); | |
54 | } | |
55 | ||
336d8ae9 VZ |
56 | /// Gets the style combined with the base style |
57 | wxRichTextAttr wxRichTextStyleDefinition::GetStyleMergedWithBase(const wxRichTextStyleSheet* sheet) const | |
58 | { | |
59 | if (!m_baseStyle.IsEmpty()) | |
60 | { | |
61 | wxRichTextStyleDefinition* baseStyle = sheet->FindStyle(m_baseStyle); | |
62 | if (baseStyle) | |
63 | { | |
64 | wxRichTextAttr baseAttr = baseStyle->GetStyleMergedWithBase(sheet); | |
65 | baseAttr.Apply(m_style, NULL); | |
66 | return baseAttr; | |
67 | } | |
68 | } | |
69 | return m_style; | |
70 | } | |
71 | ||
fe5aa22c JS |
72 | /*! |
73 | * Paragraph style definition | |
74 | */ | |
75 | ||
76 | void wxRichTextParagraphStyleDefinition::Copy(const wxRichTextParagraphStyleDefinition& def) | |
77 | { | |
78 | wxRichTextStyleDefinition::Copy(def); | |
79 | ||
80 | m_nextStyle = def.m_nextStyle; | |
81 | } | |
82 | ||
83 | bool wxRichTextParagraphStyleDefinition::operator ==(const wxRichTextParagraphStyleDefinition& def) const | |
84 | { | |
85 | return (Eq(def) && m_nextStyle == def.m_nextStyle); | |
86 | } | |
87 | ||
38f833b1 JS |
88 | /*! |
89 | * List style definition | |
90 | */ | |
91 | ||
92 | void wxRichTextListStyleDefinition::Copy(const wxRichTextListStyleDefinition& def) | |
93 | { | |
94 | wxRichTextParagraphStyleDefinition::Copy(def); | |
41a85215 | 95 | |
38f833b1 JS |
96 | int i; |
97 | for (i = 0; i < 10; i++) | |
98 | m_levelStyles[i] = def.m_levelStyles[i]; | |
99 | } | |
100 | ||
101 | bool wxRichTextListStyleDefinition::operator ==(const wxRichTextListStyleDefinition& def) const | |
102 | { | |
103 | if (!Eq(def)) | |
104 | return false; | |
105 | int i; | |
106 | for (i = 0; i < 10; i++) | |
107 | if (!(m_levelStyles[i] == def.m_levelStyles[i])) | |
108 | return false; | |
41a85215 | 109 | |
38f833b1 JS |
110 | return true; |
111 | } | |
112 | ||
113 | /// Sets/gets the attributes for the given level | |
d2d0adc7 | 114 | void wxRichTextListStyleDefinition::SetLevelAttributes(int i, const wxRichTextAttr& attr) |
38f833b1 JS |
115 | { |
116 | wxASSERT( (i >= 0 && i < 10) ); | |
117 | if (i >= 0 && i < 10) | |
118 | m_levelStyles[i] = attr; | |
119 | } | |
120 | ||
d2d0adc7 | 121 | const wxRichTextAttr* wxRichTextListStyleDefinition::GetLevelAttributes(int i) const |
38f833b1 JS |
122 | { |
123 | wxASSERT( (i >= 0 && i < 10) ); | |
124 | if (i >= 0 && i < 10) | |
125 | return & m_levelStyles[i]; | |
126 | else | |
127 | return NULL; | |
128 | } | |
129 | ||
d2d0adc7 | 130 | wxRichTextAttr* wxRichTextListStyleDefinition::GetLevelAttributes(int i) |
38f833b1 JS |
131 | { |
132 | wxASSERT( (i >= 0 && i < 10) ); | |
133 | if (i >= 0 && i < 10) | |
134 | return & m_levelStyles[i]; | |
135 | else | |
136 | return NULL; | |
137 | } | |
138 | ||
139 | /// Convenience function for setting the major attributes for a list level specification | |
140 | void wxRichTextListStyleDefinition::SetAttributes(int i, int leftIndent, int leftSubIndent, int bulletStyle, const wxString& bulletSymbol) | |
141 | { | |
142 | wxASSERT( (i >= 0 && i < 10) ); | |
143 | if (i >= 0 && i < 10) | |
144 | { | |
d2d0adc7 | 145 | wxRichTextAttr attr; |
41a85215 | 146 | |
38f833b1 JS |
147 | attr.SetBulletStyle(bulletStyle); |
148 | attr.SetLeftIndent(leftIndent, leftSubIndent); | |
149 | ||
150 | if (!bulletSymbol.IsEmpty()) | |
d2d0adc7 JS |
151 | { |
152 | if (bulletStyle & wxTEXT_ATTR_BULLET_STYLE_SYMBOL) | |
153 | attr.SetBulletText(bulletSymbol); | |
154 | else | |
155 | attr.SetBulletName(bulletSymbol); | |
156 | } | |
41a85215 WS |
157 | |
158 | m_levelStyles[i] = attr; | |
159 | } | |
38f833b1 JS |
160 | } |
161 | ||
162 | /// Finds the level corresponding to the given indentation | |
163 | int wxRichTextListStyleDefinition::FindLevelForIndent(int indent) const | |
164 | { | |
165 | int i; | |
166 | for (i = 0; i < 10; i++) | |
167 | { | |
168 | if (indent < m_levelStyles[i].GetLeftIndent()) | |
169 | { | |
170 | if (i > 0) | |
171 | return i - 1; | |
172 | else | |
173 | return 0; | |
174 | } | |
175 | } | |
176 | return 9; | |
177 | } | |
178 | ||
179 | /// Combine the list style with a paragraph style, using the given indent (from which | |
180 | /// an appropriate level is found) | |
336d8ae9 | 181 | wxRichTextAttr wxRichTextListStyleDefinition::CombineWithParagraphStyle(int indent, const wxRichTextAttr& paraStyle, wxRichTextStyleSheet* styleSheet) |
38f833b1 JS |
182 | { |
183 | int listLevel = FindLevelForIndent(indent); | |
41a85215 | 184 | |
d2d0adc7 | 185 | wxRichTextAttr attr(*GetLevelAttributes(listLevel)); |
38f833b1 JS |
186 | int oldLeftIndent = attr.GetLeftIndent(); |
187 | int oldLeftSubIndent = attr.GetLeftSubIndent(); | |
188 | ||
41a85215 | 189 | // First apply the overall paragraph style, if any |
336d8ae9 VZ |
190 | if (styleSheet) |
191 | attr.Apply(GetStyleMergedWithBase(styleSheet)); | |
192 | else | |
193 | attr.Apply(GetStyle()); | |
38f833b1 JS |
194 | |
195 | // Then apply paragraph style, e.g. from paragraph style definition | |
336d8ae9 | 196 | attr.Apply(paraStyle); |
41a85215 | 197 | |
38f833b1 JS |
198 | // We override the indents according to the list definition |
199 | attr.SetLeftIndent(oldLeftIndent, oldLeftSubIndent); | |
41a85215 | 200 | |
38f833b1 JS |
201 | return attr; |
202 | } | |
203 | ||
204 | /// Combine the base and list style, using the given indent (from which | |
205 | /// an appropriate level is found) | |
336d8ae9 | 206 | wxRichTextAttr wxRichTextListStyleDefinition::GetCombinedStyle(int indent, wxRichTextStyleSheet* styleSheet) |
38f833b1 JS |
207 | { |
208 | int listLevel = FindLevelForIndent(indent); | |
336d8ae9 | 209 | return GetCombinedStyleForLevel(listLevel, styleSheet); |
38f833b1 JS |
210 | } |
211 | ||
212 | /// Combine the base and list style, using the given indent (from which | |
213 | /// an appropriate level is found) | |
336d8ae9 | 214 | wxRichTextAttr wxRichTextListStyleDefinition::GetCombinedStyleForLevel(int listLevel, wxRichTextStyleSheet* styleSheet) |
38f833b1 | 215 | { |
d2d0adc7 | 216 | wxRichTextAttr attr(*GetLevelAttributes(listLevel)); |
38f833b1 JS |
217 | int oldLeftIndent = attr.GetLeftIndent(); |
218 | int oldLeftSubIndent = attr.GetLeftSubIndent(); | |
219 | ||
41a85215 | 220 | // Apply the overall paragraph style, if any |
336d8ae9 VZ |
221 | if (styleSheet) |
222 | attr.Apply(GetStyleMergedWithBase(styleSheet)); | |
223 | else | |
224 | attr.Apply(GetStyle()); | |
38f833b1 JS |
225 | |
226 | // We override the indents according to the list definition | |
227 | attr.SetLeftIndent(oldLeftIndent, oldLeftSubIndent); | |
41a85215 | 228 | |
38f833b1 JS |
229 | return attr; |
230 | } | |
231 | ||
232 | /// Is this a numbered list? | |
233 | bool wxRichTextListStyleDefinition::IsNumbered(int i) const | |
234 | { | |
235 | return (0 != (GetLevelAttributes(i)->GetFlags() & | |
236 | (wxTEXT_ATTR_BULLET_STYLE_ARABIC|wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER|wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER| | |
237 | wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER|wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER))); | |
238 | } | |
239 | ||
5d7836c4 JS |
240 | /*! |
241 | * The style manager | |
242 | */ | |
243 | ||
244 | IMPLEMENT_CLASS(wxRichTextStyleSheet, wxObject) | |
245 | ||
38f833b1 JS |
246 | wxRichTextStyleSheet::~wxRichTextStyleSheet() |
247 | { | |
248 | DeleteStyles(); | |
249 | ||
250 | if (m_nextSheet) | |
251 | m_nextSheet->m_previousSheet = m_previousSheet; | |
41a85215 | 252 | |
38f833b1 JS |
253 | if (m_previousSheet) |
254 | m_previousSheet->m_nextSheet = m_nextSheet; | |
41a85215 | 255 | |
38f833b1 JS |
256 | m_previousSheet = NULL; |
257 | m_nextSheet = NULL; | |
258 | } | |
259 | ||
5d7836c4 JS |
260 | /// Initialisation |
261 | void wxRichTextStyleSheet::Init() | |
262 | { | |
38f833b1 JS |
263 | m_previousSheet = NULL; |
264 | m_nextSheet = NULL; | |
5d7836c4 JS |
265 | } |
266 | ||
267 | /// Add a definition to one of the style lists | |
268 | bool wxRichTextStyleSheet::AddStyle(wxList& list, wxRichTextStyleDefinition* def) | |
269 | { | |
270 | if (!list.Find(def)) | |
271 | list.Append(def); | |
272 | return true; | |
273 | } | |
274 | ||
275 | /// Remove a style | |
276 | bool wxRichTextStyleSheet::RemoveStyle(wxList& list, wxRichTextStyleDefinition* def, bool deleteStyle) | |
277 | { | |
09f14108 | 278 | wxList::compatibility_iterator node = list.Find(def); |
5d7836c4 JS |
279 | if (node) |
280 | { | |
281 | wxRichTextStyleDefinition* def = (wxRichTextStyleDefinition*) node->GetData(); | |
09f14108 | 282 | list.Erase(node); |
5d7836c4 JS |
283 | if (deleteStyle) |
284 | delete def; | |
285 | return true; | |
286 | } | |
287 | else | |
288 | return false; | |
289 | } | |
290 | ||
336d8ae9 VZ |
291 | /// Remove a style |
292 | bool wxRichTextStyleSheet::RemoveStyle(wxRichTextStyleDefinition* def, bool deleteStyle) | |
293 | { | |
294 | if (RemoveParagraphStyle(def, deleteStyle)) | |
295 | return true; | |
296 | if (RemoveCharacterStyle(def, deleteStyle)) | |
297 | return true; | |
298 | if (RemoveListStyle(def, deleteStyle)) | |
299 | return true; | |
300 | return false; | |
301 | } | |
302 | ||
5d7836c4 | 303 | /// Find a definition by name |
38f833b1 | 304 | wxRichTextStyleDefinition* wxRichTextStyleSheet::FindStyle(const wxList& list, const wxString& name, bool recurse) const |
5d7836c4 | 305 | { |
09f14108 | 306 | for (wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext()) |
5d7836c4 JS |
307 | { |
308 | wxRichTextStyleDefinition* def = (wxRichTextStyleDefinition*) node->GetData(); | |
309 | if (def->GetName().Lower() == name.Lower()) | |
310 | return def; | |
311 | } | |
41a85215 | 312 | |
38f833b1 JS |
313 | if (m_nextSheet && recurse) |
314 | return m_nextSheet->FindStyle(list, name, recurse); | |
315 | ||
61399247 | 316 | return NULL; |
5d7836c4 JS |
317 | } |
318 | ||
319 | /// Delete all styles | |
320 | void wxRichTextStyleSheet::DeleteStyles() | |
321 | { | |
322 | WX_CLEAR_LIST(wxList, m_characterStyleDefinitions); | |
323 | WX_CLEAR_LIST(wxList, m_paragraphStyleDefinitions); | |
38f833b1 JS |
324 | WX_CLEAR_LIST(wxList, m_listStyleDefinitions); |
325 | } | |
326 | ||
327 | /// Insert into list of style sheets | |
328 | bool wxRichTextStyleSheet::InsertSheet(wxRichTextStyleSheet* before) | |
329 | { | |
330 | m_previousSheet = before->m_previousSheet; | |
331 | m_nextSheet = before; | |
41a85215 | 332 | |
38f833b1 JS |
333 | before->m_previousSheet = this; |
334 | return true; | |
335 | } | |
336 | ||
337 | /// Append to list of style sheets | |
338 | bool wxRichTextStyleSheet::AppendSheet(wxRichTextStyleSheet* after) | |
339 | { | |
340 | wxRichTextStyleSheet* last = after; | |
341 | while (last && last->m_nextSheet) | |
342 | { | |
343 | last = last->m_nextSheet; | |
344 | } | |
41a85215 | 345 | |
38f833b1 JS |
346 | if (last) |
347 | { | |
348 | m_previousSheet = last; | |
349 | last->m_nextSheet = this; | |
41a85215 | 350 | |
38f833b1 JS |
351 | return true; |
352 | } | |
353 | else | |
354 | return false; | |
355 | } | |
356 | ||
357 | /// Unlink from the list of style sheets | |
358 | void wxRichTextStyleSheet::Unlink() | |
359 | { | |
360 | if (m_previousSheet) | |
361 | m_previousSheet->m_nextSheet = m_nextSheet; | |
362 | if (m_nextSheet) | |
363 | m_nextSheet->m_previousSheet = m_previousSheet; | |
41a85215 | 364 | |
38f833b1 JS |
365 | m_previousSheet = NULL; |
366 | m_nextSheet = NULL; | |
5d7836c4 JS |
367 | } |
368 | ||
fe5aa22c JS |
369 | /// Add a definition to the character style list |
370 | bool wxRichTextStyleSheet::AddCharacterStyle(wxRichTextCharacterStyleDefinition* def) | |
371 | { | |
372 | def->GetStyle().SetCharacterStyleName(def->GetName()); | |
373 | return AddStyle(m_characterStyleDefinitions, def); | |
374 | } | |
375 | ||
376 | /// Add a definition to the paragraph style list | |
377 | bool wxRichTextStyleSheet::AddParagraphStyle(wxRichTextParagraphStyleDefinition* def) | |
378 | { | |
379 | def->GetStyle().SetParagraphStyleName(def->GetName()); | |
380 | return AddStyle(m_paragraphStyleDefinitions, def); | |
381 | } | |
382 | ||
38f833b1 JS |
383 | /// Add a definition to the list style list |
384 | bool wxRichTextStyleSheet::AddListStyle(wxRichTextListStyleDefinition* def) | |
385 | { | |
386 | def->GetStyle().SetListStyleName(def->GetName()); | |
387 | return AddStyle(m_listStyleDefinitions, def); | |
388 | } | |
389 | ||
336d8ae9 VZ |
390 | /// Add a definition to the appropriate style list |
391 | bool wxRichTextStyleSheet::AddStyle(wxRichTextStyleDefinition* def) | |
392 | { | |
393 | wxRichTextListStyleDefinition* listDef = wxDynamicCast(def, wxRichTextListStyleDefinition); | |
394 | if (listDef) | |
395 | return AddListStyle(listDef); | |
396 | ||
397 | wxRichTextParagraphStyleDefinition* paraDef = wxDynamicCast(def, wxRichTextParagraphStyleDefinition); | |
398 | if (paraDef) | |
399 | return AddParagraphStyle(paraDef); | |
400 | ||
401 | wxRichTextCharacterStyleDefinition* charDef = wxDynamicCast(def, wxRichTextCharacterStyleDefinition); | |
402 | if (charDef) | |
403 | return AddCharacterStyle(charDef); | |
404 | ||
405 | return false; | |
406 | } | |
407 | ||
408 | /// Find any definition by name | |
409 | wxRichTextStyleDefinition* wxRichTextStyleSheet::FindStyle(const wxString& name, bool recurse) const | |
410 | { | |
411 | wxRichTextListStyleDefinition* listDef = FindListStyle(name, recurse); | |
412 | if (listDef) | |
413 | return listDef; | |
414 | ||
415 | wxRichTextParagraphStyleDefinition* paraDef = FindParagraphStyle(name, recurse); | |
416 | if (paraDef) | |
417 | return paraDef; | |
418 | ||
419 | wxRichTextCharacterStyleDefinition* charDef = FindCharacterStyle(name, recurse); | |
420 | if (charDef) | |
421 | return charDef; | |
422 | ||
423 | return NULL; | |
424 | } | |
425 | ||
fe5aa22c JS |
426 | /// Copy |
427 | void wxRichTextStyleSheet::Copy(const wxRichTextStyleSheet& sheet) | |
428 | { | |
429 | DeleteStyles(); | |
430 | ||
431 | wxList::compatibility_iterator node; | |
432 | ||
433 | for (node = sheet.m_characterStyleDefinitions.GetFirst(); node; node = node->GetNext()) | |
434 | { | |
435 | wxRichTextCharacterStyleDefinition* def = (wxRichTextCharacterStyleDefinition*) node->GetData(); | |
436 | AddCharacterStyle(new wxRichTextCharacterStyleDefinition(*def)); | |
437 | } | |
438 | ||
439 | for (node = sheet.m_paragraphStyleDefinitions.GetFirst(); node; node = node->GetNext()) | |
440 | { | |
441 | wxRichTextParagraphStyleDefinition* def = (wxRichTextParagraphStyleDefinition*) node->GetData(); | |
442 | AddParagraphStyle(new wxRichTextParagraphStyleDefinition(*def)); | |
443 | } | |
38f833b1 JS |
444 | |
445 | for (node = sheet.m_listStyleDefinitions.GetFirst(); node; node = node->GetNext()) | |
446 | { | |
447 | wxRichTextListStyleDefinition* def = (wxRichTextListStyleDefinition*) node->GetData(); | |
448 | AddListStyle(new wxRichTextListStyleDefinition(*def)); | |
449 | } | |
42688aea JS |
450 | |
451 | SetName(sheet.GetName()); | |
452 | SetDescription(sheet.GetDescription()); | |
fe5aa22c JS |
453 | } |
454 | ||
455 | /// Equality | |
456 | bool wxRichTextStyleSheet::operator==(const wxRichTextStyleSheet& WXUNUSED(sheet)) const | |
457 | { | |
458 | // TODO | |
459 | return false; | |
460 | } | |
461 | ||
462 | ||
5d7836c4 JS |
463 | #if wxUSE_HTML |
464 | /*! | |
38f833b1 | 465 | * wxRichTextStyleListBox: a listbox to display styles. |
5d7836c4 JS |
466 | */ |
467 | ||
468 | IMPLEMENT_CLASS(wxRichTextStyleListBox, wxHtmlListBox) | |
469 | ||
470 | BEGIN_EVENT_TABLE(wxRichTextStyleListBox, wxHtmlListBox) | |
5d7836c4 | 471 | EVT_LEFT_DOWN(wxRichTextStyleListBox::OnLeftDown) |
38f833b1 | 472 | EVT_LEFT_DCLICK(wxRichTextStyleListBox::OnLeftDoubleClick) |
e637208a | 473 | EVT_IDLE(wxRichTextStyleListBox::OnIdle) |
5d7836c4 JS |
474 | END_EVENT_TABLE() |
475 | ||
476 | wxRichTextStyleListBox::wxRichTextStyleListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos, | |
e637208a | 477 | const wxSize& size, long style) |
5d7836c4 | 478 | { |
e637208a JS |
479 | Init(); |
480 | Create(parent, id, pos, size, style); | |
481 | } | |
482 | ||
483 | bool wxRichTextStyleListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, | |
484 | const wxSize& size, long style) | |
485 | { | |
486 | return wxHtmlListBox::Create(parent, id, pos, size, style); | |
5d7836c4 JS |
487 | } |
488 | ||
489 | wxRichTextStyleListBox::~wxRichTextStyleListBox() | |
490 | { | |
491 | } | |
492 | ||
493 | /// Returns the HTML for this item | |
494 | wxString wxRichTextStyleListBox::OnGetItem(size_t n) const | |
495 | { | |
496 | if (!GetStyleSheet()) | |
497 | return wxEmptyString; | |
41a85215 | 498 | |
38f833b1 JS |
499 | wxRichTextStyleDefinition* def = GetStyle(n); |
500 | if (def) | |
501 | return CreateHTML(def); | |
5d7836c4 | 502 | |
5d7836c4 JS |
503 | return wxEmptyString; |
504 | } | |
505 | ||
506 | // Get style for index | |
507 | wxRichTextStyleDefinition* wxRichTextStyleListBox::GetStyle(size_t i) const | |
508 | { | |
509 | if (!GetStyleSheet()) | |
510 | return NULL; | |
511 | ||
38f833b1 JS |
512 | if (GetStyleType() == wxRICHTEXT_STYLE_ALL) |
513 | { | |
514 | // First paragraph styles, then character, then list | |
515 | if (i < GetStyleSheet()->GetParagraphStyleCount()) | |
516 | return GetStyleSheet()->GetParagraphStyle(i); | |
5d7836c4 | 517 | |
38f833b1 JS |
518 | if ((i - GetStyleSheet()->GetParagraphStyleCount()) < GetStyleSheet()->GetCharacterStyleCount()) |
519 | return GetStyleSheet()->GetCharacterStyle(i - GetStyleSheet()->GetParagraphStyleCount()); | |
520 | ||
521 | if ((i - GetStyleSheet()->GetParagraphStyleCount() - GetStyleSheet()->GetCharacterStyleCount()) < GetStyleSheet()->GetListStyleCount()) | |
522 | return GetStyleSheet()->GetListStyle(i - GetStyleSheet()->GetParagraphStyleCount() - GetStyleSheet()->GetCharacterStyleCount()); | |
523 | } | |
524 | else if ((GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH) && (i < GetStyleSheet()->GetParagraphStyleCount())) | |
525 | { | |
526 | return GetStyleSheet()->GetParagraphStyle(i); | |
527 | } | |
528 | else if ((GetStyleType() == wxRICHTEXT_STYLE_CHARACTER) && (i < GetStyleSheet()->GetCharacterStyleCount())) | |
529 | { | |
530 | return GetStyleSheet()->GetCharacterStyle(i); | |
531 | } | |
532 | else if ((GetStyleType() == wxRICHTEXT_STYLE_LIST) && (i < GetStyleSheet()->GetListStyleCount())) | |
533 | { | |
534 | return GetStyleSheet()->GetListStyle(i); | |
41a85215 | 535 | } |
5d7836c4 JS |
536 | |
537 | return NULL; | |
538 | } | |
539 | ||
540 | /// Updates the list | |
541 | void wxRichTextStyleListBox::UpdateStyles() | |
542 | { | |
543 | if (GetStyleSheet()) | |
544 | { | |
dadd4f55 | 545 | SetSelection(wxNOT_FOUND); |
41a85215 | 546 | |
38f833b1 JS |
547 | if (GetStyleType() == wxRICHTEXT_STYLE_ALL) |
548 | SetItemCount(GetStyleSheet()->GetParagraphStyleCount()+GetStyleSheet()->GetCharacterStyleCount()+GetStyleSheet()->GetListStyleCount()); | |
549 | else if (GetStyleType() == wxRICHTEXT_STYLE_PARAGRAPH) | |
550 | SetItemCount(GetStyleSheet()->GetParagraphStyleCount()); | |
551 | else if (GetStyleType() == wxRICHTEXT_STYLE_CHARACTER) | |
552 | SetItemCount(GetStyleSheet()->GetCharacterStyleCount()); | |
553 | else if (GetStyleType() == wxRICHTEXT_STYLE_LIST) | |
554 | SetItemCount(GetStyleSheet()->GetListStyleCount()); | |
41a85215 | 555 | |
5d7836c4 | 556 | Refresh(); |
dadd4f55 JS |
557 | |
558 | if (GetItemCount() > 0) | |
559 | { | |
560 | SetSelection(0); | |
561 | SendSelectedEvent(); | |
41a85215 | 562 | } |
5d7836c4 JS |
563 | } |
564 | } | |
565 | ||
e637208a JS |
566 | // Get index for style name |
567 | int wxRichTextStyleListBox::GetIndexForStyle(const wxString& name) const | |
568 | { | |
569 | if (GetStyleSheet()) | |
570 | { | |
38f833b1 JS |
571 | int count = GetItemCount(); |
572 | ||
e637208a | 573 | int i; |
38f833b1 | 574 | for (i = 0; i < (int) count; i++) |
e637208a | 575 | { |
38f833b1 | 576 | wxRichTextStyleDefinition* def = GetStyle(i); |
e637208a JS |
577 | if (def->GetName() == name) |
578 | return i; | |
579 | } | |
e637208a JS |
580 | } |
581 | return -1; | |
582 | } | |
583 | ||
584 | /// Set selection for string | |
585 | int wxRichTextStyleListBox::SetStyleSelection(const wxString& name) | |
586 | { | |
587 | int i = GetIndexForStyle(name); | |
588 | if (i > -1) | |
589 | SetSelection(i); | |
590 | return i; | |
591 | } | |
592 | ||
5d7836c4 JS |
593 | // Convert a colour to a 6-digit hex string |
594 | static wxString ColourToHexString(const wxColour& col) | |
595 | { | |
596 | wxString hex; | |
597 | ||
598 | hex += wxDecToHex(col.Red()); | |
599 | hex += wxDecToHex(col.Green()); | |
600 | hex += wxDecToHex(col.Blue()); | |
601 | ||
602 | return hex; | |
603 | } | |
604 | ||
605 | /// Creates a suitable HTML fragment for a definition | |
606 | wxString wxRichTextStyleListBox::CreateHTML(wxRichTextStyleDefinition* def) const | |
607 | { | |
38f833b1 JS |
608 | // TODO: indicate list format for list style types |
609 | ||
380a5dd8 JS |
610 | wxString str; |
611 | ||
612 | bool isCentred = false; | |
336d8ae9 VZ |
613 | |
614 | wxRichTextAttr attr(def->GetStyleMergedWithBase(GetStyleSheet())); | |
380a5dd8 | 615 | |
336d8ae9 | 616 | if (attr.HasAlignment() && attr.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE) |
380a5dd8 JS |
617 | isCentred = true; |
618 | ||
619 | if (isCentred) | |
620 | str << wxT("<center>"); | |
621 | ||
622 | ||
623 | str << wxT("<table><tr>"); | |
5d7836c4 | 624 | |
336d8ae9 | 625 | if (attr.GetLeftIndent() > 0) |
5d7836c4 JS |
626 | { |
627 | wxClientDC dc((wxWindow*) this); | |
628 | ||
336d8ae9 | 629 | str << wxT("<td width=") << wxMin(50, (ConvertTenthsMMToPixels(dc, attr.GetLeftIndent())/2)) << wxT("></td>"); |
5d7836c4 JS |
630 | } |
631 | ||
380a5dd8 JS |
632 | if (isCentred) |
633 | str << wxT("<td nowrap align=\"center\">"); | |
634 | else | |
635 | str << wxT("<td nowrap>"); | |
5d7836c4 | 636 | |
dadd4f55 JS |
637 | #ifdef __WXMSW__ |
638 | int size = 3; | |
639 | #else | |
38f833b1 | 640 | int size = 4; |
dadd4f55 JS |
641 | #endif |
642 | ||
643 | int stdFontSize = 12; | |
336d8ae9 | 644 | int thisFontSize = ((attr.GetFlags() & wxTEXT_ATTR_FONT_SIZE) != 0) ? attr.GetFontSize() : stdFontSize; |
5d7836c4 | 645 | |
dadd4f55 JS |
646 | if (thisFontSize < stdFontSize) |
647 | size ++; | |
648 | else if (thisFontSize > stdFontSize) | |
649 | size --; | |
5d7836c4 JS |
650 | |
651 | str += wxT("<font"); | |
652 | ||
653 | str << wxT(" size=") << size; | |
654 | ||
336d8ae9 VZ |
655 | if (!attr.GetFontFaceName().IsEmpty()) |
656 | str << wxT(" face=\"") << attr.GetFontFaceName() << wxT("\""); | |
5d7836c4 | 657 | |
336d8ae9 VZ |
658 | if (attr.GetTextColour().Ok()) |
659 | str << wxT(" color=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\""); | |
5d7836c4 JS |
660 | |
661 | str << wxT(">"); | |
662 | ||
663 | bool hasBold = false; | |
664 | bool hasItalic = false; | |
665 | bool hasUnderline = false; | |
666 | ||
336d8ae9 | 667 | if (attr.GetFontWeight() == wxBOLD) |
5d7836c4 | 668 | hasBold = true; |
336d8ae9 | 669 | if (attr.GetFontStyle() == wxITALIC) |
5d7836c4 | 670 | hasItalic = true; |
336d8ae9 | 671 | if (attr.GetFontUnderlined()) |
5d7836c4 JS |
672 | hasUnderline = true; |
673 | ||
674 | if (hasBold) | |
675 | str << wxT("<b>"); | |
676 | if (hasItalic) | |
677 | str << wxT("<i>"); | |
678 | if (hasUnderline) | |
679 | str << wxT("<u>"); | |
680 | ||
681 | str += def->GetName(); | |
682 | ||
683 | if (hasUnderline) | |
684 | str << wxT("</u>"); | |
685 | if (hasItalic) | |
686 | str << wxT("</i>"); | |
687 | if (hasBold) | |
688 | str << wxT("</b>"); | |
689 | ||
380a5dd8 JS |
690 | if (isCentred) |
691 | str << wxT("</centre>"); | |
692 | ||
5d7836c4 JS |
693 | str << wxT("</font>"); |
694 | ||
380a5dd8 JS |
695 | str << wxT("</td></tr></table>"); |
696 | ||
697 | if (isCentred) | |
698 | str << wxT("</center>"); | |
699 | ||
5d7836c4 JS |
700 | return str; |
701 | } | |
702 | ||
703 | // Convert units in tends of a millimetre to device units | |
704 | int wxRichTextStyleListBox::ConvertTenthsMMToPixels(wxDC& dc, int units) const | |
705 | { | |
706 | int ppi = dc.GetPPI().x; | |
707 | ||
708 | // There are ppi pixels in 254.1 "1/10 mm" | |
709 | ||
710 | double pixels = ((double) units * (double)ppi) / 254.1; | |
711 | ||
712 | return (int) pixels; | |
713 | } | |
714 | ||
5d7836c4 JS |
715 | void wxRichTextStyleListBox::OnLeftDown(wxMouseEvent& event) |
716 | { | |
717 | wxVListBox::OnLeftDown(event); | |
718 | ||
719 | int item = HitTest(event.GetPosition()); | |
86015e55 JS |
720 | if (item != wxNOT_FOUND && GetApplyOnSelection()) |
721 | ApplyStyle(item); | |
e637208a | 722 | } |
5d7836c4 | 723 | |
38f833b1 | 724 | void wxRichTextStyleListBox::OnLeftDoubleClick(wxMouseEvent& event) |
e637208a | 725 | { |
38f833b1 | 726 | wxVListBox::OnLeftDown(event); |
fe5aa22c | 727 | |
38f833b1 JS |
728 | int item = HitTest(event.GetPosition()); |
729 | if (item != wxNOT_FOUND && !GetApplyOnSelection()) | |
730 | ApplyStyle(item); | |
731 | } | |
e637208a | 732 | |
38f833b1 JS |
733 | /// Helper for listbox and combo control |
734 | wxString wxRichTextStyleListBox::GetStyleToShowInIdleTime(wxRichTextCtrl* ctrl, wxRichTextStyleType styleType) | |
735 | { | |
736 | int adjustedCaretPos = ctrl->GetAdjustedCaretPosition(ctrl->GetCaretPosition()); | |
e637208a | 737 | |
38f833b1 JS |
738 | wxRichTextParagraph* para = ctrl->GetBuffer().GetParagraphAtPosition(adjustedCaretPos); |
739 | wxRichTextObject* obj = ctrl->GetBuffer().GetLeafObjectAtPosition(adjustedCaretPos); | |
740 | ||
741 | wxString styleName; | |
742 | ||
743 | // Take into account current default style just chosen by user | |
744 | if (ctrl->IsDefaultStyleShowing()) | |
745 | { | |
746 | if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_CHARACTER) && | |
747 | !ctrl->GetDefaultStyleEx().GetCharacterStyleName().IsEmpty()) | |
748 | styleName = ctrl->GetDefaultStyleEx().GetCharacterStyleName(); | |
749 | else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_PARAGRAPH) && | |
750 | !ctrl->GetDefaultStyleEx().GetParagraphStyleName().IsEmpty()) | |
751 | styleName = ctrl->GetDefaultStyleEx().GetParagraphStyleName(); | |
752 | else if ((styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_LIST) && | |
753 | !ctrl->GetDefaultStyleEx().GetListStyleName().IsEmpty()) | |
754 | styleName = ctrl->GetDefaultStyleEx().GetListStyleName(); | |
755 | } | |
756 | else if (obj && (styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_CHARACTER) && | |
757 | !obj->GetAttributes().GetCharacterStyleName().IsEmpty()) | |
758 | { | |
759 | styleName = obj->GetAttributes().GetCharacterStyleName(); | |
760 | } | |
761 | else if (para && (styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_PARAGRAPH) && | |
762 | !para->GetAttributes().GetParagraphStyleName().IsEmpty()) | |
763 | { | |
764 | styleName = para->GetAttributes().GetParagraphStyleName(); | |
765 | } | |
766 | else if (para && (styleType == wxRICHTEXT_STYLE_ALL || styleType == wxRICHTEXT_STYLE_LIST) && | |
767 | !para->GetAttributes().GetListStyleName().IsEmpty()) | |
768 | { | |
769 | styleName = para->GetAttributes().GetListStyleName(); | |
770 | } | |
41a85215 | 771 | |
38f833b1 JS |
772 | return styleName; |
773 | } | |
774 | ||
775 | /// Auto-select from style under caret in idle time | |
776 | void wxRichTextStyleListBox::OnIdle(wxIdleEvent& event) | |
777 | { | |
778 | if (CanAutoSetSelection() && GetRichTextCtrl() && wxWindow::FindFocus() != this) | |
779 | { | |
780 | wxString styleName = GetStyleToShowInIdleTime(GetRichTextCtrl(), GetStyleType()); | |
61399247 | 781 | |
e637208a JS |
782 | int sel = GetSelection(); |
783 | if (!styleName.IsEmpty()) | |
784 | { | |
785 | // Don't do the selection if it's already set | |
786 | if (sel == GetIndexForStyle(styleName)) | |
787 | return; | |
5d7836c4 | 788 | |
e637208a JS |
789 | SetStyleSelection(styleName); |
790 | } | |
791 | else if (sel != -1) | |
792 | SetSelection(-1); | |
793 | } | |
794 | event.Skip(); | |
795 | } | |
5d7836c4 | 796 | |
e637208a | 797 | /// Do selection |
86015e55 | 798 | void wxRichTextStyleListBox::ApplyStyle(int item) |
e637208a JS |
799 | { |
800 | if ( item != wxNOT_FOUND ) | |
801 | { | |
802 | wxRichTextStyleDefinition* def = GetStyle(item); | |
803 | if (def && GetRichTextCtrl()) | |
804 | { | |
805 | GetRichTextCtrl()->ApplyStyle(def); | |
806 | GetRichTextCtrl()->SetFocus(); | |
5d7836c4 JS |
807 | } |
808 | } | |
809 | } | |
810 | ||
38f833b1 JS |
811 | /*! |
812 | * wxRichTextStyleListCtrl class: manages a listbox and a choice control to | |
813 | * switch shown style types | |
814 | */ | |
815 | ||
816 | IMPLEMENT_CLASS(wxRichTextStyleListCtrl, wxControl) | |
817 | ||
818 | BEGIN_EVENT_TABLE(wxRichTextStyleListCtrl, wxControl) | |
819 | EVT_CHOICE(wxID_ANY, wxRichTextStyleListCtrl::OnChooseType) | |
820 | EVT_SIZE(wxRichTextStyleListCtrl::OnSize) | |
821 | END_EVENT_TABLE() | |
822 | ||
823 | wxRichTextStyleListCtrl::wxRichTextStyleListCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, | |
824 | const wxSize& size, long style) | |
5d7836c4 | 825 | { |
38f833b1 JS |
826 | Init(); |
827 | Create(parent, id, pos, size, style); | |
5d7836c4 JS |
828 | } |
829 | ||
38f833b1 JS |
830 | bool wxRichTextStyleListCtrl::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, |
831 | const wxSize& size, long style) | |
5d7836c4 | 832 | { |
38f833b1 | 833 | wxControl::Create(parent, id, pos, size, style); |
41a85215 | 834 | |
38f833b1 | 835 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
dadd4f55 | 836 | if (size != wxDefaultSize) |
170acdc9 | 837 | SetInitialSize(size); |
41a85215 | 838 | |
dadd4f55 | 839 | bool showSelector = ((style & wxRICHTEXTSTYLELIST_HIDE_TYPE_SELECTOR) == 0); |
41a85215 | 840 | |
dadd4f55 JS |
841 | m_styleListBox = new wxRichTextStyleListBox(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, showSelector ? wxSIMPLE_BORDER : wxNO_BORDER); |
842 | ||
843 | wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL); | |
41a85215 | 844 | |
dadd4f55 | 845 | if (showSelector) |
41a85215 | 846 | { |
dadd4f55 JS |
847 | wxArrayString choices; |
848 | choices.Add(_("All styles")); | |
849 | choices.Add(_("Paragraph styles")); | |
850 | choices.Add(_("Character styles")); | |
851 | choices.Add(_("List styles")); | |
41a85215 | 852 | |
dadd4f55 | 853 | m_styleChoice = new wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, choices); |
41a85215 | 854 | |
dadd4f55 JS |
855 | boxSizer->Add(m_styleListBox, 1, wxALL|wxEXPAND, 5); |
856 | boxSizer->Add(m_styleChoice, 0, wxALL|wxEXPAND, 5); | |
857 | } | |
858 | else | |
859 | { | |
860 | boxSizer->Add(m_styleListBox, 1, wxALL|wxEXPAND, 0); | |
861 | } | |
41a85215 | 862 | |
dadd4f55 JS |
863 | SetSizer(boxSizer); |
864 | Layout(); | |
38f833b1 JS |
865 | |
866 | m_dontUpdate = true; | |
dadd4f55 JS |
867 | |
868 | if (m_styleChoice) | |
41a85215 | 869 | { |
dadd4f55 JS |
870 | int i = StyleTypeToIndex(m_styleListBox->GetStyleType()); |
871 | m_styleChoice->SetSelection(i); | |
872 | } | |
41a85215 | 873 | |
38f833b1 | 874 | m_dontUpdate = false; |
41a85215 | 875 | |
38f833b1 JS |
876 | return true; |
877 | } | |
878 | ||
879 | wxRichTextStyleListCtrl::~wxRichTextStyleListCtrl() | |
880 | { | |
41a85215 | 881 | |
38f833b1 JS |
882 | } |
883 | ||
884 | /// React to style type choice | |
885 | void wxRichTextStyleListCtrl::OnChooseType(wxCommandEvent& event) | |
886 | { | |
887 | if (event.GetEventObject() != m_styleChoice) | |
888 | event.Skip(); | |
889 | else | |
890 | { | |
891 | if (m_dontUpdate) | |
892 | return; | |
41a85215 | 893 | |
38f833b1 JS |
894 | wxRichTextStyleListBox::wxRichTextStyleType styleType = StyleIndexToType(event.GetSelection()); |
895 | m_styleListBox->SetStyleType(styleType); | |
896 | } | |
897 | } | |
898 | ||
899 | /// Lay out the controls | |
900 | void wxRichTextStyleListCtrl::OnSize(wxSizeEvent& WXUNUSED(event)) | |
901 | { | |
902 | if (GetAutoLayout()) | |
903 | Layout(); | |
904 | } | |
905 | ||
906 | /// Get the choice index for style type | |
907 | int wxRichTextStyleListCtrl::StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType) | |
908 | { | |
909 | if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL) | |
910 | { | |
911 | return 0; | |
912 | } | |
913 | else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH) | |
914 | { | |
915 | return 1; | |
916 | } | |
917 | else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER) | |
918 | { | |
919 | return 2; | |
920 | } | |
921 | else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST) | |
922 | { | |
923 | return 3; | |
924 | } | |
925 | return 0; | |
926 | } | |
927 | ||
928 | /// Get the style type for choice index | |
929 | wxRichTextStyleListBox::wxRichTextStyleType wxRichTextStyleListCtrl::StyleIndexToType(int i) | |
930 | { | |
931 | if (i == 1) | |
932 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH; | |
933 | else if (i == 2) | |
934 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER; | |
935 | else if (i == 3) | |
936 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST; | |
937 | ||
938 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL; | |
939 | } | |
940 | ||
941 | /// Associates the control with a style manager | |
942 | void wxRichTextStyleListCtrl::SetStyleSheet(wxRichTextStyleSheet* styleSheet) | |
943 | { | |
944 | if (m_styleListBox) | |
945 | m_styleListBox->SetStyleSheet(styleSheet); | |
946 | } | |
947 | ||
948 | wxRichTextStyleSheet* wxRichTextStyleListCtrl::GetStyleSheet() const | |
949 | { | |
950 | if (m_styleListBox) | |
951 | return m_styleListBox->GetStyleSheet(); | |
952 | else | |
953 | return NULL; | |
954 | } | |
955 | ||
956 | /// Associates the control with a wxRichTextCtrl | |
957 | void wxRichTextStyleListCtrl::SetRichTextCtrl(wxRichTextCtrl* ctrl) | |
958 | { | |
959 | if (m_styleListBox) | |
960 | m_styleListBox->SetRichTextCtrl(ctrl); | |
961 | } | |
962 | ||
963 | wxRichTextCtrl* wxRichTextStyleListCtrl::GetRichTextCtrl() const | |
964 | { | |
965 | if (m_styleListBox) | |
966 | return m_styleListBox->GetRichTextCtrl(); | |
967 | else | |
968 | return NULL; | |
969 | } | |
970 | ||
971 | /// Set/get the style type to display | |
972 | void wxRichTextStyleListCtrl::SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType) | |
973 | { | |
974 | if (m_styleListBox) | |
975 | m_styleListBox->SetStyleType(styleType); | |
976 | ||
977 | m_dontUpdate = true; | |
978 | ||
979 | if (m_styleChoice) | |
41a85215 | 980 | { |
38f833b1 JS |
981 | int i = StyleTypeToIndex(m_styleListBox->GetStyleType()); |
982 | m_styleChoice->SetSelection(i); | |
983 | } | |
41a85215 | 984 | |
38f833b1 JS |
985 | m_dontUpdate = false; |
986 | } | |
987 | ||
988 | wxRichTextStyleListBox::wxRichTextStyleType wxRichTextStyleListCtrl::GetStyleType() const | |
989 | { | |
990 | if (m_styleListBox) | |
991 | return m_styleListBox->GetStyleType(); | |
992 | else | |
993 | return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL; | |
994 | } | |
995 | ||
996 | /// Updates the style list box | |
997 | void wxRichTextStyleListCtrl::UpdateStyles() | |
998 | { | |
999 | if (m_styleListBox) | |
41a85215 | 1000 | m_styleListBox->UpdateStyles(); |
5d7836c4 | 1001 | } |
5d7836c4 | 1002 | |
e637208a JS |
1003 | #if wxUSE_COMBOCTRL |
1004 | ||
1005 | /*! | |
1006 | * Style drop-down for a wxComboCtrl | |
1007 | */ | |
1008 | ||
1009 | ||
1010 | BEGIN_EVENT_TABLE(wxRichTextStyleComboPopup, wxRichTextStyleListBox) | |
1011 | EVT_MOTION(wxRichTextStyleComboPopup::OnMouseMove) | |
1012 | EVT_LEFT_DOWN(wxRichTextStyleComboPopup::OnMouseClick) | |
1013 | END_EVENT_TABLE() | |
1014 | ||
1015 | void wxRichTextStyleComboPopup::SetStringValue( const wxString& s ) | |
1016 | { | |
1017 | m_value = SetStyleSelection(s); | |
1018 | } | |
1019 | ||
1020 | wxString wxRichTextStyleComboPopup::GetStringValue() const | |
1021 | { | |
1022 | int sel = m_value; | |
1023 | if (sel > -1) | |
1024 | { | |
1025 | wxRichTextStyleDefinition* def = GetStyle(sel); | |
1026 | if (def) | |
1027 | return def->GetName(); | |
1028 | } | |
1029 | return wxEmptyString; | |
1030 | } | |
1031 | ||
1032 | // | |
1033 | // Popup event handlers | |
1034 | // | |
1035 | ||
1036 | // Mouse hot-tracking | |
1037 | void wxRichTextStyleComboPopup::OnMouseMove(wxMouseEvent& event) | |
1038 | { | |
1039 | // Move selection to cursor if it is inside the popup | |
1040 | ||
1041 | int itemHere = wxRichTextStyleListBox::HitTest(event.GetPosition()); | |
1042 | if ( itemHere >= 0 ) | |
1043 | { | |
1044 | wxRichTextStyleListBox::SetSelection(itemHere); | |
1045 | m_itemHere = itemHere; | |
1046 | } | |
1047 | event.Skip(); | |
1048 | } | |
1049 | ||
1050 | // On mouse left, set the value and close the popup | |
1051 | void wxRichTextStyleComboPopup::OnMouseClick(wxMouseEvent& WXUNUSED(event)) | |
1052 | { | |
1053 | if (m_itemHere >= 0) | |
1054 | m_value = m_itemHere; | |
1055 | ||
1056 | // Ordering is important, so we don't dismiss this popup accidentally | |
86015e55 | 1057 | // by setting the focus elsewhere e.g. in ApplyStyle |
e637208a JS |
1058 | Dismiss(); |
1059 | ||
1060 | if (m_itemHere >= 0) | |
86015e55 | 1061 | wxRichTextStyleListBox::ApplyStyle(m_itemHere); |
e637208a JS |
1062 | } |
1063 | ||
1064 | /*! | |
1065 | * wxRichTextStyleComboCtrl | |
1066 | * A combo for applying styles. | |
1067 | */ | |
1068 | ||
1069 | IMPLEMENT_CLASS(wxRichTextStyleComboCtrl, wxComboCtrl) | |
1070 | ||
1071 | BEGIN_EVENT_TABLE(wxRichTextStyleComboCtrl, wxComboCtrl) | |
1072 | EVT_IDLE(wxRichTextStyleComboCtrl::OnIdle) | |
1073 | END_EVENT_TABLE() | |
1074 | ||
1075 | bool wxRichTextStyleComboCtrl::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, | |
1076 | const wxSize& size, long style) | |
1077 | { | |
1078 | if (!wxComboCtrl::Create(parent, id, wxEmptyString, pos, size, style)) | |
1079 | return false; | |
1080 | ||
1081 | SetPopupMaxHeight(400); | |
1082 | ||
1083 | m_stylePopup = new wxRichTextStyleComboPopup; | |
1084 | ||
1085 | SetPopupControl(m_stylePopup); | |
1086 | ||
1087 | return true; | |
1088 | } | |
1089 | ||
1090 | /// Auto-select from style under caret in idle time | |
1091 | ||
1092 | // TODO: must be able to show italic, bold, combinations | |
1093 | // in style box. Do we have a concept of automatic, temporary | |
1094 | // styles that are added whenever we wish to show a style | |
1095 | // that doesn't exist already? E.g. "Bold, Italic, Underline". | |
1096 | // Word seems to generate these things on the fly. | |
1097 | // If there's a named style already, it uses e.g. Heading1 + Bold, Italic | |
1098 | // If you unembolden text in a style that has bold, it uses the | |
1099 | // term "Not bold". | |
1100 | // TODO: order styles alphabetically. This means indexes can change, | |
1101 | // so need a different way to specify selections, i.e. by name. | |
1102 | ||
1103 | void wxRichTextStyleComboCtrl::OnIdle(wxIdleEvent& event) | |
1104 | { | |
38f833b1 | 1105 | if (GetRichTextCtrl() && !IsPopupShown() && m_stylePopup && wxWindow::FindFocus() != this) |
e637208a | 1106 | { |
38f833b1 | 1107 | wxString styleName = wxRichTextStyleListBox::GetStyleToShowInIdleTime(GetRichTextCtrl(), m_stylePopup->GetStyleType()); |
41a85215 | 1108 | |
e637208a JS |
1109 | wxString currentValue = GetValue(); |
1110 | if (!styleName.IsEmpty()) | |
1111 | { | |
1112 | // Don't do the selection if it's already set | |
1113 | if (currentValue == styleName) | |
1114 | return; | |
1115 | ||
1116 | SetValue(styleName); | |
1117 | } | |
1118 | else if (!currentValue.IsEmpty()) | |
1119 | SetValue(wxEmptyString); | |
1120 | } | |
1121 | event.Skip(); | |
1122 | } | |
1123 | ||
1124 | #endif | |
1125 | // wxUSE_COMBOCTRL | |
1126 | ||
5d7836c4 JS |
1127 | #endif |
1128 | // wxUSE_HTML | |
1129 | ||
1130 | #endif | |
1131 | // wxUSE_RICHTEXT |