tut4_5.pp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. {
  2. This file extracted from the GTK 1.2 tutorial.
  3. Section 4.5
  4. Converted from C to Pascal by Thomas E. Payne
  5. }
  6. program Tut4_5;
  7. {$mode objfpc}
  8. uses
  9. glib,gdk,gtk,sysutils;
  10. //* Our callback.
  11. //* The data passed to this function is printed to stdout */
  12. procedure callback(widget : pGtkWidget ; data: pgpointer ); cdecl;
  13. begin
  14. writeln('Hello again - '+pchar(data)+' was pressed');
  15. end;
  16. //* This callback quits the program */
  17. function delete_event (widget : pGtkWidget ; event: pGdkEvent; data: pgpointer ): integer; cdecl;
  18. begin
  19. gtk_main_quit();
  20. delete_event:=0;
  21. end;
  22. var
  23. window, button, table :pGtkWidget;
  24. begin
  25. gtk_init (@argc, @argv);
  26. //* Create a new window */
  27. window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
  28. //* Set the window title */
  29. gtk_window_set_title (GTK_WINDOW (window), 'Table');
  30. //* Set a handler for delete_event that immediately
  31. //* exits GTK. */
  32. gtk_signal_connect (GTK_OBJECT (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), 20);
  36. //* Create a 2x2 table */
  37. table := gtk_table_new (2, 2, TRUE);
  38. //* Put the table in the main window */
  39. gtk_container_add (GTK_CONTAINER (window), table);
  40. //* Create first button */
  41. button := gtk_button_new_with_label ('button 1');
  42. //* When the button is clicked, we call the "callback" function
  43. //* with a pointer to "button 1" as its argument */
  44. gtk_signal_connect (GTK_OBJECT (button), 'clicked',
  45. GTK_SIGNAL_FUNC (@callback), pchar('button 1'));
  46. //* Insert button 1 into the upper left quadrant of the table */
  47. gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 1, 0, 1);
  48. gtk_widget_show (button);
  49. //* Create second button */
  50. button := gtk_button_new_with_label ('button 2');
  51. //* When the button is clicked, we call the "callback" function
  52. //* with a pointer to "button 2" as its argument */
  53. gtk_signal_connect (GTK_OBJECT (button), 'clicked',
  54. GTK_SIGNAL_FUNC (@callback), pchar('button 2'));
  55. //* Insert button 2 into the upper right quadrant of the table */
  56. gtk_table_attach_defaults (GTK_TABLE(table), button, 1, 2, 0, 1);
  57. gtk_widget_show (button);
  58. //* Create "Quit" button */
  59. button := gtk_button_new_with_label ('Quit');
  60. //* When the button is clicked, we call the "delete_event" function
  61. //* and the program exits */
  62. gtk_signal_connect (GTK_OBJECT (button), 'clicked',
  63. GTK_SIGNAL_FUNC (@delete_event), NIL);
  64. //* Insert the quit button into the both
  65. //* lower quadrants of the table */
  66. gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 2, 1, 2);
  67. gtk_widget_show (button);
  68. gtk_widget_show (table);
  69. gtk_widget_show (window);
  70. gtk_main ();
  71. end.