function slideg(lo,Gain,hi,flag,trash1,trash2)
% SLIDEG Manages a dialog box for the Sliding Gain block in SIMULINK.
%       SLIDEG(LO,GAIN,HI,FLAG) generates a dialog box when the 
%       Slider Gain block is double-clicked and manages the callbacks
%       of the uicontrols in the dialog box.  Each of the uicontrols 
%       calls this function with a different flag.
%
%       FLAG When called                      How called
%        0   Slider Gain block double-click : 
%        1   Slider callback                : LO, GAIN, and HI are empty
%        2   Lo editable text callback      : LO, GAIN, and HI are empty
%        3   Gain editable text callback    : LO, GAIN, and HI are empty
%        4   Hi editable text callback      : LO, GAIN, and HI are empty
%        6   Done pushbutton callback       : LO, GAIN, and HI are empty

%       Author(s): A. Potvin
%	Copyright (c) 1990-94 by The MathWorks, Inc.

%
% Important things that don't make this just another masked block.
%    - The Slider Gain dialog box does not disappear when system is closed
%    - It's a bad idea to have both the Slider Gain Mask and dialog box up
%
% Remember what's in figure UserData
%       ud = [slider lo_ed cu_ed hi_ed BlockHandle donebutton];

if nargin==6,
   disp('slideg: Slider gain callback calling structure has been changed.')
   disp('Present block will still work as before, but consider replacing it')
   disp('with Slider Gain block from the Linear library.')
   lo = hi;
   Gain = flag;
   hi = trash1;
   flag = trash2;
end

if flag==0,
   % A slider gain block has been double-clicked on

   % Get the current system and current block
   [sys,cb] = get_param;
   BlockHandle = get_param([sys '/' cb],'handle');

   % Assume dialog box is NOT presently on screen
   OnScreen = 0;

   % Check figures on screen to see if slider gain dialog box is already up   
   figs = get(0,'Chil');
   for i=1:length(figs),
      if strcmp(get(figs(i),'Type'),'figure'),
         ud = get(figs(i),'UserData');
         if length(ud)>5,
            if ud(5)==BlockHandle,
               % Found the Slider Gain figure
               set(0,'CurrentFig',figs(i));
               OnScreen = 1;
               set(figs(i),'Name',[sys '/' cb]),
            end % UserData contains BlockHandle
         end % UserData long enough
      end % if figure
   end % for number of root children 

   if ~OnScreen,

      % Create the slider gain dialog box
      ScreenUnit = get(0,'Unit');
      set(0,'Unit','pixels');
      ScreenS = get(0,'ScreenSize');
      layout;
      ButtonWH = [mStdButtonWidth mStdButtonHeight];
      HS = 5;
      FigW = 3*mStdButtonWidth + 4*mFrameToText;
      FigH = 3*mStdButtonHeight + 5*HS + mLineHeight;
      FigurePos = [(ScreenS(3)-FigW)/2 (ScreenS(4)-FigH)/2 FigW FigH];
      % Remark: consider putting figure in middle of SIMULINK window
      DefUIBgC = get(0,'DefaultUIControlBackgroundColor');
      f=figure('Pos',FigurePos,'Name',[sys '/' cb], ...
       'Color',DefUIBgC,'Resize','off','NextPlot','new', ...
       'NumberTitle','off');

      % Create static text
      lotxt = uicontrol('Style','text','String','Lo','Horiz','left', ...
       'Pos',[2*mFrameToText 2*mStdButtonHeight+3*HS ...
               mStdButtonWidth mLineHeight]);
      hitxt = uicontrol('Style','text','String','Hi','Horiz','right', ...
       'Pos',[mFrameToText+2*mStdButtonWidth ...
              2*mStdButtonHeight+3*HS mStdButtonWidth mLineHeight]);

      % Create slider
      % Remark: possibly check for value between zero and one
      value = (Gain-lo)/(hi-lo);
      call_str = ['slideg([],[],[]'];
      slider = uicontrol('Style','slider','Value',value, ...
       'Pos',[2*mFrameToText    2*mStdButtonHeight+mLineHeight+4*HS ...
              3*mStdButtonWidth mStdButtonHeight], ...
       'Call',[call_str ',1);']);

      % Create editable controls
      Bup = 2*HS+mStdButtonHeight;
      lo_ed = uicontrol('Style','edit','BackgroundColor','white', ...
       'Pos',[mFrameToText Bup ButtonWH], ...
       'String',num2str(lo),'UserData',lo, ...
       'Call',[call_str ',2);']);
      cu_ed = uicontrol('Style','edit','BackgroundColor','white', ...
       'Pos',[2*mFrameToText+mStdButtonWidth Bup ButtonWH], ...
       'String',num2str(Gain),'UserData',Gain, ...
       'Call',[call_str ',3);']);
      hi_ed = uicontrol('Style','edit','BackgroundColor','white', ...
       'Pos',[3*mFrameToText+2*mStdButtonWidth Bup ButtonWH], ...
       'String',num2str(hi),'UserData',hi, ...
       'Call',[call_str ',4);']);

      % Create pushbutton
      donebutton = uicontrol('Style','push','String','Done', ...
       'Pos',[mStdButtonWidth+2*mFrameToText HS ButtonWH], ...
       'Call',[call_str ',6);']);

      ud = [slider lo_ed cu_ed hi_ed BlockHandle donebutton];
      set(f,'UserData',ud);
      set(0,'Unit',ScreenUnit);
   end

