<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<font size="-1">Hi,<br>
<br>
I'm having some trouble using wxListCtrl. I've created a table and
filled it with data. When a row is activated (2-click) I receive
an index into the table: the row number. I want to read (some of)
the data from that row, but I can't figure out how.<br>
<br>
To me it appears as if wxListCtrl:getItem/1 doesn't read the data
into the Item. Below is a minimal example. Where do I go wrong?<br>
<br>
Regs,<br>
Joakim<br>
<br>
-module(test_gui).<br>
<br>
-behaviour(wx_object).<br>
<br>
%% Client API<br>
-export([start/0]).<br>
<br>
%% wx_object callbacks<br>
-export([init/1, terminate/2, code_change/3,<br>
handle_info/2, handle_call/3, handle_cast/2, handle_event/2,<br>
handle_sync_event/3]).<br>
<br>
-include_lib("wx/include/wx.hrl").<br>
<br>
-record(state, <br>
{<br>
parent<br>
}).<br>
<br>
<br>
start() -><br>
Wx = wx:new(),<br>
Size = {size, {500, 500}},<br>
Frame = wxFrame:new(Wx, ?wxID_ANY, "Title", [Size]),<br>
Config = [{parent, Frame}],<br>
wx_object:start_link(?MODULE, Config, []).<br>
<br>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br>
init(Config) -><br>
wx:batch(fun() -> do_init(Config) end).<br>
<br>
do_init(Config) -><br>
Parent = proplists:get_value(parent, Config),<br>
<br>
% Create panel and set up sizer.<br>
Panel = wxPanel:new(Parent, []),<br>
MainSizer = wxBoxSizer:new(?wxVERTICAL),<br>
wxPanel:setSizer(Panel, MainSizer),<br>
<br>
LC = wxListCtrl:new(Panel, [{style, ?wxLC_REPORT bor
?wxLC_SINGLE_SEL}]),<br>
wxSizer:add(MainSizer, LC, [{flag, ?wxEXPAND}]),<br>
<br>
</font><font size="-1"><font size="-1"> % Create columns<br>
</font> wxListCtrl:insertColumn(LC, 0, "name", []),<br>
wxListCtrl:insertColumn(LC, 1, "address", []),<br>
<br>
% Create the row<br>
wxListCtrl:insertItem(LC, 0, ""),<br>
<br>
% Write cell 0,0<br>
wxListCtrl:setItem(LC, 0, 0, "Bob"),<br>
<br>
% Write cell 0,1<br>
Item1 = wxListItem:new(),<br>
wxListItem:setId(Item1, 0),<br>
wxListItem:setColumn(Item1, 1),<br>
wxListItem:setText(Item1, "Street"),<br>
true = wxListCtrl:setItem(LC, Item1),<br>
<br>
wxFrame:show(Parent),<br>
<br>
% Read 0,0<br>
It0 = wxListItem:new(),<br>
wxListItem:setId(It0, 0),<br>
wxListItem:setColumn(It0, 0),<br>
true = wxListCtrl:getItem(LC, It0),<br>
<br>
io:format("Row 0: ~p~n", [wxListItem:getId(It0)]),<br>
io:format("Col 0: ~p~n", [wxListItem:getColumn(It0)]),<br>
io:format("getText: ~p~n", [wxListItem:getText(It0)]),<br>
<br>
% Read 0,1<br>
It1 = wxListItem:new(),<br>
wxListItem:setId(It1, 0),<br>
wxListItem:setColumn(It1, 1),<br>
true = wxListCtrl:getItem(LC, It1),<br>
<br>
io:format("Row 0: ~p~n", [wxListItem:getId(It1)]),<br>
io:format("Col 1: ~p~n", [wxListItem:getColumn(It1)]),<br>
io:format("getText: ~p~n", [wxListItem:getText(It1)]),<br>
<br>
wxWindow:connect(LC, command_list_item_activated),<br>
<br>
{Parent, #state{parent = Parent}}.<br>
<br>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br>
%% Sync events i.e. from callbacks must return ok, it can not
return a<br>
%% new state. Do the redrawing here.<br>
handle_sync_event(#wx{event = #wxPaint{}},_,<br>
#state{}) -><br>
ok.<br>
<br>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br>
%% Callbacks handled as normal gen_server callbacks<br>
%%<br>
<br>
handle_event(#wx{obj = LC,<br>
event = #wxList{type = command_list_item_activated,
itemIndex = Index}},<br>
State) -><br>
<br>
It0 = wxListItem:new(),<br>
wxListItem:setId(It0, Index),<br>
wxListItem:setColumn(It0, 0),<br>
true = wxListCtrl:getItem(LC, It0),<br>
<br>
io:format("Row 0: ~p~n", [wxListItem:getId(It0)]),<br>
io:format("Col 0: ~p~n", [wxListItem:getColumn(It0)]),<br>
io:format("getText: ~p~n", [wxListItem:getText(It0)]),<br>
<br>
It1 = wxListItem:new(),<br>
wxListItem:setId(It1, Index),<br>
wxListItem:setColumn(It1, 1),<br>
true = wxListCtrl:getItem(LC, It1),<br>
<br>
io:format("Row 0: ~p~n", [wxListItem:getId(It1)]),<br>
io:format("Col 1: ~p~n", [wxListItem:getColumn(It1)]),<br>
io:format("getText: ~p~n", [wxListItem:getText(It1)]),<br>
<br>
{noreply, State};<br>
<br>
%% Unknown event.<br>
%%<br>
handle_event(Ev = #wx{}, State) -><br>
io:format("Got event:~p\n", [Ev]),<br>
{noreply, State}.<br>
<br>
handle_info(Msg, State) -><br>
io:format("Got Info ~p\n", [Msg]),<br>
{noreply, State}.<br>
<br>
handle_call(shutdown, _From, State=#state{parent=Parent}) -><br>
io:format("Shutdown~n", []),<br>
wxPanel:destroy(Parent),<br>
{stop, normal, ok, State};<br>
<br>
handle_call(Msg, _From, State) -><br>
io:format("Got Call ~p\n", [Msg]),<br>
{reply, {error, unknown_call}, State}.<br>
<br>
handle_cast(Msg, State) -><br>
io:format("Got cast ~p~n",[Msg]),<br>
{noreply, State}.<br>
<br>
code_change(_, _, State) -><br>
{stop, ignore, State}.<br>
<br>
terminate(_Reason, _State) -><br>
io:format("Stopping~n", []),<br>
wx:destroy(),<br>
ok.<br>
<br>
<br>
</font>
</body>
</html>