~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to lib/wx/examples/demo/ex_listCtrl.erl

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-08-05 20:54:29 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090805205429-pm4pnwew8axraosl
Tags: 1:13.b.1-dfsg-5
* Fixed parentheses in Emacs mode (closes: #536891).
* Removed unnecessary conflicts with erlang-manpages package.
* Added workaround for #475459: disabled threads on sparc architecture.
  This breaks wxErlang, so it's only a temporary solution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%%
 
2
%% %CopyrightBegin%
 
3
%% 
 
4
%% Copyright Ericsson AB 2009. All Rights Reserved.
 
5
%% 
 
6
%% The contents of this file are subject to the Erlang Public License,
 
7
%% Version 1.1, (the "License"); you may not use this file except in
 
8
%% compliance with the License. You should have received a copy of the
 
9
%% Erlang Public License along with this software. If not, it can be
 
10
%% retrieved online at http://www.erlang.org/.
 
11
%% 
 
12
%% Software distributed under the License is distributed on an "AS IS"
 
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
14
%% the License for the specific language governing rights and limitations
 
15
%% under the License.
 
16
%% 
 
17
%% %CopyrightEnd%
 
18
 
 
19
-module(ex_listCtrl).
 
20
 
 
21
-include_lib("wx/include/wx.hrl").
 
22
 
 
23
-behavoiur(wx_object).
 
24
 
 
25
-export([start/1, init/1, terminate/2,  code_change/3,
 
26
         handle_info/2, handle_call/3, handle_event/2]).
 
27
 
 
28
-record(state, 
 
29
        {
 
30
          parent,
 
31
          config,
 
32
          notebook
 
33
         }).
 
34
 
 
35
start(Config) ->
 
36
    wx_object:start_link(?MODULE, Config, []).
 
37
 
 
38
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
39
init(Config) ->
 
40
        wx:batch(fun() -> do_init(Config) end).
 
41
do_init(Config) ->
 
42
    Parent = proplists:get_value(parent, Config),  
 
43
    Panel = wxPanel:new(Parent, []),
 
44
 
 
45
    %% Setup sizers
 
46
    MainSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel, 
 
47
                                     [{label, "wxListCtrl"}]),
 
48
 
 
49
    Notebook = wxNotebook:new(Panel, 1, [{style, ?wxBK_DEFAULT}]),
 
50
 
 
51
 
 
52
    ListCtrl1 = wxListCtrl:new(Notebook, [{style, ?wxLC_LIST}]),
 
53
    [wxListCtrl:insertItem(ListCtrl1, Int, "Item "++integer_to_list(Int)) ||
 
54
        Int <- lists:seq(0,50)],
 
55
    ListCtrl2 = create_list_ctrl(Notebook, [{style, ?wxLC_REPORT bor
 
56
                                             ?wxLC_SINGLE_SEL}]),
 
57
    IL = wxImageList:new(16,16),
 
58
    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_COPY", [{size, {16,16}}])),
 
59
    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_MISSING_IMAGE", [{size, {16,16}}])),
 
60
    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_TICK_MARK", [{size, {16,16}}])),
 
61
    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_CROSS_MARK", [{size, {16,16}}])),
 
62
    wxListCtrl:assignImageList(ListCtrl2, IL, ?wxIMAGE_LIST_SMALL),
 
63
    Fun =
 
64
        fun(Item) ->
 
65
                case Item rem 4 of
 
66
                    0 ->
 
67
                        wxListCtrl:setItemBackgroundColour(ListCtrl2, Item, {240,240,240,255}),
 
68
                        wxListCtrl:setItemImage(ListCtrl2, Item, 0);
 
69
                    1 -> wxListCtrl:setItemImage(ListCtrl2, Item, 1);
 
70
                    2 -> wxListCtrl:setItemImage(ListCtrl2, Item, 2),
 
71
                         wxListCtrl:setItemBackgroundColour(ListCtrl2, Item, {240,240,240,255});
 
72
                    _ -> wxListCtrl:setItemImage(ListCtrl2, Item, 3)
 
73
                end
 
74
        end,
 
75
    wx:foreach(Fun, lists:seq(0,50)),
 
