Blame platform-demos/C/samples/messagedialog.c

Packit 1470ea
#include <gtk/gtk.h>
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
/* Callback function in which reacts to the "response" signal from the user in
Packit 1470ea
 * the message dialog window.
Packit 1470ea
 * This function is used to interact with the user in the terminal.
Packit 1470ea
 */
Packit 1470ea
static void
Packit 1470ea
on_response (GtkDialog *dialog,
Packit 1470ea
             gint       response_id,
Packit 1470ea
             gpointer   user_data)
Packit 1470ea
{
Packit 1470ea
  /* If the button clicked gives response OK (response_id being -5) */
Packit 1470ea
  if (response_id == GTK_RESPONSE_OK) 
Packit 1470ea
     g_print ("*boom*\n");
Packit 1470ea
Packit 1470ea
  /* If the button clicked gives response CANCEL (response_id being -6) */
Packit 1470ea
  else if (response_id == GTK_RESPONSE_CANCEL)
Packit 1470ea
     g_print ("good choice\n");
Packit 1470ea
Packit 1470ea
  /* If the message dialog is destroyed (for example by pressing escape) */
Packit 1470ea
  else if (response_id == GTK_RESPONSE_DELETE_EVENT)
Packit 1470ea
     g_print ("dialog closed or cancelled\n");
Packit 1470ea
Packit 1470ea
  /* Destroy the dialog after one of the above actions have taken place */
Packit 1470ea
  gtk_widget_destroy (GTK_WIDGET (dialog));
Packit 1470ea
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
/* Callback function for the response signal "activate" related to the SimpleAction
Packit 1470ea
 * message_action.
Packit 1470ea
 * This function is used to cause the message dialog window to popup.
Packit 1470ea
 */
Packit 1470ea
static void
Packit 1470ea
message_cb (GSimpleAction *simple,
Packit 1470ea
            GVariant      *parameter,
Packit 1470ea
            gpointer       user_data)
Packit 1470ea
{
Packit 1470ea
   /* the parent variable in this case represents the window */
Packit 1470ea
   GtkWidget *message_dialog;
Packit 1470ea
   GtkWindow *parent = user_data;
Packit 1470ea
   
Packit 1470ea
   /* Create a new message dialog, and set the parameters as follows:
Packit 1470ea
    * Dialog Flags - make the constructed dialog modal 
Packit 1470ea
    * (modal windows prevent interaction with other windows in the application)
Packit 1470ea
    * Message Type - nonfatal warning message
Packit 1470ea
    * Buttons Type - use the ok and cancel buttons
Packit 1470ea
    * message_format - text that you want the user to see in the window 
Packit 1470ea
    */
Packit 1470ea
   message_dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL, 
Packit 1470ea
                                            GTK_MESSAGE_WARNING, 
Packit 1470ea
                                            GTK_BUTTONS_OK_CANCEL, 
Packit 1470ea
                                            "This action will cause the universe to stop existing.");
Packit 1470ea
Packit 1470ea
   gtk_widget_show_all (message_dialog);
Packit 1470ea
Packit 1470ea
   g_signal_connect (GTK_DIALOG (message_dialog), "response", 
Packit 1470ea
                    G_CALLBACK (on_response), NULL);
Packit 1470ea
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
static void
Packit 1470ea
activate (GtkApplication *app,
Packit 1470ea
          gpointer        user_data)
Packit 1470ea
{
Packit 1470ea
  GtkWidget *window;
Packit 1470ea
  GtkWidget *label;
Packit 1470ea
Packit 1470ea
  GSimpleAction *message_action;
Packit 1470ea
Packit 1470ea
  /* Create a window with a title and a default size */
Packit 1470ea
  window = gtk_application_window_new (app);
Packit 1470ea
  gtk_window_set_title (GTK_WINDOW (window), "GMenu Example");
Packit 1470ea
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 200);
Packit 1470ea
Packit 1470ea
  /* Create a label and add it to the window */
Packit 1470ea
  label = gtk_label_new ("This application goes boom!");
Packit 1470ea
  gtk_container_add (GTK_CONTAINER (window), label);
Packit 1470ea
Packit 1470ea
  /* Create a new simple action, giving it a NULL parameter type. It will 
Packit 1470ea
   * always be NULL for actions invoked from a menu. (e.g clicking on an "ok" 
Packit 1470ea
   * or "cancel" button)
Packit 1470ea
   */
Packit 1470ea
  message_action = g_simple_action_new ("message", NULL); 
Packit 1470ea
Packit 1470ea
  /* Connect the "activate" signal to the appropriate callback function */
Packit 1470ea
  g_signal_connect (message_action, "activate", G_CALLBACK (message_cb), 
Packit 1470ea
                    GTK_WINDOW (window));
Packit 1470ea
Packit 1470ea
  /* Adds the message_action to the overall action map. An Action map is an 
Packit 1470ea
   * interface that contains a number of named GAction instances 
Packit 1470ea
   * (such as message_action) 
Packit 1470ea
   */
Packit 1470ea
  g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (message_action));
