Add simple gtkmm3 code
This commit is contained in:
55
src/main.cpp
55
src/main.cpp
@@ -1,19 +1,50 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <gtkmm.h>
|
||||
#include <iostream>
|
||||
|
||||
void end_program(GtkWidget *wid, gpointer ptr)
|
||||
class CloseButton : public Gtk::Button
|
||||
{
|
||||
gtk_main_quit();
|
||||
private:
|
||||
void on_button_clicked();
|
||||
Glib::RefPtr<Gtk::Application> app;
|
||||
public:
|
||||
CloseButton(const Glib::ustring &label, Glib::RefPtr<Gtk::Application> app);
|
||||
};
|
||||
|
||||
CloseButton::CloseButton(const Glib::ustring &label, Glib::RefPtr<Gtk::Application> 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user