Skip to content

Commit

Permalink
Implemented dynamic deletion and addition of items to menu
Browse files Browse the repository at this point in the history
The code works for the current menu data structure and can be applied only to
the menubar and its sub-menus (it was tested only for them). So, the value passed
to Menu parameter will be one of the top menubar options (file,edit,tool,...).
Removing => update_menu(Menu, Item, delete, _)
Adding   => update_menu(Menu, Item, {append, Pos0, Cmd0}, Help)

A sample of use can be found in YafaRay plugin in which it remove the render
option in case a valid path to the application was not set:
Removing => wings_menu:update_menu(file, {render, ?TAG}, delete);
Adding   => wings_menu:update_menu(file, {render, ?TAG}, {append, -1, Label})

The value -1 for the append operation means the item will be appended to the end
of the menu/sub-menu; Otherwise, the item will be inserted in the specificated
position or to the end of the menu if this value is greater than current amount
of menu items.

NOTE: Added logic that allow the Yafaray - after the engine path be provided -
get its render option available under File->Render option whithout need to
restart Wings3D. Thanks oort for ask about it.
  • Loading branch information
Micheus authored and dgud committed May 18, 2016
1 parent 8f2037a commit 9734e68
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
9 changes: 9 additions & 0 deletions plugins_src/import_export/wpc_yafaray.erl
Expand Up @@ -2147,7 +2147,16 @@ pref_dialog(St) ->

pref_result(Attr, St) ->
set_user_prefs(Attr),
OldVal = get_var(renderer),
init_pref(),
case get_var(renderer) of
OldVal -> ok;
false ->
wings_menu:update_menu(file, {render, ?TAG}, delete);
_ ->
[{Label, _}] = menu_entry(render),
wings_menu:update_menu(file, {render, ?TAG}, {append, -1, Label})
end,
St.

export_dialog(Op, Title) ->
Expand Down
50 changes: 49 additions & 1 deletion src/wings_menu.erl
Expand Up @@ -124,6 +124,11 @@ build_command(Name, Names, true) ->
build_command(Name, Names, false) ->
build_command(Name, Names).

build_names(Term, Acc) when not is_tuple(Term) -> Acc;
build_names({_,Term2}, Acc) when is_boolean(Term2) -> Acc;
build_names({Term1,Term2}, Acc) ->
build_names(Term2, [Term1]++Acc).

have_option_box(Ps) ->
proplists:is_defined(option, Ps).

Expand Down Expand Up @@ -589,6 +594,49 @@ update_menu(file, Item = {recent_file, _}, delete, _) ->
true = wxMenu:delete(File, Id),
ets:delete(wings_menus, Id),
ok;
update_menu(Menu, Item, delete, _) ->
case menu_item_id(Menu, Item) of
false -> ok;
Id ->
case ets:lookup(wings_menus, Id) of
[#menu{object=MenuItem}] ->
ParentMenu = wxMenuItem:getMenu(MenuItem),
true = wxMenu:delete(ParentMenu, Id),
ets:delete(wings_menus, Id),
ok;
_ ->
ok
end
end;
update_menu(Menu, Item, {append, Pos0, Cmd0}, Help) ->
case menu_item_id(Menu, Item) of
false ->
AddItem =
fun(SubMenu, Name) ->
Pos =
if Pos0 >= 0 -> min(wxMenu:getMenuItemCount(SubMenu), Pos0);
true -> wxMenu:getMenuItemCount(SubMenu)
end,
MO = wxMenu:insert(SubMenu, Pos, -1, [{text, Cmd0}]),
Id = wxMenuItem:getId(MO),
ME=#menu{name=Name, object=MO,
wxid=Id, type=?wxITEM_NORMAL},
true = ets:insert(wings_menus, ME),
Cmd = setup_hotkey(MO, Cmd0),
wxMenuItem:setText(MO, Cmd),
is_list(Help) andalso wxMenuItem:setHelp(MO, Help)
end,

Names = build_names(Item, [Menu]),
case ets:match_object(wings_menus, #menu{name=Names, _ = '_'}) of
[#menu{object=SubMenu}] ->
AddItem(SubMenu, build_command(Item, [Menu]));
_ ->
io:format("update_menu: Item rejected (~p|~p)\n",[Menu,Item])
end;
_ ->
ok
end;
update_menu(Menu, Item, Cmd0, Help) ->
Id = menu_item_id(Menu, Item),
MI = case ets:lookup(wings_menus, Id) of
Expand Down Expand Up @@ -810,7 +858,7 @@ menu_item_desc(Desc, HotKey) ->
_ -> Desc ++ "\t" ++ HotKey
end.

%% We want to use the prefdefined id where they exist (mac) needs for it's
%% We want to use the predefined id where they exist (mac) needs for it's
%% specialized menus but we want our shortcuts hmm.
%% We also get little predefined icons for OS's that have that.
predefined_item(Menu, Item, DefId) ->
Expand Down

0 comments on commit 9734e68

Please sign in to comment.