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