Page 1 of 1

The Exit App (C)

Posted: Mon Apr 11, 2011 3:47 am
by mikethedj4
NOTE: Make sure to do all your programming in a text editor, and NOT a Word Processor!!!

In this tutorial, I showed you guys how you can create a simple window in C. Well today all we're going to do is add a button to that window.

The Coding:
All the code below does is open up a window, with the title "Exit App" for the programs name, with of 200px, and height of 10px, the app is positioned via centered.
Code: Select all
#include <gtk/gtk.h>

int main (int argc, char **argv) {
  GtkWidget *window;
  gtk_init (&argc, &argv);

//////The Window//////
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Exit App");
  g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
  gtk_window_resize(GTK_WINDOW (window),200,10);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_widget_show (window);
  gtk_main ();

  return 0;
}
So now we need to declare a call for our button so under GtkWidget *window; put down GtkWidget *button;. Pretty simple huh?, but we're not done yet.

Now lets add the following codes under gtk_container_set_border_width (GTK_CONTAINER (window), 10);.
Code: Select all
button = gtk_button_new_with_label ("Exit!");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_container_add (GTK_CONTAINER (window), button);
The line on the top tells us the button's gonna say "Exit!".
The line in the middle tells us that when we click the button it's gonna close the app.
and
The line on the bottom tells us the button will be visible.

Now make sure to add the following code before gtk_widget_show (window);
Code: Select all
gtk_widget_show (button);
NOTE: If you wanna give the whole window a border, you'd put the following code below, gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);.
Code: Select all
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
However we're not going to be adding a border in our program, I just thought I'd mention that encase someone would like to.

Now our full code for our program should look like this.
Code: Select all
#include <gtk/gtk.h>

int main (int argc, char **argv) {
  GtkWidget *window;
  GtkWidget *button;
  gtk_init (&argc, &argv);

//////The Window//////
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Exit App");
  g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
  gtk_window_resize(GTK_WINDOW (window),200,10);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

//////The Button//////
  button = gtk_button_new_with_label ("Exit!");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
  gtk_container_add (GTK_CONTAINER (window), button);

  gtk_widget_show (button);
  gtk_widget_show (window);
  gtk_main ();

  return 0;
}
Now lets save this file to our desktop as exitapp.c.

Compile:
Now we need to open our Terminal, and navigate to the desktop directory.
Code: Select all
cd Desktop
Now compile the program we just created with the following code.
Code: Select all
gcc -Wall -g -o Exit exitapp.c `pkg-config --cflags --libs gtk+-2.0` -export-dynamic
exitapp_compile.png
You should get no errors, and your application should look like this.
exitapp.png
You can download the app below as well as the source, if you wanna take a look, try it out, or may need further help.

Re: The Exit App

Posted: Thu Apr 14, 2011 12:52 pm
by Agust1337
Hey Mike,
I wrote this in CMD:
Code: Select all
gcc -Wall -g -o Exit exitapp.c `pkg-config --cflags --libs gtk+-2.0` -export-dynamic
And I just get 'gcc' is not recognized as an internal or external command, operable program or batch file.

Probable because I use windows, but I don't know :?

Re: The Exit App

Posted: Thu Apr 14, 2011 6:52 pm
by mandai
Did you run the command from the directory where the GCC compiler is installed?

Re: The Exit App

Posted: Thu Apr 14, 2011 9:43 pm
by mikethedj4
Do you have MinGW, or Cgywin installed???