I apologize for the continued issues. You're right - there are still deprecated and removed features in the code. Let me provide a completely updated version that properly uses GTK4 without relying on any deprecated or removed features.
The main issues with the previous code:
- GTK4 no longer uses event signals like
signal_button_press_event() - these are replaced with gestures
show_all() is removed in GTK4
- Widget parenting needs to be handled differently
- The event structs like
GdkEventButton are no longer available
Here's a corrected version that properly uses GTK4 features:
#include <gtkmm.h>
#include <json/json.h>
#include <fstream>
#include <vector>
#include <iostream>
class PathEditor : public Gtk::ApplicationWindow {
public:
PathEditor();
virtual ~PathEditor() = default;
private:
// Signal handlers
void on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height);
void on_press(int n_press, double x, double y);
void on_motion(double x, double y);
void on_release(int n_press, double x, double y);
void on_save();
void on_load();
// Member widgets
Gtk::DrawingArea m_drawing_area;
Gtk::Box m_button_box;
Gtk::Button m_save_button;
Gtk::Button m_load_button;
Gtk::Box m_main_box;
// Gesture controllers
Glib::RefPtr<Gtk::GestureClick> m_click_gesture;
Glib::RefPtr<Gtk::GestureDrag> m_drag_gesture;
// Data
std::vector<std::vector<std::pair<double, double>>> m_paths;
bool m_is_drawing = false;
};
PathEditor::PathEditor()
: Gtk::ApplicationWindow(),
m_button_box(Gtk::Orientation::HORIZONTAL, 6),
m_save_button("Save"),
m_load_button("Load"),
m_main_box(Gtk::Orientation::VERTICAL, 6) {
set_title("Path Editor");
set_default_size(800, 600);
// Set up drawing area
m_drawing_area.set_expand(true);
m_drawing_area.set_content_width(800);
m_drawing_area.set_content_height(500);
m_drawing_area.set_draw_func(sigc::mem_fun(*this, &PathEditor::on_draw));
// Set up gesture controllers
m_click_gesture = Gtk::GestureClick::create();
m_click_gesture->set_button(GDK_BUTTON_PRIMARY);
m_click_gesture->signal_pressed().connect(sigc::mem_fun(*this, &PathEditor::on_press));
m_click_gesture->signal_released().connect(sigc::mem_fun(*this, &PathEditor::on_release));
m_drawing_area.add_controller(m_click_gesture);
m_drag_gesture = Gtk::GestureDrag::create();
m_drag_gesture->signal_drag_update().connect([this](double offset_x, double offset_y) {
double start_x, start_y;
m_drag_gesture->get_start_point(start_x, start_y);
on_motion(start_x + offset_x, start_y + offset_y);
});
m_drawing_area.add_controller(m_drag_gesture);
// Set up buttons
m_save_button.signal_clicked().connect(sigc::mem_fun(*this, &PathEditor::on_save));
m_load_button.signal_clicked().connect(sigc::mem_fun(*this, &PathEditor::on_load));
m_button_box.append(m_save_button);
m_button_box.append(m_load_button);
// Set up main layout
m_main_box.append(m_drawing_area);
m_main_box.append(m_button_box);
set_child(m_main_box);
}
void PathEditor::on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height) {
// Clear the area with white
cr->set_source_rgb(1.0, 1.0, 1.0);
cr->paint();
// Draw paths in black
cr->set_source_rgb(0.0, 0.0, 0.0);
cr->set_line_width(2.0);
for (const auto& path : m_paths) {
if (!path.empty()) {
cr->move_to(path[0].first, path[0].second);
for (size_t i = 1; i < path.size(); i++) {
cr->line_to(path[i].first, path[i].second);
}
cr->stroke();
}
}
}
void PathEditor::on_press(int n_press, double x, double y) {
m_is_drawing = true;
m_paths.push_back({{x, y}});
m_drawing_area.queue_draw();
}
void PathEditor::on_motion(double x, double y) {
if (m_is_drawing && !m_paths.empty()) {
m_paths.back().push_back({x, y});
m_drawing_area.queue_draw();
}
}
void PathEditor::on_release(int n_press, double x, double y) {
m_is_drawing = false;
if (!m_paths.empty()) {
m_paths.back().push_back({x, y});
m_drawing_area.queue_draw();
}
}
void PathEditor::on_save() {
auto dialog = Gtk::FileDialog::create();
dialog->set_title("Save Paths");
dialog->set_initial_name("paths.json");
auto filter = Gtk::FileFilter::create();
filter->set_name("JSON files");
filter->add_pattern("*.json");
dialog->set_default_filter(filter);
dialog->save(
*this,
[this](const Glib::RefPtr<Gio::AsyncResult>& result, const Glib::RefPtr<Gtk::FileDialog>& dialog) {
try {
auto file = dialog->save_finish(result);
if (file) {
std::ofstream ofs(file->get_path());
if (ofs.is_open()) {
Json::Value root(Json::arrayValue);
for (const auto& path : m_paths) {
Json::Value path_json(Json::arrayValue);
for (const auto& point : path) {
Json::Value point_json;
point_json["x"] = point.first;
point_json["y"] = point.second;
path_json.append(point_json);
}
root.append(path_json);
}
ofs << root.toStyledString();
} else {
std::cerr << "Failed to open file for writing." << std::endl;
}
}
} catch (const Glib::Error& error) {
std::cerr << "Error saving file: " << error.what() << std::endl;
}
},
Glib::RefPtr<Gio::Cancellable>()
);
}
void PathEditor::on_load() {
auto dialog = Gtk::FileDialog::create();
dialog->set_title("Load Paths");
auto filter = Gtk::FileFilter::create();
filter->set_name("JSON files");
filter->add_pattern("*.json");
dialog->set_default_filter(filter);
dialog->open(
*this,
[this](const Glib::RefPtr<Gio::AsyncResult>& result, const Glib::RefPtr<Gtk::FileDialog>& dialog) {
try {
auto file = dialog->open_finish(result);
if (file) {
std::ifstream ifs(file->get_path());
if (ifs.is_open()) {
Json::Value root;
ifs >> root;
m_paths.clear();
if (root.isArray()) {
for (const auto& path_json : root) {
if (path_json.isArray()) {
std::vector<std::pair<double, double>> path;
for (const auto& point_json : path_json) {
if (point_json.isObject() && point_json.isMember("x") && point_json.isMember("y")) {
path.emplace_back(point_json["x"].asDouble(), point_json["y"].asDouble());
}
}
if (!path.empty()) {
m_paths.push_back(path);
}
}
}
m_drawing_area.queue_draw();
}
} else {
std::cerr << "Failed to open file for reading." << std::endl;
}
}
} catch (const Glib::Error& error) {
std::cerr << "Error loading file: " << error.what() << std::endl;
}
},
Glib::RefPtr<Gio::Cancellable>()
);
}
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create("org.example.PathEditor");
return app->make_window_and_run<PathEditor>(argc, argv);
}
Key Changes to Make It GTK4 Compatible:
-
Gesture Controllers: Replaced event signals with Gtk::GestureClick and Gtk::GestureDrag controllers
signal_button_press_event() → GestureClick::signal_pressed()
signal_motion_notify_event() → GestureDrag::signal_drag_update()
signal_button_release_event() → GestureClick::signal_released()
-
Widget Hierarchy: Used modern GTK4 methods:
append() instead of pack_start()
- Proper vertical/horizontal box layout
set_child() for the main container
-
File Dialog: Used Gtk::FileDialog with async methods:
save() and open() instead of using response signals
- Proper error handling with try/catch
- Callback-based approach for handling results
-
Removed Deprecated Features:
- No
show_all() call (widgets are visible by default in GTK4)
- No direct event handling with
GdkEventButton structs
- Proper use of GTK4 container methods
-
Other Improvements:
- Prefixed member variables with
m_ for better readability
- Set drawing area to expand to fill available space
- Added error handling for file operations
Compilation:
To compile this code, use:
g++ -o path_editor path_editor.cpp `pkg-config --cflags --libs gtkmm-4.0` -ljsoncpp
This version should now properly use GTK4 features without relying on any deprecated or removed APIs.