最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

gtk - How to get the color from GtkAda? - Stack Overflow

matteradmin9PV0评论

the attached code compile & runs.

$ ./xml_code Loading of XML is OK

(xml_code:31196): Gtk-CRITICAL **: 22:24:59.048: gtk_color_chooser_get_rgba: assertion 'GTK_IS_COLOR_CHOOSER (chooser)' failed Red : 0.00000000000000E+00 Green : 6.64864036484338E-310 Blue : 6.95298171953014E-310 Alpha : 6.64864036156160E-310

(xml_code:31196): Gtk-CRITICAL **: 22:25:02.163: gtk_color_chooser_get_rgba: assertion 'GTK_IS_COLOR_CHOOSER (chooser)' failed Red : 0.00000000000000E+00 Green : 6.64864036484338E-310 Blue : 6.95298171953014E-310 Alpha : 6.64864036156160E-310 ^C $

But i'm unable to retrieve the colors my user entered. In fact, I don't know how to load the value of the "Self" variable in the procedure "procedure On_Color1_Color_Set" in the window_callbacks.adb file. I don't find the correct method to load the variable. Perhaps is it an issue coming from GtkAda because the C code runs correctly, i don't know? Thank you Mark

--=- xml_code.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;
with Ada.Exceptions;

-- Application specific units
with Window_Callbacks; use Window_Callbacks;

procedure XML_Code is

  Builder         : Gtkada_Builder;
  Error           : aliased Glib.Error.GError;

  -- xml / Glade
  Mon_Interface   : Constant String := "<?xml version=""1.0"" encoding=""UTF-8""?>"
