All Boards GTK Manually

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

All Boards GTK Manually

All Boards GTK Manually

You can create GTK applications manually—this is just like creating Graphics Java Applications. It uses a similar layout idea!

Copy this example and save as helloworld.c:

/* example-start helloworld helloworld.c */

#include <gtk/gtk.h>

/* This is a callback function. The data arguments are ignored
* in this example. More on callbacks below. */
void hello( GtkWidget *widget, gpointer   data )
{
   g_print ("Hello World\n");
}

gint delete_event( GtkWidget *widget, GdkEvent  *event,  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();
}

int main( int   argc, char *argv[] )
{
   /* GtkWidget is the storage type for widgets */
   GtkWidget *window;
   GtkWidget *button;
   
   /* This is called in all GTK applications. Arguments are parsed
    * from the command line and are returned to the application. */
   gtk_init(&argc, &argv);
   
   /* create a new window */
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   
   /* When the window is given the "delete_event" signal (this is given
    * by the window manager, usually by the "close" option, or on the
    * titlebar), we ask it to call the delete_event () function
    * as defined above. The data passed to the callback
    * function is NULL and is ignored in the callback function. */
   gtk_signal_connect (GTK_OBJECT (window), "delete_event",
                       GTK_SIGNAL_FUNC (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. */
   gtk_signal_connect (GTK_OBJECT (window), "destroy",
                       GTK_SIGNAL_FUNC (destroy), NULL);
   
   /* Sets the border width of the window. */
   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
   
   /* Creates a new button with the label "Hello World". */
   button = gtk_button_new_with_label ("Hello World");
   
   /* When the button receives the "clicked" signal, it will call the
    * function hello() passing it NULL as its argument.  The hello()
    * function is defined above. */
   gtk_signal_connect (GTK_OBJECT (button), "clicked",
                       GTK_SIGNAL_FUNC (hello), NULL);
   
   /* This will cause the window to be destroyed by calling
    * gtk_widget_destroy(window) when "clicked".  Again, the destroy
    * signal could come from here, or the window manager. */
   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
                              GTK_OBJECT (window));
   
   /* This packs the button into the window (a gtk container). */
   gtk_container_add (GTK_CONTAINER (window), button);
   
   /* The final step is to display this newly created widget. */
   gtk_widget_show (button);
   
   /* and the window */
   gtk_widget_show (window);
   
   /* All GTK applications must have a gtk_main(). Control ends here
    * and waits for an event to occur (like a key press or
    * mouse event). */
   gtk_main ();
   
   return(0);
}
/* example-end */


Comments

Hi,

What's cross compiler that need for iMX28 package?

%3CLINGO-SUB%20id%3D%22lingo-sub-1103077%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EAll%20Boards%20GTK%20Manually%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1103077%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EYou%20can%20create%20GTK%20applications%20manually%E2%80%94this%20is%20just%20like%20creating%20Graphics%20Java%20Applications.%20It%20uses%20a%20similar%20layout%20idea!%3C%2FP%3E%3CP%3ECopy%20this%20example%20and%20save%20as%20helloworld.c%3A%3C%2FP%3E%3CPRE%20class%3D%22jive-pre%22%3E%2F*%20example-start%20helloworld%20helloworld.c%20*%2F%0A%0A%23include%20%3CGTK%3E%0A%0A%2F*%20This%20is%20a%20callback%20function.%20The%20data%20arguments%20are%20ignored%0A*%20in%20this%20example.%20More%20on%20callbacks%20below.%20*%2F%0Avoid%20hello(%20GtkWidget%20*widget%2C%20gpointer%26nbsp%3B%26nbsp%3B%20data%20)%0A%7B%0A%26nbsp%3B%26nbsp%3B%20g_print%20(%22Hello%20World%5Cn%22)%3B%0A%7D%0A%0Agint%20delete_event(%20GtkWidget%20*widget%2C%20GdkEvent%26nbsp%3B%20*event%2C%26nbsp%3B%20gpointer%26nbsp%3B%26nbsp%3B%20data%20)%0A%7B%0A%26nbsp%3B%26nbsp%3B%20%2F*%20If%20you%20return%20FALSE%20in%20the%20%22delete_event%22%20signal%20handler%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20GTK%20will%20emit%20the%20%22destroy%22%20signal.%20Returning%20TRUE%20means%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20you%20don't%20want%20the%20window%20to%20be%20destroyed.%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20This%20is%20useful%20for%20popping%20up%20'are%20you%20sure%20you%20want%20to%20quit%3F'%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20type%20dialogs.%20*%2F%0A%0A%26nbsp%3B%26nbsp%3B%20g_print%20(%22delete%20event%20occurred%5Cn%22)%3B%0A%0A%26nbsp%3B%26nbsp%3B%20%2F*%20Change%20TRUE%20to%20FALSE%20and%20the%20main%20window%20will%20be%20destroyed%20with%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20a%20%22delete_event%22.%20*%2F%0A%0A%26nbsp%3B%26nbsp%3B%20return(TRUE)%3B%0A%7D%0A%0A%2F*%20Another%20callback%20*%2F%0Avoid%20destroy(%20GtkWidget%20*widget%2C%20gpointer%26nbsp%3B%26nbsp%3B%20data%20)%0A%7B%0A%26nbsp%3B%26nbsp%3B%20gtk_main_quit()%3B%0A%7D%0A%0Aint%20main(%20int%26nbsp%3B%26nbsp%3B%20argc%2C%20char%20*argv%5B%5D%20)%0A%7B%0A%26nbsp%3B%26nbsp%3B%20%2F*%20GtkWidget%20is%20the%20storage%20type%20for%20widgets%20*%2F%0A%26nbsp%3B%26nbsp%3B%20GtkWidget%20*window%3B%0A%26nbsp%3B%26nbsp%3B%20GtkWidget%20*button%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20This%20is%20called%20in%20all%20GTK%20applications.%20Arguments%20are%20parsed%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20from%20the%20command%20line%20and%20are%20returned%20to%20the%20application.%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_init(%26amp%3Bargc%2C%20%26amp%3Bargv)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20create%20a%20new%20window%20*%2F%0A%26nbsp%3B%26nbsp%3B%20window%20%3D%20gtk_window_new%20(GTK_WINDOW_TOPLEVEL)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20When%20the%20window%20is%20given%20the%20%22delete_event%22%20signal%20(this%20is%20given%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20by%20the%20window%20manager%2C%20usually%20by%20the%20%22close%22%20option%2C%20or%20on%20the%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20titlebar)%2C%20we%20ask%20it%20to%20call%20the%20delete_event%20()%20function%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20as%20defined%20above.%20The%20data%20passed%20to%20the%20callback%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20function%20is%20NULL%20and%20is%20ignored%20in%20the%20callback%20function.%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_signal_connect%20(GTK_OBJECT%20(window)%2C%20%22delete_event%22%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20GTK_SIGNAL_FUNC%20(delete_event)%2C%20NULL)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20Here%20we%20connect%20the%20%22destroy%22%20event%20to%20a%20signal%20handler.%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20This%20event%20occurs%20when%20we%20call%20gtk_widget_destroy()%20on%20the%20window%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20or%20if%20we%20return%20FALSE%20in%20the%20%22delete_event%22%20callback.%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_signal_connect%20(GTK_OBJECT%20(window)%2C%20%22destroy%22%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20GTK_SIGNAL_FUNC%20(destroy)%2C%20NULL)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20Sets%20the%20border%20width%20of%20the%20window.%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_container_set_border_width%20(GTK_CONTAINER%20(window)%2C%2010)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20Creates%20a%20new%20button%20with%20the%20label%20%22Hello%20World%22.%20*%2F%0A%26nbsp%3B%26nbsp%3B%20button%20%3D%20gtk_button_new_with_label%20(%22Hello%20World%22)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20When%20the%20button%20receives%20the%20%22clicked%22%20signal%2C%20it%20will%20call%20the%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20function%20hello()%20passing%20it%20NULL%20as%20its%20argument.%26nbsp%3B%20The%20hello()%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20function%20is%20defined%20above.%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_signal_connect%20(GTK_OBJECT%20(button)%2C%20%22clicked%22%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20GTK_SIGNAL_FUNC%20(hello)%2C%20NULL)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20This%20will%20cause%20the%20window%20to%20be%20destroyed%20by%20calling%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20gtk_widget_destroy(window)%20when%20%22clicked%22.%26nbsp%3B%20Again%2C%20the%20destroy%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20signal%20could%20come%20from%20here%2C%20or%20the%20window%20manager.%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_signal_connect_object%20(GTK_OBJECT%20(button)%2C%20%22clicked%22%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20GTK_SIGNAL_FUNC%20(gtk_widget_destroy)%2C%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20GTK_OBJECT%20(window))%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20This%20packs%20the%20button%20into%20the%20window%20(a%20gtk%20container).%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_container_add%20(GTK_CONTAINER%20(window)%2C%20button)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20The%20final%20step%20is%20to%20display%20this%20newly%20created%20widget.%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_widget_show%20(button)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20and%20the%20window%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_widget_show%20(window)%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20%2F*%20All%20GTK%20applications%20must%20have%20a%20gtk_main().%20Control%20ends%20here%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20and%20waits%20for%20an%20event%20to%20occur%20(like%20a%20key%20press%20or%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20*%20mouse%20event).%20*%2F%0A%26nbsp%3B%26nbsp%3B%20gtk_main%20()%3B%0A%26nbsp%3B%26nbsp%3B%20%0A%26nbsp%3B%26nbsp%3B%20return(0)%3B%0A%7D%0A%2F*%20example-end%20*%2F%0A%3C%2FGTK%3E%3C%2FPRE%3E%3CP%3E%3CSPAN%20class%3D%22mce_paste_marker%22%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1103078%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20All%20Boards%20GTK%20Manually%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1103078%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EWhat's%20cross%20compiler%20that%20need%20for%20iMX28%20package%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
No ratings
Version history
Last update:
‎09-10-2020 01:47 AM
Updated by: