Re: Resizing window with Images.



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I will try to answer your image question, so here is a class definition
I use (irrelevant code has been excised):

class MainMenu : public Gtk::Window
{
public:
	MainMenu();
	virtual ~MainMenu();
	gboolean appIsReady;
	Glib::ustring userid;

protected:

	Glib::RefPtr<Gdk::Pixbuf> image;

	// a pointer to a DrawingArea
	// widget... you must have this
	Gtk::DrawingArea *da;
	Gtk::VBox *m_Box;

	virtual bool on_expose_event(GdkEventExpose* event);
	gint height;
	gint width;

  //Child widgets:
	Gtk::VBox *m_Box;
};

Here is the constructor for this code (irrelevant code excised):

MainMenu::MainMenu()
{
	// this is a class that I
	// wrote to manage images
	// in my application, so I'm
	// assuming that you know how
	// to initialize a pixbuf
	FisImages fi;
	image = fi.fiscal_logo;

	// this code sets the original
	// application window size to
	// match the image dimensions

	width = image->get_width();
	height = image->get_height();
	set_default_size(width, height);
	set_position(Gtk::WIN_POS_CENTER_ALWAYS);	

	// create a drawing area to render the
	// background image
	da = new Gtk::DrawingArea;

	// create a vertical box into which to
	// put the drawing area or any other
	// objects like a menu bar above the
	// background image or status
	// widget below it.

	m_Box = Gtk::manage(new Gtk::VBox(FALSE,2));

	// put the drawing area into the box
	m_Box->pack_start(*da);

	// add the box to your window
	add(*m_Box);

	show_all_children();
}

Now, where the rubber meets the road, the "on_expose_event":

// function to redraw the window background
// upon any window exposure event.
bool MainMenu::on_expose_event(GdkEventExpose* event)
{

	Glib::RefPtr<Gdk::Pixbuf> tmpimage =
		image->scale_simple(da->get_width(),
		da->get_height(),Gdk::INTERP_BILINEAR);

	tmpimage->render_to_drawable(da->get_window(), 					
da->get_style()->get_white_gc(),	
		0, 0, 0, 0, da->get_width(), 							da->get_height(),
		Gdk::RGB_DITHER_NONE, 0, 0);

	return true;
}

If you set your application up this way, your background image will
always be resized to whatever size your application window is set.  The
key thing to get from this is using the "on_expose_event" method to draw
any and everything in your application window.  NEVER draw anything into
your window outside this method.

I hope this helps.



/*Bob Caryl*
Fiscal Systems,Inc.
256.772.8920 Ext. 108
http://www.fis-cal.com <http://www.fis-cal.com/>/

Samvel wrote:
> Hi,
> 
> I have an application that reads picture from a file and shows it in a  
> window. The problem is with resizing main window: image is not scaled and  
> widgets are left as they showed up at first /Image is showed in Gtk::Image  
> widget/. I found only this:
> 
>    1. connect to signal_check_size() of the Main Window.
>    2. call resize_children() from function say on_signal_check_size()
> 
> But if I try to scale Image in on_signal_check_size() my application  
> simply hangs up. Is there any way to connect to a sort of "resize" signal  
> that is emitted only as Window's size is really changed and mouse button  
> released /user finished playing with borders and window is let alone/?
> 
> One more thing. I have a button in my program that should maximize Window.  
> I was surfing Internet + Google and found only one working idea of  
> implementing it /simple resize does not give desired effect/:
> 
>    while( Gtk::Main::events_pending()){
>      // Processing pending events
>      Gtk::Main::iteration( false);
>    }
>    get_window()->resize( nWidth, nHeight);
>    while(Gtk::Main::events_pending()){
>      // Processing pending events
>      Gtk::Main::iteration(false);
>    }
>    move( nXPos, nYPos);
>    resize_children();
> 
> Now I have to resize image after maximization but simple line like /I add  
> it right after resize_children() call/:
> 
>    _oImage.set( _oPixBuf->scale_simple( _oFrame.get_width() - _nXPad * 2,  
> _oFrame.get_height() - _nYPad * 2, Gdk::INTERP_NEAREST));
> 
> does not work correctly /Image is put into a Frame and Frame is in Window;  
> _nXPad, _nYPad - padding's inside frame/.
> It looks like resize_children() does not affect widgets immediately so  
> _oPixBuf is not scaled correctly because Frame didn't change it's size  
> yet: the Image is scaled according to the last used Frame size. Suppose  
> you have screen 1600x1200 and initial frame of 640x480. You press button  
> and image is not scaled but window is maximized. You press button again  
> /reverse action - get back into a last used geometry: 1600x1200 ->  
> 640x480/ and image is scaled to 1600x1200 but frame now is 640x480 though  
> resize_children() is called before scaling Image.
> 
> Any ideas? Help is really appreciated.
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFE/NKuCj6XIbb5UIRAg1IAJ4klnuNpxD6CIHTupTemXpG6kGerwCgvPgh
oIbuuy4ye8AsLaWG7cht+t4=
=QB0S
-----END PGP SIGNATURE-----



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