76
 
 
77
    ListCtrl3 = create_list_ctrl(Notebook, [{style, ?wxLC_REPORT}]),
 
78
    wxListCtrl:setTextColour(ListCtrl3, ?wxBLUE),
 
79
    wxListCtrl:setItemBackgroundColour(ListCtrl3,5,?wxRED),
 
80
    wxListCtrl:setItemBackgroundColour(ListCtrl3,3,?wxGREEN),
 
81
    wxListCtrl:setItemBackgroundColour(ListCtrl3,0,?wxCYAN),
 
82
 
 
83
    wxNotebook:addPage(Notebook, ListCtrl1, "List", []),
 
84
    wxNotebook:addPage(Notebook, ListCtrl2, "Report", []),
 
85
    wxNotebook:addPage(Notebook, ListCtrl3, "Colored multiselect", []),
 
86
 
 
87
    wxListCtrl:connect(ListCtrl1, command_list_item_selected, []),
 
88
    wxListCtrl:connect(ListCtrl2, command_list_item_selected, []),
 
89
    wxListCtrl:connect(ListCtrl3, command_list_item_selected, []),
 
90
    %% Add to sizers
 
91
    wxSizer:add(MainSizer, Notebook, [{proportion, 1},
 
92
                                      {flag, ?wxEXPAND}]),
 
93
 
 
94
    wxPanel:setSizer(Panel, MainSizer),
 
95
    {Panel, #state{parent=Panel, config=Config,
 
96
                   notebook = Notebook}}.
 
97
 
 
98
 
 
99
%%%%%%%%%%%%
 
100
%% Callbacks handled as normal gen_server callbacks
 
101
handle_info(Msg, State) ->
 
102
    demo:format(State#state.config, "Got Info ~p\n",[Msg]),
 
103
    {noreply,State}.
 
104
 
 
105
handle_call(Msg, _From, State) ->
 
106
    demo:format(State#state.config,"Got Call ~p\n",[Msg]),
 
107
    {reply,ok,State}.
 
108
 
 
109
%% Async Events are handled in handle_event as in handle_info
 
110
handle_event(#wx{obj = _ListCtrl,
 
111
                 event = #wxList{itemIndex = Item}},
 
112
             State = #state{}) ->
 
113
    demo:format(State#state.config,"Item ~p selected.\n",[Item]),
 
114
    {noreply,State};
 
115
handle_event(Ev = #wx{}, State = #state{}) ->
 
116
    demo:format(State#state.config,"Got Event ~p\n",[Ev]),
 
117
    {noreply,State}.
 
118
 
 
119
code_change(_, _, State) ->
 
120
    {stop, ignore, State}.
 
121
 
 
122
terminate(_Reason, _State) ->
 
123
    ok.
 
124
 
 
125
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
126
%%%% Local functions
 
127
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
128
-define(FIRST_COL, 0).
 
129
-define(SECOND_COL, 1).
 
130
-define(THIRD_COL, 2).
 
131
 
 
132
create_list_ctrl(Win, Options) ->
 
133
    ListCtrl = wxListCtrl:new(Win, Options),
 
134
    wxListCtrl:insertColumn(ListCtrl, ?FIRST_COL, "First Col", []),
 
135
    wxListCtrl:insertColumn(ListCtrl, ?SECOND_COL, "Second Col", []),
 
136
    wxListCtrl:insertColumn(ListCtrl, ?THIRD_COL, "Third Col", []),
 
137
    Fun =
 
138
        fun(Int) ->
 
139
                Name = integer_to_list(Int),
 
140
                wxListCtrl:insertItem(ListCtrl, Int, ""),
 
141
                wxListCtrl:setItem(ListCtrl, Int, ?FIRST_COL, "First "++Name),
 
142
                wxListCtrl:setItem(ListCtrl, Int, ?SECOND_COL, "Second "++Name),
 
143
                wxListCtrl:setItem(ListCtrl, Int, ?THIRD_COL, "Third "++Name)
 
144
        end,
 
145
    wx:foreach(Fun, lists:seq(0,50)),
 
146
 
 
147
    ListCtrl.
 
148
 
 
149