%LMIEDIT   Open the LMI Editor GUI
%
% LMIEDIT opens the LMI Editor, a graphical interface for specifying LMIs.
% To describe your LMI system,
%   1. define the matrix variables in the upper half of the
%      window.  Their type and structure is specified as in
%      LMIVAR
%   2. specify each LMI as a MATLAB expression in the lower
%      half of the window.  Type X to refer to a variable named
%      X.  Variables should not appear inside parentheses.
%
% Given this symbolic description,  LMIEDIT  generates the
% computer representation used by the LMI solvers as well as
% the problem description in terms of LMIVAR/LMITERM commands.
% Conversely,  LMIEDIT  can read a series of LMIVAR/LMITERM
% commands and display the corresponding symbolic expression
% of the LMIs.
%
% These various tasks are performed by pressing the following
% pushbuttons:
%   * 'view commands' : visualize the LMIVAR/LMITERM commands
%                       needed to specify given LMIs
%   * 'save'   : save the LMI description as a MATLAB string
%   * 'load'   : reload an LMI description previously stored
%                as a MATLAB string
%   * 'read'   : read a series of LMIVAR/LMITERM commands from
%                a file
%   * 'write'  : write the LMIVAR/LMITERM commands in a file
%   * 'create' : generate the internal description used by the
%                LMI solvers.  This representation is stored
%                in a MATLAB variable named after the LMI system
%   * 'clear all' :  clear all editable text areas
%   * 'close'  : exit LMIEDIT
%
% See the Reference Guide for details on scrolling.
%
% See also  LMIVAR, LMITERM.

% Authors: P. Gahinet and A. Nemirovski 3/95
% Copyright 1995-2004 The MathWorks, Inc.
%       $Revision: 1.1.6.1 $

clear ZZZ_ehdl
fn = [tempname '.mat'];
save(fn)
ZZZ_ehdl=zeros(1,24);

DefUIBgC = get(0,'DefaultUIcontrolBackgroundColor');
comp = computer;
if all(comp(1:2)=='PC'),
   DefUIBgC = 192/255*ones(1,3);
end


% permanent handles
%-------------------

%%% v5 code
% v5 allows for true figure placement.  v4.2 would make sure
% the figure did not exceed the screen boundaries.
% the case of the lmi editor, the title bar goes beyond
% the top of the screen
ZZZ_ehdl(1)=figure('units','norm','position',[.1 .1 .6 .8], ...
           'name','  LMI  Editor  ','color',DefUIBgC,'menubar','none');


% header
uicontrol('sty','frame','units','norm','pos',[.01 .95 .98 .045],...
          'backg',DefUIBgC);
uicontrol('sty','text','units','norm','pos',[.03 .955 .25 .035],...
          'hor','left','backg',DefUIBgC,'str','name the LMI system:');
ZZZ_ehdl(2)=uicontrol('sty','edit','units','norm','pos',[.3 .9575 .2 .03],...
                      'backg','w','hor','left','str','  ');

% variable frame
uicontrol('sty','frame','units','norm','pos',[.01 .665 .98 .275],...
          'backg',DefUIBgC);

% variable bar
ZZZ_ehdl(3)=uicontrol('sty','radio','units','norm','pos',...
          [.02 .905 .35 .03],'value',1,'str',...
           'describe the matrix variables',...
          'backg',DefUIBgC,'callb','lmireq(ZZZ_ehdl,''readvar'');');
ZZZ_ehdl(4)=uicontrol('sty','radio','units','norm','pos',...
          [.6 .905 .25 .03],'value',0,'str','view commands', ...
          'backg',DefUIBgC,'callb','lmireq(ZZZ_ehdl,''addvcode'');');
uicontrol('sty','push','str','help','units','norm',...
          'pos',[.88 .905 .1 .03],'callb',...
          'lmireq(1,''help'',[.6 .6 .38 .35]);');

