wxErlang

wxEvtHandler.erl

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
%%
%%       |_¯ \ / ¯|¯ | |  /\  |\ | |¯\ |   |_¯ |¯\                 
%%  W  X |__  V   |  |¯| /¯¯\ | \| |_/ |__ |__ |¯\  . E  R  L  
%%———————————————————————————————————————————————————————————
%%
%%   Copyright Ericsson AB 2008-2013. All Rights Reserved.
%%
%%——————————————————————————————————————————————
%% Whitespace Beautified by ScriptCulture © 2018
%% Sit Back · Feet Up · Learn wxErlang
%% For use as a reference only
%% www.scriptculture.com
%% Not check-summed 
%% wx 1.8 —————————————————————————————————————— 
%%
%% Licensed under the Apache License, 
%% Version 2.0 (the "License"); you may 
%% not use this file except in compliance 
%% with the License. You may obtain a copy 
%% of the License at:
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by 
%% applicable law or agreed to in writing, software
%% distributed under the License is distributed 
%% on an "AS IS" BASIS, WITHOUT WARRANTIES 
%% OR CONDITIONS OF ANY KIND, either 
%% express or implied. See the 
%% License for the specific 
%% language governing 
%% permissions and
%% limitations 
%% under the 
%% License.
%%%%%%%
%%%%
%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% This module is actually handwritten see ../api_gen/wx_extra/wxEvtHandler.erl
%%
%% @doc The Event handler.
%%
%% To get events from wxwidgets objects you subscribe to them by
%% calling connect/[2-3].  Events are sent as messages, if no callback
%% was supplied  These messages will be {@link wx(). #wx{}} where
%% EventRecord is a record that depends on the {@link
%% wxEventType(). event type}.  The records are defined in:
%% wx/include/wx.hrl.
%%
%% If a callback was supplied to connect, the callback will be invoked
%% (in another process) to handle the event. The callback should be of
%% arity 2.  
%%              fun (  EventRecord::wx() , EventObject::wxObject()  ).
%%
%% Beware that the callback will be in executed in new process each time.
%%
%% http://www.wxwidgets.org/manuals/stable/wx_wxevthandler.html
%% —The orginal documentation
%%%%%   
%%
%%   OVERVIEW
%%   ––––––––
%%   A class that can handle events from the windowing system. 
%%   wxWindow (and therefore all window classes) are derived from this class.
%%
%%   When events are received, wxEvtHandler 
%%   invokes the method listed in the event 
%%   table using itself as the object. 
%%
%%   When using multiple inheritance it is 
%%   imperative that the wxEvtHandler(-derived) class 
%%   be the first class inherited such that the "this" 
%%   pointer for the overall object will be identical to the 
%%   "this" pointer for the wxEvtHandler portion.
%%   
%%   RC Note: The whole callback was very confusing for, well quite some time, until 
%%            I realized that each connect call using the callback/anonymous-function
%%            method simply adds a entry onto a stack. This stack is traversed top-down
%%            when the computer is looking for a matching event, hence the 'last subscibed
%%            entry first' bit. The Event:skip() strategies discussed pertain to reading
%%            of this stack. 
%%
%%   See Also:
%%   —————————
%%   
%%   
%%   
-module(wxEvtHandler).
-include("wxe.hrl").
-include("../include/wx.hrl").

%% API
-export([  
                               connect / 2 ,

                             connect / 3 ,

                        disconnect / 1 ,

                      disconnect / 2 ,

                    disconnect / 3
                                                ]).




%% internal exports
-export([ connect_impl/2, disconnect_impl/2 ]).

-export_type([wxEvtHandler/0, wx/0, event/0]).

-type wxEvtHandler() :: wx:wx_object().



           %·%% CONNECT / 2 %%·%

%%
%%
%% @doc Equivalent to {@link connect/3. connect(This, EventType, [])}
            -spec connect(     This :: wxEvtHandler(),
                          EventType ::  wxEventType()   ) ->  ok.

connect(This, EventType)                                  ->  connect(This, EventType, []).





           %·%% CONNECT / 3 %%·%

%%
%%
%% @doc This function subscribes the to events of EventType,
%% in the range id, lastId. The events will be received as messages 
%% if no callback is supplied.
%%
%%
%% Options: 
%%
%%    {id, integer()},       The identifier (or first of the identifier range) to be 
%%                           associated with this event handler. 
%%                           Default ?wxID_ANY
%%
%%    {lastId, integer()},   The second part of the identifier range. 
%%                           If used 'id' must be set as the starting identifier range.
%%                           Default ?wxID_ANY
%%
%%    {skip,  boolean()},    If skip is true further event_handlers will be called.
%%                           This is not used if the 'callback' option is used. 
%%                           Default false.
%%
%%    {callback, function()} Use a callback fun(EventRecord::wx(), EventObject::wxObject()) 
%%                           to process the event. 
%%                              ! Default not specfied !  
%%                              i.e. a message will be delivered to the process calling this function.
%%                              which is confusing as $#!^, as, it basically means nothing.
%%
%%    {userData, term()}     An erlang term that will be sent with the event. Default: [].


             -spec connect(This      :: wxEvtHandler(),
                           EventType ::  wxEventType(),
                          [Option])                    ->  ok
              when
              Option :: { id,        integer() } 
                      | { lastId,    integer() } 
                      | { skip,      boolean() } 
                      |   callback                % business as usual, no 
                      | { callback, function() } 
                      | { userData,     term() }.

connect( This = #wx_ref{type=ThisT} ,
         EventType ,
         Options   )          ->

    EvH = parse_opts(Options, #evh{et=EventType}),
              ?CLASS(ThisT,wxEvtHandler),
            case
                wxe_util:connect_cb(This, EvH)
                of
                   ok                   ->                ok;
                  {badarg, event_type}  ->  erlang:error({badarg,EventType})
            end.


parse_opts([{callback,Fun}|R], Opts)
            when
            is_function(Fun)   ->
                                         parse_opts(R, Opts#evh{ cb=Fun    });

    %% Check Fun Arity?
parse_opts([{callback,CB={nospawn, Fun}}|R], Opts)
            when
            is_function(Fun)   ->
                                         parse_opts(R, Opts#evh{ cb=CB     });

parse_opts([callback|R], Opts) ->
                                         parse_opts(R, Opts#evh{ cb=self() });

parse_opts([{userData, UserData}|R],Opts) ->
                                         parse_opts(R, Opts#evh{ userdata=UserData });

parse_opts([{skip, Skip}|R],Opts)
            when
            is_boolean(Skip)   ->
                                         parse_opts(R, Opts#evh{ skip=Skip });

parse_opts([{id, Id}|R],Opts)
            when
            is_integer(Id)     ->
                                         parse_opts(R, Opts#evh{ id=Id     });

parse_opts([{lastId, Id}|R],Opts)
            when 
            is_integer(Id)     ->
                                         parse_opts(R, Opts#evh{ lastId=Id });

parse_opts([_BadArg|R], Opts)  ->
                                         parse_opts(R, Opts);


parse_opts([], Opts = #evh{id     = Id,
                           lastId = Lid,
                           skip   = Skip,
                           cb     = CB   })  -> 
             if 

       Skip =/= undefined
            andalso
         CB =/= 0           ->  erlang:error({badarg, {combined, skip, callback}});

        Lid =/= ?wxID_ANY
            andalso 
         Id =:= ?wxID_ANY   ->  erlang:error({badarg, no_start_identifier_range});

       Skip =:= undefined   ->  Opts#evh{skip=false};  %% Default
                     
                       true ->  Opts
            end.




%%% DISCONNECT LITERATURE %%%
%%  
%%   Disconnects the given function dynamically from the event handler, 
%%   using the specified parameters as search criteria and returning 
%%   true if a matching function has been found and removed. 
%%   This method can only disconnect functions which have 
%%   been added using the wxEvtHandler::Connect method. 
%%   There is no way to disconnect functions connected
%%   using the (static) event tables.




           %·%% DISCONNECT / 1 %%·%

%%
%%
%% @doc Equivalent to {@link disconnect/3. disconnect(This, null, [])}
%% Can also have an optional callback Fun() as an additional last argument.
            -spec disconnect(This :: wxEvtHandler()) -> boolean().

disconnect(This=#wx_ref{type=ThisT,ref=_ThisRef}) ->
                                                   ?CLASS(ThisT,wxEvtHandler),
                                                    disconnect(This, null, []).






           %·%% DISCONNECT %%·%

%%
%%
%% @doc Equivalent to {@link disconnect/3. disconnect(This, EventType, [])}
            -spec disconnect(This      :: wxEvtHandler(),
                             EventType ::  wxEventType()) -> boolean().

disconnect(This=#wx_ref{type=ThisT,ref=_ThisRef},
           EventType)
             when
             is_atom(EventType) ->
                                 ?CLASS(ThisT,wxEvtHandler),
                                 disconnect(This, EventType, []).





           %·%% DISCONNECT %%·%

%%   
%%   
%%   
%% Return Value:
%% See Also:
%%*%%*%%
%% This function unsubscribes the process or callback fun from the event handler.
%% EventType may be the atom 'null' to match any eventtype.
%% Notice that the options skip and userdata is not used to match the eventhandler.
            -spec disconnect(This      :: wxEvtHandler(),
                             EventType ::  wxEventType(),
                            [Option])  ->      boolean()
             when
             Option :: {id,        integer()}  %  The identifier (or first of the identifier range) 
                                               %  associated with the event handler function.
                     | {lastId,    integer()}  %  The second part of the identifier range associated 
                                               %  with the event handler function.
                     | {callback, function()}. %  The event handler function

disconnect(This=#wx_ref{type=ThisT,ref=_ThisRef},
           EventType, 
           Opts) ->
                  ?CLASS(ThisT,wxEvtHandler),
    
           EvH = parse_opts(Opts, #evh{et=EventType}),
             case wxe_util:disconnect_cb(This, EvH)
             of
                 {badarg, event_type} ->
                                            erlang:error({badarg,EventType});
                                 Bool ->
                                            Bool
    end.





%% @hidden
connect_impl(#wx_ref{type = ThisT,
                     ref  = ThisRef},
                                     #evh{ id       = Winid,
                                           lastId   = LastId,
                                           et       = EventType,
                                           skip     = Skip,
                                           userdata = Userdata,
                                           cb       = FunID})
        when
        is_integer(FunID)->

    EventTypeBin = list_to_binary([atom_to_list(EventType)|[0]]),
     ThisTypeBin = list_to_binary([atom_to_list( ThisT   )|[0]]),
              UD = if
                   Userdata =:= [] -> 0;
                              true -> wxe_util:send_bin(term_to_binary(Userdata)),
                                      1
                   end,
                   wxe_util:call(100, <<ThisRef:32/?UI,
                            Winid:32/?UI,LastId:32/?UI,
                            (wxe_util:from_bool(Skip)):32/?UI,
                            UD:32/?UI,
                            FunID:32/?UI,
                            (size(EventTypeBin)):32/?UI,
                            (size(ThisTypeBin)):32/?UI,
                            %% Note no alignment
                            EventTypeBin/binary,ThisTypeBin/binary>>).






%% @hidden
disconnect_impl(#wx_ref{type=_ThisT,
                         ref=ThisRef
                       },
                #evh{id=Winid, 
                 lastId=LastId, 
                     et=EventType,
                handler=#wx_ref{type=wxeEvtListener,ref=EvtList}}) 
                      ->
                        EventTypeBin = list_to_binary([atom_to_list(EventType)|[0]]),
                        wxe_util:call(101, <<EvtList:32/?UI,
                                ThisRef:32/?UI,Winid:32/?UI,LastId:32/?UI,
                                (size(EventTypeBin)):32/?UI,
                                %% Note no alignment
                                EventTypeBin/binary>>).