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