wx_object — wx v2.4.3

Search documentation of wx

# View Source wx_object behaviour (wx v2.4.3)

wx_object - Generic wx object behaviour

This is a behaviour module that can be used for "sub classing" wx objects. It works like a regular gen_server module and creates a server per object.

NOTE: Currently no form of inheritance is implemented.

The user module should export:

init(Args) should return
{wxWindow, State} | {wxWindow, State, Timeout} | ignore | {stop, Reason}

Asynchronous window event handling:
handle_event(#wx{}, State) should return
{noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}

The user module can export the following callback functions:

handle_call(Msg, {From, Tag}, State) should return
{reply, Reply, State} | {reply, Reply, State, Timeout} | {noreply, State} | {noreply, State, Timeout} | {stop, Reason, Reply, State}

handle_cast(Msg, State) should return
{noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}

If the above are not exported but called, the wx_object process will crash. The user module can also export:

Info is message e.g. {'EXIT', P, R}, {nodedown, N}, ...
handle_info(Info, State) should return , ...
{noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}

If a message is sent to the wx_object process when handle_info is not exported, the message will be dropped and ignored.

When stop is returned in one of the functions above with Reason = normal | shutdown | Term, terminate(State) is called. It lets the user module clean up, it is always called when server terminates or when wx_object() in the driver is deleted. If the Parent process terminates the Module:terminate/2 function is called.
terminate(Reason, State)

Example:

      -module(myDialog).
      -export([new/2, show/1, destroy/1]).  %% API
      -export([init/1, handle_call/3, handle_event/2,
               handle_info/2, code_change/3, terminate/2]).
               new/2, showModal/1, destroy/1]).  %% Callbacks

      %% Client API
      new(Parent, Msg) ->
         wx_object:start(?MODULE, [Parent,Id], []).

      show(Dialog) ->
         wx_object:call(Dialog, show_modal).

      destroy(Dialog) ->
         wx_object:call(Dialog, destroy).

      %% Server Implementation ala gen_server
      init([Parent, Str]) ->
         Dialog = wxDialog:new(Parent, 42, "Testing", []),
         ...
         wxDialog:connect(Dialog, command_button_clicked),
         {Dialog, MyState}.

      handle_call(show, _From, State) ->
         wxDialog:show(State#state.win),
         {reply, ok, State};
      ...
      handle_event(#wx{}, State) ->
         io:format("Users clicked button~n",[]),
         {noreply, State};
      ...

## DATA TYPES

- request_id() = term()

- server_ref() = wx:wx_object() | atom() | pid()

# Summary

## Types

event()
request_id()
server_ref()

## Callbacks

code_change(OldVsn, State, Extra)
handle_call(Request, From, State)
handle_cast(Request, State)
handle_event(Request, State)
handle_info(Info, State)
handle_sync_event(Request, Ref, State)
init(Args)
terminate(Reason, State)

## Functions

call(Obj, Request)

Make a call to a wx_object server. The call waits until it gets a result. Invokes handle_call(Request, From, State) in the server

call(Obj, Request, Timeout)

Make a call to a wx_object server with a timeout. Invokes handle_call(Request, From, State) in server

cast(Obj, Request)

Make a cast to a wx_object server. Invokes handle_cast(Request, State) in the server

check_response(Msg, Key)

Check if a received message was a reply to a RequestId

get_pid(Obj)

Get the pid of the object handle.

reply(PidTag, Reply)

Get the pid of the object handle.

send_request(Obj, Request)

Make an send_request to a generic server. and return a RequestId which can/should be used with wait_response/[1|2]. Invokes handle_call(Request, From, State) in server.

set_pid(Obj, Pid)

Sets the controlling process of the object handle.

start(Name, Mod, Args, Options)

Starts a generic wx_object server and invokes Mod:init(Args) in the new process.

start_link(Mod, Args, Options)

Starts a generic wx_object server and invokes Mod:init(Args) in the new process.

start_link(Name, Mod, Args, Options)

Starts a generic wx_object server and invokes Mod:init(Args) in the new process.

stop(Obj)

Stops a generic wx_object server with reason 'normal'. Invokes terminate(Reason,State) in the server. The call waits until the process is terminated. If the process does not exist, an exception is raised.

stop(Obj, Reason, Timeout)

Stops a generic wx_object server with the given Reason. Invokes terminate(Reason,State) in the server. The call waits until the process is terminated. If the call times out, or if the process does not exist, an exception is raised.

wait_response(RequestId)

Wait infinitely for a reply from a generic server.

wait_response(Key, Timeout)

Wait 'timeout' for a reply from a generic server.

# Types

Link to this type

# event()

View Source (not exported)

    -type event() ::
              wxActivateEvent:wxActivate() |
              wxAuiManagerEvent:wxAuiManager() |
              wxAuiNotebookEvent:wxAuiNotebook() |
              wxBookCtrlEvent:wxBookCtrl() |
              wxCalendarEvent:wxCalendar() |
              wxChildFocusEvent:wxChildFocus() |
              wxClipboardTextEvent:wxClipboardText() |
              wxCloseEvent:wxClose() |
              wxColourPickerEvent:wxColourPicker() |
              wxCommandEvent:wxCommand() |
              wxContextMenuEvent:wxContextMenu() |
              wxDateEvent:wxDate() |
              wxDisplayChangedEvent:wxDisplayChanged() |
              wxDropFilesEvent:wxDropFiles() |
              wxEraseEvent:wxErase() |
              wxFileDirPickerEvent:wxFileDirPicker() |
              wxFocusEvent:wxFocus() |
              wxFontPickerEvent:wxFontPicker() |
              wxGridEvent:wxGrid() |
              wxHelpEvent:wxHelp() |
              wxHtmlLinkEvent:wxHtmlLink() |
              wxIconizeEvent:wxIconize() |
              wxIdleEvent:wxIdle() |
              wxInitDialogEvent:wxInitDialog() |
              wxJoystickEvent:wxJoystick() |
              wxKeyEvent:wxKey() |
              wxListEvent:wxList() |
              wxMaximizeEvent:wxMaximize() |
              wxMenuEvent:wxMenu() |
              wxMouseCaptureChangedEvent:wxMouseCaptureChanged() |
              wxMouseCaptureLostEvent:wxMouseCaptureLost() |
              wxMouseEvent:wxMouse() |
              wxMoveEvent:wxMove() |
              wxNavigationKeyEvent:wxNavigationKey() |
              wxPaintEvent:wxPaint() |
              wxPaletteChangedEvent:wxPaletteChanged() |
              wxQueryNewPaletteEvent:wxQueryNewPalette() |
              wxSashEvent:wxSash() |
              wxScrollEvent:wxScroll() |
              wxScrollWinEvent:wxScrollWin() |
              wxSetCursorEvent:wxSetCursor() |
              wxShowEvent:wxShow() |
              wxSizeEvent:wxSize() |
              wxSpinEvent:wxSpin() |
              wxSplitterEvent:wxSplitter() |
              wxStyledTextEvent:wxStyledText() |
              wxSysColourChangedEvent:wxSysColourChanged() |
              wxTaskBarIconEvent:wxTaskBarIcon() |
              wxTreeEvent:wxTree() |
              wxUpdateUIEvent:wxUpdateUI() |
              wxWebViewEvent:wxWebView() |
              wxWindowCreateEvent:wxWindowCreate() |
              wxWindowDestroyEvent:wxWindowDestroy().

Link to this type

# request_id()

View Source (not exported)

    -type request_id() :: term().

Link to this type

# server_ref()

View Source (not exported)

    -type server_ref() :: Obj :: wx:wx_object() | atom() | pid().

# Callbacks

Link to this callback

# code_change(OldVsn, State, Extra)

View Source (optional)

    -callback code_change(OldVsn :: term() | {down, term()}, State :: term(), Extra :: term()) ->
                             {ok, NewState :: term()} | {error, Reason :: term()}.

Link to this callback

# handle_call(Request, From, State)

View Source (optional)

    -callback handle_call(Request :: term(), From :: {pid(), Tag :: term()}, State :: term()) ->
                             {reply, Reply :: term(), NewState :: term()} |
                             {reply, Reply :: term(), NewState :: term(), timeout() | hibernate} |
                             {noreply, NewState :: term()} |
                             {noreply, NewState :: term(), timeout() | hibernate} |
                             {stop, Reason :: term(), Reply :: term(), NewState :: term()} |
                             {stop, Reason :: term(), NewState :: term()}.

Link to this callback

# handle_cast(Request, State)

View Source (optional)

    -callback handle_cast(Request :: term(), State :: term()) ->
                             {noreply, NewState :: term()} |
                             {noreply, NewState :: term(), timeout() | hibernate} |
                             {stop, Reason :: term(), NewState :: term()}.

Link to this callback

# handle_event(Request, State)

View Source

    -callback handle_event(Request ::
                               #wx{id :: integer(), obj :: wx:wx_object(), userData :: term(), event :: event()},
                           State :: term()) ->
                              {noreply, NewState :: term()} |
                              {noreply, NewState :: term(), timeout() | hibernate} |
                              {stop, Reason :: term(), NewState :: term()}.

Link to this callback

# handle_info(Info, State)

View Source (optional)

    -callback handle_info(Info :: timeout() | term(), State :: term()) ->
                             {noreply, NewState :: term()} |
                             {noreply, NewState :: term(), timeout() | hibernate} |
                             {stop, Reason :: term(), NewState :: term()}.

Link to this callback

# handle_sync_event(Request, Ref, State)

View Source (optional)

    -callback handle_sync_event(Request ::
                                    #wx{id :: integer(),
                                        obj :: wx:wx_object(),
                                        userData :: term(),
                                        event :: event()},
                                Ref :: #wx_ref{ref :: term(), type :: term(), state :: term()},
                                State :: term()) ->
                                   ok.

Link to this callback

# init(Args)

View Source

    -callback init(Args :: term()) ->
                      {#wx_ref{ref :: term(), type :: term(), state :: term()}, State :: term()} |
                      {#wx_ref{ref :: term(), type :: term(), state :: term()},
                       State :: term(),
                       timeout() | hibernate} |
                      {stop, Reason :: term()} |
                      ignore.

Link to this callback

# terminate(Reason, State)

View Source (optional)

    -callback terminate(Reason :: normal | shutdown | {shutdown, term()} | term(), State :: term()) -> term().

# Functions

Link to this function

# call(Obj, Request)

View Source

    -spec call(Obj, Request) -> term() when Obj :: wx:wx_object() | atom() | pid(), Request :: term().

Make a call to a wx_object server. The call waits until it gets a result. Invokes handle_call(Request, From, State) in the server

Link to this function

# call(Obj, Request, Timeout)

View Source

    -spec call(Obj, Request, Timeout) -> term()
                  when Obj :: wx:wx_object() | atom() | pid(), Request :: term(), Timeout :: integer().

Make a call to a wx_object server with a timeout. Invokes handle_call(Request, From, State) in server

Link to this function

# cast(Obj, Request)

View Source

    -spec cast(Obj, Request) -> ok when Obj :: wx:wx_object() | atom() | pid(), Request :: term().

Make a cast to a wx_object server. Invokes handle_cast(Request, State) in the server

Link to this function

# check_response(Msg, Key)

View Source

    -spec check_response(Msg :: term(), Key :: request_id()) ->
                            {reply, Reply :: term()} | false | {error, {term(), server_ref()}}.

Check if a received message was a reply to a RequestId

Link to this function

# get_pid(Obj)

View Source

    -spec get_pid(Obj) -> pid() when Obj :: wx:wx_object() | atom() | pid().

Get the pid of the object handle.

Link to this function

# reply(PidTag, Reply)

View Source

    -spec reply({pid(), Tag :: term()}, Reply :: term()) -> pid().

Get the pid of the object handle.

Link to this function

# send_request(Obj, Request)

View Source

    -spec send_request(Obj, Request :: term()) -> request_id() when Obj :: wx:wx_object() | atom() | pid().

Make an send_request to a generic server. and return a RequestId which can/should be used with wait_response/[1|2]. Invokes handle_call(Request, From, State) in server.

Link to this function

# set_pid(Obj, Pid)

View Source

    -spec set_pid(Obj, Pid :: pid()) -> wx:wx_object() when Obj :: wx:wx_object() | atom() | pid().

Sets the controlling process of the object handle.

Link to this function

# start(Name, Mod, Args, Options)

View Source

    -spec start(Name, Mod, Args, Options) -> wxWindow:wxWindow() | {error, term()}
                   when
                       Name :: {local, atom()},
                       Mod :: atom(),
                       Args :: term(),
                       Flag :: trace | log | {logfile, string()} | statistics | debug,
                       Options :: [{timeout, timeout()} | {debug, [Flag]}].

Starts a generic wx_object server and invokes Mod:init(Args) in the new process.

Link to this function

# start_link(Mod, Args, Options)

View Source

    -spec start_link(Mod, Args, Options) -> wxWindow:wxWindow() | {error, term()}
                        when
                            Mod :: atom(),
                            Args :: term(),
                            Flag :: trace | log | {logfile, string()} | statistics | debug,
                            Options :: [{timeout, timeout()} | {debug, [Flag]}].

Starts a generic wx_object server and invokes Mod:init(Args) in the new process.

Link to this function

# start_link(Name, Mod, Args, Options)

View Source

    -spec start_link(Name, Mod, Args, Options) -> wxWindow:wxWindow() | {error, term()}
                        when
                            Name :: {local, atom()},
                            Mod :: atom(),
                            Args :: term(),
                            Flag :: trace | log | {logfile, string()} | statistics | debug,
                            Options :: [{timeout, timeout()} | {debug, [Flag]}].

Starts a generic wx_object server and invokes Mod:init(Args) in the new process.

Link to this function

# stop(Obj)

View Source

    -spec stop(Obj) -> ok when Obj :: wx:wx_object() | atom() | pid().

Stops a generic wx_object server with reason 'normal'. Invokes terminate(Reason,State) in the server. The call waits until the process is terminated. If the process does not exist, an exception is raised.

Link to this function

# stop(Obj, Reason, Timeout)

View Source

    -spec stop(Obj, Reason, Timeout) -> ok
                  when Obj :: wx:wx_object() | atom() | pid(), Reason :: term(), Timeout :: timeout().

Stops a generic wx_object server with the given Reason. Invokes terminate(Reason,State) in the server. The call waits until the process is terminated. If the call times out, or if the process does not exist, an exception is raised.

Link to this function

# wait_response(RequestId)

View Source

    -spec wait_response(RequestId :: request_id()) ->
                           {reply, Reply :: term()} | {error, {term(), server_ref()}}.

Wait infinitely for a reply from a generic server.

Link to this function

# wait_response(Key, Timeout)

View Source

    -spec wait_response(Key :: request_id(), Timeout :: timeout()) ->
                           {reply, Reply :: term()} | timeout | {error, {term(), server_ref()}}.

Wait 'timeout' for a reply from a generic server.