1 | function makeurl(h,url,varargin) |
---|
2 | %MAKEURL Make a URL from a uicontrol of style text. |
---|
3 | % MAKEURL(h,url) adjusts the properties of the text style UICONTROL |
---|
4 | % object to make it act like a typical URL hyperlink. The hyperlink's |
---|
5 | % target is given by the string url. |
---|
6 | % |
---|
7 | % MAKEURL(...,'ForeGroundColor',C1,'ClickedColor',C2) will set the |
---|
8 | % unclicked text color to C1 and the clicked text color to C2 where |
---|
9 | % C1 and C2 are valid colorspecs. The default values are C1 = 'b' |
---|
10 | % and C2 = 'm'. |
---|
11 | % |
---|
12 | % Ex. |
---|
13 | % |
---|
14 | % h = uicontrol('style','text','string','MathWorks'); |
---|
15 | % makeurl(h,'http://www.mathworks.com'); |
---|
16 | |
---|
17 | % Jordan Rosenthal, jr@ll.mit.edu |
---|
18 | % Initial Version, 08-Dec-1999 |
---|
19 | % Rev., 18-Dec-2001 Added -browser switch and changed email contact. |
---|
20 | |
---|
21 | %---------------------------------------------------------------------------- |
---|
22 | % Default Parameters |
---|
23 | %---------------------------------------------------------------------------- |
---|
24 | FOREGROUNDCOLOR = 'b'; |
---|
25 | CLICKEDCOLOR = 'm'; |
---|
26 | |
---|
27 | %---------------------------------------------------------------------------- |
---|
28 | % Parse Inputs |
---|
29 | %---------------------------------------------------------------------------- |
---|
30 | if nargin < 2, error('Not enough input arguments.'); end |
---|
31 | if ~strcmp(get(h,'style'),'text'), error('The UICONTROL h must be of style text.'); end |
---|
32 | if exist('varargin','var') |
---|
33 | L = length(varargin); |
---|
34 | if rem(L,2) ~= 0, error('Parameters/Values must come in pairs.'); end |
---|
35 | for i = 1:2:L |
---|
36 | switch lower(varargin{i}(1)) |
---|
37 | case 'f', FOREGROUNDCOLOR = varargin{i+1}; |
---|
38 | case 'c', CLICKEDCOLOR = varargin{i+1}; |
---|
39 | end |
---|
40 | end |
---|
41 | end |
---|
42 | %--- Add quotes around the color if given as a character ---% |
---|
43 | if ischar(CLICKEDCOLOR), CLICKEDCOLOR = ['''' CLICKEDCOLOR '''']; end |
---|
44 | |
---|
45 | %---------------------------------------------------------------------------- |
---|
46 | % Swicth units and get the pertinent parameters |
---|
47 | %---------------------------------------------------------------------------- |
---|
48 | OldUnits = get(h,'Units'); set(h,'Units','pixels'); |
---|
49 | Ext = get(h,'Extent'); |
---|
50 | Pos = get(h,'Pos'); |
---|
51 | Horiz = get(h,'HorizontalAlignment'); |
---|
52 | |
---|
53 | %---------------------------------------------------------------------------- |
---|
54 | % Add an underline by creating a 1 pixel high frame |
---|
55 | %---------------------------------------------------------------------------- |
---|
56 | Bottom = Pos(2) + Pos(4) - Ext(4) + 2; |
---|
57 | Width = Ext(3); |
---|
58 | Height = 1; |
---|
59 | switch lower(Horiz) |
---|
60 | case 'left' |
---|
61 | Left = Pos(1); |
---|
62 | case 'center' |
---|
63 | Left = Pos(1) + Pos(3)/2 - Ext(3)/2; |
---|
64 | case 'right' |
---|
65 | Left = Pos(1) + Pos(3) - Ext(3); |
---|
66 | end |
---|
67 | fPos = [Left Bottom Width Height]; |
---|
68 | hFrame = uicontrol('style','Frame','pos',fPos,'ForegroundColor',FOREGROUNDCOLOR); |
---|
69 | setappdata(h,'hFrame',hFrame); |
---|
70 | |
---|
71 | %---------------------------------------------------------------------------- |
---|
72 | % Setup callback |
---|
73 | %---------------------------------------------------------------------------- |
---|
74 | ButtonDownFcn = ... |
---|
75 | ['set([gcbo getappdata(gcbo,''hFrame'')],''ForeGroundColor'', ' CLICKEDCOLOR ');']; |
---|
76 | ButtonDownFcn = [ ButtonDownFcn 'web(''' url ''',''-browser'');' ]; |
---|
77 | |
---|
78 | %---------------------------------------------------------------------------- |
---|
79 | % Adjust the properties and restore units |
---|
80 | %---------------------------------------------------------------------------- |
---|
81 | set(h,'ForegroundColor',FOREGROUNDCOLOR, ... |
---|
82 | 'ButtonDownFcn',ButtonDownFcn,'Enable','Inactive','Units',OldUnits); |
---|