2
0

tut2_1.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. {
  2. This file extracted from the GTK 1.2 tutorial.
  3. Section 2.1
  4. Converted from C to Pascal by Thomas E. Payne
  5. }
  6. program tut2_1;
  7. {$mode objfpc}
  8. uses
  9. glib,gdk,gtk;
  10. procedure hello(widget : pGtkWidget ; data: pgpointer ); cdecl;
  11. begin
  12. writeln('Hello World');
  13. end;
  14. function delete_event (widget : pGtkWidget ; event: pGdkEvent; data: pgpointer ): integer; cdecl;
  15. begin
  16. writeln('Delete Event Occurred');
  17. delete_event := ord(true);
  18. end;
  19. procedure destroy(widget : pGtkWidget ; data: pgpointer ); cdecl;
  20. begin
  21. gtk_main_quit();
  22. end;
  23. var
  24. window, button : pGtkWidget;//GtkWidget is the storage type for widgets
  25. begin
  26. // This is called in all GTK applications. Arguments are parsed
  27. // from the command line and are returned to the application.
  28. gtk_init (@argc, @argv);
  29. // create a new window
  30. window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
  31. // When the window is given the "delete_event" signal (this is given
  32. // by the window manager, usually by the 'close' option, or on the
  33. // titlebar), we ask it to call the delete_event () function
  34. // as defined above. The data passed to the callback
  35. // function is NULL and is ignored in the callback function.
  36. gtk_signal_connect (pGTKOBJECT (window), 'delete_event',
  37. GTK_SIGNAL_FUNC (@delete_event), NIL);
  38. // Here we connect the "destroy" event to a signal handler.
  39. // This event occurs when we call gtk_widget_destroy() on the window,
  40. // or if we return 'FALSE' in the "delete_event" callback.
  41. gtk_signal_connect (pGTKOBJECT (window), 'destroy',
  42. GTK_SIGNAL_FUNC (@destroy), NULL);
  43. // Sets the border width of the window.
  44. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  45. // Creates a new button with the label "Hello World".
  46. button := gtk_button_new_with_label ('Hello_World');
  47. // When the button receives the "clicked" signal, it will call the
  48. // function hello() passing it NULL as its argument. The hello()
  49. // function is defined above. */
  50. gtk_signal_connect (pGTKOBJECT (button), 'clicked',
  51. GTK_SIGNAL_FUNC (@hello), NULL);
  52. // This will cause the window to be destroyed by calling
  53. // gtk_widget_destroy(window) when "clicked". Again, the destroy
  54. // signal could come from here, or the window manager
  55. gtk_signal_connect_object (pGTKOBJECT (button), 'clicked',
  56. GTK_SIGNAL_FUNC (@gtk_widget_destroy),
  57. pGTKOBJECT(window));
  58. // This packs the button into the window (a gtk container).
  59. gtk_container_add (GTK_CONTAINER (window), button);
  60. // The final step is to display this newly created widget.
  61. gtk_widget_show (button);
  62. // and the window
  63. gtk_widget_show (window);
  64. // All GTK applications must have a gtk_main(). Control ends here
  65. // and waits for an event to occur (like a key press or
  66. // mouse event).
  67. gtk_main ();
  68. end.