From 7fa1afafc263089ddeea201e1bf84f7da6bb1c34 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 15 Dec 2019 19:18:07 +0100 Subject: [PATCH] Re-add support for icons to Mac wxDataViewCheckIconTextRenderer Icons were not supported any longer since the switch to the native implementation in 235e61c311ee1cc3c74a637984ca40b4ed773ae2. Restore them now by using text attachments (thanks to Scott B for the idea). The vertical alignment is still not right, but it's better than nothing. See #17473. --- src/osx/cocoa/dataview.mm | 41 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/osx/cocoa/dataview.mm b/src/osx/cocoa/dataview.mm index 86c6b0468b..6ad23b57a1 100644 --- a/src/osx/cocoa/dataview.mm +++ b/src/osx/cocoa/dataview.mm @@ -3426,7 +3426,46 @@ bool wxDataViewCheckIconTextRenderer::MacRender() break; } [cell setIntValue:nativecbvalue]; - [cell setTitle:wxCFStringRef(checkIconText.GetText()).AsNSString()]; + + const wxCFStringRef textString(checkIconText.GetText()); + + const wxIcon& icon = checkIconText.GetIcon(); + if ( icon.IsOk() ) + { + NSTextAttachmentCell* const attachmentCell = + [[NSTextAttachmentCell alloc] initImageCell: icon.GetNSImage()]; + NSTextAttachment* const attachment = [NSTextAttachment new]; + [attachment setAttachmentCell: attachmentCell]; + + // Note: this string is released by the autorelease pool and must not + // be released manually below. + NSAttributedString* const iconString = + [NSAttributedString attributedStringWithAttachment: attachment]; + + NSAttributedString* const separatorString = + [[NSAttributedString alloc] initWithString: @" "]; + + NSAttributedString* const textAttrString = + [[NSAttributedString alloc] initWithString: textString.AsNSString()]; + + NSMutableAttributedString* const fullString = + [NSMutableAttributedString new]; + [fullString appendAttributedString: iconString]; + [fullString appendAttributedString: separatorString]; + [fullString appendAttributedString: textAttrString]; + + [cell setAttributedTitle: fullString]; + + [fullString release]; + [separatorString release]; + [textAttrString release]; + [attachment release]; + [attachmentCell release]; + } + else + { + [cell setTitle: textString.AsNSString()]; + } return true; }