Thank you for your patience! GTK4 and gtkmm-4 introduced some significant API changes compared to GTK3/gtkmm-3, and many event handling and widget methods were changed or removed.
Let's do a correct GTK4 + gtkmm-4 C++ example that:
Uses Gtk::DrawingArea::set_draw_func() for drawing
Uses Gtk::GestureClick and Gtk::GestureDrag for input handling (mouse events)
Uses Gtk::FileDialog (GTK4's new file dialog API) for import/export
Uses Gtk::Box::append() (GTK4 style)
Uses set_child() instead of add()
Uses Gdk::Point or Gdk::PointD for points (GTK4 uses Gdk::Point for integer points, but for drawing we want floating points, so Gdk::PointD or just double coords)
Uses nlohmann/json for JSON serialization
Complete GTK4 + gtkmm-4 C++ example with proper GTK4 API usage:
cpp
#include <gtkmm.h>
#include <nlohmann/json.hpp>
#include <fstream>
#include <vector>
using json = nlohmann::json;
class PathEditor : public Gtk::ApplicationWindow {
public:
PathEditor()
: drawing_area(), paths(), current_path(), drawing(false)
{
set_default_size(800, 600);
set_title("Simple Path Editor");
// Setup drawing area
drawing_area.set_draw_func([this](auto& da, auto& cr, int width, int height) {
this->on_draw(cr, width, height);
});
// Setup gestures for input handling
gesture_click = Gtk::GestureClick::create();
gesture_drag = Gtk::GestureDrag::create();
drawing_area.add_controller(gesture_click);
drawing_area.add_controller(gesture_drag);
gesture_click->signal_pressed().connect(sigc::mem_fun(*this, &PathEditor::on_click_pressed));
gesture_click->signal_released().connect(sigc::mem_fun(*this, &PathEditor::on_click_released));
gesture_drag->signal_drag_update().connect(sigc::mem_fun(*this, &PathEditor::on_drag_update));
// Layout
auto vbox = Gtk::Box(Gtk::Orientation::VERTICAL, 6);
vbox.append(drawing_area);
auto hbox = Gtk::Box(Gtk::Orientation::HORIZONTAL, 6);
auto import_button = Gtk::Button::create("Import JSON");
import_button->signal_clicked().connect(sigc::mem_fun(*this, &PathEditor::on_import_clicked));
hbox.append(*import_button);
auto export_button = Gtk::Button::create("Export JSON");
export_button->signal_clicked().connect(sigc::mem_fun(*this, &PathEditor::on_export_clicked));
hbox.append(*export_button);
vbox.append(hbox);
set_child(vbox);
}
private:
Gtk::DrawingArea drawing_area;
Glib::RefPtr<Gtk::GestureClick> gesture_click;
Glib::RefPtr<Gtk::GestureDrag> gesture_drag;
std::vector<std::vector<Gdk::PointD>> paths;
std::vector<Gdk::PointD> current_path;
bool drawing;
void on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height) {
// Clear background white
cr->set_source_rgb(1, 1, 1);
cr->paint();
// Draw all saved paths
cr->set_source_rgb(0, 0, 0);
cr->set_line_width(2.0);
for (const auto& path : paths) {
if (path.size() < 2) continue;
cr->move_to(path[0].x, path[0].y);
for (size_t i = 1; i < path.size(); ++i) {
cr->line_to(path[i].x, path[i].y);
}
cr->stroke();
}
// Draw current path
if (current_path.size() >= 2) {
cr->move_to(current_path[0].x, current_path[0].y);
for (size_t i = 1; i < current_path.size(); ++i) {
cr->line_to(current_path[i].x, current_path[i].y);
}
cr->stroke();
}
}
void on_click_pressed(Gtk::GestureClick* gesture, int /*n_press*/, double x, double y) {
drawing = true;
current_path.clear();
current_path.emplace_back(x, y);
drawing_area.queue_draw();
}
void on_click_released(Gtk::GestureClick* gesture, int /*n_press*/, double x, double y) {
if (drawing) {
current_path.emplace_back(x, y);
paths.push_back(current_path);
current_path.clear();
drawing = false;
drawing_area.queue_draw();
}
}
void on_drag_update(Gtk::GestureDrag* gesture, double offset_x, double offset_y) {
if (drawing) {
// Get current pointer position relative to drawing_area
double x, y;
gesture->get_last_position(x, y);
current_path.emplace_back(x, y);
drawing_area.queue_draw();
}
}
void on_import_clicked() {
auto dialog = Gtk::FileDialog::create_for_open(*this);
dialog->set_modal(true);
dialog->set_title("Import JSON");
dialog->add_filter(Gtk::FileFilter::create("JSON files")->add_mime_type("application/json"));
dialog->show();
dialog->signal_response().connect([this, dialog](int response) {
if (response == Gtk::ResponseType::ACCEPT) {
auto file = dialog->get_file();
if (file) {
try {
std::ifstream ifs(file->get_path());
json j;
ifs >> j;
paths = json_to_paths(j);
current_path.clear();
drawing_area.queue_draw();
} catch (const std::exception& e) {
Gtk::MessageDialog error_dialog(*this, "Failed to load JSON: " + std::string(e.what()), false, Gtk::MessageType::ERROR);
error_dialog.run();
}
}
}
dialog->close();
});
}
void on_export_clicked() {
auto dialog = Gtk::FileDialog::create_for_save(*this);
dialog->set_modal(true);
dialog->set_title("Export JSON");
dialog->add_filter(Gtk::FileFilter::create("JSON files")->add_mime_type("application/json"));
dialog->show();
dialog->signal_response().connect([this, dialog](int response) {
if (response == Gtk::ResponseType::ACCEPT) {
auto file = dialog->get_file();
if (file) {
try {
std::ofstream ofs(file->get_path());
json j = paths_to_json(paths);
ofs << j.dump(4);
} catch (const std::exception& e) {
Gtk::MessageDialog error_dialog(*this, "Failed to save JSON: " + std::string(e.what()), false, Gtk::MessageType::ERROR);
error_dialog.run();
}
}
}
dialog->close();
});
}
json paths_to_json(const std::vector<std::vector<Gdk::PointD>>& paths) {
json j = json::array();
for (const auto& path : paths) {
json path_json = json::array();
for (const auto& pt : path) {
path_json.push_back({{"x", pt.x}, {"y", pt.y}});
}
j.push_back(path_json);
}
return j;
}
std::vector<std::vector<Gdk::PointD>> json_to_paths(const json& j) {
std::vector<std::vector<Gdk::PointD>> result;
for (const auto& path_json : j) {
std::vector<Gdk::PointD> path;
for (const auto& pt_json : path_json) {
double x = pt_json.value("x", 0.0);
double y = pt_json.value("y", 0.0);
path.emplace_back(x, y);
}
result.push_back(path);
}
return result;
}
};
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create("org.example.patheditor");
PathEditor window;
return app->run(argc, argv);
}
Key points:
Input handling: Uses Gtk::GestureClick and Gtk::GestureDrag controllers attached to the drawing area.
Drawing: Uses Gtk::DrawingArea::set_draw_func() with Cairo context.
File dialogs: Uses Gtk::FileDialog (GTK4's new file dialog API).
Layout: Uses Gtk::Box::append() and set_child() for GTK4.
Points: Uses Gdk::PointD (double precision points) for smooth drawing.
JSON: Uses nlohmann/json for serialization/deserialization.