Gtkmm scroll_to()目的

时间:2017-07-07 13:53:41

标签: c++ gtk gtkmm3

我想知道这个成员函数的scroll_to(TextBuffer::iterator& iter, double within_margin = 0)参数 within_margin 。 API说:

  

用于此功能的有效屏幕缩小了in_margin的大小   ...
  的参数
  within_margin margin为屏幕尺寸的[0.0,0.5]分数。

我只是不明白。此参数修改行为的内容和时间是什么? Gtk的每个语言绑定都包含相同的描述。我编写了一个小应用程序,因此您可以自己将参数传递给参数。

#include <gtkmm.h>
#include <iostream>
#include <string>
using namespace std;

Gtk::TextView* text_view;
void on_add_button_clicked();
void on_scroll_button_clicked();

int main(int argc, char* argv[]) {
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
    Gtk::Window window;
    Glib::RefPtr<Gdk::Monitor> primary_monitor = window.get_screen()->get_display()->get_primary_monitor();
    Gdk::Rectangle monitor_size;
    primary_monitor->get_geometry(monitor_size);
    // half-size of primary-monitor
    int width = monitor_size.get_width() / 2;
    int height = monitor_size.get_height() / 2;

    window.set_default_size(width, height);
    window.set_title(__FILE__);

    Gtk::Grid grid;
    grid.set_row_spacing(5);
    grid.set_column_spacing(5);

    Gtk::ScrolledWindow scroll_window;
    text_view = new Gtk::TextView();
    text_view->set_editable(true);
    scroll_window.add(*text_view);
    scroll_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
    scroll_window.set_hexpand(true);
    scroll_window.set_vexpand(true);
    Glib::RefPtr<Gtk::TextBuffer> text_buffer = text_view->get_buffer();
    text_buffer->set_text("Hello!\n");
    text_view->set_buffer(text_buffer);
    grid.attach(scroll_window, 0, 0, 2, 2);


    Gtk::Button add_button("add text");
    add_button.signal_clicked().connect(sigc::ptr_fun(&on_add_button_clicked));
    grid.attach_next_to(add_button, scroll_window, Gtk::POS_BOTTOM, 1, 1);

    Gtk::Button scroll_button("scroll to somewhere");
    scroll_button.signal_clicked().connect(sigc::ptr_fun(&on_scroll_button_clicked));
    grid.attach_next_to(scroll_button, add_button, Gtk::POS_RIGHT, 1, 1);

    window.add(grid);
    window.show_all();

    return app->run(window);
}


void on_add_button_clicked() {
    Glib::RefPtr<Gtk::TextBuffer> text_buffer = text_view->get_buffer();
    for (int i = 0; i != 100; ++i) {
        text_buffer->insert_at_cursor("foobar\n");
    }
}

void on_scroll_button_clicked() {
    Glib::RefPtr<Gtk::TextBuffer> text_buffer = text_view->get_buffer();
    Gtk::TextBuffer::iterator it = text_buffer->end();
    text_view->scroll_to(it, 0.49);
}

您可以使用g++ -o scroll scroll.cpp -Wall -pedantic-errors `pkg-config gtkmm-3.0 --cflags --libs`编译代码。

谢谢

1 个答案:

答案 0 :(得分:1)

如果var i = 0; function duplicate() { var original = document.getElementById('ytContainer' + i); var clone = original.cloneNode(true); clone.id = "ytContainer" + ++i; embed = clone.children[0]; embed.id = 'ytsrc' + i; embed.src = 'https://www.youtube.com/v/' + input.value; original.parentNode.appendChild(clone); }为0,那么<div id="ytContainer0"> <embed id="ytsrc" width="800" height="500" src="https://www.youtube.com/v/CrHsXTqbwkM" /> </div> <input id="input" type="text" name="Youtube Source" /> <button id="button" onclick="duplicate();">Click to change source!</button>可以将目标放在屏幕的任意位置。例如,如果margin为0.45,那么scroll_to()会将目标放在屏幕的中间10%,如果可能的话

您在示例中没有看到这个的原因是因为您正在滚动到结束迭代器,并且无法滚动视图以便文本的末尾显示在中间的屏幕。 (有些文字视图包含文字后面的额外空格,以使其成为可能; margin没有。)