Set Focus to GtkEntry when GtkNotebook Tab selected
- From: "Thomas A. Moulton" <tom moulton us>
- To: GTK Devel List <gtk-app-devel-list gnome org>
- Subject: Set Focus to GtkEntry when GtkNotebook Tab selected
- Date: Wed, 26 Jun 2013 22:02:54 -0400
I have the following object:
GtkWindow
GtkNotebook
GtkBox
GtkScrolledWindow, GtkTextArea
GtkEntry
I am trying to have the Entry get focus when I change Tabs
When the program starts it does focus on Tab 1's entry but I can not get
it to keep focus
without clicking the entry area manually.
I set gtk_widget_set_can_focus(GTK_WIDGET(entry), 1);
notebook
g_signal_connect(G_OBJECT(nb), "switch-page",
G_CALLBACK(ChatNB_switch_page_cb), NULL);
G_MODULE_EXPORT void ChatNB_switch_page_cb(GtkNotebook *notebook,
GtkWidget *page, guint page_num, gpointer user_data) {
printf("switch-page %d\r\n", page_num);fflush(stdout);
if (page_num >= 0 && page_num < 5 && entries[page_num] != NULL) {
gtk_widget_grab_focus(GTK_WIDGET(entries[page_num]));
}
}
I have looked around and seen that this question is often asked but
answers are not clear...
i run the program below and click Tab 1 and then Tab 2
tom
Full single file example
/*
============================================================================
Name : Scrolled.c
Author : Tom Moulton
Version :
Copyright : (c) 2013 Thoomas A. Moulton, Public Domain
Description : Hello World in GTK+
============================================================================
*/
#include <gtk/gtk.h>
#include <string.h>
GtkNotebook *nb;
GtkEntry *entries[5];
void Enter_Chat(GtkEntry *entry, GtkTextBuffer *buf) {
char line[512];
const gchar *cmd;
GtkTextIter endit;
cmd = gtk_entry_get_text(entry);
strcpy(line, cmd);
strcat(line, "\r\n");
gtk_entry_set_text(entry, "");
gtk_text_buffer_get_end_iter(buf, &endit);
gtk_text_buffer_insert_with_tags(buf, &endit, line, -1, NULL, NULL);
}
gboolean Enter_focus_in_event(GtkEntry *entry, gpointer *buf) {
printf("Entry %x focus in event\r\n", (unsigned
int)entry);fflush(stdout);
//gtk_widget_grab_focus(GTK_WIDGET(entry));
return TRUE;
}
void Enter_grab_focus(GtkEntry *entry, gpointer *buf) {
printf("Entry %x grabbed focus\r\n", (unsigned
int)entry);fflush(stdout);
}
void Chat_scroll_to_end(GtkTextBuffer *buf, GtkTextView *text) {
GtkTextMark *mark;
mark = gtk_text_buffer_get_mark(buf, "eob");
gtk_text_view_scroll_mark_onscreen(text, mark);
}
void ChatNew(gchar *name) {
GtkBox *chat;
GtkTextBuffer *buf;
GtkTextView *text;
GtkEntry *entry;
GtkScrolledWindow *scroll;
GtkTextIter endit;
GtkLabel *label;
gint pg;
text = (GtkTextView *)gtk_text_view_new();
gtk_text_view_set_editable(text, FALSE);
gtk_text_view_set_wrap_mode(text, GTK_WRAP_WORD_CHAR);
gtk_text_view_set_cursor_visible(text, FALSE);
buf = gtk_text_buffer_new(NULL);
gtk_text_view_set_buffer(text, buf);
scroll = (GtkScrolledWindow *)gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(scroll, GTK_POLICY_NEVER,
GTK_POLICY_ALWAYS);
gtk_container_add(GTK_CONTAINER(scroll), GTK_WIDGET(text));
entry = (GtkEntry *)gtk_entry_new();
gtk_widget_set_size_request(GTK_WIDGET(entry), -1, 30);
gtk_entry_set_has_frame(entry, 0);
gtk_widget_set_can_focus(GTK_WIDGET(entry), 1);
chat = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0));
gtk_box_pack_start(chat, GTK_WIDGET(scroll), TRUE, TRUE, 0);
gtk_box_pack_end(chat, (GtkWidget *)entry, FALSE, TRUE, 0);
g_signal_connect(G_OBJECT(entry), "activate",
G_CALLBACK(Enter_Chat), (gpointer)buf);
g_signal_connect(G_OBJECT(entry), "focus-in-event",
G_CALLBACK(Enter_focus_in_event), NULL);
g_signal_connect(G_OBJECT(entry), "grab-focus",
G_CALLBACK(Enter_grab_focus), NULL);
gtk_text_buffer_get_end_iter(buf, &endit);
gtk_text_buffer_create_mark(buf, "eob", &endit, FALSE);
g_signal_connect(G_OBJECT(buf), "changed",
G_CALLBACK(Chat_scroll_to_end), (gpointer)text);
label = (GtkLabel *)gtk_label_new(name);
pg = gtk_notebook_append_page(nb, GTK_WIDGET(chat),
GTK_WIDGET(label));
if (pg != -1) entries[pg] = entry;
}
G_MODULE_EXPORT void ChatNB_switch_page_cb(GtkNotebook *notebook,
GtkWidget *page, guint page_num, gpointer user_data) {
printf("switch-page %d\r\n", page_num);fflush(stdout);
if (page_num >= 0 && page_num < 5 && entries[page_num] != NULL) {
gtk_widget_grab_focus(GTK_WIDGET(entries[page_num]));
}
}
int main (int argc, char *argv[])
{
GtkWidget *window;
entries[0] = NULL;
entries[1] = NULL;
entries[2] = NULL;
entries[3] = NULL;
entries[4] = NULL;
gtk_init (&argc, &argv);
/* create the main, top level, window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* give it the title */
gtk_window_set_title (GTK_WINDOW (window), "Hello World");
gtk_window_set_default_size(GTK_WINDOW (window), 800, 600);
/* Connect the destroy signal of the window to gtk_main_quit
* When the window is about to be destroyed we get a notification and
* stop the main GTK+ loop
*/
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit),
NULL);
nb = (GtkNotebook *)gtk_notebook_new();
ChatNew("Tab #1");
ChatNew("Tab #2");
g_signal_connect(G_OBJECT(nb), "switch-page",
G_CALLBACK(ChatNB_switch_page_cb), NULL);
/* and insert it into the main window */
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET(nb));
/* make sure that everything, window and label, are visible */
gtk_widget_show_all (window);
/* start the main loop, and let it rest there until the application
is closed */
gtk_main ();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]