helloworld.pas 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. program HelloWorld;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Glib2, Gdk2, Gtk2;
  5. (* This is a callback function. The data arguments are ignored
  6. * in this example. More on callbacks below. *)
  7. procedure hello(Widget: PGtkWidget; Data: gpointer); cdecl;
  8. begin
  9. g_print ('Hello World'#13#10);
  10. end;
  11. function delete_event( Widget: PGtkWidget; Event: PGdkEvent; Data: gpointer):gint; cdecl;
  12. begin
  13. (* If you return FALSE in the "delete_event" signal handler,
  14. * GTK will emit the "destroy" signal. Returning TRUE means
  15. * you don't want the window to be destroyed.
  16. * This is useful for popping up 'are you sure you want to quit?'
  17. * type dialogs. *)
  18. g_print ('delete event occurred'#13#10);
  19. (* Change TRUE to FALSE and the main window will be destroyed with
  20. * a "delete_event". *)
  21. Result:=gTRUE;
  22. end;
  23. (* Another callback *)
  24. procedure destroy(Widget: PGtkWidget; Data: gpointer); cdecl;
  25. begin
  26. gtk_main_quit;
  27. end;
  28. var
  29. (* GtkWidget is the storage type for widgets *)
  30. Window: PGtkWidget;
  31. Button: PGtkWidget;
  32. begin
  33. (* This is called in all GTK applications. Arguments are parsed
  34. * from the command line and are returned to the application. *)
  35. gtk_init (@argc, @argv);
  36. (* create a new window *)
  37. window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
  38. (* When the window is given the "delete_event" signal (this is given
  39. * by the window manager, usually by the "close" option, or on the
  40. * titlebar), we ask it to call the delete_event () function
  41. * as defined above. The data passed to the callback
  42. * function is NULL and is ignored in the callback function. *)
  43. g_signal_connect (G_OBJECT (window), 'delete_event',
  44. G_CALLBACK (@delete_event), NULL);
  45. (* Here we connect the "destroy" event to a signal handler.
  46. * This event occurs when we call gtk_widget_destroy() on the window,
  47. * or if we return FALSE in the "delete_event" callback. *)
  48. g_signal_connect (G_OBJECT (window), 'destroy',
  49. G_CALLBACK (@destroy), NULL);
  50. (* Sets the border width of the window. *)
  51. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  52. (* Creates a new button with the label "Hello World". *)
  53. button := gtk_button_new_with_label ('Hello World');
  54. (* When the button receives the "clicked" signal, it will call the
  55. * function hello() passing it NULL as its argument. The hello()
  56. * function is defined above. *)
  57. g_signal_connect (G_OBJECT (button), 'clicked',
  58. G_CALLBACK (@hello), NULL);
  59. (* This will cause the window to be destroyed by calling
  60. * gtk_widget_destroy(window) when "clicked". Again, the destroy
  61. * signal could come from here, or the window manager. *)
  62. g_signal_connect_swapped (G_OBJECT (button), 'clicked',
  63. G_CALLBACK (@gtk_widget_destroy), window);
  64. (* This packs the button into the window (a gtk container). *)
  65. gtk_container_add (GTK_CONTAINER (window), button);
  66. (* The final step is to display this newly created widget. *)
  67. gtk_widget_show (button);
  68. (* and the window *)
  69. gtk_widget_show (window);
  70. (* All GTK applications must have a gtk_main(). Control ends here
  71. * and waits for an event to occur (like a key press or
  72. * mouse event). *)
  73. gtk_main ();
  74. end.