Skip to content

Commit fdaff6c

Browse files
authored
Added Image Widget
Corrected errors in Context_2D Improved displaying and retrieving Image_Data
1 parent 6344a71 commit fdaff6c

File tree

3 files changed

+100
-30
lines changed

3 files changed

+100
-30
lines changed

ada_gui-gnoga-gui-element-canvas-context_2d.adb

+16-9
Original file line numberDiff line numberDiff line change
@@ -956,10 +956,13 @@ package body Ada_GUI.Gnoga.Gui.Element.Canvas.Context_2D is
956956
is
957957
use Ada.Strings.Fixed;
958958

959-
D : Gnoga.Pixel_Data_Type (1 .. Width, 1 .. Height);
960-
961-
S : Integer := Value'First;
962-
F : Integer := Value'First - 1;
959+
D : Gnoga.Pixel_Data_Type (1 .. Width, 1 .. Height);
960+
S : Integer;
961+
F : Integer := Value'First - 1;
962+
Red : Color_Type;
963+
Green : Color_Type;
964+
Blue : Color_Type;
965+
Alpha : Color_Type;
963966

964967
function Split return Color_Type;
965968
-- Split string and extract values
@@ -979,9 +982,13 @@ package body Ada_GUI.Gnoga.Gui.Element.Canvas.Context_2D is
979982
return Color_Type'Value (Value (S .. F - 1));
980983
end Split;
981984
begin
982-
for X in 1 .. Width loop
983-
for Y in 1 .. Height loop
984-
D (X, Y) := (Split, Split, Split, Split);
985+
for Y in 1 .. Height loop
986+
for X in 1 .. Width loop
987+
Red := Split;
988+
Green := Split;
989+
Blue := Split;
990+
Alpha := Split;
991+
D (X, Y) := (Red, Green, Blue, Alpha);
985992
end loop;
986993
end loop;
987994

