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