function varargout = segtool(varargin)
% SEGTOOL M-file for segtool.fig
%      SEGTOOL, by itself, creates a new SEGTOOL or raises the existing
%      singleton*.
%
%      H = SEGTOOL returns the handle to a new SEGTOOL or the handle to
%      the existing singleton*.
%
%      SEGTOOL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SEGTOOL.M with the given input arguments.
%
%      SEGTOOL('Property','Value',...) creates a new SEGTOOL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before segtool_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to segtool_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help segtool

% Last Modified by GUIDE v2.5 21-Apr-2007 00:11:03

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @segtool_OpeningFcn, ...
                   'gui_OutputFcn',  @segtool_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before segtool is made visible.
function segtool_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to segtool (see VARARGIN)

% Choose default command line output for segtool
handles.output = hObject;

%структура шаблона
handles.templeteFile.wav=0;
handles.templeteFile.Fs=0;
handles.templeteFile.Bits=0;

%структура рабочего сигнала
handles.workFile.wav=0;
handles.workFile.Fs=0;
handles.workFile.Bits=0;
handles.workFile.prevwav=0;
handles.workFile.name=[];
handles.workFile.path=[];

handles.zoom=1; %Масштаб
handles.c=1;

patch([-1; -1; 1; 1],[-2; 2; 2; -2],[-1;-1;-1;-1],'r',...
    'Tag','Slc',...
    'FaceColor',[0.5 0.5 1],...
    'FaceAlpha',0.6,...
    'Visible','off',...
    'ButtonDownFcn','segtool(''axes1_ButtonDownFcn'',gcbo,[],guidata(gcbo))');
line([-1 -1],[-2 2],'Tag','S1',...
    'LineStyle',':',...
    'Visible','off',...
    'ButtonDownFcn','segtool(''line_ButtonDownFcn'',gcbo,[],guidata(gcbo))');
line([1 1],[-2 2],'Tag','S2',...
    'LineStyle',':',...
    'Visible','off',...
    'ButtonDownFcn','segtool(''line_ButtonDownFcn'',gcbo,[],guidata(gcbo))');
line([0 0],[-2 2],'Tag','M1',...
    'Color','g',...
    'LineStyle',':',...
    'LineWidth',1,...
    'Visible','off',...
    'ButtonDownFcn','segtool(''line_ButtonDownFcn'',gcbo,[],guidata(gcbo))');

line([0 0],[-2 2],'Tag','M2',...
    'Color','g',...
    'LineStyle',':',...
    'LineWidth',1,...
    'Visible','off',...
    'ButtonDownFcn','segtool(''line_ButtonDownFcn'',gcbo,[],guidata(gcbo))');

handles.butPress='Up';
handles.object='none';
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes segtool wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = segtool_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --------------------------------------------------------------------
function MExit_Callback(hObject, eventdata, handles)
% hObject    handle to MExit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
delete(handles.figure1);

% --------------------------------------------------------------------
function MOpenTemplet_Callback(hObject, eventdata, handles)
% hObject    handle to MOpenTemplet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
bt='No';

%Проверка на изменения рабочего файла
if ~isequal(handles.workFile.prevwav,handles.workFile.wav)
    bt=questdlg('Файл изменен. Сохранить?','Сохранение');
end


