]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/datavcmn.cpp | |
3 | // Purpose: wxDataViewCtrl base classes and common parts | |
4 | // Author: Robert Roebling | |
5 | // Created: 2006/02/20 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2006, Robert Roebling | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #if wxUSE_DATAVIEWCTRL | |
19 | ||
20 | #include "wx/dataview.h" | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/dc.h" | |
24 | #include "wx/settings.h" | |
25 | #include "wx/log.h" | |
26 | #include "wx/crt.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/datectrl.h" | |
30 | #include "wx/spinctrl.h" | |
31 | #include "wx/choice.h" | |
32 | #include "wx/imaglist.h" | |
33 | ||
34 | const char wxDataViewCtrlNameStr[] = "dataviewCtrl"; | |
35 | ||
36 | namespace | |
37 | { | |
38 | ||
39 | // Custom handler pushed on top of the edit control used by wxDataViewCtrl to | |
40 | // forward some events to the main control itself. | |
41 | class wxDataViewEditorCtrlEvtHandler: public wxEvtHandler | |
42 | { | |
43 | public: | |
44 | wxDataViewEditorCtrlEvtHandler(wxWindow *editor, wxDataViewRenderer *owner) | |
45 | { | |
46 | m_editorCtrl = editor; | |
47 | m_owner = owner; | |
48 | ||
49 | m_finished = false; | |
50 | } | |
51 | ||
52 | void AcceptChangesAndFinish(); | |
53 | void SetFocusOnIdle( bool focus = true ) { m_focusOnIdle = focus; } | |
54 | ||
55 | protected: | |
56 | void OnChar( wxKeyEvent &event ); | |
57 | void OnTextEnter( wxCommandEvent &event ); | |
58 | void OnKillFocus( wxFocusEvent &event ); | |
59 | void OnIdle( wxIdleEvent &event ); | |
60 | ||
61 | private: | |
62 | wxDataViewRenderer *m_owner; | |
63 | wxWindow *m_editorCtrl; | |
64 | bool m_finished; | |
65 | bool m_focusOnIdle; | |
66 | ||
67 | private: | |
68 | DECLARE_EVENT_TABLE() | |
69 | }; | |
70 | ||
71 | } // anonymous namespace | |
72 | ||
73 | // --------------------------------------------------------- | |
74 | // wxDataViewItemAttr | |
75 | // --------------------------------------------------------- | |
76 | ||
77 | wxFont wxDataViewItemAttr::GetEffectiveFont(const wxFont& font) const | |
78 | { | |
79 | if ( !HasFont() ) | |
80 | return font; | |
81 | ||
82 | wxFont f(font); | |
83 | if ( GetBold() ) | |
84 | f.MakeBold(); | |
85 | if ( GetItalic() ) | |
86 | f.MakeItalic(); | |
87 | return f; | |
88 | } | |
89 | ||
90 | ||
91 | // --------------------------------------------------------- | |
92 | // wxDataViewModelNotifier | |
93 | // --------------------------------------------------------- | |
94 | ||
95 | #include "wx/listimpl.cpp" | |
96 | WX_DEFINE_LIST(wxDataViewModelNotifiers) | |
97 | ||
98 | bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items ) | |
99 | { | |
100 | size_t count = items.GetCount(); | |
101 | size_t i; | |
102 | for (i = 0; i < count; i++) | |
103 | if (!ItemAdded( parent, items[i] )) return false; | |
104 | ||
105 | return true; | |
106 | } | |
107 | ||
108 | bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items ) | |
109 | { | |
110 | size_t count = items.GetCount(); | |
111 | size_t i; | |
112 | for (i = 0; i < count; i++) | |
113 | if (!ItemDeleted( parent, items[i] )) return false; | |
114 | ||
115 | return true; | |
116 | } | |
117 | ||
118 | bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray &items ) | |
119 | { | |
120 | size_t count = items.GetCount(); | |
121 | size_t i; | |
122 | for (i = 0; i < count; i++) | |
123 | if (!ItemChanged( items[i] )) return false; | |
124 | ||
125 | return true; | |
126 | } | |
127 | ||
128 | // --------------------------------------------------------- | |
129 | // wxDataViewModel | |
130 | // --------------------------------------------------------- | |
131 | ||
132 | wxDataViewModel::wxDataViewModel() | |
133 | { | |
134 | m_notifiers.DeleteContents( true ); | |
135 | } | |
136 | ||
137 | bool wxDataViewModel::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item ) | |
138 | { | |
139 | bool ret = true; | |
140 | ||
141 | wxDataViewModelNotifiers::iterator iter; | |
142 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
143 | { | |
144 | wxDataViewModelNotifier* notifier = *iter; | |
145 | if (!notifier->ItemAdded( parent, item )) | |
146 | ret = false; | |
147 | } | |
148 | ||
149 | return ret; | |
150 | } | |
151 | ||
152 | bool wxDataViewModel::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item ) | |
153 | { | |
154 | bool ret = true; | |
155 | ||
156 | wxDataViewModelNotifiers::iterator iter; | |
157 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
158 | { | |
159 | wxDataViewModelNotifier* notifier = *iter; | |
160 | if (!notifier->ItemDeleted( parent, item )) | |
161 | ret = false; | |
162 | } | |
163 | ||
164 | return ret; | |
165 | } | |
166 | ||
167 | bool wxDataViewModel::ItemChanged( const wxDataViewItem &item ) | |
168 | { | |
169 | bool ret = true; | |
170 | ||
171 | wxDataViewModelNotifiers::iterator iter; | |
172 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
173 | { | |
174 | wxDataViewModelNotifier* notifier = *iter; | |
175 | if (!notifier->ItemChanged( item )) | |
176 | ret = false; | |
177 | } | |
178 | ||
179 | return ret; | |
180 | } | |
181 | ||
182 | bool wxDataViewModel::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items ) | |
183 | { | |
184 | bool ret = true; | |
185 | ||
186 | wxDataViewModelNotifiers::iterator iter; | |
187 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
188 | { | |
189 | wxDataViewModelNotifier* notifier = *iter; | |
190 | if (!notifier->ItemsAdded( parent, items )) | |
191 | ret = false; | |
192 | } | |
193 | ||
194 | return ret; | |
195 | } | |
196 | ||
197 | bool wxDataViewModel::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items ) | |
198 | { | |
199 | bool ret = true; | |
200 | ||
201 | wxDataViewModelNotifiers::iterator iter; | |
202 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
203 | { | |
204 | wxDataViewModelNotifier* notifier = *iter; | |
205 | if (!notifier->ItemsDeleted( parent, items )) | |
206 | ret = false; | |
207 | } | |
208 | ||
209 | return ret; | |
210 | } | |
211 | ||
212 | bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray &items ) | |
213 | { | |
214 | bool ret = true; | |
215 | ||
216 | wxDataViewModelNotifiers::iterator iter; | |
217 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
218 | { | |
219 | wxDataViewModelNotifier* notifier = *iter; | |
220 | if (!notifier->ItemsChanged( items )) | |
221 | ret = false; | |
222 | } | |
223 | ||
224 | return ret; | |
225 | } | |
226 | ||
227 | bool wxDataViewModel::ValueChanged( const wxDataViewItem &item, unsigned int col ) | |
228 | { | |
229 | bool ret = true; | |
230 | ||
231 | wxDataViewModelNotifiers::iterator iter; | |
232 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
233 | { | |
234 | wxDataViewModelNotifier* notifier = *iter; | |
235 | if (!notifier->ValueChanged( item, col )) | |
236 | ret = false; | |
237 | } | |
238 | ||
239 | return ret; | |
240 | } | |
241 | ||
242 | bool wxDataViewModel::Cleared() | |
243 | { | |
244 | bool ret = true; | |
245 | ||
246 | wxDataViewModelNotifiers::iterator iter; | |
247 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
248 | { | |
249 | wxDataViewModelNotifier* notifier = *iter; | |
250 | if (!notifier->Cleared()) | |
251 | ret = false; | |
252 | } | |
253 | ||
254 | return ret; | |
255 | } | |
256 | ||
257 | bool wxDataViewModel::BeforeReset() | |
258 | { | |
259 | bool ret = true; | |
260 | ||
261 | wxDataViewModelNotifiers::iterator iter; | |
262 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
263 | { | |
264 | wxDataViewModelNotifier* notifier = *iter; | |
265 | if (!notifier->BeforeReset()) | |
266 | ret = false; | |
267 | } | |
268 | ||
269 | return ret; | |
270 | } | |
271 | ||
272 | bool wxDataViewModel::AfterReset() | |
273 | { | |
274 | bool ret = true; | |
275 | ||
276 | wxDataViewModelNotifiers::iterator iter; | |
277 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
278 | { | |
279 | wxDataViewModelNotifier* notifier = *iter; | |
280 | if (!notifier->AfterReset()) | |
281 | ret = false; | |
282 | } | |
283 | ||
284 | return ret; | |
285 | } | |
286 | ||
287 | void wxDataViewModel::Resort() | |
288 | { | |
289 | wxDataViewModelNotifiers::iterator iter; | |
290 | for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter) | |
291 | { | |
292 | wxDataViewModelNotifier* notifier = *iter; | |
293 | notifier->Resort(); | |
294 | } | |
295 | } | |
296 | ||
297 | void wxDataViewModel::AddNotifier( wxDataViewModelNotifier *notifier ) | |
298 | { | |
299 | m_notifiers.push_back( notifier ); | |
300 | notifier->SetOwner( this ); | |
301 | } | |
302 | ||
303 | void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier *notifier ) | |
304 | { | |
305 | m_notifiers.DeleteObject( notifier ); | |
306 | } | |
307 | ||
308 | int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, | |
309 | unsigned int column, bool ascending ) const | |
310 | { | |
311 | // sort branches before leaves | |
312 | bool item1_is_container = IsContainer(item1); | |
313 | bool item2_is_container = IsContainer(item2); | |
314 | ||
315 | if (item1_is_container && !item2_is_container) | |
316 | return 1; | |
317 | if (item2_is_container && !item1_is_container) | |
318 | return -1; | |
319 | ||
320 | wxVariant value1,value2; | |
321 | GetValue( value1, item1, column ); | |
322 | GetValue( value2, item2, column ); | |
323 | ||
324 | if (!ascending) | |
325 | { | |
326 | wxVariant temp = value1; | |
327 | value1 = value2; | |
328 | value2 = temp; | |
329 | } | |
330 | ||
331 | if (value1.GetType() == wxT("string")) | |
332 | { | |
333 | wxString str1 = value1.GetString(); | |
334 | wxString str2 = value2.GetString(); | |
335 | int res = str1.Cmp( str2 ); | |
336 | if (res) | |
337 | return res; | |
338 | } | |
339 | else if (value1.GetType() == wxT("long")) | |
340 | { | |
341 | long l1 = value1.GetLong(); | |
342 | long l2 = value2.GetLong(); | |
343 | long res = l1-l2; | |
344 | if (res) | |
345 | return res; | |
346 | } | |
347 | else if (value1.GetType() == wxT("double")) | |
348 | { | |
349 | double d1 = value1.GetDouble(); | |
350 | double d2 = value2.GetDouble(); | |
351 | if (d1 < d2) | |
352 | return 1; | |
353 | if (d1 > d2) | |
354 | return -1; | |
355 | } | |
356 | else if (value1.GetType() == wxT("datetime")) | |
357 | { | |
358 | wxDateTime dt1 = value1.GetDateTime(); | |
359 | wxDateTime dt2 = value2.GetDateTime(); | |
360 | if (dt1.IsEarlierThan(dt2)) | |
361 | return 1; | |
362 | if (dt2.IsEarlierThan(dt1)) | |
363 | return -1; | |
364 | } | |
365 | ||
366 | // items must be different | |
367 | wxUIntPtr id1 = wxPtrToUInt(item1.GetID()), | |
368 | id2 = wxPtrToUInt(item2.GetID()); | |
369 | ||
370 | return ascending ? id1 - id2 : id2 - id1; | |
371 | } | |
372 | ||
373 | // --------------------------------------------------------- | |
374 | // wxDataViewIndexListModel | |
375 | // --------------------------------------------------------- | |
376 | ||
377 | static int my_sort( int *v1, int *v2 ) | |
378 | { | |
379 | return *v2-*v1; | |
380 | } | |
381 | ||
382 | ||
383 | wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size ) | |
384 | { | |
385 | // IDs are ordered until an item gets deleted or inserted | |
386 | m_ordered = true; | |
387 | ||
388 | // build initial index | |
389 | unsigned int i; | |
390 | for (i = 1; i < initial_size+1; i++) | |
391 | m_hash.Add( wxDataViewItem(wxUIntToPtr(i)) ); | |
392 | m_nextFreeID = initial_size + 1; | |
393 | } | |
394 | ||
395 | void wxDataViewIndexListModel::Reset( unsigned int new_size ) | |
396 | { | |
397 | /* wxDataViewModel:: */ BeforeReset(); | |
398 | ||
399 | m_hash.Clear(); | |
400 | ||
401 | // IDs are ordered until an item gets deleted or inserted | |
402 | m_ordered = true; | |
403 | ||
404 | // build initial index | |
405 | unsigned int i; | |
406 | for (i = 1; i < new_size+1; i++) | |
407 | m_hash.Add( wxDataViewItem(wxUIntToPtr(i)) ); | |
408 | ||
409 | m_nextFreeID = new_size + 1; | |
410 | ||
411 | /* wxDataViewModel:: */ AfterReset(); | |
412 | } | |
413 | ||
414 | void wxDataViewIndexListModel::RowPrepended() | |
415 | { | |
416 | m_ordered = false; | |
417 | ||
418 | unsigned int id = m_nextFreeID; | |
419 | m_nextFreeID++; | |
420 | ||
421 | wxDataViewItem item( wxUIntToPtr(id) ); | |
422 | m_hash.Insert( item, 0 ); | |
423 | ItemAdded( wxDataViewItem(0), item ); | |
424 | ||
425 | } | |
426 | ||
427 | void wxDataViewIndexListModel::RowInserted( unsigned int before ) | |
428 | { | |
429 | m_ordered = false; | |
430 | ||
431 | unsigned int id = m_nextFreeID; | |
432 | m_nextFreeID++; | |
433 | ||
434 | wxDataViewItem item( wxUIntToPtr(id) ); | |
435 | m_hash.Insert( item, before ); | |
436 | ItemAdded( wxDataViewItem(0), item ); | |
437 | } | |
438 | ||
439 | void wxDataViewIndexListModel::RowAppended() | |
440 | { | |
441 | unsigned int id = m_nextFreeID; | |
442 | m_nextFreeID++; | |
443 | ||
444 | wxDataViewItem item( wxUIntToPtr(id) ); | |
445 | m_hash.Add( item ); | |
446 | ItemAdded( wxDataViewItem(0), item ); | |
447 | } | |
448 | ||
449 | void wxDataViewIndexListModel::RowDeleted( unsigned int row ) | |
450 | { | |
451 | m_ordered = false; | |
452 | ||
453 | wxDataViewItem item( m_hash[row] ); | |
454 | m_hash.RemoveAt( row ); | |
455 | /* wxDataViewModel:: */ ItemDeleted( wxDataViewItem(0), item ); | |
456 | } | |
457 | ||
458 | void wxDataViewIndexListModel::RowsDeleted( const wxArrayInt &rows ) | |
459 | { | |
460 | m_ordered = false; | |
461 | ||
462 | wxDataViewItemArray array; | |
463 | unsigned int i; | |
464 | for (i = 0; i < rows.GetCount(); i++) | |
465 | { | |
466 | wxDataViewItem item( m_hash[rows[i]] ); | |
467 | array.Add( item ); | |
468 | } | |
469 | ||
470 | wxArrayInt sorted = rows; | |
471 | sorted.Sort( my_sort ); | |
472 | for (i = 0; i < sorted.GetCount(); i++) | |
473 | m_hash.RemoveAt( sorted[i] ); | |
474 | ||
475 | /* wxDataViewModel:: */ ItemsDeleted( wxDataViewItem(0), array ); | |
476 | } | |
477 | ||
478 | void wxDataViewIndexListModel::RowChanged( unsigned int row ) | |
479 | { | |
480 | /* wxDataViewModel:: */ ItemChanged( GetItem(row) ); | |
481 | } | |
482 | ||
483 | void wxDataViewIndexListModel::RowValueChanged( unsigned int row, unsigned int col ) | |
484 | { | |
485 | /* wxDataViewModel:: */ ValueChanged( GetItem(row), col ); | |
486 | } | |
487 | ||
488 | unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem &item ) const | |
489 | { | |
490 | if (m_ordered) | |
491 | return wxPtrToUInt(item.GetID())-1; | |
492 | ||
493 | // assert for not found | |
494 | return (unsigned int) m_hash.Index( item ); | |
495 | } | |
496 | ||
497 | wxDataViewItem wxDataViewIndexListModel::GetItem( unsigned int row ) const | |
498 | { | |
499 | wxASSERT( row < m_hash.GetCount() ); | |
500 | return wxDataViewItem( m_hash[row] ); | |
501 | } | |
502 | ||
503 | bool wxDataViewIndexListModel::HasDefaultCompare() const | |
504 | { | |
505 | return !m_ordered; | |
506 | } | |
507 | ||
508 | int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1, | |
509 | const wxDataViewItem& item2, | |
510 | unsigned int WXUNUSED(column), | |
511 | bool ascending) const | |
512 | { | |
513 | if (m_ordered) | |
514 | { | |
515 | unsigned int pos1 = wxPtrToUInt(item1.GetID()); // -1 not needed here | |
516 | unsigned int pos2 = wxPtrToUInt(item2.GetID()); // -1 not needed here | |
517 | ||
518 | if (ascending) | |
519 | return pos1 - pos2; | |
520 | else | |
521 | return pos2 - pos1; | |
522 | } | |
523 | ||
524 | if (ascending) | |
525 | return GetRow(item1) - GetRow(item2); | |
526 | ||
527 | return GetRow(item2) - GetRow(item1); | |
528 | } | |
529 | ||
530 | unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const | |
531 | { | |
532 | if (item.IsOk()) | |
533 | return 0; | |
534 | ||
535 | children = m_hash; | |
536 | ||
537 | return m_hash.GetCount(); | |
538 | } | |
539 | ||
540 | // --------------------------------------------------------- | |
541 | // wxDataViewVirtualListModel | |
542 | // --------------------------------------------------------- | |
543 | ||
544 | #ifndef __WXMAC__ | |
545 | ||
546 | wxDataViewVirtualListModel::wxDataViewVirtualListModel( unsigned int initial_size ) | |
547 | { | |
548 | m_size = initial_size; | |
549 | } | |
550 | ||
551 | void wxDataViewVirtualListModel::Reset( unsigned int new_size ) | |
552 | { | |
553 | /* wxDataViewModel:: */ BeforeReset(); | |
554 | ||
555 | m_size = new_size; | |
556 | ||
557 | /* wxDataViewModel:: */ AfterReset(); | |
558 | } | |
559 | ||
560 | void wxDataViewVirtualListModel::RowPrepended() | |
561 | { | |
562 | m_size++; | |
563 | wxDataViewItem item( wxUIntToPtr(1) ); | |
564 | ItemAdded( wxDataViewItem(0), item ); | |
565 | } | |
566 | ||
567 | void wxDataViewVirtualListModel::RowInserted( unsigned int before ) | |
568 | { | |
569 | m_size++; | |
570 | wxDataViewItem item( wxUIntToPtr(before+1) ); | |
571 | ItemAdded( wxDataViewItem(0), item ); | |
572 | } | |
573 | ||
574 | void wxDataViewVirtualListModel::RowAppended() | |
575 | { | |
576 | m_size++; | |
577 | wxDataViewItem item( wxUIntToPtr(m_size) ); | |
578 | ItemAdded( wxDataViewItem(0), item ); | |
579 | } | |
580 | ||
581 | void wxDataViewVirtualListModel::RowDeleted( unsigned int row ) | |
582 | { | |
583 | m_size--; | |
584 | wxDataViewItem item( wxUIntToPtr(row+1) ); | |
585 | /* wxDataViewModel:: */ ItemDeleted( wxDataViewItem(0), item ); | |
586 | } | |
587 | ||
588 | void wxDataViewVirtualListModel::RowsDeleted( const wxArrayInt &rows ) | |
589 | { | |
590 | m_size -= rows.GetCount(); | |
591 | ||
592 | wxArrayInt sorted = rows; | |
593 | sorted.Sort( my_sort ); | |
594 | ||
595 | wxDataViewItemArray array; | |
596 | unsigned int i; | |
597 | for (i = 0; i < sorted.GetCount(); i++) | |
598 | { | |
599 | wxDataViewItem item( wxUIntToPtr(sorted[i]+1) ); | |
600 | array.Add( item ); | |
601 | } | |
602 | /* wxDataViewModel:: */ ItemsDeleted( wxDataViewItem(0), array ); | |
603 | } | |
604 | ||
605 | void wxDataViewVirtualListModel::RowChanged( unsigned int row ) | |
606 | { | |
607 | /* wxDataViewModel:: */ ItemChanged( GetItem(row) ); | |
608 | } | |
609 | ||
610 | void wxDataViewVirtualListModel::RowValueChanged( unsigned int row, unsigned int col ) | |
611 | { | |
612 | /* wxDataViewModel:: */ ValueChanged( GetItem(row), col ); | |
613 | } | |
614 | ||
615 | unsigned int wxDataViewVirtualListModel::GetRow( const wxDataViewItem &item ) const | |
616 | { | |
617 | return wxPtrToUInt( item.GetID() ) -1; | |
618 | } | |
619 | ||
620 | wxDataViewItem wxDataViewVirtualListModel::GetItem( unsigned int row ) const | |
621 | { | |
622 | return wxDataViewItem( wxUIntToPtr(row+1) ); | |
623 | } | |
624 | ||
625 | bool wxDataViewVirtualListModel::HasDefaultCompare() const | |
626 | { | |
627 | return true; | |
628 | } | |
629 | ||
630 | int wxDataViewVirtualListModel::Compare(const wxDataViewItem& item1, | |
631 | const wxDataViewItem& item2, | |
632 | unsigned int WXUNUSED(column), | |
633 | bool ascending) const | |
634 | { | |
635 | unsigned int pos1 = wxPtrToUInt(item1.GetID()); // -1 not needed here | |
636 | unsigned int pos2 = wxPtrToUInt(item2.GetID()); // -1 not needed here | |
637 | ||
638 | if (ascending) | |
639 | return pos1 - pos2; | |
640 | else | |
641 | return pos2 - pos1; | |
642 | } | |
643 | ||
644 | unsigned int wxDataViewVirtualListModel::GetChildren( const wxDataViewItem &WXUNUSED(item), wxDataViewItemArray &WXUNUSED(children) ) const | |
645 | { | |
646 | return 0; // should we report an error ? | |
647 | } | |
648 | ||
649 | #endif // __WXMAC__ | |
650 | ||
651 | //----------------------------------------------------------------------------- | |
652 | // wxDataViewIconText | |
653 | //----------------------------------------------------------------------------- | |
654 | ||
655 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText,wxObject) | |
656 | ||
657 | IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV) | |
658 | ||
659 | // --------------------------------------------------------- | |
660 | // wxDataViewRendererBase | |
661 | // --------------------------------------------------------- | |
662 | ||
663 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject) | |
664 | ||
665 | wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype, | |
666 | wxDataViewCellMode WXUNUSED(mode), | |
667 | int WXUNUSED(align) ) | |
668 | { | |
669 | m_variantType = varianttype; | |
670 | m_owner = NULL; | |
671 | } | |
672 | ||
673 | wxDataViewRendererBase::~wxDataViewRendererBase() | |
674 | { | |
675 | if ( m_editorCtrl ) | |
676 | DestroyEditControl(); | |
677 | } | |
678 | ||
679 | wxDataViewCtrl* wxDataViewRendererBase::GetView() const | |
680 | { | |
681 | return const_cast<wxDataViewRendererBase*>(this)->GetOwner()->GetOwner(); | |
682 | } | |
683 | ||
684 | bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect ) | |
685 | { | |
686 | wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner(); | |
687 | ||
688 | // Before doing anything we send an event asking if editing of this item is really wanted. | |
689 | wxDataViewEvent start_event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, dv_ctrl->GetId() ); | |
690 | start_event.SetDataViewColumn( GetOwner() ); | |
691 | start_event.SetModel( dv_ctrl->GetModel() ); | |
692 | start_event.SetItem( item ); | |
693 | start_event.SetEventObject( dv_ctrl ); | |
694 | dv_ctrl->GetEventHandler()->ProcessEvent( start_event ); | |
695 | if( !start_event.IsAllowed() ) | |
696 | return false; | |
697 | ||
698 | m_item = item; // remember for later | |
699 | ||
700 | unsigned int col = GetOwner()->GetModelColumn(); | |
701 | wxVariant value; | |
702 | dv_ctrl->GetModel()->GetValue( value, item, col ); | |
703 | ||
704 | m_editorCtrl = CreateEditorCtrl( dv_ctrl->GetMainWindow(), labelRect, value ); | |
705 | ||
706 | // there might be no editor control for the given item | |
707 | if(!m_editorCtrl) | |
708 | return false; | |
709 | ||
710 | wxDataViewEditorCtrlEvtHandler *handler = | |
711 | new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this ); | |
712 | ||
713 | m_editorCtrl->PushEventHandler( handler ); | |
714 | ||
715 | #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL) | |
716 | handler->SetFocusOnIdle(); | |
717 | #else | |
718 | m_editorCtrl->SetFocus(); | |
719 | #endif | |
720 | ||
721 | // Now we should send Editing Started event | |
722 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv_ctrl->GetId() ); | |
723 | event.SetDataViewColumn( GetOwner() ); | |
724 | event.SetModel( dv_ctrl->GetModel() ); | |
725 | event.SetItem( item ); | |
726 | event.SetEventObject( dv_ctrl ); | |
727 | dv_ctrl->GetEventHandler()->ProcessEvent( event ); | |
728 | ||
729 | return true; | |
730 | } | |
731 | ||
732 | void wxDataViewRendererBase::DestroyEditControl() | |
733 | { | |
734 | // Remove our event handler first to prevent it from (recursively) calling | |
735 | // us again as it would do via a call to FinishEditing() when the editor | |
736 | // loses focus when we hide it below. | |
737 | wxEvtHandler * const handler = m_editorCtrl->PopEventHandler(); | |
738 | ||
739 | // Hide the control immediately but don't delete it yet as there could be | |
740 | // some pending messages for it. | |
741 | m_editorCtrl->Hide(); | |
742 | ||
743 | wxPendingDelete.Append(handler); | |
744 | wxPendingDelete.Append(m_editorCtrl); | |
745 | } | |
746 | ||
747 | void wxDataViewRendererBase::CancelEditing() | |
748 | { | |
749 | if (!m_editorCtrl) | |
750 | return; | |
751 | ||
752 | DestroyEditControl(); | |
753 | } | |
754 | ||
755 | bool wxDataViewRendererBase::FinishEditing() | |
756 | { | |
757 | if (!m_editorCtrl) | |
758 | return true; | |
759 | ||
760 | wxVariant value; | |
761 | GetValueFromEditorCtrl( m_editorCtrl, value ); | |
762 | ||
763 | wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner(); | |
764 | ||
765 | DestroyEditControl(); | |
766 | ||
767 | dv_ctrl->GetMainWindow()->SetFocus(); | |
768 | ||
769 | bool isValid = Validate(value); | |
770 | unsigned int col = GetOwner()->GetModelColumn(); | |
771 | ||
772 | // Now we should send Editing Done event | |
773 | wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv_ctrl->GetId() ); | |
774 | event.SetDataViewColumn( GetOwner() ); | |
775 | event.SetModel( dv_ctrl->GetModel() ); | |
776 | event.SetItem( m_item ); | |
777 | event.SetValue( value ); | |
778 | event.SetColumn( col ); | |
779 | event.SetEditCanceled( !isValid ); | |
780 | event.SetEventObject( dv_ctrl ); | |
781 | dv_ctrl->GetEventHandler()->ProcessEvent( event ); | |
782 | ||
783 | if ( isValid && event.IsAllowed() ) | |
784 | { | |
785 | dv_ctrl->GetModel()->ChangeValue(value, m_item, col); | |
786 | return true; | |
787 | } | |
788 | ||
789 | return false; | |
790 | } | |
791 | ||
792 | void wxDataViewRendererBase::PrepareForItem(const wxDataViewModel *model, | |
793 | const wxDataViewItem& item, | |
794 | unsigned column) | |
795 | { | |
796 | wxVariant value; | |
797 | model->GetValue(value, item, column); | |
798 | SetValue(value); | |
799 | ||
800 | wxDataViewItemAttr attr; | |
801 | model->GetAttr(item, column, attr); | |
802 | SetAttr(attr); | |
803 | ||
804 | SetEnabled(model->IsEnabled(item, column)); | |
805 | } | |
806 | ||
807 | ||
808 | // ---------------------------------------------------------------------------- | |
809 | // wxDataViewCustomRendererBase | |
810 | // ---------------------------------------------------------------------------- | |
811 | ||
812 | bool wxDataViewCustomRendererBase::ActivateCell(const wxRect& cell, | |
813 | wxDataViewModel *model, | |
814 | const wxDataViewItem & item, | |
815 | unsigned int col, | |
816 | const wxMouseEvent* mouseEvent) | |
817 | { | |
818 | // Compatibility code | |
819 | if ( mouseEvent ) | |
820 | return LeftClick(mouseEvent->GetPosition(), cell, model, item, col); | |
821 | else | |
822 | return Activate(cell, model, item, col); | |
823 | } | |
824 | ||
825 | void wxDataViewCustomRendererBase::RenderBackground(wxDC* dc, const wxRect& rect) | |
826 | { | |
827 | if ( !m_attr.HasBackgroundColour() ) | |
828 | return; | |
829 | ||
830 | const wxColour& colour = m_attr.GetBackgroundColour(); | |
831 | wxDCPenChanger changePen(*dc, colour); | |
832 | wxDCBrushChanger changeBrush(*dc, colour); | |
833 | ||
834 | dc->DrawRectangle(rect); | |
835 | } | |
836 | ||
837 | void | |
838 | wxDataViewCustomRendererBase::WXCallRender(wxRect rectCell, wxDC *dc, int state) | |
839 | { | |
840 | wxCHECK_RET( dc, "no DC to draw on in custom renderer?" ); | |
841 | ||
842 | // adjust the rectangle ourselves to account for the alignment | |
843 | wxRect rectItem = rectCell; | |
844 | const int align = GetAlignment(); | |
845 | if ( align != wxDVR_DEFAULT_ALIGNMENT ) | |
846 | { | |
847 | const wxSize size = GetSize(); | |
848 | ||
849 | // take alignment into account only if there is enough space, otherwise | |
850 | // show as much contents as possible | |
851 | // | |
852 | // notice that many existing renderers (e.g. wxDataViewSpinRenderer) | |
853 | // return hard-coded size which can be more than they need and if we | |
854 | // trusted their GetSize() we'd draw the text out of cell bounds | |
855 | // entirely | |
856 | ||
857 | if ( size.x >= 0 && size.x < rectCell.width ) | |
858 | { | |
859 | if ( align & wxALIGN_CENTER_HORIZONTAL ) | |
860 | rectItem.x += (rectCell.width - size.x)/2; | |
861 | else if ( align & wxALIGN_RIGHT ) | |
862 | rectItem.x += rectCell.width - size.x; | |
863 | // else: wxALIGN_LEFT is the default | |
864 | ||
865 | rectItem.width = size.x; | |
866 | } | |
867 | ||
868 | if ( size.y >= 0 && size.y < rectCell.height ) | |
869 | { | |
870 | if ( align & wxALIGN_CENTER_VERTICAL ) | |
871 | rectItem.y += (rectCell.height - size.y)/2; | |
872 | else if ( align & wxALIGN_BOTTOM ) | |
873 | rectItem.y += rectCell.height - size.y; | |
874 | // else: wxALIGN_TOP is the default | |
875 | ||
876 | rectItem.height = size.y; | |
877 | } | |
878 | } | |
879 | ||
880 | ||
881 | // set up the DC attributes | |
882 | ||
883 | // override custom foreground with the standard one for the selected items | |
884 | // because we currently don't allow changing the selection background and | |
885 | // custom colours may be unreadable on it | |
886 | wxColour col; | |
887 | if ( state & wxDATAVIEW_CELL_SELECTED ) | |
888 | col = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT); | |
889 | else if ( m_attr.HasColour() ) | |
890 | col = m_attr.GetColour(); | |
891 | else // use default foreground | |
892 | col = GetOwner()->GetOwner()->GetForegroundColour(); | |
893 | ||
894 | wxDCTextColourChanger changeFg(*dc, col); | |
895 | ||
896 | wxDCFontChanger changeFont(*dc); | |
897 | if ( m_attr.HasFont() ) | |
898 | changeFont.Set(m_attr.GetEffectiveFont(dc->GetFont())); | |
899 | ||
900 | Render(rectItem, dc, state); | |
901 | } | |
902 | ||
903 | wxSize wxDataViewCustomRendererBase::GetTextExtent(const wxString& str) const | |
904 | { | |
905 | const wxDataViewCtrl *view = GetView(); | |
906 | ||
907 | if ( m_attr.HasFont() ) | |
908 | { | |
909 | wxFont font(m_attr.GetEffectiveFont(view->GetFont())); | |
910 | wxSize size; | |
911 | view->GetTextExtent(str, &size.x, &size.y, NULL, NULL, &font); | |
912 | return size; | |
913 | } | |
914 | else | |
915 | { | |
916 | return view->GetTextExtent(str); | |
917 | } | |
918 | } | |
919 | ||
920 | void | |
921 | wxDataViewCustomRendererBase::RenderText(const wxString& text, | |
922 | int xoffset, | |
923 | wxRect rect, | |
924 | wxDC *dc, | |
925 | int WXUNUSED(state)) | |
926 | { | |
927 | wxRect rectText = rect; | |
928 | rectText.x += xoffset; | |
929 | rectText.width -= xoffset; | |
930 | ||
931 | // check if we want to ellipsize the text if it doesn't fit | |
932 | wxString ellipsizedText; | |
933 | if ( GetEllipsizeMode() != wxELLIPSIZE_NONE ) | |
934 | { | |
935 | ellipsizedText = wxControl::Ellipsize | |
936 | ( | |
937 | text, | |
938 | *dc, | |
939 | GetEllipsizeMode(), | |
940 | rectText.width, | |
941 | wxELLIPSIZE_FLAGS_NONE | |
942 | ); | |
943 | } | |
944 | ||
945 | // get the alignment to use | |
946 | int align = GetAlignment(); | |
947 | if ( align == wxDVR_DEFAULT_ALIGNMENT ) | |
948 | { | |
949 | // if we don't have an explicit alignment ourselves, use that of the | |
950 | // column in horizontal direction and default vertical alignment | |
951 | align = GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL; | |
952 | } | |
953 | ||
954 | dc->DrawLabel(ellipsizedText.empty() ? text : ellipsizedText, | |
955 | rectText, align); | |
956 | } | |
957 | ||
958 | //----------------------------------------------------------------------------- | |
959 | // wxDataViewEditorCtrlEvtHandler | |
960 | //----------------------------------------------------------------------------- | |
961 | ||
962 | BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler) | |
963 | EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar) | |
964 | EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus) | |
965 | EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle) | |
966 | EVT_TEXT_ENTER (-1, wxDataViewEditorCtrlEvtHandler::OnTextEnter) | |
967 | END_EVENT_TABLE() | |
968 | ||
969 | void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event ) | |
970 | { | |
971 | if (m_focusOnIdle) | |
972 | { | |
973 | m_focusOnIdle = false; | |
974 | if (wxWindow::FindFocus() != m_editorCtrl) | |
975 | m_editorCtrl->SetFocus(); | |
976 | } | |
977 | ||
978 | event.Skip(); | |
979 | } | |
980 | ||
981 | void wxDataViewEditorCtrlEvtHandler::OnTextEnter( wxCommandEvent &WXUNUSED(event) ) | |
982 | { | |
983 | m_finished = true; | |
984 | m_owner->FinishEditing(); | |
985 | } | |
986 | ||
987 | void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event ) | |
988 | { | |
989 | switch ( event.m_keyCode ) | |
990 | { | |
991 | case WXK_RETURN: | |
992 | m_finished = true; | |
993 | m_owner->FinishEditing(); | |
994 | break; | |
995 | ||
996 | case WXK_ESCAPE: | |
997 | { | |
998 | m_finished = true; | |
999 | m_owner->CancelEditing(); | |
1000 | break; | |
1001 | } | |
1002 | default: | |
1003 | event.Skip(); | |
1004 | } | |
1005 | } | |
1006 | ||
1007 | void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event ) | |
1008 | { | |
1009 | if (!m_finished) | |
1010 | { | |
1011 | m_finished = true; | |
1012 | m_owner->FinishEditing(); | |
1013 | } | |
1014 | ||
1015 | event.Skip(); | |
1016 | } | |
1017 | ||
1018 | // --------------------------------------------------------- | |
1019 | // wxDataViewColumnBase | |
1020 | // --------------------------------------------------------- | |
1021 | ||
1022 | void wxDataViewColumnBase::Init(wxDataViewRenderer *renderer, | |
1023 | unsigned int model_column) | |
1024 | { | |
1025 | m_renderer = renderer; | |
1026 | m_model_column = model_column; | |
1027 | m_owner = NULL; | |
1028 | m_renderer->SetOwner( (wxDataViewColumn*) this ); | |
1029 | } | |
1030 | ||
1031 | wxDataViewColumnBase::~wxDataViewColumnBase() | |
1032 | { | |
1033 | delete m_renderer; | |
1034 | } | |
1035 | ||
1036 | // --------------------------------------------------------- | |
1037 | // wxDataViewCtrlBase | |
1038 | // --------------------------------------------------------- | |
1039 | ||
1040 | IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl) | |
1041 | ||
1042 | wxDataViewCtrlBase::wxDataViewCtrlBase() | |
1043 | { | |
1044 | m_model = NULL; | |
1045 | m_expander_column = 0; | |
1046 | m_indent = 8; | |
1047 | } | |
1048 | ||
1049 | wxDataViewCtrlBase::~wxDataViewCtrlBase() | |
1050 | { | |
1051 | if (m_model) | |
1052 | { | |
1053 | m_model->DecRef(); | |
1054 | m_model = NULL; | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model ) | |
1059 | { | |
1060 | if (m_model) | |
1061 | { | |
1062 | m_model->DecRef(); // discard old model, if any | |
1063 | } | |
1064 | ||
1065 | // add our own reference to the new model: | |
1066 | m_model = model; | |
1067 | if (m_model) | |
1068 | { | |
1069 | m_model->IncRef(); | |
1070 | } | |
1071 | ||
1072 | return true; | |
1073 | } | |
1074 | ||
1075 | wxDataViewModel* wxDataViewCtrlBase::GetModel() | |
1076 | { | |
1077 | return m_model; | |
1078 | } | |
1079 | ||
1080 | const wxDataViewModel* wxDataViewCtrlBase::GetModel() const | |
1081 | { | |
1082 | return m_model; | |
1083 | } | |
1084 | ||
1085 | void wxDataViewCtrlBase::ExpandAncestors( const wxDataViewItem & item ) | |
1086 | { | |
1087 | if (!m_model) return; | |
1088 | ||
1089 | if (!item.IsOk()) return; | |
1090 | ||
1091 | wxVector<wxDataViewItem> parentChain; | |
1092 | ||
1093 | // at first we get all the parents of the selected item | |
1094 | wxDataViewItem parent = m_model->GetParent(item); | |
1095 | while (parent.IsOk()) | |
1096 | { | |
1097 | parentChain.push_back(parent); | |
1098 | parent = m_model->GetParent(parent); | |
1099 | } | |
1100 | ||
1101 | // then we expand the parents, starting at the root | |
1102 | while (!parentChain.empty()) | |
1103 | { | |
1104 | Expand(parentChain.back()); | |
1105 | parentChain.pop_back(); | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | wxDataViewItem wxDataViewCtrlBase::GetCurrentItem() const | |
1110 | { | |
1111 | return HasFlag(wxDV_MULTIPLE) ? DoGetCurrentItem() | |
1112 | : GetSelection(); | |
1113 | } | |
1114 | ||
1115 | void wxDataViewCtrlBase::SetCurrentItem(const wxDataViewItem& item) | |
1116 | { | |
1117 | wxCHECK_RET( item.IsOk(), "Can't make current an invalid item." ); | |
1118 | ||
1119 | if ( HasFlag(wxDV_MULTIPLE) ) | |
1120 | DoSetCurrentItem(item); | |
1121 | else | |
1122 | Select(item); | |
1123 | } | |
1124 | ||
1125 | wxDataViewItem wxDataViewCtrlBase::GetSelection() const | |
1126 | { | |
1127 | if ( GetSelectedItemsCount() != 1 ) | |
1128 | return wxDataViewItem(); | |
1129 | ||
1130 | wxDataViewItemArray selections; | |
1131 | GetSelections(selections); | |
1132 | return selections[0]; | |
1133 | } | |
1134 | ||
1135 | wxDataViewColumn * | |
1136 | wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column, | |
1137 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1138 | { | |
1139 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1140 | new wxDataViewTextRenderer( wxT("string"), mode ), | |
1141 | model_column, width, align, flags ); | |
1142 | AppendColumn( ret ); | |
1143 | return ret; | |
1144 | } | |
1145 | ||
1146 | wxDataViewColumn * | |
1147 | wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column, | |
1148 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1149 | { | |
1150 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1151 | new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ), | |
1152 | model_column, width, align, flags ); | |
1153 | AppendColumn( ret ); | |
1154 | return ret; | |
1155 | } | |
1156 | ||
1157 | wxDataViewColumn * | |
1158 | wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column, | |
1159 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1160 | { | |
1161 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1162 | new wxDataViewToggleRenderer( wxT("bool"), mode ), | |
1163 | model_column, width, align, flags ); | |
1164 | AppendColumn( ret ); | |
1165 | return ret; | |
1166 | } | |
1167 | ||
1168 | wxDataViewColumn * | |
1169 | wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column, | |
1170 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1171 | { | |
1172 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1173 | new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ), | |
1174 | model_column, width, align, flags ); | |
1175 | AppendColumn( ret ); | |
1176 | return ret; | |
1177 | } | |
1178 | ||
1179 | wxDataViewColumn * | |
1180 | wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column, | |
1181 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1182 | { | |
1183 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1184 | new wxDataViewDateRenderer( wxT("datetime"), mode ), | |
1185 | model_column, width, align, flags ); | |
1186 | AppendColumn( ret ); | |
1187 | return ret; | |
1188 | } | |
1189 | ||
1190 | wxDataViewColumn * | |
1191 | wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column, | |
1192 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1193 | { | |
1194 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1195 | new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ), | |
1196 | model_column, width, align, flags ); | |
1197 | AppendColumn( ret ); | |
1198 | return ret; | |
1199 | } | |
1200 | ||
1201 | wxDataViewColumn * | |
1202 | wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column, | |
1203 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1204 | { | |
1205 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1206 | new wxDataViewTextRenderer( wxT("string"), mode ), | |
1207 | model_column, width, align, flags ); | |
1208 | AppendColumn( ret ); | |
1209 | return ret; | |
1210 | } | |
1211 | ||
1212 | wxDataViewColumn * | |
1213 | wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column, | |
1214 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1215 | { | |
1216 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1217 | new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ), | |
1218 | model_column, width, align, flags ); | |
1219 | AppendColumn( ret ); | |
1220 | return ret; | |
1221 | } | |
1222 | ||
1223 | wxDataViewColumn * | |
1224 | wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column, | |
1225 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1226 | { | |
1227 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1228 | new wxDataViewToggleRenderer( wxT("bool"), mode ), | |
1229 | model_column, width, align, flags ); | |
1230 | AppendColumn( ret ); | |
1231 | return ret; | |
1232 | } | |
1233 | ||
1234 | wxDataViewColumn * | |
1235 | wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column, | |
1236 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1237 | { | |
1238 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1239 | new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ), | |
1240 | model_column, width, align, flags ); | |
1241 | AppendColumn( ret ); | |
1242 | return ret; | |
1243 | } | |
1244 | ||
1245 | wxDataViewColumn * | |
1246 | wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column, | |
1247 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1248 | { | |
1249 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1250 | new wxDataViewDateRenderer( wxT("datetime"), mode ), | |
1251 | model_column, width, align, flags ); | |
1252 | AppendColumn( ret ); | |
1253 | return ret; | |
1254 | } | |
1255 | ||
1256 | wxDataViewColumn * | |
1257 | wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column, | |
1258 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1259 | { | |
1260 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1261 | new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ), | |
1262 | model_column, width, align, flags ); | |
1263 | AppendColumn( ret ); | |
1264 | return ret; | |
1265 | } | |
1266 | ||
1267 | wxDataViewColumn * | |
1268 | wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column, | |
1269 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1270 | { | |
1271 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1272 | new wxDataViewTextRenderer( wxT("string"), mode ), | |
1273 | model_column, width, align, flags ); | |
1274 | PrependColumn( ret ); | |
1275 | return ret; | |
1276 | } | |
1277 | ||
1278 | wxDataViewColumn * | |
1279 | wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column, | |
1280 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1281 | { | |
1282 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1283 | new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ), | |
1284 | model_column, width, align, flags ); | |
1285 | PrependColumn( ret ); | |
1286 | return ret; | |
1287 | } | |
1288 | ||
1289 | wxDataViewColumn * | |
1290 | wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column, | |
1291 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1292 | { | |
1293 | ||
1294 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1295 | new wxDataViewToggleRenderer( wxT("bool"), mode ), | |
1296 | model_column, width, align, flags ); | |
1297 | PrependColumn( ret ); | |
1298 | return ret; | |
1299 | } | |
1300 | ||
1301 | wxDataViewColumn * | |
1302 | wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column, | |
1303 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1304 | { | |
1305 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1306 | new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ), | |
1307 | model_column, width, align, flags ); | |
1308 | PrependColumn( ret ); | |
1309 | return ret; | |
1310 | } | |
1311 | ||
1312 | wxDataViewColumn * | |
1313 | wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column, | |
1314 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1315 | { | |
1316 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1317 | new wxDataViewDateRenderer( wxT("datetime"), mode ), | |
1318 | model_column, width, align, flags ); | |
1319 | PrependColumn( ret ); | |
1320 | return ret; | |
1321 | } | |
1322 | ||
1323 | wxDataViewColumn * | |
1324 | wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column, | |
1325 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1326 | { | |
1327 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1328 | new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ), | |
1329 | model_column, width, align, flags ); | |
1330 | PrependColumn( ret ); | |
1331 | return ret; | |
1332 | } | |
1333 | ||
1334 | wxDataViewColumn * | |
1335 | wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column, | |
1336 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1337 | { | |
1338 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1339 | new wxDataViewTextRenderer( wxT("string"), mode ), | |
1340 | model_column, width, align, flags ); | |
1341 | PrependColumn( ret ); | |
1342 | return ret; | |
1343 | } | |
1344 | ||
1345 | wxDataViewColumn * | |
1346 | wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column, | |
1347 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1348 | { | |
1349 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1350 | new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ), | |
1351 | model_column, width, align, flags ); | |
1352 | PrependColumn( ret ); | |
1353 | return ret; | |
1354 | } | |
1355 | ||
1356 | wxDataViewColumn * | |
1357 | wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column, | |
1358 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1359 | { | |
1360 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1361 | new wxDataViewToggleRenderer( wxT("bool"), mode ), | |
1362 | model_column, width, align, flags ); | |
1363 | PrependColumn( ret ); | |
1364 | return ret; | |
1365 | } | |
1366 | ||
1367 | wxDataViewColumn * | |
1368 | wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column, | |
1369 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1370 | { | |
1371 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1372 | new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ), | |
1373 | model_column, width, align, flags ); | |
1374 | PrependColumn( ret ); | |
1375 | return ret; | |
1376 | } | |
1377 | ||
1378 | wxDataViewColumn * | |
1379 | wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column, | |
1380 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1381 | { | |
1382 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1383 | new wxDataViewDateRenderer( wxT("datetime"), mode ), | |
1384 | model_column, width, align, flags ); | |
1385 | PrependColumn( ret ); | |
1386 | return ret; | |
1387 | } | |
1388 | ||
1389 | wxDataViewColumn * | |
1390 | wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column, | |
1391 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1392 | { | |
1393 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1394 | new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ), | |
1395 | model_column, width, align, flags ); | |
1396 | PrependColumn( ret ); | |
1397 | return ret; | |
1398 | } | |
1399 | ||
1400 | bool | |
1401 | wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col ) | |
1402 | { | |
1403 | col->SetOwner( (wxDataViewCtrl*) this ); | |
1404 | return true; | |
1405 | } | |
1406 | ||
1407 | bool | |
1408 | wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col ) | |
1409 | { | |
1410 | col->SetOwner( (wxDataViewCtrl*) this ); | |
1411 | return true; | |
1412 | } | |
1413 | ||
1414 | bool | |
1415 | wxDataViewCtrlBase::InsertColumn( unsigned int WXUNUSED(pos), wxDataViewColumn *col ) | |
1416 | { | |
1417 | col->SetOwner( (wxDataViewCtrl*) this ); | |
1418 | return true; | |
1419 | } | |
1420 | ||
1421 | // --------------------------------------------------------- | |
1422 | // wxDataViewEvent | |
1423 | // --------------------------------------------------------- | |
1424 | ||
1425 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent) | |
1426 | ||
1427 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, wxDataViewEvent ); | |
1428 | ||
1429 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, wxDataViewEvent ); | |
1430 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, wxDataViewEvent ); | |
1431 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, wxDataViewEvent ); | |
1432 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, wxDataViewEvent ); | |
1433 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, wxDataViewEvent ); | |
1434 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, wxDataViewEvent ); | |
1435 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, wxDataViewEvent ); | |
1436 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, wxDataViewEvent ); | |
1437 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, wxDataViewEvent ); | |
1438 | ||
1439 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, wxDataViewEvent ); | |
1440 | ||
1441 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, wxDataViewEvent ); | |
1442 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, wxDataViewEvent ); | |
1443 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent ); | |
1444 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent ); | |
1445 | ||
1446 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_CACHE_HINT, wxDataViewEvent ); | |
1447 | ||
1448 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent ); | |
1449 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, wxDataViewEvent ); | |
1450 | wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, wxDataViewEvent ); | |
1451 | ||
1452 | ||
1453 | ||
1454 | // ------------------------------------- | |
1455 | // wxDataViewSpinRenderer | |
1456 | // ------------------------------------- | |
1457 | ||
1458 | wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) : | |
1459 | wxDataViewCustomRenderer(wxT("long"), mode, alignment ) | |
1460 | { | |
1461 | m_min = min; | |
1462 | m_max = max; | |
1463 | } | |
1464 | ||
1465 | wxWindow* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value ) | |
1466 | { | |
1467 | long l = value; | |
1468 | wxSize size = labelRect.GetSize(); | |
1469 | #ifdef __WXMAC__ | |
1470 | size = wxSize( wxMax(70,labelRect.width ), -1 ); | |
1471 | #endif | |
1472 | wxString str; | |
1473 | str.Printf( wxT("%d"), (int) l ); | |
1474 | wxSpinCtrl *sc = new wxSpinCtrl( parent, wxID_ANY, str, | |
1475 | labelRect.GetTopLeft(), size, wxSP_ARROW_KEYS|wxTE_PROCESS_ENTER, m_min, m_max, l ); | |
1476 | #ifdef __WXMAC__ | |
1477 | size = sc->GetSize(); | |
1478 | wxPoint pt = sc->GetPosition(); | |
1479 | sc->SetSize( pt.x - 4, pt.y - 4, size.x, size.y ); | |
1480 | #endif | |
1481 | ||
1482 | return sc; | |
1483 | } | |
1484 | ||
1485 | bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value ) | |
1486 | { | |
1487 | wxSpinCtrl *sc = (wxSpinCtrl*) editor; | |
1488 | long l = sc->GetValue(); | |
1489 | value = l; | |
1490 | return true; | |
1491 | } | |
1492 | ||
1493 | bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state ) | |
1494 | { | |
1495 | wxString str; | |
1496 | str.Printf(wxT("%d"), (int) m_data ); | |
1497 | RenderText( str, 0, rect, dc, state ); | |
1498 | return true; | |
1499 | } | |
1500 | ||
1501 | wxSize wxDataViewSpinRenderer::GetSize() const | |
1502 | { | |
1503 | wxSize sz = GetTextExtent(wxString::Format("%d", (int)m_data)); | |
1504 | ||
1505 | // Allow some space for the spin buttons, which is approximately the size | |
1506 | // of a scrollbar (and getting pixel-exact value would be complicated). | |
1507 | // Also add some whitespace between the text and the button: | |
1508 | sz.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
1509 | sz.x += GetTextExtent("M").x; | |
1510 | ||
1511 | return sz; | |
1512 | } | |
1513 | ||
1514 | bool wxDataViewSpinRenderer::SetValue( const wxVariant &value ) | |
1515 | { | |
1516 | m_data = value.GetLong(); | |
1517 | return true; | |
1518 | } | |
1519 | ||
1520 | bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const | |
1521 | { | |
1522 | value = m_data; | |
1523 | return true; | |
1524 | } | |
1525 | ||
1526 | // ------------------------------------- | |
1527 | // wxDataViewChoiceRenderer | |
1528 | // ------------------------------------- | |
1529 | ||
1530 | #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__) | |
1531 | ||
1532 | wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString& choices, wxDataViewCellMode mode, int alignment ) : | |
1533 | wxDataViewCustomRenderer(wxT("string"), mode, alignment ) | |
1534 | { | |
1535 | m_choices = choices; | |
1536 | } | |
1537 | ||
1538 | wxWindow* wxDataViewChoiceRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value ) | |
1539 | { | |
1540 | wxChoice* c = new wxChoice | |
1541 | ( | |
1542 | parent, | |
1543 | wxID_ANY, | |
1544 | labelRect.GetTopLeft(), | |
1545 | wxSize(labelRect.GetWidth(), -1), | |
1546 | m_choices | |
1547 | ); | |
1548 | c->Move(labelRect.GetRight() - c->GetRect().width, wxDefaultCoord); | |
1549 | c->SetStringSelection( value.GetString() ); | |
1550 | return c; | |
1551 | } | |
1552 | ||
1553 | bool wxDataViewChoiceRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value ) | |
1554 | { | |
1555 | wxChoice *c = (wxChoice*) editor; | |
1556 | wxString s = c->GetStringSelection(); | |
1557 | value = s; | |
1558 | return true; | |
1559 | } | |
1560 | ||
1561 | bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state ) | |
1562 | { | |
1563 | RenderText( m_data, 0, rect, dc, state ); | |
1564 | return true; | |
1565 | } | |
1566 | ||
1567 | wxSize wxDataViewChoiceRenderer::GetSize() const | |
1568 | { | |
1569 | wxSize sz; | |
1570 | ||
1571 | for ( wxArrayString::const_iterator i = m_choices.begin(); i != m_choices.end(); ++i ) | |
1572 | sz.IncTo(GetTextExtent(*i)); | |
1573 | ||
1574 | // Allow some space for the right-side button, which is approximately the | |
1575 | // size of a scrollbar (and getting pixel-exact value would be complicated). | |
1576 | // Also add some whitespace between the text and the button: | |
1577 | sz.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
1578 | sz.x += GetTextExtent("M").x; | |
1579 | ||
1580 | return sz; | |
1581 | } | |
1582 | ||
1583 | bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value ) | |
1584 | { | |
1585 | m_data = value.GetString(); | |
1586 | return true; | |
1587 | } | |
1588 | ||
1589 | bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const | |
1590 | { | |
1591 | value = m_data; | |
1592 | return true; | |
1593 | } | |
1594 | ||
1595 | // ---------------------------------------------------------------------------- | |
1596 | // wxDataViewChoiceByIndexRenderer | |
1597 | // ---------------------------------------------------------------------------- | |
1598 | ||
1599 | wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices, | |
1600 | wxDataViewCellMode mode, int alignment ) : | |
1601 | wxDataViewChoiceRenderer( choices, mode, alignment ) | |
1602 | { | |
1603 | } | |
1604 | ||
1605 | wxWindow* wxDataViewChoiceByIndexRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value ) | |
1606 | { | |
1607 | wxVariant string_value = GetChoice( value.GetLong() ); | |
1608 | ||
1609 | return wxDataViewChoiceRenderer::CreateEditorCtrl( parent, labelRect, string_value ); | |
1610 | } | |
1611 | ||
1612 | bool wxDataViewChoiceByIndexRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value ) | |
1613 | { | |
1614 | wxVariant string_value; | |
1615 | if (!wxDataViewChoiceRenderer::GetValueFromEditorCtrl( editor, string_value )) | |
1616 | return false; | |
1617 | ||
1618 | value = (long) GetChoices().Index( string_value.GetString() ); | |
1619 | return true; | |
1620 | } | |
1621 | ||
1622 | bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value ) | |
1623 | { | |
1624 | wxVariant string_value = GetChoice( value.GetLong() ); | |
1625 | return wxDataViewChoiceRenderer::SetValue( string_value ); | |
1626 | } | |
1627 | ||
1628 | bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const | |
1629 | { | |
1630 | wxVariant string_value; | |
1631 | if (!wxDataViewChoiceRenderer::GetValue( string_value )) | |
1632 | return false; | |
1633 | ||
1634 | value = (long) GetChoices().Index( string_value.GetString() ); | |
1635 | return true; | |
1636 | } | |
1637 | ||
1638 | #endif | |
1639 | ||
1640 | // --------------------------------------------------------- | |
1641 | // wxDataViewDateRenderer | |
1642 | // --------------------------------------------------------- | |
1643 | ||
1644 | #if (defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)) && wxUSE_DATEPICKCTRL | |
1645 | ||
1646 | wxDataViewDateRenderer::wxDataViewDateRenderer(const wxString& varianttype, | |
1647 | wxDataViewCellMode mode, int align) | |
1648 | : wxDataViewCustomRenderer(varianttype, mode, align) | |
1649 | { | |
1650 | } | |
1651 | ||
1652 | wxWindow * | |
1653 | wxDataViewDateRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value) | |
1654 | { | |
1655 | return new wxDatePickerCtrl | |
1656 | ( | |
1657 | parent, | |
1658 | wxID_ANY, | |
1659 | value.GetDateTime(), | |
1660 | labelRect.GetTopLeft(), | |
1661 | labelRect.GetSize() | |
1662 | ); | |
1663 | } | |
1664 | ||
1665 | bool wxDataViewDateRenderer::GetValueFromEditorCtrl(wxWindow *editor, wxVariant& value) | |
1666 | { | |
1667 | wxDatePickerCtrl *ctrl = static_cast<wxDatePickerCtrl*>(editor); | |
1668 | value = ctrl->GetValue(); | |
1669 | return true; | |
1670 | } | |
1671 | ||
1672 | bool wxDataViewDateRenderer::SetValue(const wxVariant& value) | |
1673 | { | |
1674 | m_date = value.GetDateTime(); | |
1675 | return true; | |
1676 | } | |
1677 | ||
1678 | bool wxDataViewDateRenderer::GetValue(wxVariant& value) const | |
1679 | { | |
1680 | value = m_date; | |
1681 | return true; | |
1682 | } | |
1683 | ||
1684 | bool wxDataViewDateRenderer::Render(wxRect cell, wxDC* dc, int state) | |
1685 | { | |
1686 | wxString tmp = m_date.FormatDate(); | |
1687 | RenderText( tmp, 0, cell, dc, state ); | |
1688 | return true; | |
1689 | } | |
1690 | ||
1691 | wxSize wxDataViewDateRenderer::GetSize() const | |
1692 | { | |
1693 | return GetTextExtent(m_date.FormatDate()); | |
1694 | } | |
1695 | ||
1696 | #endif // (defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)) && wxUSE_DATEPICKCTRL | |
1697 | ||
1698 | //----------------------------------------------------------------------------- | |
1699 | // wxDataViewListStore | |
1700 | //----------------------------------------------------------------------------- | |
1701 | ||
1702 | wxDataViewListStore::wxDataViewListStore() | |
1703 | { | |
1704 | } | |
1705 | ||
1706 | wxDataViewListStore::~wxDataViewListStore() | |
1707 | { | |
1708 | wxVector<wxDataViewListStoreLine*>::iterator it; | |
1709 | for (it = m_data.begin(); it != m_data.end(); ++it) | |
1710 | { | |
1711 | wxDataViewListStoreLine* line = *it; | |
1712 | delete line; | |
1713 | } | |
1714 | } | |
1715 | ||
1716 | void wxDataViewListStore::PrependColumn( const wxString &varianttype ) | |
1717 | { | |
1718 | m_cols.Insert( varianttype, 0 ); | |
1719 | } | |
1720 | ||
1721 | void wxDataViewListStore::InsertColumn( unsigned int pos, const wxString &varianttype ) | |
1722 | { | |
1723 | m_cols.Insert( varianttype, pos ); | |
1724 | } | |
1725 | ||
1726 | void wxDataViewListStore::AppendColumn( const wxString &varianttype ) | |
1727 | { | |
1728 | m_cols.Add( varianttype ); | |
1729 | } | |
1730 | ||
1731 | unsigned int wxDataViewListStore::GetColumnCount() const | |
1732 | { | |
1733 | return m_cols.GetCount(); | |
1734 | } | |
1735 | ||
1736 | wxString wxDataViewListStore::GetColumnType( unsigned int pos ) const | |
1737 | { | |
1738 | return m_cols[pos]; | |
1739 | } | |
1740 | ||
1741 | void wxDataViewListStore::AppendItem( const wxVector<wxVariant> &values, wxClientData *data ) | |
1742 | { | |
1743 | wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data ); | |
1744 | line->m_values = values; | |
1745 | m_data.push_back( line ); | |
1746 | ||
1747 | RowAppended(); | |
1748 | } | |
1749 | ||
1750 | void wxDataViewListStore::PrependItem( const wxVector<wxVariant> &values, wxClientData *data ) | |
1751 | { | |
1752 | wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data ); | |
1753 | line->m_values = values; | |
1754 | m_data.insert( m_data.begin(), line ); | |
1755 | ||
1756 | RowPrepended(); | |
1757 | } | |
1758 | ||
1759 | void wxDataViewListStore::InsertItem( unsigned int row, const wxVector<wxVariant> &values, | |
1760 | wxClientData *data ) | |
1761 | { | |
1762 | wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data ); | |
1763 | line->m_values = values; | |
1764 | m_data.insert( m_data.begin()+row, line ); | |
1765 | ||
1766 | RowInserted( row ); | |
1767 | } | |
1768 | ||
1769 | void wxDataViewListStore::DeleteItem( unsigned int row ) | |
1770 | { | |
1771 | wxVector<wxDataViewListStoreLine*>::iterator it = m_data.begin() + row; | |
1772 | delete *it; | |
1773 | m_data.erase( it ); | |
1774 | ||
1775 | RowDeleted( row ); | |
1776 | } | |
1777 | ||
1778 | void wxDataViewListStore::DeleteAllItems() | |
1779 | { | |
1780 | wxVector<wxDataViewListStoreLine*>::iterator it; | |
1781 | for (it = m_data.begin(); it != m_data.end(); ++it) | |
1782 | { | |
1783 | wxDataViewListStoreLine* line = *it; | |
1784 | delete line; | |
1785 | } | |
1786 | ||
1787 | m_data.clear(); | |
1788 | ||
1789 | Reset( 0 ); | |
1790 | } | |
1791 | ||
1792 | void wxDataViewListStore::GetValueByRow( wxVariant &value, unsigned int row, unsigned int col ) const | |
1793 | { | |
1794 | wxDataViewListStoreLine *line = m_data[row]; | |
1795 | value = line->m_values[col]; | |
1796 | } | |
1797 | ||
1798 | bool wxDataViewListStore::SetValueByRow( const wxVariant &value, unsigned int row, unsigned int col ) | |
1799 | { | |
1800 | wxDataViewListStoreLine *line = m_data[row]; | |
1801 | line->m_values[col] = value; | |
1802 | ||
1803 | return true; | |
1804 | } | |
1805 | ||
1806 | //----------------------------------------------------------------------------- | |
1807 | // wxDataViewListCtrl | |
1808 | //----------------------------------------------------------------------------- | |
1809 | ||
1810 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewListCtrl,wxDataViewCtrl) | |
1811 | ||
1812 | BEGIN_EVENT_TABLE(wxDataViewListCtrl,wxDataViewCtrl) | |
1813 | EVT_SIZE( wxDataViewListCtrl::OnSize ) | |
1814 | END_EVENT_TABLE() | |
1815 | ||
1816 | wxDataViewListCtrl::wxDataViewListCtrl() | |
1817 | { | |
1818 | } | |
1819 | ||
1820 | wxDataViewListCtrl::wxDataViewListCtrl( wxWindow *parent, wxWindowID id, | |
1821 | const wxPoint& pos, const wxSize& size, long style, | |
1822 | const wxValidator& validator ) | |
1823 | { | |
1824 | Create( parent, id, pos, size, style, validator ); | |
1825 | } | |
1826 | ||
1827 | wxDataViewListCtrl::~wxDataViewListCtrl() | |
1828 | { | |
1829 | } | |
1830 | ||
1831 | ||
1832 | bool wxDataViewListCtrl::Create( wxWindow *parent, wxWindowID id, | |
1833 | const wxPoint& pos, const wxSize& size, long style, | |
1834 | const wxValidator& validator ) | |
1835 | { | |
1836 | if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) ) | |
1837 | return false; | |
1838 | ||
1839 | wxDataViewListStore *store = new wxDataViewListStore; | |
1840 | AssociateModel( store ); | |
1841 | store->DecRef(); | |
1842 | ||
1843 | return true; | |
1844 | } | |
1845 | ||
1846 | bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *column, const wxString &varianttype ) | |
1847 | { | |
1848 | GetStore()->AppendColumn( varianttype ); | |
1849 | return wxDataViewCtrl::AppendColumn( column ); | |
1850 | } | |
1851 | ||
1852 | bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *column, const wxString &varianttype ) | |
1853 | { | |
1854 | GetStore()->PrependColumn( varianttype ); | |
1855 | return wxDataViewCtrl::PrependColumn( column ); | |
1856 | } | |
1857 | ||
1858 | bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *column, const wxString &varianttype ) | |
1859 | { | |
1860 | GetStore()->InsertColumn( pos, varianttype ); | |
1861 | return wxDataViewCtrl::InsertColumn( pos, column ); | |
1862 | } | |
1863 | ||
1864 | bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *col ) | |
1865 | { | |
1866 | return PrependColumn( col, "string" ); | |
1867 | } | |
1868 | ||
1869 | bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col ) | |
1870 | { | |
1871 | return InsertColumn( pos, col, "string" ); | |
1872 | } | |
1873 | ||
1874 | bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *col ) | |
1875 | { | |
1876 | return AppendColumn( col, "string" ); | |
1877 | } | |
1878 | ||
1879 | wxDataViewColumn *wxDataViewListCtrl::AppendTextColumn( const wxString &label, | |
1880 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1881 | { | |
1882 | GetStore()->AppendColumn( wxT("string") ); | |
1883 | ||
1884 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1885 | new wxDataViewTextRenderer( wxT("string"), mode ), | |
1886 | GetStore()->GetColumnCount()-1, width, align, flags ); | |
1887 | ||
1888 | wxDataViewCtrl::AppendColumn( ret ); | |
1889 | ||
1890 | return ret; | |
1891 | } | |
1892 | ||
1893 | wxDataViewColumn *wxDataViewListCtrl::AppendToggleColumn( const wxString &label, | |
1894 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1895 | { | |
1896 | GetStore()->AppendColumn( wxT("bool") ); | |
1897 | ||
1898 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1899 | new wxDataViewToggleRenderer( wxT("bool"), mode ), | |
1900 | GetStore()->GetColumnCount()-1, width, align, flags ); | |
1901 | ||
1902 | wxDataViewCtrl::AppendColumn( ret ); | |
1903 | ||
1904 | return ret; | |
1905 | } | |
1906 | ||
1907 | wxDataViewColumn *wxDataViewListCtrl::AppendProgressColumn( const wxString &label, | |
1908 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1909 | { | |
1910 | GetStore()->AppendColumn( wxT("long") ); | |
1911 | ||
1912 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1913 | new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ), | |
1914 | GetStore()->GetColumnCount()-1, width, align, flags ); | |
1915 | ||
1916 | wxDataViewCtrl::AppendColumn( ret ); | |
1917 | ||
1918 | return ret; | |
1919 | } | |
1920 | ||
1921 | wxDataViewColumn *wxDataViewListCtrl::AppendIconTextColumn( const wxString &label, | |
1922 | wxDataViewCellMode mode, int width, wxAlignment align, int flags ) | |
1923 | { | |
1924 | GetStore()->AppendColumn( wxT("wxDataViewIconText") ); | |
1925 | ||
1926 | wxDataViewColumn *ret = new wxDataViewColumn( label, | |
1927 | new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ), | |
1928 | GetStore()->GetColumnCount()-1, width, align, flags ); | |
1929 | ||
1930 | wxDataViewCtrl::AppendColumn( ret ); | |
1931 | ||
1932 | return ret; | |
1933 | } | |
1934 | ||
1935 | void wxDataViewListCtrl::OnSize( wxSizeEvent &event ) | |
1936 | { | |
1937 | event.Skip( true ); | |
1938 | } | |
1939 | ||
1940 | //----------------------------------------------------------------------------- | |
1941 | // wxDataViewTreeStore | |
1942 | //----------------------------------------------------------------------------- | |
1943 | ||
1944 | wxDataViewTreeStoreNode::wxDataViewTreeStoreNode( | |
1945 | wxDataViewTreeStoreNode *parent, | |
1946 | const wxString &text, const wxIcon &icon, wxClientData *data ) | |
1947 | { | |
1948 | m_parent = parent; | |
1949 | m_text = text; | |
1950 | m_icon = icon; | |
1951 | m_data = data; | |
1952 | } | |
1953 | ||
1954 | wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode() | |
1955 | { | |
1956 | if (m_data) | |
1957 | delete m_data; | |
1958 | } | |
1959 | ||
1960 | #include "wx/listimpl.cpp" | |
1961 | WX_DEFINE_LIST(wxDataViewTreeStoreNodeList) | |
1962 | ||
1963 | wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode( | |
1964 | wxDataViewTreeStoreNode *parent, const wxString &text, | |
1965 | const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) : | |
1966 | wxDataViewTreeStoreNode( parent, text, icon, data ) | |
1967 | { | |
1968 | m_iconExpanded = expanded; | |
1969 | m_isExpanded = false; | |
1970 | m_children.DeleteContents(true); | |
1971 | } | |
1972 | ||
1973 | wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode() | |
1974 | { | |
1975 | } | |
1976 | ||
1977 | //----------------------------------------------------------------------------- | |
1978 | ||
1979 | wxDataViewTreeStore::wxDataViewTreeStore() | |
1980 | { | |
1981 | m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString ); | |
1982 | } | |
1983 | ||
1984 | wxDataViewTreeStore::~wxDataViewTreeStore() | |
1985 | { | |
1986 | delete m_root; | |
1987 | } | |
1988 | ||
1989 | wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent, | |
1990 | const wxString &text, const wxIcon &icon, wxClientData *data ) | |
1991 | { | |
1992 | wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent ); | |
1993 | if (!parent_node) return wxDataViewItem(0); | |
1994 | ||
1995 | wxDataViewTreeStoreNode *node = | |
1996 | new wxDataViewTreeStoreNode( parent_node, text, icon, data ); | |
1997 | parent_node->GetChildren().Append( node ); | |
1998 | ||
1999 | return node->GetItem(); | |
2000 | } | |
2001 | ||
2002 | wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent, | |
2003 | const wxString &text, const wxIcon &icon, wxClientData *data ) | |
2004 | { | |
2005 | wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent ); | |
2006 | if (!parent_node) return wxDataViewItem(0); | |
2007 | ||
2008 | wxDataViewTreeStoreNode *node = | |
2009 | new wxDataViewTreeStoreNode( parent_node, text, icon, data ); | |
2010 | parent_node->GetChildren().Insert( node ); | |
2011 | ||
2012 | return node->GetItem(); | |
2013 | } | |
2014 | ||
2015 | wxDataViewItem | |
2016 | wxDataViewTreeStore::InsertItem(const wxDataViewItem& parent, | |
2017 | const wxDataViewItem& previous, | |
2018 | const wxString& text, | |
2019 | const wxIcon& icon, | |
2020 | wxClientData *data) | |
2021 | { | |
2022 | wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent ); | |
2023 | if (!parent_node) return wxDataViewItem(0); | |
2024 | ||
2025 | wxDataViewTreeStoreNode *previous_node = FindNode( previous ); | |
2026 | int pos = parent_node->GetChildren().IndexOf( previous_node ); | |
2027 | if (pos == wxNOT_FOUND) return wxDataViewItem(0); | |
2028 | ||
2029 | wxDataViewTreeStoreNode *node = | |
2030 | new wxDataViewTreeStoreNode( parent_node, text, icon, data ); | |
2031 | parent_node->GetChildren().Insert( (size_t) pos, node ); | |
2032 | ||
2033 | return node->GetItem(); | |
2034 | } | |
2035 | ||
2036 | wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent, | |
2037 | const wxString &text, const wxIcon &icon, const wxIcon &expanded, | |
2038 | wxClientData *data ) | |
2039 | { | |
2040 | wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent ); | |
2041 | if (!parent_node) return wxDataViewItem(0); | |
2042 | ||
2043 | wxDataViewTreeStoreContainerNode *node = | |
2044 | new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data ); | |
2045 | parent_node->GetChildren().Insert( node ); | |
2046 | ||
2047 | return node->GetItem(); | |
2048 | } | |
2049 | ||
2050 | wxDataViewItem | |
2051 | wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent, | |
2052 | const wxString &text, | |
2053 | const wxIcon& icon, | |
2054 | const wxIcon& expanded, | |
2055 | wxClientData * data) | |
2056 | { | |
2057 | wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent ); | |
2058 | if (!parent_node) return wxDataViewItem(0); | |
2059 | ||
2060 | wxDataViewTreeStoreContainerNode *node = | |
2061 | new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data ); | |
2062 | parent_node->GetChildren().Append( node ); | |
2063 | ||
2064 | return node->GetItem(); | |
2065 | } | |
2066 | ||
2067 | wxDataViewItem | |
2068 | wxDataViewTreeStore::InsertContainer(const wxDataViewItem& parent, | |
2069 | const wxDataViewItem& previous, | |
2070 | const wxString& text, | |
2071 | const wxIcon& icon, | |
2072 | const wxIcon& expanded, | |
2073 | wxClientData * data) | |
2074 | { | |
2075 | wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent ); | |
2076 | if (!parent_node) return wxDataViewItem(0); | |
2077 | ||
2078 | wxDataViewTreeStoreNode *previous_node = FindNode( previous ); | |
2079 | int pos = parent_node->GetChildren().IndexOf( previous_node ); | |
2080 | if (pos == wxNOT_FOUND) return wxDataViewItem(0); | |
2081 | ||
2082 | wxDataViewTreeStoreContainerNode *node = | |
2083 | new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data ); | |
2084 | parent_node->GetChildren().Insert( (size_t) pos, node ); | |
2085 | ||
2086 | return node->GetItem(); | |
2087 | } | |
2088 | ||
2089 | bool wxDataViewTreeStore::IsContainer( const wxDataViewItem& item ) const | |
2090 | { | |
2091 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2092 | if (!node) return false; | |
2093 | ||
2094 | return node->IsContainer(); | |
2095 | } | |
2096 | ||
2097 | wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const | |
2098 | { | |
2099 | wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent ); | |
2100 | if (!parent_node) return wxDataViewItem(0); | |
2101 | ||
2102 | wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos ); | |
2103 | if (node) | |
2104 | return wxDataViewItem(node->GetData()); | |
2105 | ||
2106 | return wxDataViewItem(0); | |
2107 | } | |
2108 | ||
2109 | int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const | |
2110 | { | |
2111 | wxDataViewTreeStoreNode *node = FindNode( parent ); | |
2112 | if (!node) return -1; | |
2113 | ||
2114 | if (!node->IsContainer()) | |
2115 | return 0; | |
2116 | ||
2117 | wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node; | |
2118 | return (int) container_node->GetChildren().GetCount(); | |
2119 | } | |
2120 | ||
2121 | void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text ) | |
2122 | { | |
2123 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2124 | if (!node) return; | |
2125 | ||
2126 | node->SetText( text ); | |
2127 | } | |
2128 | ||
2129 | wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const | |
2130 | { | |
2131 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2132 | if (!node) return wxEmptyString; | |
2133 | ||
2134 | return node->GetText(); | |
2135 | } | |
2136 | ||
2137 | void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon ) | |
2138 | { | |
2139 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2140 | if (!node) return; | |
2141 | ||
2142 | node->SetIcon( icon ); | |
2143 | } | |
2144 | ||
2145 | const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const | |
2146 | { | |
2147 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2148 | if (!node) return wxNullIcon; | |
2149 | ||
2150 | return node->GetIcon(); | |
2151 | } | |
2152 | ||
2153 | void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon ) | |
2154 | { | |
2155 | wxDataViewTreeStoreContainerNode *node = FindContainerNode( item ); | |
2156 | if (!node) return; | |
2157 | ||
2158 | node->SetExpandedIcon( icon ); | |
2159 | } | |
2160 | ||
2161 | const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const | |
2162 | { | |
2163 | wxDataViewTreeStoreContainerNode *node = FindContainerNode( item ); | |
2164 | if (!node) return wxNullIcon; | |
2165 | ||
2166 | return node->GetExpandedIcon(); | |
2167 | } | |
2168 | ||
2169 | void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data ) | |
2170 | { | |
2171 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2172 | if (!node) return; | |
2173 | ||
2174 | node->SetData( data ); | |
2175 | } | |
2176 | ||
2177 | wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const | |
2178 | { | |
2179 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2180 | if (!node) return NULL; | |
2181 | ||
2182 | return node->GetData(); | |
2183 | } | |
2184 | ||
2185 | void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item ) | |
2186 | { | |
2187 | if (!item.IsOk()) return; | |
2188 | ||
2189 | wxDataViewItem parent_item = GetParent( item ); | |
2190 | ||
2191 | wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item ); | |
2192 | if (!parent_node) return; | |
2193 | ||
2194 | wxDataViewTreeStoreContainerNode *node = FindContainerNode( item ); | |
2195 | if (!node) return; | |
2196 | ||
2197 | parent_node->GetChildren().DeleteObject( node ); | |
2198 | } | |
2199 | ||
2200 | void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item ) | |
2201 | { | |
2202 | wxDataViewTreeStoreContainerNode *node = FindContainerNode( item ); | |
2203 | if (!node) return; | |
2204 | ||
2205 | node->GetChildren().clear(); | |
2206 | } | |
2207 | ||
2208 | void wxDataViewTreeStore::DeleteAllItems() | |
2209 | { | |
2210 | DeleteChildren(wxDataViewItem(m_root)); | |
2211 | } | |
2212 | ||
2213 | void | |
2214 | wxDataViewTreeStore::GetValue(wxVariant &variant, | |
2215 | const wxDataViewItem &item, | |
2216 | unsigned int WXUNUSED(col)) const | |
2217 | { | |
2218 | // if (col != 0) return; | |
2219 | ||
2220 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2221 | if (!node) return; | |
2222 | ||
2223 | wxIcon icon( node->GetIcon()); | |
2224 | if (node->IsContainer()) | |
2225 | { | |
2226 | wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node; | |
2227 | if (container->IsExpanded() && container->GetExpandedIcon().IsOk()) | |
2228 | icon = container->GetExpandedIcon(); | |
2229 | } | |
2230 | ||
2231 | wxDataViewIconText data( node->GetText(), icon ); | |
2232 | ||
2233 | variant << data; | |
2234 | } | |
2235 | ||
2236 | bool | |
2237 | wxDataViewTreeStore::SetValue(const wxVariant& variant, | |
2238 | const wxDataViewItem& item, | |
2239 | unsigned int WXUNUSED(col)) | |
2240 | { | |
2241 | // if (col != 0) return false; | |
2242 | ||
2243 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2244 | if (!node) return false; | |
2245 | ||
2246 | wxDataViewIconText data; | |
2247 | ||
2248 | data << variant; | |
2249 | ||
2250 | node->SetText( data.GetText() ); | |
2251 | node->SetIcon( data.GetIcon() ); | |
2252 | ||
2253 | return true; | |
2254 | } | |
2255 | ||
2256 | wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const | |
2257 | { | |
2258 | wxDataViewTreeStoreNode *node = FindNode( item ); | |
2259 | if (!node) return wxDataViewItem(0); | |
2260 | ||
2261 | wxDataViewTreeStoreNode *parent = node->GetParent(); | |
2262 | if (!parent) return wxDataViewItem(0); | |
2263 | ||
2264 | if (parent == m_root) | |
2265 | return wxDataViewItem(0); | |
2266 | ||
2267 | return parent->GetItem(); | |
2268 | } | |
2269 | ||
2270 | unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const | |
2271 | { | |
2272 | wxDataViewTreeStoreContainerNode *node = FindContainerNode( item ); | |
2273 | if (!node) return 0; | |
2274 | ||
2275 | wxDataViewTreeStoreNodeList::iterator iter; | |
2276 | for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++) | |
2277 | { | |
2278 | wxDataViewTreeStoreNode* child = *iter; | |
2279 | children.Add( child->GetItem() ); | |
2280 | } | |
2281 | ||
2282 | return node->GetChildren().GetCount(); | |
2283 | } | |
2284 | ||
2285 | int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, | |
2286 | unsigned int WXUNUSED(column), bool WXUNUSED(ascending) ) const | |
2287 | { | |
2288 | wxDataViewTreeStoreNode *node1 = FindNode( item1 ); | |
2289 | wxDataViewTreeStoreNode *node2 = FindNode( item2 ); | |
2290 | ||
2291 | if (!node1 || !node2) | |
2292 | return 0; | |
2293 | ||
2294 | wxDataViewTreeStoreContainerNode* parent1 = | |
2295 | (wxDataViewTreeStoreContainerNode*) node1->GetParent(); | |
2296 | wxDataViewTreeStoreContainerNode* parent2 = | |
2297 | (wxDataViewTreeStoreContainerNode*) node2->GetParent(); | |
2298 | ||
2299 | if (parent1 != parent2) | |
2300 | { | |
2301 | wxLogError( wxT("Comparing items with different parent.") ); | |
2302 | return 0; | |
2303 | } | |
2304 | ||
2305 | if (node1->IsContainer() && !node2->IsContainer()) | |
2306 | return -1; | |
2307 | ||
2308 | if (node2->IsContainer() && !node1->IsContainer()) | |
2309 | return 1; | |
2310 | ||
2311 | return parent1->GetChildren().IndexOf( node1 ) - parent2->GetChildren().IndexOf( node2 ); | |
2312 | } | |
2313 | ||
2314 | wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const | |
2315 | { | |
2316 | if (!item.IsOk()) | |
2317 | return m_root; | |
2318 | ||
2319 | return (wxDataViewTreeStoreNode*) item.GetID(); | |
2320 | } | |
2321 | ||
2322 | wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const | |
2323 | { | |
2324 | if (!item.IsOk()) | |
2325 | return (wxDataViewTreeStoreContainerNode*) m_root; | |
2326 | ||
2327 | wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID(); | |
2328 | ||
2329 | if (!node->IsContainer()) | |
2330 | return NULL; | |
2331 | ||
2332 | return (wxDataViewTreeStoreContainerNode*) node; | |
2333 | } | |
2334 | ||
2335 | //----------------------------------------------------------------------------- | |
2336 | // wxDataViewTreeCtrl | |
2337 | //----------------------------------------------------------------------------- | |
2338 | ||
2339 | IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl) | |
2340 | ||
2341 | BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl) | |
2342 | EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded) | |
2343 | EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed) | |
2344 | EVT_SIZE( wxDataViewTreeCtrl::OnSize ) | |
2345 | END_EVENT_TABLE() | |
2346 | ||
2347 | bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id, | |
2348 | const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator ) | |
2349 | { | |
2350 | if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) ) | |
2351 | return false; | |
2352 | ||
2353 | // create the standard model and a column in the tree | |
2354 | wxDataViewTreeStore *store = new wxDataViewTreeStore; | |
2355 | AssociateModel( store ); | |
2356 | store->DecRef(); | |
2357 | ||
2358 | AppendIconTextColumn | |
2359 | ( | |
2360 | wxString(), // no label (header is not shown anyhow) | |
2361 | 0, // the only model column | |
2362 | wxDATAVIEW_CELL_EDITABLE, | |
2363 | -1, // default width | |
2364 | wxALIGN_NOT, // and alignment | |
2365 | 0 // not resizable | |
2366 | ); | |
2367 | ||
2368 | return true; | |
2369 | } | |
2370 | ||
2371 | wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent, | |
2372 | const wxString &text, int iconIndex, wxClientData *data ) | |
2373 | { | |
2374 | wxDataViewItem res = GetStore()-> | |
2375 | AppendItem( parent, text, GetImage(iconIndex), data ); | |
2376 | ||
2377 | GetStore()->ItemAdded( parent, res ); | |
2378 | ||
2379 | return res; | |
2380 | } | |
2381 | ||
2382 | wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent, | |
2383 | const wxString &text, int iconIndex, wxClientData *data ) | |
2384 | { | |
2385 | wxDataViewItem res = GetStore()-> | |
2386 | PrependItem( parent, text, GetImage(iconIndex), data ); | |
2387 | ||
2388 | GetStore()->ItemAdded( parent, res ); | |
2389 | ||
2390 | return res; | |
2391 | } | |
2392 | ||
2393 | wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous, | |
2394 | const wxString &text, int iconIndex, wxClientData *data ) | |
2395 | { | |
2396 | wxDataViewItem res = GetStore()-> | |
2397 | InsertItem( parent, previous, text, GetImage(iconIndex), data ); | |
2398 | ||
2399 | GetStore()->ItemAdded( parent, res ); | |
2400 | ||
2401 | return res; | |
2402 | } | |
2403 | ||
2404 | wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent, | |
2405 | const wxString &text, int iconIndex, int expandedIndex, wxClientData *data ) | |
2406 | { | |
2407 | wxDataViewItem res = GetStore()-> | |
2408 | PrependContainer( parent, text, | |
2409 | GetImage(iconIndex), GetImage(expandedIndex), data ); | |
2410 | ||
2411 | GetStore()->ItemAdded( parent, res ); | |
2412 | ||
2413 | return res; | |
2414 | } | |
2415 | ||
2416 | wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent, | |
2417 | const wxString &text, int iconIndex, int expandedIndex, wxClientData *data ) | |
2418 | { | |
2419 | wxDataViewItem res = GetStore()-> | |
2420 | AppendContainer( parent, text, | |
2421 | GetImage(iconIndex), GetImage(expandedIndex), data ); | |
2422 | ||
2423 | GetStore()->ItemAdded( parent, res ); | |
2424 | ||
2425 | return res; | |
2426 | } | |
2427 | ||
2428 | wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous, | |
2429 | const wxString &text, int iconIndex, int expandedIndex, wxClientData *data ) | |
2430 | { | |
2431 | wxDataViewItem res = GetStore()-> | |
2432 | InsertContainer( parent, previous, text, | |
2433 | GetImage(iconIndex), GetImage(expandedIndex), data ); | |
2434 | ||
2435 | GetStore()->ItemAdded( parent, res ); | |
2436 | ||
2437 | return res; | |
2438 | } | |
2439 | ||
2440 | void wxDataViewTreeCtrl::SetItemText( const wxDataViewItem& item, const wxString &text ) | |
2441 | { | |
2442 | GetStore()->SetItemText(item,text); | |
2443 | ||
2444 | // notify control | |
2445 | GetStore()->ValueChanged( item, 0 ); | |
2446 | } | |
2447 | ||
2448 | void wxDataViewTreeCtrl::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon ) | |
2449 | { | |
2450 | GetStore()->SetItemIcon(item,icon); | |
2451 | ||
2452 | // notify control | |
2453 | GetStore()->ValueChanged( item, 0 ); | |
2454 | } | |
2455 | ||
2456 | void wxDataViewTreeCtrl::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon ) | |
2457 | { | |
2458 | GetStore()->SetItemExpandedIcon(item,icon); | |
2459 | ||
2460 | // notify control | |
2461 | GetStore()->ValueChanged( item, 0 ); | |
2462 | } | |
2463 | ||
2464 | void wxDataViewTreeCtrl::DeleteItem( const wxDataViewItem& item ) | |
2465 | { | |
2466 | wxDataViewItem parent_item = GetStore()->GetParent( item ); | |
2467 | ||
2468 | GetStore()->DeleteItem(item); | |
2469 | ||
2470 | // notify control | |
2471 | GetStore()->ItemDeleted( parent_item, item ); | |
2472 | } | |
2473 | ||
2474 | void wxDataViewTreeCtrl::DeleteChildren( const wxDataViewItem& item ) | |
2475 | { | |
2476 | wxDataViewTreeStoreContainerNode *node = GetStore()->FindContainerNode( item ); | |
2477 | if (!node) return; | |
2478 | ||
2479 | wxDataViewItemArray array; | |
2480 | wxDataViewTreeStoreNodeList::iterator iter; | |
2481 | for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++) | |
2482 | { | |
2483 | wxDataViewTreeStoreNode* child = *iter; | |
2484 | array.Add( child->GetItem() ); | |
2485 | } | |
2486 | ||
2487 | GetStore()->DeleteChildren( item ); | |
2488 | ||
2489 | // notify control | |
2490 | GetStore()->ItemsDeleted( item, array ); | |
2491 | } | |
2492 | ||
2493 | void wxDataViewTreeCtrl::DeleteAllItems() | |
2494 | { | |
2495 | GetStore()->DeleteAllItems(); | |
2496 | ||
2497 | GetStore()->Cleared(); | |
2498 | } | |
2499 | ||
2500 | void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event ) | |
2501 | { | |
2502 | if (HasImageList()) return; | |
2503 | ||
2504 | wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() ); | |
2505 | if (!container) return; | |
2506 | ||
2507 | container->SetExpanded( true ); | |
2508 | ||
2509 | GetStore()->ItemChanged( event.GetItem() ); | |
2510 | } | |
2511 | ||
2512 | void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event ) | |
2513 | { | |
2514 | if (HasImageList()) return; | |
2515 | ||
2516 | wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() ); | |
2517 | if (!container) return; | |
2518 | ||
2519 | container->SetExpanded( false ); | |
2520 | ||
2521 | GetStore()->ItemChanged( event.GetItem() ); | |
2522 | } | |
2523 | ||
2524 | void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event ) | |
2525 | { | |
2526 | #if defined(wxUSE_GENERICDATAVIEWCTRL) | |
2527 | // automatically resize our only column to take the entire control width | |
2528 | if ( GetColumnCount() ) | |
2529 | { | |
2530 | wxSize size = GetClientSize(); | |
2531 | GetColumn(0)->SetWidth(size.x); | |
2532 | } | |
2533 | #endif | |
2534 | event.Skip( true ); | |
2535 | } | |
2536 | ||
2537 | #endif // wxUSE_DATAVIEWCTRL | |
2538 |