www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/+mbcgui/+container/addPanelEnable.m

    function addPanelEnable(P, State)
%ADDPANELENABLE Add an Enable property to a uipanel.
%
%   ADDPANELENABLE(P, STATE) adds an Enable property to the uipanel P, and
%   optionally intitialise it to STATE.  Setting the Enable property will
%   toggle the enable state of the title text and if the panel has a
%   LayoutComponent property will also pass on the enable status to the
%   contained layout.
%
%   Note that the disabling of the frame title will only work correctly on
%   win32 platforms and for etched-style uipanels.

%   Copyright 2008-2014 The MathWorks, Inc.

P = mbcgui.hgclassesutil.toHandle(P);
if ~isempty(findprop(P, 'Enable'))
    % Don't try to add another enable property
    return
end

mbcgui.hgclassesutil.addprop(P, 'Enable', 'SetMethod', @iSetEnable);

% Create a text item that will take over the title duties from the frame
vis = get(P, 'Visible');
titleStr = get(P, 'Title');
if isempty(titleStr) 
    vis = 'off';
end
hTitle = uicontrol(...
    'Parent', get(P, 'Parent'), ...
    'Style', 'text', ...
    'Visible', vis, ...
    'Units', 'pixels', ...
    'Position', [0 0 1 1], ...
    'String', titleStr, ...
    'HorizontalAlignment', 'left', ...
    'HandleVisibility', 'off', ...
    'Tag', 'PanelTitleEnableText', ...
    'UserData', P, ...
    'BackgroundColor', get(P, 'BackgroundColor'), ...
    'ForegroundColor', get(P, 'ForegroundColor'), ...
    'FontAngle', get(P, 'FontAngle'), ...
    'FontName', get(P, 'FontName'), ...
    'FontSize', get(P, 'FontSize'), ...
    'FontUnits', get(P, 'FontUnits'), ...
    'FontWeight', get(P, 'FontWeight'));

% Create listeners on panel properties that will affect the title label
List = {mbcgui.hgclassesutil.proplistener(P, ...
    {'Parent', 'BackgroundColor', 'ForegroundColor'},...
    'PostSet', @iSyncProperty); ...
    mbcgui.hgclassesutil.proplistener(P, ...
    {'FontAngle', 'FontName', 'FontSize', 'FontUnits', 'FontWeight'},...
    'PostSet', @iSyncFontProperty); ...
    mbcgui.hgclassesutil.proplistener(P, 'Visible', 'PostSet', @iSetVisible); ...
    mbcgui.hgclassesutil.proplistener(P, 'Title', 'PostSet', @iSetTitle); ...
    mbcgui.hgclassesutil.proplistener(P, 'TitlePosition', 'PostSet', @iSetPosition); ...
    mbcgui.hgclassesutil.listener(P, 'SizeChanged', @iSetPosition); ...
    };

% Store listeners and the title text item in hidden properties
mbcgui.hgclassesutil.addprop(P, 'MBCDisableTitle', 'Hidden', true);
mbcgui.hgclassesutil.addprop(P, 'MBCDisableListeners', 'Hidden', true);
set(P, 'MBCDisableTitle', hTitle, 'MBCDisableListeners', List);

% Set enable state if it is provided
if nargin>1
    set(P, 'Enable', State);
else
    set(P, 'Enable', 'on');
end

iUpdateLabelPosition(P, hTitle);


function iSetEnable(P, EnState)
if ~any(strcmpi(EnState, {'on', 'inactive', 'off'}))
    error(message('mbc:mbcgui:container:addPanelEnable:InvalidPropertyValue'));
end
P.Enable=EnState;
set(get(P, 'MBCDisableTitle'), 'Enable', EnState);
if isprop(P, 'LayoutComponent')
    % Pass on enable status to layout
    set(get(P, 'LayoutComponent'), 'Enable', EnState);
end


function iSetVisible(src, evt)
P = evt.AffectedObject;
titleStr = get(P, 'Title');
vis = get(P, 'Visible');
if isempty(titleStr)
    % Force text object to be invisible for empty strings.
    vis = 'off';
end
set(get(P, 'MBCDisableTitle'), 'Visible', vis);


function iSyncProperty(src, evt)
P = evt.AffectedObject;
set(get(P, 'MBCDisableTitle'), src.Name, get(P, src.Name));


function iSyncFontProperty(src, evt)
P = evt.AffectedObject;

% Font size and unit changes may occur out of the proper order.  
% To get round this we just get and set all font properties together.
FontProps = {'FontAngle', 'FontName', 'FontUnits', 'FontSize', 'FontWeight'};
FontSettings = get(P, FontProps);
set(get(P, 'MBCDisableTitle'), FontProps, FontSettings);
iUpdateLabelPosition(P, get(P, 'MBCDisableTitle'));


function iSetPosition(src, evt)
P = evt.Source;
iUpdateLabelPosition(P, get(P, 'MBCDisableTitle'));


function iSetTitle(src, evt)
P = evt.AffectedObject; 
titleStr = get(P, 'Title');
vis = get(P, 'Visible');
hText = get(P, 'MBCDisableTitle');

if strcmp(vis, 'on') && isempty(titleStr)
    % Make sure the text is made invisible for empty strings
    vis = 'off';
end

set(hText, 'String', titleStr, 'Visible', vis);

iUpdateLabelPosition(P, get(P, 'MBCDisableTitle'));


function iUpdateLabelPosition(P, hTitle)
% Get x/y offsets that depend on platform and HG status
xOff = iGetXOffset(P);
yOff = iGetYOffset;

List = get(P, 'MBCDisableListeners');
mbcgui.hgclassesutil.setListenerEnabled(List, false);
ext = get(hTitle, 'Extent');
pnlPos = getpixelposition(P);
mbcgui.hgclassesutil.setListenerEnabled(List, true);

% Correct the extent height value - it seems to be off by a scale related to 72
% dpi
ext(4) = ext(4).*72/get(0, 'ScreenPixelsPerInch');

% Place the title text accordig to TitlePosition setting
titleloc = get(P, 'TitlePosition');
titlepos = [0 0 ext(3)-2 ext(4)];
align = 'left';
switch titleloc(1:3)
    case 'lef'
        titlepos(1) = pnlPos(1)+xOff;
    case 'cen'
        titlepos(1) = pnlPos(1)+pnlPos(3)/2-ext(3)/2-1;
        align = 'center';
    case 'rig'
        titlepos(1) = pnlPos(1)+pnlPos(3)-xOff-ext(3);
        align = 'right';
end
switch titleloc(end-2:end)
    case 'top'
        titlepos(2) = pnlPos(2)+pnlPos(4)-yOff-ext(4);
    case 'tom'
        titlepos(2) = pnlPos(2)+yOff+1;
end

set(hTitle, 'Position', titlepos, 'HorizontalAlignment', align);


function x = iGetXOffset(P)
% Return an x offset for the label
x = 6;


function y = iGetYOffset
% Return a y offset for the label.
y = 0;