Entry callback not called on pressing return.



Hi everyone,

I'm learning gtk+ and am writting a small frontend to anther program of
mine. Well, to test my understanding I wrote some code with only a text
widget and an entry widget. When text has been typed into the entry widget
and return pressed the text is copied into the text widget and erased from
the entry box.

This code worked (note past-tense). After a make clean; make it stopped
working. Nothing changed (that I can think of) except I untar'ed the 2.2.16
linux kernel under /usr/src/linux.

I'm using gtk+ 2.2.6, it must be a fluke that it worked before because I
tried it under NetBSD w/ gtk+ 2.2.8 and I got the same behavior I now get
under linux.

Anyone help! I really need some insight on this.

Thanks, Matthew

#include <gtk/gtk.h>
#include <string.h>

void close_application( GtkWidget *widget, gpointer data ) {

	gtk_main_quit();
}

void entry_activated( GtkWidget* entry, gpointer tw ) {

	gchar* entry_text;
	GdkFont *fixed_font;
	GtkWidget *text_widget = (GtkWidget*)tw;

	fixed_font = gdk_font_load(
	             "-misc-fixed-medium-r-*-*-*-140-*-*-*-*-*-*" );
	
	entry_text = gtk_entry_get_text( GTK_ENTRY(entry) );
	if( strlen( entry_text ) == 0 );
		return;

g_print( "in callback\n" );

	/* erase any input in entry field */
	gtk_entry_set_text( GTK_ENTRY(entry), "" ); 

	gtk_text_freeze( GTK_TEXT(text_widget) );
	gtk_text_insert( GTK_TEXT(text_widget), fixed_font, 
	                 &text_widget->style->black, NULL, entry_text, -1 );
	gtk_text_insert( GTK_TEXT(text_widget), NULL, NULL, NULL, "\n", -1 ); 
	gtk_text_thaw( GTK_TEXT(text_widget) );
}


int main( int argc, char *argv[] ) {

	GtkWidget *window;
	GtkWidget *vbox1, *hbox1;
	GtkWidget *text;
	GtkWidget *text_entry, *label;
	GtkWidget *table;
	GtkWidget *vscrollbar;

	gtk_init( &argc, &argv );

	window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
	gtk_window_set_policy( GTK_WINDOW(window), TRUE, TRUE, FALSE );
	gtk_widget_set_usize( window, 600, 500 );
	gtk_signal_connect( GTK_OBJECT( window ), "destroy",
	                    GTK_SIGNAL_FUNC( close_application ), NULL );

	gtk_window_set_title( GTK_WINDOW( window ), "Beval" );
	gtk_container_set_border_width( GTK_CONTAINER( window ), 5 );

	vbox1 = gtk_vbox_new( FALSE, 0 );
	gtk_container_add( GTK_CONTAINER( window ), vbox1 );
	gtk_widget_show( vbox1 );

	/* table holds the text widget and vscrollbar */
	table = gtk_table_new( 2, 2, FALSE );
	gtk_table_set_row_spacing( GTK_TABLE(table), 0, 2 );
	gtk_table_set_col_spacing( GTK_TABLE(table), 0, 2 );
	gtk_box_pack_start( GTK_BOX(vbox1), table, TRUE, TRUE, 0 );
	gtk_widget_show( table );

	/* text widget that displays results */
	text = gtk_text_new( NULL, NULL );
	gtk_table_attach( GTK_TABLE(table), text, 0, 1, 0, 1,
	                  GTK_EXPAND | GTK_SHRINK | GTK_FILL,
                          GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
	gtk_widget_show( text );
	gtk_widget_realize( text );

	vscrollbar = gtk_vscrollbar_new( GTK_TEXT(text)->vadj );
	gtk_table_attach( GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
	                  GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0 );
	gtk_widget_show( vscrollbar );

	hbox1 = gtk_hbox_new( FALSE, 0 );
	gtk_box_pack_start( GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0 );
	gtk_widget_show( hbox1 );

	label = gtk_label_new( "Enter expression: " );
	gtk_box_pack_start( GTK_BOX(hbox1), label, FALSE, FALSE, 0 );
	gtk_widget_show( label );

	text_entry = gtk_entry_new_with_max_length( 2048 );
	gtk_box_pack_start( GTK_BOX(hbox1), text_entry, TRUE, TRUE, 0 );
	gtk_signal_connect( GTK_OBJECT(text_entry), "activate",
	                    entry_activated, (gpointer)text );
	gtk_widget_show( text_entry );

	gtk_widget_show( window );

g_print( "entering gtk_main()\n" );
	
	gtk_main();

g_print( "leaving gtk_main()\n" );

	return(0);
}

-- 
"Never let your conscience trick you into owning up to the truth when there's
still some chance somebody might believe the lie." -- Charles Noblett




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