tut3_3.pp 3.1 KB

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