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