The Exit App (C)
Posted: Mon Apr 11, 2011 3:47 am
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.
Now lets add the following codes under gtk_container_set_border_width (GTK_CONTAINER (window), 10);.
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);
Now our full code for our program should look like this.
Compile:
Now we need to open our Terminal, and navigate to the desktop directory.
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
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.#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;
}
Now lets add the following codes under gtk_container_set_border_width (GTK_CONTAINER (window), 10);.
Code: Select all
The line on the top tells us the button's gonna say "Exit!".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 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
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);.
gtk_widget_show (button);
Code: Select all
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.gtk_container_set_border_width (GTK_CONTAINER (window), 10);
Now our full code for our program should look like this.
Code: Select all
Now lets save this file to our desktop as exitapp.c.#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;
}
Compile:
Now we need to open our Terminal, and navigate to the desktop directory.
Code: Select all
Now compile the program we just created with the following code.
cd Desktop
Code: Select all
You should get no errors, and your application should look like this.
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.gcc -Wall -g -o Exit exitapp.c `pkg-config --cflags --libs gtk+-2.0` -export-dynamic