1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2011-2018. 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-module(cdv_table_wx).
20
21-behaviour(wx_object).
22
23-export([start_link/2]).
24%% wx_object callbacks
25-export([init/1, handle_info/2, terminate/2, code_change/3, handle_call/3,
26	 handle_event/2, handle_cast/2]).
27
28-include_lib("wx/include/wx.hrl").
29-include("observer_defs.hrl").
30
31%% Records
32-record(state,
33	{trunc_warn=[]}).
34
35start_link(ParentWin, Info) ->
36    wx_object:start_link(?MODULE, [ParentWin, Info], []).
37
38%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39
40init([ParentWin, Callback]) when is_atom(Callback) ->
41    {ok,TableInfo} = Callback:get_info(),
42    init([ParentWin, TableInfo]);
43
44init([ParentWin, {ColumnSpec,Info,TW}]) ->
45    Style0 = ?wxLC_REPORT bor ?wxLC_SINGLE_SEL bor ?wxLC_HRULES bor ?wxLC_VRULES,
46    Style =
47	case lists:all(fun({"",_,_}) -> true; (_) -> false end, ColumnSpec) of
48	    true -> Style0 bor ?wxLC_NO_HEADER;
49	    false -> Style0
50	end,
51    Grid = wxListCtrl:new(ParentWin, [{style, Style}]),
52    Li = wxListItem:new(),
53    Scale = observer_wx:get_scale(),
54    AddListEntry = fun({Name, Align, DefSize}, Col) ->
55			   wxListItem:setText(Li, Name),
56			   wxListItem:setAlign(Li, Align),
57			   wxListCtrl:insertColumn(Grid, Col, Li),
58			   wxListCtrl:setColumnWidth(Grid, Col, DefSize*Scale),
59			   Col + 1
60		   end,
61    lists:foldl(AddListEntry, 0, ColumnSpec),
62    wxListItem:destroy(Li),
63    Insert = fun(RowData, Row) ->
64		     wxListCtrl:insertItem(Grid, Row, ""),
65		     set_items(Grid,Row,RowData,0),
66		     Row + 1
67	     end,
68    lists:foldl(Insert, 0, Info),
69    {Grid, #state{trunc_warn=TW}}.
70
71%%%%%%%%%%%%%%%%%%%%%%% Callbacks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72
73handle_info(active, State) ->
74    cdv_wx:set_status(State#state.trunc_warn),
75    {noreply, State};
76
77handle_info(Info, State) ->
78    io:format("~p:~p: Unhandled info: ~tp~n", [?MODULE, ?LINE, Info]),
79    {noreply, State}.
80
81terminate(_Reason, _State) ->
82    ok.
83
84code_change(_, _, State) ->
85    {ok, State}.
86
87handle_call(Msg, _From, State) ->
88    io:format("~p~p: Unhandled Call ~tp~n",[?MODULE, ?LINE, Msg]),
89    {reply, ok, State}.
90
91handle_cast(Msg, State) ->
92    io:format("~p~p: Unhandled cast ~tp~n",[?MODULE, ?LINE, Msg]),
93    {noreply, State}.
94
95handle_event(Event, State) ->
96    io:format("~p:~p: Unhandled event ~tp\n", [?MODULE,?LINE,Event]),
97    {noreply, State}.
98
99%%%%%%%%%%%%%%%%%%%%%%% Internal %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100set_items(Grid,Row,[Col|Cols],ColN) ->
101    Str = case Col of
102	      undefined -> "";
103	      _ -> observer_lib:to_str(Col)
104	  end,
105    wxListCtrl:setItem(Grid, Row, ColN, Str),
106    set_items(Grid,Row,Cols,ColN+1);
107set_items(_,_,[],_) ->
108    ok.
109