gpt4 book ai didi

c++ - 带有GTK +/gtkmm网格的分屏

转载 作者:行者123 更新时间:2023-12-02 10:24:43 32 4
gpt4 key购买 nike

我尝试用gtkmm建立一个窗口,其中有两个文本 View 。文本 View 应安排为垂直拆分屏幕。
像那样:
Split screen

稍后,我希望能够一次又一次地垂直和水平分割屏幕,并调整分割区域的大小,例如在emacs中。

我以为一个简单的分屏应该很容易,但是我已经被卡在那里了。
我考虑过使用Gtk::Grid作为布局容器,并且每当用户想要拆分屏幕时,我都考虑过在新创建的区域中添加行或列并添加新的文本 View 。

这是我的代码:

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`

这样会产生结果:
Too small text views

文本 View 显然很小。如果我在两个文本 View 上都将hexpand和vexpand设置为true,则text_view1将压制text_view2。

最佳答案

正如@Gerhardh在评论中建议的那样,您可以使用GtkPaned

#include <gtkmm.h>
#include <memory>
#include <string>

struct Body
{
inline static int i=0;
Gtk::Box Box;
Gtk::Button SplitHButton, SplitVButton, CloseButton;
Gtk::Label Label;
Body()
{
SplitHButton.set_label("h");
SplitVButton.set_label("v");
Label.set_text(std::to_string(i++));
CloseButton.set_label("c");
Box.add(SplitHButton);
Box.add(SplitVButton);
Box.add(Label);
Box.add(CloseButton);
Box.show_all();
}
};

struct Pane
{
Gtk::Paned PaneWidget;
std::shared_ptr<Pane> ChildPane1, ChildPane2;
Body Body1, Body2;
Pane(Gtk::Orientation orientation=Gtk::ORIENTATION_HORIZONTAL):
PaneWidget(orientation)
{
PaneWidget.add1(Body1.Box);
PaneWidget.add2(Body2.Box);

Body1.SplitHButton.signal_clicked().connect([this]{Split(ChildPane1, Gtk::ORIENTATION_HORIZONTAL, Body1, true);});
Body1.SplitVButton.signal_clicked().connect([this]{Split(ChildPane1, Gtk::ORIENTATION_VERTICAL, Body1, true);});
Body2.SplitHButton.signal_clicked().connect([this]{Split(ChildPane2, Gtk::ORIENTATION_HORIZONTAL, Body2, false);});
Body2.SplitVButton.signal_clicked().connect([this]{Split(ChildPane2, Gtk::ORIENTATION_VERTICAL, Body2, false);});

PaneWidget.show_all();
}

void Split(std::shared_ptr<Pane>& pane, Gtk::Orientation orientation, Body& body, bool leftTop)
{
pane = std::make_shared<Pane>(orientation);
PaneWidget.remove(body.Box);
if(leftTop)
PaneWidget.add1(pane->PaneWidget);
else
PaneWidget.add2(pane->PaneWidget);

auto lambda = [&]{
PaneWidget.remove(pane->PaneWidget);
if(leftTop)
PaneWidget.add1(body.Box);
else
PaneWidget.add2(body.Box);
PaneWidget.show_all();
pane.reset();
};

pane->Body1.CloseButton.signal_clicked().connect(lambda);
pane->Body2.CloseButton.signal_clicked().connect(lambda);
}
};

int main()
{
auto GtkApp = Gtk::Application::create();
Gtk::Window w;
Pane p;
w.add(p.PaneWidget);
w.resize(800,600);
w.show_all();
GtkApp->run(w);
return 0;
}

enter image description here

关于c++ - 带有GTK +/gtkmm网格的分屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46216476/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com