www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/+mbcgui/+widget/Label.m
classdef Label < mbcgui.widget.BasicContainer %LABEL Basic label component. % mbcgui.widget.Label is a component that displays a label string. The % string is truncated and terminated with an ellipsis when there is not % enough room to display the full string. When this happens the tooltip % can optionally be used to display the full string. % % Label properties: % String - String to display in the label. % UseTooltip - Boolean flag that indicates whether to use the % tooltip to display the full string. % HorizontalAlignment - HorizontalAlignment of label. % BackgroundColor - BackgroundColor of label. % ForegroundColor - ForegroundColor of label. % FontName - FontName of label. % FontSize - FontSize of label. % FontWeight - FontWeight of label. % % See also BasicContainer, Component. % Copyright 2008-2010 The MathWorks, Inc. properties(AbortSet) %STRING String to display in the label % The String property contains the string that will be displayed in % the label. When the label is too narrow to show all of the string, % the display will be truncated and appended with an ellipsis. When % this happens, the full string will be shown as a tooltip if the % UseTooltip property is set to true. % % See also UseTooltip. String = ''; %USETOOLTIP Indicate whether to show the full string in a tooltip % When the UseTooltip property is set to true, the full label string % is shown as a tooltip when the display is truncating the string. % If the full string is already visible in the label, no tooltip is % shown. UseTooltip = true; end properties(Dependent) HorizontalAlignment BackgroundColor ForegroundColor FontName FontSize FontWeight TooltipString UIContextMenu ButtonDownFcn end methods function obj = Label(varargin) %LABEL Construct a new Label object % H = mbcgui.widget.Label(PROP, VAL, ...) constructs a new Label % object with its properties set to the values specified by the input % list of property-value pairs. obj@mbcgui.widget.BasicContainer(varargin{:}); TextProps = obj.getDepPropPairs(... {'HorizontalAlignment', 'BackgroundColor', 'ForegroundColor', ... 'FontName', 'FontSize', 'FontWeight', 'TooltipString','UIContextMenu'}); obj.ContentHandle = uicontrol(... 'Parent', obj.Parent, ... 'Units', 'pixels', ... 'Style', 'text', ... 'HitTest', 'off', ... 'Enable', obj.Enable, ... 'Visible', obj.Visible, ... 'Position', obj.Position, ... 'Tag', 'mbcgui.widget.Label:Control', ... TextProps{:}); obj.updateLabel; end % Set and Get functions that forward values to the text uicontrol function set.HorizontalAlignment(obj, Align) obj.forwardProperty(obj.ContentHandle,'HorizontalAlignment', Align); end function Align = get.HorizontalAlignment(obj) Align = obj.retrieveProperty(obj.ContentHandle,'HorizontalAlignment', 'center'); end function set.BackgroundColor(obj, BGCol) obj.forwardProperty(obj.ContentHandle,'BackgroundColor', BGCol); end function BGCol = get.BackgroundColor(obj) BGCol = obj.retrieveProperty(obj.ContentHandle,'BackgroundColor', [1 1 1]); end function set.ForegroundColor(obj, FGCol) obj.forwardProperty(obj.ContentHandle,'ForegroundColor', FGCol); end function FGCol = get.ForegroundColor(obj) FGCol = obj.retrieveProperty(obj.ContentHandle,'ForegroundColor', [0 0 0]); end function set.FontName(obj, Name) obj.forwardProperty(obj.ContentHandle,'FontName', Name); end function Name = get.FontName(obj) Name = obj.retrieveProperty(obj.ContentHandle,'FontName', 'Sans Serif'); end function set.FontSize(obj, Size) obj.forwardProperty(obj.ContentHandle,'FontSize', Size); end function Size = get.FontSize(obj) Size = obj.retrieveProperty(obj.ContentHandle,'FontSize', 8); end function set.FontWeight(obj, W) obj.forwardProperty(obj.ContentHandle,'FontWeight', W); end function W = get.FontWeight(obj) W = obj.retrieveProperty(obj.ContentHandle,'FontWeight', 'normal'); end function set.String(obj, S) % Respond to string property set by updating the displayed label if ~ischar(S) error(message('mbc:mbcgui:widget:Label:InvalidPropertyValue')); end obj.String = S; % Truncate the string before forwarding it obj.updateLabel; end function set.UIContextMenu(obj, uic) obj.forwardProperty(obj.ContentHandle,'UIContextMenu', uic); end function uic = get.UIContextMenu(obj) uic = obj.retrieveProperty(obj.ContentHandle,'UIContextMenu', []); end function set.ButtonDownFcn(obj, Fcn) obj.forwardProperty(obj.ContentHandle,'ButtonDownFcn', Fcn); end function Fcn = get.ButtonDownFcn(obj) Fcn = obj.retrieveProperty(obj.ContentHandle,'ButtonDownFcn', []); end function set.UseTooltip(obj, UseTT) if ~isscalar(UseTT) || ~islogical(UseTT) error(message('mbc:mbcgui:widget:Label:InvalidPropertyValue1')); end obj.UseTooltip = UseTT; if UseTT % Redo truncation - this will update the tooltip if required obj.updateLabel; else % Clear the tooltip set(obj.ContentHandle, 'TooltipString', ''); end end function set.TooltipString(obj, TT) if ~obj.UseTooltip % Only accept manually-set tooltip if the object is not set to % automatically use the tooltip to display the full string obj.forwardProperty(obj.ContentHandle,'TooltipString', TT); end end function TT = get.TooltipString(obj) TT = obj.retrieveProperty(obj.ContentHandle,'TooltipString', ''); end end methods(Access=protected) function setPosition(obj, pos) % Update the label truncation when the width changes oldP = get(obj.ContentHandle, 'Position'); obj.setPosition@mbcgui.widget.BasicContainer(pos); if oldP(3)~=pos(3) obj.updateLabel; end end end methods(Access=private) function updateLabel(obj) % Update the uicontrol settings from the current string hLabel = obj.ContentHandle; if ~isempty(hLabel) % Set string to the truncated version str = obj.String; truncstr = obj.getTruncatedString(str); set(hLabel, 'String', truncstr); % Set tooltip if required if obj.UseTooltip if strcmp(truncstr, str) set(hLabel, 'TooltipString', ''); else set(hLabel, 'TooltipString', str); end end end end function str = getTruncatedString(obj, str) % Form the correctly truncated string % Assume that the font is plain. This saves a lot of time compared % with finding which string it really is. fontweight = 1; fontangle = 0; str = char(com.mathworks.toolbox.mbc.text.Util.convertToShortenedForm( ... str, ... get(obj.ContentHandle, 'FontName'), ... get(obj.ContentHandle, 'FontSize'), ... fontweight, ... fontangle, ... obj.Position(3))); end end end