Gtkmm:: Basics Signals&Handler Widgets Adjustments

Widgets:

gtkmm applications consist of windows containing widgets, such as buttons and text boxes. In some other systems, widgets are called "controls". For each widget in your application's windows, there is a C++ object in your application's code. So you just need to call a method of the widget's class to affect the visible widget.

Widgets are arranged inside container widgets such as frames and notebooks, in a hierarchy of widgets within widgets. Some of these container widgets, such as Gtk::VBox, are not visible - they exist only to arrange other widgets.

Signals:

gtkmm, like most GUI toolkits, is event-driven. When an event occurs, such as the press of a mouse button, the appropriate signal will be emitted by the Widget that was pressed. Each Widget has a different set of signals that it can emit. To make a button click result in an action, we set up a signal handler to catch the button's "clicked" signal.   

signal 有这些表现:

  button.signal_clicked().connect( sigc::ptr_fun(&on_button_clicked) );
button.signal_clicked().connect( sigc::mem_fun(some_object, &some_class::on_button_clicked) );
button.signal_clicked().connect( sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &HelloWorld::on_button_clicked), "button 1") );
显然使用这种方法可以得到一个带有参数的handler(例子中为Glib::ustring).
(把各种数据转换字符串,使用osstream)
Intermediate Type

Some parts of the gtkmm API use intermediate data containers, such as Glib::StringArrayHandle instead of a specific Standard C++ container such as std::vector or std::list. You should not declare these types yourself -- you should use whatever Standard C++ container you prefer instead. gtkmm will do the conversion for you. Here are some of these intermediate types:


  • Glib::StringArrayHandle or Glib::ArrayHandle<Glib::ustring>: Use std::vector<Glib::ustring>, std::list<Glib::ustring>, const char*[], etc.

  • Glib::ListHandle<Gtk::Widget*>: Use std::vector<Gtk::Widget*>, std::list<Gtk::Widget*>, etc.

  • Glib::SListHandle<Gtk::Widget*>: Use std::vector<Gtk::Widget*>, std::list<Gtk::Widget*>, etc.

Hello World Example
in main()
Gtk::Main kit(argc, argv); HelloWorld helloworld; Gtk::Main::run(helloworld); 就不解释了,估计是个GUI都得有把……
in HelloWorld:public Window constructor, add(m_button), m_button.show(); 估计就是在window中添加一个widget...

Button

Gtk::Button is also a container so you could put any other widget, such as a Gtk::Image into it. Using the method of the Button , maybe add_xxx?

The Gtk::Button widget has the following signals, but most of the time you will just handle the clicked signal:

pressed

Emitted when the button is pressed.

released

Emitted when the button is released.

clicked

Emitted when the button is pressed and released.

enter

Emitted when the mouse pointer moves over the button's window.

leave

Emitted when the mouse pointer leaves the button's window.

To retrieve the state of the ToggleButton, you can use the get_active() method. This returns true if the button is "down". You can also set the toggle button's state, with set_active(). Note that, if you do this, and the state actually changes, it causes the "clicked" signal to be emitted. This is usually what you want.

Gtk::CheckButton inherits from Gtk::ToggleButton. The only real difference between the two is Gtk::CheckButton's appearance. You can check, set, and toggle a checkbox using the same
member methods as for Gtk::ToggleButton.

Like checkboxes, radio buttons also inherit from Gtk::ToggleButton, but these work in groups, and only one RadioButton in a group can be selected at any one time.
Group:
Way1:
Gtk::RadioButton::Group group = m_rb1.get_group();
m_rb2.set_group(group);
m_rb3.set_group(group);
Way2:
RadioButtons::RadioButtons()
{
Gtk::RadioButton::Group group;
Gtk::RadioButton *m_rb1 = Gtk::manage(
new Gtk::RadioButton(group,"button1"));
Gtk::RadioButton *m_rb2 = manage(
new Gtk::RadioButton(group,"button2"));
Gtk::RadioButton *m_rb3 = manage(
new Gtk::RadioButton(group,"button3"));
}
but we can't just m_rb2.set_group(m_rb1.get_group()); because the group is modified by set_group() and therefore non-const.

Range Widget

Gtk::Scale and Gtk::Scrollbar both inherit from Gtk::Range and share much functionality. They contain a "trough" and a "slider" (sometimes called a "thumbwheel" in other GUI environments). Dragging the slider with the pointer moves it within the trough, while clicking in the trough advances the slider towards the location of the click, either completely, or by a designated amount, depending on which mouse button is used. This should be familiar scrollbar behaviour.

As will be explained in the Adjustment section, all Range widgets are associated with a Adjustment object. To change the lower, upper, and current values used by the widget you need to use the methods of its Adjustment, which you can get with the get_adjustment() method. The Range widgets' default constructors create an Adjustment automatically, or you can specify an existing Adjustment, maybe to share it with another widget.

Scroll Bar

These are standard scrollbars. They should be used only to scroll another widget, such as, a Gtk::Entry, or a Gtk::Viewport, though it's usually easier to use the Gtk::ScrolledWindow widget in most cases.There are horizontal and vertical scrollbar classes - Gtk::HScrollbar and Gtk::VScrollbar.故而显然经常把他们的adjustment放在一起来用。

Scale Widget

Scale widgets can display their current value as a number next to the trough. By default they show the value, but you can change this with the set_draw_value() method.设置是否显示。对应有get

The value displayed by a scale widget is rounded to one decimal point by default, as is the value field in its Gtk::Adjustment. You can change this with the set_digits() method.有效位数。对应有get

