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