gtk + vte scrollback无法正常工作

时间:2012-07-03 01:39:26

标签: c gtk vte

gtk + ver 2.24.8

vte ver 0.28.2

我刚刚开始创建终端应用程序,但是,我在网上找到的所有示例(5)都没有工作回滚。在vte中回滚是否有问题,或者是我没有看到的东西?这是一个例子:

#include <gtk/gtk.h>
#include <vte/vte.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <gdk/gdkkeysyms.h> // includes GDK_q


/* gcc -Wall -g term.c -o term `pkg-config --cflags --libs gtk+-2.0 vte` */

long size;
char *buf;
char *ptr;

gboolean key_press_win_main (GtkWidget *widget, GdkEventKey *event, gpointer user_data);
void quit_activated();
static gboolean delete_event(GtkWidget *,GdkEvent *);

int main( int argc, char *argv[] )
{

  size = pathconf(".", _PC_PATH_MAX);
  if ((buf = (char *)malloc((size_t)size)) != NULL) ptr = getcwd(buf, (size_t)size);

  GtkWidget *window_main;
  GtkWidget *vbox;
  GtkWidget *vte;
  GtkWidget *notebook;
  GtkWidget *scrolled_window;
  GtkWidget *label;

  gtk_init (&argc, &argv);

  window_main = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window_main), "Caraterm v0.0.1");

  vbox = gtk_vbox_new(FALSE,0);
  gtk_container_add (GTK_CONTAINER (window_main), vbox);


  notebook = gtk_notebook_new ();
  gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_BOTTOM);
  gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);

  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  label = gtk_label_new ("term1");
  gtk_notebook_append_page (GTK_NOTEBOOK (notebook), scrolled_window, label);

  vte = vte_terminal_new();
  vte_terminal_set_background_transparent(VTE_TERMINAL(vte), FALSE);
  vte_terminal_set_size(VTE_TERMINAL(vte), 80, 45);
  vte_terminal_set_scrollback_lines(VTE_TERMINAL (vte), -1); /* infinite scrollback */
  vte_terminal_fork_command(VTE_TERMINAL(vte), NULL, NULL, NULL, ptr, TRUE, TRUE,TRUE);
  vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL (vte), TRUE);
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), vte);

  printf("%s\n", vte_terminal_get_emulation(VTE_TERMINAL (vte)));

  g_signal_connect(G_OBJECT(window_main),"destroy",G_CALLBACK(quit_activated),NULL);
  g_signal_connect(G_OBJECT(window_main),"delete_event",G_CALLBACK(delete_event),NULL);
  g_signal_connect (G_OBJECT (window_main), "key_press_event", G_CALLBACK (key_press_win_main), NULL);


  gtk_widget_show (label);
  gtk_widget_show (notebook);
  gtk_widget_show (scrolled_window);
  gtk_widget_show (vte);
  gtk_widget_show (vbox);
  gtk_widget_show (window_main);

  gtk_main ();

  return 0;
}

gboolean key_press_win_main (GtkWidget *widget, GdkEventKey *event, gpointer user_data)

{

  switch (event->keyval)
  {
     case GDK_q:
            if (event -> state & GDK_CONTROL_MASK)
            {
        printf("entered q\n");
        quit_activated();
                    }

      break;

    default:
      return FALSE; 
  }

  return FALSE; 
}

void quit_activated()
{   
  gtk_main_quit();

}

static gboolean delete_event(GtkWidget *window_main,GdkEvent *event)
{  
  return FALSE;
}

此示例将滚动但仅通过vte_terminal_set_size(VTE_TERMINAL(vte), 80, 45);来容纳vte集的原始大小。如果通过简单地返回或从程序生成输出向终端添加更多行,则回滚缓冲区不会扩展。虽然指定了无限制的回滚vte_terminal_set_scrollback_lines(VTE_TERMINAL (vte), -1); /* infinite scrollback */

,但它固定为45行

我错过了什么?感谢。

3 个答案:

答案 0 :(得分:1)

为了使回滚工作,需要更改的行是:

scrolled_window = gtk_scrolled_window_new (NULL, NULL);

为:

scrolled_window = gtk_scrolled_window_new (NULL, terminal->adjustment);

其中终端定义为:

VteTerminal *terminal;

vte = vte_terminal_new();
terminal = VTE_TERMINAL (vte);

答案 1 :(得分:0)

由于我之前没有与VTE合作过,所以我并不是那么自信。但是,看起来vte_terminal本身就是一个可滚动的小部件。我建议你试着摆脱scrolled_window并看看。

此外,here是我发现的例子。

答案 2 :(得分:0)

试试这个作品

//gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), vte);
 gtk_container_add (GTK_CONTAINER(scrolled_window),vte);