/* Syllable GUI example "Hello world" program. The code is run like this: main() -> MyApp() constructor -> MyWindow() constructor Press Alt+W or Alt+Q to quit the app. Compile this with the commands: c++ -o HelloWorld HelloWorld.cpp -lsyllable Or use the HelloWorld examples for OMake: omake Or classic Make: make Run it with: ./HelloWorld - Anthony Morphett (awmorp@gmail.com), June 2008. */ #include // Include the definitions of the classes that we will use #include #include using namespace os; // This means that we don't need to prefix everything by "os::", e.g. "os::Application" // Tell the compiler that we will define classes MyWindow and MyApp later class MyApp; class MyWindow; /*** Define the MyApp class - our customised Application object. *** We need to create one of these before we can do anything else GUI-related, as it establishes the connection with the appserver. Usually the class definition would go in a separate file, myapp.h, but in this case as it is so simple we include it here. We declare the members and methods here, but we don't give the code for the method functions yet. This comes later. */ class MyApp : public Application // Declaring class MyApp, derived from libsyllable class Application { public: // We want the following methods/members to be accessible from anywhere in the app, e.g. in main() // The constructor. The Application constructor requires a mimetype parameter, to identify the application. // Usually this is "application/x-vnd.YourAppName" (the x-vnd. part comes from the MIME specification). MyApp( const char* pzMimeType ); // The destructor ~MyApp(); private: // We want the following members only to be accessible from within MyApp, ie from code inside some MyApp method. // It is good practice to make everything private, unless it needs to be accessed from other objects or elsewhere. // This will hold a pointer to the window MyWindow* m_pcWindow; }; // Don't forget the ; at the end! // *** Define the MyWindow class. This class contains the code for displaying some content in the window. *** class MyWindow : public Window { public: // Constructor. It adds a StringView to the window, which displays "Hello, world!". // The parameter cFrame tells where the window should be on screen. MyWindow( const Rect& cFrame ); // Destructor ~MyWindow(); private: // Declare the m_pcStringView member. This will hold a pointer to the StringView which displays the text. StringView* m_pcStringView; }; // ****** Now we give the code for the class methods ****** // *** First, MyApp *** // The constructor - create a window and show it MyApp::MyApp( const char* pzMimeType ) : Application( pzMimeType ) // This means to automatically call the Application constructor first { m_pcWindow = new MyWindow( Rect( 200,200,400,400 ) ); // Create a new window at (200,200)-(400,400) on screen m_pcWindow->Show(); // Show the window - make it visible } // The destructor. In this case, we don't need to do anything in the destructor, but we must include it (or else the app won't link properly). MyApp::~MyApp() {} // *** Next, MyWindow *** MyWindow::MyWindow( const Rect& cFrame ) : Window( cFrame, "TestApp-window", "Test application!" ) /* Call the Window constructor. cFrame is the window's position on screen. "TestApp-window" is the name of the window (this is for debugging, it isn't shown on screen anywhere). "Test application!" is the title that appears in the window's titlebar. */ { // Create a StringView, with the text "Hello world!". // This creates the view in memory, but later we need to manually add it to the window for it to be displayed on screen. m_pcStringView = new StringView( GetBounds(), "TestApp-stringview", "Hello, world!", ALIGN_CENTER ); /* GetBounds() - make the view fill the entire window (GetBounds() returns the size of the window). "TestApp-stringview" - the name of the view, used for debugging and for finding the view later. Not shown on screen. "Hello, world!" - the text to display in the view. ALIGN_CENTER - center the text. */ // Add the view to the window. This means it will be displayed in the window. AddChild( m_pcStringView ); } // Destructor - no need to do anything MyWindow::~MyWindow() {} // ****** Now main(), the entry point to the app ****** // main() : the normal entry point to the program. This is the first thing run when the program starts int main( int nArgc, char* apzArgv[] ) { // Create the Application object, which connects to the appserver and creates a window MyApp* pcApp = new MyApp( "application/x-vnd.ExampleApp" ); // Create the application object pcApp->Run(); return( 0 ); }