Skip to content

Commit 4eec95f

Browse files
author
Anthony Leonardo Gracio
committed
U621-016: Allow specifying browser's export page format
We now display a dialog allowing to choose the page format when exporting a browser to a given PDF or PNG file. Depends-On: I2d6c321ea97591d31f425bd148f2ed33b84a89a8 Change-Id: I23ccb8b7b6c4ad317f5c0c0ace182cdddc8af3a9
1 parent 12376b4 commit 4eec95f

File tree

6 files changed

+289
-58
lines changed

6 files changed

+289
-58
lines changed

browsers/src/browsers-canvas.adb

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
-- of the license. --
1616
------------------------------------------------------------------------------
1717

18+
with Ada.Characters.Handling;
1819
with Ada.Containers.Indefinite_Hashed_Maps;
1920
with Ada.Strings.Hash;
2021
with GNATCOLL.JSON; use GNATCOLL.JSON;
@@ -37,7 +38,9 @@ with Gdk.RGBA; use Gdk.RGBA;
3738
with Gdk.Window; use Gdk.Window;
3839

3940
with Gtk.Box; use Gtk.Box;
41+
with Gtk.Dialog; use Gtk.Dialog;
4042
with Gtk.Enums; use Gtk.Enums;
43+
with Gtk.GEntry; use Gtk.GEntry;
4144
with Gtk.Handlers; use Gtk.Handlers;
4245
with Gtk.Image; use Gtk.Image;
4346
with Gtk.Menu; use Gtk.Menu;
@@ -61,6 +64,7 @@ with Commands; use Commands;
6164
with Commands.Interactive; use Commands.Interactive;
6265
with Generic_Views; use Generic_Views;
6366
with GPS.Core_Kernels; use GPS.Core_Kernels;
67+
with GPS.Dialogs; use GPS.Dialogs;
6468
with GPS.Intl; use GPS.Intl;
6569
with GPS.Kernel; use GPS.Kernel;
6670
with GPS.Kernel.Actions; use GPS.Kernel.Actions;
@@ -863,23 +867,65 @@ package body Browsers.Canvas is
863867

864868
begin
865869
declare
866-
Name : constant Virtual_File :=
867-
Select_File
868-
(Title => -("Export Browser As " &
869-
Description),
870-
Parent => Get_Main_Window (Kernel),
871-
File_Pattern => +("*" & Extension),
872-
Pattern_Name => -Description,
873-
Default_Name => +("gpsbrowser" & Extension),
874-
Use_Native_Dialog => Use_Native_Dialogs.Get_Pref,
875-
Kind => Save_File,
876-
History => Get_History (Kernel));
877-
Success : Boolean;
870+
Dialog : GPS_Dialog;
871+
Name : Virtual_File;
872+
Page_Format_Combo : Combo_Box;
873+
File_Ent : Gtk_Entry;
874+
Success : Boolean;
875+
Response : Gtk_Response_Type;
876+
P_Format : Page_Format;
878877
begin
878+
-- Create a dialog to select the page's format and a filename
879+
GPS.Dialogs.Gtk_New
880+
(Dialog,
881+
Title => -"Select page format and filename",
882+
Flags => Destroy_With_Parent or Modal,
883+
Kernel => Kernel);
884+
Dialog.Add_OK_Cancel;
885+
886+
-- Create a combo box with an entry to choose the page format
887+
Page_Format_Combo := Dialog.Add_Combo
888+
(Message => "Page format",
889+
Key => "browsers_page_format",
890+
Tooltip => "Select the page format. You can use predefined "
891+
& "formats (e.g: a4_portrait) via the combo box or specify "
892+
& "a custom size with the following format: '<width>, <height>'");
893+
894+
-- Add the predefined page formats to the combo box
895+
for P_Format in Predefined_Page_Format_Type loop
896+
Page_Format_Combo.Add_Choice
897+
(Ada.Characters.Handling.To_Lower
898+
(Predefined_Page_Format_Type'Image (P_Format)));
899+
end loop;
900+
901+
-- Create the filename selection entry
902+
File_Ent := Dialog.Add_File_Selection_Entry
903+
(Message => "Select file",
904+
Key => Histories.History_Key
905+
("browsers_export_filename_" & Extension),
906+
File_Pattern => "*" & Extension,
907+
Pattern_Name => Description,
908+
Default_Name => "gpsbrowser" & Extension,
909+
Kind => Save_File);
910+
911+
-- Show the dialog
912+
Dialog.Show_All;
913+
Response := Dialog.Run;
914+
915+
if Response /= Gtk_Response_OK then
916+
Dialog.Destroy;
917+
return False;
918+
end if;
919+
920+
-- The user pressed 'Ok': create the file if the filename is correct
921+
922+
Name := Create_From_UTF8 (File_Ent.Get_Text);
923+
P_Format := To_Page_Format (Page_Format_Combo.Get_Text);
924+
879925
if Name /= GNATCOLL.VFS.No_File then
880926
Success := Data.Browser.View.Export
881927
(Filename => Name.Display_Full_Name,
882-
Page => A4_Landscape,
928+
Page => P_Format,
883929
Format => Data.Format,
884930
Visible_Area_Only => not Data.Whole);
885931

@@ -889,6 +935,8 @@ package body Browsers.Canvas is
889935
Mode => GPS.Kernel.Error);
890936
end if;
891937
end if;
938+
939+
Dialog.Destroy;
892940
end;
893941

894942
return False;

browsers/src/browsers-scripts.adb

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
------------------------------------------------------------------------------
1717

1818
with Ada.Characters.Handling; use Ada.Characters.Handling;
19-
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
2019
with Browsers.Canvas; use Browsers.Canvas;
2120
with Cairo.Pattern; use Cairo, Cairo.Pattern;
2221
with Glib; use Glib;
@@ -186,7 +185,7 @@ package body Browsers.Scripts is
186185
Position => Position_Automatic,
187186
Group => Group_Default,
188187
Commands_Category => "Browsers");
189-
use Browser_Views;
188+
190189
subtype Browser_View is Browser_Views.View_Access;
191190

