【问题标题】:How do I run gtk demos?如何运行 gtk 演示?
【发布时间】:2011-02-14 12:19:46
【问题描述】:

它们位于:

share\gtk-2.0\demo

但是它们都不包含main 函数,我怎样才能使以下textscroll.c 真正起作用:

/* Text Widget/Automatic scrolling
 *
 * This example demonstrates how to use the gravity of 
 * GtkTextMarks to keep a text view scrolled to the bottom
 * while appending text.
 */

#include <gtk/gtk.h>
#include "demo-common.h"

/* Scroll to the end of the buffer.
 */
static gboolean
scroll_to_end (GtkTextView *textview)
{
  GtkTextBuffer *buffer;
  GtkTextIter iter;
  GtkTextMark *mark;
  char *spaces;
  static int count;

  buffer = gtk_text_view_get_buffer (textview);

  /* Get "end" mark. It's located at the end of buffer because 
   * of right gravity
   */
  mark = gtk_text_buffer_get_mark (buffer, "end");
  gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);

  /* and insert some text at its position, the iter will be 
   * revalidated after insertion to point to the end of inserted text
   */
  spaces = g_strnfill (count++, ' ');
  gtk_text_buffer_insert (buffer, &iter, "\n", -1);
  gtk_text_buffer_insert (buffer, &iter, spaces, -1);
  gtk_text_buffer_insert (buffer, &iter,
                          "Scroll to end scroll to end scroll "
                          "to end scroll to end ",
                          -1);
  g_free (spaces);

  /* Now scroll the end mark onscreen.
   */
  gtk_text_view_scroll_mark_onscreen (textview, mark);

  /* Emulate typewriter behavior, shift to the left if we 
   * are far enough to the right.
   */
  if (count > 150)
    count = 0;

  return TRUE;
}

/* Scroll to the bottom of the buffer.
 */
static gboolean
scroll_to_bottom (GtkTextView *textview)
{
  GtkTextBuffer *buffer;
  GtkTextIter iter;
  GtkTextMark *mark;
  char *spaces;
  static int count;

  buffer = gtk_text_view_get_buffer (textview);

  /* Get end iterator */
  gtk_text_buffer_get_end_iter (buffer, &iter);

  /* and insert some text at it, the iter will be revalidated
   * after insertion to point to the end of inserted text
   */
  spaces = g_strnfill (count++, ' ');
  gtk_text_buffer_insert (buffer, &iter, "\n", -1);
  gtk_text_buffer_insert (buffer, &iter, spaces, -1);
  gtk_text_buffer_insert (buffer, &iter,
                          "Scroll to bottom scroll to bottom scroll "
                          "to bottom scroll to bottom",
                          -1);
  g_free (spaces);

  /* Move the iterator to the beginning of line, so we don't scroll 
   * in horizontal direction 
   */
  gtk_text_iter_set_line_offset (&iter, 0);

  /* and place the mark at iter. the mark will stay there after we
   * insert some text at the end because it has right gravity.
   */
  mark = gtk_text_buffer_get_mark (buffer, "scroll");
  gtk_text_buffer_move_mark (buffer, mark, &iter);

  /* Scroll the mark onscreen.
   */
  gtk_text_view_scroll_mark_onscreen (textview, mark);

  /* Shift text back if we got enough to the right.
   */
  if (count > 40)
    count = 0;

  return TRUE;
}

static guint
setup_scroll (GtkTextView *textview,
              gboolean     to_end)
{
  GtkTextBuffer *buffer;
  GtkTextIter iter;

  buffer = gtk_text_view_get_buffer (textview);
  gtk_text_buffer_get_end_iter (buffer, &iter);

  if (to_end)
  {
    /* If we want to scroll to the end, including horizontal scrolling,
     * then we just create a mark with right gravity at the end of the 
     * buffer. It will stay at the end unless explicitely moved with 
     * gtk_text_buffer_move_mark.
     */
    gtk_text_buffer_create_mark (buffer, "end", &iter, FALSE);

    /* Add scrolling timeout. */
    return g_timeout_add (50, (GSourceFunc) scroll_to_end, textview);
  }
  else
  {
    /* If we want to scroll to the bottom, but not scroll horizontally, 
     * then an end mark won't do the job. Just create a mark so we can 
     * use it with gtk_text_view_scroll_mark_onscreen, we'll position it
     * explicitely when needed. Use left gravity so the mark stays where 
     * we put it after inserting new text.
     */
    gtk_text_buffer_create_mark (buffer, "scroll", &iter, TRUE);

    /* Add scrolling timeout. */
    return g_timeout_add (100, (GSourceFunc) scroll_to_bottom, textview);
  }
}

