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