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