static void
remove_timeout (GtkWidget *window,
                gpointer   timeout)
{
  g_source_remove (GPOINTER_TO_UINT (timeout));
}

static void
create_text_view (GtkWidget *hbox,
                  gboolean   to_end)
{
  GtkWidget *swindow;
  GtkWidget *textview;
  guint timeout;

  swindow = gtk_scrolled_window_new (NULL, NULL);
  gtk_box_pack_start (GTK_BOX (hbox), swindow, TRUE, TRUE, 0);
  textview = gtk_text_view_new ();
  gtk_container_add (GTK_CONTAINER (swindow), textview);

  timeout = setup_scroll (GTK_TEXT_VIEW (textview), to_end);

  /* Remove the timeout in destroy handler, so we don't try to
   * scroll destroyed widget. 
   */
  g_signal_connect (textview, "destroy",
                    G_CALLBACK (remove_timeout),
                    GUINT_TO_POINTER (timeout));
}

GtkWidget *
do_textscroll (GtkWidget *do_widget)
{
  static GtkWidget *window = NULL;

  if (!window)
    {
      GtkWidget *hbox;

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      g_signal_connect (window, "destroy",
            G_CALLBACK (gtk_widget_destroyed), &window);
      gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

      hbox = gtk_hbox_new (TRUE, 6);
      gtk_container_add (GTK_CONTAINER (window), hbox);

      create_text_view (hbox, TRUE);
      create_text_view (hbox, FALSE);
    }

  if (!gtk_widget_get_visible (window))
      gtk_widget_show_all (window);
  else
      gtk_widget_destroy (window);

  return window;
}

【问题讨论】:

    标签: c gtk demo


    【解决方案1】:

    您也可以在线浏览源码:http://git.gnome.org/browse/gtk+/tree/demos/gtk-demo

    main() 函数位于main.c 的最后。

    编辑:

    这是一个简单的main() 函数,您可以将其添加到文件末尾。您还必须从开头删除#include "demo-common.h" 行。

    int
    main(int argc, char **argv)
    {
        GtkWidget *window;
    
        gtk_init(&argc, &argv);
        window = do_textscroll(NULL);
        g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
        gtk_main();
        return 0;
    }
    

    【讨论】:

    • 这太复杂了,你能详细说明我如何编写一个使用textscroll.cmain 函数吗?
    • 你看了5分钟说“太复杂了”??嘘。您希望如何以那种的态度擅长编程?不过,我今天心情很好,所以请看我的编辑。
    • 谢谢@ptomato,对不起,我真的是gtk的新手,甚至c,再次感谢!
    • 2019 年仍然有用 ... 测试单个演示程序。正常的发行版(我的是 Fedora)应该有 gtk3-demo 从命令行可用,所以你可以很容易地复制你想使用的源代码。
    【解决方案2】:

    你应该跑

    gtk-demo
    

    从命令行。将显示一个包含所有演示的窗口。您可以浏览源代码,并通过双击它们来运行演示。

    【讨论】:

    • 它有效。我必须已经安装了 jhbuild 并且它可以工作。谢谢。
    【解决方案3】:

    2015 年更新: 运行%gtkdir%/bin/gtk3-demo.exe

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 2015-05-20
      • 1970-01-01
      • 2015-05-14
      • 2017-04-30
      • 2016-11-14
      • 2018-11-05
      相关资源
      最近更新 更多