Re: Help with transparent pixmaps



In message <003501c08ab4$4d577ee0$ea57623e e6z1t6>you write:
>Hi,
>    I have a problem: I am in need of displaying a pixmap widget with a
>transparent area... but I want to control and change the shape of this area
>each time I create it. So, I have the non-trasparent pixmap in a file (png,
>let's say), and the 'mask' for the alpha channel in a bicolor one. Now, how
>can I assemble the two?? I've checked the header files of gdk_imlib, gdk_h
>and gtkpixmap, and it seems that nothing suits my need... well, I figured it
>could be done with imlib... apparently, not! So I wrote something like:

1) check the "shapes" example in the testgtk.c file
2) understand gtk_widget_shape_combine_mask()
2) the following code should give you some ideas, even though its in C++

/*
    Copyright (C) 1999 Paul Barton-Davis 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    $Id: shaped_window.cc,v 1.4 2000/06/20 16:50:13 pbd Exp $
*/

#include <gtk--.h>
#include <gtkmmext/pix.h>
#include <gtkmmext/shaped_window.h>

using namespace Gtkmmext;

ShapedWindow::ShapedWindow (Pix *pixset, Gtk::Widget *w)

{
	pix = pixset;
	current_pix = 0;
	exposed = false;
	shapeable_parent = w;
}

ShapedWindow::~ShapedWindow ()

{
	finish_pix (pix);
}

void
ShapedWindow::set_current_pix (size_t n)

{
	n = MIN(n,pix->max_pixmap());

	if (n != current_pix) {
		current_pix = n;
		draw_pixmap ();
	}
}

void
ShapedWindow::realize_impl ()

{
	gint width, height;
	Gtk::Widget *parent;

	Gtk::DrawingArea::realize_impl ();

	mystyle = get_style()->gtkobj();

	if (!shapeable_parent) {
		parent = this;
	} else {
		parent = shapeable_parent;
	}
	
	pix->generate (get_window(),
		       (GdkColor *) &mystyle->bg[GTK_STATE_NORMAL]);
	
	gdk_window_get_size (pix->pixmap (0), &width, &height);
	set_usize (width, height);

	gtk_widget_shape_combine_mask (GTK_WIDGET(parent->gtkobj()),
				       pix->shape_mask (0),
				       0, 0);

	exposed = true;
	draw_pixmap ();
}

gint
ShapedWindow::expose_event_impl (GdkEventExpose *ev)

{
	if (ev->count == 0) {
		draw_pixmap ();
	}
	return TRUE;
}

void
ShapedWindow::state_changed_impl (GtkStateType somestate)

{
	current_pix = (size_t) get_state() % pix->n_pixmaps();
	draw_pixmap();
}

void
ShapedWindow::draw_pixmap ()

{
	if (exposed) {

		if (!pix->homogenous()) {
			Gtk::Widget *parent;
			
			if (!shapeable_parent) {
				parent = this;
			} else {
				parent = shapeable_parent;
			}

			gtk_widget_shape_combine_mask 
				(GTK_WIDGET(parent->gtkobj()),
				 pix->shape_mask (current_pix),
				 0, 0);
		}
		
		gdk_draw_pixmap (get_window(),
				 mystyle->bg_gc[get_state()],
				 pix->pixmap (current_pix),
				 0, 0, 0, 0,
				 -1, -1);
	}
}

/*
    Copyright (C) 1999 Paul Barton-Davis 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    $Id: shaped_window.h,v 1.5 2000/06/06 05:41:59 pbd Exp $
*/

#ifndef __gtkmm_shapedwindow_h__
#define __gtkmm_shapedwindow_h__

namespace Gtkmmext {

class Pix;

class ShapedWindow : public Gtk::DrawingArea

{
  private: 
        Gtk::Widget *shapeable_parent;
	Pix *pix;
	const GtkStyle *mystyle;
	int current_pix;
	void draw_pixmap ();
	bool exposed;

 protected:
	virtual void realize_impl ();
	virtual gint expose_event_impl (GdkEventExpose *);
	virtual void state_changed_impl (GtkStateType);

  public:
       ShapedWindow (Pix *pix, Gtk::Widget *shapeable_parent = 0);
       virtual ~ShapedWindow();

       void set_current_pix (size_t pixnum);
};

} /* namespace Gtkmmext */

#endif // __gtkmm_shapedwindow_h__




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