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