add old lci module
This commit is contained in:
1 parent
38a6e815f0
commit
8499631091
171 files changed
+25562
No files matched your search
@@ -0,0 +1,560 @@
|
||||
#ifndef PMC_GUI_GTK_HPP
|
||||
#define PMC_GUI_GTK_HPP
|
||||
|
||||
/**
|
||||
* @file GTK.hpp
|
||||
* @brief GTK库入口封装
|
||||
*
|
||||
* 提供GTK库的统一入口和简化接口,便于在pmc项目中使用GTK进行GUI开发。
|
||||
*/
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace pmc {
|
||||
namespace gui {
|
||||
|
||||
/**
|
||||
* @class GTKApplication
|
||||
* @brief GTK应用程序封装类
|
||||
*
|
||||
* 封装GTK应用程序的初始化和运行逻辑,提供简化的API。
|
||||
*/
|
||||
class GTKApplication {
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param app_id 应用程序ID
|
||||
* @param app_name 应用程序名称
|
||||
*/
|
||||
GTKApplication(const std::string& app_id, const std::string& app_name);
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~GTKApplication();
|
||||
|
||||
/**
|
||||
* @brief 初始化GTK应用程序
|
||||
* @param argc 命令行参数数量
|
||||
* @param argv 命令行参数数组
|
||||
* @return 初始化是否成功
|
||||
*/
|
||||
bool initialize(int argc, char** argv);
|
||||
|
||||
/**
|
||||
* @brief 运行应用程序
|
||||
* @return 应用程序退出码
|
||||
*/
|
||||
int run();
|
||||
|
||||
/**
|
||||
* @brief 获取GTK应用程序指针
|
||||
* @return GtkApplication指针
|
||||
*/
|
||||
GtkApplication* get_app() const { return app_; }
|
||||
|
||||
/**
|
||||
* @brief 设置应用程序图标
|
||||
* @param icon_path 图标文件路径
|
||||
* @return 设置是否成功
|
||||
*/
|
||||
bool set_icon(const std::string& icon_path);
|
||||
|
||||
/**
|
||||
* @brief 设置窗口默认大小
|
||||
* @param width 窗口宽度
|
||||
* @param height 窗口高度
|
||||
*/
|
||||
void set_default_window_size(int width, int height);
|
||||
|
||||
private:
|
||||
std::string app_id_;
|
||||
std::string app_name_;
|
||||
GtkApplication* app_;
|
||||
int default_width_;
|
||||
int default_height_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class GTKWindow
|
||||
* @brief GTK窗口封装类
|
||||
*
|
||||
* 封装GTK窗口的创建和管理,提供简化的窗口操作接口。
|
||||
*/
|
||||
class GTKWindow {
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param title 窗口标题
|
||||
* @param width 窗口宽度
|
||||
* @param height 窗口高度
|
||||
*/
|
||||
GTKWindow(const std::string& title, int width = 800, int height = 600);
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~GTKWindow();
|
||||
|
||||
/**
|
||||
* @brief 显示窗口
|
||||
*/
|
||||
void show();
|
||||
|
||||
/**
|
||||
* @brief 隐藏窗口
|
||||
*/
|
||||
void hide();
|
||||
|
||||
/**
|
||||
* @brief 设置窗口标题
|
||||
* @param title 新标题
|
||||
*/
|
||||
void set_title(const std::string& title);
|
||||
|
||||
/**
|
||||
* @brief 获取窗口标题
|
||||
* @return 窗口标题
|
||||
*/
|
||||
std::string get_title() const;
|
||||
|
||||
/**
|
||||
* @brief 设置窗口大小
|
||||
* @param width 宽度
|
||||
* @param height 高度
|
||||
*/
|
||||
void set_size(int width, int height);
|
||||
|
||||
/**
|
||||
* @brief 获取窗口宽度
|
||||
* @return 窗口宽度
|
||||
*/
|
||||
int get_width() const;
|
||||
|
||||
/**
|
||||
* @brief 获取窗口高度
|
||||
* @return 窗口高度
|
||||
*/
|
||||
int get_height() const;
|
||||
|
||||
/**
|
||||
* @brief 获取GTK窗口指针
|
||||
* @return GtkWindow指针
|
||||
*/
|
||||
GtkWindow* get_window() const { return window_; }
|
||||
|
||||
/**
|
||||
* @brief 设置关闭回调函数
|
||||
* @param callback 关闭回调函数
|
||||
*/
|
||||
void set_close_callback(std::function<void()> callback);
|
||||
|
||||
/**
|
||||
* @brief 添加子控件
|
||||
* @param widget 要添加的控件
|
||||
*/
|
||||
void add_widget(GtkWidget* widget);
|
||||
|
||||
/**
|
||||
* @brief 设置窗口位置居中
|
||||
*/
|
||||
void center_on_screen();
|
||||
|
||||
private:
|
||||
GtkWindow* window_;
|
||||
GtkWidget* container_;
|
||||
std::string title_;
|
||||
int width_;
|
||||
int height_;
|
||||
std::function<void()> close_callback_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class GTKButton
|
||||
* @brief GTK按钮封装类
|
||||
*
|
||||
* 封装GTK按钮的创建和事件处理。
|
||||
*/
|
||||
class GTKButton {
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param label 按钮标签
|
||||
*/
|
||||
GTKButton(const std::string& label);
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~GTKButton();
|
||||
|
||||
/**
|
||||
* @brief 设置点击回调函数
|
||||
* @param callback 点击回调函数
|
||||
*/
|
||||
void set_click_callback(std::function<void()> callback);
|
||||
|
||||
/**
|
||||
* @brief 获取GTK按钮指针
|
||||
* @return GtkButton指针
|
||||
*/
|
||||
GtkButton* get_button() const { return button_; }
|
||||
|
||||
/**
|
||||
* @brief 设置按钮标签
|
||||
* @param label 新标签
|
||||
*/
|
||||
void set_label(const std::string& label);
|
||||
|
||||
/**
|
||||
* @brief 获取按钮标签
|
||||
* @return 按钮标签
|
||||
*/
|
||||
std::string get_label() const;
|
||||
|
||||
/**
|
||||
* @brief 启用按钮
|
||||
*/
|
||||
void enable();
|
||||
|
||||
/**
|
||||
* @brief 禁用按钮
|
||||
*/
|
||||
void disable();
|
||||
|
||||
/**
|
||||
* @brief 设置按钮工具提示
|
||||
* @param tooltip 工具提示文本
|
||||
*/
|
||||
void set_tooltip(const std::string& tooltip);
|
||||
|
||||
private:
|
||||
GtkButton* button_;
|
||||
std::string label_;
|
||||
std::function<void()> click_callback_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class GTKLabel
|
||||
* @brief GTK标签封装类
|
||||
*
|
||||
* 封装GTK标签的创建和文本管理。
|
||||
*/
|
||||
class GTKLabel {
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param text 标签文本
|
||||
*/
|
||||
GTKLabel(const std::string& text = "");
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~GTKLabel();
|
||||
|
||||
/**
|
||||
* @brief 设置标签文本
|
||||
* @param text 新文本
|
||||
*/
|
||||
void set_text(const std::string& text);
|
||||
|
||||
/**
|
||||
* @brief 获取标签文本
|
||||
* @return 标签文本
|
||||
*/
|
||||
std::string get_text() const;
|
||||
|
||||
/**
|
||||
* @brief 获取GTK标签指针
|
||||
* @return GtkLabel指针
|
||||
*/
|
||||
GtkLabel* get_label() const { return label_; }
|
||||
|
||||
/**
|
||||
* @brief 设置文本对齐方式
|
||||
* @param xalign 水平对齐 (0.0=左对齐, 0.5=居中, 1.0=右对齐)
|
||||
* @param yalign 垂直对齐 (0.0=顶部, 0.5=居中, 1.0=底部)
|
||||
*/
|
||||
void set_alignment(float xalign = 0.0f, float yalign = 0.5f);
|
||||
|
||||
/**
|
||||
* @brief 设置文本换行
|
||||
* @param wrap 是否换行
|
||||
*/
|
||||
void set_wrap(bool wrap);
|
||||
|
||||
/**
|
||||
* @brief 设置选择模式
|
||||
* @param selectable 是否可选择
|
||||
*/
|
||||
void set_selectable(bool selectable);
|
||||
|
||||
private:
|
||||
GtkLabel* label_;
|
||||
std::string text_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class GTKEntry
|
||||
* @brief GTK输入框封装类
|
||||
*
|
||||
* 封装GTK输入框的创建和文本管理。
|
||||
*/
|
||||
class GTKEntry {
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param placeholder 占位符文本
|
||||
*/
|
||||
GTKEntry(const std::string& placeholder = "");
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~GTKEntry();
|
||||
|
||||
/**
|
||||
* @brief 获取输入文本
|
||||
* @return 输入文本
|
||||
*/
|
||||
std::string get_text() const;
|
||||
|
||||
/**
|
||||
* @brief 设置输入文本
|
||||
* @param text 新文本
|
||||
*/
|
||||
void set_text(const std::string& text);
|
||||
|
||||
/**
|
||||
* @brief 清空输入框
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* @brief 获取GTK输入框指针
|
||||
* @return GtkEntry指针
|
||||
*/
|
||||
GtkEntry* get_entry() const { return entry_; }
|
||||
|
||||
/**
|
||||
* @brief 设置占位符文本
|
||||
* @param placeholder 占位符文本
|
||||
*/
|
||||
void set_placeholder(const std::string& placeholder);
|
||||
|
||||
/**
|
||||
* @brief 设置输入变化回调函数
|
||||
* @param callback 输入变化回调函数
|
||||
*/
|
||||
void set_changed_callback(std::function<void()> callback);
|
||||
|
||||
/**
|
||||
* @brief 设置激活回调函数(回车键)
|
||||
* @param callback 激活回调函数
|
||||
*/
|
||||
void set_activate_callback(std::function<void()> callback);
|
||||
|
||||
/**
|
||||
* @brief 启用输入框
|
||||
*/
|
||||
void enable();
|
||||
|
||||
/**
|
||||
* @brief 禁用输入框
|
||||
*/
|
||||
void disable();
|
||||
|
||||
private:
|
||||
GtkEntry* entry_;
|
||||
std::string placeholder_;
|
||||
std::function<void()> changed_callback_;
|
||||
std::function<void()> activate_callback_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class GTKTextView
|
||||
* @brief GTK文本视图封装类
|
||||
*
|
||||
* 封装GTK文本视图的创建和文本管理,支持多行文本编辑。
|
||||
*/
|
||||
class GTKTextView {
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
*/
|
||||
GTKTextView();
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~GTKTextView();
|
||||
|
||||
/**
|
||||
* @brief 获取文本内容
|
||||
* @return 文本内容
|
||||
*/
|
||||
std::string get_text() const;
|
||||
|
||||
/**
|
||||
* @brief 设置文本内容
|
||||
* @param text 新文本
|
||||
*/
|
||||
void set_text(const std::string& text);
|
||||
|
||||
/**
|
||||
* @brief 追加文本
|
||||
* @param text 要追加的文本
|
||||
*/
|
||||
void append_text(const std::string& text);
|
||||
|
||||
/**
|
||||
* @brief 清空文本
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* @brief 获取GTK文本视图指针
|
||||
* @return GtkTextView指针
|
||||
*/
|
||||
GtkTextView* get_text_view() const { return text_view_; }
|
||||
|
||||
/**
|
||||
* @brief 获取文本缓冲区
|
||||
* @return GtkTextBuffer指针
|
||||
*/
|
||||
GtkTextBuffer* get_buffer() const { return buffer_; }
|
||||
|
||||
/**
|
||||
* @brief 设置只读模式
|
||||
* @param read_only 是否只读
|
||||
*/
|
||||
void set_read_only(bool read_only);
|
||||
|
||||
/**
|
||||
* @brief 设置文本变化回调函数
|
||||
* @param callback 文本变化回调函数
|
||||
*/
|
||||
void set_changed_callback(std::function<void()> callback);
|
||||
|
||||
/**
|
||||
* @brief 滚动到底部
|
||||
*/
|
||||
void scroll_to_bottom();
|
||||
|
||||
private:
|
||||
GtkTextView* text_view_;
|
||||
GtkTextBuffer* buffer_;
|
||||
std::function<void()> changed_callback_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @class GTKBox
|
||||
* @brief GTK盒子布局容器封装类
|
||||
*
|
||||
* 封装GTK盒子布局容器的创建和子控件管理。
|
||||
*/
|
||||
class GTKBox {
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param orientation 布局方向 (GTK_ORIENTATION_HORIZONTAL 或 GTK_ORIENTATION_VERTICAL)
|
||||
* @param spacing 子控件间距
|
||||
*/
|
||||
GTKBox(GtkOrientation orientation = GTK_ORIENTATION_VERTICAL, int spacing = 5);
|
||||
|
||||
/**
|
||||
* @brief 析构函数
|
||||
*/
|
||||
~GTKBox();
|
||||
|
||||
/**
|
||||
* @brief 添加子控件
|
||||
* @param widget 要添加的控件
|
||||
* @param expand 是否扩展
|
||||
* @param fill 是否填充
|
||||
* @param padding 内边距
|
||||
*/
|
||||
void add_widget(GtkWidget* widget, bool expand = false, bool fill = false, int padding = 0);
|
||||
|
||||
/**
|
||||
* @brief 获取GTK盒子指针
|
||||
* @return GtkBox指针
|
||||
*/
|
||||
GtkBox* get_box() const { return box_; }
|
||||
|
||||
/**
|
||||
* @brief 设置间距
|
||||
* @param spacing 新间距
|
||||
*/
|
||||
void set_spacing(int spacing);
|
||||
|
||||
/**
|
||||
* @brief 设置对齐方式
|
||||
* @param align 对齐方式
|
||||
*/
|
||||
void set_alignment(GtkAlign align);
|
||||
|
||||
/**
|
||||
* @brief 设置均匀分配
|
||||
* @param homogeneous 是否均匀分配空间
|
||||
*/
|
||||
void set_homogeneous(bool homogeneous);
|
||||
|
||||
private:
|
||||
GtkBox* box_;
|
||||
GtkOrientation orientation_;
|
||||
int spacing_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 初始化GTK库
|
||||
* @param argc 命令行参数数量指针
|
||||
* @param argv 命令行参数数组指针
|
||||
* @return 初始化是否成功
|
||||
*/
|
||||
bool gtk_init(int* argc, char*** argv);
|
||||
|
||||
/**
|
||||
* @brief 运行GTK主循环
|
||||
*/
|
||||
void gtk_main();
|
||||
|
||||
/**
|
||||
* @brief 退出GTK主循环
|
||||
*/
|
||||
void gtk_main_quit();
|
||||
|
||||
/**
|
||||
* @brief 显示错误对话框
|
||||
* @param parent 父窗口
|
||||
* @param title 对话框标题
|
||||
* @param message 错误消息
|
||||
*/
|
||||
void show_error_dialog(GtkWindow* parent, const std::string& title, const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 显示信息对话框
|
||||
* @param parent 父窗口
|
||||
* @param title 对话框标题
|
||||
* @param message 信息消息
|
||||
*/
|
||||
void show_info_dialog(GtkWindow* parent, const std::string& title, const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 显示确认对话框
|
||||
* @param parent 父窗口
|
||||
* @param title 对话框标题
|
||||
* @param message 确认消息
|
||||
* @return 用户是否确认
|
||||
*/
|
||||
bool show_confirm_dialog(GtkWindow* parent, const std::string& title, const std::string& message);
|
||||
|
||||
} // namespace gui
|
||||
} // namespace pmc
|
||||
|
||||
#endif // PMC_GUI_GTK_HPP
|
||||
@@ -0,0 +1,456 @@
|
||||
#ifndef PMC_GUI_GTK_IMPL_HPP
|
||||
#define PMC_GUI_GTK_IMPL_HPP
|
||||
|
||||
/**
|
||||
* @file GTK_impl.hpp
|
||||
* @brief GTK库封装实现模板
|
||||
*
|
||||
* 提供GTK封装类的内联实现,便于头文件包含。
|
||||
*/
|
||||
|
||||
#include "gui/GTK.hpp"
|
||||
#include <iostream>
|
||||
|
||||
namespace pmc {
|
||||
namespace gui {
|
||||
|
||||
// GTKApplication 实现
|
||||
inline GTKApplication::GTKApplication(const std::string& app_id, const std::string& app_name)
|
||||
: app_id_(app_id), app_name_(app_name), app_(nullptr), default_width_(800), default_height_(600) {
|
||||
}
|
||||
|
||||
inline GTKApplication::~GTKApplication() {
|
||||
if (app_) {
|
||||
g_object_unref(app_);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool GTKApplication::initialize(int argc, char** argv) {
|
||||
app_ = gtk_application_new(app_id_.c_str(), G_APPLICATION_FLAGS_NONE);
|
||||
if (!app_) {
|
||||
std::cerr << "Failed to create GTK application: " << app_name_ << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline int GTKApplication::run() {
|
||||
if (!app_) {
|
||||
std::cerr << "GTK application not initialized" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
return g_application_run(G_APPLICATION(app_), 0, nullptr);
|
||||
}
|
||||
|
||||
inline bool GTKApplication::set_icon(const std::string& icon_path) {
|
||||
// 实现图标设置逻辑
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void GTKApplication::set_default_window_size(int width, int height) {
|
||||
default_width_ = width;
|
||||
default_height_ = height;
|
||||
}
|
||||
|
||||
// GTKWindow 实现
|
||||
inline GTKWindow::GTKWindow(const std::string& title, int width, int height)
|
||||
: title_(title), width_(width), height_(height), window_(nullptr), container_(nullptr) {
|
||||
window_ = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
|
||||
if (window_) {
|
||||
gtk_window_set_title(window_, title_.c_str());
|
||||
gtk_window_set_default_size(window_, width_, height_);
|
||||
container_ = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
|
||||
gtk_container_add(GTK_CONTAINER(window_), container_);
|
||||
}
|
||||
}
|
||||
|
||||
inline GTKWindow::~GTKWindow() {
|
||||
// GTK会自动管理窗口内存
|
||||
}
|
||||
|
||||
inline void GTKWindow::show() {
|
||||
if (window_) {
|
||||
gtk_widget_show_all(GTK_WIDGET(window_));
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKWindow::hide() {
|
||||
if (window_) {
|
||||
gtk_widget_hide(GTK_WIDGET(window_));
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKWindow::set_title(const std::string& title) {
|
||||
title_ = title;
|
||||
if (window_) {
|
||||
gtk_window_set_title(window_, title_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string GTKWindow::get_title() const {
|
||||
return title_;
|
||||
}
|
||||
|
||||
inline void GTKWindow::set_size(int width, int height) {
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
if (window_) {
|
||||
gtk_window_resize(window_, width_, height_);
|
||||
}
|
||||
}
|
||||
|
||||
inline int GTKWindow::get_width() const {
|
||||
return width_;
|
||||
}
|
||||
|
||||
inline int GTKWindow::get_height() const {
|
||||
return height_;
|
||||
}
|
||||
|
||||
inline void GTKWindow::set_close_callback(std::function<void()> callback) {
|
||||
close_callback_ = callback;
|
||||
if (window_) {
|
||||
g_signal_connect(window_, "destroy", G_CALLBACK(+[](GtkWidget* widget, gpointer data) {
|
||||
auto self = static_cast<GTKWindow*>(data);
|
||||
if (self && self->close_callback_) {
|
||||
self->close_callback_();
|
||||
}
|
||||
}), this);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKWindow::add_widget(GtkWidget* widget) {
|
||||
if (container_ && widget) {
|
||||
gtk_container_add(GTK_CONTAINER(container_), widget);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKWindow::center_on_screen() {
|
||||
if (window_) {
|
||||
gtk_window_set_position(window_, GTK_WIN_POS_CENTER);
|
||||
}
|
||||
}
|
||||
|
||||
// GTKButton 实现
|
||||
inline GTKButton::GTKButton(const std::string& label)
|
||||
: label_(label) {
|
||||
button_ = GTK_BUTTON(gtk_button_new_with_label(label_.c_str()));
|
||||
}
|
||||
|
||||
inline GTKButton::~GTKButton() {
|
||||
// GTK会自动管理按钮内存
|
||||
}
|
||||
|
||||
inline void GTKButton::set_click_callback(std::function<void()> callback) {
|
||||
click_callback_ = callback;
|
||||
if (button_) {
|
||||
g_signal_connect(button_, "clicked", G_CALLBACK(+[](GtkButton* button, gpointer data) {
|
||||
auto self = static_cast<GTKButton*>(data);
|
||||
if (self && self->click_callback_) {
|
||||
self->click_callback_();
|
||||
}
|
||||
}), this);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKButton::set_label(const std::string& label) {
|
||||
label_ = label;
|
||||
if (button_) {
|
||||
gtk_button_set_label(button_, label_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string GTKButton::get_label() const {
|
||||
return label_;
|
||||
}
|
||||
|
||||
inline void GTKButton::enable() {
|
||||
if (button_) {
|
||||
gtk_widget_set_sensitive(GTK_WIDGET(button_), TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKButton::disable() {
|
||||
if (button_) {
|
||||
gtk_widget_set_sensitive(GTK_WIDGET(button_), FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKButton::set_tooltip(const std::string& tooltip) {
|
||||
if (button_) {
|
||||
gtk_widget_set_tooltip_text(GTK_WIDGET(button_), tooltip.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// GTKLabel 实现
|
||||
inline GTKLabel::GTKLabel(const std::string& text)
|
||||
: text_(text) {
|
||||
label_ = GTK_LABEL(gtk_label_new(text_.c_str()));
|
||||
}
|
||||
|
||||
inline GTKLabel::~GTKLabel() {
|
||||
// GTK会自动管理标签内存
|
||||
}
|
||||
|
||||
inline void GTKLabel::set_text(const std::string& text) {
|
||||
text_ = text;
|
||||
if (label_) {
|
||||
gtk_label_set_text(label_, text_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string GTKLabel::get_text() const {
|
||||
return text_;
|
||||
}
|
||||
|
||||
inline void GTKLabel::set_alignment(float xalign, float yalign) {
|
||||
if (label_) {
|
||||
gtk_label_set_xalign(label_, xalign);
|
||||
gtk_label_set_yalign(label_, yalign);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKLabel::set_wrap(bool wrap) {
|
||||
if (label_) {
|
||||
gtk_label_set_line_wrap(label_, wrap);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKLabel::set_selectable(bool selectable) {
|
||||
if (label_) {
|
||||
gtk_label_set_selectable(label_, selectable);
|
||||
}
|
||||
}
|
||||
|
||||
// GTKEntry 实现
|
||||
inline GTKEntry::GTKEntry(const std::string& placeholder)
|
||||
: placeholder_(placeholder) {
|
||||
entry_ = GTK_ENTRY(gtk_entry_new());
|
||||
if (!placeholder_.empty()) {
|
||||
gtk_entry_set_placeholder_text(entry_, placeholder_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
inline GTKEntry::~GTKEntry() {
|
||||
// GTK会自动管理输入框内存
|
||||
}
|
||||
|
||||
inline std::string GTKEntry::get_text() const {
|
||||
if (entry_) {
|
||||
const gchar* text = gtk_entry_get_text(entry_);
|
||||
return text ? std::string(text) : "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
inline void GTKEntry::set_text(const std::string& text) {
|
||||
if (entry_) {
|
||||
gtk_entry_set_text(entry_, text.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKEntry::clear() {
|
||||
if (entry_) {
|
||||
gtk_entry_set_text(entry_, "");
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKEntry::set_placeholder(const std::string& placeholder) {
|
||||
placeholder_ = placeholder;
|
||||
if (entry_) {
|
||||
gtk_entry_set_placeholder_text(entry_, placeholder_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKEntry::set_changed_callback(std::function<void()> callback) {
|
||||
changed_callback_ = callback;
|
||||
if (entry_) {
|
||||
g_signal_connect(entry_, "changed", G_CALLBACK(+[](GtkEntry* entry, gpointer data) {
|
||||
auto self = static_cast<GTKEntry*>(data);
|
||||
if (self && self->changed_callback_) {
|
||||
self->changed_callback_();
|
||||
}
|
||||
}), this);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKEntry::set_activate_callback(std::function<void()> callback) {
|
||||
activate_callback_ = callback;
|
||||
if (entry_) {
|
||||
g_signal_connect(entry_, "activate", G_CALLBACK(+[](GtkEntry* entry, gpointer data) {
|
||||
auto self = static_cast<GTKEntry*>(data);
|
||||
if (self && self->activate_callback_) {
|
||||
self->activate_callback_();
|
||||
}
|
||||
}), this);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKEntry::enable() {
|
||||
if (entry_) {
|
||||
gtk_widget_set_sensitive(GTK_WIDGET(entry_), TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKEntry::disable() {
|
||||
if (entry_) {
|
||||
gtk_widget_set_sensitive(GTK_WIDGET(entry_), FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
// GTKTextView 实现
|
||||
inline GTKTextView::GTKTextView() {
|
||||
text_view_ = GTK_TEXT_VIEW(gtk_text_view_new());
|
||||
buffer_ = gtk_text_view_get_buffer(text_view_);
|
||||
}
|
||||
|
||||
inline GTKTextView::~GTKTextView() {
|
||||
// GTK会自动管理文本视图内存
|
||||
}
|
||||
|
||||
inline std::string GTKTextView::get_text() const {
|
||||
if (buffer_) {
|
||||
GtkTextIter start, end;
|
||||
gtk_text_buffer_get_start_iter(buffer_, &start);
|
||||
gtk_text_buffer_get_end_iter(buffer_, &end);
|
||||
gchar* text = gtk_text_buffer_get_text(buffer_, &start, &end, FALSE);
|
||||
std::string result(text ? text : "");
|
||||
g_free(text);
|
||||
return result;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
inline void GTKTextView::set_text(const std::string& text) {
|
||||
if (buffer_) {
|
||||
gtk_text_buffer_set_text(buffer_, text.c_str(), -1);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKTextView::append_text(const std::string& text) {
|
||||
if (buffer_) {
|
||||
GtkTextIter end;
|
||||
gtk_text_buffer_get_end_iter(buffer_, &end);
|
||||
gtk_text_buffer_insert(buffer_, &end, text.c_str(), -1);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKTextView::clear() {
|
||||
if (buffer_) {
|
||||
gtk_text_buffer_set_text(buffer_, "", -1);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKTextView::set_read_only(bool read_only) {
|
||||
if (text_view_) {
|
||||
gtk_text_view_set_editable(text_view_, !read_only);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKTextView::set_changed_callback(std::function<void()> callback) {
|
||||
changed_callback_ = callback;
|
||||
if (buffer_) {
|
||||
g_signal_connect(buffer_, "changed", G_CALLBACK(+[](GtkTextBuffer* buffer, gpointer data) {
|
||||
auto self = static_cast<GTKTextView*>(data);
|
||||
if (self && self->changed_callback_) {
|
||||
self->changed_callback_();
|
||||
}
|
||||
}), this);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKTextView::scroll_to_bottom() {
|
||||
if (text_view_ && buffer_) {
|
||||
GtkTextIter end;
|
||||
gtk_text_buffer_get_end_iter(buffer_, &end);
|
||||
gtk_text_view_scroll_to_iter(text_view_, &end, 0.0, FALSE, 0.0, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
// GTKBox 实现
|
||||
inline GTKBox::GTKBox(GtkOrientation orientation, int spacing)
|
||||
: orientation_(orientation), spacing_(spacing) {
|
||||
box_ = GTK_BOX(gtk_box_new(orientation_, spacing_));
|
||||
}
|
||||
|
||||
inline GTKBox::~GTKBox() {
|
||||
// GTK会自动管理盒子内存
|
||||
}
|
||||
|
||||
inline void GTKBox::add_widget(GtkWidget* widget, bool expand, bool fill, int padding) {
|
||||
if (box_ && widget) {
|
||||
gtk_box_pack_start(box_, widget, expand, fill, padding);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKBox::set_spacing(int spacing) {
|
||||
spacing_ = spacing;
|
||||
if (box_) {
|
||||
gtk_box_set_spacing(box_, spacing_);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKBox::set_alignment(GtkAlign align) {
|
||||
if (box_) {
|
||||
gtk_widget_set_halign(GTK_WIDGET(box_), align);
|
||||
gtk_widget_set_valign(GTK_WIDGET(box_), align);
|
||||
}
|
||||
}
|
||||
|
||||
inline void GTKBox::set_homogeneous(bool homogeneous) {
|
||||
if (box_) {
|
||||
gtk_box_set_homogeneous(box_, homogeneous);
|
||||
}
|
||||
}
|
||||
|
||||
// 全局函数实现
|
||||
inline bool gtk_init(int* argc, char*** argv) {
|
||||
return gtk_init_check(argc, argv);
|
||||
}
|
||||
|
||||
inline void gtk_main() {
|
||||
::gtk_main();
|
||||
}
|
||||
|
||||
inline void gtk_main_quit() {
|
||||
::gtk_main_quit();
|
||||
}
|
||||
|
||||
inline void show_error_dialog(GtkWindow* parent, const std::string& title, const std::string& message) {
|
||||
GtkWidget* dialog = gtk_message_dialog_new(parent,
|
||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_MESSAGE_ERROR,
|
||||
GTK_BUTTONS_OK,
|
||||
"%s", message.c_str());
|
||||
gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
|
||||
gtk_dialog_run(GTK_DIALOG(dialog));
|
||||
gtk_widget_destroy(dialog);
|
||||
}
|
||||
|
||||
inline void show_info_dialog(GtkWindow* parent, const std::string& title, const std::string& message) {
|
||||
GtkWidget* dialog = gtk_message_dialog_new(parent,
|
||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_MESSAGE_INFO,
|
||||
GTK_BUTTONS_OK,
|
||||
"%s", message.c_str());
|
||||
gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
|
||||
gtk_dialog_run(GTK_DIALOG(dialog));
|
||||
gtk_widget_destroy(dialog);
|
||||
}
|
||||
|
||||
inline bool show_confirm_dialog(GtkWindow* parent, const std::string& title, const std::string& message) {
|
||||
GtkWidget* dialog = gtk_message_dialog_new(parent,
|
||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_MESSAGE_QUESTION,
|
||||
GTK_BUTTONS_YES_NO,
|
||||
"%s", message.c_str());
|
||||
gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
|
||||
gint response = gtk_dialog_run(GTK_DIALOG(dialog));
|
||||
gtk_widget_destroy(dialog);
|
||||
return response == GTK_RESPONSE_YES;
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace pmc
|
||||
|
||||
#endif // PMC_GUI_GTK_IMPL_HPP
|
||||
@@ -0,0 +1,174 @@
|
||||
# PMC GUI 模块 - GTK 封装
|
||||
|
||||
## 概述
|
||||
|
||||
本模块提供了对GTK库的C++封装,简化在pmc项目中使用GTK进行GUI开发的过程。封装提供了面向对象的接口,隐藏了GTK的C语言细节。
|
||||
|
||||
## 文件结构
|
||||
|
||||
```
|
||||
include/gui/
|
||||
├── GTK.hpp # 主封装头文件(类声明)
|
||||
├── GTK_impl.hpp # 内联实现文件
|
||||
├── gui.hpp # GUI模块主入口
|
||||
├── example.hpp # 使用示例
|
||||
└── README.md # 本文档
|
||||
```
|
||||
|
||||
## 主要类
|
||||
|
||||
### 1. GTKApplication
|
||||
封装GTK应用程序的初始化和运行。
|
||||
|
||||
```cpp
|
||||
pmc::gui::GTKApplication app("com.example.pmc", "PMC Application");
|
||||
app.initialize(argc, argv);
|
||||
app.run();
|
||||
```
|
||||
|
||||
### 2. GTKWindow
|
||||
封装GTK窗口的创建和管理。
|
||||
|
||||
```cpp
|
||||
pmc::gui::GTKWindow window("My Window", 800, 600);
|
||||
window.set_close_callback([]() {
|
||||
std::cout << "Window closing" << std::endl;
|
||||
gtk_main_quit();
|
||||
});
|
||||
window.show();
|
||||
```
|
||||
|
||||
### 3. GTKButton
|
||||
封装GTK按钮的创建和事件处理。
|
||||
|
||||
```cpp
|
||||
pmc::gui::GTKButton button("Click Me");
|
||||
button.set_click_callback([]() {
|
||||
std::cout << "Button clicked!" << std::endl;
|
||||
});
|
||||
```
|
||||
|
||||
### 4. GTKEntry
|
||||
封装GTK输入框的创建和文本管理。
|
||||
|
||||
```cpp
|
||||
pmc::gui::GTKEntry entry("Enter text here...");
|
||||
entry.set_activate_callback([]() {
|
||||
std::string text = entry.get_text();
|
||||
std::cout << "Entered: " << text << std::endl;
|
||||
});
|
||||
```
|
||||
|
||||
### 5. GTKTextView
|
||||
封装GTK文本视图的创建和文本管理。
|
||||
|
||||
```cpp
|
||||
pmc::gui::GTKTextView text_view;
|
||||
text_view.set_read_only(true);
|
||||
text_view.append_text("Hello, World!\n");
|
||||
```
|
||||
|
||||
### 6. GTKBox
|
||||
封装GTK盒子布局容器的创建和子控件管理。
|
||||
|
||||
```cpp
|
||||
pmc::gui::GTKBox vbox(GTK_ORIENTATION_VERTICAL, 5);
|
||||
vbox.add_widget(GTK_WIDGET(button.get_button()));
|
||||
vbox.add_widget(GTK_WIDGET(entry.get_entry()));
|
||||
```
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 简单聊天窗口
|
||||
|
||||
```cpp
|
||||
#include "gui/example.hpp"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
pmc::gui::example::run_simple_chat_example(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### 设置窗口
|
||||
|
||||
```cpp
|
||||
#include "gui/example.hpp"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
pmc::gui::example::run_settings_example(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
## 编译要求
|
||||
|
||||
### 依赖项
|
||||
- GTK+ 3.0 或更高版本
|
||||
- C++11 或更高版本
|
||||
|
||||
### Ubuntu/Debian 安装依赖
|
||||
```bash
|
||||
sudo apt-get install libgtk-3-dev
|
||||
```
|
||||
|
||||
### Fedora/RHEL 安装依赖
|
||||
```bash
|
||||
sudo dnf install gtk3-devel
|
||||
```
|
||||
|
||||
### 编译命令示例
|
||||
```bash
|
||||
g++ -std=c++11 -o myapp main.cpp `pkg-config --cflags --libs gtk+-3.0`
|
||||
```
|
||||
|
||||
## 集成到pmc项目
|
||||
|
||||
### 1. 在CMakeLists.txt中添加
|
||||
```cmake
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
|
||||
|
||||
include_directories(${GTK3_INCLUDE_DIRS})
|
||||
target_link_libraries(your_target ${GTK3_LIBRARIES})
|
||||
```
|
||||
|
||||
### 2. 使用封装类
|
||||
```cpp
|
||||
#include "gui/GTK.hpp"
|
||||
|
||||
// 使用pmc::gui命名空间中的类
|
||||
```
|
||||
|
||||
## 设计原则
|
||||
|
||||
1. **简化接口**:隐藏GTK的C语言细节,提供C++面向对象接口
|
||||
2. **资源管理**:使用RAII原则管理GTK对象生命周期
|
||||
3. **类型安全**:使用C++类型系统减少错误
|
||||
4. **可扩展性**:易于添加新的封装类
|
||||
5. **向后兼容**:保持与原生GTK API的互操作性
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **线程安全**:GTK不是线程安全的,所有GUI操作必须在主线程进行
|
||||
2. **内存管理**:GTK使用引用计数,封装类会自动处理引用
|
||||
3. **信号连接**:使用lambda表达式简化信号处理
|
||||
4. **错误处理**:提供基本的错误检查和对话框支持
|
||||
|
||||
## 扩展指南
|
||||
|
||||
要添加新的GTK控件封装:
|
||||
|
||||
1. 在`GTK.hpp`中声明类
|
||||
2. 在`GTK_impl.hpp`中实现内联方法
|
||||
3. 提供构造函数、析构函数和主要方法
|
||||
4. 添加信号连接支持
|
||||
5. 更新示例和文档
|
||||
|
||||
## 许可证
|
||||
|
||||
本模块遵循pmc项目的整体许可证。
|
||||
|
||||
## 贡献
|
||||
|
||||
欢迎提交问题和拉取请求改进本模块。
|
||||
@@ -0,0 +1,100 @@
|
||||
#ifndef PMC_GUI_HPP
|
||||
#define PMC_GUI_HPP
|
||||
|
||||
/**
|
||||
* @file gui.hpp
|
||||
* @brief pmc项目GUI模块主入口
|
||||
*
|
||||
* 提供GTK GUI模块的统一入口,简化GUI开发。
|
||||
*/
|
||||
|
||||
#include "gui/GTK.hpp"
|
||||
|
||||
namespace pmc {
|
||||
namespace gui {
|
||||
|
||||
/**
|
||||
* @brief 初始化GUI模块
|
||||
* @param argc 命令行参数数量
|
||||
* @param argv 命令行参数数组
|
||||
* @return 初始化是否成功
|
||||
*/
|
||||
bool init(int argc, char** argv);
|
||||
|
||||
/**
|
||||
* @brief 运行GUI主循环
|
||||
*/
|
||||
void run();
|
||||
|
||||
/**
|
||||
* @brief 退出GUI主循环
|
||||
*/
|
||||
void quit();
|
||||
|
||||
/**
|
||||
* @brief 创建主窗口
|
||||
* @param title 窗口标题
|
||||
* @param width 窗口宽度
|
||||
* @param height 窗口高度
|
||||
* @return 窗口指针
|
||||
*/
|
||||
GTKWindow* create_main_window(const std::string& title = "PMC Application",
|
||||
int width = 800, int height = 600);
|
||||
|
||||
/**
|
||||
* @brief 创建聊天窗口
|
||||
* @param parent 父窗口
|
||||
* @return 聊天窗口指针
|
||||
*/
|
||||
GTKWindow* create_chat_window(GTKWindow* parent = nullptr);
|
||||
|
||||
/**
|
||||
* @brief 创建设置窗口
|
||||
* @param parent 父窗口
|
||||
* @return 设置窗口指针
|
||||
*/
|
||||
GTKWindow* create_settings_window(GTKWindow* parent = nullptr);
|
||||
|
||||
/**
|
||||
* @brief 显示关于对话框
|
||||
* @param parent 父窗口
|
||||
*/
|
||||
void show_about_dialog(GTKWindow* parent);
|
||||
|
||||
/**
|
||||
* @brief 显示状态栏
|
||||
* @param parent 父窗口
|
||||
* @param message 状态消息
|
||||
*/
|
||||
void show_status_bar(GTKWindow* parent, const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 更新状态栏消息
|
||||
* @param message 新状态消息
|
||||
*/
|
||||
void update_status_bar(const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 显示进度对话框
|
||||
* @param parent 父窗口
|
||||
* @param title 对话框标题
|
||||
* @param message 进度消息
|
||||
*/
|
||||
void show_progress_dialog(GTKWindow* parent, const std::string& title, const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 更新进度对话框
|
||||
* @param progress 进度值 (0.0-1.0)
|
||||
* @param message 进度消息
|
||||
*/
|
||||
void update_progress_dialog(double progress, const std::string& message);
|
||||
|
||||
/**
|
||||
* @brief 关闭进度对话框
|
||||
*/
|
||||
void close_progress_dialog();
|
||||
|
||||
} // namespace gui
|
||||
} // namespace pmc
|
||||
|
||||
#endif // PMC_GUI_HPP
|
||||
Reference in new issue
Block a user