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