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