所附代码编译并运行。
-我应该想要检索和操作 GtkAda 小部件的文件名。 我确实认为当我们直接管理对象时这是可能的(如何使用 GtkAda 创建文件选择器?)。 但我无法做同样的事情,这意味着从 xml 文件内部获取 GtkFileChooserButton 的文件名。
-您可以帮助并解释一下您是如何做的吗,因为即使使用 GnatStudio 及其方法,我也不知道该怎么做。
谢谢马克
-- window_callbacks.ads
with Gtkada.Builder; use Gtkada.Builder;
package Window_Callbacks is
procedure On_File1_File_Set (Builder : access Gtkada_Builder_Record'Class);
end Window_Callbacks;
-- window_callbacks.adb
-- units from GtkAda
with Gtk.Main;
-- units from GtkAda
with Gtk.File_Chooser; use Gtk.File_Chooser;
with Gtk.File_Chooser_Button; use Gtk.File_Chooser_Button;
with Gtkada.File_Selection; use Gtkada.File_Selection;
-- units from Glib
with Glib; use Glib;
with Glib.Object; use Glib.Object;
-- Ada predefined units
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Exceptions;
package body Window_Callbacks is
---------------------------------
-- On_File1_File_Set --
---------------------------------
procedure On_File1_File_Set (Builder : access Gtkada_Builder_Record'Class) is
Button : access Gtk_File_Chooser_Button_Record;
begin
null;
--Button := Gtk.File_Button.Get_File_Name ( XXX, "file1"); --something like this..
--Prints the file_name of the selection box.
--Ada.Text_IO.Put_Line ("File selected " & Gtk.File_Chooser_Button.Get_File__Name (Button));
end On_File1_File_Set;
end Window_Callbacks;
-- glade_8.adb
-- units from Gtk
with Gtk.Main;
with Glib.Error; use Glib.Error;
with Gtk.Widget; use Gtk.Widget;
with Gtk.Builder; use Gtk.Builder;
with Gtkada.Builder; use Gtkada.Builder;
-- Ada predefined units
with Ada.Text_IO; use Ada.Text_IO;
-- Application specific units
with Window_Callbacks; use Window_Callbacks;
procedure Glade_8 is
Mon_Interface : Constant String :=
"<?xml version=""1.0"" encoding=""UTF-8""?>"
& "<!-- Generated with glade 3.40.0 -->"
& "<interface>"
& " <requires lib=""gtk+"" version=""3.20""/>"
& " <object class=""GtkAdjustment"" id=""adjustment1"">"
& " <property name=""upper"">100</property>"
& " <property name=""step-increment"">1</property>"
& " <property name=""page-increment"">10</property>"
& " </object>"
& " <object class=""GtkListStore"" id=""liststore1"">"
& " <columns>"
& " <!-- column-name gchararray1 -->"
& " <column type=""gchararray""/>"
& " </columns>"
& " <data>"
& " <row>"
& " <col id=""0"" translatable=""yes"">test1</col>"
& " </row>"
& " <row>"
& " <col id=""0"" translatable=""yes"">test2</col>"
& " </row>"
& " <row>"
& " <col id=""0"" translatable=""yes"">test3</col>"
& " </row>"
& " </data>"
& " </object>"
& " <object class=""GtkWindow"" id=""window"">"
& " <property name=""can-focus"">False</property>"
& " <child>"
& " <object class=""GtkFixed"" id=""fixed1"">"
& " <property name=""visible"">True</property>"
& " <property name=""can-focus"">False</property>"
& " <child>"
& " <object class=""GtkFileChooserButton"" id=""file1"">"
& " <property name=""width-request"">196</property>"
& " <property name=""visible"">True</property>"
& " <property name=""can-focus"">False</property>"
& " <property name=""title"" translatable=""yes""/>"
& " <signal name=""file-set"" handler=""on_file1_file_set"" swapped=""no""/>"
& " </object>"
& " <packing>"
& " <property name=""x"">9</property>"
& " <property name=""y"">234</property>"
& " </packing>"
& " </child>"
& " </object>"
& " </child>"
& " </object>"
& "</interface>";
Builder : Gtkada_Builder;
Error : aliased Glib.Error.GError;
use type Glib.Guint;
begin
Gtk.Main.Init;
-- Etape 1 : créer un Builder
Gtk_New (Builder);
if Add_From_String (Gtk_Builder(Builder), Mon_Interface, Error'Access) = 0 then
Put_Line ("Error : " & Get_Message (Error));
Error_Free (Error);
return;
end if;
-- Etape 2 : créer les handlers des events
Register_Handler (Builder, "on_file1_file_set", On_File1_File_Set'Access);
-- Etape 3 : Do_Connect connecte tous les handlers enregistrés en une fois.
Do_Connect (Builder);
-- Etape 4 : Afficher la fenetre avec ses dépendances
Show_All (Gtk_Widget (Get_Object (GTK_Builder (Builder), "window")));
-- Etape 5 : Lancer la boucle infinie.
Gtk.Main.Main;
-- Etape 6 : Unref pour libérer la memoire associée au Builder.
Unref (Builder);
end Glade_8;
您需要获取Gtk_File_Chooser_Button,然后从按钮中获取文件名。 在window_callback.adb中
procedure On_File1_File_Set (Builder : access Gtkada_Builder_Record'Class) is
Button : access Gtk_File_Chooser_Button_Record;
begin
-- Get the file chooser button
Button := Gtk_File_Chooser_Button(Get_Object(Builder, "file1"));
-- Get the filename
declare
filename: string := Get_FileName(Button);
begin
Ada.Text_IO.Put_Line ("File selected " & filename);
end;
end On_File1_File_Set;
失踪 当您关闭对话框时,X 按钮还需要一个回调处理程序,否则它永远不会关闭并保留在后台。