Linux Headquarters

I've installed Linux… now what???

GtkDialog – Dialog Box

Introduction
A dialog box is used to create a pop-up window of some kind. Usually, they are used for simple tasks such as displaying a message to the user or prompting for a simple input. In this example, we have a GtkButton widget in the main window that, when pressed, creates a dialog box. This dialog box displays a simple message and has a button that closes the dialog box when pressed.

Source Code

/*
 *File name: dialog.c
 */

#include <gtk/gtk.h>
#include <glib.h>

/*-- This function allows the program to exit properly when the window is closed --*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{
  g_print ("Quitting...\n");
  gtk_main_quit();
  return (FALSE);
}

/*-- This function responds to the mouse click on the button --*/
void button_clicked(GtkWidget *widget, gpointer gdata)
{
  GtkWidget *dialog, *label, *okay_button;

  /* Create the widgets */
  dialog = gtk_dialog_new();
  label = gtk_label_new ("This is a dialog box");
  okay_button = gtk_button_new_with_label("Okay");

  /* Ensure that the dialog box is destroyed when the user clicks ok. */
  gtk_signal_connect_object(GTK_OBJECT (okay_button), "clicked", gtk_widget_destroy, GTK_OBJECT(dialog));
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->action_area), okay_button);

  /* Add the label, and show everything we've added to the dialog. */
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), label);
  gtk_widget_show_all (dialog);

  g_print("Button was clicked.\n");
}

int main (int argc, char *argv[])
{
  /*-- Declare the GTK Widgets used in the program --*/
  GtkWidget *window;
  GtkWidget *button;

  /*--  Initialize GTK --*/
  gtk_init (&argc, &argv);

  /*-- Create the new window --*/
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  /*-- Create a button --*/
  button = gtk_button_new_with_label("Click Me");

  /*-- Connect the window to the destroyapp function  --*/
  gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL);

  /*-- Connect the button to the button_was_clicked function --*/
  gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(button_clicked), NULL);

  /*-- Add the button to the window --*/
  gtk_container_add(GTK_CONTAINER (window), button);

  /*-- Add a border to the window to give the button a little room --*/
  gtk_container_border_width (GTK_CONTAINER (window), 15);

  /*-- Display the widgets --*/
  gtk_widget_show(button);
  gtk_widget_show(window);

  /*-- Start the GTK event loop --*/
  gtk_main();

  /*-- Return 0 if exit is successful --*/
  return 0;
}

Compile the Source Code
gcc -Wall -g dialog.c -o dialog `gtk-config –cflags` `gtk-config –libs`

Execute the Program
./dialog

What is Related

Comments are closed.

Subscribe to email feed

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Dedicated Linux Serv

Linux is the popular system nowadays, offering all the benefits ...

StarOffice 5.1

Introduction StarOffice 5.1 is a complete office suite with a word ...

AxY FTP

Introduction AxY FTP, formerly known as wxFTP, is a graphical FTP ...

Adobe Acrobat PDF Re

Introduction Many of you are probably familar with Adobe Acrobat Reader ...

Macromedia Shockwave

Introduction Macromedia has developed a version of its popular Shockwave plugin ...

Twitter updates