Also, the value can be drawn in different positions relative to the trough, specified by the set_value_pos() method.

最大值和最小值都通过一个upper和lower来确定,而page决定了本身按钮的跳跃宽度,即最大值为upper-page。这个在glade里面可以调节。

The update policy of a Range widget defines at what points during user interaction it will change the value field of its Gtk::Adjustment and emit the value_changed signal. The update policies, set with the set_update_policy() method, are:

  • Gtk::UPDATE_CONTINUOUS - This is the default. The value_changed signal is emitted continuously, i.e. whenever the slider is moved by even the tiniest amount.

  • Gtk::UPDATE_DISCONTINUOUS - The value_changed signal is only emitted once the slider has stopped moving and the user has released the mouse button.

  • Gtk::UPDATE_DELAYED - The value_changed signal is emitted when the user releases the mouse button, or if the slider stops moving for a short period of time.

adjustment

Gtk::Adjustment(float value, float lower, float upper, float step_increment = 1, float page_increment = 10, float page_size = 0);

The value argument is the initial value of the adjustment, usually corresponding to the topmost or leftmost position of an adjustable widget.

The lower and upper arguments specifies the possible range of values which the adjustment can hold.

The step_increment argument specifies the smaller of the two increments by which the user can change the value, while the page_increment is the larger one.

The page_size argument usually corresponds somehow to the visible area of a panning widget. The upper argument is used to represent the bottom most or right most coordinate in a panning widget's child.

TODO: Investigate the upper argument properly. There was some unclear stuff about it not always being the upper value.



The adjustment object can be used as the constructor of the range object or be used with the member function set_adjustment();

To access the value of a Gtk::Adjustment, you can use the get_value() and set_value() methods:

As mentioned earlier, Gtk::Adjustment can emit signals.This is, of course, how updates happen automatically when you share an Adjustment object between a Scrollbar and another adjustable widget; all adjustable widgets connect signal handlers to their adjustment's value_changed signal, as can your program.

 Miscellaneous Widgets

Labels are the main method of placing non-editable text in windows, for instance to place a title next to a Entry widget. You can specify the text in the constructor, or with the set_text() method. The width of the label will be adjusted automatically. You can produce multi-line labels by putting line breaks (""n") in the label string.

The label text can be justified using the set_justify() method. The widget is also capable of word-wrapping - this can be activated with set_line_wrap().

Entry widgets allow the user to enter text (surprisingly enough).

You can change the contents with the set_text() method, and read the current contents with the get_text() method.

Occasionally you might want to make an Entry widget read-only. This can be done by passing false to the set_editable() method.

For the input of passwords, passphrases and other information you don't want echoed on the screen, calling set_visibility() with false will cause the text to be hidden.

You might want to be notified whenever the user types in a text entry widget. Gtk::Entry provides two signals, activate and changed, for just this purpose. activate is emitted when the user presses the enter key in a text-entry widget; changed is emitted when the text in the widget changes. You can use these, for instance, to validate or filter the text the user types.

 

SpinButtons use an Adjustment object to hold information about the range of values. These Adjustment attributes are used by the Spin Button like so:

  • value: value for the Spin Button

  • lower: lower range value

  • upper: upper range value

  • step_increment: value to increment/decrement when pressing mouse button 1 on a button

  • page_increment: value to increment/decrement when pressing mouse button 2 on a button

  • page_size: unused

Additionally, mouse button 3 can be used to jump directly to the upper or lower values.

The SpinButton can create a default Adjustment, which you can access via the get_adjustment() method, or you can specify an existing Adjustment in the constructor.

The number of decimal places can be altered using the set_digits() method. You can set the spinbutton's value using the set_value() method, and retrieve it with get_value().

The spin() method 'spins' the SpinButton, as if one of its arrows had been clicked. You need to specify a Gtk::SpinType to specify the direction or new position.

To prevent the user from typing non-numeric characters into the entry box, pass true to the set_numeric() method.

To make the SpinButton 'wrap' between its upper and lower bounds, use the set_wrap() method.

To force it to snap to the nearest step_increment, use set_snap_to_ticks().

You can modify the update policy using the set_update_policy() method, specifying either Gtk::UPDATE_ALWAYS or Gtk::UPDATE_IF_VALID. Gtk::UPDATE_ALWAYS causes the SpinButton to ignore errors encountered while converting the text in the entry box to a numeric value. This setting also therefore allows the SpinButton to accept non-numeric values. You can force an immediate update using the update() method.

 


Progress bars are used to show the status of an ongoing operation. For instance, a ProgressBar can show how much of a task has been completed.

To change the value shown, use the set_fraction() method, passing a double between 0 and 1 to provide the new percentage. where percentage is a number, from 0 to 1, indicating what fraction of the bar should be filled. A ProgressBaris horizontal and left-to-right by default, but you can change it to a vertical progress bar by using the set_orientation() method.


Besides indicating the amount of progress that has occured, the progress bar can also be used to indicate that there is some activity; this is done by placing the progress bar in activity mode. In this mode, the progress bar displays a small rectangle which moves back and forth. Activity mode is useful in situations where the progress of an operation cannot be calculated as a value range (e.g., receiving a file of unknown length).

To do this, you need to call the pulse() method at regular intervals. You can also choose the step size, with the set_pulse_step() method. When in continuous mode, the progress bar can also display a configurable text string within its trough, using the set_text() method.

posted on 2008-12-20 01:40  壶中仙  阅读(935)  评论(1)    收藏  举报

导航