The Exit App (C)

Linux tutorials and code.
4 posts Page 1 of 1
Contributors
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

The Exit App (C)
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.
You do not have the required permissions to view the files attached to this post.
Last edited by mikethedj4 on Sat Jul 16, 2011 6:08 pm, edited 2 times in total.
User avatar
Agust1337
Coding God
Coding God
Posts: 2456
Joined: Fri Feb 19, 2010 8:18 pm

Re: The Exit App
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 :?
You're crazy!
I'm not crazy, my mother had me tested. ~Sheldon Cooper
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: The Exit App
mandai
Did you run the command from the directory where the GCC compiler is installed?
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

Re: The Exit App
mikethedj4
Do you have MinGW, or Cgywin installed???
4 posts Page 1 of 1
Return to “Programming”