]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/listctrl_mac.cpp
6d7f881fd51b41ec07af6c16b1d26dfe9fb4a14b
[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 wxMacDataItem* dataItem = m_dbImpl->GetItemFromLine(info.m_itemId);
1950
1951 wxListEvent event( wxEVT_COMMAND_LIST_INSERT_ITEM, GetId() );
1952 event.SetEventObject( this );
1953 event.m_itemIndex = info.m_itemId;
1954 GetEventHandler()->ProcessEvent( event );
1955 return info.m_itemId;
1956 }
1957 return -1;
1958 }
1959
1960 long wxListCtrl::InsertItem(long index, const wxString& label)
1961 {
1962 if (m_genericImpl)
1963 return m_genericImpl->InsertItem(index, label);
1964
1965 wxListItem info;
1966 info.m_text = label;
1967 info.m_mask = wxLIST_MASK_TEXT;
1968 info.m_itemId = index;
1969 return InsertItem(info);
1970 }
1971
1972 // Inserts an image item
1973 long wxListCtrl::InsertItem(long index, int imageIndex)
1974 {
1975 if (m_genericImpl)
1976 return m_genericImpl->InsertItem(index, imageIndex);
1977
1978 wxListItem info;
1979 info.m_image = imageIndex;
1980 info.m_mask = wxLIST_MASK_IMAGE;
1981 info.m_itemId = index;
1982 return InsertItem(info);
1983 }
1984
1985 // Inserts an image/string item
1986 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1987 {
1988 if (m_genericImpl)
1989 return m_genericImpl->InsertItem(index, label, imageIndex);
1990
1991 wxListItem info;
1992 info.m_image = imageIndex;
1993 info.m_text = label;
1994 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1995 info.m_itemId = index;
1996 return InsertItem(info);
1997 }
1998
1999 // For list view mode (only), inserts a column.
2000 long wxListCtrl::InsertColumn(long col, wxListItem& item)
2001 {
2002 if (m_genericImpl)
2003 return m_genericImpl->InsertColumn(col, item);
2004
2005 if (m_dbImpl)
2006 {
2007 int width = item.GetWidth();
2008 if ( !(item.GetMask() & wxLIST_MASK_WIDTH) )
2009 width = 150;
2010
2011 DataBrowserPropertyType type = kDataBrowserCustomType; //kDataBrowserTextType;
2012 wxImageList* imageList = GetImageList(wxIMAGE_LIST_SMALL);
2013 if (imageList && imageList->GetImageCount() > 0)
2014 {
2015 wxBitmap bmp = imageList->GetBitmap(0);
2016 //if (bmp.Ok())
2017 // type = kDataBrowserIconAndTextType;
2018 }
2019
2020 SInt16 just = teFlushDefault;
2021 if (item.GetMask() & wxLIST_MASK_FORMAT)
2022 {
2023 if (item.GetAlign() == wxLIST_FORMAT_LEFT)
2024 just = teFlushLeft;
2025 else if (item.GetAlign() == wxLIST_FORMAT_CENTER)
2026 just = teCenter;
2027 else if (item.GetAlign() == wxLIST_FORMAT_RIGHT)
2028 just = teFlushRight;
2029 }
2030 m_dbImpl->InsertColumn(col, type, item.GetText(), just, width);
2031 SetColumn(col, item);
2032
2033 // set/remove options based on the wxListCtrl type.
2034 DataBrowserTableViewColumnID id;
2035 m_dbImpl->GetColumnIDFromIndex(col, &id);
2036 DataBrowserPropertyFlags flags;
2037 verify_noerr(m_dbImpl->GetPropertyFlags(id, &flags));
2038 if (GetWindowStyleFlag() & wxLC_EDIT_LABELS)
2039 flags |= kDataBrowserPropertyIsEditable;
2040
2041 if (GetWindowStyleFlag() & wxLC_VIRTUAL){
2042 flags &= ~kDataBrowserListViewSortableColumn;
2043 }
2044 verify_noerr(m_dbImpl->SetPropertyFlags(id, flags));
2045 }
2046
2047 return col;
2048 }
2049
2050 long wxListCtrl::InsertColumn(long col,
2051 const wxString& heading,
2052 int format,
2053 int width)
2054 {
2055 if (m_genericImpl)
2056 return m_genericImpl->InsertColumn(col, heading, format, width);
2057
2058 wxListItem item;
2059 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
2060 item.m_text = heading;
2061 if ( width > -1 )
2062 {
2063 item.m_mask |= wxLIST_MASK_WIDTH;
2064 item.m_width = width;
2065 }
2066 item.m_format = format;
2067
2068 return InsertColumn(col, item);
2069 }
2070
2071 // scroll the control by the given number of pixels (exception: in list view,
2072 // dx is interpreted as number of columns)
2073 bool wxListCtrl::ScrollList(int dx, int dy)
2074 {
2075 if (m_genericImpl)
2076 return m_genericImpl->ScrollList(dx, dy);
2077
2078 if (m_dbImpl)
2079 {
2080 m_dbImpl->SetScrollPosition(dx, dy);
2081 }
2082 return true;
2083 }
2084
2085
2086 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
2087 {
2088 if (m_genericImpl)
2089 return m_genericImpl->SortItems(fn, data);
2090
2091 if (m_dbImpl)
2092 {
2093 m_compareFunc = fn;
2094 m_compareFuncData = data;
2095 SortDataBrowserContainer( m_dbImpl->GetControlRef(), kDataBrowserNoItem, true);
2096
2097 // we need to do this after each call, else we get a crash from wxPython when
2098 // SortItems is called the second time.
2099 m_compareFunc = NULL;
2100 m_compareFuncData = 0;
2101 }
2102
2103 return true;
2104 }
2105
2106 void wxListCtrl::OnRenameTimer()
2107 {
2108 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2109
2110 EditLabel( m_current );
2111 }
2112
2113 bool wxListCtrl::OnRenameAccept(long itemEdit, const wxString& value)
2114 {
2115 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetId() );
2116 le.SetEventObject( this );
2117 le.m_itemIndex = itemEdit;
2118
2119 GetItem( le.m_item );
2120 le.m_item.m_text = value;
2121 return !GetEventHandler()->ProcessEvent( le ) ||
2122 le.IsAllowed();
2123 }
2124
2125 void wxListCtrl::OnRenameCancelled(long itemEdit)
2126 {
2127 // let owner know that the edit was cancelled
2128 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2129
2130 le.SetEditCanceled(true);
2131
2132 le.SetEventObject( this );
2133 le.m_itemIndex = itemEdit;
2134
2135 GetItem( le.m_item );
2136 GetEventHandler()->ProcessEvent( le );
2137 }
2138
2139 // ----------------------------------------------------------------------------
2140 // virtual list controls
2141 // ----------------------------------------------------------------------------
2142
2143 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2144 {
2145 // this is a pure virtual function, in fact - which is not really pure
2146 // because the controls which are not virtual don't need to implement it
2147 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2148
2149 return wxEmptyString;
2150 }
2151
2152 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2153 {
2154 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
2155 -1,
2156 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2157 return -1;
2158 }
2159
2160 int wxListCtrl::OnGetItemColumnImage(long item, long column) const
2161 {
2162 if (!column)
2163 return OnGetItemImage(item);
2164
2165 return -1;
2166 }
2167
2168 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2169 {
2170 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2171 _T("invalid item index in OnGetItemAttr()") );
2172
2173 // no attributes by default
2174 return NULL;
2175 }
2176
2177 void wxListCtrl::SetItemCount(long count)
2178 {
2179 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2180
2181 if (m_genericImpl)
2182 {
2183 m_genericImpl->SetItemCount(count);
2184 return;
2185 }
2186
2187 if (m_dbImpl)
2188 {
2189 // we need to temporarily disable the new item creation notification
2190 // procedure to speed things up
2191 // FIXME: Even this doesn't seem to help much...
2192
2193 // FIXME: Find a more efficient way to do this.
2194 m_dbImpl->MacClear();
2195
2196 DataBrowserCallbacks callbacks;
2197 DataBrowserItemNotificationUPP itemUPP;
2198 GetDataBrowserCallbacks(m_dbImpl->GetControlRef(), &callbacks);
2199 itemUPP = callbacks.u.v1.itemNotificationCallback;
2200 callbacks.u.v1.itemNotificationCallback = 0;
2201 m_dbImpl->SetCallbacks(&callbacks);
2202 ::AddDataBrowserItems(m_dbImpl->GetControlRef(), kDataBrowserNoItem,
2203 count, NULL, kDataBrowserItemNoProperty);
2204 callbacks.u.v1.itemNotificationCallback = itemUPP;
2205 m_dbImpl->SetCallbacks(&callbacks);
2206 }
2207 m_count = count;
2208 }
2209
2210 void wxListCtrl::RefreshItem(long item)
2211 {
2212 if (m_genericImpl)
2213 {
2214 m_genericImpl->RefreshItem(item);
2215 return;
2216 }
2217
2218 wxRect rect;
2219 GetItemRect(item, rect);
2220 RefreshRect(rect);
2221 }
2222
2223 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2224 {
2225 if (m_genericImpl)
2226 {
2227 m_genericImpl->RefreshItems(itemFrom, itemTo);
2228 return;
2229 }
2230
2231 wxRect rect1, rect2;
2232 GetItemRect(itemFrom, rect1);
2233 GetItemRect(itemTo, rect2);
2234
2235 wxRect rect = rect1;
2236 rect.height = rect2.GetBottom() - rect1.GetTop();
2237
2238 RefreshRect(rect);
2239 }
2240
2241 void wxListCtrl::SetDropTarget( wxDropTarget *dropTarget )
2242 {
2243 #if wxUSE_DRAG_AND_DROP
2244 if (m_genericImpl)
2245 m_genericImpl->SetDropTarget( dropTarget );
2246
2247 if (m_dbImpl)
2248 wxWindow::SetDropTarget( dropTarget );
2249 #endif
2250 }
2251
2252 wxDropTarget *wxListCtrl::GetDropTarget() const
2253 {
2254 #if wxUSE_DRAG_AND_DROP
2255 if (m_genericImpl)
2256 return m_genericImpl->GetDropTarget();
2257
2258 if (m_dbImpl)
2259 return wxWindow::GetDropTarget();
2260 #endif
2261 return NULL;
2262 }
2263
2264 #if wxABI_VERSION >= 20801
2265 void wxListCtrl::SetFocus()
2266 {
2267 if (m_genericImpl)
2268 {
2269 m_genericImpl->SetFocus();
2270 return;
2271 }
2272
2273 wxWindow::SetFocus();
2274 }
2275 #endif
2276
2277 // wxMac internal data structures
2278
2279 wxMacListCtrlItem::~wxMacListCtrlItem()
2280 {
2281 }
2282
2283 void wxMacListCtrlItem::Notification(wxMacDataItemBrowserControl *owner ,
2284 DataBrowserItemNotification message,
2285 DataBrowserItemDataRef itemData ) const
2286 {
2287
2288 wxMacDataBrowserListCtrlControl *lb = wxDynamicCast(owner, wxMacDataBrowserListCtrlControl);
2289
2290 // we want to depend on as little as possible to make sure tear-down of controls is safe
2291 if ( message == kDataBrowserItemRemoved)
2292 {
2293 if ( lb != NULL && lb->GetClientDataType() == wxClientData_Object )
2294 {
2295 delete (wxClientData*) (m_data);
2296 }
2297
2298 delete this;
2299 return;
2300 }
2301 else if ( message == kDataBrowserItemAdded )
2302 {
2303 // we don't issue events on adding, the item is not really stored in the list yet, so we
2304 // avoid asserts by gettting out now
2305 return ;
2306 }
2307
2308 wxListCtrl *list = wxDynamicCast( owner->GetPeer() , wxListCtrl );
2309 if ( list && lb )
2310 {
2311 bool trigger = false;
2312
2313 wxListEvent event( wxEVT_COMMAND_LIST_ITEM_SELECTED, list->GetId() );
2314 bool isSingle = (list->GetWindowStyle() & wxLC_SINGLE_SEL) != 0;
2315
2316 event.SetEventObject( list );
2317 event.m_itemIndex = owner->GetLineFromItem( this ) ;
2318 if ( !list->IsVirtual() )
2319 {
2320 lb->MacGetColumnInfo(event.m_itemIndex,0,event.m_item);
2321 }
2322
2323 switch (message)
2324 {
2325 case kDataBrowserItemDeselected:
2326 event.SetEventType(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
2327 if ( !isSingle )
2328 trigger = !lb->IsSelectionSuppressed();
2329 break;
2330
2331 case kDataBrowserItemSelected:
2332 trigger = !lb->IsSelectionSuppressed();
2333 break;
2334
2335 case kDataBrowserItemDoubleClicked:
2336 event.SetEventType( wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2337 trigger = true;
2338 break;
2339
2340 case kDataBrowserEditStarted :
2341 // TODO : how to veto ?
2342 event.SetEventType( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT ) ;
2343 trigger = true ;
2344 break ;
2345
2346 case kDataBrowserEditStopped :
2347 // TODO probably trigger only upon the value store callback, because
2348 // here IIRC we cannot veto
2349 event.SetEventType( wxEVT_COMMAND_LIST_END_LABEL_EDIT ) ;
2350 trigger = true ;
2351 break ;
2352
2353 default:
2354 break;
2355 }
2356
2357 if ( trigger )
2358 {
2359 // direct notification is not always having the listbox GetSelection() having in synch with event
2360 wxPostEvent( list->GetEventHandler(), event );
2361 }
2362 }
2363
2364 }
2365
2366 IMPLEMENT_DYNAMIC_CLASS(wxMacDataBrowserListCtrlControl, wxMacDataItemBrowserControl )
2367
2368 wxMacDataBrowserListCtrlControl::wxMacDataBrowserListCtrlControl( wxWindow *peer, const wxPoint& pos, const wxSize& size, long style)
2369 : wxMacDataItemBrowserControl( peer, pos, size, style )
2370 {
2371 OSStatus err = noErr;
2372 m_clientDataItemsType = wxClientData_None;
2373 m_isVirtual = false;
2374 m_flags = 0;
2375
2376 if ( style & wxLC_VIRTUAL )
2377 m_isVirtual = true;
2378
2379 DataBrowserSelectionFlags options = kDataBrowserDragSelect;
2380 if ( style & wxLC_SINGLE_SEL )
2381 {
2382 options |= kDataBrowserSelectOnlyOne;
2383 }
2384 else
2385 {
2386 options |= kDataBrowserCmdTogglesSelection;
2387 }
2388
2389 err = SetSelectionFlags( options );
2390 verify_noerr( err );
2391
2392 DataBrowserCustomCallbacks callbacks;
2393 InitializeDataBrowserCustomCallbacks( &callbacks, kDataBrowserLatestCustomCallbacks );
2394
2395 if ( gDataBrowserDrawItemUPP == NULL )
2396 gDataBrowserDrawItemUPP = NewDataBrowserDrawItemUPP(DataBrowserDrawItemProc);
2397
2398 if ( gDataBrowserHitTestUPP == NULL )
2399 gDataBrowserHitTestUPP = NewDataBrowserHitTestUPP(DataBrowserHitTestProc);
2400
2401 callbacks.u.v1.drawItemCallback = gDataBrowserDrawItemUPP;
2402 callbacks.u.v1.hitTestCallback = gDataBrowserHitTestUPP;
2403
2404 SetDataBrowserCustomCallbacks( GetControlRef(), &callbacks );
2405
2406 if ( style & wxLC_LIST )
2407 {
2408 InsertColumn(0, kDataBrowserIconAndTextType, wxEmptyString, -1, -1);
2409 verify_noerr( AutoSizeColumns() );
2410 }
2411
2412 if ( style & wxLC_LIST || style & wxLC_NO_HEADER )
2413 verify_noerr( SetHeaderButtonHeight( 0 ) );
2414
2415 if ( m_isVirtual )
2416 SetSortProperty( kMinColumnId - 1 );
2417 else
2418 SetSortProperty( kMinColumnId );
2419
2420 m_sortOrder = SortOrder_None;
2421
2422 if ( style & wxLC_SORT_DESCENDING )
2423 {
2424 SetSortOrder( kDataBrowserOrderDecreasing );
2425 }
2426 else if ( style & wxLC_SORT_ASCENDING )
2427 {
2428 SetSortOrder( kDataBrowserOrderIncreasing );
2429 }
2430
2431 if ( style & wxLC_VRULES )
2432 {
2433 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
2434 verify_noerr( DataBrowserChangeAttributes(m_controlRef, kDataBrowserAttributeListViewDrawColumnDividers, kDataBrowserAttributeNone) );
2435 #endif
2436 }
2437
2438 verify_noerr( SetHiliteStyle(kDataBrowserTableViewFillHilite ) );
2439 verify_noerr( SetHasScrollBars( (style & wxHSCROLL) != 0 , true ) );
2440 }
2441
2442 pascal Boolean wxMacDataBrowserListCtrlControl::DataBrowserEditTextProc(
2443 ControlRef browser,
2444 DataBrowserItemID itemID,
2445 DataBrowserPropertyID property,
2446 CFStringRef theString,
2447 Rect *maxEditTextRect,
2448 Boolean *shrinkToFit)
2449 {
2450 Boolean result = false;
2451 wxMacDataBrowserListCtrlControl* ctl = wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser ), wxMacDataBrowserListCtrlControl);
2452 if ( ctl != 0 )
2453 {
2454 result = ctl->ConfirmEditText(itemID, property, theString, maxEditTextRect, shrinkToFit);
2455 theString = CFSTR("Hello!");
2456 }
2457 return result;
2458 }
2459
2460 bool wxMacDataBrowserListCtrlControl::ConfirmEditText(
2461 DataBrowserItemID itemID,
2462 DataBrowserPropertyID property,
2463 CFStringRef theString,
2464 Rect *maxEditTextRect,
2465 Boolean *shrinkToFit)
2466 {
2467 return false;
2468 }
2469
2470 pascal void wxMacDataBrowserListCtrlControl::DataBrowserDrawItemProc(
2471 ControlRef browser,
2472 DataBrowserItemID itemID,
2473 DataBrowserPropertyID property,
2474 DataBrowserItemState itemState,
2475 const Rect *itemRect,
2476 SInt16 gdDepth,
2477 Boolean colorDevice)
2478 {
2479 wxMacDataBrowserListCtrlControl* ctl = wxDynamicCast(wxMacControl::GetReferenceFromNativeControl( browser ), wxMacDataBrowserListCtrlControl);
2480 if ( ctl != 0 )
2481 {
2482 ctl->DrawItem(itemID, property, itemState, itemRect, gdDepth, colorDevice);
2483 }
2484 }
2485
2486 // routines needed for DrawItem
2487 enum
2488 {
2489 kIconWidth = 16,
2490 kIconHeight = 16,
2491 kTextBoxHeight = 14,
2492 kIconTextSpacingV = 2,
2493 kItemPadding = 4,
2494 kContentHeight = kIconHeight + kTextBoxHeight + kIconTextSpacingV
2495 };
2496
2497 static void calculateCGDrawingBounds(CGRect inItemRect, CGRect *outIconRect, CGRect *outTextRect, bool hasIcon = false)
2498 {
2499 float textBottom;
2500 float iconH, iconW = 0;
2501 float padding = kItemPadding;
2502 if (hasIcon)
2503 {
2504 iconH = kIconHeight;
2505 iconW = kIconWidth;
2506 padding = padding*2;
2507 }
2508
2509 textBottom = inItemRect.origin.y;
2510
2511 *outIconRect = CGRectMake(inItemRect.origin.x + kItemPadding,
2512 textBottom + kIconTextSpacingV, kIconWidth,
2513 kIconHeight);
2514
2515 *outTextRect = CGRectMake(inItemRect.origin.x + padding + iconW,
2516 textBottom + kIconTextSpacingV, inItemRect.size.width - padding - iconW,
2517 inItemRect.size.height - kIconTextSpacingV);
2518 }
2519
2520 void wxMacDataBrowserListCtrlControl::DrawItem(
2521 DataBrowserItemID itemID,
2522 DataBrowserPropertyID property,
2523 DataBrowserItemState itemState,
2524 const Rect *itemRect,
2525 SInt16 gdDepth,
2526 Boolean colorDevice)
2527 {
2528 wxString text;
2529 wxFont font = wxNullFont;
2530 int imgIndex = -1;
2531 short listColumn = property - kMinColumnId;
2532
2533 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
2534 wxMacListCtrlItem* lcItem;
2535 wxColour color = *wxBLACK;
2536 wxColour bgColor = wxNullColour;
2537
2538 if (listColumn >= 0)
2539 {
2540 if (!m_isVirtual)
2541 {
2542 lcItem = (wxMacListCtrlItem*) itemID;
2543 if (lcItem->HasColumnInfo(listColumn)){
2544 wxListItem* item = lcItem->GetColumnInfo(listColumn);
2545
2546 // we always use the 0 column to get font and text/background colors.
2547 if (lcItem->HasColumnInfo(0))
2548 {
2549 wxListItem* firstItem = lcItem->GetColumnInfo(0);
2550 color = firstItem->GetTextColour();
2551 bgColor = firstItem->GetBackgroundColour();
2552 font = firstItem->GetFont();
2553 }
2554
2555 if (item->GetMask() & wxLIST_MASK_TEXT)
2556 text = item->GetText();
2557 if (item->GetMask() & wxLIST_MASK_IMAGE)
2558 imgIndex = item->GetImage();
2559 }
2560
2561 }
2562 else
2563 {
2564 text = list->OnGetItemText( (long)itemID-1, listColumn );
2565 imgIndex = list->OnGetItemColumnImage( (long)itemID-1, listColumn );
2566 wxListItemAttr* attrs = list->OnGetItemAttr( (long)itemID-1 );
2567 if (attrs)
2568 {
2569 if (attrs->HasBackgroundColour())
2570 bgColor = attrs->GetBackgroundColour();
2571 if (attrs->HasTextColour())
2572 color = attrs->GetTextColour();
2573 if (attrs->HasFont())
2574 font = attrs->GetFont();
2575 }
2576 }
2577 }
2578
2579 wxColour listBgColor = list->GetBackgroundColour();
2580 if (bgColor == wxNullColour)
2581 bgColor = listBgColor;
2582
2583 wxFont listFont = list->GetFont();
2584 if (font == wxNullFont)
2585 font = listFont;
2586
2587 wxMacCFStringHolder cfString;
2588 cfString.Assign( text, wxLocale::GetSystemEncoding() );
2589
2590 Rect enclosingRect;
2591 CGRect enclosingCGRect, iconCGRect, textCGRect;
2592 Boolean active;
2593 ThemeDrawingState savedState = NULL;
2594 CGContextRef context = (CGContextRef)list->MacGetDrawingContext();
2595 RGBColor labelColor;
2596 labelColor.red = 0;
2597 labelColor.green = 0;
2598 labelColor.blue = 0;
2599
2600 RGBColor backgroundColor;
2601 backgroundColor.red = 255;
2602 backgroundColor.green = 255;
2603 backgroundColor.blue = 255;
2604
2605 GetDataBrowserItemPartBounds(GetControlRef(), itemID, property, kDataBrowserPropertyEnclosingPart,
2606 &enclosingRect);
2607
2608 enclosingCGRect = CGRectMake(enclosingRect.left,
2609 enclosingRect.top,
2610 enclosingRect.right - enclosingRect.left,
2611 enclosingRect.bottom - enclosingRect.top);
2612
2613 active = IsControlActive(GetControlRef());
2614
2615 // don't paint the background over the vertical rule line
2616 if ( list->GetWindowStyleFlag() & wxLC_VRULES )
2617 {
2618 enclosingCGRect.origin.x += 1;
2619 enclosingCGRect.size.width -= 1;
2620 }
2621 if (itemState == kDataBrowserItemIsSelected)
2622 {
2623
2624 GetThemeDrawingState(&savedState);
2625
2626 GetThemeBrushAsColor(kThemeBrushAlternatePrimaryHighlightColor, 32, true, &backgroundColor);
2627 GetThemeTextColor(kThemeTextColorWhite, gdDepth, colorDevice, &labelColor);
2628
2629 CGContextSaveGState(context);
2630
2631 CGContextSetRGBFillColor(context, (float)backgroundColor.red / (float)USHRT_MAX,
2632 (float)backgroundColor.green / (float)USHRT_MAX,
2633 (float)backgroundColor.blue / (float)USHRT_MAX, 1.0);
2634 CGContextFillRect(context, enclosingCGRect);
2635
2636 CGContextRestoreGState(context);
2637 }
2638 else
2639 {
2640
2641 if (color.Ok())
2642 labelColor = MAC_WXCOLORREF( color.GetPixel() );
2643 else if (list->GetTextColour().Ok())
2644 labelColor = MAC_WXCOLORREF( list->GetTextColour().GetPixel() );
2645
2646 if (bgColor.Ok())
2647 {
2648 backgroundColor = MAC_WXCOLORREF( bgColor.GetPixel() );
2649 CGContextSaveGState(context);
2650
2651 CGContextSetRGBFillColor(context, (float)backgroundColor.red / (float)USHRT_MAX,
2652 (float)backgroundColor.green / (float)USHRT_MAX,
2653 (float)backgroundColor.blue / (float)USHRT_MAX, 1.0);
2654 CGContextFillRect(context, enclosingCGRect);
2655
2656 CGContextRestoreGState(context);
2657 }
2658 }
2659
2660 calculateCGDrawingBounds(enclosingCGRect, &iconCGRect, &textCGRect, (imgIndex != -1) );
2661
2662 if (imgIndex != -1)
2663 {
2664 wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL);
2665 if (imageList && imageList->GetImageCount() > 0){
2666 wxBitmap bmp = imageList->GetBitmap(imgIndex);
2667 IconRef icon = bmp.GetBitmapData()->GetIconRef();
2668
2669 CGContextSaveGState(context);
2670 CGContextTranslateCTM(context, 0,iconCGRect.origin.y + CGRectGetMaxY(iconCGRect));
2671 CGContextScaleCTM(context,1.0f,-1.0f);
2672 PlotIconRefInContext(context, &iconCGRect, kAlignNone,
2673 active ? kTransformNone : kTransformDisabled, NULL,
2674 kPlotIconRefNormalFlags, icon);
2675
2676 CGContextRestoreGState(context);
2677 }
2678 }
2679
2680 HIThemeTextHorizontalFlush hFlush = kHIThemeTextHorizontalFlushLeft;
2681 UInt16 fontID = kThemeViewsFont;
2682
2683 if (font.Ok())
2684 {
2685 if (font.GetFamily() != wxFONTFAMILY_DEFAULT)
2686 fontID = font.MacGetThemeFontID();
2687
2688 // FIXME: replace these with CG or ATSUI calls so we can remove this #ifndef.
2689 #ifndef __LP64__
2690 ::TextSize( (short)(font.MacGetFontSize()) ) ;
2691 ::TextFace( font.MacGetFontStyle() ) ;
2692 #endif
2693 }
2694
2695 wxListItem item;
2696 list->GetColumn(listColumn, item);
2697 if (item.GetMask() & wxLIST_MASK_FORMAT)
2698 {
2699 if (item.GetAlign() == wxLIST_FORMAT_LEFT)
2700 hFlush = kHIThemeTextHorizontalFlushLeft;
2701 else if (item.GetAlign() == wxLIST_FORMAT_CENTER)
2702 hFlush = kHIThemeTextHorizontalFlushCenter;
2703 else if (item.GetAlign() == wxLIST_FORMAT_RIGHT)
2704 {
2705 hFlush = kHIThemeTextHorizontalFlushRight;
2706 textCGRect.origin.x -= kItemPadding; // give a little extra paddding
2707 }
2708 }
2709
2710 HIThemeTextInfo info;
2711 info.version = kHIThemeTextInfoVersionZero;
2712 info.state = active ? kThemeStateActive : kThemeStateInactive;
2713 info.fontID = fontID;
2714 info.horizontalFlushness = hFlush;
2715 info.verticalFlushness = kHIThemeTextVerticalFlushCenter;
2716 info.options = kHIThemeTextBoxOptionNone;
2717 info.truncationPosition = kHIThemeTextTruncationEnd;
2718 info.truncationMaxLines = 1;
2719
2720 CGContextSaveGState(context);
2721 CGContextSetRGBFillColor (context, (float)labelColor.red / (float)USHRT_MAX,
2722 (float)labelColor.green / (float)USHRT_MAX,
2723 (float)labelColor.blue / (float)USHRT_MAX, 1.0);
2724
2725 HIThemeDrawTextBox(cfString, &textCGRect, &info, context, kHIThemeOrientationNormal);
2726
2727 CGContextRestoreGState(context);
2728
2729 if (savedState != NULL)
2730 SetThemeDrawingState(savedState, true);
2731 }
2732
2733 OSStatus wxMacDataBrowserListCtrlControl::GetSetItemData(DataBrowserItemID itemID,
2734 DataBrowserPropertyID property,
2735 DataBrowserItemDataRef itemData,
2736 Boolean changeValue )
2737 {
2738 wxString text;
2739 int imgIndex = -1;
2740 short listColumn = property - kMinColumnId;
2741
2742 OSStatus err = errDataBrowserPropertyNotSupported;
2743 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
2744 wxMacListCtrlItem* lcItem;
2745
2746 if (listColumn >= 0)
2747 {
2748 if (!m_isVirtual)
2749 {
2750 lcItem = (wxMacListCtrlItem*) itemID;
2751 if (lcItem && lcItem->HasColumnInfo(listColumn)){
2752 wxListItem* item = lcItem->GetColumnInfo(listColumn);
2753 if (item->GetMask() & wxLIST_MASK_TEXT)
2754 text = item->GetText();
2755 if (item->GetMask() & wxLIST_MASK_IMAGE)
2756 imgIndex = item->GetImage();
2757 }
2758 }
2759 else
2760 {
2761 text = list->OnGetItemText( (long)itemID-1, listColumn );
2762 imgIndex = list->OnGetItemColumnImage( (long)itemID-1, listColumn );
2763 }
2764 }
2765
2766 if ( !changeValue )
2767 {
2768 switch (property)
2769 {
2770 case kDataBrowserItemIsEditableProperty :
2771 if ( list && list->HasFlag( wxLC_EDIT_LABELS ) )
2772 {
2773 verify_noerr(SetDataBrowserItemDataBooleanValue( itemData, true ));
2774 err = noErr ;
2775 }
2776 break ;
2777 default :
2778 if ( property >= kMinColumnId )
2779 {
2780 wxMacCFStringHolder cfStr;
2781
2782 if (text){
2783 cfStr.Assign( text, wxLocale::GetSystemEncoding() );
2784 err = ::SetDataBrowserItemDataText( itemData, cfStr );
2785 err = noErr;
2786 }
2787
2788
2789
2790 if ( imgIndex != -1 )
2791 {
2792 wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL);
2793 if (imageList && imageList->GetImageCount() > 0){
2794 wxBitmap bmp = imageList->GetBitmap(imgIndex);
2795 IconRef icon = bmp.GetBitmapData()->GetIconRef();
2796 ::SetDataBrowserItemDataIcon(itemData, icon);
2797 }
2798 }
2799
2800 }
2801 break ;
2802 }
2803
2804 }
2805 else
2806 {
2807 switch (property)
2808 {
2809 default:
2810 if ( property >= kMinColumnId )
2811 {
2812 short listColumn = property - kMinColumnId;
2813
2814 // TODO probably send the 'end edit' from here, as we
2815 // can then deal with the veto
2816 CFStringRef sr ;
2817 verify_noerr( GetDataBrowserItemDataText( itemData , &sr ) ) ;
2818 wxMacCFStringHolder cfStr(sr) ;;
2819 if (m_isVirtual)
2820 list->SetItem( (long)itemData-1 , listColumn, cfStr.AsString() ) ;
2821 else
2822 {
2823 if (lcItem)
2824 lcItem->SetColumnTextValue( listColumn, cfStr.AsString() );
2825 }
2826 err = noErr ;
2827 }
2828 break;
2829 }
2830 }
2831 return err;
2832 }
2833
2834 void wxMacDataBrowserListCtrlControl::ItemNotification(DataBrowserItemID itemID,
2835 DataBrowserItemNotification message,
2836 DataBrowserItemDataRef itemData )
2837 {
2838 // we want to depend on as little as possible to make sure tear-down of controls is safe
2839 if ( message == kDataBrowserItemRemoved)
2840 {
2841 // make sure MacDelete does the proper teardown.
2842 return;
2843 }
2844 else if ( message == kDataBrowserItemAdded )
2845 {
2846 // we don't issue events on adding, the item is not really stored in the list yet, so we
2847 // avoid asserts by getting out now
2848 return ;
2849 }
2850
2851 wxListCtrl *list = wxDynamicCast( GetPeer() , wxListCtrl );
2852 if ( list )
2853 {
2854 bool trigger = false;
2855
2856 wxListEvent event( wxEVT_COMMAND_LIST_ITEM_SELECTED, list->GetId() );
2857 bool isSingle = (list->GetWindowStyle() & wxLC_SINGLE_SEL) != 0;
2858
2859 event.SetEventObject( list );
2860 if ( !list->IsVirtual() )
2861 {
2862 DataBrowserTableViewRowIndex result = 0;
2863 verify_noerr( GetItemRow( itemID, &result ) ) ;
2864 event.m_itemIndex = result;
2865
2866 if (event.m_itemIndex >= 0)
2867 MacGetColumnInfo(event.m_itemIndex,0,event.m_item);
2868 }
2869 else
2870 {
2871 event.m_itemIndex = (long)itemID-1;
2872 }
2873
2874 switch (message)
2875 {
2876 case kDataBrowserItemDeselected:
2877 event.SetEventType(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
2878 if ( !isSingle )
2879 trigger = !IsSelectionSuppressed();
2880 break;
2881
2882 case kDataBrowserItemSelected:
2883 trigger = !IsSelectionSuppressed();
2884
2885 break;
2886
2887 case kDataBrowserItemDoubleClicked:
2888 event.SetEventType( wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2889 trigger = true;
2890 break;
2891
2892 case kDataBrowserEditStarted :
2893 // TODO : how to veto ?
2894 event.SetEventType( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT ) ;
2895 trigger = true ;
2896 break ;
2897
2898 case kDataBrowserEditStopped :
2899 // TODO probably trigger only upon the value store callback, because
2900 // here IIRC we cannot veto
2901 event.SetEventType( wxEVT_COMMAND_LIST_END_LABEL_EDIT ) ;
2902 trigger = true ;
2903 break ;
2904
2905 default:
2906 break;
2907 }
2908
2909 if ( trigger )
2910 {
2911 // direct notification is not always having the listbox GetSelection() having in synch with event
2912 wxPostEvent( list->GetEventHandler(), event );
2913 }
2914 }
2915 }
2916
2917 Boolean wxMacDataBrowserListCtrlControl::CompareItems(DataBrowserItemID itemOneID,
2918 DataBrowserItemID itemTwoID,
2919 DataBrowserPropertyID sortProperty)
2920 {
2921
2922 bool retval = false;
2923 wxString itemText;
2924 wxString otherItemText;
2925 long itemOrder;
2926 long otherItemOrder;
2927
2928 int colId = sortProperty - kMinColumnId;
2929
2930 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
2931
2932 DataBrowserSortOrder sort;
2933 verify_noerr(GetSortOrder(&sort));
2934
2935 if (colId >= 0)
2936 {
2937 if (!m_isVirtual)
2938 {
2939 wxMacListCtrlItem* item = (wxMacListCtrlItem*)itemOneID;
2940 wxMacListCtrlItem* otherItem = (wxMacListCtrlItem*)itemTwoID;
2941
2942 itemOrder = item->GetOrder();
2943 otherItemOrder = item->GetOrder();
2944
2945 wxListCtrlCompare func = list->GetCompareFunc();
2946 if (func != NULL)
2947 {
2948 long item1 = -1;
2949 long item2 = -1;
2950 if (item && item->HasColumnInfo(0))
2951 item1 = item->GetColumnInfo(0)->GetData();
2952 if (otherItem && otherItem->HasColumnInfo(0))
2953 item2 = otherItem->GetColumnInfo(0)->GetData();
2954
2955 if (item1 > -1 && item2 > -1)
2956 {
2957 int result = func(item1, item2, list->GetCompareFuncData());
2958 if (sort == kDataBrowserOrderIncreasing)
2959 return result >= 0;
2960 else
2961 return result < 0;
2962 }
2963 }
2964
2965 // we can't use the native control's sorting abilities, so just
2966 // sort by item id.
2967 return itemOrder < otherItemOrder;
2968 }
2969 else
2970 {
2971
2972 long itemNum = (long)itemOneID;
2973 long otherItemNum = (long)itemTwoID;
2974 itemText = list->OnGetItemText( itemNum-1, colId );
2975 otherItemText = list->OnGetItemText( otherItemNum-1, colId );
2976
2977 // virtual listctrls don't support sorting
2978 return itemNum < otherItemNum;
2979 }
2980 }
2981 else{
2982 // fallback for undefined cases
2983 retval = itemOneID < itemTwoID;
2984 }
2985
2986 return retval;
2987 }
2988
2989 wxMacDataBrowserListCtrlControl::~wxMacDataBrowserListCtrlControl()
2990 {
2991 }
2992
2993 void wxMacDataBrowserListCtrlControl::MacSetColumnInfo( unsigned int row, unsigned int column, wxListItem* item )
2994 {
2995 wxMacDataItem* dataItem = GetItemFromLine(row);
2996 wxASSERT_MSG( dataItem, _T("could not obtain wxMacDataItem for row in MacSetColumnInfo. Is row a valid wxListCtrl row?") );
2997 if (item)
2998 {
2999 wxMacListCtrlItem* listItem = wx_static_cast(wxMacListCtrlItem*,dataItem);
3000 bool hasInfo = listItem->HasColumnInfo( column );
3001 listItem->SetColumnInfo( column, item );
3002 listItem->SetOrder(row);
3003 UpdateState(dataItem, item);
3004
3005 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
3006
3007 // NB: When this call was made before a control was completely shown, it would
3008 // update the item prematurely (i.e. no text would be listed) and, on show,
3009 // only the sorted column would be refreshed, meaning only first column text labels
3010 // would be shown. Making sure not to update items until the control is visible
3011 // seems to fix this issue.
3012 if (hasInfo && list->IsShown())
3013 UpdateItem( wxMacDataBrowserRootContainer, listItem , kMinColumnId + column );
3014 }
3015 }
3016
3017 // apply changes that need to happen immediately, rather than when the
3018 // databrowser control fires a callback.
3019 void wxMacDataBrowserListCtrlControl::UpdateState(wxMacDataItem* dataItem, wxListItem* listItem)
3020 {
3021 bool isSelected = IsItemSelected( dataItem );
3022 bool isSelectedState = (listItem->GetState() == wxLIST_STATE_SELECTED);
3023
3024 // toggle the selection state if wxListInfo state and actual state don't match.
3025 if ( isSelected != isSelectedState )
3026 {
3027 DataBrowserSetOption options = kDataBrowserItemsAdd;
3028 if (!isSelectedState)
3029 options = kDataBrowserItemsRemove;
3030 SetSelectedItem(dataItem, options);
3031 }
3032 // TODO: Set column width if item width > than current column width
3033 }
3034
3035 void wxMacDataBrowserListCtrlControl::MacGetColumnInfo( unsigned int row, unsigned int column, wxListItem& item )
3036 {
3037 wxMacDataItem* dataItem = GetItemFromLine(row);
3038 wxASSERT_MSG( dataItem, _T("could not obtain wxMacDataItem in MacGetColumnInfo. Is row a valid wxListCtrl row?") );
3039 // CS should this guard against dataItem = 0 ? , as item is not a pointer if (item) is not appropriate
3040 //if (item)
3041 {
3042 wxMacListCtrlItem* listItem =wx_static_cast(wxMacListCtrlItem*,dataItem);
3043
3044 if (!listItem->HasColumnInfo( column ))
3045 return;
3046
3047 wxListItem* oldItem = listItem->GetColumnInfo( column );
3048
3049 if (oldItem)
3050 {
3051 long mask = item.GetMask();
3052 if ( !mask )
3053 // by default, get everything for backwards compatibility
3054 mask = -1;
3055
3056 if ( mask & wxLIST_MASK_TEXT )
3057 item.SetText(oldItem->GetText());
3058 if ( mask & wxLIST_MASK_IMAGE )
3059 item.SetImage(oldItem->GetImage());
3060 if ( mask & wxLIST_MASK_DATA )
3061 item.SetData(oldItem->GetData());
3062 if ( mask & wxLIST_MASK_STATE )
3063 item.SetState(oldItem->GetState());
3064 if ( mask & wxLIST_MASK_WIDTH )
3065 item.SetWidth(oldItem->GetWidth());
3066 if ( mask & wxLIST_MASK_FORMAT )
3067 item.SetAlign(oldItem->GetAlign());
3068
3069 item.SetTextColour(oldItem->GetTextColour());
3070 item.SetBackgroundColour(oldItem->GetBackgroundColour());
3071 item.SetFont(oldItem->GetFont());
3072 }
3073 }
3074 }
3075
3076 void wxMacDataBrowserListCtrlControl::MacInsertItem( unsigned int n, wxListItem* item )
3077 {
3078 wxMacDataItemBrowserControl::MacInsert(n, item->GetText());
3079 MacSetColumnInfo(n, 0, item);
3080 }
3081
3082 wxMacDataItem* wxMacDataBrowserListCtrlControl::CreateItem()
3083 {
3084 return new wxMacListCtrlItem();
3085 }
3086
3087 wxMacListCtrlItem::wxMacListCtrlItem()
3088 {
3089 m_rowItems = wxListItemList();
3090 }
3091
3092 int wxMacListCtrlItem::GetColumnImageValue( unsigned int column )
3093 {
3094 if ( HasColumnInfo(column) )
3095 return GetColumnInfo(column)->GetImage();
3096
3097 return -1;
3098 }
3099
3100 void wxMacListCtrlItem::SetColumnImageValue( unsigned int column, int imageIndex )
3101 {
3102 if ( HasColumnInfo(column) )
3103 GetColumnInfo(column)->SetImage(imageIndex);
3104 }
3105
3106 wxString wxMacListCtrlItem::GetColumnTextValue( unsigned int column )
3107 {
3108 if ( column == 0 )
3109 return GetLabel();
3110
3111 if ( HasColumnInfo(column) )
3112 return GetColumnInfo(column)->GetText();
3113
3114 return wxEmptyString;
3115 }
3116
3117 void wxMacListCtrlItem::SetColumnTextValue( unsigned int column, const wxString& text )
3118 {
3119 if ( HasColumnInfo(column) )
3120 GetColumnInfo(column)->SetText(text);
3121
3122 // for compatibility with superclass APIs
3123 if ( column == 0 )
3124 SetLabel(text);
3125 }
3126
3127 wxListItem* wxMacListCtrlItem::GetColumnInfo( unsigned int column )
3128 {
3129 wxASSERT_MSG( HasColumnInfo(column), _T("invalid column index in wxMacListCtrlItem") );
3130 return m_rowItems[column];
3131 }
3132
3133 bool wxMacListCtrlItem::HasColumnInfo( unsigned int column )
3134 {
3135 return !(m_rowItems.find( column ) == m_rowItems.end());
3136 }
3137
3138 void wxMacListCtrlItem::SetColumnInfo( unsigned int column, wxListItem* item )
3139 {
3140
3141 if ( !HasColumnInfo(column) )
3142 {
3143 wxListItem* listItem = new wxListItem(*item);
3144 m_rowItems[column] = listItem;
3145 }
3146 else
3147 {
3148 wxListItem* listItem = GetColumnInfo( column );
3149 long mask = item->GetMask();
3150 if (mask & wxLIST_MASK_TEXT)
3151 listItem->SetText(item->GetText());
3152 if (mask & wxLIST_MASK_DATA)
3153 listItem->SetData(item->GetData());
3154 if (mask & wxLIST_MASK_IMAGE)
3155 listItem->SetImage(item->GetImage());
3156 if (mask & wxLIST_MASK_STATE)
3157 listItem->SetState(item->GetState());
3158 if (mask & wxLIST_MASK_FORMAT)
3159 listItem->SetAlign(item->GetAlign());
3160 if (mask & wxLIST_MASK_WIDTH)
3161 listItem->SetWidth(item->GetWidth());
3162
3163 if ( item->HasAttributes() )
3164 {
3165 if ( listItem->HasAttributes() )
3166 listItem->GetAttributes()->AssignFrom(*item->GetAttributes());
3167 else
3168 {
3169 listItem->SetTextColour(item->GetTextColour());
3170 listItem->SetBackgroundColour(item->GetBackgroundColour());
3171 listItem->SetFont(item->GetFont());
3172 }
3173 }
3174 }
3175 }
3176
3177 #endif // wxUSE_LISTCTRL