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