if ~isequal(bt,'Cancel')%Если отмена, то выходим
    
    if isequal(bt,'Yes')%Если да, то сохраним
        if ~isequal(handles.workFile.name,[])
            MSave_Callback(hObject,eventdata,handles);
        else
            MSaveAs_Callback(hObject,evendata,handles);
        end
    end
    
    [filename,pathname]=uigetfile('*.wav','wav-file');
    if ~isequal(filename,0)

        set(findobj('Tag','MSave'),'Enable','off','Accelerator','');
        set(findobj('Tag','MSaveAs'),'Accelerato','s');
        
        [handles.templeteFile.wav, ...
         handles.templeteFile.Fs, ...
         handles.templeteFile.Bits]=wavread(strcat(pathname,filename));

        handles.workFile.wav=[];
        handles.workFile.wav(1:length(handles.templeteFile.wav))=0;
        handles.workFile.Fs=handles.templeteFile.Fs;
        handles.workFile.Bits=handles.templeteFile.Bits;
        handles.workFile.prevwav=handles.workFile.wav;
        handles.workFile.name=[];
        handles.workFile.path=[];
     
        delete(findobj('Tag','Templete'));
        delete(findobj('Tag','Work'));
     
        axes(handles.axes1);
        [sx,n]=scalex(handles.templeteFile.wav,-0.3,0.9);
        set(handles.axes1,'YTick',[-0.9 n]);
        line(1:length(handles.templeteFile.wav),...
             sx,...
             'Tag','Templete',...
             'Color','y',...
             'ButtonDownFcn','segtool(''axes1_ButtonDownFcn'',gcbo,[],guidata(gcbo))');
        
        [sx,n]=scalex(handles.workFile.wav,-0.9,-0.4);
        line(1:length(handles.templeteFile.wav),...
             sx,...
             'Tag','Work',...
             'Color','r',...
             'ButtonDownFcn','segtool(''axes1_ButtonDownFcn'',gcbo,[],guidata(gcbo))');
        set(handles.axes1,'XLim',[1 length(handles.templeteFile.wav)]);
        
        %открываем доступ к элементам окна
        set(findobj('Tag','MNew'),'Enable','on');
        set(findobj('Tag','MOpen'),'Enable','on');
        set(findobj('Tag','MSaveAs'),'Enable','on');
        set(findobj('Tag','Play'),'Enable','on');
        set(findobj('Tag','PZoomPlus'),'Enable','on');
        
        guidata(hObject,handles);
    end
end

