Home

libhw Example

First create a class that inherits from hwApp. Your class must implement the createInstance and init methods.

#ifndef TESTAPP_H
#define TESTAPP_H

#include <hwApp.h>

class MainWindow;

class TestApp : public hwApp
{
public:
    
    TestApp* createInstance();
    void init();
    
private:
    
    MainWindow* myMainWindow;
};

#endif
Here are the methods for the TestApp class.
#include <TestApp.h>
#include <hwServer.h>
#include <hwTheme.h>
#include <MainWindow.h>

TestApp* TestApp::createInstance()
{
    return new TestApp();
}

void TestApp::init()
{
    myMainWindow = new MainWindow();
    setActiveWindow(myMainWindow);
    setTheme(new hwTheme("testapp.css"));
}

int main(int argc, char** argv)
{
    hwServer::getInstance()->setAppFactory(new TestApp());
    hwServer::getInstance()->run(8888);
}
In the main method, the hwServer singleton is instantiated. The TestApp class is registered as the factory for creating new instances. Each new user connection gets its own instance of the application. Each instance is started in its own thread. The run method tells the app which port to listen on for connections.

The MainWindow is a class that inherits from hwWindow and sets up the main user interface for the test application.

We will create a simple main window with a button that when clicked updates a label with the current time.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <hwWindow.h>

class MainWindow : public hwWindow
{
public:
    
    MainWindow();
    
private:

};

#endif

Here is the MainWindow source

#include <MainWindow.h>
#include <hwLabel.h>
#include <hwTableLayout.h>
#include <hwButton.h>
#include <time.h>

/*
 * Create button to handle onClick method.
 *
 */
class testButton : public hwButton
{
public:

    testButton(hwLabel* label) : myLabel(label) {}
    
    void onClick() {
        time_t rawtime;
        struct tm* timeinfo;
        time(&rawtime);
        timeinfo = localtime(&rawtime);
        myLabel->setTitle(asctime(timeinfo));
    }
    
private:

    hwLabel* myLabel;
};

MainWindow::MainWindow()
{
    hwTableLayout* tablelayout1 = new hwTableLayout();
    add(tablelayout1);
    
    hwLabel* label1 = new hwLabel("The current time is:");
    tablelayout1->add(label1, 0, 0);
    
    hwLabel* label2 = new hwLabel("");
    label2->setName("timeLabel");
    tablelayout1->add(label2, 0, 1);

    testButton* button1 = new testButton(label2);
    tablelayout1->add(button1, 0, 2);
}

We then create a style sheet. All size and style information is set in style sheets, not in the code.

body {
    font-family: arial;
    padding: 0px;
    margin: 0px;
}

.hwWindow {
    background: #eeeeee;
    border: 1px solid blue; 
    border-collapse: collapse;
    padding: 0px;
    margin: 0px;
}

.hwWindowTitlebar {
    height: 30px;
    background: #0000cc;
    color: #ffffff;
    font-weight: bold;
    border-spacing: 3px;
}

.hwTableLayout { 
    border: 0px;
}

.hwLabel {
    font-weight: bold;
}

#timeLabel {
    background: #ffffff;
    font-weight: normal;
    border: 1px solid black;
    padding: 3px;
}

Here's what it looks like

Here's a flash video

Here's the source to download

testapp.zip


© 2009 Adam Siegel