User Tools

Site Tools


en:software:matlab:trepr:dev:gui:position

Positioning GUI Windows Relative to Each Other

It is good practice to give the user control over where to display the main GUI window in the first place, as different screen dimensions and especially different operating systems behave differently. Furthermore, it is good practice to position each subwindow relative to the main GUI, whether in the long run configurable or not.

Position of the Main GUI Window

The position of the main GUI window is handled through means of the INI file. The settings from the INI file are applied by means of the private guiConfigApply routine.

Following are both excerpts from the INI file,

[general]
dx = 20
dy = 40

and the guiConfigApply routine:

function status = guiConfigApply(guiname)
%...
try
%...
    % Switch depending on GUI name - use GUI mfilename therefore
    switch lower(guiname)
        case 'treprgui'
            %...
            % Set position of the main GUI window
            if isfield(ad.configuration,'general')
                guiPosition = get(handle,'Position');
                if isfield(ad.configuration.general,'dx')
                    guiPosition(1) = ad.configuration.general.dx;
                    set(handle,'Position',guiPosition);
                end
                if isfield(ad.configuration.general,'dy')
                    guiPosition(2) = ad.configuration.general.dy;
                    set(handle,'Position',guiPosition);
                end
            end
            %...
    end
    %...
catch
    %...
end

The actual call of guiConfigApply takes place after defining all the GUI elements, as applying involves at least partially setting the status of certain GUI elements, that is impossible to do before the elements are defined.

Position of Subwindows

Every subsequent subwindow should have a statement similar to the following at the very beginning, best directly after the code snipped responsible for the singleton behaviour:

% Try to get main GUI position
mainGUIHandle = trEPRguiGetWindowHandle();
if ishandle(mainGUIHandle)
    mainGUIPosition = get(mainGUIHandle,'Position');
    guiPosition = [mainGUIPosition(1),mainGUIPosition(2)+100,950,550];
else
    guiPosition = [20,170,950,550];
end

The code snipped actually defining the GUI window should then look similar to the following example:

hMainFigure = figure('Tag',mfilename,...
    % ...
    'Position',guiPosition,...
    % ...
    );

Please note that the definition of guiPosition tries to be as robust as possible and should always include a sensible default position in case that no position information of the main GUI can be obtained.

en/software/matlab/trepr/dev/gui/position.txt · Last modified: 2020/09/30 21:35 by 127.0.0.1