]> git.tdb.fi Git - libs/gltk.git/blob - examples/helloworld.cpp
Adjust things to conform to changes in other libraries
[libs/gltk.git] / examples / helloworld.cpp
1 /*
2 A simple graphical Hello World application implemented with mspgltk.
3 Demonstrates some of the most common widget types.
4 */
5
6 #include <msp/core/application.h>
7 #include <msp/graphics/simplewindow.h>
8 #include <msp/gl/blend.h>
9 #include <msp/gl/device.h>
10 #include <msp/gl/matrix.h>
11 #include <msp/gl/windowview.h>
12 #include <msp/gltk/button.h>
13 #include <msp/gltk/entry.h>
14 #include <msp/gltk/label.h>
15 #include <msp/gltk/panel.h>
16 #include <msp/gltk/resources.h>
17 #include <msp/gltk/root.h>
18
19 using namespace Msp;
20
21 // Application class.  Because it's so much nicer than global variables.
22 class HelloWorld: public Msp::RegisteredApplication<HelloWorld>
23 {
24 private:
25         // A window and graphics device to display our widgets in
26         Graphics::SimpleWindow wnd;
27         GL::Device gl_dev;
28         GL::WindowView view;
29
30         // GLtk resources and widgets
31         GLtk::Resources res;
32         GLtk::Root root;
33         GLtk::Entry *ent_name;
34         GLtk::Label *lbl_hello;
35
36 public:
37         HelloWorld(int, char **);
38 private:
39         virtual void tick();
40         void show_hello();
41 };
42
43
44 HelloWorld::HelloWorld(int, char **):
45         wnd(200, 200),
46         gl_dev(wnd),
47         view(wnd),
48         // Load resources.  This must be done before the root widget is created.
49         res("basic.skin"),
50         // A Root receives input from a Graphics::Window and passes it on
51         root(res, wnd)
52 {
53         wnd.set_title("Hello World");
54         wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
55
56         /* Container widgets will delete their contents upon destruction so we can
57         safely forget about the pointers after setting the widgets up. */
58
59         // Panels can be used to divide the window into sub-areas
60         GLtk::Panel *panel = new GLtk::Panel;
61         root.add(*panel);
62         panel->set_geometry(GLtk::Geometry(20, 20, 160, 160));
63
64         GLtk::Label *lbl;
65         // Prompts can be displayed with Labels
66         panel->add(*(lbl = new GLtk::Label("Type your name:")));
67         lbl->set_geometry(GLtk::Geometry(10, 130, 140, 20));
68
69         // The user can type text into an Entry
70         panel->add(*(ent_name = new GLtk::Entry));
71         ent_name->set_geometry(GLtk::Geometry(10, 100, 140, 27));
72
73         GLtk::Button *btn;
74         // Buttons can be wired to cause things to happen
75         panel->add(*(btn = new GLtk::Button("Hello")));
76         btn->set_geometry(GLtk::Geometry(10, 68, 140, 27));
77         btn->signal_clicked.connect(sigc::mem_fun(this, &HelloWorld::show_hello));
78
79         // Another label for displaying some information
80         panel->add(*(lbl_hello = new GLtk::Label));
81         lbl_hello->set_geometry(GLtk::Geometry(10, 42, 140, 20));
82
83         // The user might want to exit the program (*gasp*)
84         panel->add(*(btn = new GLtk::Button("Exit")));
85         btn->set_geometry(GLtk::Geometry(50, 10, 100, 27));
86         btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
87
88         view.set_content(&root);
89
90         // Done with setting things up, show the window!
91         wnd.show();
92 }
93
94 // This function will be called periodically from the main loop
95 void HelloWorld::tick()
96 {
97         wnd.tick();
98         view.render();
99 }
100
101 // Displays a greeting to the user
102 void HelloWorld::show_hello()
103 {
104         lbl_hello->set_text("Hello, "+ent_name->get_text()+"!");
105 }