Treeview control in Win32 API - how to display a single treeview item using different fonts? - winapi

I'm faced with a problem where I need to display some characters in a tree-view item (those belonging to the Symbol charset) using Symbol font while others in the default System font (Segoi UI on my Windows 7).
Custom draw allows us to draw different items using different fonts, but I would like to draw the same item string using different fonts as it applies to each character in the string as told above.
So, what I've done with not-so-pleasing results w.r.t. drawing performance upon a horizontal scroll when the number of items is more so far is this:
I disabled horizontal scrolling in my tree-view control using TVS_NOHSCROLL style (since I'm using my own scroll bar control inside the tree-view window to handle all horizontal scrolling)
I sub-classed the treeview control and in the sub-classed winproc, I handle the horizontal scroll notification and mouse notification (where I do my own hittesting and send message like TVM_EXPAND and TVM_SELECT as a result of mosue clicks/double-clicks). Also the scroll bar range is set based on how wide my custom drawn string is (the maximum length amongst all items).
I draw the string for each item upon receiving CDDS_ITEMPOSTPAINT using my own fonts for each character in the item.
The above approach (I left out some details for the sake of brevity) works BUT there are some problems which makes me post this question here and look for an alternare way:
Problems:
The horizontal scroll bar control I create is hosted "inside" the tree-view control at the bottom of the tree-view window. However, when the number of items goes beyond what the tree-view client area can accommodate vertically, the last visible tree-view item gets obscured by the scroll bar control. This can be solved by not making the scroll bar a child of the tree-view and hosting it outside the tree-view window just below it. But I don't want to do this since the scroll bar should typically be a child window of the tree-view.
This is the major one. Since I draw the items myself at each horizontal scroll, the drawing performance upon horizontal scrolling is very slow and also leads to flicker upon scrolling.
Any ideas will be much appreciated as I've been grappling with this for the last one week without success.
I can also post the relevant code here if you want to see the approach I took but I'm sure there shoould be a better approach to this and there must be some other people who would've faced this problem and solved it in the past.
Thanks in advance.

Custom-draw allows you to draw items however you want. You are not limited to a single font per item. When you receive the NM_CUSTOMDRAW notification, draw whatever you want on the provided HDC for the specified item. You can draw pieces of text in one font, pieces of text in a different font, etc. Be sure to return CDRF_SKIPDEFAULT so the TreeView itself will not try to draw anything on the item.

#Anurag S Sharma: I tried to edit this into Remy's answer. It's incomplete as is, but addresses your comment/concerns and answers this particularly vexing/useful question...
The problem is that ff I return CDRF_SKIPDEFAULT, Windows does not even draw the +/- buttons (expanding/collapsing) nor the indent lines in the control which I do want Windows to draw. – Anurag S Sharma
To retain the lines, buttons, and icons you can use ExcludeClipRect to mask only the text region and instead of returning CDRF_SKIPDEFAULT, return 0 as if you didn't draw anything. This itself would not be necessary if the text of the tree item was empty, except that the margins of the text will always be drawn by the default handler (note that Microsoft's controls do not always respect clipping shapes, but in this case they do.)
To replicate the classic TreeView label style in your custom draw procedure you need to do something like the following:
HTREEITEM item = (HTREEITEM)p->dwItemSpec;
TreeView_GetItemRect(p->hdr.hwndFrom,item,&p->rc,1);
RECT cr, rc = p->rc; GetClientRect(p->hdr.hwndFrom,&cr);
DrawTextW(p->hdc,text,-1,&rc,DT_CALCRECT|DT_NOPREFIX|DT_NOCLIP);
rc.right+=4; rc.bottom+=2; IntersectRect(&rc,&cr,&rc);
ExtTextOutW(p->hdc,rc.left+2,rc.top+1,ETO_CLIPPED|ETO_OPAQUE,&rc,text,wcslen(text),0);

Related

How do you update all the TreeView items full area when the vertical scrollbar shows up or goes away?

I am using NM_CUSTOMDRAW to draw tree view items. My items right justify parts of it by using a right margin based on the RECT area reported available. The issue I have is that when the vertical scrollbar appears or disappears, only the item expanded/collapsed and the area under the scrollbar seems to update (invalidated regions). This causes the repaint requests (with the new width) to not update the area correctly.
For example (using text and a single space as example): You could have something with the scrollbar be right justified text ## where ## is the scrollbar then when the scrollbar goes away you end up with right justified text t instead of right justified text
Is there a good way to fix this?
One thought is if I could catch a message when a scrollbar shows up or goes away, I could just invalidate the window to force a redraw. Is there such a message?
Or is there a way to add to the invalidated region without triggering a redraw loop but would update the full items area?
Another thought is I can just use the full window RECT size and use a right margin large enough that wouldn't be under the scroll area but I'd rather not do that.
Thanks!

