Gtk:如何在文本视图中获取文件的一部分,并带有与完整文件相关的滚动条

发布于 2024-08-29 15:03:25 字数 466 浏览 4 评论 0原文

我正在尝试制作一个非常大的文件编辑器(其中编辑器一次仅将缓冲区的一部分存储在内存中),但在构建文本视图对象时我陷入了困境。基本上 - 我知道我必须能够动态更新文本视图缓冲区,并且我不知道如何让滚动条与完整文件相关,而文本视图仅包含文件的一小部分缓冲区。

我在 Gtk.ScrolledWindow 和 ScrollBars 上使用过 Gtk.Adjustment,但尽管我可以扩展滚动条的范围,但它们仍然适用于缓冲区的范围,而不是文件大小(我尝试通过 Gtk.Adjustment 设置)参数)当我加载到文本视图中时。我需要有一个小部件,它“知道”它正在查看文件的一部分,并且可以根据需要加载/卸载缓冲区以查看文件的不同部分。到目前为止,我相信我会响应“change_view”来计算我何时关闭,或即将关闭当前缓冲区并需要加载下一个缓冲区,但我不知道如何让滚动条具有顶部与文件的开头相关,底部与文件的结尾相关,而不是与 textview 中加载的缓冲区相关。

任何帮助将不胜感激,谢谢!

I'm trying to make a very large file editor (where the editor only stores a part of the buffer in memory at a time), but I'm stuck while building my textview object. Basically- I know that I have to be able to update the text view buffer dynamically, and I don't know hot to get the scrollbars to relate to the full file while the textview contains only a small buffer of the file.

I've played with Gtk.Adjustment on a Gtk.ScrolledWindow and ScrollBars, but though I can extend the range of the scrollbars, they still apply to the range of the buffer and not the filesize (which I try to set via Gtk.Adjustment parameters) when I load into textview. I need to have a widget that "knows" that it is looking at a part of a file, and can load/unload buffers as necessary to view different parts of the file. So far, I believe I'll respond to the "change_view" to calculate when I'm off, or about to be off the current buffer and need to load the next, but I don't know how to get the scrollbars to have the top relate to the beginning of the file, and the bottom relate to the end of the file, rather than to the loaded buffer in textview.

Any help would be greatly appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

维持三分热 2024-09-05 15:03:25

您可能应该创建自己的 Gtk.TextBuffer 实现,因为默认实现依赖于将整个缓冲区存储在内存中。

You probably should create your own Gtk.TextBuffer implementation, as the default one relies on storing whole buffer in memory.

人疚 2024-09-05 15:03:25

我同意 el.pescado 的回答,但你也可以尝试伪造它。计算您正在编辑的文件中的行数。将一屏文本放入缓冲区中,并用换行符填充其余部分,以便缓冲区具有与文件相同的行数。

然后连接到包含文本视图的滚动窗口的垂直调整的changed信号,这将在窗口滚动时通知您。发生这种情况时,用换行符替换缓冲区中已有的文本,然后加载您现在正在查看的部分。

您可以使用此代码告诉您应该在文本视图中查看哪些行号(可能有错误,我是从内存中执行此操作并即时翻译成Python):

visible_rect = textview.get_visible_rect()
top = textview.get_iter_at_location(visible_rect.x, visible_rect.y)
bottom = textview.get_iter_at_location(visible_rect.x, visible_rect.y + visible_rect.height)
top_line, bottom_line = top.get_line(), bottom.get_line()

I agree with el.pescado's answer, but you could also try to fake it. Count the number of lines in the file you're editing. Put one screenful of text in the buffer and fill the rest with newlines so the buffer has the same number of lines as the file.

Then connect to the changed signal of the vertical adjustment of the scrolled window that contains the text view, this will notify you whenever the window is scrolled. When that happens, replace the text you already had in the buffer with newlines, and load the section you are now looking at.

You can tell which line numbers you are supposed to be looking at in a text view with this code (may have bugs, I'm doing this from memory and translating into Python on the fly):

visible_rect = textview.get_visible_rect()
top = textview.get_iter_at_location(visible_rect.x, visible_rect.y)
bottom = textview.get_iter_at_location(visible_rect.x, visible_rect.y + visible_rect.height)
top_line, bottom_line = top.get_line(), bottom.get_line()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文