2
0

tut3_3.pp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. {
  2. This file extracted from the GTK 1.2 tutorial.
  3. Section 3.3
  4. Converted from C to Pascal by Thomas E. Payne
  5. }
  6. program tut3_3;
  7. {$mode objfpc}
  8. uses
  9. glib,gdk,gtk,sysutils;
  10. procedure callback(widget : pGtkWidget ; data: pgpointer ); cdecl;
  11. begin
  12. writeln('Hello again - '+pchar(data)+' was pressed');
  13. end;
  14. function delete_event (widget : pGtkWidget ; event: pGdkEvent; data: pgpointer ): integer; cdecl;
  15. begin
  16. gtk_main_quit();
  17. delete_event:=0;
  18. end;
  19. var
  20. window, button, box1 : pGtkWidget;//GtkWidget is the storage type for widgets
  21. begin
  22. // This is called in all GTK applications. Arguments are parsed
  23. // from the command line and are returned to the application.
  24. gtk_init (@argc, @argv);
  25. // create a new window
  26. window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
  27. // This is a new call, this just sets the title of our
  28. // new window to "Hello Buttons!" */
  29. gtk_window_set_title (GTK_WINDOW (window), 'Hello Buttons!');
  30. // Here we just set a handler for delete_event that immediately
  31. // exits GTK. */
  32. gtk_signal_connect (pGTKOBJECT (window), 'delete_event',
  33. GTK_SIGNAL_FUNC (@delete_event), NIL);
  34. // Sets the border width of the window.
  35. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  36. // We create a box to pack widgets into. This is described in detail
  37. // in the "packing" section. The box is not really visible, it
  38. // is just used as a tool to arrange widgets. */
  39. box1 := gtk_hbox_new(FALSE, 0);
  40. // Put the box into the main window. */
  41. gtk_container_add (GTK_CONTAINER (window), box1);
  42. // Creates a new button with the label "Button 1".
  43. button := gtk_button_new_with_label ('Button 1');
  44. // Now when the button is clicked, we call the "callback" function
  45. // with a pointer to "button 1" as its argument */
  46. gtk_signal_connect (pGTKOBJECT (button), 'clicked',
  47. GTK_SIGNAL_FUNC (@callback), pchar('Button 1'));
  48. // Instead of gtk_container_add, we pack this button into the invisible
  49. // box, which has been packed into the window. */
  50. gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
  51. // Always remember this step, this tells GTK that our preparation for
  52. // this button is complete, and it can now be displayed. */
  53. gtk_widget_show (button);
  54. // Do these same steps again to create a second button */
  55. button := gtk_button_new_with_label ('Button 2');
  56. // Call the same callback function with a different argument,
  57. // passing a pointer to "button 2" instead. */
  58. gtk_signal_connect (GTK_OBJECT (button), 'clicked',
  59. GTK_SIGNAL_FUNC (@callback), pchar('Button 2'));
  60. gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
  61. // The order in which we show the buttons is not really important, but I
  62. // recommend showing the window last, so it all pops up at once. */
  63. gtk_widget_show(button);
  64. gtk_widget_show(box1);
  65. // and the window
  66. gtk_widget_show (window);
  67. // All GTK applications must have a gtk_main(). Control ends here
  68. // and waits for an event to occur (like a key press or
  69. // mouse event).
  70. gtk_main ();
  71. end.