& "<!-- Generated with glade 3.40.0 -->"
& "<interface>"
& "  <requires lib=""gtk+"" version=""3.24""/>"
& "  <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 Col_1 -->"
& "      <column type=""gchararray""/>"
& "    </columns>"
& "    <data>"
& "      <row>"
& "        <col id=""0"">Ligne-1</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">bbb</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">dddd</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">eeeeeeee</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">ligne-5</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">ligne-6</col>"
& "      </row>"
& "    </data>"
& "  </object>"
& "  <object class=""GtkWindow"" id=""window"">"
& "    <property name=""name"">window</property>"
& "    <property name=""width-request"">89</property>"
& "    <property name=""height-request"">3</property>"
& "    <property name=""can-focus"">False</property>"
& "    <property name=""hexpand"">True</property>"
& "    <property name=""vexpand"">True</property>"
& "    <property name=""border-width"">0</property>"
& "    <property name=""window-position"">center</property>"
& "    <property name=""gravity"">center</property>"
& "    <child>"
& "      <object class=""GtkFixed"" id=""fixed1"">"
& "        <property name=""name"">fixed1</property>"
& "        <property name=""visible"">True</property>"
& "        <property name=""can-focus"">False</property>"
& "        <child>"
& "          <object class=""GtkColorButton"" id=""color1"">"
& "            <property name=""width-request"">100</property>"
& "            <property name=""height-request"">33</property>"
& "            <property name=""visible"">True</property>"
& "            <property name=""can-focus"">True</property>"
& "            <property name=""receives-default"">True</property>"
& "            <signal name=""color-set"" handler=""on_color1_color_set"" swapped=""no""/>"
& "          </object>"
& "          <packing>"
& "            <property name=""x"">298</property>"
& "            <property name=""y"">303</property>"
& "          </packing>"
& "        </child>"
& "      </object>"
& "    </child>"
& "  </object>"
& "</interface>";

  use type Glib.Guint;
     
  begin

    Gtk.Main.Init;

    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;
    Put_Line ("Loading of XML is OK ");

    Register_Handler (Builder, "on_window1_delete_event", On_Window1_Delete_Event'Access);

    Register_Handler (Builder, "on_color1_color_set",     On_Color1_Color_Set'Access);

    Do_Connect (Builder);

    Show_All (Gtk_Widget (Get_Object (GTK_Builder (Builder), "window")));

    Gtk.Main.Main;

    Unref (Builder);

  end XML_Code;
 

--=- window_callbacks.ads -=-
with Gtkada.Builder; use Gtkada.Builder;

package Window_Callbacks is

  function On_Window1_Delete_Event (Builder : access Gtkada_Builder_Record'Class) return Boolean;  --Ajout
  Procedure On_Color1_Color_Set    (Builder : access Gtkada_Builder_Record'Class);

end Window_Callbacks;


--=- window_callbacks.adb --=--    
-- units from GtkAda
with Gtk.Main;

with Gtk.Color_Chooser;           use Gtk.Color_Chooser;
with Gdk.RGBA;                    use Gdk.RGBA;

-- units from Glib
with Glib;              use Glib;

-- Ada predefined units
with Ada.Text_IO;       use Ada.Text_IO;


package body Window_Callbacks  is

  -----------------------------------------------
  -- On_Window1_Delete_Event
  -----------------------------------------------
  function On_Window1_Delete_Event 
                 (Builder : access Gtkada_Builder_Record'Class) return Boolean is
    pragma Unreferenced (Builder); -- C'est sciemment que Builder n'est pas utilisé dans la fonction
    begin
      Gtk.Main.Main_Quit;
      return False;
    end On_Window1_Delete_Event;

  ---------------------------------
  -- On_Color1_Color_Set  --
  ---------------------------------
  procedure On_Color1_Color_Set (Builder : access Gtkada_Builder_Record'Class) is
    pragma Unreferenced (Builder);

    Self  : Gtk_Color_Chooser;
    Color : Gdk_RGBA;
    begin
    
      Gtk.Color_Chooser.Get_RGBA (Self, Color);
    
      put_line ("Red : "   &  Gdouble'Image (Color.Red));
      put_line ("Green : " &  Gdouble'Image (Color.Green));     
      put_line ("Blue : "  &  Gdouble'Image (Color.Blue));    
      put_line ("Alpha : " &  Gdouble'Image (Color.Alpha));
    
    end On_Color1_Color_Set;

end Window_Callbacks;

the attached code compile & runs.

$ ./xml_code Loading of XML is OK

(xml_code:31196): Gtk-CRITICAL **: 22:24:59.048: gtk_color_chooser_get_rgba: assertion 'GTK_IS_COLOR_CHOOSER (chooser)' failed Red : 0.00000000000000E+00 Green : 6.64864036484338E-310 Blue : 6.95298171953014E-310 Alpha : 6.64864036156160E-310

(xml_code:31196): Gtk-CRITICAL **: 22:25:02.163: gtk_color_chooser_get_rgba: assertion 'GTK_IS_COLOR_CHOOSER (chooser)' failed Red : 0.00000000000000E+00 Green : 6.64864036484338E-310 Blue : 6.95298171953014E-310 Alpha : 6.64864036156160E-310 ^C $

But i'm unable to retrieve the colors my user entered. In fact, I don't know how to load the value of the "Self" variable in the procedure "procedure On_Color1_Color_Set" in the window_callbacks.adb file. I don't find the correct method to load the variable. Perhaps is it an issue coming from GtkAda because the C code runs correctly, i don't know? Thank you Mark

--=- xml_code.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;
with Ada.Exceptions;

-- Application specific units
with Window_Callbacks; use Window_Callbacks;

procedure XML_Code is

  Builder         : Gtkada_Builder;
  Error           : aliased Glib.Error.GError;

  -- xml / Glade
  Mon_Interface   : Constant String := "<?xml version=""1.0"" encoding=""UTF-8""?>"
& "<!-- Generated with glade 3.40.0 -->"
& "<interface>"
& "  <requires lib=""gtk+"" version=""3.24""/>"
& "  <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 Col_1 -->"
& "      <column type=""gchararray""/>"
& "    </columns>"
& "    <data>"
& "      <row>"
& "        <col id=""0"">Ligne-1</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">bbb</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">dddd</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">eeeeeeee</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">ligne-5</col>"
& "      </row>"
& "      <row>"
& "        <col id=""0"">ligne-6</col>"
& "      </row>"
& "    </data>"
& "  </object>"
& "  <object class=""GtkWindow"" id=""window"">"
& "    <property name=""name"">window</property>"
& "    <property name=""width-request"">89</property>"
& "    <property name=""height-request"">3</property>"
& "    <property name=""can-focus"">False</property>"
& "    <property name=""hexpand"">True</property>"
& "    <property name=""vexpand"">True</property>"
& "    <property name=""border-width"">0</property>"
& "    <property name=""window-position"">center</property>"
& "    <property name=""gravity"">center</property>"
& "    <child>"
& "      <object class=""GtkFixed"" id=""fixed1"">"
& "        <property name=""name"">fixed1</property>"
& "        <property name=""visible"">True</property>"
& "        <property name=""can-focus"">False</property>"
& "        <child>"
& "          <object class=""GtkColorButton"" id=""color1"">"
& "            <property name=""width-request"">100</property>"
& "            <property name=""height-request"">33</property>"
& "            <property name=""visible"">True</property>"
& "            <property name=""can-focus"">True</property>"
& "            <property name=""receives-default"">True</property>"
& "            <signal name=""color-set"" handler=""on_color1_color_set"" swapped=""no""/>"
& "          </object>"
& "          <packing>"
& "            <property name=""x"">298</property>"
& "            <property name=""y"">303</property>"
& "          </packing>"
& "        </child>"
& "      </object>"
& "    </child>"
& "  </object>"
& "</interface>";

  use type Glib.Guint;
     
  begin

    Gtk.Main.Init;

    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;
    Put_Line ("Loading of XML is OK ");

    Register_Handler (Builder, "on_window1_delete_event", On_Window1_Delete_Event'Access);

    Register_Handler (Builder, "on_color1_color_set",     On_Color1_Color_Set'Access);

    Do_Connect (Builder);

    Show_All (Gtk_Widget (Get_Object (GTK_Builder (Builder), "window")));

    Gtk.Main.Main;

    Unref (Builder);

  end XML_Code;
 

--=- window_callbacks.ads -=-
with Gtkada.Builder; use Gtkada.Builder;

package Window_Callbacks is

  function On_Window1_Delete_Event (Builder : access Gtkada_Builder_Record'Class) return Boolean;  --Ajout
  Procedure On_Color1_Color_Set    (Builder : access Gtkada_Builder_Record'Class);

end Window_Callbacks;


--=- window_callbacks.adb --=--    
-- units from GtkAda
with Gtk.Main;

with Gtk.Color_Chooser;           use Gtk.Color_Chooser;
with Gdk.RGBA;                    use Gdk.RGBA;

-- units from Glib
with Glib;              use Glib;

-- Ada predefined units
with Ada.Text_IO;       use Ada.Text_IO;


package body Window_Callbacks  is

  -----------------------------------------------
  -- On_Window1_Delete_Event
  -----------------------------------------------
  function On_Window1_Delete_Event 
                 (Builder : access Gtkada_Builder_Record'Class) return Boolean is
    pragma Unreferenced (Builder); -- C'est sciemment que Builder n'est pas utilisé dans la fonction
    begin
      Gtk.Main.Main_Quit;
      return False;
    end On_Window1_Delete_Event;

  ---------------------------------
  -- On_Color1_Color_Set  --
  ---------------------------------
  procedure On_Color1_Color_Set (Builder : access Gtkada_Builder_Record'Class) is
    pragma Unreferenced (Builder);

    Self  : Gtk_Color_Chooser;
    Color : Gdk_RGBA;
    begin
    
      Gtk.Color_Chooser.Get_RGBA (Self, Color);
    
      put_line ("Red : "   &  Gdouble'Image (Color.Red));
      put_line ("Green : " &  Gdouble'Image (Color.Green));     
      put_line ("Blue : "  &  Gdouble'Image (Color.Blue));    
      put_line ("Alpha : " &  Gdouble'Image (Color.Alpha));
    
    end On_Color1_Color_Set;

end Window_Callbacks;
Share Improve this question asked Nov 16, 2024 at 21:41 MarkMark 696 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

The problem is that in On_Color1_Color_Set, self is just a null value. The Gtk_Color_Chooser has already been closed so there is no way of accessing it. The only thing you can play with is the button Color1.

First, add the declaration of Color_Button to the with statements

with Gtk.Color_Button; use Gtk.Color_Button;

The Gtk.Color_Chooser can be removed. In the callback

procedure On_Color1_Color_Set(builder: access Gtkada_Builder_Record'Class) is
   self: Gtk_Color_Button;
   rgba: Gdk_RGBA;
begin
   -- Get the color button
   self := Gtk_Color_Button(builder.Get_Object("color1"));
   -- Get the RGBA value from the button
   Gtk.Color_Button.Get_RGBA(self, rgba);

   -- Convert RGBA to a string
   declare
       color: constant String := To_String(rgba);
   begin
       Put_Line(color);

       -- RGB values in floating point format
       Put_Line("Red   " & rgba.red'image);
       Put_Line("Green " & rgba.green'image);
       Put_Line("Blue  " & rgba.blue'image);
       Put_Line("Alpha " & rgba.alpha'image);
   end;
end On_Color1_Color_Set;

Choosing one of the colors, you get

Loading of XML is OK 
rgb(129,61,156)
Red    5.05882352941176E-01
Green  2.39215686274510E-01
Blue   6.11764705882353E-01
Alpha  1.00000000000000E+00

I find the integers easier to use than the floats.

Post a comment

comment list (0)

  1. No comments so far