Packit 1470ea
Packit 1470ea
  gtk_widget_show_all (window);
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
/* Callback function for the response signal "activate" from the "quit" action 
Packit 1470ea
 * in the function directly below.
Packit 1470ea
 */ 
Packit 1470ea
static void
Packit 1470ea
quit_cb (GSimpleAction *simple,
Packit 1470ea
         GVariant      *parameter,
Packit 1470ea
         gpointer       user_data)
Packit 1470ea
{
Packit 1470ea
  GApplication *application = user_data;
Packit 1470ea
Packit 1470ea
  g_application_quit (application);
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
/* Startup function for the menu we are creating in this sample */
Packit 1470ea
static void
Packit 1470ea
startup (GApplication *app,
Packit 1470ea
         gpointer      user_data)
Packit 1470ea
{
Packit 1470ea
  GMenu *menu;
Packit 1470ea
  GSimpleAction *quit_action;
Packit 1470ea
Packit 1470ea
  /* Initialize the GMenu, and add a menu item with label "Message" and action 
Packit 1470ea
   * "win.message". Also add another menu item with label "Quit" and action 
Packit 1470ea
   * "app.quit" 
Packit 1470ea
   */
Packit 1470ea
  menu = g_menu_new ();
Packit 1470ea
  g_menu_append (menu, "Message", "win.message");
Packit 1470ea
  g_menu_append (menu, "Quit", "app.quit");
Packit 1470ea
Packit 1470ea
  /* Create a new simple action for the application. (In this case it is the 
Packit 1470ea
   * "quit" action.
Packit 1470ea
   */
Packit 1470ea
  quit_action = g_simple_action_new ("quit", NULL);
Packit 1470ea
Packit 1470ea
  /* Ensure that the menu we have just created is set for the overall application */
Packit 1470ea
  gtk_application_set_app_menu (GTK_APPLICATION (app), G_MENU_MODEL (menu));
Packit 1470ea
Packit 1470ea
  g_signal_connect (quit_action, 
Packit 1470ea
                    "activate", 
Packit 1470ea
                    G_CALLBACK (quit_cb), 
Packit 1470ea
                    app);
Packit 1470ea
Packit 1470ea
  g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit_action));
Packit 1470ea
Packit 1470ea
}
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
/* Startup function for the application */
Packit 1470ea
int
Packit 1470ea
main (int argc, char **argv)
Packit 1470ea
{
Packit 1470ea
  GtkApplication *app;
Packit 1470ea
  int status;
Packit 1470ea
Packit 1470ea
  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
Packit 1470ea
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
Packit 1470ea
  g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
Packit 1470ea
  status = g_application_run (G_APPLICATION (app), argc, argv);
Packit 1470ea
  g_object_unref (app);
Packit 1470ea
Packit 1470ea
  return status;
Packit 1470ea
}