helloworld2.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. program helloworld2;
  2. {$mode objfpc} {$H+}
  3. uses
  4. Glib2, Gdk2, Gtk2;
  5. (* Our new improved callback. The data passed to this function
  6. * is printed to stdout. *)
  7. procedure callback (widget : PGtkWidget;
  8. data : gpointer); cdecl;
  9. begin
  10. writeln ('Hello again - ', Pgchar (data), ' was pressed');
  11. end;
  12. (* another callback *)
  13. function delete_event (widget: PGtkWidget;
  14. event : PGdkEvent;
  15. data : gpointer): gboolean; cdecl;
  16. begin
  17. gtk_main_quit;
  18. delete_event := FALSE;
  19. end;
  20. var
  21. window,
  22. button,
  23. box1 : PGtkWidget; (* GtkWidget is the storage type for widgets *)
  24. begin
  25. (* This is called in all GTK applications. Arguments are parsed
  26. * from the command line and are returned to the application. *)
  27. gtk_init (@argc, @argv);
  28. (* Create a new window *)
  29. window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
  30. (* This is a new call, which just sets the title of our
  31. * new window to "Hello Buttons!" *)
  32. gtk_window_set_title (GTK_WINDOW (window), 'Hello Buttons!');
  33. (* Here we just set a handler for delete_event that immediately
  34. * exits GTK. *)
  35. g_signal_connect (G_OBJECT (window), 'delete_event',
  36. G_CALLBACK (@delete_event), NULL);
  37. (* Sets the border width of the window. *)
  38. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  39. (* We create a box to pack widgets into. This is described in detail
  40. * in the "packing" section. The box is not really visible, it
  41. * is just used as a tool to arrange widgets. *)
  42. box1 := gtk_hbox_new (FALSE, 0);
  43. (* Put the box into the main window. *)
  44. gtk_container_add (GTK_CONTAINER (window), box1);
  45. (* Creates a new button with the label "Button 1". *)
  46. button := gtk_button_new_with_label ('Button 1');
  47. (* Now when the button is clicked, we call the "callback" function
  48. * with a pointer to "button 1" as its argument *)
  49. g_signal_connect (G_OBJECT (button), 'clicked',
  50. G_CALLBACK (@callback), PChar('button 1'));
  51. (* Instead of gtk_container_add, we pack this button into the invisible
  52. * box, which has been packed into the window. *)
  53. gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
  54. (* Always remember this step, this tells GTK that our preparation for
  55. * this button is complete, and it can now be displayed. *)
  56. gtk_widget_show (button);
  57. (* Do these same steps again to create a second button *)
  58. button := gtk_button_new_with_label ('Button 2');
  59. (* Call the same callback function with a different argument,
  60. * passing a pointer to "button 2" instead. *)
  61. g_signal_connect (G_OBJECT (button), 'clicked',
  62. G_CALLBACK (@callback), PChar('button 2'));
  63. gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
  64. (* The order in which we show the buttons is not really important, but I
  65. * recommend showing the window last, so it all pops up at once. *)
  66. gtk_widget_show (button);
  67. gtk_widget_show (box1);
  68. gtk_widget_show (window);
  69. (* Rest in gtk_main and wait for the fun to begin! *)
  70. gtk_main ();
  71. end.