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