% layout for var specs
ZZZ_ehdl(5)=uicontrol('sty','text','units','norm','pos',[.03 .87 .2 .03],...
                      'backg',DefUIBgC,'hor','cent','str','variable name',...
                      'userd',0);
ZZZ_ehdl(6)=uicontrol('sty','text','units','norm','pos',[.275 .87 .15 .03],...
                      'backg',DefUIBgC,'hor','cent','str','type (S/R/G)');
ZZZ_ehdl(7)=uicontrol('sty','text','units','norm','pos',[.47 .87 .5 .03],...
                      'backg',DefUIBgC,'hor','cent','str','structure');
ZZZ_ehdl(8)=uicontrol('sty','edit','units','norm','pos',[.03 .67 .2 .2],...
                      'backg','w','max',50,'str','  ','horiz','left',...
                      'callb','set(ZZZ_ehdl(5),''userd'',1);');
ZZZ_ehdl(9)=uicontrol('sty','edit','units','norm','horiz','left','pos',...
                      [.275 .67 .15 .2],'backg','w','max',50,'str','  ',...
                      'callb','set(ZZZ_ehdl(5),''userd'',1);');
ZZZ_ehdl(10)=uicontrol('sty','edit','units','norm','pos',[.47 .67 .5 .2],...
                      'backg','w','max',50,'userd',0,'str','  ','horiz','left',...
                      'callb','set(ZZZ_ehdl(5),''userd'',1);');
ZZZ_ehdl(11)=uicontrol('sty','edit','string','  ','max',50,...
                       'units','norm','pos',[.03 .67 .94 .225],...
                       'backg','w','vis','off','userd',0,'horiz','left',...
                       'callb','set(ZZZ_ehdl(11),''userd'',1);');



% LMI frame
uicontrol('sty','frame','units','norm','pos',[.01 .085 .98 .57],...
          'backg',DefUIBgC);

% LMI bar
ZZZ_ehdl(12)=uicontrol('sty','radio','units','norm','pos',...
          [.02 .62 .5 .03],'value',1, ...
          'str','describe the LMIs as MATLAB expressions', ...
          'backg',DefUIBgC,'callb','lmireq(ZZZ_ehdl,''readterm'');');
ZZZ_ehdl(13)=uicontrol('sty','radio','units','norm','pos',...
          [.6 .62 .25 .03],'value',0,'str','view commands', ...
          'backg',DefUIBgC,'callb','lmireq(ZZZ_ehdl,''addtcode'');');
uicontrol('sty','push','str','help','units','norm',...
          'pos',[.88 .62 .1 .03],'callb',...
          'lmireq(2,''help'',[.6 .2 .38 .35]);');


% layout for LMI expr.
% expression
ZZZ_ehdl(14)=uicontrol('sty','edit','string','  ','max',50,'horiz','left',...
          'units','norm','pos',[.03 .09 .94 .525],'userd',0,...
          'backg','w','callb','set(ZZZ_ehdl(14),''userd'',1);');
% code
ZZZ_ehdl(15)=uicontrol('sty','edit','string','  ','max',50,'horiz','left',...
          'units','norm','pos',[.03 .09 .94 .525],'backg','w',...
          'vis','off','userd',0,'callb','set(ZZZ_ehdl(15),''userd'',1);');



% push buttons
%%%%%%%%%%%%%%
% LMI description
uicontrol('sty','frame','units','norm','pos',[.01 .005 .25 .075],...
          'backg',DefUIBgC);
uicontrol('sty','text','units','norm','pos',[.02 .055 .22 .02],...
          'hor','center','backg',DefUIBgC,'str','LMI description');
uicontrol('sty','push','units','norm','pos',[.02 .01 .11 .04],...
          'string','load','callb','lmireq(ZZZ_ehdl,''load'');');
uicontrol('sty','push','units','norm','pos',[.14 .01 .11 .04],...
          'string','save','callb','lmireq(ZZZ_ehdl,''save'');');

