Nana C++ Library  
open-source C++ framework project 
The Programmer's Guide 
nana::gui::label

Description

A label widget can dispaly a text string and images.

Model of

Widget Window 

Public base classes

class widget_object<widget_tag>: public widget

Typedefs

C++ 03
struct command
{
    enum t{enter, leave, click};
};
Defines the event type for format listener.

C++11
enum class command{ enter, leave, click }; Ditto.

Members

label() The construction.
label(window wd, bool visible) Creates a label.
label(window wd, const nana::string& title, bool visible = true) Ditto.
label(window wd, const nana::char_t* title, bool visible = true)  Ditto.
label(window wd, const rectangle& r = rectangle(), bool visible = true) Ditto.
void format(bool enter_format_mode) Switchs the format mode of the widget.
void transparent(bool) Switchs the label widget to the transparent background mode.
bool transparent() const True if the label is transparent background mode.
nana::size measure(unsigned limited) const Return the size of the text. If the limited is not zero, it returns the size that the text changes lines when a line of text beyonds the width specified by limited

C++03
void add_format_listener(const nana::functor<void(command::t, const nana::string&)> & )  Adds the format listener.
void text_align(nana::align::t, nana::align_v::t = nana::align_v::top) Set the text alignment.

C++11
void add_format_listener(const std::function<void(command, const nana::string&)>&) Ditto.
void add_format_listener(std::function<void(command, const nana::string&)> && ) Ditto.
void text_align(nana::align, nana::align_v = nana::align_v::top) Set the text alignment.

File

nana/gui/widgets/label.hpp

Notes

1. It is not good efficient that the background mode of the label is transparent.

2. If the caption of a lable contains a character '\n', the label will hide the character and display the text string after '\n' in a new line.

3. The format mode accepts the definition for displaying mutile-style text. For example.

label.format(true);
label.caption(STR("Hello, <bold=true>This is a bold text</>"));
Label with bold text 

label.caption(STR("Hello, <bold=true color=0xff0000>This is a bold red text</>"));
Label with bold red text 

label.caption(STR("Hello <bold=true color=0xff0000 font=\"Consolas\">This is a bold red Consolas text</>"));
Label with bold red Consolas text 

label.caption(STR("Hello, <bold=true color=0xff0000 font=\"Consolas\" url=\"http://nanapro.sourceforge.net\">This is a bold red Consolas text</>"));
Arrow cursor becomes a hand when the cursor moves over the red text. Meanwhile, it is open a web browser to open the URL by clicking the red text.[NOTE: the url only works under Windows]

using namespace nana::gui;

//void listener(label::command cmd, const nana::string& s) //C++11
void listener(label::command::t cmd, const nana::string& s)
{
    if(label::command::click == cmd)
    {
        msgbox mb(STR("target clicked"));
        mb<<STR("the target \"")<<s<<"\" is clicked";
        mb();
    }
}

int main()
{
    form fm;
    label lab(fm, nana::rectangle(0, 0, 100, 40));
    lab.format(true);
    lab.add_format_listener(listener);
    lab.caption(STR("Click <color=0xFF target=\"target id\">here</>"));
    fm.show();
    exec();
}

4, Some reserved words: red, green, blue, white, black. It can simplify the format text and increas it readable.
lab.caption(STR("Click <color=0xFF target=\"target id\">here</>"));
vs
lab.caption(STR("Click <blue target=\"target id\">here</>"));

5, Image is supported for format mode.
lab.caption(STR("<image=\"file.bmp\"><bold red size=20>Nana C++ Library</>"));
As you see, the image tag has not a close-tag </>
Defaultly, the image is displayed with its orignal size. With specifying a size, we can get a proper size by which the image is displayed.
label.caption(STR("<image=\"file.bmp\" size=(150,150)><bold red size=20>Nana C++ Library</>"));
Specifying a proper size
size=(150,150) means that it stretchs the image to the specified size.
In many situations, we want to display the image as it is if it is greater than/less than a specified size. There are two reserved words can achieve it.
max_limited: stretchs the image to the specified size with preserving aspect ratio when its one of edge beyonds the specified size.
min_limited: stretchs the image to the specified size with preserving aspect ratio when its one of edge is less than the specified size.

label.caption(STR("<image=\"file.bmp\" size=(150,150) max_limited><bold red size=20>Nana C++ Library</>"));
A Proper Size

6, Alignments for format mode.
Defaulty, the alignment is baseline-aligned. The library provides 4 kinds of alignment which are: top, center, bottom and baseline. They are useful tools when display texts with different size in a line.
label.caption(STR("<size=12 top>top</><size=14 center>center<size=16 bottom>bottom</><size=30>baseline</><size=10>small font by baseline</>"));
Label Alignment


See also

None.


Move to The Nana Programmer's Guide Main Page