GtkScrolledWindow containing GtkTextView containing a TextBuffer can't keep at bottom of Buffer
- From: "Thomas A. Moulton" <tom moulton us>
- To: gtk-app-devel-list <gtk-app-devel-list gnome org>
- Subject: GtkScrolledWindow containing GtkTextView containing a TextBuffer can't keep at bottom of Buffer
- Date: Sun, 09 Jun 2013 09:21:29 -0400
I see a lot of questions about this and I have not found an answer that
works for me.
I create the chat window like this:
CHAT *ChatNew(char *player) {
CHAT *chatlog;
GtkBox *chat;
GtkTextBuffer *buf;
GtkTextView *text;
GtkEntry *entry;
GtkScrolledWindow *scroll;
GtkTextIter endit;
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);
printf("New TextBuffer %x\r\n", (int)buf);
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_scrolled_window_add_with_viewport(scroll, GTK_WIDGET(text));
entry = (GtkEntry *)gtk_entry_new();
gtk_widget_set_size_request(GTK_WIDGET(entry), -1, 30);
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);
chatlog = g_malloc0(sizeof(CHAT));
if (chatlog == NULL) return NULL;
strcpy(chatlog->player, player);
chatlog->scroll = scroll;
chatlog->box = chat;
chatlog->text = text;
chatlog->entry = entry;
g_signal_connect(G_OBJECT(entry), "activate",
G_CALLBACK(Enter_Chat), (gpointer)chatlog);
//buf = gtk_text_view_get_buffer(chatlog->text);
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);
//g_signal_connect(G_OBJECT(buf), "key_press_event",
G_CALLBACK(Chat_keys), (gpointer)chatlog);
return chatlog;
}
The caller adds it to a GtkNotebook
if (item == NULL) {
chatlog = ChatNew(name);
label = (GtkLabel *)gtk_label_new(name);
g_queue_insert_sorted(gfibs.chats, chatlog,
chatNB_sort_chat, 0);
n = gtk_notebook_insert_page(nb, (GtkWidget *)chatlog->box,
(GtkWidget *)label, 2);
buf = gtk_text_view_get_buffer(chatlog->text);
textv = chatlog->text;
gtk_widget_show((GtkWidget *)chatlog->scroll);
gtk_widget_show((GtkWidget *)chatlog->box);
gtk_widget_show((GtkWidget *)chatlog->text);
gtk_widget_show((GtkWidget *)chatlog->entry);
gtk_widget_show((GtkWidget *)label);
printf("chat inserted at %d\r\n", n);
} else {
chatlog = (CHAT *)item->data;
buf = NULL;
if (chatlog != NULL && chatlog->text != NULL) {
buf = gtk_text_view_get_buffer(chatlog->text);
textv = chatlog->text;
n = gtk_notebook_page_num(nb, GTK_WIDGET(chatlog->box));
}
}
if (buf != NULL) {
GtkTextIter endit;
gtk_text_buffer_get_end_iter(buf, &endit);
gtk_text_buffer_insert_with_tags(buf, &endit, line, -1, NULL,
NULL);
return TRUE;
}
and then to make sure it is visible
void Chat_scroll_to_end(GtkTextBuffer *buf, GtkTextView *text) {
GtkTextMark *mark;
printf("Chat_scroll_to_end arg %x text %x parent %x\r\n", (int)buf,
(int)text,
(int)gtk_widget_get_parent(GTK_WIDGET(buf)));fflush(stdout);
mark = gtk_text_buffer_get_mark(buf, "eob");
gtk_text_view_scroll_mark_onscreen(text, mark);
}
The printf above at times tells me the objects are not valid, but the
addresses have not changed.
Also the TextBuffer has a parent of 0! I even allocated and set the
buffer manually.
If there it no anything obvious wrong here I will strip it down to a
simpler test case
Tom
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]