How to properly use sizing with Cocoa

ALL,
NSSmallControlSize page has following explanation:
This constant is for controls that cannot be resized in one direction,
such as push buttons, radio buttons, checkboxes, sliders, scroll bars,
pop-up buttons, tabs, and progress indicators. You should use a small
system font with a small control.
Is there any way to set the size for other controls? Namely NSComboBox.
Unfortunately the documentation is not talking about that. It's not even mentioning if it is at all possible.
TIA!
As the page describes, the constant is for controls that cannot be resized in one direction. Combo boxes, like the listed controls, cannot be resized vertically. Therefore it makes sense that the constant would apply to combo boxes.
This can be quickly verified by creating a combo box in Interface Builder, clicking on the Size inspector, and setting its size to Small. Since this works, we can conclude that the small size is compatible with combo boxes.

How to highlight header column on mouse hover?

I have an owner drawn header of a ListView control. If the header is not owner drawn we have a nice hover effect when mouse is on one of the header's columns. After I made header columns owner drawn I have to take care of the hover effect myself.
In order to catch it I monitor WM_MOUSEMOVE message within header window and when I get this message I redraw the column of a header with changed background first and on top of that I draw text and other necessary graphics. There is just one but... The font of a text becomes bold.
You can see it below. First header is mine, and second header is from Windows explorer with hover effect on Date modified column.
Why does the font suddenly becomes bold?
Is this the right way to implement hover effect? Maybe I missed some Header specific notification?
In owner draw mode you have to take care of everything including fonts. Don't assume you know what the current DC font is. If you want a specific font you need to select one into the DC before drawing text.
Well, I solved this mystery :) Even though I still don't know why the font used in DrawThemeText function suddenly changed in WM_MOUSEMOVE notification. My approach still relies on catching WM_MOUSEMOVE event and redrawing the column underneath the mouse pointer. In order not to change the default header font (like in illustration in my question) I used the following code to get and set it:
HFONT displayFont = (HFONT)SendMessage( header, WM_GETFONT, 0, 0 ); // gets default header font
SelectObject(hdc, displayFont); // sets device context to use newly found font
This solution has at least one drawback: I redraw the Header too frequently (each time mouse moves within header column).

How can I get an NSTextField to behave like a HTML text?

I have some static text that is usually only 1 line long that displays in a NSTextField in my xib. In some instances, the text is long enough to warrant 2 lines, and I just want the label to resize vertically to fit it, without giving me scrollers or any thing else. Think of how text on a webpage behaves… that is what I want. I just want the label to grow shrink with different text, and with adjustments to its width. How can I achieve this?
UPDATE
Here is a video of how it currently behaves: http://screencast.com/t/4JYTv7jVG3O
Notice how when the NSTextField is two lines long, there is a big gap underneath the text. This is because the stars and button are aligned at the bottom of the frame, and because I have to have the frame taller to accommodate 2 lines, they stay there. If I can get an answer to this question, I would make the frame shorter for the 1 line text, and make the bottom textfield (with the smaller text) taller to compensate. Can this type of floating layout be done?
One option is to actually use a WebView to display your content. You will then get exactly the behaviour you are expecting, at the cost of a bit of work to manage interaction with the controls.
You would need to set the WebView to display no background, using [webView setDrawsBackground:NO].
You'd also need to construct the content (including the star rating and the button) using HTML/CSS and then use the Objective-C/JavaScript bridge to call back to your app when the button is pressed.
More information on calling Objective-C from JavaScript is here.
You could probably also use an NSTextView and embed the button and star rating as NSTextAttachment objects but this is quite complex, it would be a lot easier to use a WebView.
The only other alternative that I can see is writing a view controller that manages the layout of the controls based on the current size of their container view. You would need to measure the text to do this and one way to do that is to use the excellent NS(Attributed)String+Geometrics category written by Jerry Krinock.

How to paint transparent areas for child controls?

I have a CTabCtrl subclass which I'm trying overriding WM_PAINT to perform custom drawing. The only problem is, when I change the selected tab, I get artifacts left on the dialog where the old paint code hasn't been erased before the new code is painted on top. (The standard tab controls have the selected tab appear 2 pixels bigger than non-selected tabs, so when you change from selected to non-selected, you are left with the previous paint artifacts).
What is the best way of "repainting" this area? I've tried getting the parent control's DC and BitBlt'ing that onto the child's DC, but that doesn't work because the parent DC already contains an image of this control.
Do you override the WM_ERASEBACKGROUND message as well? You should probably do that and erase the area in the control.
You could use DrawThemeParentBackground to draw the background (XP and later) if you don't want to replace all the drawing logic.

Resources