192191
function Call_Method
@@ -933,31 +932,10 @@ package body Browsers.Scripts is
933932
declare
934933
F : constant String := Nth_Arg (Data, 3, "a4");
935934
Format : Page_Format;
936-
Comma : Integer;
937935
Filename : Virtual_File;
938936
Success : Boolean;
939937
begin
940-
if F = "" or else F = "a4" or else F = "a4_portrait" then
941-
Format := A4_Portrait;
942-
elsif F = "a4_landscape" then
943-
Format := A4_Landscape;
944-
elsif F = "a3" or else F = "a3_portrait" then
945-
Format := A3_Portrait;
946-
elsif F = "a3_landscape" then
947-
Format := A3_Landscape;
948-
elsif F = "letter" or else F = "letter_portrait" then
949-
Format := Letter_Portrait;
950-
elsif F = "letter_landscape" then
951-
Format := Letter_Landscape;
952-
elsif Is_Digit (F (F'First)) then
953-
Comma := Index (F, ",");
954-
Format.Width_In_Inches :=
955-
Gdouble'Value (F (F'First .. Comma - 1));
956-
Format.Height_In_Inches :=
957-
Gdouble'Value (F (Comma + 1 .. F'Last));
958-
else
959-
Format := A4_Portrait;
960-
end if;
938+
Format := To_Page_Format (F);
961939

962940
Filename := Nth_Arg (Data, 2);
963941
Success := View.Get_View.Export

browsers/src/browsers.adb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
-- of the license. --
1616
------------------------------------------------------------------------------
1717

18+
with Ada.Characters.Handling; use Ada.Characters.Handling;
19+
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
20+
1821
with Cairo; use Cairo;
1922
with GPS.Kernel.Preferences; use GPS.Kernel.Preferences;
2023
with Gdk.RGBA; use Gdk.RGBA;
@@ -373,4 +376,39 @@ package body Browsers is
373376
return Self.Read_Only;
374377
end Is_Read_Only;
375378

379+
--------------------
380+
-- To_Page_Format --
381+
--------------------
382+
383+
function To_Page_Format (F : String) return Gtkada.Canvas_View.Page_Format
384+
is
385+
Format : Gtkada.Canvas_View.Page_Format;
386+
Comma : Integer;
387+
begin
388+
389+
if F = "" or else F = "a4" or else F = "a4_portrait" then
390+
Format := To_Page_Format (A4_Portrait);
391+
elsif F = "a4_landscape" then
392+
Format := To_Page_Format (A4_Landscape);
393+
elsif F = "a3" or else F = "a3_portrait" then
394+
Format := To_Page_Format (A3_Portrait);
395+
elsif F = "a3_landscape" then
396+
Format := To_Page_Format (A3_Landscape);
397+
elsif F = "letter" or else F = "letter_portrait" then
398+
Format := To_Page_Format (Letter_Portrait);
399+
elsif F = "letter_landscape" then
400+
Format := To_Page_Format (Letter_Landscape);
401+
elsif Is_Digit (F (F'First)) then
402+
Comma := Index (F, ",");
403+
Format.Width_In_Inches :=
404+
Gdouble'Value (F (F'First .. Comma - 1));
405+
Format.Height_In_Inches :=
406+
Gdouble'Value (F (Comma + 1 .. F'Last));
407+
else
408+
Format := To_Page_Format (A4_Portrait);
409+
end if;
410+
411+
return Format;
412+
end To_Page_Format;
413+
376414
end Browsers;

browsers/src/browsers.ads

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ package Browsers is
171171
-- to the desktop.
172172
-- You will also need to override the browser's Load_From_ML
173173

174+
function To_Page_Format (F : String) return Gtkada.Canvas_View.Page_Format;
175+
-- Convert of "a4", "a4_portrait", "a4_landscape", or similar variants
176+
-- with "a3" and "letter". It can also be a string "width,height"
177+
-- where the size is given in inches.
178+
174179
private
175180

176181
type GPS_Canvas_View_Record is new Gtkada.Canvas_View.Canvas_View_Record

0 commit comments

Comments
 (0)