Re: FW: [gtkmm] drag and drop with multiple targets



pks timing com writes:
 > 
 > if subclassing the widget you will have hundreds of still doesn't sound
 > appealing, and
 >
[snip]
 >
 > or subclass, and have the "this" pointer at your disposal ;)

lots of talk about subclassing, and a SigC::bind example ...
here is how you'ld get the same job done by deriving your widget.

//-------------------------------------------------------------------

#include <gtkmm.h>
#include <iostream>

using namespace std;

class DerivedLabel : public Gtk::Label
{
public:

    DerivedLabel(int n);

protected:

    // for most every signal_foo() you can override a
    // virtual function on_foo() ...

    // override signal_show()'s counterpart
    virtual void on_show();

    // add a number to the mix because we can
    int _number;
};

class Example : public Gtk::Window
{
public:
    Example();
    Gtk::VBox _vbox;
    Gtk::Label *_lbls[5];
};

Example::Example()
    : Gtk::Window(),
      _vbox(false, 5)
{
    for (int i = 0; i < 5; i++)
    {
	_lbls[i] = new DerivedLabel(i*i);
	_vbox.pack_start(*_lbls[i], false, false);
	cout << "_lbls[" << i << "] = " << _lbls[i] << endl;
    }
    cout << endl;

    add(_vbox);
    show_all();
}

DerivedLabel::DerivedLabel(int n)
    : Gtk::Label("foo"),
      _number(n)
{
}

void
DerivedLabel::on_show()
{
    cout << "label " << this << " is being shown" << endl;
    //                  ^^^^
    //             the label, not the Example window

    cout << "\tits number is " << _number << endl;

    // do as a normal label does
    Gtk::Label::on_show();
}

int
main(int argc, char *argv[])
{
    Gtk::Main kit(argc, argv);
    Example example;
    kit.run(example);
    return 0;
}

//--------------------------------------------------------------

I hope this clarifies the alternative.  Good Luck!



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]