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