Re: [gtk-list] How to use GtkText?




Cheng-Chang Wu <C.Wu@ping.de> writes:

> Hello!
> 
> I can't not figure out, how can I put a GtkText on GtkButton, the
> following is my test program, which always
> gives me a error message like 
> 
>     ** ERROR **: sigsegv caught 

The problem you are having, is that in order to call
gtk_widget_realize, on a widget, the widget must already be
added to a heirarchy. So you should create the window first,
then create the button, and add it to the window, then create
the text widget, and add it to the button; then realize the
text widget.

After making those changes, you'll then get a message warning
you that the widget isn't basic. 

What that means is that the button widget wants its children to be
"BASIC" widgets; that is widgets that don't take any input.  (Reason:
Who gets a button or key press in the widget: the Button widget or the
child?) But we can fake it here, as demonstrated below.

To answer your other question, I don't think there is a good reason
why the text widget needs to be realized before adding text - it
was just written that way.

After all that trouble, I think you'll find that the result isn't
all that desirable. The only case where I can see using it is if
you need multiple lines of text in the button. (This capability
will be added to labels in the near future).

But try out the following and see how you like it. I changed the
font to something that exists on my system, and used it when
inserting text, just for kicks.

Regards,
                                        Owen
--------
#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
  GtkWidget* window;
  GtkWidget* button;
  GtkWidget* text;
  GdkFont* font;

  gtk_init (&argc, &argv);

  font = gdk_font_load
    ("-adobe-utopia-regular-r-*-*-25-*-*-*-*-*-iso8859-1");

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  button = gtk_button_new ();
  gtk_container_add (GTK_CONTAINER (window), button);
  gtk_widget_show (button);

  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
			     GTK_SIGNAL_FUNC (gtk_widget_destroy),
			     GTK_OBJECT (window));

  text = gtk_text_new (NULL, NULL);
  GTK_WIDGET_SET_FLAGS (text, GTK_BASIC);
  gtk_widget_show (text);
  gtk_container_add (GTK_CONTAINER (button), text);

  gtk_widget_realize (text);
  gtk_text_insert (GTK_TEXT (text), font, 0, 0, "Quit", -1);

  gtk_widget_show (window);

  gtk_main ();

  return 0;
}




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