% Demonstration class for wrappers.
%
% The property String2 of ControlWrapper needs the function findobj:
% http://www.mathworks.fr/matlabcentral/fileexchange/14317
%
% If you don't want download finjobj, replace 'String2' by 'String'.
classdef DemoWrapper < handle
properties
hFig = [];
end
properties( SetObservable )
myString = 'Hello, World!';
myBool = true;
myChoice = 1;
myValue = 0.5;
valueIsOK = true;
end
methods
function this = DemoWrapper()
hf = FigureWrapper( ...
'Color', [1 1 1]*240/255, ...
'Name', { this, 'myString' } );
this.hFig = hf;
MagicListener( hf, 'DeleteEvent', @(H,E)delete( this ), this );
props = { ...
'Parent', hf, ...
'Units', 'normalized' };
% myString
MagicTextWrapper( props{:}, ...
'Position', [ 0.1 9/11 0.2 1/11 ], ...
'String', 'My string' );
ControlWrapper( props{:}, ...
'Style', 'edit', ...
'Position', [ 0.3 9/11 0.2 1/11 ], ...
'String2', { this, 'myString' } ); % <-- Need findobj
% myBool
ControlWrapper( props{:}, ...
'Style', 'checkbox', ...
'Position', [ 0.1 7/11 0.2 1/11 ], ...
'Value', { this, 'myBool', 'double<->bool' }, ...
'String', 'My boolean' );
% myChoice
MagicTextWrapper( props{:}, ...
'Position', [ 0.6 0.75 0.3 0.1 ], ...
'String', { this, 'myChoice', [], @(m)sprintf( 'Choice n%d', m ) }, ...
'HorizontalAlignment', 'center' );
function addComponents( currH, compH, num )
ControlWrapper( props{:}, ...
'Style', 'radio', ...
'Position', [ 0.1 currH 0.2 compH ], ...
'Value', { this, 'myChoice', 'bool<->flagvalue', num }, ...
'String', sprintf( 'Choice %d', num ), ...
'Enable', { this, 'myBool', 'onoff<->bool' } );
ControlWrapper( props{:}, ...
'Style', 'togglebutton', ...
'Position', [ 0.3 currH 0.2 compH ], ...
'Value', { this, 'myChoice', 'bool<->flagvalue', num }, ...
'String', sprintf( 'Choice %d', num ) );
PanelWrapper( props{:}, ...
'Position', [ 0.6 0.25 0.3 0.5 ], ...
'Visible', { this, 'myChoice', 'onoff<->flagvalue', num }, ...
'Title', sprintf( 'Panel n%d', num ) );
end
addComponents( 5/11, 1/11, 1 )
addComponents( 4/11, 1/11, 2 )
addComponents( 3/11, 1/11, 3 )
% myValue
MagicTextWrapper( props{:}, ...
'Position', [ 0.1 1/11 0.075 1/11 ], ...
'String', 'My value' );
ControlWrapper( props{:}, ...
'Style', 'slider', ...
'Position', [ 0.2 1/11 0.15 1/11 ], ...
'Min', 0, 'Max', 1, ...
'Value', { this, 'myValue' } );
ControlWrapper( props{:}, ...
'Style', 'edit', ...
'Position', [ 0.4 1/11 0.1 1/11 ], ...
'String', { this, 'myValue', 'string<->double==', 0, 1 } );
ControlWrapper( props{:}, ...
'Style', 'edit', ...
'Position', [ 0.55 1/11 0.1 1/11 ], ...
'BackgroundColor', { this, 'valueIsOK', [], @(m)m*[ 0.5 1 0.5 ] + ~m*[1 0.5 0.5] }, ...
'String', { this, 'myValue', @vetodemo, @(m)sprintf('%g',m) } );
function m = vetodemo( v )
m = str2double(v);
if isempty( m ) || isnan( m ) || m < 0 || 1 < m
this.valueIsOK = false;
error( GraphicalHandleWrapper.VetoError );
end
end
MagicListener( this, 'myValue', 'PostSet', @(H,E)restoreValueIsOK() );
function restoreValueIsOK()
this.valueIsOK = true;
end
end
function delete( this )
if isvalid( this.hFig )
delete( this.hFig );
end
end
end
end