function startGUI_4

close all

%% Create figure
screenSize = get(groot, 'ScreenSize');
x_size  = 400;
y_size  = 250;
figureColor	= [0.9 0.9 0.9];

fig = figure('Units', 'pixels', 'Name', 'Example' ,'Position', ...
   [(screenSize(3)-x_size)/2 (screenSize(4)-y_size)/2 x_size y_size], ...
   'Tag', 'figExample', 'Color', figureColor, 'MenuBar','none', ...
   'NumberTitle', 'off');

%% Create panel
panelColor = [0.98 0.98 0.98];

panel1 = uipanel('Units', 'pixels' , 'Position', [90 180 220 60], ...
   'Title', 'Visibility', 'BackgroundColor', panelColor, ...
   'Tag', 'panelVisibility');

%% Create text fields
textColor	= [0.95 0.95 0.95];
textFontSize1	= 9;
textFontSize2	= 10;

uicontrol('Units', 'Normalized', ...
   'Position', [0.1 0.4 0.15 0.075], 'Style', 'text', ...
   'String', 'X-position:', 'BackgroundColor', figureColor, ...
   'FontSize', textFontSize1, ...
   'Tag', 'XLabel');

uicontrol('Units', 'Normalized', ...
   'Position', [0.26 0.4 0.175 0.075], 'Style', 'text', ...
   'String', '', 'BackgroundColor', textColor, ...
   'FontSize', textFontSize2, ...
   'Tag', 'XPos');

uicontrol('Units', 'Normalized', ...
   'Position', [0.55 0.4 0.15 0.075], 'Style', 'text', ...
   'String', 'Y-position:', 'BackgroundColor', figureColor, ...
   'FontSize', textFontSize1, ...
   'Tag', 'YLabel');

uicontrol('Units', 'Normalized', ...
   'Position', [0.71 0.4 0.175 0.075], 'Style', 'text', ...
   'String', '','BackgroundColor', textColor, ...
   'FontSize', textFontSize2, ...
   'Tag', 'YPos');

%% Create pushbutton
hButton = uicontrol('Units', 'Normalized', ...
   'Position', [0.3 0.1 0.4 0.125], ...
   'Style', 'pushbutton', ...
   'String', 'End', ...
   'ForegroundColor', [0.8 0.9 0.9], ...
   'BackgroundColor', [0.1 0.5 0.6], ...
   'FontSize', textFontSize1);
%% Create checkbox
visibleHandle = uicontrol('Parent', panel1, ...
   'Units', 'Normalized', ...
   'Position', [0.225 0.25 0.6 0.5], ...
   'Style', 'checkbox', ...
   'String', 'Show position', ...
   'Tag', 'choiceOfVisibility', ...
   'BackgroundColor', panelColor, ...
   'TooltipString', 'click to show mouse position');

%%
% save references to all graphic objs by guihandles and guidata
allFigChildrens = guihandles(fig);
guidata(fig, allFigChildrens);
% set callbacks
hButton.Callback = @(a, b)closeWindow;
fig.WindowButtonDownFcn = @(a, b)figClick;
visibleHandle.Callback = @(a, b)manageShowingPosition;
end
%%
% Callback for mouse buttons
function figClick
% get all saved data from figure
allHandles = guidata(gcbo);
hTxt1 = allHandles.XLabel;
hTxt2 = allHandles.YLabel;
hFig = allHandles.figExample;

if strcmp(hFig.SelectionType, 'normal') % action for a left mouse button
   hTxt1.FontWeight = 'bold';
   hTxt2.FontWeight = 'bold';
   
elseif strcmp(hFig.SelectionType, 'alt') % action for a right mouse button
   hTxt1.FontWeight = 'normal';
   hTxt2.FontWeight = 'normal';
else
   disp('You pressed another button.');
end
end

function manageShowingPosition
% get all saved data from figure
allHandles = guidata(gcbo);
hCheck = allHandles.choiceOfVisibility;
hFig = allHandles.figExample;
hTextX = allHandles.XPos;
hTextY = allHandles.YPos;

if hCheck.Value
   % set WindowButtonMotionFcn of figure
   hFig.WindowButtonMotionFcn = @(a, b)showCoordinates;
else
   hFig.WindowButtonMotionFcn = ''; % callback is disabled
   hTextX.String = '';
   hTextY.String = '';
end

end

% when feature activated and mouse on the move...
function showCoordinates
% get all saved data from figure
allHandles = guidata(gcbo);
hTextX = allHandles.XPos;
hTextY = allHandles.YPos;
hFig = gcbo; % or hFig = allHandles.figExample;

hTextX.String = hFig.CurrentPoint(1);
hTextY.String = hFig.CurrentPoint(2);
end

% Function which is called when the figure should be closed
function closeWindow
% get all saved data from figure
allHandles = guidata(gcbo);
hFig = allHandles.figExample;

hFig.delete;
end