-
Notifications
You must be signed in to change notification settings - Fork 6
Hello World
How easy is to create a Hello World program with Nana? (Please, see current tested version in our nana-demo repository)
#include <nana/gui/wvl.hpp>
#include <nana/gui/widgets/label.hpp>
int main()
{
using namespace nana;
form fm;
label lb(fm, rectangle(fm.size()));
lb.caption("Hello, World");
fm.show();
exec();
}
Pretty easy, with understandable code. Nana uses very simple and reasonable concepts to keep it easy. Secondly, unlike other frameworks, that make you write stiff code due to name and syntax constraints, Nana can make your code more straightforward and readable. For example, answering an event.
#include <nana/gui/wvl.hpp>
#include <nana/gui/widgets/button.hpp>
void clicked(const nana::arg_click &)
{
//When the window fm is clicked, this function will be "called".
}
int main()
{
using namespace nana;
form fm;
fm.events().click(clicked);
fm.show();
exec();
}
The name of the clicked
function is not constrained. You can give it any other name you want.
It is more straightforward than implementing an event handler from inheritance.
In some situations, we don't need to care about the parameter of clicked()
function,
like in the example:
void clicked() //NO parameter defined.
{
//When the form is clicked, this function will be "called".
}
fm.events().click(clicked); //Nana allows!
Very flexible, and keep your code simple. And this feature can be applied with function object.
What makes Nana so flexible?
Nana C++ Library does not contain any "extra compilers" to parse "special syntax", Nana uses 100% C++ and the template techniques make this library very powerful and flexible. Nana is not like other template-based library that causes a lot of code bloat and requires programmers have template-skilled. It's newbie-friendly.
Nana is a complete C++ style library that compile on Visual C++ 2013/GCC 4.8 and later. Nana allows you to use lambda, a feature of C++11, for event answering. Like this:
fm.events().click( []{ //When the form is clicked, the object
//created by this lambda will be "called".
});
or
fm.events().click( [](const arg_click & ei){
//When the form is clicked, the object created
//by this lambda will be "called", and I can
//read the parameter.
});
Additionally, Nana would make code flexible with std::bind
in C++11.
This lesson is indented to get you started programming with Nana.GUI. Let's read the simple HelloWorld code.
#include <nana/gui/wvl.hpp>
#include <nana/gui/widgets/button.hpp>
int main()
{
using namespace nana;
form fm;
fm.caption("Hello World");
button btn(fm, nana::rectangle(20, 20, 150, 30));
btn.caption("Quit");
btn.events().click(API::exit);
fm.show();
exec();
}
Walkthrough Line-by-line
#include <nana/gui/wvl.hpp>
This line includes the Nana.GUI class definition.
#include <nana/gui/widgets/button.hpp>
This line includes the Nana.GUI.button class definition.
int main()
{
The main()
function is the entry point to the program. Almost always when using Nana.GUI,
main()
only needs to perform some kind of initialization before passing the control to the Nana.GUI
library, which then tells the program about the user's actions via events.
using namespace nana;
Specify the nominated namespace nana
can be used in main
function block scope.
In this example, the names form
, button
, events
, API
and exec
are defined in the namespace nana
.
With this using-directive, we can use these names directly in the main
function scope.
form fm;
This is the first piece of window-system code. A form
is created while the variable fm
is defined.
The form
is a window with title bar and a sizable border frame, it's fundamental that you can put
some widgets above it.
fm.caption("Hello World");
Set the form
to display the text "Hello World" in its title bar.
button btn(fm, nana::rectangle(20, 20, 150, 30));
After the form
, a button is created. In its constructor arg-list, the first argument tells
the btn
who the parent window is, and the following arguments describe the position and size of btn
.
btn.caption("Quit");
Set the btn
to display the text "Quit".
btn.events().click(API::exit);
event()
is a method that every Nana.GUI widgets provide, you can register an event callback
by using it. We want to exit the program while a mouse clicks on the btn. Now, register a callback
function on click
event.
form.show();
A form widget is never visible when you create it. You must call show()
to make it visible.
::nana::exec();
This is where the main()
passes the control to Nana.GUI, and exec()
will return when the
application exists. In exec()
, Nana.GUI processes the message loop and passes every event
on to the appropriate widgets.
}
btn.events().click(nana::API::exit);
What is nana::API::exit
? This is an API provided by Nana.GUI. Its prototype is
void exit()
. If exit()
is called, Nana.GUI destroy all the windows you've created and
the exec()
will return. Member event().click()
has a template argument. The argument can be a
function or a functor with a const nana::arg_mouse&
parameter or not.
Is it right to invoke exit()
in an event callback?
It is right because Nana.GUI guarantees a program correctness even when an invalid GUI objects handle is accesses .