@@ -1115,8 +1122,8 @@ package body Ada_GUI.Gnoga.Gui.Element.Canvas.Context_2D is
11151122
S : String (1 .. 16 * Value'Length (1) * Value'Length (2));
11161123
P : Positive := 1;
11171124
begin
1118-
for X in 1 .. Value'Length (1) loop
1119-
for Y in 1 .. Value'Length (2) loop
1125+
for Y in 1 .. Value'Length (2) loop
1126+
for X in 1 .. Value'Length (1) loop
11201127
declare
11211128
T : constant String :=
11221129
Gnoga.Left_Trim (Value (X, Y).Red'Img) & C &

ada_gui.adb

+67-16
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ package body Ada_GUI is
109109
Context : Ada_GUI.Gnoga.Gui.Element.Canvas.Context_2D.Context_2D_Access;
110110
Width : Positive;
111111
Height : Positive;
112+
when Image =>
113+
Img : Gnoga.Gui.Element.Common.IMG_Access;
112114
when Password_Box =>
113115
Password : Gnoga.Gui.Element.Form.Password_Access;
114116
Password_Label : Gnoga.Gui.Element.Form.Label_Access;
@@ -250,6 +252,24 @@ package body Ada_GUI is
250252
return ID;
251253
end New_Graphic_Area;
252254

255+
function New_Image (Row : Positive := 1;
256+
Column : Positive := 1;
257+
Source : String := "";
258+
Description : String := "";
259+
Break_Before : Boolean := False)
260+
return Widget_ID is
261+
ID : constant Widget_ID := (Value => Widget_List.Last_Index + 1);
262+
263+
Widget : Widget_Info (Kind => Image);
264+
begin -- New_Image
265+
Break (Desired => Break_Before, Row => Row, Column => Adjusted (Row, Column) );
266+
Widget.Img := new Gnoga.Gui.Element.Common.IMG_Type;
267+
Widget.Img.Create (Parent => Form (Row, Adjusted (Row, Column) ), URl_Source => Source, Alternative_Text => Description);
268+
Widget_List.Append (New_Item => Widget);
269+
270+
return ID;
271+
end New_Image;
272+
253273
function New_Password_Box (Row : Positive := 1;
254274
Column : Positive := 1;
255275
Text : String := "";
@@ -447,6 +467,8 @@ package body Ada_GUI is
447467
Widget.Check_Label.Hidden (Value => Hidden);
448468
when Graphic_Area =>
449469
Widget.Canvas.Hidden (Value => Hidden);
470+
when Image =>
471+
Widget.Img.Hidden (Value => Hidden);
450472
when Password_Box =>
451473
Widget.Password.Hidden (Value => Hidden);
452474
Widget.Password_Label.Hidden (Value => Hidden);
@@ -482,6 +504,8 @@ package body Ada_GUI is
482504
Widget.Check_Label.Visible (Value => Visible);
483505
when Graphic_Area =>
484506
Widget.Canvas.Visible (Value => Visible);
507+
when Image =>
508+
Widget.Img.Visible (Value => Visible);
485509
when Password_Box =>
486510
Widget.Password.Visible (Value => Visible);
487511
Widget.Password_Label.Visible (Value => Visible);
@@ -526,7 +550,11 @@ package body Ada_GUI is
526550
procedure Set_Source (ID : in Widget_ID; Source : in String) is
527551
Widget : Widget_Info := Widget_List.Element (ID.Value);
528552
begin -- Set_Source
529-
Widget.Audio.Media_Source (Source => Source);
553+
if ID.Kind = Audio_Player then
554+
Widget.Audio.Media_Source (Source => Source);
555+
else
556+
Widget.Img.URL_Source (Value => Source);
557+
end if;
530558
end Set_Source;
531559

532560
function Source (ID : Widget_ID) return String is
@@ -763,6 +791,8 @@ package body Ada_GUI is
763791
Widget.Check_Label.Background_Color (RGBA => Gnoga_Color (Color) );
764792
when Graphic_Area =>
765793
Widget.Canvas.Background_Color (RGBA => Gnoga_Color (Color) );
794+
when Image =>
795+
Widget.Img.Background_Color (RGBA => Gnoga_Color (Color) );
766796
when Password_Box =>
767797
Widget.Password.Background_Color (RGBA => Gnoga_Color (Color) );
768798
Widget.Password_Label.Background_Color (RGBA => Gnoga_Color (Color) );
@@ -798,6 +828,8 @@ package body Ada_GUI is
798828
Widget.Check_Label.Color (RGBA => Gnoga_Color (Color) );
799829
when Graphic_Area =>
800830
Widget.Canvas.Color (RGBA => Gnoga_Color (Color) );
831+
when Image =>
832+
Widget.Img.Color (RGBA => Gnoga_Color (Color) );
801833
when Password_Box =>
802834
Widget.Password.Color (RGBA => Gnoga_Color (Color) );
803835
Widget.Password_Label.Color (RGBA => Gnoga_Color (Color) );
@@ -1009,30 +1041,49 @@ package body Ada_GUI is
10091041
procedure Replace_Pixels (ID : in Widget_ID; Image : in Widget_ID; X : in Integer := 0; Y : in Integer := 0) is
10101042
Widget : Widget_Info := Widget_List.Element (ID.Value);
10111043
begin -- Replace_Pixels
1012-
Widget.Context.Draw_Image (Image => Widget_List.Element (Image.Value).Canvas.all, X => X, Y => Y);
1044+
if Image.Kind = Graphic_Area then
1045+
Widget.Context.Draw_Image (Image => Widget_List.Element (Image.Value).Canvas.all, X => X, Y => Y);
1046+
else
1047+
Widget.Context.Draw_Image (Image => Widget_List.Element (Image.Value).Img.all, X => X, Y => Y);
1048+
end if;
10131049
end Replace_Pixels;
10141050

10151051
procedure Replace_Pixels (ID : in Widget_ID; Image : in Image_Data; X : in Integer := 0; Y : in Integer := 0) is
1016-
Widget : Widget_Info := Widget_List.Element (ID.Value);
1052+
Widget : Widget_Info := Widget_List.Element (ID.Value);
1053+
Pixel : Gnoga.Pixel_Data_Type (1 .. Image'Length (2), 1 .. Image'Length (1) );
1054+
Img : Gnoga.Gui.Element.Canvas.Context_2D.Image_Data_Type;
10171055
begin -- Replace_Pixels
1018-
All_Rows : for R in Image'Range (1) loop
1019-
All_Columns : for C in Image'Range (2) loop
1020-
Widget.Context.Pixel (X => X + C, Y => Y + R, Color => Gnoga_Pixel (Image (R, C) ) );
1021-
end loop All_Columns;
1022-
end loop All_Rows;
1056+
Pixel_Rows : for R in Image'Range (1) loop -- This is a three-step process: 1. Convert Image into Pixel
1057+
Pixel_Columns : for C in Image'Range (2) loop
1058+
Pixel (C + 1, R + 1) := Gnoga_Pixel (Image (R, C) );
1059+
end loop Pixel_Columns;
1060+
end loop Pixel_Rows;
1061+
1062+
Widget.Context.Create_Image_Data (Image_Data => Img, Width => Image'Length (2), Height => Image'Length (1) );
1063+
Img.Data (Value => Pixel); -- 2. Put Pixel into Img
1064+
Widget.Context.Put_Image_Data (Image_Data => Img, Left => X, Top => Y); -- 3. Display Img in Context at X, Y
10231065
end Replace_Pixels;
10241066

10251067
function Data (ID : in Widget_ID) return Image_Data is
1026-
Widget : Widget_Info := Widget_List.Element (ID.Value);
1027-
Result : Image_Data (0 .. Widget.Height - 1, 0 .. Widget.Width - 1);
1068+
Widget : Widget_Info := Widget_List.Element (ID.Value);
1069+
Img : Gnoga.Gui.Element.Canvas.Context_2D.Image_Data_Type;
10281070
begin -- Data
1029-
All_Rows : for Y in Result'Range (1) loop
1030-
All_Columns : for X in Result'Range (2) loop
1031-
Result (Y, X) := AG_Color (Widget.Context.Pixel (X, Y) );
1032-
end loop All_Columns;
1033-
end loop All_Rows;
1071+
-- This is a three-step process: 1. Extract the image in Context into Img
1072+
Widget.Context.Get_Image_Data (Image_Data => Img, Left => 0, Top => 0, Width => Widget.Width, Height => Widget.Height);
1073+
1074+
Get_Data : declare -- 2. Get the data from Img into Pixel
1075+
Pixel : constant Gnoga.Pixel_Data_Type := Img.Data; -- 1st dimension X/columns; 2nd, Y/rows
10341076

1035-
return Result;
1077+
Result : Image_Data (0 .. Widget.Height - 1, 0 .. Widget.Width - 1);
1078+
begin -- Get_Data
1079+
All_Rows : for Y in Result'Range (1) loop -- 3. Convert Pixel into Result and return it
1080+
All_Columns : for X in Result'Range (2) loop
1081+
Result (Y, X) := AG_Color (Pixel (X + 1, Y + 1) );
1082+
end loop All_Columns;
1083+
end loop All_Rows;
1084+
1085+
return Result;
1086+
end Get_Data;
10361087
end Data;
10371088

10381089
procedure Write_BMP (Name : in String; Image : in Image_Data) is

ada_gui.ads

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
with Ada.Strings.Unbounded;
88

99
package Ada_GUI is
10-
type Widget_Kind_ID is (Audio_Player, Background_Text, Button, Check_Box, Graphic_Area, Password_Box, Progress_Bar,
10+
type Widget_Kind_ID is (Audio_Player, Background_Text, Button, Check_Box, Graphic_Area, Image, Password_Box, Progress_Bar,
1111
Radio_Buttons, Selection_List, Text_Area, Text_Box);
1212

1313
type Widget_ID is tagged private;
@@ -87,6 +87,7 @@ package Ada_GUI is
8787
return Widget_ID with Pre => Set_Up;
8888
-- Creates an Audio_Player
8989
-- Source is the source of audio; if "", then no audio is loaded
90+
-- The source may be changed with Set_Source
9091
-- In the sample implementation, Audio sources seem to be relative path names of audio files and URLs
9192
-- If Controls, then controls are displayed for the player and the user can use them to control the player
9293
-- Otherwise, no controls are displayed and control of the player must be done by the program
@@ -122,6 +123,18 @@ package Ada_GUI is
122123
-- Creates a new Graphic_Area with initial dimensions of Width by Height pixels
123124
-- (0, 0) is the upper-left corner; (Width, Height) is the lower-right corner
124125

126+
function New_Image (Row : Positive := 1;
127+
Column : Positive := 1;
128+
Source : String := "";
129+
Description : String := "";
130+
Break_Before : Boolean := False)
131+
return Widget_ID with Pre => Set_Up;
132+
-- Creates a new Image with contents defined by Source
133+
-- The source may be changed with Set_Source
134+
-- In the sample implementation, Source may be an image URL or a file name relative to the working directory, and Description
135+
-- (also called Alt Text) will be read by screen readers and displayed if Source is invalid. BMP, JPG, and PNG files have been
136+
-- tested and work; PBM and PPM are not supported
137+
125138
function New_Password_Box (Row : Positive := 1;
126139
Column : Positive := 1;
127140
Text : String := "";
@@ -309,8 +322,8 @@ package Ada_GUI is
309322
renames Selected_Button;
310323
end Dialogs;
311324

312-
procedure Set_Source (ID : in Widget_ID; Source : in String) with Pre => Set_Up and ID.Kind = Audio_Player;
313-
-- Makes Source the audio source for ID
325+
procedure Set_Source (ID : in Widget_ID; Source : in String) with Pre => Set_Up and ID.Kind in Audio_Player | Image;
326+
-- Makes Source the source for ID
314327

315328
function Source (ID : Widget_ID) return String with Pre => Set_Up and ID.Kind = Audio_Player;
316329
-- Returns the current audio source for ID
@@ -540,7 +553,7 @@ package Ada_GUI is
540553
-- If (Line_Color.None and Fill_Color.None) or Text = "", does nothing
541554

542555
procedure Replace_Pixels (ID : in Widget_ID; Image : in Widget_ID; X : in Integer := 0; Y : in Integer := 0) with
543-
Pre => Set_Up and ID.Kind = Graphic_Area and Image.Kind = Graphic_Area;
556+
Pre => Set_Up and ID.Kind = Graphic_Area and Image.Kind in Graphic_Area | Ada_Gui.Image;
544557
-- Replaces pixels in ID starting at (X, Y) and extending to the right by the width of Image, and down by the height of Image,
545558
-- with the pixels in Image
546559

@@ -558,7 +571,6 @@ package Ada_GUI is
558571
Pre => Set_Up and ID.Kind = Graphic_Area;
559572
-- Extracts the pixels in ID and returns the image
560573
-- This may be faster than calling Pixel repeatedly
561-
-- In the sample implementation, this can take tens of ms/pixel, so even a small image can take minutes
562574

563575
procedure Write_BMP (Name : in String; Image : in Image_Data) with
564576
Pre => Name'Length > 0;

0 commit comments

Comments
 (0)