檢視原始碼 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};
...
資料型別
server_ref() = wx:wx_object() | atom() | pid()
摘要
函式
向 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().
-type request_id() :: term().
-type server_ref() :: Obj :: wx:wx_object() | atom() | pid().
回呼
-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()}.
函式
-spec call(Obj, Request) -> term() when Obj :: wx:wx_object() | atom() | pid(), Request :: term().
向 wx_object 伺服器發出呼叫。該呼叫會等待直到取得結果。在伺服器中調用 handle_call(Request, From, State)
-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)
-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。
取得物件句柄的 pid。
-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().
設定物件句柄的控制程序。
-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)。
-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)。
-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)。該呼叫會等待直到程序終止。如果程序不存在,則會拋出異常。
-spec stop(Obj, Reason, Timeout) -> ok when Obj :: wx:wx_object() | atom() | pid(), Reason :: term(), Timeout :: timeout().
以給定的 Reason 停止一個通用的 wx_object 伺服器。在伺服器中調用 terminate(Reason,State)。該呼叫會等待直到程序終止。如果呼叫逾時,或如果程序不存在,則會拋出異常。
-spec wait_response(RequestId :: request_id()) -> {reply, Reply :: term()} | {error, {term(), server_ref()}}.
無限期等待來自通用伺服器的回覆。
-spec wait_response(Key :: request_id(), Timeout :: timeout()) -> {reply, Reply :: term()} | timeout | {error, {term(), server_ref()}}.
等待 'timeout' 時間,以取得來自通用伺服器的回覆。