Gtk::Socket problem?



Hello. I'm trying to embed drawing into a socket. The problem is that when I try,
it appears a second toplevel window.
I tried with the code attached. There are two files. The one writen in c/gtk+ works
well. But the one written in c++/gtkmm makes a second top level window instead of
embed the drawing.  I don't know the reason because they're almost the same to my eyes.
Anyone can help here? Is this a gtkmm-specific issue?
Thanks in advance.
#include <gtkmm/main.h>
#include <gtkmm/socket.h>
#include <gtkmm/box.h>
#include <gtkmm/window.h>
#include <SDL.h>
#include <cstdlib>
#include <stdexcept>
#include <sstream>
#include <cstdio>
#include <gtk/gtksocket.h>
#include <iostream>



class VentanaSDL
{
private:
	Gtk::Main main_;
	Gtk::Socket socket_;
	Gtk::Window window_;
	Gtk::VBox vbox_;
	SDL_Surface * superficie_;

public:

	
	VentanaSDL(int argc, char ** argv) : main_(&argc, &argv), superficie_(0)
	{		
		window_.set_title("Ventana SDL");
		window_.set_size_request(480, 272);
		socket_.set_size_request(480, 272);
		window_.set_border_width(6);
		vbox_.set_size_request(480, 272);
		
	}

	void ejecutar()
	{
		char winhack[10];
		vbox_.pack_start(socket_, false, false);
		window_.add(vbox_);


		g_assert(socket_.get_id() == gtk_socket_get_id( GTK_SOCKET(socket_.gobj())));		
		std::stringstream sdlhack;
		sdlhack << "SDL_WINDOWID=" << socket_.get_id();
		

		SDL_putenv(const_cast<char *>(sdlhack.str().c_str()));		

		if (SDL_Init(SDL_INIT_VIDEO))
			throw std::runtime_error("Error al inicializar la SDL");
		else
			superficie_ = SDL_SetVideoMode(480, 272, 0, 0);			

		std::cout << sdlhack.str() << '\n';

		window_.show_all();		
		main_.run(window_);			
	}

	~VentanaSDL()
	{
		if (superficie_) SDL_FreeSurface(superficie_);
	}
};



int main(int argc, char ** argv)
{
	VentanaSDL app(argc, argv);

	//g_timeout_add
	app.ejecutar();
}
#include <gtk/gtk.h>
#include <SDL.h>

/*
 * GTK+ SDL Window reparenting test
 * written by Mikael Eriksson <mikael_miffe_eriksson yahoo se>
 *
 * Compile with:
 * cc -g -o gtk-sdl-test gtk-sdl-test.c `pkg-config --cflags --libs gtk+-2.0 sdl`
 */

gboolean do_sdl_stuff( gpointer data ) {
	SDL_Event event;
	SDL_Surface *display = data;

	while ( SDL_PollEvent( &event ) ) {
		/* Handle quit event, not sure if this will ever appear */
		if ( event.type == SDL_QUIT ) return FALSE;
		/* Handle clear userevent */
		if ( event.type == SDL_USEREVENT && event.user.code == 0 ) {
			SDL_FillRect( display, NULL, 0 );
			SDL_Flip( display );
		}
		/* Handle draw rect userevent */
		if ( event.type == SDL_USEREVENT && event.user.code == 1 ) {
			SDL_Rect rect;
			
			rect.x = rand() % 320;
			rect.y = rand() % 240;
			rect.w = rand() % 100 + 10;
			rect.h = rand() % 100 + 10;

			printf( "%ux%u+%u+%u\n", rect.w, rect.h, rect.x, rect.y );

			SDL_FillRect( display, &rect, SDL_MapRGB( display->format, rand()%255, rand()%255, rand()%255 ) );
			SDL_Flip( display );
		}
	}

	return TRUE;
}

void button_clicked( GtkButton *button, gpointer data ) {
	/* Put draw rect userevent on queue */
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = 1;
	SDL_PushEvent( &event );
}

void clear(void) {
	/* Put clear userevent on queue */
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = 0;
	SDL_PushEvent( &event );
}

int main( int argc, char **argv ) {
	GtkWidget *window, *sock, *box, *button;
	char winhack[1024];
	SDL_Surface *display;

	gtk_init( &argc, &argv );

	/* Create widgets */
	window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
	sock = gtk_socket_new();
	box = gtk_vbox_new( FALSE, 0 );
	button = gtk_button_new_with_label( "Press me!" );

	/* Setup socket widget */
	gtk_widget_set_size_request( sock, 320, 240 );
	gtk_box_pack_start( GTK_BOX(box), sock, TRUE, FALSE, 0 );

	/* Setup button */
	gtk_container_set_border_width( GTK_CONTAINER(button), 5 );
	g_signal_connect( G_OBJECT(button), "clicked", G_CALLBACK(button_clicked), display );
	gtk_box_pack_start( GTK_BOX(box), button, FALSE, FALSE, 0 );

	/* Setup window */
	gtk_container_add( GTK_CONTAINER(window), box );
	g_signal_connect( G_OBJECT(window), "delete-event", G_CALLBACK(gtk_main_quit), NULL );
	gtk_widget_show_all( window );

	/* Make SDL windows appear inside socket window */
	snprintf( winhack, sizeof winhack, "SDL_WINDOWID=%i", gtk_socket_get_id( GTK_SOCKET(sock) ) );
	SDL_putenv( winhack );

	/* Init SDL */
	if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
		fprintf( stderr, "%s.\n", SDL_GetError() );
		return EXIT_FAILURE;
	}

	/* Create SDL window */
	if ( !( display = SDL_SetVideoMode( 320, 240, 0, 0 ) ) ) {
		fprintf( stderr, "%s.\n", SDL_GetError() );
		return EXIT_FAILURE;
	}

	/* Clear the SDL window */
	clear();

	/* Setup SDL event handlig timer */
	g_timeout_add( 100, do_sdl_stuff, display );

	printf("%s", winhack);

	/* Start main loop */
	gtk_main();

	return 0;
}


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