% --------------------------------------------------------------------
function MNew_Callback(hObject, eventdata, handles)
% hObject    handle to MNew (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
bt='No';

%Проверка на изменения рабочего файла
if ~isequal(handles.workFile.prevwav,handles.workFile.wav)
    bt=questdlg('Файл изменен. Сохранить?','Сохранение');
end


if ~isequal(bt,'Cancel')%Если отмена, то выходим
    
    if isequal(bt,'Yes')%Если да, то сохраним
        if ~isequal(handles.workFile.name,[])
            MSave_Callback(hObject,eventdata,handles);
        else
            MSaveAs_Callback(hObject,eventdata,handles);
        end
    end

    handles.workFile.wav=[];
    handles.workFile.wav(1:length(handles.templeteFile.wav))=0;
    handles.workFile.Fs=handles.templeteFile.Fs;
    handles.workFile.Bits=handles.templeteFile.Bits;
    handles.workFile.prevwav=handles.workFile.wav;
    handles.workFile.name=[];
    handles.workFile.path=[];
    [sx,n]=scalex(handles.workFile.wav,-0.9,-0.4);
    set(findobj('Tag','Work'),'YData',sx);
        
    set(findobj('Tag','MSave'),'Enable','off');
    set(findobj('Tag','MSaveAs'),'Accelerator','s')
        
    guidata(hObject,handles);
end

% --------------------------------------------------------------------
function MOpen_Callback(hObject, eventdata, handles)
% hObject    handle to MOpen (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
bt='No';

%Проверка на изменения рабочего файла
if ~isequal(handles.workFile.prevwav,handles.workFile.wav)
    bt=questdlg('Файл изменен. Сохранить?','Сохранение');
end


if ~isequal(bt,'Cancel')%Если отмена, то выходим
    
    if isequal(bt,'Yes')%Если да, то сохраним
        if ~isequal(handles.workFile.name,[])
            MSave_Callback(hObject,eventdata,handles);
        else
            MSaveAs_Callback(hObject,eventdata,handles);
        end
    end


    [filename,pathname]=uigetfile('*.wav','wav-file');
    if ~isequal(filename,0)
        [wav,Fs,Bits]=wavread(strcat(pathname,filename));
        if length(wav)==length(handles.templeteFile.wav)
            handles.workFile.wav=[];
            handles.workFile.wav=wav;
            handles.workFile.Fs=Fs;
            handles.workFile.Bits=Bits;
            handles.workFile.prevwav=handles.workFile.wav;
            handles.workFile.name=filename;
            handles.workFile.path=pathname;
            
            [sx,n]=scalex(wav,-0.9,-0.4);
            set(findobj('Tag','Work'),'YData',sx);
        
            set(findobj('Tag','MSave'),'Enable','on','Accelerator','s');
            set(findobj('Tag','MSaveAs'),'Accelerator','')
        else
            errordlg(sprintf(...
                'Невозможно подставить сигнал под шаблон.\nКоличество семплов должно совпадать'),...
                'Ошибка открытия файла');
        end
        
        guidata(hObject,handles);
    end
end
% --------------------------------------------------------------------
function MSave_Callback(hObject, eventdata, handles)
% hObject    handle to MSave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
wavwrite(handles.workFile.wav,...
         handles.workFile.Fs,...
         handles.workFile.Bits,...
         strcat(handles.workFile.path,handles.workFile.name));
handles.workFile.prevwav=handles.workFile.wav;
guidata(hObject,handles);

% --------------------------------------------------------------------
function MSaveAs_Callback(hObject, eventdata, handles)
% hObject    handle to MSaveAs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file,path]=uiputfile('*.wav','Save wav');
if ~isequal(file,0)
wavwrite(handles.workFile.wav,...
         handles.workFile.Fs,...
         handles.workFile.Bits,...
         strcat(path,file));

handles.workFile.path=path;
handles.workFile.name=file;
handles.workFile.prevwav=handles.workFile.wav;

set(findobj('Tag','MSave'),'Enable','on','Accelerator','s');
set(findobj('Tag','MSaveAs'),'Accelerator','');

guidata(hObject,handles);
end
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
w=get(handles.axes1,'XLim');
l=get(hObject,'Value');
d=l-w(1);
set(handles.axes1,'XLim',[l (w(2)+d)]);

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in PZoomPlus.
function PZoomPlus_Callback(hObject, eventdata, handles)
% hObject    handle to PZoomPlus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.zoom=round(handles.zoom*2);

zw=floor(length(handles.workFile.wav)/handles.zoom);
w=get(handles.axes1,'XLim');
c=floor((w(2)+w(1))/2);
zl=ceil(c-zw/2);
zr=floor(c+zw/2);
set(handles.axes1,'XLim',[zl zr]);

set(findobj('Tag','PZoomMinus'),'Enable','on');
set(findobj('Tag','slider1'),'Enable','on',...
    'Max',length(handles.workFile.wav)-(zr-zl),...
    'Min',1,...
    'Value',zl,...
    'SliderStep',[0.02/handles.zoom 0.2/handles.zoom]);

w=zr-zl;
set(handles.axes1,'Units','pixels');
wp=get(handles.axes1,'Position');

if wp(3)>5*w
    set(findobj('Tag','Work'),'Marker','.');
    set(findobj('Tag','Templete'),'Marker','.');
else
    set(findobj('Tag','Work'),'Marker','none');
    set(findobj('Tag','Templete'),'Marker','none');
end    

guidata(hObject,handles);

% --- Executes on button press in PZoomMinus.
function PZoomMinus_Callback(hObject, eventdata, handles)
% hObject    handle to PZoomMinus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.zoom=round(handles.zoom/2);

if handles.zoom==1
    set(handles.axes1,'XLim',[1 length(handles.workFile.wav)]);
    
    set(findobj('Tag','PZoomMinus'),'Enable','off');
    set(findobj('Tag','slider1'),'Enable','off');
    zl=1; zr=length(handles.workFile.wav);
else
    zw=floor(length(handles.workFile.wav)/handles.zoom);
    w=get(handles.axes1,'XLim');
    c=floor((w(2)+w(1))/2);
    zl=ceil(c-zw/2);
    zr=floor(c+zw/2);
    
    if zl<1
        zr=zr-zl+1;
        zl=1;
    end
    
    if zr>length(handles.workFile.wav)
        zl=zl-(zr-length(handles.workFile.wav));
        zr=length(handles.workFile.wav);
    end
    
    set(handles.axes1,'XLim',[zl zr]);
    
    set(findobj('Tag','slider1'),...
        'Max',length(handles.workFile.wav)-(zr-zl),...
        'Min',1,...
        'Value',zl,...
        'SliderStep',[0.02/handles.zoom 0.2/handles.zoom]);

end

w=zr-zl;
set(handles.axes1,'Units','pixels');
wp=get(handles.axes1,'Position');

if wp(3)>5*w
    set(findobj('Tag','Work'),'Marker','.');
    set(findobj('Tag','Templete'),'Marker','.');
else
    set(findobj('Tag','Work'),'Marker','none');
    set(findobj('Tag','Templete'),'Marker','none');
end    

guidata(hObject,handles);
    

% --- Executes on button press in Set.
function Set_Callback(hObject, eventdata, handles)
% hObject    handle to Set (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

xm1=get(findobj('Tag','M1'),'XData');
set(findobj('Tag','M1'),'Visible','off');
if isequal(get(findobj('Tag','M2'),'Visible'),'off')
    handles.workFile.wav(xm1(1))=1;
else
    xm2=get(findobj('Tag','M2'),'XData');
    handles.workFile.wav(xm1(1):xm2(1))=1;
    set(findobj('Tag','M2'),'Visible','off');
end
[sx,n]=scalex(handles.workFile.wav,-0.9,-0.4);
set(findobj('Tag','Work'),'YData',sx);

set(hObject,'Enable','off');
set(findobj('Tag','Down'),'Enable','off');
guidata(hObject,handles);

% --- Executes on button press in Down.
function Down_Callback(hObject, eventdata, handles)
% hObject    handle to Down (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
xm1=get(findobj('Tag','M1'),'XData');
set(findobj('Tag','M1'),'Visible','off');
if isequal(get(findobj('Tag','M2'),'Visible'),'off')
    handles.workFile.wav(xm1(1))=0;
else
    xm2=get(findobj('Tag','M2'),'XData');
    handles.workFile.wav(xm1(1):xm2(1))=0;
    set(findobj('Tag','M2'),'Visible','off');
end
[sx,n]=scalex(handles.workFile.wav,-0.9,-0.4);
set(findobj('Tag','Work'),'YData',sx);

set(hObject,'Enable','off');
set(findobj('Tag','Set'),'Enable','off');
guidata(hObject,handles);

% --- Executes on button press in ClrSlc.
function ClrSlc_Callback(hObject, eventdata, handles)
% hObject    handle to ClrSlc (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(findobj('Tag','S1'),'Visible','off');
set(findobj('Tag','S2'),'Visible','off');
set(findobj('Tag','Slc'),'Visible','off');

% --- Executes on button press in Play.
function Play_Callback(hObject, eventdata, handles)
% hObject    handle to Play (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if isequal(get(findobj('Tag','Slc'),'Visible'),'on')
    EdgePlay=get(findobj('Tag','Slc'),'XData');
    wavplay(handles.templeteFile.wav(EdgePlay(1):EdgePlay(3)),...
        handles.templeteFile.Fs);
else
    EdgePlay=get(handles.axes1,'XLim');
    wavplay(handles.templeteFile.wav(EdgePlay(1):EdgePlay(2)),...
        handles.templeteFile.Fs);
end

% --- Executes when figure1 is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
pos=get(hObject,'Position');

if pos(3)<260
    pos(3)=260;
end
if pos(4)<170
    pos(4)=170;
end
set(hObject,'Position',pos);

set(findobj('Tag','axes1'),'Position',[30 40 pos(3)-160 pos(4)-70]);
set(findobj('Tag','slider1'),'Position',[30 20 pos(3)-200 19]);
set(findobj('Tag','PZoomPlus'),'Position',[pos(3)-149 20 19 19]);
set(findobj('Tag','PZoomMinus'),'Position',[pos(3)-169 20 19 19]);
set(findobj('Tag','Set'),'Position',[pos(3)-110 pos(4)-50 100 20]);
set(findobj('Tag','Down'),'Position',[pos(3)-110 pos(4)-70 100 20]);
set(findobj('Tag','ClrSlc'),'Position',[pos(3)-110 pos(4)-90 100 20]);
set(findobj('Tag','Play'),'Position',[pos(3)-110 20 100 20]);

% --------------------------------------------------------------------
function MFile_Callback(hObject, eventdata, handles)
% hObject    handle to MFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
pos=get(handles.axes1,'Position');
XY=xyinaxes(get(handles.figure1,'CurrentPoint'),pos);

if ((XY(1)>=0)&(XY(1)<=pos(3)))&((XY(2)>=0)&(XY(2)<=pos(4)))
    
    if isequal(handles.butPress,'Down')
        x=round(pos2x(XY(1),get(handles.axes1,'XLim'),pos(3)));
        set(findobj('Tag',handles.object),'XData',[x x]);
        
        if isequal(handles.object,'S1')||isequal(handles.object,'S2')
            h=findobj('Tag','Slc');
            px=get(h,'XData');
            switch handles.object
                case 'S1'
                    px(1:2)=x;
                case 'S2'
                    px(3:4)=x;
            end
            set(h,'XData',px);
        end
    end
end

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
pos=get(handles.axes1,'Position');
XY=xyinaxes(get(handles.figure1,'CurrentPoint'),pos);

if isequal(handles.butPress,'Down')
    switch handles.object
        case {'M1','M2'}
            hm2=findobj('Tag','M2');
            if isequal(get(hm2,'Visible'),'on')
                hm1=findobj('Tag','M1');
                xm1=get(hm1,'XData');
                xm2=get(hm2,'XData');
                if xm1(1)>xm2(1)
                    set(hm1,'Tag','M2');
                    set(hm2,'Tag','M1');
                end
            end
        case {'S1','S2'}
            hs1=findobj('Tag','S1');
            hs2=findobj('Tag','S2');
            xs1=get(hs1,'XData');
            xs2=get(hs2,'XData');
            if xs1(1)>xs2(1)
                set(hs1,'Tag','S2');
                set(hs2,'Tag','S1');
                set(findobj('Tag','Slc'),...
                    'XData',[xs2(1);xs2(1);xs1(1);xs1(1)]);                
            end
    end
end
handles.butPress='Up';

guidata(hObject,handles);

% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
butType=get(handles.figure1,'SelectionType');

Pos=get(handles.axes1,'Position');
XY=xyinaxes(get(handles.figure1,'CurrentPoint'),Pos);

switch butType
    case 'normal' % LMB
        if isequal('off',get(findobj('Tag','M2'),'Visible'))
            handles.butPress='Down';
            
            h=findobj('Tag','M1');
            if isequal('on',get(h,'Visible'))
                h=findobj('Tag','M2');
            end

            x=round(pos2x(XY(1),get(handles.axes1,'XLim'),Pos(3)));
            set(h,'XData',[x x],'Visible','on');
            handles.object=get(h,'Tag');
        end
        
        set(findobj('Tag','Set'),'Enable','on');
        set(findobj('Tag','Down'),'Enable','on');
        
    case 'extend' % Shift+LMB
        handles.butPress='Down';
        x=round(pos2x(XY(1),get(handles.axes1,'XLim'),Pos(3)));
        
        set(findobj('Tag','S1'),'XData',[x x],'Visible','on');
        set(findobj('Tag','S2'),'XData',[x x],'Visible','on');
        handles.object='S2';
        
        set(findobj('Tag','Slc'),'XData',[x; x; x; x],'Visible','on');
        
        set(findobj('Tag','ClrSlc'),'Enable','on');
end

guidata(hObject,handles);
%------------------------------------------------------
function line_ButtonDownFcn(hObject, eventdata, handles)

butType=get(handles.figure1,'SelectionType');
switch butType
    case 'normal'
        handles.butPress='Down';
        handles.object=get(hObject,'Tag');
    case 'alt'
        switch get(hObject,'Tag')
            case 'M1'
                h=findobj('Tag','M2');
                if isequal(get(h,'Visible'),'on')
                    set(h,'Tag','M1');
                    set(hObject,'Visible','off','Tag','M2');
                else
                    set(hObject,'Visible','off');
                    
                    set(findobj('Tag','Set'),'Enable','off');
                    set(findobj('Tag','Down'),'Enable','off');
                end
            case 'M2'
                set(hObject,'Visible','off');
        end
end

guidata(hObject,handles);
%------------ доп. функции ----------------------------
function X=pos2x(pX,Edge,W)
X=Edge(1)+(Edge(2)-Edge(1))*pX/W;

function pX=x2pos(X,Edge,W)
pX=round((X-Edge(1))*W/(Edge(2)-Edge(1)));

function XY=xyinaxes(CP,Pos)
XY(1)=-Pos(1)+CP(1);
XY(2)=-Pos(2)+CP(2);

function [sx,n]=scalex(x,nmin,nmax)
omin=min(x);
omax=max(x);
if omax==0
    omax=1;
end
dxmin=nmin-omin;
odx=omax-omin;
ndx=nmax-nmin;

sx=(x-omin)*(nmax-nmin)/(omax-omin)+nmin;
n=-omin*(nmax-nmin)/(omax-omin)+nmin;

