function varargout = menueditfunc(whichcall, varargin)
% MENUEDITFUNC  Support function for GUIDE menu editor

%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.4 $  $Date: 2000/06/02 04:42:00 $

  switch whichcall
    case 'getMenuStructure'
      menuRoot = newMenuNode;
      cmnuRoot = newMenuNode;
      show = get(0, 'showhiddenhandles');
      set(0, 'showhiddenhandles', 'on');
      getMenuStructure(varargin{1}, menuRoot, 1, 0);
      getMenuStructure(varargin{1}, cmnuRoot, 1, 1);
      set(0, 'showhiddenhandles', show);
      varargout{1} = menuRoot;
      varargout{2} = cmnuRoot;

    case 'doNewMenu'
      parent = varargin{1};
      label = varargin{2};
      newMenu = uimenu(parent, 'label', label);
      adapter = requestJavaAdapter(newMenu);
      varargout{1} = newMenuNode(1, newMenu, adapter);

    case 'doNewContextMenu'
      parent = varargin{1};
      tag = varargin{2};
      newCMenu = uicontextmenu('parent', parent, 'tag', tag);
      adapter = requestJavaAdapter(newCMenu);
      varargout{1} = newMenuNode(2, newCMenu, adapter);

    case 'getCMenuData'
      [varargout{1}, varargout{2}] = getCMenuData(varargin{1});
  end

%************************************************
function getMenuStructure(handle, node, isRoot, isCMenu)
  if (isCMenu)
    % get the uicontextmenus
    if (isRoot)
      menus = findobj(allchild(handle), 'flat', 'type', 'uicontextmenu');
      for i = length(menus):-1:1
        adapter = requestJavaAdapter(menus(i));
        child = newMenuNode(2, menus(i), adapter);
        node.addChild(child);
        getMenuStructure(menus(i), child, 0, 0);
      end
    end
  else
    if (isRoot)
      menus = findobj(allchild(handle), 'flat', 'type', 'uimenu', 'serializable', 'on');
    else
      menus = get(handle, 'children');
    end

    % get the uimenus
    for i = length(menus):-1:1
      adapter = requestJavaAdapter(menus(i));
      child = newMenuNode(1, menus(i), adapter);
      node.addChild(child);
      getMenuStructure(menus(i), child, 0, 0);
    end
  end

%************************************************
% have to use the fully qualified path to
% MenuNode because just using MenuNode(type, adapter)
% attempts to call a method "MenuNode" on the
% object "adapter", rather than calling the constructor
% of "MenuNode" with "adapter" as an argument
function node = newMenuNode(varargin)
  if (nargin == 0)
    node = com.mathworks.ide.layout.menueditor.MenuNode;
  else
    node = com.mathworks.ide.layout.menueditor.MenuNode(varargin{1}, ...
                                                        varargin{2}, ...
                                                        varargin{3});
  end

%************************************************
function [names, handles] = getCMenuData(adapter)
  fig = getEnclosingFigure(double(handle(adapter)));
  handles = findobj(allchild(fig), 'flat', 'type', 'uicontextmenu');
  names = get(handles, 'tag');

%************************************************
function fig = getEnclosingFigure(fig)
  while ~isempty(fig) & ~strcmp(get(fig, 'type'), 'figure'),
    fig = get(fig, 'parent');
  end;
