0
我嘗試用gtkmm創建一個窗口,其中有兩個文本視圖。文本視圖應排列爲垂直分屏。 就像是: 用GTK +/gtkmm Grid分割屏幕
後來我想能夠在Emacs一次又一次分割屏幕的垂直和水平並調整分離區域,等等。
我認爲一個簡單的分屏應該很容易,但我已經卡在那裏了。 我曾考慮過使用Gtk :: Grid作爲佈局容器,每次用戶想要分割屏幕時,我想添加一行或一列,並在新創建的區域添加一個新的文本視圖。
這裏是我的代碼:
main.cc
#include <gtkmm/application.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
ExampleWindow window;
//Shows the window and returns when it is closed.
return app->run(window);
}
examplewindow.h
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
Gtk::Grid main_grid;
Gtk::ScrolledWindow scrolled_window1;
Gtk::ScrolledWindow scrolled_window2;
Gtk::TextView text_view1;
Gtk::TextView text_view2;
Glib::RefPtr<Gtk::TextBuffer> text_buffer1, text_buffer2;
void fill_buffers();
};
#endif //GTKMM_EXAMPLEWINDOW_H
examplewindow.cc
#include "examplewindow.h"
ExampleWindow::ExampleWindow() {
set_title("Gtk splitted textviews");
set_border_width(12);
add(main_grid);
scrolled_window1.add(text_view1);
scrolled_window1.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
scrolled_window2.add(text_view2);
scrolled_window1.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
main_grid.insert_column(0);
main_grid.attach(scrolled_window1, 0, 0, 1, 1);
//scrolled_window1.set_hexpand(true);
//scrolled_window1.set_vexpand(true);
main_grid.attach(scrolled_window2, 1, 0, 1, 1);
//scrolled_window1.set_hexpand(true);
//scrolled_window1.set_vexpand(true);
fill_buffers();
text_view1.set_buffer(text_buffer1);
text_view2.set_buffer(text_buffer2);
show_all_children();
}
ExampleWindow::~ExampleWindow() {}
void ExampleWindow::fill_buffers() {
text_buffer1 = Gtk::TextBuffer::create();
text_buffer1->set_text("This is the text from TextBuffer #1.");
text_buffer2 = Gtk::TextBuffer::create();
text_buffer2->set_text(
"This is some alternative text, from TextBuffer #2.");
}
構建具有:
g++ examplewindow.cc main.cc -o splittv `pkg-config gtkmm-3.0 --cflags --libs`
文本的意見顯然是小。如果我在兩個文本視圖上設置了hexpand和vexpand爲true,text_view1會抑制text_view2。
對於分割屏幕,我將開始尋找到['GtkPaned'](https://developer.gnome.org/gtk3 /stable/GtkPaned.html) – Gerhardh
您的代碼中存在拼寫錯誤。爲scrolled_window1設置十六進制和vexpand兩次。也許你想要改變爲scrolled_window2。 – JohnKoch