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