+pascal Boolean wxMacDataBrowserListCtrlControl::DataBrowserEditTextProc(
+ ControlRef browser,
+ DataBrowserItemID itemID,
+ DataBrowserPropertyID property,
+ CFStringRef theString,
+ Rect *maxEditTextRect,
+ Boolean *shrinkToFit)
+{
+ Boolean result = false;
+ wxMacDataBrowserListCtrlControl* ctl = dynamic_cast<wxMacDataBrowserListCtrlControl*>( wxMacControl::GetReferenceFromNativeControl( browser ) );
+ if ( ctl != 0 )
+ {
+ result = ctl->ConfirmEditText(itemID, property, theString, maxEditTextRect, shrinkToFit);
+ theString = CFSTR("Hello!");
+ }
+ return result;
+}
+
+bool wxMacDataBrowserListCtrlControl::ConfirmEditText(
+ DataBrowserItemID itemID,
+ DataBrowserPropertyID property,
+ CFStringRef theString,
+ Rect *maxEditTextRect,
+ Boolean *shrinkToFit)
+{
+ //wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
+ return false;
+}
+
+pascal void wxMacDataBrowserListCtrlControl::DataBrowserDrawItemProc(
+ ControlRef browser,
+ DataBrowserItemID itemID,
+ DataBrowserPropertyID property,
+ DataBrowserItemState itemState,
+ const Rect *itemRect,
+ SInt16 gdDepth,
+ Boolean colorDevice)
+{
+ wxMacDataBrowserListCtrlControl* ctl = dynamic_cast<wxMacDataBrowserListCtrlControl*>( wxMacControl::GetReferenceFromNativeControl( browser ) );
+ if ( ctl != 0 )
+ {
+ ctl->DrawItem(itemID, property, itemState, itemRect, gdDepth, colorDevice);
+ }
+}
+
+// routines needed for DrawItem
+enum
+{
+ kIconWidth = 16,
+ kIconHeight = 16,
+ kTextBoxHeight = 14,
+ kIconTextSpacingV = 2,
+ kItemPadding = 4,
+ kContentHeight = kIconHeight + kTextBoxHeight + kIconTextSpacingV
+};
+
+static void calculateCGDrawingBounds(CGRect inItemRect, CGRect *outIconRect, CGRect *outTextRect, bool hasIcon = false)
+{
+ float textBottom;
+ float iconH, iconW = 0;
+ float padding = kItemPadding;
+ if (hasIcon)
+ {
+ iconH = kIconHeight;
+ iconW = kIconWidth;
+ padding = padding*2;
+ }
+
+ textBottom = inItemRect.origin.y;
+
+ *outIconRect = CGRectMake(inItemRect.origin.x + kItemPadding,
+ textBottom + kIconTextSpacingV, kIconWidth,
+ kIconHeight);
+
+ *outTextRect = CGRectMake(inItemRect.origin.x + padding + iconW,
+ textBottom + kIconTextSpacingV, inItemRect.size.width - padding - iconW,
+ inItemRect.size.height - kIconTextSpacingV);
+}
+
+void wxMacDataBrowserListCtrlControl::DrawItem(
+ DataBrowserItemID itemID,
+ DataBrowserPropertyID property,
+ DataBrowserItemState itemState,
+ const Rect *itemRect,
+ SInt16 gdDepth,
+ Boolean colorDevice)
+{
+ wxString text;
+ wxFont font = wxNullFont;
+ int imgIndex = -1;
+ short listColumn = property - kMinColumnId;
+
+ wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
+ wxMacListCtrlItem* lcItem;
+ wxColour color = *wxBLACK;
+ wxColour bgColor = wxNullColour;
+
+ if (listColumn >= 0)
+ {
+ if (!m_isVirtual)
+ {
+ lcItem = (wxMacListCtrlItem*) itemID;
+ if (lcItem->HasColumnInfo(listColumn)){
+ wxListItem* item = lcItem->GetColumnInfo(listColumn);
+
+ // we always use the 0 column to get font and text/background colors.
+ if (lcItem->HasColumnInfo(0))
+ {
+ wxListItem* firstItem = lcItem->GetColumnInfo(0);
+ color = firstItem->GetTextColour();
+ bgColor = firstItem->GetBackgroundColour();
+ font = firstItem->GetFont();
+ }
+
+ if (item->GetMask() & wxLIST_MASK_TEXT)
+ text = item->GetText();
+ if (item->GetMask() & wxLIST_MASK_IMAGE)
+ imgIndex = item->GetImage();
+ }
+
+ }
+ else
+ {
+ text = list->OnGetItemText( (long)itemID-1, listColumn );
+ imgIndex = list->OnGetItemColumnImage( (long)itemID-1, listColumn );
+ wxListItemAttr* attrs = list->OnGetItemAttr( (long)itemID-1 );
+ if (attrs)
+ {
+ if (attrs->HasBackgroundColour())
+ bgColor = attrs->GetBackgroundColour();
+ if (attrs->HasTextColour())
+ color = attrs->GetTextColour();
+ if (attrs->HasFont())
+ font = attrs->GetFont();
+ }
+ }
+ }
+
+ wxColour listBgColor = list->GetBackgroundColour();
+ if (bgColor == wxNullColour)
+ bgColor = listBgColor;
+
+ wxFont listFont = list->GetFont();
+ if (font == wxNullFont)
+ font = listFont;
+
+ wxMacCFStringHolder cfString;
+ cfString.Assign( text, wxLocale::GetSystemEncoding() );
+
+ Rect enclosingRect;
+ CGRect enclosingCGRect, iconCGRect, textCGRect;
+ Boolean active;
+ ThemeDrawingState savedState = NULL;
+ CGContextRef context = (CGContextRef)list->MacGetDrawingContext();
+ RGBColor labelColor;
+
+ GetDataBrowserItemPartBounds(GetControlRef(), itemID, property, kDataBrowserPropertyEnclosingPart,
+ &enclosingRect);
+
+ enclosingCGRect = CGRectMake(enclosingRect.left,
+ enclosingRect.top,
+ enclosingRect.right - enclosingRect.left,
+ enclosingRect.bottom - enclosingRect.top);
+
+ active = IsControlActive(GetControlRef());
+
+ if (itemState == kDataBrowserItemIsSelected)
+ {
+ RGBColor foregroundColor;
+
+ GetThemeDrawingState(&savedState);
+
+ GetThemeBrushAsColor(kThemeBrushAlternatePrimaryHighlightColor, 32, true, &foregroundColor);
+ GetThemeTextColor(kThemeTextColorWhite, gdDepth, colorDevice, &labelColor);
+
+ CGContextSaveGState(context);
+
+ CGContextSetRGBFillColor(context, (float)foregroundColor.red / (float)USHRT_MAX,
+ (float)foregroundColor.green / (float)USHRT_MAX,
+ (float)foregroundColor.blue / (float)USHRT_MAX, 1.0);
+ CGContextFillRect(context, enclosingCGRect);
+
+ CGContextRestoreGState(context);
+ }
+ else
+ {
+
+ if (color.Ok())
+ labelColor = MAC_WXCOLORREF( color.GetPixel() );
+ else if (list->GetTextColour().Ok())
+ labelColor = MAC_WXCOLORREF( list->GetTextColour().GetPixel() );
+ else
+ {
+ labelColor.red = 0;
+ labelColor.green = 0;
+ labelColor.blue = 0;
+ }
+
+ if (bgColor.Ok())
+ {
+ RGBColor foregroundColor = MAC_WXCOLORREF( bgColor.GetPixel() );
+ CGContextSaveGState(context);
+
+ CGContextSetRGBFillColor(context, (float)foregroundColor.red / (float)USHRT_MAX,
+ (float)foregroundColor.green / (float)USHRT_MAX,
+ (float)foregroundColor.blue / (float)USHRT_MAX, 1.0);
+ CGContextFillRect(context, enclosingCGRect);
+
+ CGContextRestoreGState(context);
+ }
+ }
+
+ calculateCGDrawingBounds(enclosingCGRect, &iconCGRect, &textCGRect, (imgIndex != -1) );
+
+ if (imgIndex != -1)
+ {
+ wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL);
+ if (imageList && imageList->GetImageCount() > 0){
+ wxBitmap bmp = imageList->GetBitmap(imgIndex);
+ IconRef icon = bmp.GetBitmapData()->GetIconRef();
+
+ RGBColor iconLabel;
+ iconLabel.red = 0;
+ iconLabel.green = 0;
+ iconLabel.blue = 0;
+
+ CGContextSaveGState(context);
+ CGContextTranslateCTM(context, 0,iconCGRect.origin.y + CGRectGetMaxY(iconCGRect));
+ CGContextScaleCTM(context,1.0f,-1.0f);
+ PlotIconRefInContext(context, &iconCGRect, kAlignNone,
+ active ? kTransformNone : kTransformDisabled, &iconLabel,
+ kPlotIconRefNormalFlags, icon);
+
+ CGContextRestoreGState(context);
+ }
+ }
+
+ HIThemeTextHorizontalFlush hFlush = kHIThemeTextHorizontalFlushLeft;
+ UInt16 fontID = kThemeViewsFont;
+
+ if (font.Ok())
+ {
+ if (font.GetFamily() != wxFONTFAMILY_DEFAULT)
+ fontID = font.MacGetThemeFontID();
+
+// FIXME: replace these with CG or ATSUI calls so we can remove this #ifndef.
+#ifndef __LP64__
+ ::TextSize( (short)(font.MacGetFontSize()) ) ;
+ ::TextFace( font.MacGetFontStyle() ) ;
+#endif
+ }
+
+ wxListItem item;
+ list->GetColumn(listColumn, item);
+ if (item.GetMask() & wxLIST_MASK_FORMAT)
+ {
+ if (item.GetAlign() == wxLIST_FORMAT_LEFT)
+ hFlush = kHIThemeTextHorizontalFlushLeft;
+ else if (item.GetAlign() == wxLIST_FORMAT_CENTER)
+ hFlush = kHIThemeTextHorizontalFlushCenter;
+ else if (item.GetAlign() == wxLIST_FORMAT_RIGHT)
+ {
+ hFlush = kHIThemeTextHorizontalFlushRight;
+ textCGRect.origin.x -= kItemPadding; // give a little extra paddding
+ }
+ }
+
+ HIThemeTextInfo info;
+ info.version = kHIThemeTextInfoVersionZero;
+ info.state = active ? kThemeStateActive : kThemeStateInactive;
+ info.fontID = fontID;
+ info.horizontalFlushness = hFlush;
+ info.verticalFlushness = kHIThemeTextVerticalFlushCenter;
+ info.options = kHIThemeTextBoxOptionNone;
+ info.truncationPosition = kHIThemeTextTruncationEnd;
+ info.truncationMaxLines = 1;
+
+ CGContextSaveGState(context);
+ CGContextSetRGBFillColor (context, (float)labelColor.red / (float)USHRT_MAX,
+ (float)labelColor.green / (float)USHRT_MAX,
+ (float)labelColor.blue / (float)USHRT_MAX, 1.0);
+
+ HIThemeDrawTextBox(cfString, &textCGRect, &info, context, kHIThemeOrientationNormal);
+
+ CGContextRestoreGState(context);
+
+ if (savedState != NULL)
+ SetThemeDrawingState(savedState, true);
+}
+
+OSStatus wxMacDataBrowserListCtrlControl::GetSetItemData(DataBrowserItemID itemID,
+ DataBrowserPropertyID property,
+ DataBrowserItemDataRef itemData,
+ Boolean changeValue )
+{
+ wxString text;
+ int imgIndex = -1;
+ short listColumn = property - kMinColumnId;
+
+ OSStatus err = errDataBrowserPropertyNotSupported;
+ wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
+ wxMacListCtrlItem* lcItem;
+
+ if (listColumn >= 0)
+ {
+ if (!m_isVirtual)
+ {
+ lcItem = (wxMacListCtrlItem*) itemID;
+ if (lcItem->HasColumnInfo(listColumn)){
+ wxListItem* item = lcItem->GetColumnInfo(listColumn);
+ if (item->GetMask() & wxLIST_MASK_TEXT)
+ text = item->GetText();
+ if (item->GetMask() & wxLIST_MASK_IMAGE)
+ imgIndex = item->GetImage();
+ }
+ }
+ else
+ {
+ text = list->OnGetItemText( (long)itemID-1, listColumn );
+ imgIndex = list->OnGetItemColumnImage( (long)itemID-1, listColumn );
+ }
+ }
+
+ if ( !changeValue )
+ {
+ switch (property)
+ {
+ case kDataBrowserItemIsEditableProperty :
+ if ( list && list->HasFlag( wxLC_EDIT_LABELS ) )
+ {
+ verify_noerr(SetDataBrowserItemDataBooleanValue( itemData, true ));
+ err = noErr ;
+ }
+ break ;
+ default :
+ if ( property >= kMinColumnId )
+ {
+ wxMacCFStringHolder cfStr;
+
+ if (text){
+ cfStr.Assign( text, wxLocale::GetSystemEncoding() );
+ err = ::SetDataBrowserItemDataText( itemData, cfStr );
+ err = noErr;
+ }
+
+
+
+ if ( imgIndex != -1 )
+ {
+ wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL);
+ if (imageList && imageList->GetImageCount() > 0){
+ wxBitmap bmp = imageList->GetBitmap(imgIndex);
+ IconRef icon = bmp.GetBitmapData()->GetIconRef();
+ ::SetDataBrowserItemDataIcon(itemData, icon);
+ }
+ }
+
+ }
+ break ;
+ }
+
+ }
+ else
+ {
+ switch (property)
+ {
+ default:
+ if ( property >= kMinColumnId )
+ {
+ short listColumn = property - kMinColumnId;
+
+ // TODO probably send the 'end edit' from here, as we
+ // can then deal with the veto
+ CFStringRef sr ;
+ verify_noerr( GetDataBrowserItemDataText( itemData , &sr ) ) ;
+ wxMacCFStringHolder cfStr(sr) ;;
+ if (m_isVirtual)
+ list->SetItem( (long)itemData-1 , listColumn, cfStr.AsString() ) ;
+ else
+ {
+ if (lcItem)
+ lcItem->SetColumnTextValue( listColumn, cfStr.AsString() );
+ }
+ err = noErr ;
+ }
+ break;
+ }
+ }
+ return err;
+}
+
+void wxMacDataBrowserListCtrlControl::ItemNotification(DataBrowserItemID itemID,
+ DataBrowserItemNotification message,
+ DataBrowserItemDataRef itemData )
+{
+ // we want to depend on as little as possible to make sure tear-down of controls is safe
+ if ( message == kDataBrowserItemRemoved)
+ {
+ // make sure MacDelete does the proper teardown.
+ return;
+ }
+ else if ( message == kDataBrowserItemAdded )
+ {
+ // we don't issue events on adding, the item is not really stored in the list yet, so we
+ // avoid asserts by getting out now
+ return ;
+ }
+
+ wxListCtrl *list = wxDynamicCast( GetPeer() , wxListCtrl );
+ if ( list )
+ {
+ bool trigger = false;
+
+ wxListEvent event( wxEVT_COMMAND_LIST_ITEM_SELECTED, list->GetId() );
+ bool isSingle = (list->GetWindowStyle() & wxLC_SINGLE_SEL) != 0;
+
+ event.SetEventObject( list );
+ if ( !list->IsVirtual() )
+ {
+ DataBrowserTableViewRowIndex result = 0;
+ verify_noerr( GetItemRow( itemID, &result ) ) ;
+ event.m_itemIndex = result;
+
+ if (event.m_itemIndex >= 0)
+ MacGetColumnInfo(event.m_itemIndex,0,event.m_item);
+ }
+ else
+ {
+ event.m_itemIndex = (long)itemID;
+ }
+
+ switch (message)
+ {
+ case kDataBrowserItemDeselected:
+ event.SetEventType(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
+ if ( !isSingle )
+ trigger = !IsSelectionSuppressed();
+ break;
+
+ case kDataBrowserItemSelected:
+ trigger = !IsSelectionSuppressed();
+
+ break;
+
+ case kDataBrowserItemDoubleClicked:
+ event.SetEventType( wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
+ trigger = true;
+ break;
+
+ case kDataBrowserEditStarted :
+ // TODO : how to veto ?
+ event.SetEventType( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT ) ;
+ trigger = true ;
+ break ;
+
+ case kDataBrowserEditStopped :
+ // TODO probably trigger only upon the value store callback, because
+ // here IIRC we cannot veto
+ event.SetEventType( wxEVT_COMMAND_LIST_END_LABEL_EDIT ) ;
+ trigger = true ;
+ break ;
+
+ default:
+ break;
+ }
+
+ if ( trigger )
+ {
+ // direct notification is not always having the listbox GetSelection() having in synch with event
+ wxPostEvent( list->GetEventHandler(), event );
+ }
+ }
+}
+
+Boolean wxMacDataBrowserListCtrlControl::CompareItems(DataBrowserItemID itemOneID,
+ DataBrowserItemID itemTwoID,
+ DataBrowserPropertyID sortProperty)
+{
+
+ bool retval = false;
+ wxString itemText;
+ wxString otherItemText;
+ int colId = sortProperty - kMinColumnId;
+ long itemNum = 0;
+ long otherItemNum = 0;
+
+ wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
+
+ // means we need to
+ if (colId >= 0)
+ {
+ if (!m_isVirtual)
+ {
+ wxMacListCtrlItem* item = (wxMacListCtrlItem*)itemOneID;
+ wxMacListCtrlItem* otherItem = (wxMacListCtrlItem*)itemTwoID;
+ wxListCtrlCompare func = list->GetCompareFunc();
+ if (func != NULL && item->HasColumnInfo(colId) && otherItem->HasColumnInfo(colId))
+ return func(item->GetColumnInfo(colId)->GetData(), otherItem->GetColumnInfo(colId)->GetData(), list->GetCompareFuncData()) >= 0;
+
+ itemNum = item->GetOrder();
+ otherItemNum = otherItem->GetOrder();
+ if (item->HasColumnInfo(colId))
+ itemText = item->GetColumnInfo(colId)->GetText();
+ if (otherItem->HasColumnInfo(colId))
+ otherItemText = otherItem->GetColumnInfo(colId)->GetText();
+ }
+ else
+ {
+ itemNum = (long)itemOneID;
+ otherItemNum = (long)itemTwoID;
+ itemText = list->OnGetItemText( itemNum-1, colId );
+ otherItemText = list->OnGetItemText( otherItemNum-1, colId );
+
+ }
+
+ DataBrowserSortOrder sort;
+ verify_noerr(GetSortOrder(&sort));
+
+ if ( sort == kDataBrowserOrderIncreasing )
+ {
+ retval = itemText.CmpNoCase( otherItemText ) > 0;
+ }
+ else if ( sort == kDataBrowserOrderDecreasing )
+ {
+ retval = itemText.CmpNoCase( otherItemText ) < 0;
+ }
+ }
+ else{
+ // fallback for undefined cases
+ retval = itemOneID < itemTwoID;
+ }
+
+ return retval;
+}
+