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