檢視原始碼 wx_object 行為 (wx v2.4.3)

wx_object - 通用 wx 物件行為

這是一個行為模組,可以用於「子類化」wx 物件。它的運作方式類似於常規的 gen_server 模組,並為每個物件建立一個伺服器。

注意:目前沒有實作任何形式的繼承。

使用者模組應該匯出

init(Args) 應該回傳
{wxWindow, State} | {wxWindow, State, Timeout} | ignore | {stop, Reason}

非同步視窗事件處理
handle_event(#wx{}, State) 應該回傳
{noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}

使用者模組可以匯出以下回呼函式

handle_call(Msg, {From, Tag}, State) 應該回傳
{reply, Reply, State} | {reply, Reply, State, Timeout} | {noreply, State} | {noreply, State, Timeout} | {stop, Reason, Reply, State}

handle_cast(Msg, State) 應該回傳
{noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}

如果上述函式未匯出但被呼叫,wx_object 程序將會崩潰。使用者模組也可以匯出

Info 是訊息,例如 {'EXIT', P, R}, {nodedown, N}, ...
handle_info(Info, State) 應該回傳, ...
{noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}

如果訊息被傳送到 wx_object 程序,但 handle_info 未被匯出,則該訊息將會被丟棄並忽略。

當在上述其中一個函式中回傳 stop,且 Reason = normal | shutdown | Term 時,會呼叫 terminate(State)。它讓使用者模組進行清理,當伺服器終止或驅動程式中的 wx_object() 被刪除時,總是會被呼叫。如果父程序終止,則會呼叫 Module:terminate/2 函式。
terminate(Reason, State)

範例

  -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};
  ...

資料型別

摘要

函式

向 wx_object 伺服器發出呼叫。該呼叫會等待直到取得結果。在伺服器中調用 handle_call(Request, From, State)

向帶有逾時的 wx_object 伺服器發出呼叫。在伺服器中調用 handle_call(Request, From, State)

向 wx_object 伺服器發出 cast。在伺服器中調用 handle_cast(Request, State)

檢查接收到的訊息是否為 RequestId 的回覆

取得物件句柄的 pid。

取得物件句柄的 pid。

向通用伺服器發出 send_request,並回傳一個 RequestId,該 RequestId 可以/應該與 wait_response/[1|2] 一起使用。在伺服器中調用 handle_call(Request, From, State)。

設定物件句柄的控制程序。

啟動一個通用的 wx_object 伺服器,並在新程序中調用 Mod:init(Args)。

啟動一個通用的 wx_object 伺服器,並在新程序中調用 Mod:init(Args)。

啟動一個通用的 wx_object 伺服器,並在新程序中調用 Mod:init(Args)。

以 'normal' 原因停止一個通用的 wx_object 伺服器。在伺服器中調用 terminate(Reason,State)。該呼叫會等待直到程序終止。如果程序不存在,則會拋出異常。

以給定的 Reason 停止一個通用的 wx_object 伺服器。在伺服器中調用 terminate(Reason,State)。該呼叫會等待直到程序終止。如果呼叫逾時,或如果程序不存在,則會拋出異常。

無限期等待來自通用伺服器的回覆。

等待 'timeout' 時間,以取得來自通用伺服器的回覆。

型別

-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().
此型別的連結

request_id()

檢視原始碼 (未匯出)
-type request_id() :: term().
此型別的連結

server_ref()

檢視原始碼 (未匯出)
-type server_ref() :: Obj :: wx:wx_object() | atom() | pid().

回呼

此回呼的連結

code_change(OldVsn, State, Extra)

檢視原始碼 (可選)
-callback code_change(OldVsn :: term() | {down, term()}, State :: term(), Extra :: term()) ->
                         {ok, NewState :: term()} | {error, Reason :: term()}.
此回呼的連結

handle_call(Request, From, State)

檢視原始碼 (可選)
-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()}.
此回呼的連結

handle_cast(Request, State)

檢視原始碼 (可選)
-callback handle_cast(Request :: term(), State :: term()) ->
                         {noreply, NewState :: term()} |
                         {noreply, NewState :: term(), timeout() | hibernate} |
                         {stop, Reason :: term(), NewState :: term()}.
此回呼的連結

handle_event(Request, State)

檢視原始碼
-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()}.
此回呼的連結

handle_info(Info, State)

檢視原始碼 (可選)
-callback handle_info(Info :: timeout() | term(), State :: term()) ->
                         {noreply, NewState :: term()} |
                         {noreply, NewState :: term(), timeout() | hibernate} |
                         {stop, Reason :: term(), NewState :: term()}.
此回呼的連結

handle_sync_event(Request, Ref, State)

檢視原始碼 (可選)
-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.
-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.
此回呼的連結

terminate(Reason, State)

檢視原始碼 (可選)
-callback terminate(Reason :: normal | shutdown | {shutdown, term()} | term(), State :: term()) -> term().

函式

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

向 wx_object 伺服器發出呼叫。該呼叫會等待直到取得結果。在伺服器中調用 handle_call(Request, From, State)

此函式的連結

call(Obj, Request, Timeout)

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

向帶有逾時的 wx_object 伺服器發出呼叫。在伺服器中調用 handle_call(Request, From, State)

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

向 wx_object 伺服器發出 cast。在伺服器中調用 handle_cast(Request, State)

此函式的連結

check_response(Msg, Key)

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

檢查接收到的訊息是否為 RequestId 的回覆

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

取得物件句柄的 pid。

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

取得物件句柄的 pid。

此函式的連結

send_request(Obj, Request)

檢視原始碼
-spec send_request(Obj, Request :: term()) -> request_id() when Obj :: wx:wx_object() | atom() | pid().

向通用伺服器發出 send_request,並回傳一個 RequestId,該 RequestId 可以/應該與 wait_response/[1|2] 一起使用。在伺服器中調用 handle_call(Request, From, State)。

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

設定物件句柄的控制程序。

此函式的連結

start(Name, Mod, Args, Options)

檢視原始碼
-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]}].

啟動一個通用的 wx_object 伺服器,並在新程序中調用 Mod:init(Args)。

此函式的連結

start_link(Mod, Args, Options)

檢視原始碼
-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]}].

啟動一個通用的 wx_object 伺服器,並在新程序中調用 Mod:init(Args)。

此函式的連結

start_link(Name, Mod, Args, Options)

檢視原始碼
-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]}].

啟動一個通用的 wx_object 伺服器,並在新程序中調用 Mod:init(Args)。

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

以 'normal' 原因停止一個通用的 wx_object 伺服器。在伺服器中調用 terminate(Reason,State)。該呼叫會等待直到程序終止。如果程序不存在,則會拋出異常。

此函式的連結

stop(Obj, Reason, Timeout)

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

以給定的 Reason 停止一個通用的 wx_object 伺服器。在伺服器中調用 terminate(Reason,State)。該呼叫會等待直到程序終止。如果呼叫逾時,或如果程序不存在,則會拋出異常。

此函式的連結

wait_response(RequestId)

檢視原始碼
-spec wait_response(RequestId :: request_id()) ->
                       {reply, Reply :: term()} | {error, {term(), server_ref()}}.

無限期等待來自通用伺服器的回覆。

此函式的連結

wait_response(Key, Timeout)

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

等待 'timeout' 時間,以取得來自通用伺服器的回覆。