//--------------------------------------------------------------------------- // // Copyright (C) Geoworks 1997. All rights reserved. // GEOWORKS CONFIDENTIAL // // ファイル: notepad.cpp // 解説: // // 「プログラマーズ ガイド」の「Flex UI」の 2 番目のプログラム例 // // //--------------------------------------------------------------------------- #include #include #include #include #include #include #include #include #include #include #include #include #include "resource.h" #include "notepad.h" // // テキストを保持するバッファを作成します // TCHAR *_notepadTextString; // ResidentApplication のサブクラス。アプリケーションをシステムに登録し、 // AppBase の作成方法を提供します class NotepadResidentApplication : public ResidentApplication { public: NotepadResidentApplication() : ResidentApplication(_TEXT("notepad")) {}; virtual AppBase *CreateAppBase(void) {return new NotepadApp;} }; static NotepadResidentApplication notepadApp; // アプリケーションの指定属性。ここでは名前を指定していますが、 // 標準以外のスタック サイズやスレッド優先順位なども指定できます。 // static AppNameAttribute helloName(¬epadApp, NOTEPAD_APP_TEXT); //------------------------------------------------------------------------ // NotepadApp::SetAppContext(const TCHAR *) //------------------------------------------------------------------------ // // 概要: アプリケーションの作成 // スコープ: Private // 戻り値: なし // //------------------------------------------------------------------------ void NotepadApp::SetAppContext(const TCHAR *) { // // ヘルパー関数を呼び出して UI を作成します // if (AttachNotepadAppUI() == FAILURE) { EC_WARN("Unable to build application."); } // // テキスト バッファの領域を割り当てます // _notepadTextString = new TCHAR[600]; } /* End of NotepadApp::SetAppContext() */ //------------------------------------------------------------------------ // NotepadApp::AttachNotepadAppUI() //------------------------------------------------------------------------ // // 概要: アプリケーション作成用のヘルパー関数 // スコープ: Private // 戻り値: SUCCESS または FAILURE // //------------------------------------------------------------------------ Result NotepadApp::AttachNotepadAppUI(void) { // // アプリケーションのメインのフレームを作成します // FlexFrame *frame = theUIFactory->CreateFlexFrame(HINT_FRAME_WITH_NO_CLOSE_BUTTON, NOTEPAD_APP_TEXT); if (frame == NULL) { return FAILURE; } // // フレームのレイアウトを作成します // VerticalFlowLayout *mainFrameLayout = new VerticalFlowLayout( 10, VerticalFlowLayout::Y_JUSTIFY_CENTER, LayoutManagerInterface::X_ALIGN_LEFT); if (mainFrameLayout == NULL) { return FAILURE; } // // レイアウトをフレームに追加した後でフレームを追加します // frame->SetLayout(mainFrameLayout); this->Add(frame); // // アプリケーションのボタンを格納するパネルを作成します // _buttonPanel = new FlexPanel; if (_buttonPanel == NULL) { return FAILURE; } // // パネルのレイアウトを設定します // HorizontalFlowLayout *buttonPanelLayout = new HorizontalFlowLayout(8); if (buttonPanelLayout == NULL) { return FAILURE; } _buttonPanel->SetLayout(buttonPanelLayout); // // アプリケーションのボタンを作成します // if ( CreateButton(NOTEPAD_NEW_BUTTON_TITLE, NEW_BUTTON) == FAILURE ) { return FAILURE; } if ( CreateButton(NOTEPAD_SAVE_BUTTON_TITLE, SAVE_BUTTON) == FAILURE ) { return FAILURE; } if ( CreateButton(NOTEPAD_REVERT_BUTTON_TITLE, REVERT_BUTTON) == FAILURE ) { return FAILURE; } // // パネルをフレームに追加します // frame->Add(_buttonPanel); // // アプリケーションのテキスト表示を作成します // _textDisplay = theUIFactory->CreateFlexTextArea(0); if (_textDisplay == NULL) { return FAILURE; } // // テキスト表示の属性を設定します // _textDisplay->SetColumns(75); _textDisplay->SetRows(7); _textDisplay->SetMaxChars(600); // // テキスト表示をフレームに追加します // frame->Add(_textDisplay); // // フレームを表示して SUCCESS を返します // frame->SetVisible(TRUE); return SUCCESS; } /* End of NotepadApp::AttachNotepadAppUI() */ //--------------------------------------------------------------------------------------------- // NotepadApp::CreateButton(static const TCHAR *title, FlexComponentID buttonID) //--------------------------------------------------------------------------------------------- // // 概要: アプリケーションのボタンの作成 // スコープ: Private // 戻り値: SUCCESS または FAILURE // //--------------------------------------------------------------------------------------------- Result NotepadApp::CreateButton(const TCHAR *title, FlexComponentID buttonID) { // // アプリケーションのボタンを作成します // FlexButton *button = theUIFactory->CreateFlexButton(0, title); if (button == NULL) { return FAILURE; } // // ボタンをパネルに追加します // _buttonPanel->Add(button); // // アクション リスナーをシステムに追加します // button->AddActionListener(*this); // // ボタンの ID を設定して SUCCESS を返します // button->SetID(buttonID); return SUCCESS; } /* End of CreateButton() */ //------------------------------------------------------------------------ // NotepadApp::ActionPerformed() //------------------------------------------------------------------------ // // 概要: ボタンが押されたときに呼び出されるメソッド // スコープ: Private // 戻り値: なし // //------------------------------------------------------------------------ void NotepadApp::ActionPerformed(ActionEvent& event ) { // // SAVE と OPEN のためのファイル ポインタを作成します // File *myFile; // // この関数を呼び出したボタンの ID を取得します // FlexComponentID id = (event.GetSource())->GetID(); switch(id) { // // NEW がクリックされたかどうかをチェックします // case NEW_BUTTON: _textDisplay->SetText(_TEXT("")); break; // // SAVE がクリックされたかどうかチェックします // case SAVE_BUTTON: // // バッファの内容を取得します // _textDisplay->GetText(_notepadTextString, 600); // // ファイルを CREATE と READ_WRITE 属性で開きます // myFile = theFileStoreManager.Open(_TEXT("/int/tempfile"), O_CREAT | O_RDWR ); // // バッファの内容をファイルへ書き込み、 // ファイルを閉じます // myFile->Write((uint8*)_notepadTextString, 600); myFile->Close(); break; // // REVERT がクリックされたかどうかチェックします // case REVERT_BUTTON: // // ファイルがすでに存在するかどうかチェックします // if (theFileStoreManager.FileExists(_TEXT("/int/tempfile"))) { // // ファイルを READ_ONLY で開き、内容をバッファに読み込み、 // 操作終了後にファイルを閉じます // myFile = theFileStoreManager.Open(_TEXT("/int/tempfile"), O_RDONLY ); myFile->Read((uint8*)_notepadTextString, 600); myFile->Close(); // // バッファの内容を表示します // _textDisplay->SetText(_notepadTextString); } break; } } /* End of NotepadApp::ActionPerformed() */ void NotepadApp::Exit(void) { AppBase::Exit(); // // バッファ用の領域を削除します // delete(_notepadTextString); } /* End of NotepadApp::Exit() */