On Sun, 2001-08-26 at 06:02, Arno wrote:
> Hello.
>
> I have a gtk_entry and if I click on the button send it sends the text. I
> would like that one supports on enter makes the same thing. Somebody could
> help me?
>
Connecting the same callback to both the button's "clicked" and the
entry's "activate" event, as Steph suggested is probably what you want.
Markus suggestion, connecting a callback to the entry's
"key_pressed_event" and dispatching to other functions based on the key
value gives you a bit more control over the entry though. You can for
example use this to do auto-completion or prevent data in a "wrong"
format to be entered. In most cases the activate signal is what you
want to connect to, but there are times where the key_pressed_event is
useful.
Anyway, I've attached a small program with examples of both.
/mailund
--
I never realized it before, but having looked that over I'm certain
I'd rather have my eyes burned out by zombies with flaming dung sticks
than work on a conscientious Unicode regex engine.
--- Tim Peters, 3 Dec 1998
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
/* muck-up of send function */
static void
send (GtkEntry *entry)
{
g_print ("send: \"%s\"\n", gtk_entry_get_text (entry));
}
/* I use three different callbacks, mostly to let the output show
which signal is being processed. In a real application, you would
just led the button and activate signal call the same callback.
For the key-event a special callback is needed though, to process
the exact key, so you only send when you press enter and not each
time you change the text. */
static void
button_send (GtkWidget *dummy, GtkEntry *entry)
{
g_print ("button_send\n");
send (entry);
}
static void
activate_send (GtkWidget *dummy, GtkEntry *entry)
{
g_print ("activate_send\n");
send (entry);
}
static void
key_event (GtkWidget *dummy, GdkEvent *event, GtkEntry *entry)
{
g_print ("key_event\n");
switch (event->key.keyval)
{
case GDK_Tab:
if (strcmp ("a", gtk_entry_get_text (entry)) == 0)
gtk_entry_set_text (entry, "ape");
else if (strcmp ("b", gtk_entry_get_text (entry)) == 0)
gtk_entry_set_text (entry, "bonobo");
else if (strcmp ("c", gtk_entry_get_text (entry)) == 0)
gtk_entry_set_text (entry, "chimpanzee");
break;
case GDK_Return:
send (entry);
break;
}
}
int
main(int argc, char *argv[])
{
GtkWidget *win;
GtkWidget *vbox;
GtkWidget *entry;
GtkWidget *button;
gtk_init (&argc, &argv);
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
vbox = gtk_vbox_new (TRUE, 0);
gtk_container_add (GTK_CONTAINER (win), vbox);
entry = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
button = gtk_button_new_with_label ("Send");
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
/* setting up signals */
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (button_send),
entry);
gtk_signal_connect (GTK_OBJECT (entry), "activate",
GTK_SIGNAL_FUNC (activate_send),
entry);
gtk_signal_connect (GTK_OBJECT (entry), "key_press_event",
GTK_SIGNAL_FUNC (key_event),
entry);
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
Attachment:
pgpfUmIIpEtZ4.pgp
Description: PGP signature