simple.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. program simple_filechooser;
  2. (* basing upon helloworld2 example*)
  3. {$mode objfpc} {$H+}
  4. {$IFDEF GTK2_0}{$FATAL this demo needs gtk 2.4}{$ENDIF}
  5. {$IFDEF GTK2_2}{$FATAL this demo needs gtk 2.4}{$ENDIF}
  6. uses
  7. Glib2, Gdk2, Gtk2;
  8. const
  9. ACTION_OPEN = 1;
  10. ACTION_SAVE = 2;
  11. MAIN_WINDOW_KEY = 'main_window'; { uses with g_object_(get/set)_data as key }
  12. (* File dialog-callback. *)
  13. procedure dialog_callback (widget : PGtkWidget;
  14. data : gpointer); cdecl;
  15. var
  16. dialog : PGtkWidget;
  17. window : PGtkWindow;
  18. action : gint;
  19. filename : Pgchar;
  20. begin
  21. { Get a pointer to the main window }
  22. window := g_object_get_data (G_OBJECT(widget), MAIN_WINDOW_KEY);
  23. action := gint (data);
  24. case action of
  25. ACTION_OPEN:
  26. begin
  27. dialog := gtk_file_chooser_dialog_new ('Open File',
  28. window,
  29. GTK_FILE_CHOOSER_ACTION_OPEN,
  30. GTK_STOCK_OPEN, [GTK_RESPONSE_ACCEPT,
  31. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
  32. NULL]);
  33. end; { ACTION_OPEN}
  34. ACTION_SAVE:
  35. begin
  36. dialog := gtk_file_chooser_dialog_new ('Save File',
  37. window,
  38. GTK_FILE_CHOOSER_ACTION_SAVE,
  39. GTK_STOCK_SAVE, [GTK_RESPONSE_ACCEPT,
  40. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
  41. NULL]);
  42. end; { ACTION_SAVE }
  43. else begin
  44. { This should never happen }
  45. g_print ('Something is wrong here!!!.'#13#10);
  46. g_print ('No dialog created.'#13#10);
  47. {writeln crashes on my system running linux --- check why }
  48. exit;
  49. end;
  50. end; { case }
  51. if gtk_dialog_run (GTK_DIALOG (dialog)) = GTK_RESPONSE_ACCEPT then
  52. begin
  53. filename := gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
  54. g_print ('Filename %s selected.'#13#10, [filename]);
  55. // writeln ('File ', filename, ' selected.');
  56. // ToDO:
  57. // writeln crashes... check why
  58. g_free (filename);
  59. end;
  60. gtk_widget_destroy (dialog);
  61. end;
  62. (* another callback *)
  63. function delete_event (widget: PGtkWidget;
  64. event : PGdkEvent;
  65. data : gpointer): gboolean;cdecl;
  66. begin
  67. gtk_main_quit;
  68. delete_event := FALSE;
  69. end;
  70. var
  71. window,
  72. button,
  73. box1 : PGtkWidget; (* GtkWidget is the storage type for widgets *)
  74. begin
  75. (* This is called in all GTK applications. Arguments are parsed
  76. * from the command line and are returned to the application. *)
  77. gtk_init (@argc, @argv);
  78. (* Create a new window *)
  79. window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
  80. (* This is a new call, which just sets the title of our
  81. * new window to "Hello Buttons!" *)
  82. gtk_window_set_title (GTK_WINDOW (window), 'GtkFileChooser Demo');
  83. (* Here we just set a handler for delete_event that immediately
  84. * exits GTK. *)
  85. g_signal_connect (G_OBJECT (window), 'delete_event',
  86. G_CALLBACK (@delete_event), NULL);
  87. (* Sets the border width of the window. *)
  88. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  89. (* We create a box to pack widgets into. This is described in detail
  90. * in the "packing" section. The box is not really visible, it
  91. * is just used as a tool to arrange widgets. *)
  92. box1 := gtk_hbox_new (FALSE, 0);
  93. (* Put the box into the main window. *)
  94. gtk_container_add (GTK_CONTAINER (window), box1);
  95. button := gtk_button_new_with_label ('Open');
  96. (* Now when the button is clicked, we call the "callback" function
  97. * with a pointer to the main window as its argument *)
  98. g_object_set_data (G_OBJECT(button), MAIN_WINDOW_KEY, window);
  99. g_signal_connect (G_OBJECT (button), 'clicked',
  100. G_CALLBACK (@dialog_callback), pointer(ACTION_OPEN));
  101. (* Instead of gtk_container_add, we pack this button into the invisible
  102. * box, which has been packed into the window. *)
  103. gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
  104. (* Always remember this step, this tells GTK that our preparation for
  105. * this button is complete, and it can now be displayed. *)
  106. gtk_widget_show (button);
  107. (* Do these same steps again to create a second button *)
  108. button := gtk_button_new_with_label ('Save');
  109. g_object_set_data (G_OBJECT(button), MAIN_WINDOW_KEY, window);
  110. g_signal_connect (G_OBJECT (button), 'clicked',
  111. G_CALLBACK (@dialog_callback), pointer(ACTION_SAVE));
  112. gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
  113. (* The order in which we show the buttons is not really important, but I
  114. * recommend showing the window last, so it all pops up at once. *)
  115. gtk_widget_show (button);
  116. gtk_widget_show (box1);
  117. gtk_widget_show (window);
  118. (* Rest in gtk_main and wait for the fun to begin! *)
  119. gtk_main ();
  120. end.