/** @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;
}

