Re:Help Required to Set a Entry



Try my code ......

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

GtkWidget *window;
GtkWidget *vpaned;
GtkWidget *text;
GtkWidget *entry;
GtkWidget * button;
GtkWidget * table;
GtkWidget *vscrollbar;
GtkWidget *text;
gchar      * data_from_text1;

First, thing that we want to do is to create function that the main function will call when click the button

void text_toggle_editable (GtkWidget *checkbutton,
                          GtkWidget *text)
{

data_from_text1 = gtk_entry_get_text(GTK_ENTRY(entry));
gtk_text_insert (GTK_TEXT (text), NULL, &text->style->black, NULL,
                  data_from_text1, -1);

}

void enter_callback( GtkWidget *widget,
                    GtkWidget *entry )
{
 gchar *entry_text;
 entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
 printf("Entry contents: %s\n", entry_text);
}

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

   gtk_init (&argc, &argv);

   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Send from entry to show on window");
   gtk_signal_connect (GTK_OBJECT (window), "destroy",
                       GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
   gtk_widget_set_usize (GTK_WIDGET(window), 450, 400);

/* create a vpaned widget and add it to our toplevel window */

   vpaned = gtk_vpaned_new ();
   gtk_container_add (GTK_CONTAINER(window), vpaned);
   gtk_paned_set_handle_size (GTK_PANED(vpaned),
                              10);
   gtk_paned_set_gutter_size (GTK_PANED(vpaned),
                              15);
   gtk_widget_show (vpaned);

/* end of create vpaned widget */

Second, create text widget for show output

/* Create the GtkText widget */
 text = gtk_text_new (NULL, NULL);
gtk_paned_add2 (GTK_PANED(vpaned), table);
gtk_text_set_editable (GTK_TEXT (text), TRUE);
 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);

 /* Add a vertical scrollbar to the GtkText widget */
 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);

 /* End of Create Text widget */

Third,create button for click to send text from entry widget

/*Create button here.....*/
table = gtk_table_new(10, 10, TRUE);  /*Create container */
gtk_widget_show(table);                    /*Show container */
gtk_text_set_editable(GTK_TEXT(text),FALSE);
button = gtk_button_new_with_label("Show");
gtk_table_attach_defaults(GTK_TABLE(table), button, 1, 2, 2, 3);
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(text_toggle_editable), text);
gtk_paned_add2 (GTK_PANED(vpaned), button);
gtk_widget_show(button);
/*End of create button*/

Fourth, create entry widget for recieve input from end-user
/*Create text entry widget*/
entry = gtk_entry_new_with_max_length (50);
gtk_table_attach_defaults(GTK_TABLE(table), entry, 0, 10, 0, 1);
gtk_signal_connect(GTK_OBJECT(entry), "activate",
                      GTK_SIGNAL_FUNC(enter_callback),
                      entry);
gtk_entry_set_text (GTK_ENTRY (entry), "hello");
gtk_entry_append_text (GTK_ENTRY (entry), " world");
gtk_entry_select_region (GTK_ENTRY (entry),
                            0,       GTK_ENTRY(entry)->text_length);
gtk_paned_add2 (GTK_PANED(vpaned), entry);
gtk_widget_show (entry);
/*end of create text entry widget */
gtk_widget_show (window);
   gtk_main ();
   return 0;
}

these code worked properly with GTK1_2  only, ^_^
Regards,

_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* http://join.msn.com/?page=features/virus




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