% commands fileIO
uicontrol('sty','frame','units','norm','pos',[.27 .005 .25 .075],...
          'backg',DefUIBgC);
uicontrol('sty','text','units','norm','pos',[.28 .055 .22 .02],...
          'hor','center','backg',DefUIBgC,'str','commands');
uicontrol('sty','push','units','norm','pos',[.28 .01 .11 .04],...
          'string','read','callb','lmireq(ZZZ_ehdl,''getfile'',0);');
uicontrol('sty','push','string','write','units','norm', ...
          'pos',[.40 .01 .11 .04],'callb',...
          ['if isempty(strtrim(get(ZZZ_ehdl(2),''str''))),' ...
             'lmireq(ZZZ_ehdl,''nolmi''); '...
           'else lmireq(ZZZ_ehdl,''getfile'',1); end']);

% internal repres
uicontrol('sty','frame','units','norm','pos',[.53 .005 .22 .075],...
          'backg',DefUIBgC);
uicontrol('sty','text','units','norm','pos',[.535 .055 .21 .02],...
          'hor','center','backg',DefUIBgC,'str','internal description');
uicontrol('sty','push','string','create','units','norm', ...
          'pos',[.58 .01 .12 .04],'callb',...
          ['if isempty(strtrim(get(ZZZ_ehdl(2),''str''))),' ...
             'lmireq(ZZZ_ehdl,''nolmi'');' ...
           'else for ssztmp=lmireq(ZZZ_ehdl,''makelmi''), ' ...
             'eval(ssztmp); end, end, clear ssztmp;']);


uicontrol('sty','push','string','clear all','units','norm', ...
          'pos',[.76 .02 .12 .045],'callb',...
          ['set(ZZZ_ehdl([2 8:11 14:15]),''str'',''  '');' ...
           'set(ZZZ_ehdl([5,11,14:15]),''userd'',0);']);
uicontrol('sty','push','units','norm','string','close', ...
          'pos',[.89 .02 .09 .045],...
          'callb','delete(ZZZ_ehdl(1));clear ZZZ_ehdl;');



% dialog box
ZZZ_ehdl(16:20)=[uicontrol('sty','text','units','norm', ...
                   'vis','off','pos',[.55 .18 .4 .17])
         uicontrol('sty','text','units','norm','vis','off', ...
                   'pos',[.58 .30 .37 .05],'horiz','left')
         uicontrol('sty','edit','units','norm','vis','off', ...
                   'pos',[.58 .25 .34 .05],'backg','w', ...
                   'horiz','left','str','  ')
         uicontrol('sty','push','units','norm','vis','off', ...
                   'pos',[.63 .19 .08 .05],'str','OK');
         uicontrol('sty','push','units','norm','vis','off', ...
                   'pos',[.80 .19 .08 .05],'str','Cancel','callb', ...
                   ['set(ZZZ_ehdl(16:20),''vis'',''off'');' ...
                    'set(ZZZ_ehdl(14:15),''enable'',''on'');'])];

% error box
ZZZ_ehdl(21:24)=[uicontrol('sty','text','units','norm', ...
                   'pos',[.38 .40 .57 .15],'vis','off')
         uicontrol('sty','text','units','norm', ...
                   'pos',[.40 .51 .53 .03],'vis','off','horiz','center')
         uicontrol('sty','text','units','norm', ...
                   'pos',[.40 .48 .53 .03],'vis','off','horiz','center')
         uicontrol('sty','push','units','norm','pos', ...
                   [.64 .41 .08 .05],'str','OK','vis','off','callb', ...
                   ['set(ZZZ_ehdl(21:24),''vis'',''off'');' ...
                    'set(ZZZ_ehdl(22:23),''str'','' '');' ...
                    'set(ZZZ_ehdl(14:15),''enable'',''on'');'])];



% restore environment

clear comp DefUIBgC
load(fn)
delete(fn), clear fn

set(gcf,'HandleVisibility','callback')
