1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
unit framePageSetup;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics, FileUtil, LResources, Forms, ExtCtrls, StdCtrls,
Printers, OsPrinters, LCLIntf, LCLProc, Controls, CupsLCL;
type
TPageSetupMode = (psmFull, psmPapers, psmMargins);
TPageSetupOption = (
psoMargins, // margins and preview are visible
psoPapers, // papers group visible
psoOrientation // orientation group visible
);
TPageSetupOptions = set of TPageSetupOption;
{ TframePageSetup }
TframePageSetup = class(TFrame)
cbPaper: TComboBox;
cbSource: TComboBox;
panMargins: TPanel;
txtLeft: TEdit;
txtRight: TEdit;
txtTop: TEdit;
txtBottom: TEdit;
gpPaper: TGroupBox;
gpOrientation: TGroupBox;
gpMargins: TGroupBox;
lblSource: TLabel;
lblPaper: TLabel;
lblLeft: TLabel;
lblRight: TLabel;
lblTop: TLabel;
lblBottom: TLabel;
pbPreview: TPaintBox;
panSetup: TPanel;
panPreview: TPanel;
radLandscape: TRadioButton;
radPortrait: TRadioButton;
procedure cbPaperChange(Sender: TObject);
procedure panPreviewResize(Sender: TObject);
procedure pbPreviewMouseDown(Sender: TObject; Button: TMouseButton;
{%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);
procedure pbPreviewMouseWheelDown(Sender: TObject; {%H-}Shift: TShiftState;
{%H-}MousePos: TPoint; var Handled: Boolean);
procedure pbPreviewMouseWheelUp(Sender: TObject; {%H-}Shift: TShiftState;
{%H-}MousePos: TPoint; var Handled: Boolean);
procedure pbPreviewPaint(Sender: TObject);
procedure radPortraitClick(Sender: TObject);
private
{ private declarations }
FHeightTallest: Integer;
FHardMargins: TRect;
FKw,FKh,FZoom: Double;
FOptions: TPageSetupOptions;
public
{ public declarations }
procedure Initialize(AMode: TPageSetupMode);
procedure UpdatePageSize;
end;
implementation
{ TframePageSetup }
procedure TframePageSetup.pbPreviewPaint(Sender: TObject);
var
R: TRect;
procedure DrawMargin(AIndex: Integer; ASize: Integer);
begin
with pbPreview do
case AIndex of
0: // Left
begin
Canvas.MoveTo(ASize, 1);
Canvas.LineTo(ASize, Height-1);
end;
1: //Top
begin
Canvas.MoveTo(1,ASize);
Canvas.LineTo(Width-1, ASize);
end;
2: // Right
begin
Canvas.MoveTo(Width-1-ASize, 1);
Canvas.LineTo(Width-1-ASize,Height-1);
end;
3: // Bottom
begin
Canvas.MoveTo(1,Height-1-Asize);
Canvas.LineTo(Width-1, Height-1-ASize);
end;
end;
end;
begin
if Sender=nil then ;
if not (psoMargins in FOptions) then
exit;
with pbPreview do
begin
// page frame
R := Rect(0,0,Width,Height);
Canvas.Pen.Color := clBlack;
Canvas.Brush.Color:=clWhite;
Canvas.Rectangle(R);
// hard margins
Canvas.Pen.Color := RGBToColor(255,204,204);
DrawMargin(0, FHardMargins.Left );
DrawMargin(1, FHardMargins.Top );
DrawMargin(2, FHardMargins.Right );
DrawMargin(3, FHardMargins.Bottom);
end;
end;
procedure TframePageSetup.radPortraitClick(Sender: TObject);
begin
if sender=nil then ;
if radPortrait.Checked then
Printer.Orientation := poPortrait
else
Printer.Orientation := poLandsCape;
UpdatePageSize;
end;
procedure TframePageSetup.cbPaperChange(Sender: TObject);
begin
if Printer.PaperSize.DefaultPapers then
begin
if cbPaper.ItemIndex>=0 then
Printer.PaperSize.PaperName := cbPaper.Items[cbPaper.ItemIndex];
end else
Printer.PaperSize.PaperName := GetCupsComboKeyValue(cbPaper);
UpdatePageSize;
end;
procedure TframePageSetup.panPreviewResize(Sender: TObject);
var
TallH: Integer;
begin
if not (psoMargins in FOptions) then
exit;
TallH := Round(FheightTallest * FKh);
with PanPreview do
if (Height<>C_BOTHSPACES) and (TallH>(Height-C_BOTHSPACES)) then
FZoom := (Height-C_BOTHSPACES)/TallH
else
FZoom := 1.0;
end;
procedure TframePageSetup.pbPreviewMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button=mbMiddle then
begin
FZoom := 1;
UpdatePageSize;
end;
end;
procedure TframePageSetup.pbPreviewMouseWheelDown(Sender: TObject;
Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
FZoom := FZoom - 0.2;
if FZoom<0.5 then
FZoom := 0.5;
UpdatePageSize;
Handled := true;
end;
procedure TframePageSetup.pbPreviewMouseWheelUp(Sender: TObject;
Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
FZoom := FZoom + 0.2;
UpdatePageSize;
Handled := true;
end;
procedure TframePageSetup.UpdatePageSize;
begin
if not (psoMargins in FOptions) then
exit;
with Printer.PaperSize.PaperRect.PhysicalRect do
begin
PbPreview.Width := Round(Fkw * (Right - Left) * FZoom) + 2;
PbPreview.Height := Round(FKh * (Bottom - Top) * FZoom) + 2;
end;
with Printer.PaperSize.PaperRect do
begin
FHardMargins.Left := Round(Fkw * (WorkRect.Left-PhysicalRect.Left) * FZoom);
FHardMargins.Right := Round(Fkw * (Physicalrect.Right-WorkRect.Right) * FZoom);
FHardMargins.Top := Round(FkH * (WorkRect.Top-PhysicalRect.Top) * FZoom);
FHardMargins.Bottom := Round(FkH * (PhysicalRect.Bottom-WorkRect.Bottom) * FZoom);
end;
{$IFDEF DebugCUPS}
with FHardMargins do
begin
DebugLn(' Kh=%.2f Kw=%.2f',[FKh,FKw]);
DebugLn(' BoxLimits L=0 T=0 R=%d B=%d',[PbPreview.Width-1,PbPreview.Height-1]);
DebugLn('OrgMargins L=%d T=%d R=%d B=%d',[Left,Top,Right,Bottom]);
end;
{$ENDIF}
end;
procedure TframePageSetup.Initialize(AMode: TPageSetupMode);
var
i,j:Integer;
R: TPaperRect;
begin
case AMode of
psmMargins:
FOptions := [psoMargins];
psmPapers:
FOptions := [psoPapers,psoOrientation];
else
FOptions := [psoMargins,psoPapers,psoOrientation];
end;
if [psoMargins,psoPapers]*FOptions<>[] then
begin
SetupCupsCombo(cbPaper, nil, 'PageSize');
if (cbPaper.Items.Count=0) then
begin
// no cups printer papers, use default ones
cbPaper.Items := Printer.PaperSize.SupportedPapers;
cbPaper.ItemIndex:= cbPaper.Items.IndexOf(Printer.PaperSize.PaperName);
cbPaper.Enabled:=true;
end;
end;
if psoPapers in FOptions then
SetupCupsCOmbo(cbSource, nil, 'InputSlot')
else
gpPaper.Visible := false;
//TODO: support reverse variants too?
gpOrientation.Visible := (psoOrientation in FOptions);
case Printer.Orientation of
poPortrait,poReversePortrait:
radPortrait.Checked := true;
poLandscape,poReverseLandscape:
radLandscape.Checked := true;
end;
if psoMargins in FOptions then
begin
// assume 100 pix = 8.5 inch (IOW, letter size width = 100 pixels)
with ScreenInfo do
begin
FKw := (100/8.5)/Printer.XDPI;
FKh := (100/8.5)*(PixelsPerInchY/PixelsPerInchX)/Printer.YDPI;
end;
// find the tallest paper
FHeightTallest := 0;
j := -1;
if cbPaper.Enabled then
for i:=0 to cbPaper.Items.Count-1 do
begin
if Printer.PaperSize.DefaultPapers then
R := Printer.PaperSize.PaperRectOf[cbPaper.Items[i]]
else
R := Printer.PaperSize.PaperRectOf[GetCupsComboKeyValue(cbPaper, i)];
with R.PhysicalRect do
if FHeightTallest<(Bottom-Top) then
begin
FHeightTallest := (Bottom-Top);
j := i;
end;
end;
if j>=0 then
begin
{$IFDEF DebugCUPS}
DebugLn(' Tallest Paper is: %s Height=%d %.2f Inch',
[cbPaper.Items[j],FHeightTallest,FHeightTallest/Printer.YDPI]);
{$ENDIF}
end;
// zoom factor
FZoom := 1.0;
UpdatePageSize;
end else
begin
panPreview.Visible:=false;
gpMargins.Visible:=false;
end;
if AMode=psmPapers then
begin
gpOrientation.Anchors:=[akTop,akRight,akBottom];
gpOrientation.Align:=alRight;
gpPaper.Anchors:=[akTop,akLeft];
gpPaper.Align:=alClient;
PanSetup.Align:=alClient;
end else
if AMode=psmMargins then
PanSetup.Height:=gpMargins.Height+C_BOTHSPACES;
end;
initialization
{$I framepagesetup.lrs}
end.
|