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