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