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