wxApp の概要
wxApp overview
Classes: wxApp
wxWidgetsアプリケーションは
main関数を持ちません.
wxAppクラスから継承されたクラスに宣言された
OnInitがそれに相当します.
OnInitは一般的に,少なくともトップウィンドウをつくります.
A wxWidgets application does not have a
main procedure; the equivalent is the
OnInit member defined for a class derived from wxApp.
OnInit will usually create a top window as a bare minimum.
以前のバージョンのwxWidgetsと違い,OnInitはframeを返しません.
その代わりに,アプリケーションを続行する(TRUE)かしない(FALSE)かをbooleanで返します.
wxWidgetsにトップウィンドウを知らせるため,
wxApp::SetTopWindow
を呼びなさい.
Unlike in earlier versions of wxWidgets, OnInit does not return a frame. Instead it
returns a boolean value which indicates whether processing should continue (TRUE) or not (FALSE).
You call
wxApp::SetTopWindow to let wxWidgets know
about the top window.
argcとargvで表されるコマンドライン引数は,wxAppのメンバー関数の中で使える.
Note that the program's command line arguments, represented by argc
and argv, are available from within wxApp member functions.
アプリケーションは全てのウィンドウを破棄することで終了する.
全てのフレームはアプリケーションが終了する為に破棄されなければならないので,
トップレベルフレームを破棄したら自動的にその他のウィンドウも破棄されるように,
新しいウィンドウを作成する時には可能な限り常に親フレームを使うことを推奨する.
そうしない時は,トップレベルフレームの
wxCloseEventの
ハンドラで,全ての子フレームを確実に破棄する事.
An application closes by destroying all windows. Because all frames must
be destroyed for the application to exit, it is advisable to use parent
frames wherever possible when creating new frames, so that deleting the
top level frame will automatically delete child frames. The alternative
is to explicitly delete child frames in the top-level frame's
wxCloseEvent
handler.
アプリケーションは普段は自動的に終了するが,異常事態では
wxExitを
呼び出して強制終了させる事ができる.
下を参照.
In emergencies the
wxExit function can be called to kill the
application however normally the applications shuts down automatically,
see below.
アプリケーションの宣言の例
An example of defining an application follows:
class DerivedApp : public wxApp
{
public:
virtual bool OnInit();
};
IMPLEMENT_APP(DerivedApp)
bool DerivedApp::OnInit()
{
wxFrame *the_frame = new wxFrame(NULL, ID_MYFRAME, argv[0]);
...
the_frame->Show(TRUE);
SetTopWindow(the_frame);
return TRUE;
}
IMPLEMENT_APP(appClass)を使うこと.それはwxWidgetsの初期化の適切な段階で,アプリケーションクラスのインスタンスの
動的生成を行う.
以前のバージョンのwxWidgetsではグローバルなアプリケーションオブジェクトの生成に頼っていたが,
これはもはや推奨されていない.
必用なグローバル初期化がアプリケーションオブジェクトの項逐次に実行されない可能性があるからだ.
Note the use of IMPLEMENT_APP(appClass), which allows wxWidgets to dynamically create an instance of the application object
at the appropriate point in wxWidgets initialization. Previous versions of wxWidgets used
to rely on the creation of a global application object, but this is no longer recommended,
because required global initialization may not have been performed at application object
construction time.
アプリケーションオブジェクトへの参照を返すwxGetApp関数を宣言する為に,DECLARE_APP(appClass)マクロをヘッダファイルで
用いることができる.
You can also use DECLARE_APP(appClass) in a header file to declare the wxGetApp function which returns
a reference to the application object.
Application shutdown
アプリケーションの終了
Application shutdown
アプリケーションは通常,最後のトップレベルウィンドウのが閉じられた時に終了する.
これは通常期待されるどうさである.そしてトップレベルウィンドウが一つの場合,
"Exit"
メニューコマンドへの対応時に
Close()を呼び出すだけで
十分であることを意味する.
もしこの動作を望まない場合,
wxApp::SetExitOnFrameDelete
で動作を変更する事ができる.
wxWidgets2.3.3から始まったそのようなロジックは(訳注:誤訳の可能性大),プログラムがメインループに入る前に
表示されたウィンドウには働かない.
言い換えれば,
wxApp::OnInitから安全にダイアログを表示する事ができ,
一時的にトップレベルウィンドウだったそのダイアログが終了する時にアプリケーションが終了する心配をしなくて良い.
The application normally shuts down when the last of its top level windows is
closed. This is normally the expected behaviour and means that it is enough to
call
Close() in response to the
"Exit" menu
command if your program has a single top level window. If this behaviour is not
desirable
wxApp::SetExitOnFrameDelete can
be called to change it. Note that starting from wxWidgets 2.3.3 such logic
doesn't apply for the windows shown before the program enters the main loop: in
other words, you can safely show a dialog from
wxApp::OnInit and not be afraid that your application
terminates when this dialog -- which is the last top level window for the
moment -- is closed.
別のアプリケーション終了のaspectは,アプリケーションが存在し,wxWidgetsが内部構造の
クリーンナップを行う
前に呼び出される
OnExitである.
全てのwxWidgetsオブジェクトを,OnExit関数が終了する前に破棄するべきである.
特に,
アプリケーションクラスのデストラクタで破棄を行ってはいけない!
Another aspect of the application shutdown is the
OnExit
which is called when the application exits but
before wxWidgets cleans up
its internal structures. Your should delete all wxWidgets object that your
created by the time OnExit finishes. In particular, do
not destroy them
from application class' destructor!
例えば、このコードはクラッシュするかもしれない.
For example, this code may crash:
class MyApp : public wxApp
{
public:
wxCHMHelpController m_helpCtrl;
...
};
この例ではm_helpCtrlがメンバオブジェクトで,MyAppのデストラクタで破棄される.
しかし,MyAppオブジェクトはwxCHMHelpControllerが依存しているwxWidgets構造が終了処理された後に
破棄されるので危険である.この解決法はHelpCtrlをOnExitで破棄することだ.
The reason for that is that m_helpCtrl is a member object and is
thus destroyed from MyApp destructor. But MyApp object is deleted after
wxWidgets structures that wxCHMHelpController depends on were
uninitialized! The solution is to destroy HelpCtrl in OnExit:
class MyApp : public wxApp
{
public:
wxCHMHelpController *m_helpCtrl;
...
};
bool MyApp::OnInit()
{
...
m_helpCtrl = new wxCHMHelpController;
...
}
int MyApp::OnExit()
{
delete m_helpCtrl;
return 0;
}