1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2009-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19
20-module(ex_treeCtrl).
21
22-behaviour(wx_object).
23
24%% Client API
25-export([start/1]).
26
27%% wx_object callbacks
28-export([init/1, terminate/2,  code_change/3,
29	 handle_info/2, handle_call/3, handle_cast/2, handle_event/2]).
30
31-include_lib("wx/include/wx.hrl").
32
33-record(state,
34	{
35	  parent,
36	  config
37	}).
38
39start(Config) ->
40    wx_object:start_link(?MODULE, Config, []).
41
42%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43init(Config) ->
44    wx:batch(fun() -> do_init(Config) end).
45
46do_init(Config) ->
47    Parent = proplists:get_value(parent, Config),
48    Panel = wxPanel:new(Parent, []),
49
50    %% Setup sizers
51    MainSizer = wxBoxSizer:new(?wxVERTICAL),
52    Sizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
53				 [{label, "wxTreeCtrl"}]),
54
55    %% Setup treeCtrl
56    TreeCtrl = wxTreeCtrl:new(Panel, []),
57    RootId = wxTreeCtrl:addRoot(TreeCtrl, "Root"),
58    %% Name the first items
59    Items = ["item "++integer_to_list(Int)||
60		Int <- lists:seq(1,10)],
61    %% Create the first items in the treeCtrl
62    SubItems = [{wxTreeCtrl:appendItem(TreeCtrl, RootId, Item), Item}||
63		   Item <- Items],
64    %% Create sub items
65    [wxTreeCtrl:appendItem(TreeCtrl, ItemId, Item++" sub item "++integer_to_list(Int))||
66	 {ItemId, Item} <- SubItems, Int <- lists:seq(1,10)],
67    wxTreeCtrl:expand(TreeCtrl, RootId),
68
69    %% Add to sizers
70    Options = [{flag, ?wxEXPAND}, {proportion, 1}],
71    wxSizer:add(Sizer, TreeCtrl, Options),
72    wxSizer:add(MainSizer, Sizer, Options),
73
74    wxTreeCtrl:connect(TreeCtrl, command_tree_item_collapsed),
75    wxTreeCtrl:connect(TreeCtrl, command_tree_item_expanded),
76    wxTreeCtrl:connect(TreeCtrl, command_tree_sel_changed),
77    wxPanel:setSizer(Panel, MainSizer),
78    {Panel, #state{parent=Panel, config=Config}}.
79
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81%% Async Events are handled in handle_event as in handle_info
82handle_event(#wx{event = #wxTree{type = command_tree_item_collapsed,
83				 item = Item},
84		 obj = TreeCtrl},
85	     State = #state{}) ->
86    ItemText = wxTreeCtrl:getItemText(TreeCtrl, Item),
87    demo:format(State#state.config, "You have collapsed ~p.\n", [ItemText]),
88    {noreply, State};
89handle_event(#wx{event = #wxTree{type = command_tree_item_expanded,
90				 item = Item},
91		 obj = TreeCtrl},
92	     State = #state{}) ->
93    ItemText = wxTreeCtrl:getItemText(TreeCtrl, Item),
94    demo:format(State#state.config, "You have expanded ~p.\n", [ItemText]),
95    {noreply, State};
96handle_event(#wx{event = #wxTree{type = command_tree_sel_changed,
97				 item = Item},
98		 obj = TreeCtrl},
99	     State = #state{}) ->
100    ItemText = wxTreeCtrl:getItemText(TreeCtrl, Item),
101    demo:format(State#state.config, "You have selected ~p.\n", [ItemText]),
102    {noreply, State}.
103
104%% Callbacks handled as normal gen_server callbacks
105handle_info(Msg, State) ->
106    demo:format(State#state.config, "Got Info ~p\n", [Msg]),
107    {noreply, State}.
108
109handle_call(shutdown, _From, State=#state{parent=Panel}) ->
110    wxPanel:destroy(Panel),
111    {stop, normal, ok, State};
112
113handle_call(Msg, _From, State) ->
114    demo:format(State#state.config, "Got Call ~p\n", [Msg]),
115    {reply,{error, nyi}, State}.
116
117handle_cast(Msg, State) ->
118    io:format("Got cast ~p~n",[Msg]),
119    {noreply,State}.
120
121code_change(_, _, State) ->
122    {stop, ignore, State}.
123
124terminate(_Reason, _State) ->
125    ok.
126
127%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128%% Local functions
129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
131