function out1 = demoguim(option,in2,in3,in4)
%DEMOGUIM Main for GUI tools demo in the Wavelet Toolbox.
%   DEMOGUIM creates the window for GUI mode demos.
%
%   DEMOGUIM('auto') shows all the GUI mode demos
%   in automatic mode.
%
%   DEMOGUIM('loop') shows all the GUI mode demos
%   in loop mode.

%   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 12-Mar-96.
%   Last Revision: 25-Feb-1998.
%   Copyright (c) 1995-98 by The MathWorks, Inc.
% $Revision: 1.7 $

if nargin==0 , option = 'create'; end

tag_dem_tool    = 'Demo_Tool';
tag_guim_tool   = 'Guim_Tool';
tag_btn_close   = 'Demo_Close'; % tag for window
tag_sub_close   = 'Guim_Close'; % tag for sub_window

switch option
    case 'create'
        win = wfindobj('figure','tag',tag_guim_tool);
        if ~isempty(win) , return; end

        % Waiting Frame construction & begin waiting.
        %--------------------------------------------
        mousefrm(0,'watch');

        % GUIM main window initialization.
        %---------------------------------
        name = 'Gui mode demo';
        [win_demoguim,pos_win,defBtnWidth,defBtnHeight,win_units] = ...
                wdfigutl('menu',name,[13/4 33/2],tag_guim_tool);
        out1 = win_demoguim;
        str_win_demoguim = int2str(win_demoguim);
 
        % Position property of objects.
        %------------------------------
        btn_width  = 3*defBtnWidth;
        btn_heigth = 3*defBtnHeight/2;
        btn_left   = ceil(pos_win(3)-btn_width)/2;
        btn_low    = pos_win(4)-2*defBtnHeight;
        dif_heigth = 2*defBtnHeight;
        pos_dw1d   = [btn_left , btn_low , btn_width , btn_heigth];
        btn_low    = btn_low-dif_heigth;
        pos_dw2d   = [btn_left , btn_low , btn_width , btn_heigth];
        btn_low    = btn_low-dif_heigth;
        pos_wvdi   = [btn_left , btn_low , btn_width , btn_heigth];
        btn_low    = btn_low-dif_heigth;
        pos_wp1d   = [btn_left , btn_low , btn_width , btn_heigth];
        btn_low    = btn_low-dif_heigth;
        pos_wp2d   = [btn_left , btn_low , btn_width , btn_heigth];
        btn_low    = btn_low-dif_heigth;
        pos_wpdi   = [btn_left , btn_low , btn_width , btn_heigth];
        btn_low    = btn_low-dif_heigth;
        pos_cw1d   = [btn_left , btn_low , btn_width , btn_heigth];
        btn_low    = btn_low-dif_heigth;
        pos_close  = [btn_left , btn_low , btn_width , btn_heigth];

        % String property of objects.
        %----------------------------
        str_dw1d   = 'Wavelet 1-D';
        str_dw2d   = 'Wavelet 2-D';
        str_wvdi   = 'Wavelet Display';
        str_wp1d   = 'Wavelet Packet 1-D';
        str_wp2d   = 'Wavelet Packet 2-D';
        str_wpdi   = 'Wavelet Packet Display';
        str_cw1d   = 'Continuous Wavelet 1-D';
        str_close  = 'Close';

        % Callback property of objects.
        %------------------------------
        cba_dw1d   = [mfilename '(''dw1d'',' str_win_demoguim ');'];
        cba_dw2d   = [mfilename '(''dw2d'',' str_win_demoguim ');'];
        cba_wvdi   = [mfilename '(''wvdi'',' str_win_demoguim ');'];
        cba_wp1d   = [mfilename '(''wp1d'',' str_win_demoguim ');'];
        cba_wp2d   = [mfilename '(''wp2d'',' str_win_demoguim ');'];
        cba_wpdi   = [mfilename '(''wpdi'',' str_win_demoguim ');'];
        cba_cw1d   = [mfilename '(''cw1d'',' str_win_demoguim ');'];
        cba_close  = [mfilename '(''close'',' str_win_demoguim ');'];

        % Construction of objects.
        %-------------------------
        pus_dw1d   = uicontrol(...
                                   'Style','Pushbutton',...
                                   'Unit',win_units,...
                                   'Position',pos_dw1d,...
                                   'String',str_dw1d,...
                                   'Callback',cba_dw1d...
                                   );
        pus_dw2d   = uicontrol(...
                                   'Style','Pushbutton',...
                                   'Unit',win_units,...
                                   'Position',pos_dw2d,...
                                   'String',str_dw2d,...
                                   'Callback',cba_dw2d...
                                   );
        pus_wvdi   = uicontrol(...
                                   'Style','Pushbutton',...
                                   'Unit',win_units,...
                                   'Position',pos_wvdi,...
                                   'String',str_wvdi,...
                                   'Callback',cba_wvdi...
                                   );
        pus_wp1d   = uicontrol(...
                                   'Style','Pushbutton',...
                                   'Unit',win_units,...
                                   'Position',pos_wp1d,...
                                   'String',str_wp1d,...
                                   'Callback',cba_wp1d...
                                   );
        pus_wp2d   = uicontrol(...
                                   'Style','Pushbutton',...
                                   'Unit',win_units,...
                                   'Position',pos_wp2d,...
                                   'String',str_wp2d,...
                                   'Callback',cba_wp2d...
                                   );
        pus_wpdi   = uicontrol(...
                                   'Style','Pushbutton',...
                                   'Unit',win_units,...
                                   'Position',pos_wpdi,...
                                   'String',str_wpdi,...
                                   'Callback',cba_wpdi...
                                   );
        pus_cw1d   = uicontrol(...
                                   'Style','Pushbutton',...
                                   'Unit',win_units,...
                                   'Position',pos_cw1d,...
                                   'String',str_cw1d,...
                                   'Callback',cba_cw1d...
                                   );
        pus_close  = uicontrol(...
                                   'Style','Pushbutton',...
                                   'Unit',win_units,...
                                   'Position',pos_close,...
                                   'String',str_close,...
                                   'Callback',cba_close,...
                                   'Tag',tag_btn_close...
                                   );

        % Prevent OS closing.
        %--------------------
        set(win_demoguim,'CloseRequestFcn',cba_close)

        % Setting units to normalized.
        %-----------------------------
        set(findobj(win_demoguim,'Units','pixels'),'Units','Normalized');

        % Hide figure handle.
        %--------------------   
        hidegui(win_demoguim,'off');

        % End waiting.
        %---------------
        mousefrm(0,'arrow');
        drawnow

    case {'dw1d','dw2d','wvdi','wp1d','wp2d','wpdi','cw1d'}
        %***********************************%
        %** OPTION = 'dw1d' -  demo DW1D  **%
        %** OPTION = 'dw2d' -  demo DW2D  **%
        %** OPTION = 'wvdi' -  demo WVDI  **%
        %** OPTION = 'wp1d' -  demo WP1D  **%
        %** OPTION = 'wp2d' -  demo WP2D  **%
        %** OPTION = 'wpdi' -  demo WPDI  **%
        %** OPTION = 'cw1d' -  demo CW1D  **%
        %***********************************%
        if nargin==1
            win = wfindobj('figure','tag',tag_guim_tool);
            wait_mode = 0;
        else
            win = in2;
            wait_mode = 1;
            if nargin==3 , wait_mode = in3; end
        end
        gui = wait_mode;
        demoname = ['dgui' option];
        ok_win =  ~isempty(win);
        if ok_win , demoguim('enable',win,'off'); end
        if gui==0
            if ok_win
                set(findobj(win,'tag',tag_btn_close),'enable','on');
            end
            eval(demoname);
            if ok_win , demoguim('enable',win,'on'); end
        else
            demomode = 'command';
            addclose = [mfilename '(''endshow'');'];
            wshowdrv(demoname,demomode,addclose);
        end
        clear

    case 'enable'
        %*************************************************************%
        %** OPTION = 'enable' - Deseable or Enable demoguim window  **%
        %*************************************************************%
        % in2 = win handle
        % in3 = 'on' or 'off'   
        %--------------------
        if in3(1:2)=='of'
            mousefrm(0,'watch');
        else
            mousefrm(0,'arrow');
        end
        set(findobj(in2,'style','pushbutton'),'Enable',in3);
        drawnow;

    case 'auto'
        %**************************************************%
        %** OPTION = 'auto' - all demos (automatic mode) **%
        %**************************************************%
        clc
        demoguim('create');
        stop = 0;
        while stop==0
            demoguim('dw1d');
            demoguim('dw2d');
            demoguim('wvdi');
            demoguim('wp1d');
            demoguim('wp2d');
            demoguim('wpdi');
            demoguim('cw1d');
            stop = 1;
            if nargin==2
                if strcmp(in2,'loop') , stop = 0 ; end
            end
        end
        win = wfindobj('figure','tag',tag_guim_tool);
        demoguim('close',win);

    case 'loop'
        %***********************************************************%
        %** OPTION = 'loop' - loop for all demos (automatic mode) **%
        %***********************************************************%
        demoguim('auto','loop');

    case 'endshow'
        %**************************************************%
        %** OPTION = 'endshow' - used to finish wshowdrv **%
        %**************************************************%
        win = wfindobj('figure','tag',tag_guim_tool);
        set(findobj(win,'style','pushbutton'),'Enable','on');
        drawnow;

    case 'startshow'
        %***************************************************%
        %** OPTION = 'startshow' - used to start wshowdrv **%
        %***************************************************%
        figNumber = in2;
        out1 = wmemtool('rmb',figNumber,'Activefig',1);
        if isempty(out1)
            out1 = feval(in3);
            hndlList  = get(figNumber,'Userdata');
            closeHndl = hndlList(7);
            callbackStr = get(closeHndl,'Callback');
            winact = int2str(out1);
            str1 = ['fig = find(get(0,''children'')==' winact ');'];
            str2 = ['if ~isempty(fig) , clear fig; ' ...
                in3 '(''close'',' winact '); delete(' winact '); end;'];
            cba_close = [str1,str2,callbackStr];
            set(closeHndl,'Callback',cba_close,'tag',tag_sub_close);
            set(figNumber,'CloseRequestFcn',cba_close);
        end

    case 'close'
        %***********************************************%
        %** OPTION = 'close' - close demoguim window  **%
        %***********************************************%
        mousefrm(0,'watch')

        % Closing all opened main analysis windows.
        %------------------------------------------
        pus_handles = wfindobj(0,'Style','pushbutton');
        hdls        = findobj(pus_handles,'flat','Tag',tag_sub_close);
        for i=1:length(hdls)
            hdl = hdls(i);
            err = 0;
            eval('par = get(hdl,''Parent'');','err = 1;')
            eval(get(hdl,'Callback'),'err = 1;');
            eval('delete(par)','err = 1;')
        end

        % Closing the demoguim window.
        %-----------------------------
        eval('delete(in2)','err = 1;');

        win_ini = wfindobj('figure','tag',tag_dem_tool);
        if ~isempty(win_ini)
            pus_handles = findobj(win_ini,'Style','pushbutton');
            set(pus_handles,'Enable','on');
        else
            wglobals('clear')
        end
        mousefrm(0,'arrow');

    otherwise
        errargt(mfilename,'Unknown Option','msg');
        error('*');
end
