]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/listctrl_mac.cpp
ask for BPP for .bmp extension, not .bpp one
[wxWidgets.git] / src / mac / carbon / listctrl_mac.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/listctrl_mac.cpp
3 // Purpose: wxListCtrl
4 // Author: Julian Smart
5 // Modified by: Agron Selimaj
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_LISTCTRL
28
29 #include "wx/listctrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #endif
34
35 #include "wx/mac/uma.h"
36
37 #include "wx/imaglist.h"
38 #include "wx/sysopt.h"
39 #include "wx/timer.h"
40
41 #define wxMAC_ALWAYS_USE_GENERIC_LISTCTRL wxT("mac.listctrl.always_use_generic")
42
43 #if wxUSE_EXTENDED_RTTI
44 WX_DEFINE_FLAGS( wxListCtrlStyle )
45
46 wxBEGIN_FLAGS( wxListCtrlStyle )
47 // new style border flags, we put them first to
48 // use them for streaming out
49 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
50 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
51 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
52 wxFLAGS_MEMBER(wxBORDER_RAISED)
53 wxFLAGS_MEMBER(wxBORDER_STATIC)
54 wxFLAGS_MEMBER(wxBORDER_NONE)
55
56 // old style border flags
57 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
58 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
59 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
60 wxFLAGS_MEMBER(wxRAISED_BORDER)
61 wxFLAGS_MEMBER(wxSTATIC_BORDER)
62 wxFLAGS_MEMBER(wxBORDER)
63
64 // standard window styles
65 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
66 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
67 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
68 wxFLAGS_MEMBER(wxWANTS_CHARS)
69 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
70 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
71 wxFLAGS_MEMBER(wxVSCROLL)
72 wxFLAGS_MEMBER(wxHSCROLL)
73
74 wxFLAGS_MEMBER(wxLC_LIST)
75 wxFLAGS_MEMBER(wxLC_REPORT)
76 wxFLAGS_MEMBER(wxLC_ICON)
77 wxFLAGS_MEMBER(wxLC_SMALL_ICON)
78 wxFLAGS_MEMBER(wxLC_ALIGN_TOP)
79 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT)
80 wxFLAGS_MEMBER(wxLC_AUTOARRANGE)
81 wxFLAGS_MEMBER(wxLC_USER_TEXT)
82 wxFLAGS_MEMBER(wxLC_EDIT_LABELS)
83 wxFLAGS_MEMBER(wxLC_NO_HEADER)
84 wxFLAGS_MEMBER(wxLC_SINGLE_SEL)
85 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING)
86 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING)
87 wxFLAGS_MEMBER(wxLC_VIRTUAL)
88
89 wxEND_FLAGS( wxListCtrlStyle )
90
91 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl, wxControl,"wx/listctrl.h")
92
93 wxBEGIN_PROPERTIES_TABLE(wxListCtrl)
94 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
95
96 wxPROPERTY_FLAGS( WindowStyle , wxListCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
97 wxEND_PROPERTIES_TABLE()
98
99 wxBEGIN_HANDLERS_TABLE(wxListCtrl)
100 wxEND_HANDLERS_TABLE()
101
102 wxCONSTRUCTOR_5( wxListCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
103
104 /*
105 TODO : Expose more information of a list's layout etc. via appropriate objects (à la NotebookPageInfo)
106 */
107 #else
108 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
109 #endif
110
111 IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
112 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
113
114 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
115
116 WX_DECLARE_EXPORTED_LIST(wxListItem, wxListItemList);
117 #include "wx/listimpl.cpp"
118 WX_DEFINE_LIST(wxListItemList)
119 WX_DEFINE_LIST(wxColumnList)
120
121 // so we can check for column clicks
122 static const EventTypeSpec eventList[] =
123 {
124 { kEventClassControl, kEventControlHit },
125 { kEventClassControl, kEventControlDraw }
126 };
127
128 static pascal OSStatus wxMacListCtrlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
129 {
130 OSStatus result = eventNotHandledErr ;
131
132 wxMacCarbonEvent cEvent( event ) ;
133
134 ControlRef controlRef ;
135 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
136
137 wxListCtrl *window = (wxListCtrl*) data ;
138 wxListEvent le( wxEVT_COMMAND_LIST_COL_CLICK, window->GetId() );
139 le.SetEventObject( window );
140
141 switch ( GetEventKind( event ) )
142 {
143 // check if the column was clicked on and fire an event if so
144 case kEventControlHit :
145 {
146 ControlPartCode result = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart, typeControlPartCode) ;
147 if (result == kControlButtonPart){
148 DataBrowserPropertyID col;
149 GetDataBrowserSortProperty(controlRef, &col);
150 int column = col - kMinColumnId;
151 le.m_col = column;
152 // FIXME: we can't use the sort property for virtual listctrls
153 // so we need to find a better way to determine which column was clicked...
154 if (!window->IsVirtual())
155 window->GetEventHandler()->ProcessEvent( le );
156 }
157 result = CallNextEventHandler(handler, event);
158 break;
159 }
160 case kEventControlDraw:
161 {
162 CGContextRef context = cEvent.GetParameter<CGContextRef>(kEventParamCGContextRef, typeCGContextRef) ;
163 window->MacSetDrawingContext(context);
164 result = CallNextEventHandler(handler, event);
165 window->MacSetDrawingContext(NULL);
166 break;
167 }
168 default :
169 break ;
170 }
171
172
173 return result ;
174 }
175
176 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacListCtrlEventHandler )
177
178 class wxMacListCtrlItem : public wxMacListBoxItem
179 {
180 public:
181 wxMacListCtrlItem();
182
183 virtual void Notification(wxMacDataItemBrowserControl *owner ,
184 DataBrowserItemNotification message,
185 DataBrowserItemDataRef itemData ) const;
186
187 virtual void SetColumnInfo( unsigned int column, wxListItem* item );
188 virtual wxListItem* GetColumnInfo( unsigned int column );
189 virtual bool HasColumnInfo( unsigned int column );
190
191 virtual void SetColumnTextValue( unsigned int column, const wxString& text );
192 virtual wxString GetColumnTextValue( unsigned int column );
193
194 virtual int GetColumnImageValue( unsigned int column );
195 virtual void SetColumnImageValue( unsigned int column, int imageIndex );
196
197 virtual ~wxMacListCtrlItem();
198 protected:
199 wxListItemList m_rowItems;
200 };
201
202 DataBrowserDrawItemUPP gDataBrowserDrawItemUPP = NULL;
203 //DataBrowserEditItemUPP gDataBrowserEditItemUPP = NULL;
204 DataBrowserHitTestUPP gDataBrowserHitTestUPP = NULL;
205
206 // TODO: Make a better name!!
207 class wxMacDataBrowserListCtrlControl : public wxMacDataItemBrowserControl
208 {
209 public:
210 wxMacDataBrowserListCtrlControl( wxWindow *peer, const wxPoint& pos, const wxSize& size, long style );
211 virtual ~wxMacDataBrowserListCtrlControl();
212
213 // create a list item (can be a subclass of wxMacListBoxItem)
214
215 virtual wxMacDataItem* CreateItem();
216
217 virtual void MacInsertItem( unsigned int n, wxListItem* item );
218 virtual void MacSetColumnInfo( unsigned int row, unsigned int column, wxListItem* item );
219 virtual void MacGetColumnInfo( unsigned int row, unsigned int column, wxListItem& item );
220 virtual void UpdateState(wxMacDataItem* dataItem, wxListItem* item);
221
222 protected:
223 // we need to override to provide specialized handling for virtual wxListCtrls
224 virtual OSStatus GetSetItemData(DataBrowserItemID itemID,
225 DataBrowserPropertyID property,
226 DataBrowserItemDataRef itemData,
227 Boolean changeValue );
228
229 virtual void ItemNotification(
230 DataBrowserItemID itemID,
231 DataBrowserItemNotification message,
232 DataBrowserItemDataRef itemData);
233
234 virtual Boolean CompareItems(DataBrowserItemID itemOneID,
235 DataBrowserItemID itemTwoID,
236 DataBrowserPropertyID sortProperty);
237
238 static pascal void DataBrowserDrawItemProc(ControlRef browser,
239 DataBrowserItemID item,
240 DataBrowserPropertyID property,
241 DataBrowserItemState itemState,
242 const Rect *theRect,
243 SInt16 gdDepth,
244 Boolean colorDevice);
245
246 virtual void DrawItem(DataBrowserItemID itemID,
247 DataBrowserPropertyID property,
248 DataBrowserItemState itemState,
249 const Rect *itemRect,
250 SInt16 gdDepth,
251 Boolean colorDevice);
252
253 static pascal Boolean DataBrowserEditTextProc(ControlRef browser,
254 DataBrowserItemID item,
255 DataBrowserPropertyID property,
256 CFStringRef theString,
257 Rect *maxEditTextRect,
258 Boolean *shrinkToFit);
259
260 static pascal Boolean DataBrowserHitTestProc(ControlRef browser,
261 DataBrowserItemID itemID,
262 DataBrowserPropertyID property,
263 const Rect *theRect,
264 const Rect *mouseRect) { return true; }
265
266 virtual bool ConfirmEditText(DataBrowserItemID item,
267 DataBrowserPropertyID property,
268 CFStringRef theString,
269 Rect *maxEditTextRect,
270 Boolean *shrinkToFit);
271
272
273
274 wxClientDataType m_clientDataItemsType;
275 bool m_isVirtual;
276
277 };
278
279 class wxMacListCtrlEventDelegate : public wxEvtHandler
280 {
281 public:
282 wxMacListCtrlEventDelegate( wxListCtrl* list, int id );
283 virtual bool ProcessEvent( wxEvent& event );
284
285 private:
286 wxListCtrl* m_list;
287 int m_id;
288 };
289
290 wxMacListCtrlEventDelegate::wxMacListCtrlEventDelegate( wxListCtrl* list, int id )
291 {
292 m_list = list;
293 m_id = id;
294 }
295
296 bool wxMacListCtrlEventDelegate::ProcessEvent( wxEvent& event )
297 {
298 // even though we use a generic list ctrl underneath, make sure
299 // we present ourselves as wxListCtrl.
300 event.SetEventObject( m_list );
301 event.SetId( m_id );
302
303 if ( !event.IsKindOf( CLASSINFO( wxCommandEvent ) ) )
304 {
305 if (m_list->GetEventHandler()->ProcessEvent( event ))
306 return true;
307 }
308 return wxEvtHandler::ProcessEvent(event);
309 }
310
311 //-----------------------------------------------------------------------------
312 // wxListCtrlRenameTimer (internal)
313 //-----------------------------------------------------------------------------
314
315 class wxListCtrlRenameTimer: public wxTimer
316 {
317 private:
318 wxListCtrl *m_owner;
319
320 public:
321 wxListCtrlRenameTimer( wxListCtrl *owner );
322 void Notify();
323 };
324
325 //-----------------------------------------------------------------------------
326 // wxListCtrlTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
327 //-----------------------------------------------------------------------------
328
329 class wxListCtrlTextCtrlWrapper : public wxEvtHandler
330 {
331 public:
332 // NB: text must be a valid object but not Create()d yet
333 wxListCtrlTextCtrlWrapper(wxListCtrl *owner,
334 wxTextCtrl *text,
335 long itemEdit);
336
337 wxTextCtrl *GetText() const { return m_text; }
338
339 void AcceptChangesAndFinish();
340
341 protected:
342 void OnChar( wxKeyEvent &event );
343 void OnKeyUp( wxKeyEvent &event );
344 void OnKillFocus( wxFocusEvent &event );
345
346 bool AcceptChanges();
347 void Finish();
348
349 private:
350 wxListCtrl *m_owner;
351 wxTextCtrl *m_text;
352 wxString m_startValue;
353 long m_itemEdited;
354 bool m_finished;
355 bool m_aboutToFinish;
356
357 DECLARE_EVENT_TABLE()
358 };
359
360 //-----------------------------------------------------------------------------
361 // wxListCtrlRenameTimer (internal)
362 //-----------------------------------------------------------------------------
363
364 wxListCtrlRenameTimer::wxListCtrlRenameTimer( wxListCtrl *owner )
365 {
366 m_owner = owner;
367 }
368
369 void wxListCtrlRenameTimer::Notify()
370 {
371 m_owner->OnRenameTimer();
372 }
373
374 //-----------------------------------------------------------------------------
375 // wxListCtrlTextCtrlWrapper (internal)
376 //-----------------------------------------------------------------------------
377
378 BEGIN_EVENT_TABLE(wxListCtrlTextCtrlWrapper, wxEvtHandler)
379 EVT_CHAR (wxListCtrlTextCtrlWrapper::OnChar)
380 EVT_KEY_UP (wxListCtrlTextCtrlWrapper::OnKeyUp)
381 EVT_KILL_FOCUS (wxListCtrlTextCtrlWrapper::OnKillFocus)
382 END_EVENT_TABLE()
383
384 wxListCtrlTextCtrlWrapper::wxListCtrlTextCtrlWrapper(wxListCtrl *owner,
385 wxTextCtrl *text,
386 long itemEdit)
387 : m_startValue(owner->GetItemText(itemEdit)),
388 m_itemEdited(itemEdit)
389 {
390 m_owner = owner;
391 m_text = text;
392 m_finished = false;
393 m_aboutToFinish = false;
394
395 wxRect rectLabel;
396 int offset = 8;
397 owner->GetItemRect(itemEdit, rectLabel);
398
399 m_text->Create(owner, wxID_ANY, m_startValue,
400 wxPoint(rectLabel.x+offset,rectLabel.y),
401 wxSize(rectLabel.width-offset,rectLabel.height));
402 m_text->SetFocus();
403
404 m_text->PushEventHandler(this);
405 }
406
407 void wxListCtrlTextCtrlWrapper::Finish()
408 {
409 if ( !m_finished )
410 {
411 m_finished = true;
412
413 m_text->RemoveEventHandler(this);
414 m_owner->FinishEditing(m_text);
415
416 wxPendingDelete.Append( this );
417 }
418 }
419
420 bool wxListCtrlTextCtrlWrapper::AcceptChanges()
421 {
422 const wxString value = m_text->GetValue();
423
424 if ( value == m_startValue )
425 // nothing changed, always accept
426 return true;
427
428 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
429 // vetoed by the user
430 return false;
431
432 // accepted, do rename the item
433 m_owner->SetItemText(m_itemEdited, value);
434
435 return true;
436 }
437
438 void wxListCtrlTextCtrlWrapper::AcceptChangesAndFinish()
439 {
440 m_aboutToFinish = true;
441
442 // Notify the owner about the changes
443 AcceptChanges();
444
445 // Even if vetoed, close the control (consistent with MSW)
446 Finish();
447 }
448
449 void wxListCtrlTextCtrlWrapper::OnChar( wxKeyEvent &event )
450 {
451 switch ( event.m_keyCode )
452 {
453 case WXK_RETURN:
454 AcceptChangesAndFinish();
455 break;
456
457 case WXK_ESCAPE:
458 m_owner->OnRenameCancelled( m_itemEdited );
459 Finish();
460 break;
461
462 default:
463 event.Skip();
464 }
465 }
466
467 void wxListCtrlTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
468 {
469 if (m_finished)
470 {
471 event.Skip();
472 return;
473 }
474
475 // auto-grow the textctrl:
476 wxSize parentSize = m_owner->GetSize();
477 wxPoint myPos = m_text->GetPosition();
478 wxSize mySize = m_text->GetSize();
479 int sx, sy;
480 m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy);
481 if (myPos.x + sx > parentSize.x)
482 sx = parentSize.x - myPos.x;
483 if (mySize.x > sx)
484 sx = mySize.x;
485 m_text->SetSize(sx, wxDefaultCoord);
486
487 event.Skip();
488 }
489
490 void wxListCtrlTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
491 {
492 if ( !m_finished && !m_aboutToFinish )
493 {
494 if ( !AcceptChanges() )
495 m_owner->OnRenameCancelled( m_itemEdited );
496
497 Finish();
498 }
499
500 // We must let the native text control handle focus
501 event.Skip();
502 }
503
504 BEGIN_EVENT_TABLE(wxListCtrl, wxControl)
505 EVT_LEFT_DOWN(wxListCtrl::OnLeftDown)
506 EVT_LEFT_DCLICK(wxListCtrl::OnDblClick)
507 END_EVENT_TABLE()
508
509 // ============================================================================
510 // implementation
511 // ============================================================================
512
513 wxMacListControl* wxListCtrl::GetPeer() const
514 {
515 return dynamic_cast<wxMacListControl*>(m_peer);
516 }
517
518 // ----------------------------------------------------------------------------
519 // wxListCtrl construction
520 // ----------------------------------------------------------------------------
521
522 void wxListCtrl::Init()
523 {
524 m_imageListNormal = NULL;
525 m_imageListSmall = NULL;
526 m_imageListState = NULL;
527
528 // keep track of if we created our own image lists, or if they were assigned
529 // to us.
530 m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = false;
531 m_colCount = 0;
532 m_count = 0;
533 m_textCtrl = NULL;
534 m_genericImpl = NULL;
535 m_dbImpl = NULL;
536 m_compareFunc = NULL;
537 m_compareFuncData = 0;
538 m_colsInfo = wxColumnList();
539 m_textColor = wxNullColour;
540 m_bgColor = wxNullColour;
541 m_textctrlWrapper = NULL;
542 m_current = -1;
543 m_renameTimer = new wxListCtrlRenameTimer( this );
544 }
545
546 class wxGenericListCtrlHook : public wxGenericListCtrl
547 {
548 public:
549 wxGenericListCtrlHook(wxListCtrl* parent,
550 wxWindowID id,
551 const wxPoint& pos,
552 const wxSize& size,
553 long style,
554 const wxValidator& validator,
555 const wxString& name)
556 : wxGenericListCtrl(parent, id, pos, size, style, validator, name),
557 m_nativeListCtrl(parent)
558 {
559 }
560
561 protected:
562 virtual wxListItemAttr * OnGetItemAttr(long item) const
563 {
564 return m_nativeListCtrl->OnGetItemAttr(item);
565 }
566
567 virtual int OnGetItemImage(long item) const
568 {
569 return m_nativeListCtrl->OnGetItemImage(item);
570 }
571
572 virtual int OnGetItemColumnImage(long item, long column) const
573 {
574 return m_nativeListCtrl->OnGetItemColumnImage(item, column);
575 }
576
577 virtual wxString OnGetItemText(long item, long column) const
578 {
579 return m_nativeListCtrl->OnGetItemText(item, column);
580 }
581
582 wxListCtrl* m_nativeListCtrl;
583
584 };
585
586 void wxListCtrl::OnLeftDown(wxMouseEvent& event)
587 {
588 if ( m_textctrlWrapper )
589 {
590 m_current = -1;
591 m_textctrlWrapper->AcceptChangesAndFinish();
592 }
593
594 int hitResult;
595 long current = HitTest(event.GetPosition(), hitResult);
596 if ((current == m_current) &&
597 (hitResult == wxLIST_HITTEST_ONITEM) &&
598 HasFlag(wxLC_EDIT_LABELS) )
599 {
600 m_renameTimer->Start( 100, true );
601 }
602 else
603 {
604 m_current = current;
605 }
606 event.Skip();
607 }
608
609 void wxListCtrl::OnDblClick(wxMouseEvent& event)
610 {
611 m_current = -1;
612 event.Skip();
613 }
614
615 bool wxListCtrl::Create(wxWindow *parent,
616 wxWindowID id,
617 const wxPoint& pos,
618 const wxSize& size,
619 long style,
620 const wxValidator& validator,
621 const wxString& name)
622 {
623
624 // for now, we'll always use the generic list control for ICON and LIST views,
625 // because they dynamically change the number of columns on resize.
626 // Also, allow the user to set it to use the list ctrl as well.
627 if ( (wxSystemOptions::HasOption( wxMAC_ALWAYS_USE_GENERIC_LISTCTRL )
628 && (wxSystemOptions::GetOptionInt( wxMAC_ALWAYS_USE_GENERIC_LISTCTRL ) == 1)) ||
629 (style & wxLC_ICON) || (style & wxLC_SMALL_ICON) || (style & wxLC_LIST) )
630 {
631 m_macIsUserPane = true;
632
633 long paneStyle = style;
634 paneStyle &= ~wxSIMPLE_BORDER;
635 paneStyle &= ~wxDOUBLE_BORDER;
636 paneStyle &= ~wxSUNKEN_BORDER;
637 paneStyle &= ~wxRAISED_BORDER;
638 paneStyle &= ~wxSTATIC_BORDER;
639 if ( !wxWindow::Create(parent, id, pos, size, paneStyle | wxNO_BORDER, name) )
640 return false;
641
642 // since the generic control is a child, make sure we position it at 0, 0
643 m_genericImpl = new wxGenericListCtrlHook(this, id, wxPoint(0, 0), size, style, validator, name);
644 m_genericImpl->PushEventHandler( new wxMacListCtrlEventDelegate( this, GetId() ) );
645 return true;
646 }
647
648 else
649 {
650 m_macIsUserPane = false;
651
652 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
653 return false;
654 m_dbImpl = new wxMacDataBrowserListCtrlControl( this, pos, size, style );
655 m_peer = m_dbImpl;
656
657 MacPostControlCreate( pos, size );
658
659 InstallControlEventHandler( m_peer->GetControlRef() , GetwxMacListCtrlEventHandlerUPP(),
660 GetEventTypeCount(eventList), eventList, this,
661 (EventHandlerRef *)&m_macListCtrlEventHandler);
662 }
663
664 return true;
665 }
666
667 wxListCtrl::~wxListCtrl()
668 {
669 if (m_genericImpl)
670 {
671 m_genericImpl->PopEventHandler(/* deleteHandler = */ true);
672 }
673
674 if (m_ownsImageListNormal)
675 delete m_imageListNormal;
676 if (m_ownsImageListSmall)
677 delete m_imageListSmall;
678 if (m_ownsImageListState)
679 delete m_imageListState;
680
681 delete m_renameTimer;
682 }
683
684 // ----------------------------------------------------------------------------
685 // set/get/change style
686 // ----------------------------------------------------------------------------
687
688 // Add or remove a single window style
689 void wxListCtrl::SetSingleStyle(long style, bool add)
690 {
691 long flag = GetWindowStyleFlag();
692
693 // Get rid of conflicting styles
694 if ( add )
695 {
696 if ( style & wxLC_MASK_TYPE)
697 flag = flag & ~wxLC_MASK_TYPE;
698 if ( style & wxLC_MASK_ALIGN )
699 flag = flag & ~wxLC_MASK_ALIGN;
700 if ( style & wxLC_MASK_SORT )
701 flag = flag & ~wxLC_MASK_SORT;
702 }
703
704 if ( add )
705 flag |= style;
706 else
707 flag &= ~style;
708
709 SetWindowStyleFlag(flag);
710 }
711
712 // Set the whole window style
713 void wxListCtrl::SetWindowStyleFlag(long flag)
714 {
715 if ( flag != m_windowStyle )
716 {
717 m_windowStyle = flag;
718
719 if (m_genericImpl)
720 {
721 m_genericImpl->SetWindowStyleFlag(flag);
722 }
723
724 Refresh();
725 }
726 }
727
728 void wxListCtrl::DoSetSize( int x, int y, int width, int height, int sizeFlags )
729 {
730 wxControl::DoSetSize(x, y, width, height, sizeFlags);
731
732 if (m_genericImpl)
733 m_genericImpl->SetSize(0, 0, width, height, sizeFlags);
734
735 // determine if we need a horizontal scrollbar, and add it if so
736 if (m_dbImpl)
737 {
738 int totalWidth = 0;
739 for (int column = 0; column < GetColumnCount(); column++)
740 {
741 totalWidth += m_dbImpl->GetColumnWidth( column );
742 }
743
744 Boolean vertScrollBar;
745 GetDataBrowserHasScrollBars( m_dbImpl->GetControlRef(), NULL, &vertScrollBar );
746 if (totalWidth > width)
747 SetDataBrowserHasScrollBars( m_dbImpl->GetControlRef(), true, vertScrollBar );
748 else
749 SetDataBrowserHasScrollBars( m_dbImpl->GetControlRef(), false, vertScrollBar );
750 }
751 }
752
753 wxSize wxListCtrl::DoGetBestSize() const
754 {
755 return wxWindow::DoGetBestSize();
756 }
757
758 bool wxListCtrl::SetFont(const wxFont& font)
759 {
760 bool rv = true;
761 rv = wxControl::SetFont(font);
762 if (m_genericImpl)
763 rv = m_genericImpl->SetFont(font);
764 return rv;
765 }
766
767 bool wxListCtrl::SetForegroundColour(const wxColour& colour)
768 {
769 bool rv = true;
770 if (m_genericImpl)
771 rv = m_genericImpl->SetForegroundColour(colour);
772 if (m_dbImpl)
773 SetTextColour(colour);
774 return rv;
775 }
776
777 bool wxListCtrl::SetBackgroundColour(const wxColour& colour)
778 {
779 bool rv = true;
780 if (m_genericImpl)
781 rv = m_genericImpl->SetBackgroundColour(colour);
782 if (m_dbImpl)
783 m_bgColor = colour;
784 return rv;
785 }
786
787 wxColour wxListCtrl::GetBackgroundColour()
788 {
789 if (m_genericImpl)
790 return m_genericImpl->GetBackgroundColour();
791 if (m_dbImpl)
792 return m_bgColor;
793
794 return wxNullColour;
795 }
796
797 // ----------------------------------------------------------------------------
798 // accessors
799 // ----------------------------------------------------------------------------
800
801 // Gets information about this column
802 bool wxListCtrl::GetColumn(int col, wxListItem& item) const
803 {
804 if (m_genericImpl)
805 return m_genericImpl->GetColumn(col, item);
806
807 bool success = true;
808
809 if (m_dbImpl)
810 {
811 wxColumnList::compatibility_iterator node = m_colsInfo.Item( col );
812 wxASSERT_MSG( node, _T("invalid column index in wxMacListCtrlItem") );
813 wxListItem* column = node->GetData();
814
815 long mask = column->GetMask();
816 if (mask & wxLIST_MASK_TEXT)
817 item.SetText(column->GetText());
818 if (mask & wxLIST_MASK_DATA)
819 item.SetData(column->GetData());
820 if (mask & wxLIST_MASK_IMAGE)
821 item.SetImage(column->GetImage());
822 if (mask & wxLIST_MASK_STATE)
823 item.SetState(column->GetState());
824 if (mask & wxLIST_MASK_FORMAT)
825 item.SetAlign(column->GetAlign());
826 if (mask & wxLIST_MASK_WIDTH)
827 item.SetWidth(column->GetWidth());
828 }
829
830 return success;
831 }
832
833 // Sets information about this column
834 bool wxListCtrl::SetColumn(int col, wxListItem& item)
835 {
836 if (m_genericImpl)
837 return m_genericImpl->SetColumn(col, item);
838
839 if (m_dbImpl)
840 {
841 long mask = item.GetMask();
842 if ( col >= (int)m_colsInfo.GetCount() )
843 {
844 wxListItem* listItem = new wxListItem(item);
845 m_colsInfo.Append( listItem );
846 }
847 else
848 {
849 wxListItem listItem;
850 GetColumn( col, listItem );
851
852 if (mask & wxLIST_MASK_TEXT)
853 listItem.SetText(item.GetText());
854 if (mask & wxLIST_MASK_DATA)
855 listItem.SetData(item.GetData());
856 if (mask & wxLIST_MASK_IMAGE)
857 listItem.SetImage(item.GetImage());
858 if (mask & wxLIST_MASK_STATE)
859 listItem.SetState(item.GetState());
860 if (mask & wxLIST_MASK_FORMAT)
861 listItem.SetAlign(item.GetAlign());
862 if (mask & wxLIST_MASK_WIDTH)
863 listItem.SetWidth(item.GetWidth());
864 }
865
866 // change the appearance in the databrowser.
867 DataBrowserListViewHeaderDesc columnDesc;
868 columnDesc.version=kDataBrowserListViewLatestHeaderDesc;
869 verify_noerr( m_dbImpl->GetHeaderDesc( kMinColumnId + col, &columnDesc ) );
870
871 /*
872 if (item.GetMask() & wxLIST_MASK_TEXT)
873 {
874 wxFontEncoding enc;
875 if ( m_font.Ok() )
876 enc = m_font.GetEncoding();
877 else
878 enc = wxLocale::GetSystemEncoding();
879 wxMacCFStringHolder cfTitle;
880 cfTitle.Assign( item.GetText() , enc );
881 if(columnDesc.titleString)
882 CFRelease(columnDesc.titleString);
883 columnDesc.titleString = cfTitle;
884 }
885 */
886
887 if (item.GetMask() & wxLIST_MASK_IMAGE && item.GetImage() != -1 )
888 {
889 columnDesc.btnContentInfo.contentType = kControlContentIconRef;
890 wxImageList* imageList = GetImageList(wxIMAGE_LIST_SMALL);
891 if (imageList && imageList->GetImageCount() > 0 )
892 {
893 wxBitmap bmp = imageList->GetBitmap( item.GetImage() );
894 IconRef icon = bmp.GetBitmapData()->GetIconRef();
895 columnDesc.btnContentInfo.u.iconRef = icon;
896 }
897 }
898
899 verify_noerr( m_dbImpl->SetHeaderDesc( kMinColumnId + col, &columnDesc ) );
900
901 }
902 return true;
903 }
904
905 int wxListCtrl::GetColumnCount() const
906 {
907 if (m_genericImpl)
908 return m_genericImpl->GetColumnCount();
909
910 if (m_dbImpl)
911 {
912 UInt32 count;
913 m_dbImpl->GetColumnCount(&count);
914 return count;
915 }
916
917 return m_colCount;
918 }
919
920 // Gets the column width
921 int wxListCtrl::GetColumnWidth(int col) const
922 {
923 if (m_genericImpl)
924 return m_genericImpl->GetColumnWidth(col);
925
926 if (m_dbImpl)
927 {
928 return m_dbImpl->GetColumnWidth(col);
929 }
930
931 return 0;
932 }
933
934 // Sets the column width
935 bool wxListCtrl::SetColumnWidth(int col, int width)
936 {
937 if (m_genericImpl)
938 return m_genericImpl->SetColumnWidth(col, width);
939
940 if (m_dbImpl)
941 {
942 int mywidth = width;
943 if (width == wxLIST_AUTOSIZE || width == wxLIST_AUTOSIZE_USEHEADER)
944 mywidth = 150;
945
946 if (col == -1)
947 {
948 for (int column = 0; column < GetColumnCount(); column++)
949 {
950 wxListItem colInfo;
951 GetColumn(col, colInfo);
952
953 colInfo.SetWidth(width);
954 SetColumn(col, colInfo);
955
956 m_dbImpl->SetColumnWidth(col, mywidth);
957 }
958 }
959 else
960 {
961 wxListItem colInfo;
962 GetColumn(col, colInfo);
963
964 colInfo.SetWidth(width);
965 SetColumn(col, colInfo);
966 m_dbImpl->SetColumnWidth(col, mywidth);
967 }
968 return true;
969 }
970
971 return false;
972 }
973
974 // Gets the number of items that can fit vertically in the
975 // visible area of the list control (list or report view)
976 // or the total number of items in the list control (icon
977 // or small icon view)
978 int wxListCtrl::GetCountPerPage() const
979 {
980 if (m_genericImpl)
981 return m_genericImpl->GetCountPerPage();
982
983 if (m_dbImpl)
984 {
985 }
986
987 return 1;
988 }
989
990 // Gets the edit control for editing labels.
991 wxTextCtrl* wxListCtrl::GetEditControl() const
992 {
993 if (m_genericImpl)
994 return m_genericImpl->GetEditControl();
995
996 return NULL;
997 }
998
999 // Gets information about the item
1000 bool wxListCtrl::GetItem(wxListItem& info) const
1001 {
1002 if (m_genericImpl)
1003 return m_genericImpl->GetItem(info);
1004
1005 if (m_dbImpl)
1006 {
1007 if (!IsVirtual())
1008 m_dbImpl->MacGetColumnInfo(info.m_itemId, info.m_col, info);
1009 else
1010 {
1011 info.SetText( OnGetItemText(info.m_itemId, info.m_col) );
1012 info.SetImage( OnGetItemColumnImage(info.m_itemId, info.m_col) );
1013 if (info.GetMask() & wxLIST_MASK_STATE)
1014 {
1015 if (IsDataBrowserItemSelected( m_dbImpl->GetControlRef(), info.m_itemId+1 ))
1016 info.SetState(info.GetState() | wxLIST_STATE_SELECTED);
1017 }
1018
1019 wxListItemAttr* attrs = OnGetItemAttr( info.m_itemId );
1020 if (attrs)
1021 {
1022 info.SetFont( attrs->GetFont() );
1023 info.SetBackgroundColour( attrs->GetBackgroundColour() );
1024 info.SetTextColour( attrs->GetTextColour() );
1025 }
1026 }
1027 }
1028 bool success = true;
1029 return success;
1030 }
1031
1032 // Sets information about the item
1033 bool wxListCtrl::SetItem(wxListItem& info)
1034 {
1035 if (m_genericImpl)
1036 return m_genericImpl->SetItem(info);
1037
1038 if (m_dbImpl)
1039 m_dbImpl->MacSetColumnInfo( info.m_itemId, info.m_col, &info );
1040
1041 return true;
1042 }
1043
1044 long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
1045 {
1046 if (m_genericImpl)
1047 return m_genericImpl->SetItem(index, col, label, imageId);
1048
1049 wxListItem info;
1050 info.m_text = label;
1051 info.m_mask = wxLIST_MASK_TEXT;
1052 info.m_itemId = index;
1053 info.m_col = col;
1054 if ( imageId > -1 )
1055 {
1056 info.m_image = imageId;
1057 info.m_mask |= wxLIST_MASK_IMAGE;
1058 }
1059 return SetItem(info);
1060 }
1061
1062
1063 // Gets the item state
1064 int wxListCtrl::GetItemState(long item, long stateMask) const
1065 {
1066 if (m_genericImpl)
1067 return m_genericImpl->GetItemState(item, stateMask);
1068
1069 if (m_dbImpl)
1070 {
1071 if ( HasFlag(wxLC_VIRTUAL) )
1072 {
1073 if (stateMask == wxLIST_STATE_SELECTED)
1074 {
1075 if (IsDataBrowserItemSelected( m_dbImpl->GetControlRef(), item+1 ))
1076 return wxLIST_STATE_SELECTED;
1077 else
1078 return 0;
1079 }
1080 }
1081 else
1082 {
1083 wxListItem info;
1084
1085 info.m_mask = wxLIST_MASK_STATE;
1086 info.m_stateMask = stateMask;
1087 info.m_itemId = item;
1088
1089 if (!GetItem(info))
1090 return 0;
1091
1092 return info.m_state;
1093 }
1094 }
1095
1096 return 0;
1097 }
1098
1099 // Sets the item state
1100 bool wxListCtrl::SetItemState(long item, long state, long stateMask)
1101 {
1102 if (m_genericImpl)
1103 return m_genericImpl->SetItemState(item, state, stateMask);
1104
1105 if (m_dbImpl)
1106 {
1107 DataBrowserSetOption option = kDataBrowserItemsAdd;
1108 if ( stateMask == wxLIST_STATE_SELECTED && state == 0 )
1109 option = kDataBrowserItemsRemove;
1110
1111 if (item == -1)
1112 {
1113 if ( HasFlag(wxLC_VIRTUAL) )
1114 {
1115 wxMacDataItemBrowserSelectionSuppressor suppressor(m_dbImpl);
1116 m_dbImpl->SetSelectedAllItems(option);
1117 }
1118 else
1119 {
1120 for(int i = 0; i < GetItemCount();i++)
1121 {
1122 wxListItem info;
1123 info.m_itemId = i;
1124 info.m_mask = wxLIST_MASK_STATE;
1125 info.m_stateMask = stateMask;
1126 info.m_state = state;
1127 SetItem(info);
1128 }
1129 }
1130 }
1131 else
1132 {
1133 if ( HasFlag(wxLC_VIRTUAL) )
1134 {
1135 long itemID = item+1;
1136 SetDataBrowserSelectedItems(m_dbImpl->GetControlRef(), 1, (DataBrowserItemID*)&itemID, option);
1137 }
1138 else
1139 {
1140 wxListItem info;
1141 info.m_itemId = item;
1142 info.m_mask = wxLIST_MASK_STATE;
1143 info.m_stateMask = stateMask;
1144 info.m_state = state;
1145 return SetItem(info);
1146 }
1147 }
1148 }
1149 return true;
1150 }
1151
1152 // Sets the item image
1153 bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage))
1154 {
1155 return SetItemColumnImage(item, 0, image);
1156 }
1157
1158 // Sets the item image
1159 bool wxListCtrl::SetItemColumnImage(long item, long column, int image)
1160 {
1161 if (m_genericImpl)
1162 return m_genericImpl->SetItemColumnImage(item, column, image);
1163
1164 wxListItem info;
1165
1166 info.m_mask = wxLIST_MASK_IMAGE;
1167 info.m_image = image;
1168 info.m_itemId = item;
1169 info.m_col = column;
1170
1171 return SetItem(info);
1172 }
1173
1174 // Gets the item text
1175 wxString wxListCtrl::GetItemText(long item) const
1176 {
1177 if (m_genericImpl)
1178 return m_genericImpl->GetItemText(item);
1179
1180 wxListItem info;
1181
1182 info.m_mask = wxLIST_MASK_TEXT;
1183 info.m_itemId = item;
1184
1185 if (!GetItem(info))
1186 return wxEmptyString;
1187 return info.m_text;
1188 }
1189
1190 // Sets the item text
1191 void wxListCtrl::SetItemText(long item, const wxString& str)
1192 {
1193 if (m_genericImpl)
1194 return m_genericImpl->SetItemText(item, str);
1195
1196 wxListItem info;
1197
1198 info.m_mask = wxLIST_MASK_TEXT;
1199 info.m_itemId = item;
1200 info.m_text = str;
1201
1202 SetItem(info);
1203 }
1204
1205 // Gets the item data
1206 long wxListCtrl::GetItemData(long item) const
1207 {
1208 if (m_genericImpl)
1209 return m_genericImpl->GetItemData(item);
1210
1211 wxListItem info;
1212
1213 info.m_mask = wxLIST_MASK_DATA;
1214 info.m_itemId = item;
1215
1216 if (!GetItem(info))
1217 return 0;
1218 return info.m_data;
1219 }
1220
1221 // Sets the item data
1222 bool wxListCtrl::SetItemData(long item, long data)
1223 {
1224 if (m_genericImpl)
1225 return m_genericImpl->SetItemData(item, data);
1226
1227 wxListItem info;
1228
1229 info.m_mask = wxLIST_MASK_DATA;
1230 info.m_itemId = item;
1231 info.m_data = data;
1232
1233 return SetItem(info);
1234 }
1235
1236 wxRect wxListCtrl::GetViewRect() const
1237 {
1238 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
1239 _T("wxListCtrl::GetViewRect() only works in icon mode") );
1240
1241 if (m_genericImpl)
1242 return m_genericImpl->GetViewRect();
1243
1244 wxRect rect;
1245 return rect;
1246 }
1247
1248 // Gets the item rectangle
1249 bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
1250 {
1251 if (m_genericImpl)
1252 return m_genericImpl->GetItemRect(item, rect, code);
1253
1254
1255 if (m_dbImpl)
1256 {
1257 DataBrowserItemID id;
1258 DataBrowserPropertyID col = kMinColumnId;
1259 Rect bounds;
1260 DataBrowserPropertyPart part = kDataBrowserPropertyEnclosingPart;
1261 if ( code == wxLIST_RECT_LABEL )
1262 part = kDataBrowserPropertyTextPart;
1263 else if ( code == wxLIST_RECT_ICON )
1264 part = kDataBrowserPropertyIconPart;
1265
1266 if ( !(GetWindowStyleFlag() & wxLC_VIRTUAL) )
1267 {
1268 wxMacDataItem* thisItem = m_dbImpl->GetItemFromLine(item);
1269 id = (DataBrowserItemID) thisItem;
1270 }
1271 else
1272 id = item+1;
1273
1274 GetDataBrowserItemPartBounds( m_dbImpl->GetControlRef(), id, col, part, &bounds );
1275
1276 rect.x = bounds.left;
1277 rect.y = bounds.top;
1278 rect.width = bounds.right - bounds.left; //GetClientSize().x; // we need the width of the whole row, not just the item.
1279 rect.height = bounds.bottom - bounds.top;
1280 //fprintf("id = %d, bounds = %d, %d, %d, %d\n", id, rect.x, rect.y, rect.width, rect.height);
1281 }
1282 return true;
1283 }
1284
1285 // Gets the item position
1286 bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
1287 {
1288 if (m_genericImpl)
1289 return m_genericImpl->GetItemPosition(item, pos);
1290
1291 bool success = false;
1292
1293 if (m_dbImpl)
1294 {
1295 wxRect itemRect;
1296 GetItemRect(item, itemRect);
1297 pos = itemRect.GetPosition();
1298 success = true;
1299 }
1300
1301 return success;
1302 }
1303
1304 // Sets the item position.
1305 bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
1306 {
1307 if (m_genericImpl)
1308 return m_genericImpl->SetItemPosition(item, pos);
1309
1310 return false;
1311 }
1312
1313 // Gets the number of items in the list control
1314 int wxListCtrl::GetItemCount() const
1315 {
1316 if (m_genericImpl)
1317 return m_genericImpl->GetItemCount();
1318
1319 if (m_dbImpl)
1320 return m_dbImpl->MacGetCount();
1321
1322 return m_count;
1323 }
1324
1325 void wxListCtrl::SetItemSpacing( int spacing, bool isSmall )
1326 {
1327 if (m_genericImpl)
1328 m_genericImpl->SetItemSpacing(spacing, isSmall);
1329 }
1330
1331 wxSize wxListCtrl::GetItemSpacing() const
1332 {
1333 if (m_genericImpl)
1334 return m_genericImpl->GetItemSpacing();
1335
1336 return wxSize(0, 0);
1337 }
1338
1339 void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
1340 {
1341 if (m_genericImpl)
1342 {
1343 m_genericImpl->SetItemTextColour(item, col);
1344 return;
1345 }
1346
1347 wxListItem info;
1348 info.m_itemId = item;
1349 info.SetTextColour( col );
1350 SetItem( info );
1351 }
1352
1353 wxColour wxListCtrl::GetItemTextColour( long item ) const
1354 {
1355 if (m_genericImpl)
1356 return m_genericImpl->GetItemTextColour(item);
1357
1358 if (m_dbImpl)
1359 {
1360 wxListItem info;
1361 if (GetItem(info))
1362 return info.GetTextColour();
1363 }
1364 return wxNullColour;
1365 }
1366
1367 void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
1368 {
1369 if (m_genericImpl)
1370 {
1371 m_genericImpl->SetItemBackgroundColour(item, col);
1372 return;
1373 }
1374
1375 wxListItem info;
1376 info.m_itemId = item;
1377 info.SetBackgroundColour( col );
1378 SetItem( info );
1379 }
1380
1381 wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
1382 {
1383 if (m_genericImpl)
1384 return m_genericImpl->GetItemBackgroundColour(item);
1385
1386 if (m_dbImpl)
1387 {
1388 wxListItem info;
1389 if (GetItem(info))
1390 return info.GetBackgroundColour();
1391 }
1392 return wxNullColour;
1393 }
1394
1395 void wxListCtrl::SetItemFont( long item, const wxFont &f )
1396 {
1397 if (m_genericImpl)
1398 {
1399 m_genericImpl->SetItemFont(item, f);
1400 return;
1401 }
1402
1403 wxListItem info;
1404 info.m_itemId = item;
1405 info.SetFont( f );
1406 SetItem( info );
1407 }
1408
1409 wxFont wxListCtrl::GetItemFont( long item ) const
1410 {
1411 if (m_genericImpl)
1412 return m_genericImpl->GetItemFont(item);
1413
1414 if (m_dbImpl)
1415 {
1416 wxListItem info;
1417 if (GetItem(info))
1418 return info.GetFont();
1419 }
1420
1421 return wxNullFont;
1422 }
1423
1424 // Gets the number of selected items in the list control
1425 int wxListCtrl::GetSelectedItemCount() const
1426 {
1427 if (m_genericImpl)
1428 return m_genericImpl->GetSelectedItemCount();
1429
1430 if (m_dbImpl)
1431 return m_dbImpl->GetSelectedItemCount(NULL, true);
1432
1433 return 0;
1434 }
1435
1436 // Gets the text colour of the listview
1437 wxColour wxListCtrl::GetTextColour() const
1438 {
1439 if (m_genericImpl)
1440 return m_genericImpl->GetTextColour();
1441
1442 // TODO: we need owner drawn list items to customize text color.
1443 if (m_dbImpl)
1444 return m_textColor;
1445
1446 return wxNullColour;
1447 }
1448
1449 // Sets the text colour of the listview
1450 void wxListCtrl::SetTextColour(const wxColour& col)
1451 {
1452 if (m_genericImpl)
1453 {
1454 m_genericImpl->SetTextColour(col);
1455 return;
1456 }
1457
1458 if (m_dbImpl)
1459 m_textColor = col;
1460 }
1461
1462 // Gets the index of the topmost visible item when in
1463 // list or report view
1464 long wxListCtrl::GetTopItem() const
1465 {
1466 if (m_genericImpl)
1467 return m_genericImpl->GetTopItem();
1468
1469 if (m_dbImpl)
1470 {
1471 int flags = 0;
1472 long item = HitTest( wxPoint(1, 1), flags);
1473 if (flags == wxLIST_HITTEST_ONITEM)
1474 return item;
1475 }
1476
1477 return 0;
1478 }
1479
1480 // Searches for an item, starting from 'item'.
1481 // 'geometry' is one of
1482 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1483 // 'state' is a state bit flag, one or more of
1484 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1485 // item can be -1 to find the first item that matches the
1486 // specified flags.
1487 // Returns the item or -1 if unsuccessful.
1488 long wxListCtrl::GetNextItem(long item, int geom, int state) const
1489 {
1490 if (m_genericImpl)
1491 return m_genericImpl->GetNextItem(item, geom, state);
1492
1493 if (m_dbImpl && geom == wxLIST_NEXT_ALL && state == wxLIST_STATE_SELECTED )
1494 {
1495 long count = m_dbImpl->MacGetCount() ;
1496 for ( long line = item + 1 ; line < count; line++ )
1497 {
1498 wxMacDataItem* id = m_dbImpl->GetItemFromLine(line);
1499 if ( m_dbImpl->IsItemSelected(id ) )
1500 return line;
1501 }
1502 return -1;
1503 }
1504
1505 return 0;
1506 }
1507
1508
1509 wxImageList *wxListCtrl::GetImageList(int which) const
1510 {
1511 if (m_genericImpl)
1512 return m_genericImpl->GetImageList(which);
1513
1514 if ( which == wxIMAGE_LIST_NORMAL )
1515 {
1516 return m_imageListNormal;
1517 }
1518 else if ( which == wxIMAGE_LIST_SMALL )
1519 {
1520 return m_imageListSmall;
1521 }
1522 else if ( which == wxIMAGE_LIST_STATE )
1523 {
1524 return m_imageListState;
1525 }
1526 return NULL;
1527 }
1528
1529 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
1530 {
1531 if (m_genericImpl)
1532 {
1533 m_genericImpl->SetImageList(imageList, which);
1534 return;
1535 }
1536
1537 if ( which == wxIMAGE_LIST_NORMAL )
1538 {
1539 if (m_ownsImageListNormal) delete m_imageListNormal;
1540 m_imageListNormal = imageList;
1541 m_ownsImageListNormal = false;
1542 }
1543 else if ( which == wxIMAGE_LIST_SMALL )
1544 {
1545 if (m_ownsImageListSmall) delete m_imageListSmall;
1546 m_imageListSmall = imageList;
1547 m_ownsImageListSmall = false;
1548 }
1549 else if ( which == wxIMAGE_LIST_STATE )
1550 {
1551 if (m_ownsImageListState) delete m_imageListState;
1552 m_imageListState = imageList;
1553 m_ownsImageListState = false;
1554 }
1555 }
1556
1557 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1558 {
1559 if (m_genericImpl)
1560 {
1561 m_genericImpl->AssignImageList(imageList, which);
1562 return;
1563 }
1564
1565 SetImageList(imageList, which);
1566 if ( which == wxIMAGE_LIST_NORMAL )
1567 m_ownsImageListNormal = true;
1568 else if ( which == wxIMAGE_LIST_SMALL )
1569 m_ownsImageListSmall = true;
1570 else if ( which == wxIMAGE_LIST_STATE )
1571 m_ownsImageListState = true;
1572 }
1573
1574 // ----------------------------------------------------------------------------
1575 // Operations
1576 // ----------------------------------------------------------------------------
1577
1578 // Arranges the items
1579 bool wxListCtrl::Arrange(int flag)
1580 {
1581 if (m_genericImpl)
1582 return m_genericImpl->Arrange(flag);
1583 return false;
1584 }
1585
1586 // Deletes an item
1587 bool wxListCtrl::DeleteItem(long item)
1588 {
1589 if (m_genericImpl)
1590 return m_genericImpl->DeleteItem(item);
1591
1592 if (m_dbImpl)
1593 {
1594 m_dbImpl->MacDelete(item);
1595 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ITEM, GetId() );
1596 event.SetEventObject( this );
1597 event.m_itemIndex = item;
1598 GetEventHandler()->ProcessEvent( event );
1599
1600 }
1601 return true;
1602 }
1603
1604 // Deletes all items
1605 bool wxListCtrl::DeleteAllItems()
1606 {
1607 if (m_genericImpl)
1608 return m_genericImpl->DeleteAllItems();
1609
1610 if (m_dbImpl)
1611 {
1612 m_dbImpl->MacClear();
1613 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetId() );
1614 event.SetEventObject( this );
1615 GetEventHandler()->ProcessEvent( event );
1616 }
1617 return true;
1618 }
1619
1620 // Deletes all items
1621 bool wxListCtrl::DeleteAllColumns()
1622 {
1623 if (m_genericImpl)
1624 return m_genericImpl->DeleteAllColumns();
1625
1626 if (m_dbImpl)
1627 {
1628 UInt32 cols;
1629 m_dbImpl->GetColumnCount(&cols);
1630 for (UInt32 col = 0; col < cols; col++)
1631 {
1632 DeleteColumn(0);
1633 }
1634 }
1635
1636 return true;
1637 }
1638
1639 // Deletes a column
1640 bool wxListCtrl::DeleteColumn(int col)
1641 {
1642 if (m_genericImpl)
1643 return m_genericImpl->DeleteColumn(col);
1644
1645 if (m_dbImpl)
1646 {
1647 OSStatus err = m_dbImpl->RemoveColumn(col);
1648 return err == noErr;
1649 }
1650
1651 return true;
1652 }
1653
1654 // Clears items, and columns if there are any.
1655 void wxListCtrl::ClearAll()
1656 {
1657 if (m_genericImpl)
1658 {
1659 m_genericImpl->ClearAll();
1660 return;
1661 }
1662
1663 if (m_dbImpl)
1664 {
1665 DeleteAllItems();
1666 DeleteAllColumns();
1667 }
1668 }
1669
1670 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1671 {
1672 if (m_genericImpl)
1673 return m_genericImpl->EditLabel(item, textControlClass);
1674
1675 if (m_dbImpl)
1676 {
1677 wxCHECK_MSG( (item >= 0) && ((long)item < GetItemCount()), NULL,
1678 wxT("wrong index in wxListCtrl::EditLabel()") );
1679
1680 wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
1681 wxT("EditLabel() needs a text control") );
1682
1683 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
1684 le.SetEventObject( this );
1685 le.m_itemIndex = item;
1686 le.m_col = 0;
1687 GetItem( le.m_item );
1688
1689 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
1690 {
1691 // vetoed by user code
1692 return NULL;
1693 }
1694
1695 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
1696 m_textctrlWrapper = new wxListCtrlTextCtrlWrapper(this, text, item);
1697 return m_textctrlWrapper->GetText();
1698 }
1699 return NULL;
1700 }
1701
1702 // End label editing, optionally cancelling the edit
1703 bool wxListCtrl::EndEditLabel(bool cancel)
1704 {
1705 // TODO: generic impl. doesn't have this method - is it needed for us?
1706 if (m_genericImpl)
1707 return true; // m_genericImpl->EndEditLabel(cancel);
1708
1709 if (m_dbImpl)
1710 verify_noerr( SetDataBrowserEditItem(m_dbImpl->GetControlRef(), kDataBrowserNoItem, kMinColumnId) );
1711 return true;
1712 }
1713
1714 // Ensures this item is visible
1715 bool wxListCtrl::EnsureVisible(long item)
1716 {
1717 if (m_genericImpl)
1718 return m_genericImpl->EnsureVisible(item);
1719
1720 if (m_dbImpl)
1721 {
1722 wxMacDataItem* dataItem = m_dbImpl->GetItemFromLine(item);
1723 m_dbImpl->RevealItem(dataItem, kDataBrowserRevealWithoutSelecting);
1724 }
1725
1726 return true;
1727 }
1728
1729 // Find an item whose label matches this string, starting from the item after 'start'
1730 // or the beginning if 'start' is -1.
1731 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1732 {
1733 if (m_genericImpl)
1734 return m_genericImpl->FindItem(start, str, partial);
1735
1736 wxString str_upper = str.Upper();
1737
1738 long idx = start;
1739 if (idx < 0)
1740 idx = 0;
1741 long count = GetItemCount();
1742
1743 while (idx < count)
1744 {
1745 wxString line_upper = GetItemText(idx).Upper();
1746 if (!partial)
1747 {
1748 if (line_upper == str_upper )
1749 return idx;
1750 }
1751 else
1752 {
1753 if (line_upper.find(str_upper) == 0)
1754 return idx;
1755 }
1756
1757 idx++;
1758 };
1759
1760 return wxNOT_FOUND;
1761 }
1762
1763 // Find an item whose data matches this data, starting from the item after 'start'
1764 // or the beginning if 'start' is -1.
1765 long wxListCtrl::FindItem(long start, long data)
1766 {
1767 if (m_genericImpl)
1768 return m_genericImpl->FindItem(start, data);
1769
1770 long idx = start;
1771 if (idx < 0)
1772 idx = 0;
1773 long count = GetItemCount();
1774
1775 while (idx < count)
1776 {
1777 if (GetItemData(idx) == data)
1778 return idx;
1779 idx++;
1780 };
1781
1782 return wxNOT_FOUND;
1783 }
1784
1785 // Find an item nearest this position in the specified direction, starting from
1786 // the item after 'start' or the beginning if 'start' is -1.
1787 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1788 {
1789 if (m_genericImpl)
1790 return m_genericImpl->FindItem(start, pt, direction);
1791 return -1;
1792 }
1793
1794 // Determines which item (if any) is at the specified point,
1795 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1796 long
1797 wxListCtrl::HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const
1798 {
1799 if (m_genericImpl)
1800 return m_genericImpl->HitTest(point, flags, ptrSubItem);
1801
1802 flags = wxLIST_HITTEST_NOWHERE;
1803 if (m_dbImpl)
1804 {
1805 int colHeaderHeight = 22; // TODO: Find a way to get this value from the db control?
1806 UInt16 rowHeight = 0;
1807 m_dbImpl->GetDefaultRowHeight(&rowHeight);
1808
1809 int y = point.y;
1810 // get the actual row by taking scroll position into account
1811 UInt32 offsetX, offsetY;
1812 m_dbImpl->GetScrollPosition( &offsetY, &offsetX );
1813 y += offsetY;
1814
1815 if ( !(GetWindowStyleFlag() & wxLC_NO_HEADER) )
1816 y -= colHeaderHeight;
1817
1818 if ( y < 0 )
1819 return -1;
1820
1821 int row = y / rowHeight;
1822 DataBrowserItemID id;
1823 m_dbImpl->GetItemID( (DataBrowserTableViewRowIndex) row, &id );
1824
1825 // TODO: Use GetDataBrowserItemPartBounds to return if we are in icon or label
1826 if ( !(GetWindowStyleFlag() & wxLC_VIRTUAL ) )
1827 {
1828 wxMacListCtrlItem* lcItem;
1829 lcItem = (wxMacListCtrlItem*) id;
1830 if (lcItem)
1831 {
1832 flags = wxLIST_HITTEST_ONITEM;
1833 return row;
1834 }
1835 }
1836 else
1837 {
1838 if (row < GetItemCount() )
1839 {
1840 flags = wxLIST_HITTEST_ONITEM;
1841 return row;
1842 }
1843 }
1844
1845 }
1846 return -1;
1847 }
1848
1849 int wxListCtrl::GetScrollPos(int orient) const
1850 {
1851 if (m_genericImpl)
1852 return m_genericImpl->GetScrollPos(orient);
1853
1854 if (m_dbImpl)
1855 {
1856 UInt32 offsetX, offsetY;
1857 m_dbImpl->GetScrollPosition( &offsetY, &offsetX );
1858 if ( orient == wxHORIZONTAL )
1859 return offsetX;
1860 else
1861 return offsetY;
1862 }
1863
1864 return 0;
1865 }
1866
1867 // Inserts an item, returning the index of the new item if successful,
1868 // -1 otherwise.
1869 long wxListCtrl::InsertItem(wxListItem& info)
1870 {
1871 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1872
1873 if (m_genericImpl)
1874 return m_genericImpl->InsertItem(info);
1875
1876 if (m_dbImpl && !IsVirtual())
1877 {
1878 int count = GetItemCount();
1879
1880 if (info.m_itemId > count)
1881 info.m_itemId = count;
1882
1883 m_dbImpl->MacInsertItem(info.m_itemId, &info );
1884 wxListEvent event( wxEVT_COMMAND_LIST_INSERT_ITEM, GetId() );
1885 event.SetEventObject( this );
1886 event.m_itemIndex = info.m_itemId;
1887 GetEventHandler()->ProcessEvent( event );
1888 return info.m_itemId;
1889 }
1890 return -1;
1891 }
1892
1893 long wxListCtrl::InsertItem(long index, const wxString& label)
1894 {
1895 if (m_genericImpl)
1896 return m_genericImpl->InsertItem(index, label);
1897
1898 wxListItem info;
1899 info.m_text = label;
1900 info.m_mask = wxLIST_MASK_TEXT;
1901 info.m_itemId = index;
1902 return InsertItem(info);
1903 }
1904
1905 // Inserts an image item
1906 long wxListCtrl::InsertItem(long index, int imageIndex)
1907 {
1908 if (m_genericImpl)
1909 return m_genericImpl->InsertItem(index, imageIndex);
1910
1911 wxListItem info;
1912 info.m_image = imageIndex;
1913 info.m_mask = wxLIST_MASK_IMAGE;
1914 info.m_itemId = index;
1915 return InsertItem(info);
1916 }
1917
1918 // Inserts an image/string item
1919 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1920 {
1921 if (m_genericImpl)
1922 return m_genericImpl->InsertItem(index, label, imageIndex);
1923
1924 wxListItem info;
1925 info.m_image = imageIndex;
1926 info.m_text = label;
1927 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1928 info.m_itemId = index;
1929 return InsertItem(info);
1930 }
1931
1932 // For list view mode (only), inserts a column.
1933 long wxListCtrl::InsertColumn(long col, wxListItem& item)
1934 {
1935 if (m_genericImpl)
1936 return m_genericImpl->InsertColumn(col, item);
1937
1938 if (m_dbImpl)
1939 {
1940 int width = item.GetWidth();
1941 if ( !(item.GetMask() & wxLIST_MASK_WIDTH) )
1942 width = 150;
1943
1944 DataBrowserPropertyType type = kDataBrowserCustomType; //kDataBrowserTextType;
1945 wxImageList* imageList = GetImageList(wxIMAGE_LIST_SMALL);
1946 if (imageList && imageList->GetImageCount() > 0)
1947 {
1948 wxBitmap bmp = imageList->GetBitmap(0);
1949 //if (bmp.Ok())
1950 // type = kDataBrowserIconAndTextType;
1951 }
1952
1953 SInt16 just = teFlushDefault;
1954 if (item.GetMask() & wxLIST_MASK_FORMAT)
1955 {
1956 if (item.GetAlign() == wxLIST_FORMAT_LEFT)
1957 just = teFlushLeft;
1958 else if (item.GetAlign() == wxLIST_FORMAT_CENTER)
1959 just = teCenter;
1960 else if (item.GetAlign() == wxLIST_FORMAT_RIGHT)
1961 just = teFlushRight;
1962 }
1963 m_dbImpl->InsertColumn(col, type, item.GetText(), just, width);
1964 SetColumn(col, item);
1965
1966 // set/remove options based on the wxListCtrl type.
1967 DataBrowserTableViewColumnID id;
1968 m_dbImpl->GetColumnIDFromIndex(col, &id);
1969 DataBrowserPropertyFlags flags;
1970 verify_noerr(m_dbImpl->GetPropertyFlags(id, &flags));
1971 if (GetWindowStyleFlag() & wxLC_EDIT_LABELS)
1972 flags |= kDataBrowserPropertyIsEditable;
1973
1974 if (GetWindowStyleFlag() & wxLC_VIRTUAL){
1975 flags &= ~kDataBrowserListViewSortableColumn;
1976 }
1977 verify_noerr(m_dbImpl->SetPropertyFlags(id, flags));
1978 }
1979
1980 return col;
1981 }
1982
1983 long wxListCtrl::InsertColumn(long col,
1984 const wxString& heading,
1985 int format,
1986 int width)
1987 {
1988 if (m_genericImpl)
1989 return m_genericImpl->InsertColumn(col, heading, format, width);
1990
1991 wxListItem item;
1992 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1993 item.m_text = heading;
1994 if ( width > -1 )
1995 {
1996 item.m_mask |= wxLIST_MASK_WIDTH;
1997 item.m_width = width;
1998 }
1999 item.m_format = format;
2000
2001 return InsertColumn(col, item);
2002 }
2003
2004 // scroll the control by the given number of pixels (exception: in list view,
2005 // dx is interpreted as number of columns)
2006 bool wxListCtrl::ScrollList(int dx, int dy)
2007 {
2008 if (m_genericImpl)
2009 return m_genericImpl->ScrollList(dx, dy);
2010
2011 if (m_dbImpl)
2012 {
2013 m_dbImpl->SetScrollPosition(dx, dy);
2014 }
2015 return true;
2016 }
2017
2018
2019 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
2020 {
2021 if (m_genericImpl)
2022 return m_genericImpl->SortItems(fn, data);
2023
2024 if (m_dbImpl)
2025 {
2026 m_compareFunc = fn;
2027 m_compareFuncData = data;
2028 }
2029
2030 return true;
2031 }
2032
2033 void wxListCtrl::OnRenameTimer()
2034 {
2035 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2036
2037 EditLabel( m_current );
2038 }
2039
2040 bool wxListCtrl::OnRenameAccept(long itemEdit, const wxString& value)
2041 {
2042 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetId() );
2043 le.SetEventObject( this );
2044 le.m_itemIndex = itemEdit;
2045
2046 GetItem( le.m_item );
2047 le.m_item.m_text = value;
2048 return !GetEventHandler()->ProcessEvent( le ) ||
2049 le.IsAllowed();
2050 }
2051
2052 void wxListCtrl::OnRenameCancelled(long itemEdit)
2053 {
2054 // let owner know that the edit was cancelled
2055 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2056
2057 le.SetEditCanceled(true);
2058
2059 le.SetEventObject( this );
2060 le.m_itemIndex = itemEdit;
2061
2062 GetItem( le.m_item );
2063 GetEventHandler()->ProcessEvent( le );
2064 }
2065
2066 // ----------------------------------------------------------------------------
2067 // virtual list controls
2068 // ----------------------------------------------------------------------------
2069
2070 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2071 {
2072 // this is a pure virtual function, in fact - which is not really pure
2073 // because the controls which are not virtual don't need to implement it
2074 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2075
2076 return wxEmptyString;
2077 }
2078
2079 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2080 {
2081 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
2082 -1,
2083 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2084 return -1;
2085 }
2086
2087 int wxListCtrl::OnGetItemColumnImage(long item, long column) const
2088 {
2089 if (!column)
2090 return OnGetItemImage(item);
2091
2092 return -1;
2093 }
2094
2095 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2096 {
2097 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2098 _T("invalid item index in OnGetItemAttr()") );
2099
2100 // no attributes by default
2101 return NULL;
2102 }
2103
2104 void wxListCtrl::SetItemCount(long count)
2105 {
2106 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2107
2108 if (m_genericImpl)
2109 {
2110 m_genericImpl->SetItemCount(count);
2111 return;
2112 }
2113
2114 if (m_dbImpl)
2115 {
2116 // we need to temporarily disable the new item creation notification
2117 // procedure to speed things up
2118 // FIXME: Even this doesn't seem to help much...
2119
2120 // FIXME: Find a more efficient way to do this.
2121 m_dbImpl->MacClear();
2122
2123 DataBrowserCallbacks callbacks;
2124 DataBrowserItemNotificationUPP itemUPP;
2125 GetDataBrowserCallbacks(m_dbImpl->GetControlRef(), &callbacks);
2126 itemUPP = callbacks.u.v1.itemNotificationCallback;
2127 callbacks.u.v1.itemNotificationCallback = 0;
2128 m_dbImpl->SetCallbacks(&callbacks);
2129 ::AddDataBrowserItems(m_dbImpl->GetControlRef(), kDataBrowserNoItem,
2130 count, NULL, kDataBrowserItemNoProperty);
2131 callbacks.u.v1.itemNotificationCallback = itemUPP;
2132 m_dbImpl->SetCallbacks(&callbacks);
2133 }
2134 m_count = count;
2135 }
2136
2137 void wxListCtrl::RefreshItem(long item)
2138 {
2139 if (m_genericImpl)
2140 {
2141 m_genericImpl->RefreshItem(item);
2142 return;
2143 }
2144
2145 wxRect rect;
2146 GetItemRect(item, rect);
2147 RefreshRect(rect);
2148 }
2149
2150 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2151 {
2152 if (m_genericImpl)
2153 {
2154 m_genericImpl->RefreshItems(itemFrom, itemTo);
2155 return;
2156 }
2157
2158 wxRect rect1, rect2;
2159 GetItemRect(itemFrom, rect1);
2160 GetItemRect(itemTo, rect2);
2161
2162 wxRect rect = rect1;
2163 rect.height = rect2.GetBottom() - rect1.GetTop();
2164
2165 RefreshRect(rect);
2166 }
2167
2168 void wxListCtrl::SetDropTarget( wxDropTarget *dropTarget )
2169 {
2170 #if wxUSE_DRAG_AND_DROP
2171 if (m_genericImpl)
2172 m_genericImpl->SetDropTarget( dropTarget );
2173
2174 if (m_dbImpl)
2175 wxWindow::SetDropTarget( dropTarget );
2176 #endif
2177 }
2178
2179 wxDropTarget *wxListCtrl::GetDropTarget() const
2180 {
2181 #if wxUSE_DRAG_AND_DROP
2182 if (m_genericImpl)
2183 return m_genericImpl->GetDropTarget();
2184
2185 if (m_dbImpl)
2186 return wxWindow::GetDropTarget();
2187 #endif
2188 return NULL;
2189 }
2190
2191 // wxMac internal data structures
2192
2193 wxMacListCtrlItem::~wxMacListCtrlItem()
2194 {
2195 }
2196
2197 void wxMacListCtrlItem::Notification(wxMacDataItemBrowserControl *owner ,
2198 DataBrowserItemNotification message,
2199 DataBrowserItemDataRef itemData ) const
2200 {
2201
2202 wxMacDataBrowserListCtrlControl *lb = dynamic_cast<wxMacDataBrowserListCtrlControl*>(owner);
2203
2204 // we want to depend on as little as possible to make sure tear-down of controls is safe
2205 if ( message == kDataBrowserItemRemoved)
2206 {
2207 if ( lb != NULL && lb->GetClientDataType() == wxClientData_Object )
2208 {
2209 delete (wxClientData*) (m_data);
2210 }
2211
2212 delete this;
2213 return;
2214 }
2215 else if ( message == kDataBrowserItemAdded )
2216 {
2217 // we don't issue events on adding, the item is not really stored in the list yet, so we
2218 // avoid asserts by gettting out now
2219 return ;
2220 }
2221
2222 wxListCtrl *list = wxDynamicCast( owner->GetPeer() , wxListCtrl );
2223 if ( list && lb )
2224 {
2225 bool trigger = false;
2226
2227 wxListEvent event( wxEVT_COMMAND_LIST_ITEM_SELECTED, list->GetId() );
2228 bool isSingle = (list->GetWindowStyle() & wxLC_SINGLE_SEL) != 0;
2229
2230 event.SetEventObject( list );
2231 event.m_itemIndex = owner->GetLineFromItem( this ) ;
2232 if ( !list->IsVirtual() )
2233 {
2234 lb->MacGetColumnInfo(event.m_itemIndex,0,event.m_item);
2235 }
2236
2237 switch (message)
2238 {
2239 case kDataBrowserItemDeselected:
2240 event.SetEventType(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
2241 if ( !isSingle )
2242 trigger = !lb->IsSelectionSuppressed();
2243 break;
2244
2245 case kDataBrowserItemSelected:
2246 trigger = !lb->IsSelectionSuppressed();
2247 break;
2248
2249 case kDataBrowserItemDoubleClicked:
2250 event.SetEventType( wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2251 trigger = true;
2252 break;
2253
2254 case kDataBrowserEditStarted :
2255 // TODO : how to veto ?
2256 event.SetEventType( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT ) ;
2257 trigger = true ;
2258 break ;
2259
2260 case kDataBrowserEditStopped :
2261 // TODO probably trigger only upon the value store callback, because
2262 // here IIRC we cannot veto
2263 event.SetEventType( wxEVT_COMMAND_LIST_END_LABEL_EDIT ) ;
2264 trigger = true ;
2265 break ;
2266
2267 default:
2268 break;
2269 }
2270
2271 if ( trigger )
2272 {
2273 // direct notification is not always having the listbox GetSelection() having in synch with event
2274 wxPostEvent( list->GetEventHandler(), event );
2275 }
2276 }
2277
2278 }
2279
2280 wxMacDataBrowserListCtrlControl::wxMacDataBrowserListCtrlControl( wxWindow *peer, const wxPoint& pos, const wxSize& size, long style)
2281 : wxMacDataItemBrowserControl( peer, pos, size, style )
2282 {
2283 OSStatus err = noErr;
2284 m_clientDataItemsType = wxClientData_None;
2285 m_isVirtual = false;
2286
2287 if ( style & wxLC_VIRTUAL )
2288 m_isVirtual = true;
2289
2290 DataBrowserSelectionFlags options = kDataBrowserDragSelect;
2291 if ( style & wxLC_SINGLE_SEL )
2292 {
2293 options |= kDataBrowserSelectOnlyOne;
2294 }
2295 else
2296 {
2297 options |= kDataBrowserCmdTogglesSelection;
2298 }
2299
2300 err = SetSelectionFlags( options );
2301 verify_noerr( err );
2302
2303 DataBrowserCustomCallbacks callbacks;
2304 InitializeDataBrowserCustomCallbacks( &callbacks, kDataBrowserLatestCustomCallbacks );
2305
2306 if ( gDataBrowserDrawItemUPP == NULL )
2307 gDataBrowserDrawItemUPP = NewDataBrowserDrawItemUPP(DataBrowserDrawItemProc);
2308
2309 if ( gDataBrowserHitTestUPP == NULL )
2310 gDataBrowserHitTestUPP = NewDataBrowserHitTestUPP(DataBrowserHitTestProc);
2311
2312 callbacks.u.v1.drawItemCallback = gDataBrowserDrawItemUPP;
2313 callbacks.u.v1.hitTestCallback = gDataBrowserHitTestUPP;
2314
2315 SetDataBrowserCustomCallbacks( GetControlRef(), &callbacks );
2316
2317 if ( style & wxLC_LIST )
2318 {
2319 InsertColumn(0, kDataBrowserIconAndTextType, wxEmptyString, -1, -1);
2320 verify_noerr( AutoSizeColumns() );
2321 }
2322
2323 if ( style & wxLC_LIST || style & wxLC_NO_HEADER )
2324 verify_noerr( SetHeaderButtonHeight( 0 ) );
2325
2326 if ( m_isVirtual )
2327 SetSortProperty( kMinColumnId - 1 );
2328 else
2329 SetSortProperty( kMinColumnId );
2330 if ( style & wxLC_SORT_ASCENDING )
2331 {
2332 m_sortOrder = SortOrder_Text_Ascending;
2333 SetSortOrder( kDataBrowserOrderIncreasing );
2334 }
2335 else if ( style & wxLC_SORT_DESCENDING )
2336 {
2337 m_sortOrder = SortOrder_Text_Descending;
2338 SetSortOrder( kDataBrowserOrderDecreasing );
2339 }
2340 else
2341 {
2342 m_sortOrder = SortOrder_None;
2343 }
2344
2345 if ( style & wxLC_VRULES )
2346 {
2347 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
2348 verify_noerr( DataBrowserChangeAttributes(m_controlRef, kDataBrowserAttributeListViewDrawColumnDividers, kDataBrowserAttributeNone) );
2349 #endif
2350 }
2351
2352 verify_noerr( SetHiliteStyle(kDataBrowserTableViewFillHilite ) );
2353 err = SetHasScrollBars( (style & wxHSCROLL) != 0 , true );
2354 }
2355
2356 pascal Boolean wxMacDataBrowserListCtrlControl::DataBrowserEditTextProc(
2357 ControlRef browser,
2358 DataBrowserItemID itemID,
2359 DataBrowserPropertyID property,
2360 CFStringRef theString,
2361 Rect *maxEditTextRect,
2362 Boolean *shrinkToFit)
2363 {
2364 Boolean result = false;
2365 wxMacDataBrowserListCtrlControl* ctl = dynamic_cast<wxMacDataBrowserListCtrlControl*>( wxMacControl::GetReferenceFromNativeControl( browser ) );
2366 if ( ctl != 0 )
2367 {
2368 result = ctl->ConfirmEditText(itemID, property, theString, maxEditTextRect, shrinkToFit);
2369 theString = CFSTR("Hello!");
2370 }
2371 return result;
2372 }
2373
2374 bool wxMacDataBrowserListCtrlControl::ConfirmEditText(
2375 DataBrowserItemID itemID,
2376 DataBrowserPropertyID property,
2377 CFStringRef theString,
2378 Rect *maxEditTextRect,
2379 Boolean *shrinkToFit)
2380 {
2381 return false;
2382 }
2383
2384 pascal void wxMacDataBrowserListCtrlControl::DataBrowserDrawItemProc(
2385 ControlRef browser,
2386 DataBrowserItemID itemID,
2387 DataBrowserPropertyID property,
2388 DataBrowserItemState itemState,
2389 const Rect *itemRect,
2390 SInt16 gdDepth,
2391 Boolean colorDevice)
2392 {
2393 wxMacDataBrowserListCtrlControl* ctl = dynamic_cast<wxMacDataBrowserListCtrlControl*>( wxMacControl::GetReferenceFromNativeControl( browser ) );
2394 if ( ctl != 0 )
2395 {
2396 ctl->DrawItem(itemID, property, itemState, itemRect, gdDepth, colorDevice);
2397 }
2398 }
2399
2400 // routines needed for DrawItem
2401 enum
2402 {
2403 kIconWidth = 16,
2404 kIconHeight = 16,
2405 kTextBoxHeight = 14,
2406 kIconTextSpacingV = 2,
2407 kItemPadding = 4,
2408 kContentHeight = kIconHeight + kTextBoxHeight + kIconTextSpacingV
2409 };
2410
2411 static void calculateCGDrawingBounds(CGRect inItemRect, CGRect *outIconRect, CGRect *outTextRect, bool hasIcon = false)
2412 {
2413 float textBottom;
2414 float iconH, iconW = 0;
2415 float padding = kItemPadding;
2416 if (hasIcon)
2417 {
2418 iconH = kIconHeight;
2419 iconW = kIconWidth;
2420 padding = padding*2;
2421 }
2422
2423 textBottom = inItemRect.origin.y;
2424
2425 *outIconRect = CGRectMake(inItemRect.origin.x + kItemPadding,
2426 textBottom + kIconTextSpacingV, kIconWidth,
2427 kIconHeight);
2428
2429 *outTextRect = CGRectMake(inItemRect.origin.x + padding + iconW,
2430 textBottom + kIconTextSpacingV, inItemRect.size.width - padding - iconW,
2431 inItemRect.size.height - kIconTextSpacingV);
2432 }
2433
2434 void wxMacDataBrowserListCtrlControl::DrawItem(
2435 DataBrowserItemID itemID,
2436 DataBrowserPropertyID property,
2437 DataBrowserItemState itemState,
2438 const Rect *itemRect,
2439 SInt16 gdDepth,
2440 Boolean colorDevice)
2441 {
2442 wxString text;
2443 wxFont font = wxNullFont;
2444 int imgIndex = -1;
2445 short listColumn = property - kMinColumnId;
2446
2447 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
2448 wxMacListCtrlItem* lcItem;
2449 wxColour color = *wxBLACK;
2450 wxColour bgColor = wxNullColour;
2451
2452 if (listColumn >= 0)
2453 {
2454 if (!m_isVirtual)
2455 {
2456 lcItem = (wxMacListCtrlItem*) itemID;
2457 if (lcItem->HasColumnInfo(listColumn)){
2458 wxListItem* item = lcItem->GetColumnInfo(listColumn);
2459
2460 // we always use the 0 column to get font and text/background colors.
2461 if (lcItem->HasColumnInfo(0))
2462 {
2463 wxListItem* firstItem = lcItem->GetColumnInfo(0);
2464 color = firstItem->GetTextColour();
2465 bgColor = firstItem->GetBackgroundColour();
2466 font = firstItem->GetFont();
2467 }
2468
2469 if (item->GetMask() & wxLIST_MASK_TEXT)
2470 text = item->GetText();
2471 if (item->GetMask() & wxLIST_MASK_IMAGE)
2472 imgIndex = item->GetImage();
2473 }
2474
2475 }
2476 else
2477 {
2478 text = list->OnGetItemText( (long)itemID-1, listColumn );
2479 imgIndex = list->OnGetItemColumnImage( (long)itemID-1, listColumn );
2480 wxListItemAttr* attrs = list->OnGetItemAttr( (long)itemID-1 );
2481 if (attrs)
2482 {
2483 if (attrs->HasBackgroundColour())
2484 bgColor = attrs->GetBackgroundColour();
2485 if (attrs->HasTextColour())
2486 color = attrs->GetTextColour();
2487 if (attrs->HasFont())
2488 font = attrs->GetFont();
2489 }
2490 }
2491 }
2492
2493 wxColour listBgColor = list->GetBackgroundColour();
2494 if (bgColor == wxNullColour)
2495 bgColor = listBgColor;
2496
2497 wxFont listFont = list->GetFont();
2498 if (font == wxNullFont)
2499 font = listFont;
2500
2501 wxMacCFStringHolder cfString;
2502 cfString.Assign( text, wxLocale::GetSystemEncoding() );
2503
2504 Rect enclosingRect;
2505 CGRect enclosingCGRect, iconCGRect, textCGRect;
2506 Boolean active;
2507 ThemeDrawingState savedState = NULL;
2508 CGContextRef context = (CGContextRef)list->MacGetDrawingContext();
2509 RGBColor labelColor;
2510 labelColor.red = 0;
2511 labelColor.green = 0;
2512 labelColor.blue = 0;
2513
2514 RGBColor backgroundColor;
2515 backgroundColor.red = 255;
2516 backgroundColor.green = 255;
2517 backgroundColor.blue = 255;
2518
2519 GetDataBrowserItemPartBounds(GetControlRef(), itemID, property, kDataBrowserPropertyEnclosingPart,
2520 &enclosingRect);
2521
2522 enclosingCGRect = CGRectMake(enclosingRect.left,
2523 enclosingRect.top,
2524 enclosingRect.right - enclosingRect.left,
2525 enclosingRect.bottom - enclosingRect.top);
2526
2527 active = IsControlActive(GetControlRef());
2528
2529 // don't paint the background over the vertical rule line
2530 if ( list->GetWindowStyleFlag() & wxLC_VRULES )
2531 {
2532 enclosingCGRect.origin.x += 1;
2533 enclosingCGRect.size.width -= 1;
2534 }
2535 if (itemState == kDataBrowserItemIsSelected)
2536 {
2537
2538 GetThemeDrawingState(&savedState);
2539
2540 GetThemeBrushAsColor(kThemeBrushAlternatePrimaryHighlightColor, 32, true, &backgroundColor);
2541 GetThemeTextColor(kThemeTextColorWhite, gdDepth, colorDevice, &labelColor);
2542
2543 CGContextSaveGState(context);
2544
2545 CGContextSetRGBFillColor(context, (float)backgroundColor.red / (float)USHRT_MAX,
2546 (float)backgroundColor.green / (float)USHRT_MAX,
2547 (float)backgroundColor.blue / (float)USHRT_MAX, 1.0);
2548 CGContextFillRect(context, enclosingCGRect);
2549
2550 CGContextRestoreGState(context);
2551 }
2552 else
2553 {
2554
2555 if (color.Ok())
2556 labelColor = MAC_WXCOLORREF( color.GetPixel() );
2557 else if (list->GetTextColour().Ok())
2558 labelColor = MAC_WXCOLORREF( list->GetTextColour().GetPixel() );
2559
2560 if (bgColor.Ok())
2561 {
2562 backgroundColor = MAC_WXCOLORREF( bgColor.GetPixel() );
2563 CGContextSaveGState(context);
2564
2565 CGContextSetRGBFillColor(context, (float)backgroundColor.red / (float)USHRT_MAX,
2566 (float)backgroundColor.green / (float)USHRT_MAX,
2567 (float)backgroundColor.blue / (float)USHRT_MAX, 1.0);
2568 CGContextFillRect(context, enclosingCGRect);
2569
2570 CGContextRestoreGState(context);
2571 }
2572 }
2573
2574 calculateCGDrawingBounds(enclosingCGRect, &iconCGRect, &textCGRect, (imgIndex != -1) );
2575
2576 if (imgIndex != -1)
2577 {
2578 wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL);
2579 if (imageList && imageList->GetImageCount() > 0){
2580 wxBitmap bmp = imageList->GetBitmap(imgIndex);
2581 IconRef icon = bmp.GetBitmapData()->GetIconRef();
2582
2583 CGContextSaveGState(context);
2584 CGContextTranslateCTM(context, 0,iconCGRect.origin.y + CGRectGetMaxY(iconCGRect));
2585 CGContextScaleCTM(context,1.0f,-1.0f);
2586 PlotIconRefInContext(context, &iconCGRect, kAlignNone,
2587 active ? kTransformNone : kTransformDisabled, NULL,
2588 kPlotIconRefNormalFlags, icon);
2589
2590 CGContextRestoreGState(context);
2591 }
2592 }
2593
2594 HIThemeTextHorizontalFlush hFlush = kHIThemeTextHorizontalFlushLeft;
2595 UInt16 fontID = kThemeViewsFont;
2596
2597 if (font.Ok())
2598 {
2599 if (font.GetFamily() != wxFONTFAMILY_DEFAULT)
2600 fontID = font.MacGetThemeFontID();
2601
2602 // FIXME: replace these with CG or ATSUI calls so we can remove this #ifndef.
2603 #ifndef __LP64__
2604 ::TextSize( (short)(font.MacGetFontSize()) ) ;
2605 ::TextFace( font.MacGetFontStyle() ) ;
2606 #endif
2607 }
2608
2609 wxListItem item;
2610 list->GetColumn(listColumn, item);
2611 if (item.GetMask() & wxLIST_MASK_FORMAT)
2612 {
2613 if (item.GetAlign() == wxLIST_FORMAT_LEFT)
2614 hFlush = kHIThemeTextHorizontalFlushLeft;
2615 else if (item.GetAlign() == wxLIST_FORMAT_CENTER)
2616 hFlush = kHIThemeTextHorizontalFlushCenter;
2617 else if (item.GetAlign() == wxLIST_FORMAT_RIGHT)
2618 {
2619 hFlush = kHIThemeTextHorizontalFlushRight;
2620 textCGRect.origin.x -= kItemPadding; // give a little extra paddding
2621 }
2622 }
2623
2624 HIThemeTextInfo info;
2625 info.version = kHIThemeTextInfoVersionZero;
2626 info.state = active ? kThemeStateActive : kThemeStateInactive;
2627 info.fontID = fontID;
2628 info.horizontalFlushness = hFlush;
2629 info.verticalFlushness = kHIThemeTextVerticalFlushCenter;
2630 info.options = kHIThemeTextBoxOptionNone;
2631 info.truncationPosition = kHIThemeTextTruncationEnd;
2632 info.truncationMaxLines = 1;
2633
2634 CGContextSaveGState(context);
2635 CGContextSetRGBFillColor (context, (float)labelColor.red / (float)USHRT_MAX,
2636 (float)labelColor.green / (float)USHRT_MAX,
2637 (float)labelColor.blue / (float)USHRT_MAX, 1.0);
2638
2639 HIThemeDrawTextBox(cfString, &textCGRect, &info, context, kHIThemeOrientationNormal);
2640
2641 CGContextRestoreGState(context);
2642
2643 if (savedState != NULL)
2644 SetThemeDrawingState(savedState, true);
2645 }
2646
2647 OSStatus wxMacDataBrowserListCtrlControl::GetSetItemData(DataBrowserItemID itemID,
2648 DataBrowserPropertyID property,
2649 DataBrowserItemDataRef itemData,
2650 Boolean changeValue )
2651 {
2652 wxString text;
2653 int imgIndex = -1;
2654 short listColumn = property - kMinColumnId;
2655
2656 OSStatus err = errDataBrowserPropertyNotSupported;
2657 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
2658 wxMacListCtrlItem* lcItem;
2659
2660 if (listColumn >= 0)
2661 {
2662 if (!m_isVirtual)
2663 {
2664 lcItem = (wxMacListCtrlItem*) itemID;
2665 if (lcItem && lcItem->HasColumnInfo(listColumn)){
2666 wxListItem* item = lcItem->GetColumnInfo(listColumn);
2667 if (item->GetMask() & wxLIST_MASK_TEXT)
2668 text = item->GetText();
2669 if (item->GetMask() & wxLIST_MASK_IMAGE)
2670 imgIndex = item->GetImage();
2671 }
2672 }
2673 else
2674 {
2675 text = list->OnGetItemText( (long)itemID-1, listColumn );
2676 imgIndex = list->OnGetItemColumnImage( (long)itemID-1, listColumn );
2677 }
2678 }
2679
2680 if ( !changeValue )
2681 {
2682 switch (property)
2683 {
2684 case kDataBrowserItemIsEditableProperty :
2685 if ( list && list->HasFlag( wxLC_EDIT_LABELS ) )
2686 {
2687 verify_noerr(SetDataBrowserItemDataBooleanValue( itemData, true ));
2688 err = noErr ;
2689 }
2690 break ;
2691 default :
2692 if ( property >= kMinColumnId )
2693 {
2694 wxMacCFStringHolder cfStr;
2695
2696 if (text){
2697 cfStr.Assign( text, wxLocale::GetSystemEncoding() );
2698 err = ::SetDataBrowserItemDataText( itemData, cfStr );
2699 err = noErr;
2700 }
2701
2702
2703
2704 if ( imgIndex != -1 )
2705 {
2706 wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL);
2707 if (imageList && imageList->GetImageCount() > 0){
2708 wxBitmap bmp = imageList->GetBitmap(imgIndex);
2709 IconRef icon = bmp.GetBitmapData()->GetIconRef();
2710 ::SetDataBrowserItemDataIcon(itemData, icon);
2711 }
2712 }
2713
2714 }
2715 break ;
2716 }
2717
2718 }
2719 else
2720 {
2721 switch (property)
2722 {
2723 default:
2724 if ( property >= kMinColumnId )
2725 {
2726 short listColumn = property - kMinColumnId;
2727
2728 // TODO probably send the 'end edit' from here, as we
2729 // can then deal with the veto
2730 CFStringRef sr ;
2731 verify_noerr( GetDataBrowserItemDataText( itemData , &sr ) ) ;
2732 wxMacCFStringHolder cfStr(sr) ;;
2733 if (m_isVirtual)
2734 list->SetItem( (long)itemData-1 , listColumn, cfStr.AsString() ) ;
2735 else
2736 {
2737 if (lcItem)
2738 lcItem->SetColumnTextValue( listColumn, cfStr.AsString() );
2739 }
2740 err = noErr ;
2741 }
2742 break;
2743 }
2744 }
2745 return err;
2746 }
2747
2748 void wxMacDataBrowserListCtrlControl::ItemNotification(DataBrowserItemID itemID,
2749 DataBrowserItemNotification message,
2750 DataBrowserItemDataRef itemData )
2751 {
2752 // we want to depend on as little as possible to make sure tear-down of controls is safe
2753 if ( message == kDataBrowserItemRemoved)
2754 {
2755 // make sure MacDelete does the proper teardown.
2756 return;
2757 }
2758 else if ( message == kDataBrowserItemAdded )
2759 {
2760 // we don't issue events on adding, the item is not really stored in the list yet, so we
2761 // avoid asserts by getting out now
2762 return ;
2763 }
2764
2765 wxListCtrl *list = wxDynamicCast( GetPeer() , wxListCtrl );
2766 if ( list )
2767 {
2768 bool trigger = false;
2769
2770 wxListEvent event( wxEVT_COMMAND_LIST_ITEM_SELECTED, list->GetId() );
2771 bool isSingle = (list->GetWindowStyle() & wxLC_SINGLE_SEL) != 0;
2772
2773 event.SetEventObject( list );
2774 if ( !list->IsVirtual() )
2775 {
2776 DataBrowserTableViewRowIndex result = 0;
2777 verify_noerr( GetItemRow( itemID, &result ) ) ;
2778 event.m_itemIndex = result;
2779
2780 if (event.m_itemIndex >= 0)
2781 MacGetColumnInfo(event.m_itemIndex,0,event.m_item);
2782 }
2783 else
2784 {
2785 event.m_itemIndex = (long)itemID-1;
2786 }
2787
2788 switch (message)
2789 {
2790 case kDataBrowserItemDeselected:
2791 event.SetEventType(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
2792 if ( !isSingle )
2793 trigger = !IsSelectionSuppressed();
2794 break;
2795
2796 case kDataBrowserItemSelected:
2797 trigger = !IsSelectionSuppressed();
2798
2799 break;
2800
2801 case kDataBrowserItemDoubleClicked:
2802 event.SetEventType( wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2803 trigger = true;
2804 break;
2805
2806 case kDataBrowserEditStarted :
2807 // TODO : how to veto ?
2808 event.SetEventType( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT ) ;
2809 trigger = true ;
2810 break ;
2811
2812 case kDataBrowserEditStopped :
2813 // TODO probably trigger only upon the value store callback, because
2814 // here IIRC we cannot veto
2815 event.SetEventType( wxEVT_COMMAND_LIST_END_LABEL_EDIT ) ;
2816 trigger = true ;
2817 break ;
2818
2819 default:
2820 break;
2821 }
2822
2823 if ( trigger )
2824 {
2825 // direct notification is not always having the listbox GetSelection() having in synch with event
2826 wxPostEvent( list->GetEventHandler(), event );
2827 }
2828 }
2829 }
2830
2831 Boolean wxMacDataBrowserListCtrlControl::CompareItems(DataBrowserItemID itemOneID,
2832 DataBrowserItemID itemTwoID,
2833 DataBrowserPropertyID sortProperty)
2834 {
2835
2836 bool retval = false;
2837 wxString itemText;
2838 wxString otherItemText;
2839 int colId = sortProperty - kMinColumnId;
2840
2841 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
2842
2843 if (colId >= 0)
2844 {
2845 if (!m_isVirtual)
2846 {
2847 wxMacListCtrlItem* item = (wxMacListCtrlItem*)itemOneID;
2848 wxMacListCtrlItem* otherItem = (wxMacListCtrlItem*)itemTwoID;
2849
2850 // FIXME: This code causes a crash in wxPython for some reason
2851 // and moreover, further testing shows that the column click event
2852 // is only sent to the list ctrl after the native control has finished
2853 // sorting items anyway. So just disable this for now.
2854
2855 //wxListCtrlCompare func = list->GetCompareFunc();
2856 //long item1 = GetLineFromItem(item);
2857 //long item2 = GetLineFromItem(otherItem);
2858
2859 //if (func != NULL && item->HasColumnInfo(colId) && otherItem->HasColumnInfo(colId))
2860 // return func(item1, item2, list->GetCompareFuncData()) >= 0;
2861
2862 if (item->HasColumnInfo(colId))
2863 itemText = item->GetColumnInfo(colId)->GetText();
2864 if (otherItem->HasColumnInfo(colId))
2865 otherItemText = otherItem->GetColumnInfo(colId)->GetText();
2866 }
2867 else
2868 {
2869
2870 long itemNum = (long)itemOneID;
2871 long otherItemNum = (long)itemTwoID;
2872 itemText = list->OnGetItemText( itemNum-1, colId );
2873 otherItemText = list->OnGetItemText( otherItemNum-1, colId );
2874
2875 }
2876
2877 DataBrowserSortOrder sort;
2878 verify_noerr(GetSortOrder(&sort));
2879
2880 if ( sort == kDataBrowserOrderIncreasing )
2881 {
2882 retval = itemText.CmpNoCase( otherItemText ) > 0;
2883 }
2884 else if ( sort == kDataBrowserOrderDecreasing )
2885 {
2886 retval = itemText.CmpNoCase( otherItemText ) < 0;
2887 }
2888 }
2889 else{
2890 // fallback for undefined cases
2891 retval = itemOneID < itemTwoID;
2892 }
2893
2894 return retval;
2895 }
2896
2897 wxMacDataBrowserListCtrlControl::~wxMacDataBrowserListCtrlControl()
2898 {
2899 }
2900
2901 void wxMacDataBrowserListCtrlControl::MacSetColumnInfo( unsigned int row, unsigned int column, wxListItem* item )
2902 {
2903 wxMacDataItem* dataItem = GetItemFromLine(row);
2904 wxASSERT_MSG( dataItem, _T("could not obtain wxMacDataItem for row in MacSetColumnInfo. Is row a valid wxListCtrl row?") );
2905 if (item)
2906 {
2907 wxMacListCtrlItem* listItem = dynamic_cast<wxMacListCtrlItem*>(dataItem);
2908 listItem->SetColumnInfo( column, item );
2909 UpdateState(dataItem, item);
2910 }
2911 }
2912
2913 // apply changes that need to happen immediately, rather than when the
2914 // databrowser control fires a callback.
2915 void wxMacDataBrowserListCtrlControl::UpdateState(wxMacDataItem* dataItem, wxListItem* listItem)
2916 {
2917 bool isSelected = IsItemSelected( dataItem );
2918 bool isSelectedState = (listItem->GetState() == wxLIST_STATE_SELECTED);
2919
2920 // toggle the selection state if wxListInfo state and actual state don't match.
2921 if ( isSelected != isSelectedState )
2922 {
2923 DataBrowserSetOption options = kDataBrowserItemsAdd;
2924 if (!isSelectedState)
2925 options = kDataBrowserItemsRemove;
2926 SetSelectedItem(dataItem, options);
2927 }
2928 // TODO: Set column width if item width > than current column width
2929 }
2930
2931 void wxMacDataBrowserListCtrlControl::MacGetColumnInfo( unsigned int row, unsigned int column, wxListItem& item )
2932 {
2933 wxMacDataItem* dataItem = GetItemFromLine(row);
2934 wxASSERT_MSG( dataItem, _T("could not obtain wxMacDataItem in MacGetColumnInfo. Is row a valid wxListCtrl row?") );
2935 // CS should this guard against dataItem = 0 ? , as item is not a pointer if (item) is not appropriate
2936 //if (item)
2937 {
2938 wxMacListCtrlItem* listItem = dynamic_cast<wxMacListCtrlItem*>(dataItem);
2939
2940 if (!listItem->HasColumnInfo( column ))
2941 return;
2942
2943 wxListItem* oldItem = listItem->GetColumnInfo( column );
2944
2945 if (oldItem)
2946 {
2947 long mask = item.GetMask();
2948 if ( !mask )
2949 // by default, get everything for backwards compatibility
2950 mask = -1;
2951
2952 if ( mask & wxLIST_MASK_TEXT )
2953 item.SetText(oldItem->GetText());
2954 if ( mask & wxLIST_MASK_IMAGE )
2955 item.SetImage(oldItem->GetImage());
2956 if ( mask & wxLIST_MASK_DATA )
2957 item.SetData(oldItem->GetData());
2958 if ( mask & wxLIST_MASK_STATE )
2959 item.SetState(oldItem->GetState());
2960 if ( mask & wxLIST_MASK_WIDTH )
2961 item.SetWidth(oldItem->GetWidth());
2962 if ( mask & wxLIST_MASK_FORMAT )
2963 item.SetAlign(oldItem->GetAlign());
2964
2965 item.SetTextColour(oldItem->GetTextColour());
2966 item.SetBackgroundColour(oldItem->GetBackgroundColour());
2967 item.SetFont(oldItem->GetFont());
2968 }
2969 }
2970 }
2971
2972 void wxMacDataBrowserListCtrlControl::MacInsertItem( unsigned int n, wxListItem* item )
2973 {
2974 wxMacDataItemBrowserControl::MacInsert(n, item->GetText());
2975 MacSetColumnInfo(n, 0, item);
2976 }
2977
2978 wxMacDataItem* wxMacDataBrowserListCtrlControl::CreateItem()
2979 {
2980 return new wxMacListCtrlItem();
2981 }
2982
2983 wxMacListCtrlItem::wxMacListCtrlItem()
2984 {
2985 m_rowItems = wxListItemList( wxKEY_INTEGER );
2986 }
2987
2988 int wxMacListCtrlItem::GetColumnImageValue( unsigned int column )
2989 {
2990 if ( HasColumnInfo(column) )
2991 return GetColumnInfo(column)->GetImage();
2992
2993 return -1;
2994 }
2995
2996 void wxMacListCtrlItem::SetColumnImageValue( unsigned int column, int imageIndex )
2997 {
2998 if ( HasColumnInfo(column) )
2999 GetColumnInfo(column)->SetImage(imageIndex);
3000 }
3001
3002 wxString wxMacListCtrlItem::GetColumnTextValue( unsigned int column )
3003 {
3004 if ( column == 0 )
3005 return GetLabel();
3006
3007 if ( HasColumnInfo(column) )
3008 return GetColumnInfo(column)->GetText();
3009
3010 return wxEmptyString;
3011 }
3012
3013 void wxMacListCtrlItem::SetColumnTextValue( unsigned int column, const wxString& text )
3014 {
3015 if ( HasColumnInfo(column) )
3016 GetColumnInfo(column)->SetText(text);
3017
3018 // for compatibility with superclass APIs
3019 if ( column == 0 )
3020 SetLabel(text);
3021 }
3022
3023 wxListItem* wxMacListCtrlItem::GetColumnInfo( unsigned int column )
3024 {
3025 wxListItemList::compatibility_iterator node = m_rowItems.Find( column );
3026 wxASSERT_MSG( node, _T("invalid column index in wxMacListCtrlItem") );
3027
3028 return node->GetData();
3029 }
3030
3031 bool wxMacListCtrlItem::HasColumnInfo( unsigned int column )
3032 {
3033 return m_rowItems.Find( column ) != NULL;
3034 }
3035
3036 void wxMacListCtrlItem::SetColumnInfo( unsigned int column, wxListItem* item )
3037 {
3038
3039 if ( !HasColumnInfo(column) )
3040 {
3041 wxListItem* listItem = new wxListItem(*item);
3042 m_rowItems.Append( column, listItem );
3043 }
3044 else
3045 {
3046 wxListItem* listItem = GetColumnInfo( column );
3047 long mask = item->GetMask();
3048 if (mask & wxLIST_MASK_TEXT)
3049 listItem->SetText(item->GetText());
3050 if (mask & wxLIST_MASK_DATA)
3051 listItem->SetData(item->GetData());
3052 if (mask & wxLIST_MASK_IMAGE)
3053 listItem->SetImage(item->GetImage());
3054 if (mask & wxLIST_MASK_STATE)
3055 listItem->SetState(item->GetState());
3056 if (mask & wxLIST_MASK_FORMAT)
3057 listItem->SetAlign(item->GetAlign());
3058 if (mask & wxLIST_MASK_WIDTH)
3059 listItem->SetWidth(item->GetWidth());
3060
3061 if ( item->HasAttributes() )
3062 {
3063 if ( listItem->HasAttributes() )
3064 listItem->GetAttributes()->AssignFrom(*item->GetAttributes());
3065 else
3066 {
3067 listItem->SetTextColour(item->GetTextColour());
3068 listItem->SetBackgroundColour(item->GetBackgroundColour());
3069 listItem->SetFont(item->GetFont());
3070 }
3071 }
3072 }
3073 }
3074
3075 #endif // wxUSE_LISTCTRL