gtk -examples
This commit is contained in:
parent
23266da4bc
commit
2fb2456d8d
|
@ -1,36 +1,20 @@
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
/* This is a callback function. The data arguments are ignored
|
/* Our new improved callback. The data passed to this function
|
||||||
* in this example. More on callbacks below. */
|
* is printed to stdout. */
|
||||||
void hello( GtkWidget *widget,
|
void callback( GtkWidget *widget,
|
||||||
gpointer data )
|
gpointer data )
|
||||||
{
|
{
|
||||||
g_print ("Hello World\n");
|
g_print ("Hello again - %s was pressed\n", (gchar *) data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* another callback */
|
||||||
gint delete_event( GtkWidget *widget,
|
gint delete_event( GtkWidget *widget,
|
||||||
GdkEvent *event,
|
GdkEvent *event,
|
||||||
gpointer data )
|
gpointer data )
|
||||||
{
|
|
||||||
/* If you return FALSE in the "delete_event" signal handler,
|
|
||||||
* GTK will emit the "destroy" signal. Returning TRUE means
|
|
||||||
* you don't want the window to be destroyed.
|
|
||||||
* This is useful for popping up 'are you sure you want to quit?'
|
|
||||||
* type dialogs. */
|
|
||||||
|
|
||||||
g_print ("delete event occurred\n");
|
|
||||||
|
|
||||||
/* Change TRUE to FALSE and the main window will be destroyed with
|
|
||||||
* a "delete_event". */
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Another callback */
|
|
||||||
void destroy( GtkWidget *widget,
|
|
||||||
gpointer data )
|
|
||||||
{
|
{
|
||||||
gtk_main_quit ();
|
gtk_main_quit ();
|
||||||
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main( int argc,
|
int main( int argc,
|
||||||
|
@ -39,59 +23,70 @@ int main( int argc,
|
||||||
/* GtkWidget is the storage type for widgets */
|
/* GtkWidget is the storage type for widgets */
|
||||||
GtkWidget *window;
|
GtkWidget *window;
|
||||||
GtkWidget *button;
|
GtkWidget *button;
|
||||||
|
GtkWidget *box1;
|
||||||
|
|
||||||
/* This is called in all GTK applications. Arguments are parsed
|
/* This is called in all GTK applications. Arguments are parsed
|
||||||
* from the command line and are returned to the application. */
|
* from the command line and are returned to the application. */
|
||||||
gtk_init (&argc, &argv);
|
gtk_init (&argc, &argv);
|
||||||
|
|
||||||
/* create a new window */
|
/* Create a new window */
|
||||||
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
||||||
|
|
||||||
/* When the window is given the "delete_event" signal (this is given
|
/* This is a new call, which just sets the title of our
|
||||||
* by the window manager, usually by the "close" option, or on the
|
* new window to "Hello Buttons!" */
|
||||||
* titlebar), we ask it to call the delete_event () function
|
gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");
|
||||||
* as defined above. The data passed to the callback
|
|
||||||
* function is NULL and is ignored in the callback function. */
|
/* Here we just set a handler for delete_event that immediately
|
||||||
|
* exits GTK. */
|
||||||
g_signal_connect (G_OBJECT (window), "delete_event",
|
g_signal_connect (G_OBJECT (window), "delete_event",
|
||||||
G_CALLBACK (delete_event), NULL);
|
G_CALLBACK (delete_event), NULL);
|
||||||
|
|
||||||
/* Here we connect the "destroy" event to a signal handler.
|
|
||||||
* This event occurs when we call gtk_widget_destroy() on the window,
|
|
||||||
* or if we return FALSE in the "delete_event" callback. */
|
|
||||||
g_signal_connect (G_OBJECT (window), "destroy",
|
|
||||||
G_CALLBACK (destroy), NULL);
|
|
||||||
|
|
||||||
/* Sets the border width of the window. */
|
/* Sets the border width of the window. */
|
||||||
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
|
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
|
||||||
|
|
||||||
/* Creates a new button with the label "Hello World". */
|
/* We create a box to pack widgets into. This is described in detail
|
||||||
button = gtk_button_new_with_label ("Hello World");
|
* in the "packing" section. The box is not really visible, it
|
||||||
|
* is just used as a tool to arrange widgets. */
|
||||||
|
box1 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
||||||
|
|
||||||
/* When the button receives the "clicked" signal, it will call the
|
/* Put the box into the main window. */
|
||||||
* function hello() passing it NULL as its argument. The hello()
|
gtk_container_add (GTK_CONTAINER (window), box1);
|
||||||
* function is defined above. */
|
|
||||||
|
/* Creates a new button with the label "Button 1". */
|
||||||
|
button = gtk_button_new_with_label ("Button 1");
|
||||||
|
|
||||||
|
/* Now when the button is clicked, we call the "callback" function
|
||||||
|
* with a pointer to "button 1" as its argument */
|
||||||
g_signal_connect (G_OBJECT (button), "clicked",
|
g_signal_connect (G_OBJECT (button), "clicked",
|
||||||
G_CALLBACK (hello), NULL);
|
G_CALLBACK (callback), (gpointer) "button 1");
|
||||||
|
|
||||||
/* This will cause the window to be destroyed by calling
|
/* Instead of gtk_container_add, we pack this button into the invisible
|
||||||
* gtk_widget_destroy(window) when "clicked". Again, the destroy
|
* box, which has been packed into the window. */
|
||||||
* signal could come from here, or the window manager. */
|
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
|
||||||
g_signal_connect_swapped (G_OBJECT (button), "clicked",
|
|
||||||
G_CALLBACK (gtk_widget_destroy),
|
|
||||||
G_OBJECT (window));
|
|
||||||
|
|
||||||
/* This packs the button into the window (a gtk container). */
|
/* Always remember this step, this tells GTK that our preparation for
|
||||||
gtk_container_add (GTK_CONTAINER (window), button);
|
* this button is complete, and it can now be displayed. */
|
||||||
|
|
||||||
/* The final step is to display this newly created widget. */
|
|
||||||
gtk_widget_show (button);
|
gtk_widget_show (button);
|
||||||
|
|
||||||
/* and the window */
|
/* Do these same steps again to create a second button */
|
||||||
|
button = gtk_button_new_with_label ("Button 2");
|
||||||
|
|
||||||
|
/* Call the same callback function with a different argument,
|
||||||
|
* passing a pointer to "button 2" instead. */
|
||||||
|
g_signal_connect (G_OBJECT (button), "clicked",
|
||||||
|
G_CALLBACK (callback), (gpointer) "button 2");
|
||||||
|
|
||||||
|
gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
|
||||||
|
|
||||||
|
/* The order in which we show the buttons is not really important, but I
|
||||||
|
* recommend showing the window last, so it all pops up at once. */
|
||||||
|
gtk_widget_show (button);
|
||||||
|
|
||||||
|
gtk_widget_show (box1);
|
||||||
|
|
||||||
gtk_widget_show (window);
|
gtk_widget_show (window);
|
||||||
|
|
||||||
/* All GTK applications must have a gtk_main(). Control ends here
|
/* Rest in gtk_main and wait for the fun to begin! */
|
||||||
* and waits for an event to occur (like a key press or
|
|
||||||
* mouse event). */
|
|
||||||
gtk_main ();
|
gtk_main ();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
gcc -Wall -g helloworld_with_box.c -o helloworld_with_box `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0` && ./helloworld_with_box
|
Loading…
Reference in New Issue