Re: gtk_text_insert - beginner



 The code generated by glade is place in 6 files. If you did not change the
default settings, they will be

  main.c : Where the main function is placed.
  callback.h : The declarations to callbacks generated by glade. (i.e. -
on_button1_clicked() ; etc..
  callback.c : Where ou should put you code that handles the user interaction.
  support.c : some useful functions used during runtime. (create_pixmap,
lookup_widget() etc..);

   interface.h/ interface.c : Where the code for the interface is generated -
(DO NOT EDIT this files )


  So, text1 definition is inside interface.c ( use the grep program to see
where it is:  %grep text1 *.c  ).

  From the callback.c, it will not be visible (to the compipler) -- so there
are 2 solutions to get a pointer to the widget:

  1) use the generated function lookup_widget(GtkWidget* w, const char*
name_of_widget).  So it will be like this

 void on_button1_clicked(GtkButton* button1, gpointer user_data)
 {
        GtkWidget* text1 = NULL;
        /* now we get the pointer using the button passed in, we need to cast,
so the compiler does not give warnings.. */
        text1 = lookup_widget(GTK_WIDGET(button1), "text1");

        g_return_if_fails(text1 != NULL); /* oops -- the name of the widget is
wrong and could not be found... */

        /* if it found text1, life is god and it will reach here */
        gtk_text_insert(GTK_TEXT(text1),NULL,NULL,NULL,"some text",-1);
 }

 Explanation: You use the name of the widget (set in glade, on the name
property of the widget -- glade automatically gives a name .. such as button1,
text1, button25 ) You can change that to give a better name: something like:
"main_win_input_bt", or whetever naming convention you use. -- That is the
string you pass to the lookup_widget_function.

 Solution 2) On the callback widown in glade, when you generate the callback,
you can pass the object name as the user_data parameter. The name is the actual
name of the variable (which you can also change in glade). Pass this widget is
the "Object" value on the callback in glade. Then you can do something like:

  
 void on_button1_clicked(GtkButton* button1, gpointer user_data)
 {
        GtkWidget* text1 = NULL;
      
        text1 = GTK_WIDGET(user_data)

        g_return_if_fails(text1 != NULL); /* oops -- the user_data was not
setup correctly in glade by me !! my FALUT !!*/

        /* if we set the user data correctly in glade, we will be here */
        gtk_text_insert(GTK_TEXT(text1),NULL,NULL,NULL,"some text",-1);
 }

 Which approach is better ? It depends on your choice -- the lookup_widget
looks for the widget in a for loop, so it will be slower than passing the
pointer -- how slow ? Extremely insignificat slow, since the number of
interation will depend on the number of widget you have -- usually not more
than 100 -- You will not even be able to time the difference.
   
 Hope this helps.

HArring.
 
--- hracekp volny cz wrote:
Hello all

Firstly I am begineer with developing GTK+ and I using Glade and
Anjuta. I have got simple problem.
If I develop new application, I create GUI with Glade and all is OK.
I have got button1 widget there and text1 widget there.
If I insert signal on_button1_clicked I want after this click insert
to the text box some text, but compiler says me that text1 widget is
undeclared symbol.

void on_button1_clicked ...
{
      gtk_text_insert(GTK_TEXT(text1),NULL,NULL,NULL,"some text",-1);
}

Can you help me with problems, where I wrote code.
Can you send me emails to my address and to the conference.

Thank you very much
Petr

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


__________________________________________________
Do you Yahoo!?
Yahoo! News - Today's headlines
http://news.yahoo.com



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