


FIND_UIMENU Finds/creates an item in a UIcontextmenu. HITEM = FIND_UIMENU(PARENT,TAG) searches the UIcontextmenu (if any) associated with the object PARENT for a menu item with tag TAG. If such a menu item exists, its handle is returned in HITEM. If no such item is found, or if PARENT does not have an associated UIcontextmenu, the function returns the empty matrix. HITEM = FIND_UIMENU(PARENT,TAG,LABEL,CALLBACK) similarly searches the PARENT UIcontextmenu for an item with tag TAG. If no such menu is found, it is created (along with a UIcontextmenu, if necessary) with the following properties: 'Tag' is set to TAG, 'Label' is set to LABEL, and 'Checked' is set to 'off'. [HITEM,HMENU] = FIND_UIMENU(...) also returns a handle to the UIcontextmenu itself.


0001 function [cxitem,cxmenu] = find_uimenu(parent, tag, label, callback) 0002 %FIND_UIMENU Finds/creates an item in a UIcontextmenu. 0003 % HITEM = FIND_UIMENU(PARENT,TAG) searches the UIcontextmenu (if any) 0004 % associated with the object PARENT for a menu item with tag TAG. If 0005 % such a menu item exists, its handle is returned in HITEM. If no such 0006 % item is found, or if PARENT does not have an associated UIcontextmenu, 0007 % the function returns the empty matrix. 0008 % 0009 % HITEM = FIND_UIMENU(PARENT,TAG,LABEL,CALLBACK) similarly searches the 0010 % PARENT UIcontextmenu for an item with tag TAG. If no such menu is 0011 % found, it is created (along with a UIcontextmenu, if necessary) with 0012 % the following properties: 'Tag' is set to TAG, 'Label' is set to 0013 % LABEL, and 'Checked' is set to 'off'. 0014 % 0015 % [HITEM,HMENU] = FIND_UIMENU(...) also returns a handle to the 0016 % UIcontextmenu itself. 0017 0018 0019 %%%%%%%%%%%%%%%%%%%%%%%%%%%% Parse Inputs %%%%%%%%%%%%%%%%%%%%%%%%%%%% 0020 if (~ishandle(parent)), error('First argument must be a valid handle.'); end; 0021 searchonly = (nargin < 3); 0022 0023 %%%%%%%%%%%%%%%%%%%%%%%%%%% Find/create Menu %%%%%%%%%%%%%%%%%%%%%%%%% 0024 cxmenu = get(parent, 'UIContextMenu'); 0025 if (isempty(cxmenu)), % if no context menu exists ... 0026 if (searchonly), cxitem = []; return; 0027 else, cxmenu = uicontextmenu; set(parent, 'UIContextMenu', cxmenu); 0028 end 0029 end 0030 0031 cxitem = findobj(cxmenu, 'Tag', tag); 0032 if (isempty(cxitem)) % if no matching menu item exists ... 0033 if (searchonly), return; 0034 else, cxitem = uimenu(cxmenu, 'Tag', tag, 'Checked', 'off', ... 0035 'Label', label, 'Callback', callback); 0036 end 0037 end