function uiWidth = largestuiwidth(strs,uiStyle)
% LARGESTEUIWIDTH Return the uicontrol extent width of the largest string in
%                 the input cell array of strings.  
%                 A fudge factor is added for popups and radio buttons to 
%                 compensate for the triangle on the popup and the circle on 
%                 the radio button.
% Inputs:
%   strs - cell array of strings.
%   uiStyle - uicontrol style.
%
% Output:
%   uiWidth -  the uicontrol extent width.

%   Author(s): P. Pacheco
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.2 $  $Date: 2000/06/13 21:23:17 $ 

if nargin == 1,
    uiStyle = 'Text';
end

if length(strs) > 1,
    [strs,maxLen] = largestStrLen(strs);  % Get largest string
end

% Make GUI's figure handle available in order to create the temporary ui.
shh = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','on');

% Determine the uiextent for the largest (or only) string.
temp_h = uicontrol('style','Text','visible','off','string',strs);
uiExtent = get(temp_h,'extent');
delete(temp_h);
set(0,'ShowHiddenHandles',shh);

switch uiStyle
case {'popup','radiobutton','checkbox'},
    % Add 21 to the width because these uis have part of the control
    % that doesn't display the text, e.g. the triangle in the popup.
    uiExtent(3) = uiExtent(3)+21; 
case 'pushbutton',
    % Add a fudge factor to the width so that the string doesn't end at
    % the very edge of the pushbuton.
    uiExtent(3) = uiExtent(3)+4;
end
uiWidth = uiExtent(3);

%----------------------------------------------------------------------
function [str,maxLen] = largestStrLen(strs)
% LARGESTSTRLEN Determine the length of largest string
%
% Input:
%   strs - cell array of strings
% Outputs:
%   str - largest string in the cell array of strings
%   maxLen - length of the largest string found in cell array
%
maxLen = 0;
str = strs(1);
for k = 1:length(strs), 
    currentLen = length(strs{k});
    if maxLen < currentLen,
        str = strs(k);
        maxLen = currentLen;
    end
end

% [EOF] largestuiwidth.m
