Re: How to draw to a pixbuf?

On Tue, Sep 02, 2008 at 11:08:20AM +0100, Chris Vine wrote:
> It has also occurred to me that another and probably easier way to the
> same result is to create a drawable (such as a Gdk::Pixmap object), get
> a cairo_t surface for it with gdk_cairo_create(), and then after
> drawing create a GdkPixbuf object from it with
> gdk_pixbuf_get_from_drawable() which you can wrap as a Gdk::Pixbuf
> object.

Thanks for your help! I got everything working now, except one
minor detail: I'd like to create the icons with partial transparency.
However, I can't seem to get that to work. It works for the stock
icons (ie 'Gtk::Stock::ABOUT' is transparent, I think, around the star).

The code I have now is:

void MyChessboardWidget::initialize_menus(void)
  M_icon_factory = Gtk::IconFactory::create();

  GdkColor light_square_color;

  int color_count = 0;
  for (bool iswhite = false; color_count < 2; ++color_count, iswhite = true)
    using namespace cwmm;
    Type type(nothing);
      Glib::RefPtr<Gdk::Pixmap> icon_pixmap = Gdk::Pixmap::create(M_drawable->get_window(), 16, 16);
      Cairo::RefPtr<Cairo::Context> cairo_context = icon_pixmap->create_cairo_context();
      cairo_t* cr = cairo_context->cobj();
      cairo_set_source_rgb(cr, / 65535.0, / 65535.0, / 65535.0);
      std::string icon_str(iswhite ? "white_" : "black_");
      switch (type())
        case nothing_bits:
          icon_str += "nothing";
        case pawn_bits:
          draw_pawn(cr, 8, 8, 16, iswhite);
          icon_str += "pawn";
        case rook_bits:
          draw_rook(cr, 8, 8, 16, iswhite);
          icon_str += "rook";
        case knight_bits:
          draw_knight(cr, 8, 8, 16, iswhite);
          icon_str += "knight";
        case bishop_bits:
          draw_bishop(cr, 8, 8, 16, iswhite);
          icon_str += "bishop";
        case queen_bits:
          draw_queen(cr, 8, 8, 16, iswhite);
          icon_str += "queen";
        case king_bits:
          draw_king(cr, 8, 8, 16, iswhite);
          icon_str += "king";
      icon_str += "_icon";
      Glib::RefPtr<Gdk::Pixbuf> icon_pixbuf = Gdk::Pixbuf::create(Glib::RefPtr<Gdk::Drawable>::cast_static(icon_pixmap), 0, 0, 16, 16);
      // Does not work: icon_pixbuf->add_alpha(true, 255, 255, 255);
      M_icon_factory->add(Gtk::StockID(icon_str.c_str()), Gtk::IconSet(icon_pixbuf));

      do { TypeData type_data = { type() + 1 }; type = type_data; } while (type() == 4);
    while (type() != 8);

  m_refActionGroup = Gtk::ActionGroup::create();
  m_refActionGroup->add(Gtk::Action::create("PlacepieceMenu", "Placepiece Menu"));
  m_refActionGroup->add(Gtk::Action::create("PlacepieceBlackPawn", Gtk::StockID("black_pawn_icon"), "Black Pawn"),
          sigc::mem_fun(*this, &MyChessboardWidget::on_menu_placepiece_black_pawn));
  m_refActionGroup->add(Gtk::Action::create("PlacepieceBlackRook", Gtk::StockID("black_rook_icon"), "Black Rook"),
          sigc::mem_fun(*this, &MyChessboardWidget::on_menu_placepiece_black_rook));
  m_refActionGroup->add(Gtk::Action::create("PlacepieceBlackKnight", Gtk::StockID("black_knight_icon"), "Black Knight"),
          sigc::mem_fun(*this, &MyChessboardWidget::on_menu_placepiece_black_knight));
... and so on.

This code is now using 'light_square_color' as background color, but I tried to
use say 'pink' as background color (and I tried black and white too) and then
tried to replace that with transparency with the Pixbuf::add_alpha() call, but that
never worked :/ It has no visible effect.

I also tried to create a cairo contex with alpha from the start, but that has
problems: M_drawable->get_window()  (my main window) simply has no transparancy,
it's my screen - and if I explicitely set the depth to 32 with:

  Glib::RefPtr<Gdk::Pixmap> icon_pixmap = Gdk::Pixmap::create(Glib::RefPtr<Gdk::Drawable>(0), 16, 16, 32);

then it complains about not having a colormap. If then I add the colormap from
M_drawable I get a runtime error that the depth's aren't matching.

Carlo Wood <carlo alinoe com>

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