Two Gtk TextView widgets with shared Scrollbar

霸气de小男生 提交于 2019-12-12 15:46:25

问题


I want to have two TextView widgets side by side which are scrolled together with a single scrollbar.

I can put both TextView widgets in a Hbox and then add them to a Viewport and then to a ScrolledWindow. However this will not work how I want. Scrolling from the scrollbar will work. But actions occurring in the TextView wont change the scroll position. (arrow keys, page up, page down etc) and I also can't programatically change the scrolling with TextView.ScrollToMark and other other TextView scrolling methods.

How can I have two TextView widgets share a scrollbar and have actions in the TextView's update the scrolling?


回答1:


The key is to use a ScrolledWindow instead of a Viewport, and to have the adjustments shared between the ScrolledWindows. The Viewport will just scroll over a widget which is too large to fit in the container. On the other hand, the ScrolledWindow links its adjustments to the TextView adjustments, which are influenced by cursor movement.

I managed to get this to work by linking the adjustments between ScrolledWindows, hiding their scrollbars, and then putting them in one big ScrolledWindow that just controls all the subwindow scrollbars (which are not shown).

Below is the full code solution in Python. You should be able to adapt this easily to C#.

#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk

if __name__ == "__main__":
    window = gtk.Window()

    box = gtk.HBox()

    textview1 = gtk.TextView()
    textview2 = gtk.TextView()

    hadjustment = None
    vadjustment = None

    for textview in (textview1, textview2):
        sw = gtk.ScrolledWindow()
        # don't show the scrollbars on these sub-scrolledwindows
        sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
        sw.add(textview)

        # use the first scrolledwindow's adjustments
        if hadjustment is None:
            hadjustment = sw.get_hadjustment()
        else:
            sw.set_hadjustment(hadjustment)
        if vadjustment is None:
            vadjustment = sw.get_vadjustment()
        else:
            sw.set_vadjustment(vadjustment)

        box.pack_start(sw, padding=5)

        buffer = textview.get_buffer()
        buffer.set_text("If a widget has native scrolling abilities,\n"
                        " it can be added to the gtk.ScrolledWindow\n"
                        "with the gtk.Container.add() method. If a\n"
                        "widget does not, you must first add the\n"
                        "widget to a gtk.Viewport, then add the\n"
                        "gtk.Viewport to the scrolled window. The\n"
                        "convenience method add_with_viewport() does\n"
                        "exactly this, so you can ignore the presence\n"
                        "of the viewport.")

    # this scrolled window will encompass the rest and have scrollbars
    main_sw = gtk.ScrolledWindow(hadjustment, vadjustment)
    main_sw.set_policy(gtk.POLICY_ALWAYS, gtk.POLICY_ALWAYS)
    main_sw.add(box)
    window.add(main_sw)

    window.show_all()
    gtk.main()


来源:https://stackoverflow.com/questions/6617816/two-gtk-textview-widgets-with-shared-scrollbar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!