Created
November 13, 2020 16:20
-
-
Save gicrisf/ff67b6a4a2c8cf69155e4fd07e42e363 to your computer and use it in GitHub Desktop.
Minimal setup for a gtk::MessageDialog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Make a transient parent if you can't pass a real toplevel window | |
| let transient = gtk::Window::new(gtk::WindowType::Popup); | |
| transient.set_position(gtk::WindowPosition::Center); | |
| // Make Message Dialog with your favorite settings | |
| let dialog = gtk::MessageDialog::new( | |
| Some(&transient), // toplevel window is preferred, but the point is that he wants a window | |
| gtk::DialogFlags::MODAL, // https://gtk-rs.org/docs/gtk/struct.DialogFlags.html | |
| gtk::MessageType::Error, // https://gtk-rs.org/docs/gtk/enum.MessageType.html | |
| gtk::ButtonsType::Ok, // https://gtk-rs.org/docs/gtk/enum.ButtonsType.html | |
| &error.to_string() // Show GTK error or replace it with your own error | |
| ); | |
| let result = dialog.run(); // run dialog and return the result in a variable | |
| match result { | |
| gtk::ResponseType::Ok => dialog.close(), // if result is Ok... NOT calling destroy() because unsafe | |
| _ => dialog.close(), // else... | |
| }; | |
| // Go on with your software | |
| // This code is based on the original C code in the docs: | |
| // https://gtk-rs.org/docs/gtk/struct.MessageDialog.html | |
| /* | |
| ``` | |
| GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT; | |
| dialog = gtk_message_dialog_new (parent_window, | |
| flags, | |
| GTK_MESSAGE_ERROR, | |
| GTK_BUTTONS_CLOSE, | |
| "Error reading “%s”: %s", | |
| filename, | |
| g_strerror (errno)); | |
| gtk_dialog_run (GTK_DIALOG (dialog)); | |
| gtk_widget_destroy (dialog); | |
| ``` | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment