//--------------------------------------------------------------------------- // // Geoworks (R) application software and GEOS (R) operating system // software copyright (C) 1990-1998 Geoworks. All rights reserved. // United States Patents 5327529, 5237651, and 5438662. U.K. Patents // 0375703 and 0631677. German Patents P3854269.2-08 and // 69307728.1-08. French Patents 0375703 and 0631677. Other // international patents pending. // // This software is the confidential and proprietary information of // Geoworks ("Confidential Information"). You shall not disclose // such Confidential Information and shall use it only in accordance // with the terms of the license agreement you entered into with // Geoworks. // // PROJECT: GEOS_SC Documentation // MODULE: Flex UI Chapter // FILE: notepad.cpp // // VERSION 2.0 // // DESCRIPTION: // // An notepad application that uses three buttons to save and // revert text in a text buffer. // //------------------------------------------------------------------------- #include // For data types and ec checking. #include // For creating objects in the user interface. #include // For looks in the user interface. #include // For the file store manager. #include // For file operations. #include // For subclassing the resident application. #include // For using localization resources. #include "resource.h" // For the resource strings. #include "notepad.h" // For the defined methods in the class. // // Strings that are not user-visible and should not be localized. // const TCHAR * const NOTEPADAPP_NAME = _TEXT("notepad"); const TCHAR * const NOTEPADAPP_FILE_PATH = _TEXT("/int/tempfile"); // // Our subclass of ResidentApplication, which registers this application // with the system, and provides a way for this app to create its AppBase. // class NotepadAppResidentApplication : public ResidentApplication { public: NotepadAppResidentApplication() : ResidentApplication(NOTEPADAPP_NAME) {}; virtual AppBase *CreateAppBase(void) { return new NotepadApp; } }; static NotepadAppResidentApplication notepadApp; // // Any attributes that we want to specify about our app - in this case, we // specify the name, but we could also specify an image, maybe // a non-standard stack size or thread priority, etc. // static AppNameAttribute notepadAppName(¬epadApp, NOTEPADAPP_APP_TEXT); //--------------------------------------------------------------------------- // NotepadApp::NotepadApp() //--------------------------------------------------------------------------- // // SYNOPSIS: The constructor initializes variables to the initial values. // SCOPE: Public // RETURN: nothing // //--------------------------------------------------------------------------- NotepadApp::NotepadApp() : _notepadAppMainFrame(NULL), _notepadAppButtonPanel(NULL), _notepadAppTextDisplay(NULL), _notepadAppUIBuilt(FALSE) { } /* End of NotepadApp::NotepadApp() */ //------------------------------------------------------------------------ // NotepadApp::SetAppContext() //------------------------------------------------------------------------ // // SYNOPSIS: Called when the app is started. Calls the helper function // AttachNotepadAppUI to build the user interface. // SCOPE: Public // RETURN: nothing // PARAMETERS: [in] context - The application context. // //------------------------------------------------------------------------ void NotepadApp::SetAppContext(const TCHAR *context) { // // Call the helper function to build the UI. // if (!_notepadAppUIBuilt) { if (AttachNotepadAppUI() == FAILURE) { EC_WARN("Unable to build application."); Exit(); } else { _notepadAppUIBuilt = TRUE; } } // // Since we do not use the context parameter, we must pass it to the // USE_IT macro to suppress compiler warnings. // USE_IT(context); } /* End of NotepadApp::SetAppContext() */ //------------------------------------------------------------------------ // NotepadApp::AttachNotepadAppUI() //------------------------------------------------------------------------ // // SYNOPSIS: Called by SetAppContext to build the user interface. // Attempts to create and add elements of the user interface. // If any element cannot be created or added, FAILURE is // returned and SetAppContext will call the Exit method to // destroy the user interface. // SCOPE: Private // RETURN: SUCCESS or FAILURE // //------------------------------------------------------------------------ Result NotepadApp::AttachNotepadAppUI(void) { // // Create the main frame and add it to the application. // _notepadAppMainFrame = theUIFactory->CreateFlexFrame(HINT_FRAME_WITH_NO_CLOSE_BUTTON, NOTEPADAPP_APP_TEXT); if (NULL == _notepadAppMainFrame) { EC_WARN("Unable to build main frame."); return FAILURE; } if (Add(_notepadAppMainFrame) != SUCCESS) { EC_WARN("Unable to add the main frame."); delete _notepadAppMainFrame; return FAILURE; } // // Create a layout and set it on the frame. // VerticalFlowLayout *frameLayout = new VerticalFlowLayout( NOTEPADAPP_FRAME_GAP, VerticalFlowLayout::Y_JUSTIFY_CENTER, LayoutManagerInterface::X_ALIGN_LEFT); if (NULL == frameLayout) { EC_WARN("Unable to create the frame layout."); return FAILURE; } _notepadAppMainFrame->SetLayout(frameLayout); // // Create a panel to hold the buttons for the application // and attempt to add it. // _notepadAppButtonPanel = theUIFactory->CreateFlexHorizontalPanel(0, NOTEPADAPP_BUTTON_PANEL_GAP); if (NULL == _notepadAppButtonPanel) { EC_WARN("Unable to build the button panel."); return FAILURE; } if (_notepadAppMainFrame->Add(_notepadAppButtonPanel) != SUCCESS) { EC_WARN("Unable to add the button panel."); delete _notepadAppButtonPanel; return FAILURE; } // // Create the buttons for the application. // if ( CreateButton(NOTEPADAPP_CLEAR_BUTTON_TITLE, NOTEPADAPP_CLEAR_BUTTON) == FAILURE ) { EC_WARN("Unable to create the clear button."); return FAILURE; } if ( CreateButton(NOTEPADAPP_SAVE_BUTTON_TITLE, NOTEPADAPP_SAVE_BUTTON) == FAILURE ) { EC_WARN("Unable to create the save button."); return FAILURE; } if ( CreateButton(NOTEPADAPP_REVERT_BUTTON_TITLE, NOTEPADAPP_REVERT_BUTTON) == FAILURE ) { EC_WARN("Unable to create the revert button."); return FAILURE; } // // Create the text display and attempt to add it to the application. // _notepadAppTextDisplay = theUIFactory->CreateFlexTextArea(0); if (NULL == _notepadAppTextDisplay) { EC_WARN("Unable to create text display."); return FAILURE; } if (_notepadAppMainFrame->Add(_notepadAppTextDisplay) != SUCCESS) { EC_WARN("Unable to add the text display."); delete _notepadAppTextDisplay; return FAILURE; } // // Set the attributes of the text display. // _notepadAppTextDisplay->SetColumns(75); _notepadAppTextDisplay->SetRows(7); _notepadAppTextDisplay->SetMaxChars(600); // // Show the frame and return SUCCESS. // _notepadAppMainFrame->SetVisible(TRUE); return SUCCESS; } /* End of NotepadApp::AttachNotepadAppUI() */ //--------------------------------------------------------------------------- // NotepadApp::CreateButton() //--------------------------------------------------------------------------- // // SYNOPSIS: Creates the buttons for the application. // SCOPE: Private // RETURN: SUCCESS or FAILURE // PARAMETERS The text title of the button and the enumerated button // identification. //--------------------------------------------------------------------------- Result NotepadApp::CreateButton(const TCHAR *title, FlexComponentID buttonID) { // // Create a button and attempt to add it to the application. // FlexButton *button = theUIFactory->CreateFlexButton(0, title); if (NULL == button) { EC_WARN("Unable to create a button."); return FAILURE; } if (_notepadAppButtonPanel->Add(button) != SUCCESS) { EC_WARN("Unable to add a button."); delete button; return FAILURE; } // // Make the application an action listener. // if (button->AddActionListener(*this) != SUCCESS) { EC_WARN("Unable to add action listener."); return FAILURE; } // // Set the id for the button and return SUCCESS. // button->SetID(buttonID); return SUCCESS; } /* End of CreateButton() */ //------------------------------------------------------------------------ // NotepadApp::ActionPerformed() //------------------------------------------------------------------------ // // SYNOPSIS: A button press will call this method. Perform different // file operations, depending on which button was pressed. // SCOPE: Public // RETURN: nothing // //------------------------------------------------------------------------ void NotepadApp::ActionPerformed(ActionEvent& event ) { // // Create a file pointer for SAVE and REVERT, as well as a buffer for // the text string and an integer to keep track of the buffer size. // File *myFile = NULL; TCHAR *textBuffer = NULL; int32 bufferSize = 0; // // Get the ID of the button that called this function. // FlexComponentID id = (event.GetSource())->GetID(); switch(id) { // // Check if CLEAR was clicked. // case NOTEPADAPP_CLEAR_BUTTON: _notepadAppTextDisplay->SetText(_TEXT("")); break; // // Check if SAVE was clicked. // case NOTEPADAPP_SAVE_BUTTON: // // Set the size of the buffer to read. // bufferSize = (_notepadAppTextDisplay->GetCharCount() + 1); textBuffer = new TCHAR[bufferSize]; // // Get the contents of the buffer. // _notepadAppTextDisplay->GetText(textBuffer, bufferSize); // // Open the file CREATE and READ_WRITE. // myFile = theFileStoreManager.Open(NOTEPADAPP_FILE_PATH, O_CREAT | O_RDWR ); if (myFile != NULL) { // // Write the contents of the buffer to the file. // myFile->Write((uint8*)textBuffer, bufferSize * sizeof(TCHAR)); myFile->SetSize(bufferSize * sizeof(TCHAR)); } else { _notepadAppTextDisplay->SetText(NOTEPADAPP_ERROR); } // // Clean up. // myFile->Close(); delete [] textBuffer; myFile = NULL; break; // // Check if the button clicked is the REVERT button. // case NOTEPADAPP_REVERT_BUTTON: // // Open the file READ_ONLY and read the contents into the buffer. // Copy the contents of the buffer into the text area and close // the file when done. // myFile = theFileStoreManager.Open(NOTEPADAPP_FILE_PATH, O_RDONLY ); if (myFile != NULL) { myFile->GetSize(&bufferSize); textBuffer = new TCHAR[bufferSize]; if ((myFile->Read((uint8*)textBuffer, bufferSize) == bufferSize)){ _notepadAppTextDisplay->SetText(textBuffer); } else { _notepadAppTextDisplay->SetText(NOTEPADAPP_ERROR); } myFile->Close(); } else { _notepadAppTextDisplay->SetText(NOTEPADAPP_ERROR); } // // Clean up. // delete [] textBuffer; myFile = NULL; break; } } /* End of NotepadApp::ActionPerformed() */ //--------------------------------------------------------------------------- // NotepadApp::Exit() //--------------------------------------------------------------------------- // // SYNOPSIS: Use Exit to clean up when the application quits. This // includes deleting any elements of the user interface, // such as layouts, that were not created by theUIFactory. // SCOPE: Public // RETURN: nothing // //--------------------------------------------------------------------------- void NotepadApp::Exit(void) { // // Delete the layout for the main frame. // if (_notepadAppMainFrame) { delete _notepadAppMainFrame->RemoveLayout(); } // // Set the boolean for the application. // _notepadAppUIBuilt = FALSE; AppBase::Exit(); } /* End NotepadApp::Exit() */