elseif flag==1,
   % Slider callback
   fig = gcf;
   ud = get(fig,'UserData');
   lo = get(ud(2),'UserData');
   hi = get(ud(4),'UserData');
   value = lo+get(ud(1),'Value')*(hi-lo);
   ValueString = num2str(value);

   % Remark: check that system is still open and block still exists
   MaskDialog = ['eval(''slideg(' num2str(lo) ',' ...
    num2str(ValueString) ',' num2str(hi) ',0)'');'];
   set_param(ud(5),'Gain',ValueString, ...
                   'Mask Display',ValueString, ...
                   'Mask Dialogue',MaskDialog);
   set(ud(3),'String',ValueString);
   set(ud(3),'UserData',value);

elseif flag==2,
   % Lo uicontrol editable text callback
   fig = gcf;
   ud = get(fig,'UserData');
   lo = get(ud(2),'UserData');
   hi = get(ud(4),'UserData');
   Gain = get(ud(3),'UserData');

   tmp_str  =  get(ud(2),'String');
   % Check to make sure string is not empty
   error_str = '';
   if isempty(tmp_str),
      error_str = 'Input cannot be empty. No change made.';
   else
      num = str2num(tmp_str);
   end

   % Check to make sure string evaluates to an appropriate number
   if isempty(error_str) & isempty(num),
      error_str = 'Input must be a number. No change made';
   elseif isempty(error_str) & (num>Gain),
      error_str = 'Lo>Gain.  Change Gain first';
   end

   if ~isempty(error_str),
      set(ud(2),'String',num2str(lo));
      disp(setstr(7));
      errordlg(error_str);
   else
      lo = num;
      set(ud(2),'UserData',lo);
      set(ud(2),'String',tmp_str);
      value = (Gain-lo)/(hi-lo);
      set(ud(1),'Value',value);
   end

   % Remark: check that system is still open and block still exists
   MaskDialog = ['eval(''slideg(' num2str(lo) ',' ...
    num2str(Gain) ',' num2str(hi) ',0)'');'];
   set_param(ud(5),'Mask Dialogue',MaskDialog);

elseif flag==3,
   % Current Value uicontrol editable text callback
   fig = gcf;
   ud = get(fig,'UserData');
   lo = get(ud(2),'UserData');
   hi = get(ud(4),'UserData');
   Gain = get(ud(3),'UserData');

   tmp_str =  get(ud(3),'String');
   % Check to make sure string is not empty
   error_str = '';
   if isempty(tmp_str),
      error_str = 'Input cannot be empty. No change made.';
   else
      num = str2num(tmp_str);
   end

   % Check to make sure string evaluates to an appropriate number
   if isempty(error_str) & isempty(num),
      error_str = 'Input must be a number. No change made';
   elseif isempty(error_str) & (num<lo | num>hi),
      error_str = 'Lo>Gain or Hi<Gain';
   end

   if ~isempty(error_str),
      set(ud(3),'String',num2str(Gain));
      disp(setstr(7));
      errordlg(error_str);
   else
      Gain = num;
      set(ud(3),'UserData',Gain);
      set(ud(3),'String',tmp_str);
      value = (Gain-lo)/(hi-lo);
      set(ud(1),'Value',value);
   end

   % Remark: check that system is still open and block still exists
   MaskDialog = ['eval(''slideg(' num2str(lo) ',' ...
    num2str(Gain) ',' num2str(hi) ',0)'');'];
   set_param(ud(5),'Gain',num2str(Gain), ...
                   'Mask Display',num2str(Gain), ...
                   'Mask Dialogue',MaskDialog);

elseif flag==4,
   % Hi uicontrol editable text callback
   fig = gcf;
   ud = get(fig,'UserData');
   lo = get(ud(2),'UserData');
   hi = get(ud(4),'UserData');
   Gain = get(ud(3),'UserData');

   tmp_str =  get(ud(4),'String');
   % Check to make sure string is not empty
   error_str = '';
   if isempty(tmp_str),
      error_str = 'Input cannot be empty. No change made.';
   else
      num = str2num(tmp_str);
   end

   % Check to make sure string evaluates to an appropriate number
   if isempty(error_str) & isempty(num),
      error_str = 'Input must be a number. No change made';
   elseif isempty(error_str) & num<Gain,
      error_str = 'Hi<Gain.  Change Gain first';
   end

   if ~isempty(error_str),
      set(ud(4),'String',num2str(hi));
      disp(setstr(7));
      errordlg(error_str);
   else
      hi = num;
      set(ud(4),'UserData',hi);
      set(ud(4),'String',tmp_str);
      value = (Gain-lo)/(hi-lo);
      set(ud(1),'Value',value);
   end

   % Remark: check that system is still open and block still exists
   MaskDialog = ['eval(''slideg(' num2str(lo) ',' ...
    num2str(Gain) ',' num2str(hi) ',0)'');'];
   set_param(ud(5),'Mask Dialogue',MaskDialog);

elseif flag==6,
   % Close pushbutton callback
   delete(gcf);
else
   % Unknown
   error('slideg: unknown callback')
end

% end slideg
