I am having some troubling positioning a checkbox in a UI. I have (programmatically) added a number of rows of checkboxes to a figure. On my PC with windows XP (Matlab 2011b) I see that these are alligned nicely. I use a height of 15 pixels (as also indicated in this thread: http://www.mathworks.com/matlabcentral/answers/10123-calculate-the-size-needed-for-a-uicontrol) E.g. 10 rows of checkboxes for a total vertical size of 150 pixels.
However, on another PC with Windows 7 (also 2011b), I see that the checkboxes overlap. They are not fully displayed. Some trial and error indicates that the size of these checkboxes is 18 pixels. Another difference I noted is that the ScreenPixelsPerInch property of the root object is set to 116. On the Windows XP computer it is set to 96. Could that explain the difference? At http://www.mathworks.com/matlabcentral/newsreader/view_thread/303963 someone has encountered the same difference in DPI setting. I can see that chaning the ScreenPixelsPerInch settings changes the font size and button size. The size of the checkbox seems unaffected.
My question: on what is the size of the checkbox control depending? How can I determine how much space I need to fit a number of checkboxes?
Update I'm now starting to wonder if this is something Windows 7 specific. I use the 'outerposition' property to place multiple figures next to each other without overlapping, but also not leaving any space between them. This works fine on Windows XP. However, when looking closely at the figures on my Windows 7 machine, I noticed the borders are larger and overlapping. Does Matlab on Windows 7 draw certain UI elements (e.g. borders, checkboxes) in a different way?
Another Update I think I'm getting closer to a solution. I found that the Windows display setting "text size DPI" has something to do with this. Normally, this settings is set to 100% (96 dpi). This is reflected in the ScreenPixelsPerInch attribute of the root object in Matlab and it gives a checkbox height of 15 pixels. I tried various settings and saw the following:
Windows text size setting - Windows DPI - Matlab DPI - Checkbox size 100 % - 96 - 96 - 15 pixels 125 % - 120 - 116 - 18 pixels 150 % - 144 - 116 - 22 pixels 200 % - 192 - 116 - 22 pixels
As can be seen, for the 125% setting, Matlab scales nicely. For the 150% setting, the checkbox size is scaled properly (1.5 * 15 ~ 22). However, this is not reflected in the ScreenPixelsPerInch attribute of Matlab, which stays at 116.
Thus, I have an (partial) answer: use only 100 and 125 % settings in Windows. Does anybody know how to detect the large windows DPI settings (>125%)? It seems not detectable by looking at the ScreenPixelsPerInch attribute.
To answer my own question:
One can use the following code which uses only Matlab routines:
dpi = get(0, 'ScreenPixelsPerInch');
if dpi >= 116 h = 18; else h = 15; end
For the cases I tested, the ScreenPixelsPerInch property only returns 96 or 116. This does not cover all cases. Therefore, the following solution uses a Java method:
dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
if dpi < 120 h = 15; elseif dpi < 144 h = 18; else h = 22; end
During my testing, I only encountered these 3 checkbox sizes. The range of resolutions tested is between 96 and 288 DPI.
The size of checkboxes and popupmenus is controlled by the operating system. While most of the settings can be determined by the Windows-API function GetSystemMetrics, finding out the exact dimensions is not trivial, see e.g. http://stackoverflow.com/questions/1164868/how-to-get-size-of-check-and-gap-in-check-box. Whenn different OS', fonts and screen resolutions come into play, a 100% exact solution is not possible in my opinion.
Do it the hard and reliable way: Create a checkbox, capture the screen, use the image processing toolbox to find the edges. Finding the top-left and bottom right pixels with a color different from the figures background should even work without the IPT.