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_listCtrl).
21
22-include_lib("wx/include/wx.hrl").
23
24-behaviour(wx_object).
25
26-export([start/1, init/1, terminate/2,  code_change/3,
27	 handle_info/2, handle_call/3, handle_cast/2, handle_event/2]).
28
29-record(state,
30	{
31	  parent,
32	  config,
33	  notebook
34	 }).
35
36start(Config) ->
37    wx_object:start_link(?MODULE, Config, []).
38
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40init(Config) ->
41        wx:batch(fun() -> do_init(Config) end).
42
43do_init(Config) ->
44    Parent = proplists:get_value(parent, Config),
45    Panel = wxPanel:new(Parent, []),
46
47    %% Setup sizers
48    MainSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
49				     [{label, "wxListCtrl"}]),
50
51    Notebook = wxNotebook:new(Panel, 1, [{style, ?wxBK_DEFAULT}]),
52
53
54    ListCtrl1 = wxListCtrl:new(Notebook, [{style, ?wxLC_LIST}]),
55    [wxListCtrl:insertItem(ListCtrl1, Int, "Item "++integer_to_list(Int)) ||
56	Int <- lists:seq(0,50)],
57    ListCtrl2 = create_list_ctrl(Notebook, [{style, ?wxLC_REPORT bor
58					     ?wxLC_SINGLE_SEL}]),
59    IL = wxImageList:new(16,16),
60    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_COPY", [{size, {16,16}}])),
61    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_MISSING_IMAGE", [{size, {16,16}}])),
62    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_QUESTION", [{size, {16,16}}])),
63    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_WARNING", [{size, {16,16}}])),
64    wxListCtrl:assignImageList(ListCtrl2, IL, ?wxIMAGE_LIST_SMALL),
65    Fun =
66	fun(Item) ->
67		case Item rem 4 of
68		    0 ->
69			wxListCtrl:setItemBackgroundColour(ListCtrl2, Item, {240,240,240,255}),
70			wxListCtrl:setItemImage(ListCtrl2, Item, 0);
71		    1 -> wxListCtrl:setItemImage(ListCtrl2, Item, 1);
72		    2 -> wxListCtrl:setItemImage(ListCtrl2, Item, 2),
73			 wxListCtrl:setItemBackgroundColour(ListCtrl2, Item, {240,240,240,255});
74		    _ -> wxListCtrl:setItemImage(ListCtrl2, Item, 3)
75		end
76	end,
77    wx:foreach(Fun, lists:seq(0,50)),
78
79    ListCtrl3 = create_list_ctrl(Notebook, [{style, ?wxLC_REPORT}]),
80    wxListCtrl:setTextColour(ListCtrl3, ?wxBLUE),
81    wxListCtrl:setItemBackgroundColour(ListCtrl3,5,?wxRED),
82    wxListCtrl:setItemBackgroundColour(ListCtrl3,3,?wxGREEN),
83    wxListCtrl:setItemBackgroundColour(ListCtrl3,0,?wxCYAN),
84
85    IA = wxListItemAttr:new(),
86    wxListItemAttr:setTextColour(IA, {190, 25, 25}),
87    LC4Opts = [{style, ?wxLC_REPORT bor ?wxLC_VIRTUAL},
88	       {onGetItemText, fun(_This, Item, 0) ->
89				       "Row " ++ integer_to_list(Item);
90				  (_, Item, 1) when Item rem 5 == 0 ->
91				       "Column 2";
92				  (_, _, _) -> ""
93			       end},
94	       {onGetItemAttr, fun(_This, Item) when Item rem 3 == 0 ->
95				       IA;
96				  (_This, _Item)  ->
97				       wx:typeCast(wx:null(), wxListItemAttr)
98			       end},
99	       {onGetItemColumnImage, fun(_This, Item, 1) ->
100					      Item rem 4;
101					 (_, _, _) ->
102					      -1
103				      end}
104	      ],
105    ListCtrl4 = wxListCtrl:new(Notebook, LC4Opts),
106    wxListCtrl:setImageList(ListCtrl4, IL, ?wxIMAGE_LIST_SMALL),
107
108    wxListCtrl:insertColumn(ListCtrl4, 0, "Column 1"),
109    wxListCtrl:insertColumn(ListCtrl4, 1, "Column 2"),
110    wxListCtrl:setColumnWidth(ListCtrl4, 0, 200),
111    wxListCtrl:setColumnWidth(ListCtrl4, 1, 200),
112    wxListCtrl:setItemCount(ListCtrl4, 1000000),
113
114
115    wxListCtrl:connect(ListCtrl1, command_list_item_selected, []),
116    wxListCtrl:connect(ListCtrl2, command_list_item_selected, []),
117    wxListCtrl:connect(ListCtrl3, command_list_item_selected, []),
118    wxListCtrl:connect(ListCtrl4, command_list_item_selected, []),
119
120    %% Add to sizers
121    wxNotebook:addPage(Notebook, ListCtrl1, "List", []),
122    wxNotebook:addPage(Notebook, ListCtrl2, "Report", []),
123    wxNotebook:addPage(Notebook, ListCtrl3, "Colored multiselect", []),
124    wxNotebook:addPage(Notebook, ListCtrl4, "Virtual Report", []),
125
126    wxSizer:add(MainSizer, Notebook, [{proportion, 1},
127				      {flag, ?wxEXPAND}]),
128
129    wxPanel:setSizer(Panel, MainSizer),
130    {Panel, #state{parent=Panel, config=Config,
131		   notebook = Notebook}}.
132
133
134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135%% Async Events are handled in handle_event as in handle_info
136handle_event(#wx{obj = _ListCtrl,
137		 event = #wxList{itemIndex = Item}},
138	     State = #state{}) ->
139    demo:format(State#state.config,"Item ~p selected.\n",[Item]),
140    {noreply,State}.
141
142%% Callbacks handled as normal gen_server callbacks
143handle_info(Msg, State) ->
144    demo:format(State#state.config, "Got Info ~p\n",[Msg]),
145    {noreply,State}.
146
147handle_call(shutdown, _From, State=#state{parent=Panel}) ->
148    wxPanel:destroy(Panel),
149    {stop, normal, ok, State};
150
151handle_call(Msg, _From, State) ->
152    demo:format(State#state.config,"Got Call ~p\n",[Msg]),
153    {reply,ok,State}.
154
155handle_cast(Msg, State) ->
156    io:format("Got cast ~p~n",[Msg]),
157    {noreply,State}.
158
159
160code_change(_, _, State) ->
161    {stop, ignore, State}.
162
163terminate(_Reason, _State) ->
164    ok.
165
166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167%%%% Local functions
168%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169-define(FIRST_COL, 0).
170-define(SECOND_COL, 1).
171-define(THIRD_COL, 2).
172
173create_list_ctrl(Win, Options) ->
174    ListCtrl = wxListCtrl:new(Win, Options),
175    wxListCtrl:insertColumn(ListCtrl, ?FIRST_COL, "First Col", []),
176    wxListCtrl:insertColumn(ListCtrl, ?SECOND_COL, "Second Col", []),
177    wxListCtrl:insertColumn(ListCtrl, ?THIRD_COL, "Third Col", []),
178    Fun =
179	fun(Int) ->
180		Name = integer_to_list(Int),
181		wxListCtrl:insertItem(ListCtrl, Int, ""),
182		wxListCtrl:setItem(ListCtrl, Int, ?FIRST_COL, "First "++Name),
183		wxListCtrl:setItem(ListCtrl, Int, ?SECOND_COL, "Second "++Name),
184		wxListCtrl:setItem(ListCtrl, Int, ?THIRD_COL, "Third "++Name)
185	end,
186    wx:foreach(Fun, lists:seq(0,50)),
187
188    ListCtrl.
189
190
191