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