1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
* Copyright 2008 Klaus Triendl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <gtkmm.h>
#include <sigx/sigx.h>
// fwd decl
class TheGUI;
class TheThread: public sigx::glib_threadable
{
public:
TheThread(TheGUI* thegui);
protected:
// virtuals from sigx::threadable
virtual void on_startup();
void on_first_idle();
void on_button_clicked();
private:
TheGUI* m_thegui;
};
class TheGUI: public Gtk::Window, public sigx::glib_auto_dispatchable
{
public:
TheGUI();
public:
// expose Gtk::Button::signal_clicked of m_btn
sigx::signal_f<Glib::SignalProxy0<void> > signal_button_clicked;
private:
void on_first_idle();
void on_thethread_ready();
private:
TheThread m_thethread;
Gtk::Button* m_btn;
};
#include <iostream>
TheThread::TheThread(TheGUI* thegui):
m_thegui(thegui)
{}
//virtual
void TheThread::on_startup()
{
// one-shot idle handler
maincontext()->signal_idle().connect(
sigc::bind_return(
sigc::mem_fun(this, &TheThread::on_first_idle),
false
)
);
}
void TheThread::on_first_idle()
{
std::cout << "TheTread connects to the button_clicked signal." << std::endl;
// connect to TheGUI::signal_button_clicked
m_thegui->signal_button_clicked().connect_notify(
sigc::mem_fun(this, &TheThread::on_button_clicked)
);
}
void TheThread::on_button_clicked()
{
std::cout << "TheThread got notified that the button was clicked" << std::endl;
}
TheGUI::TheGUI():
Gtk::Window(),
sigx::glib_auto_dispatchable(),
// set up signal functor with late object instance binding (because m_btn is still invalid
// but will be constructed later)
signal_button_clicked(*this, sigc::ref(m_btn), &Gtk::Button::signal_clicked),
m_thethread(this),
m_btn()
{
m_btn = Gtk::manage(new Gtk::Button("notify thread"));
m_btn->set_sensitive(false);
add(*m_btn);
// end the thread if close button is clicked
signal_delete_event().connect(
// hide parameter GdkEvent*
sigc::hide(sigc::bind_return(sigc::mem_fun(m_thethread, &TheThread::finish), false))
);
// one-shot idle handler to start the thread
Glib::signal_idle().connect(
sigc::bind_return(sigc::mem_fun(this, &TheGUI::on_first_idle), false)
);
show_all_children();
}
void TheGUI::on_first_idle()
{
std::cout << "starting TheThread." << std::endl;
// start the thread
m_thethread.run();
// it's not yet correct to activate the "notify thread" button because we don't
// know exactly when the thread will have connected to our signal unless we in turn
// let us notify but this is ok for this test example
m_btn->set_sensitive(true);
}
int main(int arg_count, char** args)
{
Glib::thread_init();
Gtk::Main main(arg_count, args);
TheGUI thegui;
main.run(thegui);
}
|