[erlang-questions] Reading data from wxListCtrl
Joakim Hirsch
joakim@REDACTED
Thu Oct 2 10:30:46 CEST 2014
I found the answer.
To be able to read back data from a wxListCtrl, using
wxListCtrl:getItem/2 you need to set the item mask using
wxListItem:setMask/2. This is (indirectly) documented under getMask/1 in
the external docs. To get the text you should call:
wxListItem:new/0
wxListItem:setId/2
wxListItem:setColumn/2
wxListItem:setMask/2, with mask at least containing ?wxLIST_MASK_TEXT
wxListCtrl:getItem/2
wxListItem:destroy/1
/Joakim
Joakim Hirsch skrev 2014-09-30 11:29:
> Hi,
>
> 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.
>
> 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?
>
> Regs,
> Joakim
>
> -module(test_gui).
>
> -behaviour(wx_object).
>
> %% Client API
> -export([start/0]).
>
> %% wx_object callbacks
> -export([init/1, terminate/2, code_change/3,
> handle_info/2, handle_call/3, handle_cast/2, handle_event/2,
> handle_sync_event/3]).
>
> -include_lib("wx/include/wx.hrl").
>
> -record(state,
> {
> parent
> }).
>
>
> start() ->
> Wx = wx:new(),
> Size = {size, {500, 500}},
> Frame = wxFrame:new(Wx, ?wxID_ANY, "Title", [Size]),
> Config = [{parent, Frame}],
> wx_object:start_link(?MODULE, Config, []).
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> init(Config) ->
> wx:batch(fun() -> do_init(Config) end).
>
> do_init(Config) ->
> Parent = proplists:get_value(parent, Config),
>
> % Create panel and set up sizer.
> Panel = wxPanel:new(Parent, []),
> MainSizer = wxBoxSizer:new(?wxVERTICAL),
> wxPanel:setSizer(Panel, MainSizer),
>
> LC = wxListCtrl:new(Panel, [{style, ?wxLC_REPORT bor
> ?wxLC_SINGLE_SEL}]),
> wxSizer:add(MainSizer, LC, [{flag, ?wxEXPAND}]),
>
> % Create columns
> wxListCtrl:insertColumn(LC, 0, "name", []),
> wxListCtrl:insertColumn(LC, 1, "address", []),
>
> % Create the row
> wxListCtrl:insertItem(LC, 0, ""),
>
> % Write cell 0,0
> wxListCtrl:setItem(LC, 0, 0, "Bob"),
>
> % Write cell 0,1
> Item1 = wxListItem:new(),
> wxListItem:setId(Item1, 0),
> wxListItem:setColumn(Item1, 1),
> wxListItem:setText(Item1, "Street"),
> true = wxListCtrl:setItem(LC, Item1),
>
> wxFrame:show(Parent),
>
> % Read 0,0
> It0 = wxListItem:new(),
> wxListItem:setId(It0, 0),
> wxListItem:setColumn(It0, 0),
> true = wxListCtrl:getItem(LC, It0),
>
> io:format("Row 0: ~p~n", [wxListItem:getId(It0)]),
> io:format("Col 0: ~p~n", [wxListItem:getColumn(It0)]),
> io:format("getText: ~p~n", [wxListItem:getText(It0)]),
>
> % Read 0,1
> It1 = wxListItem:new(),
> wxListItem:setId(It1, 0),
> wxListItem:setColumn(It1, 1),
> true = wxListCtrl:getItem(LC, It1),
>
> io:format("Row 0: ~p~n", [wxListItem:getId(It1)]),
> io:format("Col 1: ~p~n", [wxListItem:getColumn(It1)]),
> io:format("getText: ~p~n", [wxListItem:getText(It1)]),
>
> wxWindow:connect(LC, command_list_item_activated),
>
> {Parent, #state{parent = Parent}}.
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> %% Sync events i.e. from callbacks must return ok, it can not return a
> %% new state. Do the redrawing here.
> handle_sync_event(#wx{event = #wxPaint{}},_,
> #state{}) ->
> ok.
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> %% Callbacks handled as normal gen_server callbacks
> %%
>
> handle_event(#wx{obj = LC,
> event = #wxList{type = command_list_item_activated, itemIndex
> = Index}},
> State) ->
>
> It0 = wxListItem:new(),
> wxListItem:setId(It0, Index),
> wxListItem:setColumn(It0, 0),
> true = wxListCtrl:getItem(LC, It0),
>
> io:format("Row 0: ~p~n", [wxListItem:getId(It0)]),
> io:format("Col 0: ~p~n", [wxListItem:getColumn(It0)]),
> io:format("getText: ~p~n", [wxListItem:getText(It0)]),
>
> It1 = wxListItem:new(),
> wxListItem:setId(It1, Index),
> wxListItem:setColumn(It1, 1),
> true = wxListCtrl:getItem(LC, It1),
>
> io:format("Row 0: ~p~n", [wxListItem:getId(It1)]),
> io:format("Col 1: ~p~n", [wxListItem:getColumn(It1)]),
> io:format("getText: ~p~n", [wxListItem:getText(It1)]),
>
> {noreply, State};
>
> %% Unknown event.
> %%
> handle_event(Ev = #wx{}, State) ->
> io:format("Got event:~p\n", [Ev]),
> {noreply, State}.
>
> handle_info(Msg, State) ->
> io:format("Got Info ~p\n", [Msg]),
> {noreply, State}.
>
> handle_call(shutdown, _From, State=#state{parent=Parent}) ->
> io:format("Shutdown~n", []),
> wxPanel:destroy(Parent),
> {stop, normal, ok, State};
>
> handle_call(Msg, _From, State) ->
> io:format("Got Call ~p\n", [Msg]),
> {reply, {error, unknown_call}, State}.
>
> handle_cast(Msg, State) ->
> io:format("Got cast ~p~n",[Msg]),
> {noreply, State}.
>
> code_change(_, _, State) ->
> {stop, ignore, State}.
>
> terminate(_Reason, _State) ->
> io:format("Stopping~n", []),
> wx:destroy(),
> ok.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141002/a06ae23c/attachment.htm>
More information about the erlang-questions
mailing list