]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/listctrl_mac.cpp
9cfef3b8d6445fb27d6c4faa8711a3ed8e71b8eb
[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 if (m_dbImpl && geom == wxLIST_NEXT_ALL && state == wxLIST_STATE_SELECTED )
1564 {
1565 long count = m_dbImpl->MacGetCount() ;
1566 for ( long line = item + 1 ; line < count; line++ )
1567 {
1568 wxMacDataItem* id = m_dbImpl->GetItemFromLine(line);
1569 if ( m_dbImpl->IsItemSelected(id ) )
1570 return line;
1571 }
1572 return -1;
1573 }
1574
1575 return 0;
1576 }
1577
1578
1579 wxImageList *wxListCtrl::GetImageList(int which) const
1580 {
1581 if (m_genericImpl)
1582 return m_genericImpl->GetImageList(which);
1583
1584 if ( which == wxIMAGE_LIST_NORMAL )
1585 {
1586 return m_imageListNormal;
1587 }
1588 else if ( which == wxIMAGE_LIST_SMALL )
1589 {
1590 return m_imageListSmall;
1591 }
1592 else if ( which == wxIMAGE_LIST_STATE )
1593 {
1594 return m_imageListState;
1595 }
1596 return NULL;
1597 }
1598
1599 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
1600 {
1601 if (m_genericImpl)
1602 {
1603 m_genericImpl->SetImageList(imageList, which);
1604 return;
1605 }
1606
1607 if ( which == wxIMAGE_LIST_NORMAL )
1608 {
1609 if (m_ownsImageListNormal) delete m_imageListNormal;
1610 m_imageListNormal = imageList;
1611 m_ownsImageListNormal = false;
1612 }
1613 else if ( which == wxIMAGE_LIST_SMALL )
1614 {
1615 if (m_ownsImageListSmall) delete m_imageListSmall;
1616 m_imageListSmall = imageList;
1617 m_ownsImageListSmall = false;
1618 }
1619 else if ( which == wxIMAGE_LIST_STATE )
1620 {
1621 if (m_ownsImageListState) delete m_imageListState;
1622 m_imageListState = imageList;
1623 m_ownsImageListState = false;
1624 }
1625 }
1626
1627 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1628 {
1629 if (m_genericImpl)
1630 {
1631 m_genericImpl->AssignImageList(imageList, which);
1632 return;
1633 }
1634
1635 SetImageList(imageList, which);
1636 if ( which == wxIMAGE_LIST_NORMAL )
1637 m_ownsImageListNormal = true;
1638 else if ( which == wxIMAGE_LIST_SMALL )
1639 m_ownsImageListSmall = true;
1640 else if ( which == wxIMAGE_LIST_STATE )
1641 m_ownsImageListState = true;
1642 }
1643
1644 // ----------------------------------------------------------------------------
1645 // Operations
1646 // ----------------------------------------------------------------------------
1647
1648 // Arranges the items
1649 bool wxListCtrl::Arrange(int flag)
1650 {
1651 if (m_genericImpl)
1652 return m_genericImpl->Arrange(flag);
1653 return false;
1654 }
1655
1656 // Deletes an item
1657 bool wxListCtrl::DeleteItem(long item)
1658 {
1659 if (m_genericImpl)
1660 return m_genericImpl->DeleteItem(item);
1661
1662 if (m_dbImpl)
1663 {
1664 m_dbImpl->MacDelete(item);
1665 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ITEM, GetId() );
1666 event.SetEventObject( this );
1667 event.m_itemIndex = item;
1668 GetEventHandler()->ProcessEvent( event );
1669
1670 }
1671 return true;
1672 }
1673
1674 // Deletes all items
1675 bool wxListCtrl::DeleteAllItems()
1676 {
1677 if (m_genericImpl)
1678 return m_genericImpl->DeleteAllItems();
1679
1680 if (m_dbImpl)
1681 {
1682 m_dbImpl->MacClear();
1683 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetId() );
1684 event.SetEventObject( this );
1685 GetEventHandler()->ProcessEvent( event );
1686 }
1687 return true;
1688 }
1689
1690 // Deletes all items
1691 bool wxListCtrl::DeleteAllColumns()
1692 {
1693 if (m_genericImpl)
1694 return m_genericImpl->DeleteAllColumns();
1695
1696 if (m_dbImpl)
1697 {
1698 UInt32 cols;
1699 m_dbImpl->GetColumnCount(&cols);
1700 for (UInt32 col = 0; col < cols; col++)
1701 {
1702 DeleteColumn(0);
1703 }
1704 }
1705
1706 return true;
1707 }
1708
1709 // Deletes a column
1710 bool wxListCtrl::DeleteColumn(int col)
1711 {
1712 if (m_genericImpl)
1713 return m_genericImpl->DeleteColumn(col);
1714
1715 if (m_dbImpl)
1716 {
1717 OSStatus err = m_dbImpl->RemoveColumn(col);
1718 return err == noErr;
1719 }
1720
1721 return true;
1722 }
1723
1724 // Clears items, and columns if there are any.
1725 void wxListCtrl::ClearAll()
1726 {
1727 if (m_genericImpl)
1728 {
1729 m_genericImpl->ClearAll();
1730 return;
1731 }
1732
1733 if (m_dbImpl)
1734 {
1735 DeleteAllItems();
1736 DeleteAllColumns();
1737 }
1738 }
1739
1740 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1741 {
1742 if (m_genericImpl)
1743 return m_genericImpl->EditLabel(item, textControlClass);
1744
1745 if (m_dbImpl)
1746 {
1747 wxCHECK_MSG( (item >= 0) && ((long)item < GetItemCount()), NULL,
1748 wxT("wrong index in wxListCtrl::EditLabel()") );
1749
1750 wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
1751 wxT("EditLabel() needs a text control") );
1752
1753 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
1754 le.SetEventObject( this );
1755 le.m_itemIndex = item;
1756 le.m_col = 0;
1757 GetItem( le.m_item );
1758
1759 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
1760 {
1761 // vetoed by user code
1762 return NULL;
1763 }
1764
1765 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
1766 m_textctrlWrapper = new wxListCtrlTextCtrlWrapper(this, text, item);
1767 return m_textctrlWrapper->GetText();
1768 }
1769 return NULL;
1770 }
1771
1772 // End label editing, optionally cancelling the edit
1773 bool wxListCtrl::EndEditLabel(bool cancel)
1774 {
1775 // TODO: generic impl. doesn't have this method - is it needed for us?
1776 if (m_genericImpl)
1777 return true; // m_genericImpl->EndEditLabel(cancel);
1778
1779 if (m_dbImpl)
1780 verify_noerr( SetDataBrowserEditItem(m_dbImpl->GetControlRef(), kDataBrowserNoItem, kMinColumnId) );
1781 return true;
1782 }
1783
1784 // Ensures this item is visible
1785 bool wxListCtrl::EnsureVisible(long item)
1786 {
1787 if (m_genericImpl)
1788 return m_genericImpl->EnsureVisible(item);
1789
1790 if (m_dbImpl)
1791 {
1792 wxMacDataItem* dataItem = m_dbImpl->GetItemFromLine(item);
1793 m_dbImpl->RevealItem(dataItem, kDataBrowserRevealWithoutSelecting);
1794 }
1795
1796 return true;
1797 }
1798
1799 // Find an item whose label matches this string, starting from the item after 'start'
1800 // or the beginning if 'start' is -1.
1801 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1802 {
1803 if (m_genericImpl)
1804 return m_genericImpl->FindItem(start, str, partial);
1805
1806 wxString str_upper = str.Upper();
1807
1808 long idx = start;
1809 if (idx < 0)
1810 idx = 0;
1811 long count = GetItemCount();
1812
1813 while (idx < count)
1814 {
1815 wxString line_upper = GetItemText(idx).Upper();
1816 if (!partial)
1817 {
1818 if (line_upper == str_upper )
1819 return idx;
1820 }
1821 else
1822 {
1823 if (line_upper.find(str_upper) == 0)
1824 return idx;
1825 }
1826
1827 idx++;
1828 };
1829
1830 return wxNOT_FOUND;
1831 }
1832
1833 // Find an item whose data matches this data, starting from the item after 'start'
1834 // or the beginning if 'start' is -1.
1835 long wxListCtrl::FindItem(long start, long data)
1836 {
1837 if (m_genericImpl)
1838 return m_genericImpl->FindItem(start, data);
1839
1840 long idx = start;
1841 if (idx < 0)
1842 idx = 0;
1843 long count = GetItemCount();
1844
1845 while (idx < count)
1846 {
1847 if (GetItemData(idx) == data)
1848 return idx;
1849 idx++;
1850 };
1851
1852 return wxNOT_FOUND;
1853 }
1854
1855 // Find an item nearest this position in the specified direction, starting from
1856 // the item after 'start' or the beginning if 'start' is -1.
1857 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1858 {
1859 if (m_genericImpl)
1860 return m_genericImpl->FindItem(start, pt, direction);
1861 return -1;
1862 }
1863
1864 // Determines which item (if any) is at the specified point,
1865 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1866 long
1867 wxListCtrl::HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const
1868 {
1869 if (m_genericImpl)
1870 return m_genericImpl->HitTest(point, flags, ptrSubItem);
1871
1872 flags = wxLIST_HITTEST_NOWHERE;
1873 if (m_dbImpl)
1874 {
1875 int colHeaderHeight = 22; // TODO: Find a way to get this value from the db control?
1876 UInt16 rowHeight = 0;
1877 m_dbImpl->GetDefaultRowHeight(&rowHeight);
1878
1879 int y = point.y;
1880 // get the actual row by taking scroll position into account
1881 UInt32 offsetX, offsetY;
1882 m_dbImpl->GetScrollPosition( &offsetY, &offsetX );
1883 y += offsetY;
1884
1885 if ( !(GetWindowStyleFlag() & wxLC_NO_HEADER) )
1886 y -= colHeaderHeight;
1887
1888 if ( y < 0 )
1889 return -1;
1890
1891 int row = y / rowHeight;
1892 DataBrowserItemID id;
1893 m_dbImpl->GetItemID( (DataBrowserTableViewRowIndex) row, &id );
1894
1895 // TODO: Use GetDataBrowserItemPartBounds to return if we are in icon or label
1896 if ( !(GetWindowStyleFlag() & wxLC_VIRTUAL ) )
1897 {
1898 wxMacListCtrlItem* lcItem;
1899 lcItem = (wxMacListCtrlItem*) id;
1900 if (lcItem)
1901 {
1902 flags = wxLIST_HITTEST_ONITEM;
1903 return row;
1904 }
1905 }
1906 else
1907 {
1908 if (row < GetItemCount() )
1909 {
1910 flags = wxLIST_HITTEST_ONITEM;
1911 return row;
1912 }
1913 }
1914
1915 }
1916 return -1;
1917 }
1918
1919 int wxListCtrl::GetScrollPos(int orient) const
1920 {
1921 if (m_genericImpl)
1922 return m_genericImpl->GetScrollPos(orient);
1923
1924 if (m_dbImpl)
1925 {
1926 UInt32 offsetX, offsetY;
1927 m_dbImpl->GetScrollPosition( &offsetY, &offsetX );
1928 if ( orient == wxHORIZONTAL )
1929 return offsetX;
1930 else
1931 return offsetY;
1932 }
1933
1934 return 0;
1935 }
1936
1937 // Inserts an item, returning the index of the new item if successful,
1938 // -1 otherwise.
1939 long wxListCtrl::InsertItem(wxListItem& info)
1940 {
1941 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1942
1943 if (m_genericImpl)
1944 return m_genericImpl->InsertItem(info);
1945
1946 if (m_dbImpl && !IsVirtual())
1947 {
1948 int count = GetItemCount();
1949
1950 if (info.m_itemId > count)
1951 info.m_itemId = count;
1952
1953 m_dbImpl->MacInsertItem(info.m_itemId, &info );
1954
1955 wxListEvent event( wxEVT_COMMAND_LIST_INSERT_ITEM, GetId() );
1956 event.SetEventObject( this );
1957 event.m_itemIndex = info.m_itemId;
1958 GetEventHandler()->ProcessEvent( event );
1959 return info.m_itemId;
1960 }
1961 return -1;
1962 }
1963
1964 long wxListCtrl::InsertItem(long index, const wxString& label)
1965 {
1966 if (m_genericImpl)
1967 return m_genericImpl->InsertItem(index, label);
1968
1969 wxListItem info;
1970 info.m_text = label;
1971 info.m_mask = wxLIST_MASK_TEXT;
1972 info.m_itemId = index;
1973 return InsertItem(info);
1974 }
1975
1976 // Inserts an image item
1977 long wxListCtrl::InsertItem(long index, int imageIndex)
1978 {
1979 if (m_genericImpl)
1980 return m_genericImpl->InsertItem(index, imageIndex);
1981
1982 wxListItem info;
1983 info.m_image = imageIndex;
1984 info.m_mask = wxLIST_MASK_IMAGE;
1985 info.m_itemId = index;
1986 return InsertItem(info);
1987 }
1988
1989 // Inserts an image/string item
1990 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1991 {
1992 if (m_genericImpl)
1993 return m_genericImpl->InsertItem(index, label, imageIndex);
1994
1995 wxListItem info;
1996 info.m_image = imageIndex;
1997 info.m_text = label;
1998 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1999 info.m_itemId = index;
2000 return InsertItem(info);
2001 }
2002
2003 // For list view mode (only), inserts a column.
2004 long wxListCtrl::InsertColumn(long col, wxListItem& item)
2005 {
2006 if (m_genericImpl)
2007 return m_genericImpl->InsertColumn(col, item);
2008
2009 if (m_dbImpl)
2010 {
2011 int width = item.GetWidth();
2012 if ( !(item.GetMask() & wxLIST_MASK_WIDTH) )
2013 width = 150;
2014
2015 DataBrowserPropertyType type = kDataBrowserCustomType; //kDataBrowserTextType;
2016 wxImageList* imageList = GetImageList(wxIMAGE_LIST_SMALL);
2017 if (imageList && imageList->GetImageCount() > 0)
2018 {
2019 wxBitmap bmp = imageList->GetBitmap(0);
2020 //if (bmp.Ok())
2021 // type = kDataBrowserIconAndTextType;
2022 }
2023
2024 SInt16 just = teFlushDefault;
2025 if (item.GetMask() & wxLIST_MASK_FORMAT)
2026 {
2027 if (item.GetAlign() == wxLIST_FORMAT_LEFT)
2028 just = teFlushLeft;
2029 else if (item.GetAlign() == wxLIST_FORMAT_CENTER)
2030 just = teCenter;
2031 else if (item.GetAlign() == wxLIST_FORMAT_RIGHT)
2032 just = teFlushRight;
2033 }
2034 m_dbImpl->InsertColumn(col, type, item.GetText(), just, width);
2035 SetColumn(col, item);
2036
2037 // set/remove options based on the wxListCtrl type.
2038 DataBrowserTableViewColumnID id;
2039 m_dbImpl->GetColumnIDFromIndex(col, &id);
2040 DataBrowserPropertyFlags flags;
2041 verify_noerr(m_dbImpl->GetPropertyFlags(id, &flags));
2042 if (GetWindowStyleFlag() & wxLC_EDIT_LABELS)
2043 flags |= kDataBrowserPropertyIsEditable;
2044
2045 if (GetWindowStyleFlag() & wxLC_VIRTUAL){
2046 flags &= ~kDataBrowserListViewSortableColumn;
2047 }
2048 verify_noerr(m_dbImpl->SetPropertyFlags(id, flags));
2049 }
2050
2051 return col;
2052 }
2053
2054 long wxListCtrl::InsertColumn(long col,
2055 const wxString& heading,
2056 int format,
2057 int width)
2058 {
2059 if (m_genericImpl)
2060 return m_genericImpl->InsertColumn(col, heading, format, width);
2061
2062 wxListItem item;
2063 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
2064 item.m_text = heading;
2065 if ( width > -1 )
2066 {
2067 item.m_mask |= wxLIST_MASK_WIDTH;
2068 item.m_width = width;
2069 }
2070 item.m_format = format;
2071
2072 return InsertColumn(col, item);
2073 }
2074
2075 // scroll the control by the given number of pixels (exception: in list view,
2076 // dx is interpreted as number of columns)
2077 bool wxListCtrl::ScrollList(int dx, int dy)
2078 {
2079 if (m_genericImpl)
2080 return m_genericImpl->ScrollList(dx, dy);
2081
2082 if (m_dbImpl)
2083 {
2084 m_dbImpl->SetScrollPosition(dx, dy);
2085 }
2086 return true;
2087 }
2088
2089
2090 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
2091 {
2092 if (m_genericImpl)
2093 return m_genericImpl->SortItems(fn, data);
2094
2095 if (m_dbImpl)
2096 {
2097 m_compareFunc = fn;
2098 m_compareFuncData = data;
2099 SortDataBrowserContainer( m_dbImpl->GetControlRef(), kDataBrowserNoItem, true);
2100
2101 // we need to do this after each call, else we get a crash from wxPython when
2102 // SortItems is called the second time.
2103 m_compareFunc = NULL;
2104 m_compareFuncData = 0;
2105 }
2106
2107 return true;
2108 }
2109
2110 void wxListCtrl::OnRenameTimer()
2111 {
2112 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2113
2114 EditLabel( m_current );
2115 }
2116
2117 bool wxListCtrl::OnRenameAccept(long itemEdit, const wxString& value)
2118 {
2119 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetId() );
2120 le.SetEventObject( this );
2121 le.m_itemIndex = itemEdit;
2122
2123 GetItem( le.m_item );
2124 le.m_item.m_text = value;
2125 return !GetEventHandler()->ProcessEvent( le ) ||
2126 le.IsAllowed();
2127 }
2128
2129 void wxListCtrl::OnRenameCancelled(long itemEdit)
2130 {
2131 // let owner know that the edit was cancelled
2132 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2133
2134 le.SetEditCanceled(true);
2135
2136 le.SetEventObject( this );
2137 le.m_itemIndex = itemEdit;
2138
2139 GetItem( le.m_item );
2140 GetEventHandler()->ProcessEvent( le );
2141 }
2142
2143 // ----------------------------------------------------------------------------
2144 // virtual list controls
2145 // ----------------------------------------------------------------------------
2146
2147 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2148 {
2149 // this is a pure virtual function, in fact - which is not really pure
2150 // because the controls which are not virtual don't need to implement it
2151 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2152
2153 return wxEmptyString;
2154 }
2155
2156 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2157 {
2158 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
2159 -1,
2160 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2161 return -1;
2162 }
2163
2164 int wxListCtrl::OnGetItemColumnImage(long item, long column) const
2165 {
2166 if (!column)
2167 return OnGetItemImage(item);
2168
2169 return -1;
2170 }
2171
2172 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2173 {
2174 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2175 _T("invalid item index in OnGetItemAttr()") );
2176
2177 // no attributes by default
2178 return NULL;
2179 }
2180
2181 void wxListCtrl::SetItemCount(long count)
2182 {
2183 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2184
2185 if (m_genericImpl)
2186 {
2187 m_genericImpl->SetItemCount(count);
2188 return;
2189 }
2190
2191 if (m_dbImpl)
2192 {
2193 // we need to temporarily disable the new item creation notification
2194 // procedure to speed things up
2195 // FIXME: Even this doesn't seem to help much...
2196
2197 // FIXME: Find a more efficient way to do this.
2198 m_dbImpl->MacClear();
2199
2200 DataBrowserCallbacks callbacks;
2201 DataBrowserItemNotificationUPP itemUPP;
2202 GetDataBrowserCallbacks(m_dbImpl->GetControlRef(), &callbacks);
2203 itemUPP = callbacks.u.v1.itemNotificationCallback;
2204 callbacks.u.v1.itemNotificationCallback = 0;
2205 m_dbImpl->SetCallbacks(&callbacks);
2206 ::AddDataBrowserItems(m_dbImpl->GetControlRef(), kDataBrowserNoItem,
2207 count, NULL, kDataBrowserItemNoProperty);
2208 callbacks.u.v1.itemNotificationCallback = itemUPP;
2209 m_dbImpl->SetCallbacks(&callbacks);
2210 }
2211 m_count = count;
2212 }
2213
2214 void wxListCtrl::RefreshItem(long item)
2215 {
2216 if (m_genericImpl)
2217 {
2218 m_genericImpl->RefreshItem(item);
2219 return;
2220 }
2221
2222 wxRect rect;
2223 GetItemRect(item, rect);
2224 RefreshRect(rect);
2225 }
2226
2227 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2228 {
2229 if (m_genericImpl)
2230 {
2231 m_genericImpl->RefreshItems(itemFrom, itemTo);
2232 return;
2233 }
2234
2235 wxRect rect1, rect2;
2236 GetItemRect(itemFrom, rect1);
2237 GetItemRect(itemTo, rect2);
2238
2239 wxRect rect = rect1;
2240 rect.height = rect2.GetBottom() - rect1.GetTop();
2241
2242 RefreshRect(rect);
2243 }
2244
2245 void wxListCtrl::SetDropTarget( wxDropTarget *dropTarget )
2246 {
2247 #if wxUSE_DRAG_AND_DROP
2248 if (m_genericImpl)
2249 m_genericImpl->SetDropTarget( dropTarget );
2250
2251 if (m_dbImpl)
2252 wxWindow::SetDropTarget( dropTarget );
2253 #endif
2254 }
2255
2256 wxDropTarget *wxListCtrl::GetDropTarget() const
2257 {
2258 #if wxUSE_DRAG_AND_DROP
2259 if (m_genericImpl)
2260 return m_genericImpl->GetDropTarget();
2261
2262 if (m_dbImpl)
2263 return wxWindow::GetDropTarget();
2264 #endif
2265 return NULL;
2266 }
2267
2268 #if wxABI_VERSION >= 20801
2269 void wxListCtrl::SetFocus()
2270 {
2271 if (m_genericImpl)
2272 {
2273 m_genericImpl->SetFocus();
2274 return;
2275 }
2276
2277 wxWindow::SetFocus();
2278 }
2279 #endif
2280
2281 // wxMac internal data structures
2282
2283 wxMacListCtrlItem::~wxMacListCtrlItem()
2284 {
2285 }
2286
2287 void wxMacListCtrlItem::Notification(wxMacDataItemBrowserControl *owner ,
2288 DataBrowserItemNotification message,
2289 DataBrowserItemDataRef itemData ) const
2290 {
2291
2292 wxMacDataBrowserListCtrlControl *lb = wxDynamicCast(owner, wxMacDataBrowserListCtrlControl);
2293
2294 // we want to depend on as little as possible to make sure tear-down of controls is safe
2295 if ( message == kDataBrowserItemRemoved)
2296 {
2297 if ( lb != NULL && lb->GetClientDataType() == wxClientData_Object )
2298 {
2299 delete (wxClientData*) (m_data);
2300 }
2301
2302 delete this;
2303 return;
2304 }
2305 else if ( message == kDataBrowserItemAdded )
2306 {
2307 // we don't issue events on adding, the item is not really stored in the list yet, so we
2308 // avoid asserts by gettting out now
2309 return ;
2310 }
2311
2312 wxListCtrl *list = wxDynamicCast( owner->GetPeer() , wxListCtrl );
2313 if ( list && lb )
2314 {
2315 bool trigger = false;
2316
2317 wxListEvent event( wxEVT_COMMAND_LIST_ITEM_SELECTED, list->GetId() );
2318 bool isSingle = (list->GetWindowStyle() & wxLC_SINGLE_SEL) != 0;
2319
2320 event.SetEventObject( list );
2321 event.m_itemIndex = owner->GetLineFromItem( this ) ;
2322 if ( !list->IsVirtual() )
2323 {
2324 lb->MacGetColumnInfo(event.m_itemIndex,0,event.m_item);
2325 }
2326
2327 switch (message)
2328 {
2329 case kDataBrowserItemDeselected:
2330 event.SetEventType(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
2331 if ( !isSingle )
2332 trigger = !lb->IsSelectionSuppressed();
2333 break;
2334
2335 case kDataBrowserItemSelected:
2336 trigger = !lb->IsSelectionSuppressed();
2337 break;
2338
2339 case kDataBrowserItemDoubleClicked:
2340 event.SetEventType( wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2341 trigger = true;
2342 break;
2343
2344 case kDataBrowserEditStarted :
2345 // TODO : how to veto ?
2346 event.SetEventType( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT ) ;
2347 trigger = true ;
2348 break ;
2349
2350 case kDataBrowserEditStopped :
2351 // TODO probably trigger only upon the value store callback, because
2352 // here IIRC we cannot veto
2353 event.SetEventType( wxEVT_COMMAND_LIST_END_LABEL_EDIT ) ;
2354 trigger = true ;
2355 break ;
2356
2357 default:
2358 break;
2359 }
2360
2361 if ( trigger )
2362 {
2363 // direct notification is not always having the listbox GetSelection() having in synch with event
2364 wxPostEvent( list->GetEventHandler(), event );
2365 }
2366 }
2367
2368 }
2369
2370 IMPLEMENT_DYNAMIC_CLASS(wxMacDataBrowserListCtrlControl, wxMacDataItemBrowserControl )
2371
2372 wxMacDataBrowserListCtrlControl::wxMacDataBrowserListCtrlControl( wxWindow *peer, const wxPoint& pos, const wxSize& size, long style)
2373 : wxMacDataItemBrowserControl( peer, pos, size, style )
2374 {
2375 OSStatus err = noErr;
2376 m_clientDataItemsType = wxClientData_None;
2377 m_isVirtual = false;
2378 m_flags = 0;
2379
2380 if ( style & wxLC_VIRTUAL )
2381 m_isVirtual = true;
2382
2383 DataBrowserSelectionFlags options = kDataBrowserDragSelect;
2384 if ( style & wxLC_SINGLE_SEL )
2385 {
2386 options |= kDataBrowserSelectOnlyOne;
2387 }
2388 else
2389 {
2390 options |= kDataBrowserCmdTogglesSelection;
2391 }
2392
2393 err = SetSelectionFlags( options );
2394 verify_noerr( err );
2395
2396 DataBrowserCustomCallbacks callbacks;
2397 InitializeDataBrowserCustomCallbacks( &callbacks, kDataBrowserLatestCustomCallbacks );
2398
2399 if ( gDataBrowserDrawItemUPP == NULL )
2400 gDataBrowserDrawItemUPP = NewDataBrowserDrawItemUPP(DataBrowserDrawItemProc);
2401
2402 if ( gDataBrowserHitTestUPP == NULL )
2403 gDataBrowserHitTestUPP = NewDataBrowserHitTestUPP(DataBrowserHitTestProc);
2404
2405 callbacks.u.v1.drawItemCallback = gDataBrowserDrawItemUPP;
2406 callbacks.u.v1.hitTestCallback = gDataBrowserHitTestUPP;
2407
2408 SetDataBrowserCustomCallbacks( GetControlRef(), &callbacks );
2409
2410 if ( style & wxLC_LIST )
2411 {
2412 InsertColumn(0, kDataBrowserIconAndTextType, wxEmptyString, -1, -1);
2413 verify_noerr( AutoSizeColumns() );
2414 }
2415
2416 if ( style & wxLC_LIST || style & wxLC_NO_HEADER )
2417 verify_noerr( SetHeaderButtonHeight( 0 ) );
2418
2419 if ( m_isVirtual )
2420 SetSortProperty( kMinColumnId - 1 );
2421 else
2422 SetSortProperty( kMinColumnId );
2423
2424 m_sortOrder = SortOrder_None;
2425
2426 if ( style & wxLC_SORT_DESCENDING )
2427 {
2428 SetSortOrder( kDataBrowserOrderDecreasing );
2429 }
2430 else if ( style & wxLC_SORT_ASCENDING )
2431 {
2432 SetSortOrder( kDataBrowserOrderIncreasing );
2433 }
2434
2435 if ( style & wxLC_VRULES )
2436 {
2437 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
2438 verify_noerr( DataBrowserChangeAttributes(m_controlRef, kDataBrowserAttributeListViewDrawColumnDividers, kDataBrowserAttributeNone) );
2439 #endif
2440 }
2441
2442 verify_noerr( SetHiliteStyle(kDataBrowserTableViewFillHilite ) );
2443 verify_noerr( SetHasScrollBars( (style & wxHSCROLL) != 0 , true ) );
2444 }
2445
2446 pascal Boolean wxMacDataBrowserListCtrlControl::DataBrowserEditTextProc(
2447 ControlRef browser,
2448 DataBrowserItemID itemID,
2449 DataBrowserPropertyID property,
2450 CFStringRef theString,
2451 Rect *maxEditTextRect,
2452 Boolean *shrinkToFit)
2453 {
2454 Boolean result = false;
2455 wxMacDataBrowserListCtrlControl* ctl = wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser ), wxMacDataBrowserListCtrlControl);
2456 if ( ctl != 0 )
2457 {
2458 result = ctl->ConfirmEditText(itemID, property, theString, maxEditTextRect, shrinkToFit);
2459 theString = CFSTR("Hello!");
2460 }
2461 return result;
2462 }
2463
2464 bool wxMacDataBrowserListCtrlControl::ConfirmEditText(
2465 DataBrowserItemID itemID,
2466 DataBrowserPropertyID property,
2467 CFStringRef theString,
2468 Rect *maxEditTextRect,
2469 Boolean *shrinkToFit)
2470 {
2471 return false;
2472 }
2473
2474 pascal void wxMacDataBrowserListCtrlControl::DataBrowserDrawItemProc(
2475 ControlRef browser,
2476 DataBrowserItemID itemID,
2477 DataBrowserPropertyID property,
2478 DataBrowserItemState itemState,
2479 const Rect *itemRect,
2480 SInt16 gdDepth,
2481 Boolean colorDevice)
2482 {
2483 wxMacDataBrowserListCtrlControl* ctl = wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser ), wxMacDataBrowserListCtrlControl);
2484 if ( ctl != 0 )
2485 {
2486 ctl->DrawItem(itemID, property, itemState, itemRect, gdDepth, colorDevice);
2487 }
2488 }
2489
2490 // routines needed for DrawItem
2491 enum
2492 {
2493 kIconWidth = 16,
2494 kIconHeight = 16,
2495 kTextBoxHeight = 14,
2496 kIconTextSpacingV = 2,
2497 kItemPadding = 4,
2498 kContentHeight = kIconHeight + kTextBoxHeight + kIconTextSpacingV
2499 };
2500
2501 static void calculateCGDrawingBounds(CGRect inItemRect, CGRect *outIconRect, CGRect *outTextRect, bool hasIcon = false)
2502 {
2503 float textBottom;
2504 float iconH, iconW = 0;
2505 float padding = kItemPadding;
2506 if (hasIcon)
2507 {
2508 iconH = kIconHeight;
2509 iconW = kIconWidth;
2510 padding = padding*2;
2511 }
2512
2513 textBottom = inItemRect.origin.y;
2514
2515 *outIconRect = CGRectMake(inItemRect.origin.x + kItemPadding,
2516 textBottom + kIconTextSpacingV, kIconWidth,
2517 kIconHeight);
2518
2519 *outTextRect = CGRectMake(inItemRect.origin.x + padding + iconW,
2520 textBottom + kIconTextSpacingV, inItemRect.size.width - padding - iconW,
2521 inItemRect.size.height - kIconTextSpacingV);
2522 }
2523
2524 void wxMacDataBrowserListCtrlControl::DrawItem(
2525 DataBrowserItemID itemID,
2526 DataBrowserPropertyID property,
2527 DataBrowserItemState itemState,
2528 const Rect *itemRect,
2529 SInt16 gdDepth,
2530 Boolean colorDevice)
2531 {
2532 wxString text;
2533 wxFont font = wxNullFont;
2534 int imgIndex = -1;
2535 short listColumn = property - kMinColumnId;
2536
2537 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
2538 wxMacListCtrlItem* lcItem;
2539 wxColour color = *wxBLACK;
2540 wxColour bgColor = wxNullColour;
2541
2542 if (listColumn >= 0)
2543 {
2544 if (!m_isVirtual)
2545 {
2546 lcItem = (wxMacListCtrlItem*) itemID;
2547 if (lcItem->HasColumnInfo(listColumn)){
2548 wxListItem* item = lcItem->GetColumnInfo(listColumn);
2549
2550 // we always use the 0 column to get font and text/background colors.
2551 if (lcItem->HasColumnInfo(0))
2552 {
2553 wxListItem* firstItem = lcItem->GetColumnInfo(0);
2554 color = firstItem->GetTextColour();
2555 bgColor = firstItem->GetBackgroundColour();
2556 font = firstItem->GetFont();
2557 }
2558
2559 if (item->GetMask() & wxLIST_MASK_TEXT)
2560 text = item->GetText();
2561 if (item->GetMask() & wxLIST_MASK_IMAGE)
2562 imgIndex = item->GetImage();
2563 }
2564
2565 }
2566 else
2567 {
2568 text = list->OnGetItemText( (long)itemID-1, listColumn );
2569 imgIndex = list->OnGetItemColumnImage( (long)itemID-1, listColumn );
2570 wxListItemAttr* attrs = list->OnGetItemAttr( (long)itemID-1 );
2571 if (attrs)
2572 {
2573 if (attrs->HasBackgroundColour())
2574 bgColor = attrs->GetBackgroundColour();
2575 if (attrs->HasTextColour())
2576 color = attrs->GetTextColour();
2577 if (attrs->HasFont())
2578 font = attrs->GetFont();
2579 }
2580 }
2581 }
2582
2583 wxColour listBgColor = list->GetBackgroundColour();
2584 if (bgColor == wxNullColour)
2585 bgColor = listBgColor;
2586
2587 wxFont listFont = list->GetFont();
2588 if (font == wxNullFont)
2589 font = listFont;
2590
2591 wxMacCFStringHolder cfString;
2592 cfString.Assign( text, wxLocale::GetSystemEncoding() );
2593
2594 Rect enclosingRect;
2595 CGRect enclosingCGRect, iconCGRect, textCGRect;
2596 Boolean active;
2597 ThemeDrawingState savedState = NULL;
2598 CGContextRef context = (CGContextRef)list->MacGetDrawingContext();
2599 RGBColor labelColor;
2600 labelColor.red = 0;
2601 labelColor.green = 0;
2602 labelColor.blue = 0;
2603
2604 RGBColor backgroundColor;
2605 backgroundColor.red = 255;
2606 backgroundColor.green = 255;
2607 backgroundColor.blue = 255;
2608
2609 GetDataBrowserItemPartBounds(GetControlRef(), itemID, property, kDataBrowserPropertyEnclosingPart,
2610 &enclosingRect);
2611
2612 enclosingCGRect = CGRectMake(enclosingRect.left,
2613 enclosingRect.top,
2614 enclosingRect.right - enclosingRect.left,
2615 enclosingRect.bottom - enclosingRect.top);
2616
2617 active = IsControlActive(GetControlRef());
2618
2619 // don't paint the background over the vertical rule line
2620 if ( list->GetWindowStyleFlag() & wxLC_VRULES )
2621 {
2622 enclosingCGRect.origin.x += 1;
2623 enclosingCGRect.size.width -= 1;
2624 }
2625 if (itemState == kDataBrowserItemIsSelected)
2626 {
2627
2628 GetThemeDrawingState(&savedState);
2629
2630 if (active)
2631 {
2632 GetThemeBrushAsColor(kThemeBrushAlternatePrimaryHighlightColor, 32, true, &backgroundColor);
2633 GetThemeTextColor(kThemeTextColorWhite, gdDepth, colorDevice, &labelColor);
2634 }
2635 else
2636 {
2637 GetThemeBrushAsColor(kThemeBrushSecondaryHighlightColor, 32, true, &backgroundColor);
2638 GetThemeTextColor(kThemeTextColorBlack, gdDepth, colorDevice, &labelColor);
2639 }
2640 CGContextSaveGState(context);
2641
2642 CGContextSetRGBFillColor(context, (float)backgroundColor.red / (float)USHRT_MAX,
2643 (float)backgroundColor.green / (float)USHRT_MAX,
2644 (float)backgroundColor.blue / (float)USHRT_MAX, 1.0);
2645 CGContextFillRect(context, enclosingCGRect);
2646
2647 CGContextRestoreGState(context);
2648 }
2649 else
2650 {
2651
2652 if (color.Ok())
2653 labelColor = MAC_WXCOLORREF( color.GetPixel() );
2654 else if (list->GetTextColour().Ok())
2655 labelColor = MAC_WXCOLORREF( list->GetTextColour().GetPixel() );
2656
2657 if (bgColor.Ok())
2658 {
2659 backgroundColor = MAC_WXCOLORREF( bgColor.GetPixel() );
2660 CGContextSaveGState(context);
2661
2662 CGContextSetRGBFillColor(context, (float)backgroundColor.red / (float)USHRT_MAX,
2663 (float)backgroundColor.green / (float)USHRT_MAX,
2664 (float)backgroundColor.blue / (float)USHRT_MAX, 1.0);
2665 CGContextFillRect(context, enclosingCGRect);
2666
2667 CGContextRestoreGState(context);
2668 }
2669 }
2670
2671 calculateCGDrawingBounds(enclosingCGRect, &iconCGRect, &textCGRect, (imgIndex != -1) );
2672
2673 if (imgIndex != -1)
2674 {
2675 wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL);
2676 if (imageList && imageList->GetImageCount() > 0){
2677 wxBitmap bmp = imageList->GetBitmap(imgIndex);
2678 IconRef icon = bmp.GetBitmapData()->GetIconRef();
2679
2680 CGContextSaveGState(context);
2681 CGContextTranslateCTM(context, 0,iconCGRect.origin.y + CGRectGetMaxY(iconCGRect));
2682 CGContextScaleCTM(context,1.0f,-1.0f);
2683 PlotIconRefInContext(context, &iconCGRect, kAlignNone,
2684 active ? kTransformNone : kTransformDisabled, NULL,
2685 kPlotIconRefNormalFlags, icon);
2686
2687 CGContextRestoreGState(context);
2688 }
2689 }
2690
2691 HIThemeTextHorizontalFlush hFlush = kHIThemeTextHorizontalFlushLeft;
2692 UInt16 fontID = kThemeViewsFont;
2693
2694 if (font.Ok())
2695 {
2696 if (font.GetFamily() != wxFONTFAMILY_DEFAULT)
2697 fontID = font.MacGetThemeFontID();
2698
2699 // FIXME: replace these with CG or ATSUI calls so we can remove this #ifndef.
2700 #ifndef __LP64__
2701 ::TextSize( (short)(font.MacGetFontSize()) ) ;
2702 ::TextFace( font.MacGetFontStyle() ) ;
2703 #endif
2704 }
2705
2706 wxListItem item;
2707 list->GetColumn(listColumn, item);
2708 if (item.GetMask() & wxLIST_MASK_FORMAT)
2709 {
2710 if (item.GetAlign() == wxLIST_FORMAT_LEFT)
2711 hFlush = kHIThemeTextHorizontalFlushLeft;
2712 else if (item.GetAlign() == wxLIST_FORMAT_CENTER)
2713 hFlush = kHIThemeTextHorizontalFlushCenter;
2714 else if (item.GetAlign() == wxLIST_FORMAT_RIGHT)
2715 {
2716 hFlush = kHIThemeTextHorizontalFlushRight;
2717 textCGRect.origin.x -= kItemPadding; // give a little extra paddding
2718 }
2719 }
2720
2721 HIThemeTextInfo info;
2722 info.version = kHIThemeTextInfoVersionZero;
2723 info.state = active ? kThemeStateActive : kThemeStateInactive;
2724 info.fontID = fontID;
2725 info.horizontalFlushness = hFlush;
2726 info.verticalFlushness = kHIThemeTextVerticalFlushCenter;
2727 info.options = kHIThemeTextBoxOptionNone;
2728 info.truncationPosition = kHIThemeTextTruncationEnd;
2729 info.truncationMaxLines = 1;
2730
2731 CGContextSaveGState(context);
2732 CGContextSetRGBFillColor (context, (float)labelColor.red / (float)USHRT_MAX,
2733 (float)labelColor.green / (float)USHRT_MAX,
2734 (float)labelColor.blue / (float)USHRT_MAX, 1.0);
2735
2736 HIThemeDrawTextBox(cfString, &textCGRect, &info, context, kHIThemeOrientationNormal);
2737
2738 CGContextRestoreGState(context);
2739
2740 if (savedState != NULL)
2741 SetThemeDrawingState(savedState, true);
2742 }
2743
2744 OSStatus wxMacDataBrowserListCtrlControl::GetSetItemData(DataBrowserItemID itemID,
2745 DataBrowserPropertyID property,
2746 DataBrowserItemDataRef itemData,
2747 Boolean changeValue )
2748 {
2749 wxString text;
2750 int imgIndex = -1;
2751 short listColumn = property - kMinColumnId;
2752
2753 OSStatus err = errDataBrowserPropertyNotSupported;
2754 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
2755 wxMacListCtrlItem* lcItem;
2756
2757 if (listColumn >= 0)
2758 {
2759 if (!m_isVirtual)
2760 {
2761 lcItem = (wxMacListCtrlItem*) itemID;
2762 if (lcItem && lcItem->HasColumnInfo(listColumn)){
2763 wxListItem* item = lcItem->GetColumnInfo(listColumn);
2764 if (item->GetMask() & wxLIST_MASK_TEXT)
2765 text = item->GetText();
2766 if (item->GetMask() & wxLIST_MASK_IMAGE)
2767 imgIndex = item->GetImage();
2768 }
2769 }
2770 else
2771 {
2772 text = list->OnGetItemText( (long)itemID-1, listColumn );
2773 imgIndex = list->OnGetItemColumnImage( (long)itemID-1, listColumn );
2774 }
2775 }
2776
2777 if ( !changeValue )
2778 {
2779 switch (property)
2780 {
2781 case kDataBrowserItemIsEditableProperty :
2782 if ( list && list->HasFlag( wxLC_EDIT_LABELS ) )
2783 {
2784 verify_noerr(SetDataBrowserItemDataBooleanValue( itemData, true ));
2785 err = noErr ;
2786 }
2787 break ;
2788 default :
2789 if ( property >= kMinColumnId )
2790 {
2791 wxMacCFStringHolder cfStr;
2792
2793 if (text){
2794 cfStr.Assign( text, wxLocale::GetSystemEncoding() );
2795 err = ::SetDataBrowserItemDataText( itemData, cfStr );
2796 err = noErr;
2797 }
2798
2799
2800
2801 if ( imgIndex != -1 )
2802 {
2803 wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL);
2804 if (imageList && imageList->GetImageCount() > 0){
2805 wxBitmap bmp = imageList->GetBitmap(imgIndex);
2806 IconRef icon = bmp.GetBitmapData()->GetIconRef();
2807 ::SetDataBrowserItemDataIcon(itemData, icon);
2808 }
2809 }
2810
2811 }
2812 break ;
2813 }
2814
2815 }
2816 else
2817 {
2818 switch (property)
2819 {
2820 default:
2821 if ( property >= kMinColumnId )
2822 {
2823 short listColumn = property - kMinColumnId;
2824
2825 // TODO probably send the 'end edit' from here, as we
2826 // can then deal with the veto
2827 CFStringRef sr ;
2828 verify_noerr( GetDataBrowserItemDataText( itemData , &sr ) ) ;
2829 wxMacCFStringHolder cfStr(sr) ;;
2830 if (m_isVirtual)
2831 list->SetItem( (long)itemData-1 , listColumn, cfStr.AsString() ) ;
2832 else
2833 {
2834 if (lcItem)
2835 lcItem->SetColumnTextValue( listColumn, cfStr.AsString() );
2836 }
2837 err = noErr ;
2838 }
2839 break;
2840 }
2841 }
2842 return err;
2843 }
2844
2845 void wxMacDataBrowserListCtrlControl::ItemNotification(DataBrowserItemID itemID,
2846 DataBrowserItemNotification message,
2847 DataBrowserItemDataRef itemData )
2848 {
2849 // we want to depend on as little as possible to make sure tear-down of controls is safe
2850 if ( message == kDataBrowserItemRemoved)
2851 {
2852 // make sure MacDelete does the proper teardown.
2853 return;
2854 }
2855 else if ( message == kDataBrowserItemAdded )
2856 {
2857 // we don't issue events on adding, the item is not really stored in the list yet, so we
2858 // avoid asserts by getting out now
2859 return ;
2860 }
2861
2862 wxListCtrl *list = wxDynamicCast( GetPeer() , wxListCtrl );
2863 if ( list )
2864 {
2865 bool trigger = false;
2866
2867 wxListEvent event( wxEVT_COMMAND_LIST_ITEM_SELECTED, list->GetId() );
2868 bool isSingle = (list->GetWindowStyle() & wxLC_SINGLE_SEL) != 0;
2869
2870 event.SetEventObject( list );
2871 if ( !list->IsVirtual() )
2872 {
2873 DataBrowserTableViewRowIndex result = 0;
2874 verify_noerr( GetItemRow( itemID, &result ) ) ;
2875 event.m_itemIndex = result;
2876
2877 if (event.m_itemIndex >= 0)
2878 MacGetColumnInfo(event.m_itemIndex,0,event.m_item);
2879 }
2880 else
2881 {
2882 event.m_itemIndex = (long)itemID-1;
2883 }
2884
2885 switch (message)
2886 {
2887 case kDataBrowserItemDeselected:
2888 event.SetEventType(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
2889 if ( !isSingle )
2890 trigger = !IsSelectionSuppressed();
2891 break;
2892
2893 case kDataBrowserItemSelected:
2894 trigger = !IsSelectionSuppressed();
2895
2896 break;
2897
2898 case kDataBrowserItemDoubleClicked:
2899 event.SetEventType( wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2900 trigger = true;
2901 break;
2902
2903 case kDataBrowserEditStarted :
2904 // TODO : how to veto ?
2905 event.SetEventType( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT ) ;
2906 trigger = true ;
2907 break ;
2908
2909 case kDataBrowserEditStopped :
2910 // TODO probably trigger only upon the value store callback, because
2911 // here IIRC we cannot veto
2912 event.SetEventType( wxEVT_COMMAND_LIST_END_LABEL_EDIT ) ;
2913 trigger = true ;
2914 break ;
2915
2916 default:
2917 break;
2918 }
2919
2920 if ( trigger )
2921 {
2922 // direct notification is not always having the listbox GetSelection() having in synch with event
2923 wxPostEvent( list->GetEventHandler(), event );
2924 }
2925 }
2926 }
2927
2928 Boolean wxMacDataBrowserListCtrlControl::CompareItems(DataBrowserItemID itemOneID,
2929 DataBrowserItemID itemTwoID,
2930 DataBrowserPropertyID sortProperty)
2931 {
2932
2933 bool retval = false;
2934 wxString itemText;
2935 wxString otherItemText;
2936 long itemOrder;
2937 long otherItemOrder;
2938
2939 int colId = sortProperty - kMinColumnId;
2940
2941 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
2942
2943 DataBrowserSortOrder sort;
2944 verify_noerr(GetSortOrder(&sort));
2945
2946 if (colId >= 0)
2947 {
2948 if (!m_isVirtual)
2949 {
2950 wxMacListCtrlItem* item = (wxMacListCtrlItem*)itemOneID;
2951 wxMacListCtrlItem* otherItem = (wxMacListCtrlItem*)itemTwoID;
2952
2953 itemOrder = item->GetOrder();
2954 otherItemOrder = item->GetOrder();
2955
2956 wxListCtrlCompare func = list->GetCompareFunc();
2957 if (func != NULL)
2958 {
2959 long item1 = -1;
2960 long item2 = -1;
2961 if (item && item->HasColumnInfo(0))
2962 item1 = item->GetColumnInfo(0)->GetData();
2963 if (otherItem && otherItem->HasColumnInfo(0))
2964 item2 = otherItem->GetColumnInfo(0)->GetData();
2965
2966 if (item1 > -1 && item2 > -1)
2967 {
2968 int result = func(item1, item2, list->GetCompareFuncData());
2969 if (sort == kDataBrowserOrderIncreasing)
2970 return result >= 0;
2971 else
2972 return result < 0;
2973 }
2974 }
2975
2976 // we can't use the native control's sorting abilities, so just
2977 // sort by item id.
2978 return itemOrder < otherItemOrder;
2979 }
2980 else
2981 {
2982
2983 long itemNum = (long)itemOneID;
2984 long otherItemNum = (long)itemTwoID;
2985 itemText = list->OnGetItemText( itemNum-1, colId );
2986 otherItemText = list->OnGetItemText( otherItemNum-1, colId );
2987
2988 // virtual listctrls don't support sorting
2989 return itemNum < otherItemNum;
2990 }
2991 }
2992 else{
2993 // fallback for undefined cases
2994 retval = itemOneID < itemTwoID;
2995 }
2996
2997 return retval;
2998 }
2999
3000 wxMacDataBrowserListCtrlControl::~wxMacDataBrowserListCtrlControl()
3001 {
3002 }
3003
3004 void wxMacDataBrowserListCtrlControl::MacSetColumnInfo( unsigned int row, unsigned int column, wxListItem* item )
3005 {
3006 wxMacDataItem* dataItem = GetItemFromLine(row);
3007 wxASSERT_MSG( dataItem, _T("could not obtain wxMacDataItem for row in MacSetColumnInfo. Is row a valid wxListCtrl row?") );
3008 if (item)
3009 {
3010 wxMacListCtrlItem* listItem = wx_static_cast(wxMacListCtrlItem*,dataItem);
3011 bool hasInfo = listItem->HasColumnInfo( column );
3012 listItem->SetColumnInfo( column, item );
3013 listItem->SetOrder(row);
3014 UpdateState(dataItem, item);
3015
3016 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
3017
3018 // NB: When this call was made before a control was completely shown, it would
3019 // update the item prematurely (i.e. no text would be listed) and, on show,
3020 // only the sorted column would be refreshed, meaning only first column text labels
3021 // would be shown. Making sure not to update items until the control is visible
3022 // seems to fix this issue.
3023 if (hasInfo && list->IsShown())
3024 UpdateItem( wxMacDataBrowserRootContainer, listItem , kMinColumnId + column );
3025 }
3026 }
3027
3028 // apply changes that need to happen immediately, rather than when the
3029 // databrowser control fires a callback.
3030 void wxMacDataBrowserListCtrlControl::UpdateState(wxMacDataItem* dataItem, wxListItem* listItem)
3031 {
3032 bool isSelected = IsItemSelected( dataItem );
3033 bool isSelectedState = (listItem->GetState() == wxLIST_STATE_SELECTED);
3034
3035 // toggle the selection state if wxListInfo state and actual state don't match.
3036 if ( isSelected != isSelectedState )
3037 {
3038 DataBrowserSetOption options = kDataBrowserItemsAdd;
3039 if (!isSelectedState)
3040 options = kDataBrowserItemsRemove;
3041 SetSelectedItem(dataItem, options);
3042 }
3043 // TODO: Set column width if item width > than current column width
3044 }
3045
3046 void wxMacDataBrowserListCtrlControl::MacGetColumnInfo( unsigned int row, unsigned int column, wxListItem& item )
3047 {
3048 wxMacDataItem* dataItem = GetItemFromLine(row);
3049 wxASSERT_MSG( dataItem, _T("could not obtain wxMacDataItem in MacGetColumnInfo. Is row a valid wxListCtrl row?") );
3050 // CS should this guard against dataItem = 0 ? , as item is not a pointer if (item) is not appropriate
3051 //if (item)
3052 {
3053 wxMacListCtrlItem* listItem =wx_static_cast(wxMacListCtrlItem*,dataItem);
3054
3055 if (!listItem->HasColumnInfo( column ))
3056 return;
3057
3058 wxListItem* oldItem = listItem->GetColumnInfo( column );
3059
3060 if (oldItem)
3061 {
3062 long mask = item.GetMask();
3063 if ( !mask )
3064 // by default, get everything for backwards compatibility
3065 mask = -1;
3066
3067 if ( mask & wxLIST_MASK_TEXT )
3068 item.SetText(oldItem->GetText());
3069 if ( mask & wxLIST_MASK_IMAGE )
3070 item.SetImage(oldItem->GetImage());
3071 if ( mask & wxLIST_MASK_DATA )
3072 item.SetData(oldItem->GetData());
3073 if ( mask & wxLIST_MASK_STATE )
3074 item.SetState(oldItem->GetState());
3075 if ( mask & wxLIST_MASK_WIDTH )
3076 item.SetWidth(oldItem->GetWidth());
3077 if ( mask & wxLIST_MASK_FORMAT )
3078 item.SetAlign(oldItem->GetAlign());
3079
3080 item.SetTextColour(oldItem->GetTextColour());
3081 item.SetBackgroundColour(oldItem->GetBackgroundColour());
3082 item.SetFont(oldItem->GetFont());
3083 }
3084 }
3085 }
3086
3087 void wxMacDataBrowserListCtrlControl::MacInsertItem( unsigned int n, wxListItem* item )
3088 {
3089 wxMacDataItemBrowserControl::MacInsert(n, item->GetText());
3090 MacSetColumnInfo(n, 0, item);
3091 }
3092
3093 wxMacDataItem* wxMacDataBrowserListCtrlControl::CreateItem()
3094 {
3095 return new wxMacListCtrlItem();
3096 }
3097
3098 wxMacListCtrlItem::wxMacListCtrlItem()
3099 {
3100 m_rowItems = wxListItemList();
3101 }
3102
3103 int wxMacListCtrlItem::GetColumnImageValue( unsigned int column )
3104 {
3105 if ( HasColumnInfo(column) )
3106 return GetColumnInfo(column)->GetImage();
3107
3108 return -1;
3109 }
3110
3111 void wxMacListCtrlItem::SetColumnImageValue( unsigned int column, int imageIndex )
3112 {
3113 if ( HasColumnInfo(column) )
3114 GetColumnInfo(column)->SetImage(imageIndex);
3115 }
3116
3117 wxString wxMacListCtrlItem::GetColumnTextValue( unsigned int column )
3118 {
3119 if ( column == 0 )
3120 return GetLabel();
3121
3122 if ( HasColumnInfo(column) )
3123 return GetColumnInfo(column)->GetText();
3124
3125 return wxEmptyString;
3126 }
3127
3128 void wxMacListCtrlItem::SetColumnTextValue( unsigned int column, const wxString& text )
3129 {
3130 if ( HasColumnInfo(column) )
3131 GetColumnInfo(column)->SetText(text);
3132
3133 // for compatibility with superclass APIs
3134 if ( column == 0 )
3135 SetLabel(text);
3136 }
3137
3138 wxListItem* wxMacListCtrlItem::GetColumnInfo( unsigned int column )
3139 {
3140 wxASSERT_MSG( HasColumnInfo(column), _T("invalid column index in wxMacListCtrlItem") );
3141 return m_rowItems[column];
3142 }
3143
3144 bool wxMacListCtrlItem::HasColumnInfo( unsigned int column )
3145 {
3146 return !(m_rowItems.find( column ) == m_rowItems.end());
3147 }
3148
3149 void wxMacListCtrlItem::SetColumnInfo( unsigned int column, wxListItem* item )
3150 {
3151
3152 if ( !HasColumnInfo(column) )
3153 {
3154 wxListItem* listItem = new wxListItem(*item);
3155 m_rowItems[column] = listItem;
3156 }
3157 else
3158 {
3159 wxListItem* listItem = GetColumnInfo( column );
3160 long mask = item->GetMask();
3161 if (mask & wxLIST_MASK_TEXT)
3162 listItem->SetText(item->GetText());
3163 if (mask & wxLIST_MASK_DATA)
3164 listItem->SetData(item->GetData());
3165 if (mask & wxLIST_MASK_IMAGE)
3166 listItem->SetImage(item->GetImage());
3167 if (mask & wxLIST_MASK_STATE)
3168 listItem->SetState(item->GetState());
3169 if (mask & wxLIST_MASK_FORMAT)
3170 listItem->SetAlign(item->GetAlign());
3171 if (mask & wxLIST_MASK_WIDTH)
3172 listItem->SetWidth(item->GetWidth());
3173
3174 if ( item->HasAttributes() )
3175 {
3176 if ( listItem->HasAttributes() )
3177 listItem->GetAttributes()->AssignFrom(*item->GetAttributes());
3178 else
3179 {
3180 listItem->SetTextColour(item->GetTextColour());
3181 listItem->SetBackgroundColour(item->GetBackgroundColour());
3182 listItem->SetFont(item->GetFont());
3183 }
3184 }
3185 }
3186 }
3187
3188 #endif // wxUSE_LISTCTRL
3189