Stage Widget

The Clutter::Gtk::Embed widget allows you to place a Clutter::Stage inside an existing GTK+ window. For instance, the window might contain other GTK+ widgets allowing the user to affect the actors in stage. Use Clutter::Gtk::Embed::create() to instantiate the widget and then add it to a container just like any other GTK+ widget. Call Clutter::Gtk::Embed::get_stage() to get the Clutter::Stage from the Clutter::Gtk::Embed widget so you can then use the main Cluttermm API.

When using the Clutter::Gtk::Embed widget you should use Clutter::Gtk::init() instead of Clutter::init() and Gtk::Main to initialize Clutter and gtkmm. And you should use the regular Gtk::Main::run() method to start the mainloop rather than Clutter::main().

For simplicity, all other examples in this document will instead use Clutter::Stage::get_default(), but all the techniques can also be used with a stage inside the Clutter::Gtk::Embed widget.

Clutter::Embed class reference

Example

The following example shows a Clutter::Gtk::Embed GTK+ widget and changes the stage color when a button is clicked.

Note that this example requires the clutter-gtkmm-0.8 library as well as the standard cluttermm-0.8 library. You can create an executable from this code like so, being careful to use backticks around the call to pkg-config. See also the Header Files And Linking section.

c++ -Wall -g `pkg-config --cflags --libs clutter-gtkmm-0.8` -o example main.cc

Figure 4.2. Stage Widget

Stage Widget

Source Code

File: main.cc

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

namespace
{

class StageWindow : public Gtk::Window
{
public:
  StageWindow();
  virtual ~StageWindow();

private:
  Clutter::Gtk::Embed* embed_;
  Glib::RefPtr<Clutter::Stage> stage_;
  bool colored_;

  void on_button_clicked();
  static bool on_stage_button_press(Clutter::ButtonEvent* event);
};

StageWindow::StageWindow()
:
  embed_(0),
  colored_(false)
{
  Gtk::Box *const box = new Gtk::VBox(false, 6);
  add(*Gtk::manage(box));

  Gtk::Button *const button = new Gtk::Button("Change color");
  box->pack_end(*Gtk::manage(button), Gtk::PACK_SHRINK);

  embed_ = new Clutter::Gtk::Embed();
  box->pack_start(*Gtk::manage(embed_), Gtk::PACK_EXPAND_WIDGET);
  embed_->set_size_request(200, 200);

  button->signal_clicked().connect(sigc::mem_fun(*this, &StageWindow::on_button_clicked));

  stage_ = embed_->get_stage();
  stage_->reference();
  stage_->set_color(Clutter::Color(0, 0, 0)); // black
  stage_->signal_button_press_event().connect(&StageWindow::on_stage_button_press);

  show_all();
  stage_->show();
}

StageWindow::~StageWindow()
{}

void StageWindow::on_button_clicked()
{
  colored_ = !colored_;
  stage_->set_color((colored_) ? Clutter::Color(32, 32, 160)
    : Clutter::Color(0, 0, 0));
}

bool StageWindow::on_stage_button_press(Clutter::ButtonEvent* event)
{
  int x = 0;
  int y = 0;
  // TODO: Wrap properly
  clutter_event_get_coords(reinterpret_cast<Clutter::Event*>(event), &x, &y);

  std::cout << "Stage clicked at (" << x << ", " << y << ")\n";

  return true; // stop further handling of this event
}

} // anonymous namespace


int main(int argc, char** argv)
{
  try
  {
    Clutter::Gtk::init(&argc, &argv);
    Gtk::Main kit (&argc, &argv);

    StageWindow window;
    Gtk::Main::run(window);
  }
  catch (const Glib::Error& error)
  {
    std::cerr << "Exception: " << error.what() << std::endl;
    return 1;
  }

  return 0;
}