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