dirk/C++/gtkmm/is_visible
Ravensburg, 10-02-13
is_visible – What Does It Return?
Today, I was thinking about Gtk::Widget::show(), Gtk::Widget::hide(), and Gtk::Widget::is_visible().
My gtkmm manual isn't really a help – documentation for these methods is missing.
I could had a look in the GTK+ manual but decided to make a sample application, instead.
These methods seem rather simple to use at the first glance.
However, if a certain behaviour is granted this can really simplify code.
Thus, I had two questions in mind:
- Does
Gtk::Widget::is_visible() return whether widget has been set to be visible, or whether it is really displayed?
- Are
Gtk::Widget::show()/Gtk::Widget::hide() safe against double call?
Source Code
is_visible.cc |
/** @file sample about show(), hide(), and is_visible()
*
* Last CVS checkin:
* $Date: $
* $Author: scheff $
*/
/**************************************************************************/
// standard C/C++ header:
#include <iostream>
// Gnome header:
#include <gtkmm/button.h>
#include <gtkmm/main.h>
#include <gtkmm/window.h>
using namespace std;
/**************************************************************************/
class Window: public Gtk::Window {
// methods:
protected:
/// @name Overloaded Event Handling
//@{
/// called when widget becomes visible.
virtual void on_show(void);
/// called when widget becomes invisible.
virtual void on_hide(void);
//@}
};
/**************************************************************************/
// Overloaded Event Handling
void Window::on_show(void)
{
cout << "Window::on_show()" << endl;
Gtk::Window::on_show();
}
void Window::on_hide(void)
{
cout << "Window::on_hide()" << endl;
Gtk::Window::on_hide();
}
/**************************************************************************/
/// called when button has been clicked.
static void onBtnClicked(Window *pWin)
{
cout << "win.is_visible(): " << (pWin->is_visible() ? "true" : "false")
<< endl;
}
/** waits for user confirm.
*
* @param action text with next action
*/
static void pause(const char *action)
{
cout << "Press [ENTER] to " << action << ": " << flush;
string str; getline(cin, str);
}
/** provides the main function of application.
*
* @param argc number of command line arguments
* @param argv pointer to array of pointers to strings with command line
* arguments
* @return 0 ... application exited regularly\n
* else ... execution of application aborted
*/
int main(int argc, char *argv[])
{
// init
pause("start");
{
Gtk::Main gtkMain(argc, argv);
// init GUI
Window win;
Gtk::Button gtkBtn("is_visible()");
gtkBtn.signal_clicked().connect(
sigc::bind<0>(sigc::ptr_fun(&onBtnClicked), &win));
gtkBtn.show();
win.add(gtkBtn);
cout << "Before gtkMain.run(win):" << endl
<< "win.is_visible(): " << (win.is_visible() ? "true" : "false")
<< endl;
// run
gtkMain.run(win);
cout << "After gtkMain.run(win):" << endl
<< "win.is_visible(): " << (win.is_visible() ? "true" : "false")
<< endl;
}
pause("continue");
{
Gtk::Main gtkMain(argc, argv);
// init GUI
Window win;
Gtk::Button gtkBtn("is_visible()");
win.add(gtkBtn);
cout
<< "win.is_visible(): " << (win.is_visible() ? "true" : "false")
<< endl
<< "gtkBtn.is_visible(): " << (gtkBtn.is_visible()? "true" : "false")
<< endl;
gtkBtn.show();
cout << "gtkBtn.show();" << endl
<< "win.is_visible(): " << (win.is_visible() ? "true" : "false")
<< endl
<< "gtkBtn.is_visible(): " << (gtkBtn.is_visible()? "true" : "false")
<< endl;
gtkBtn.hide();
cout << "gtkBtn.hide();" << endl
<< "win.is_visible(): " << (win.is_visible() ? "true" : "false")
<< endl
<< "gtkBtn.is_visible(): " << (gtkBtn.is_visible()? "true" : "false")
<< endl;
win.show();
cout << "win.show();" << endl
<< "win.is_visible(): " << (win.is_visible() ? "true" : "false")
<< endl
<< "gtkBtn.is_visible(): " << (gtkBtn.is_visible()? "true" : "false")
<< endl;
win.show();
cout << "win.show();" << endl
<< "win.is_visible(): " << (win.is_visible() ? "true" : "false")
<< endl
<< "gtkBtn.is_visible(): " << (gtkBtn.is_visible()? "true" : "false")
<< endl;
cout << "gtkMain.run(win)" << endl;
gtkMain.run(win);
}
// done
pause("finish");
return 0;
}
|
I compiled this sample on Linux with:
> g++ -o is_visible `pkg-config gtkmm-2.4 --cflags --libs` is_visible.cc↵
>
|
Output
| Console: |
|---|
Press [ENTER] to start: ↵
Before gtkMain.run(win):
win.is_visible(): false
Window::on_show()
|
| Snapshot |
 |
| Console/Interaction: |
Press [ENTER] to start:
Before gtkMain.run(win):
win.is_visible(): false
Window::on_show()
click on the button
win.is_visible(): true
click on the button
Window::on_hide()
After gtkMain.run(win):
win.is_visible(): false
Press [ENTER] to continue: ↵
win.is_visible(): false
gtkBtn.is_visible(): false
gtkBtn.show();
win.is_visible(): false
gtkBtn.is_visible(): true
gtkBtn.hide();
win.is_visible(): false
gtkBtn.is_visible(): false
Window::on_show()
win.show();
win.is_visible(): true
gtkBtn.is_visible(): false
win.show();
win.is_visible(): true
gtkBtn.is_visible(): false
gtkMain.run(win)
|
| Snapshot |
 |
| Console/Interaction: |
click on the button
Window::on_hide()
Press [ENTER] to finish: ↵
|
Conclusion
This looks fine.
(I'm rather not surprised.
It does what I expected.)
Gtk::Widget::is_visible() returns true, after Gtk::Widget::show() has been called.
This is even true when the parent window is not (yet) visible.
Gtk::Widget::show()/Gtk::Widget::hide() are safe against double calls.
Files
dirk/C++/gtkmm/is_visible
Last modified: Thu Feb 25 17:38:34 2010