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