function addMenu(h, tplot, menutype)

%% Get the right label and callback
if strcmp(menutype,'delete')
    propval = {'Label','Replace with NaNs','Callback',...
        @(es,ed) delselection(tplot)};
elseif strcmp(menutype,'remove')
    propval = {'Label','Remove observations','Callback',...
        @(es,ed) rmselection(tplot)};
elseif strcmp(menutype,'keep')
    propval = {'Label','Keep observations','Callback',...
        @(es,ed) rmselection(tplot,'complement')};
elseif strcmp(menutype,'newevent')
    propval = {'Label','Add new event...','Callback', ...
        {@localAddEvent h tplot},'Separator','on'};    
else
    return
end

%% Install uimenu on to selected curve's context menu    
if isempty(h.Menu.(menutype))
    for k=1:prod(size(h.SelectionCurves))
        h.Menus.(menutype) = [h.Menus.(menutype); uimenu(propval{:},...
            'Parent',get(h.SelectionCurves(k),'Uicontextmenu'))];
    end
    
    %% Install uimenu on time selection rectangle   
    
    if strcmp(menutype,'remove') || strcmp(menutype,'delete') || ...
            strcmp(menutype,'keep')
        for k=1:prod(size(h.SelectionCurves))
              h.Menus.(menutype) = [h.Menus.(menutype); uimenu(propval{:},...
                  'Parent',get(h.SelectionPatch(k),'Uicontextmenu'))]; 
        end
    end
end

function localAddEvent(eventSrc,eventData,view,tplot)

%% Create a new event
ind = find(cell2mat(get(tplot.waves,{'View'}))==view);
pos = get(get(eventSrc,'Parent'),'Userdata');
if strcmp(tplot.AbsoluteTime,'on')
    e = tsnewevent(datestr(pos(1,1)*tsunitconv('days',tplot.TimeUnits)+ ...
         datenum(tplot.StartDate)));
else
    e = tsnewevent(pos(1,1));
end


%% Add it to the selected time series
if ~isempty(e)
    % Convert the plot time unit to the timeseries time units
    refdate = tplot.waves(ind).DataSrc.Timeseries.TimeInfo.StartDate;
    if ~isempty(refdate)
        e.Time = (e.Time-datenum(refdate))*...
            tsunitconv(tplot.waves(ind).DataSrc.Timeseries.TimeInfo.Units,...
            'days');
    else
        e.Time = e.Time*tsunitconv(tplot.waves(ind).DataSrc.Timeseries.TimeInfo.Units,...
            tplot.TimeUnits);
    end
    tplot.waves(ind).DataSrc.Timeseries.addevent(e);
    tplot.waves(ind).DataSrc.Timeseries.send('datachange')
end

%% Popup the data selection mode since the event data tip will not select
%% properly in data selecton mode
tplot.setselectmode('None')