From 374d29629be1f78acedefd75fe7dacd1afd3c50c Mon Sep 17 00:00:00 2001 From: James H Date: Mon, 3 Jan 2022 16:13:10 +0000 Subject: [PATCH] Add simple gtkmm3 code --- src/main.cpp | 55 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8870a9f..1a09cfd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,50 @@ -#include +#include +#include -void end_program(GtkWidget *wid, gpointer ptr) +class CloseButton : public Gtk::Button { - gtk_main_quit(); +private: + void on_button_clicked(); + Glib::RefPtr app; +public: + CloseButton(const Glib::ustring &label, Glib::RefPtr app); +}; + +CloseButton::CloseButton(const Glib::ustring &label, Glib::RefPtr app) +{ + this->app = app; + + set_label(label); + + signal_clicked().connect(sigc::mem_fun(*this, &CloseButton::on_button_clicked)); + +} + +void CloseButton::on_button_clicked() +{ + this->app.get()->quit(); +} + +class MyWindow : public Gtk::Window +{ +public: + MyWindow(); +}; + +MyWindow::MyWindow() +{ + set_title("Basic application"); + set_default_size(200, 200); } int main(int argc, char* argv[]) { - gtk_init(&argc, &argv); - GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL); - GtkWidget *btn = gtk_button_new_with_label("Close Window"); - g_signal_connect(btn, "clicked", G_CALLBACK(end_program), NULL); - g_signal_connect(win, "delete_event", G_CALLBACK(end_program), NULL); - gtk_container_add(GTK_CONTAINER(win), btn); - gtk_widget_show_all(win); - gtk_main(); - return 0; + auto app = Gtk::Application::create("org.gtkmm.examples.base"); + MyWindow window; + + CloseButton button("Close", app); + window.add(button); + window.show_all(); + + return app->run(window); }