-
Notifications
You must be signed in to change notification settings - Fork 0
/
rppreviewmeta.cs
1036 lines (1017 loc) · 27.2 KB
/
rppreviewmeta.cs
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Copyright
/*
* Report Manager: Database Reporting tool for .Net and Mono
*
* The contents of this file are subject to the MPL License
* with optional use of GPL or LGPL licenses.
* You may not use this file except in compliance with the
* Licenses. You may obtain copies of the Licenses at:
* http://reportman.sourceforge.net/license
*
* Software is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations.
*
* Copyright (c) 1994 - 2008 Toni Martir (toni@reportman.es)
* All Rights Reserved.
*/
#endregion
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using Reportman.Drawing;
#if REPMAN_DESIGN
using System.ComponentModel;
using System.Windows.Forms.Design;
#endif
namespace Reportman.Drawing.Forms
{
/// <summary>
/// Event launched after a page is drawn
/// </summary>
/// <param name="prm">PreviewMetaFile containing the page</param>
public delegate void PageDrawnEvent(PreviewMetaFile prm);
/// <summary>
/// Event launched when Page Setup action is activated by the user
/// </summary>
/// <returns>Returns true if the user accept page setup changes</returns>
public delegate bool AlterReportEvent();
/// <summary>
/// Event launched to alter mail options before send to mail in preview is done
/// </summary>
/// <returns>Returns true if the user accept page setup changes</returns>
public delegate void SendMailEvent(object sender,SendMailEventArgs args);
/// <summary>
/// Windowed control to preview report MetaFiles, it's used by PreviewWinForms.
/// </summary>
/// <remarks>
/// You can use this control to create customized preview windows
/// </remarks>
#if REPMAN_DESIGN
[DesignerAttribute(typeof(PreviewMetafileDesigner))]
#endif
#if REPMAN_COMPACT
#else
[ToolboxBitmapAttribute(typeof(PreviewMetaFile), "previewmetafile.ico")]
#endif
public class PreviewMetaFile : ScrollableControl
{
#if REPMAN_COMPACT
Point MyAutoScrollPosition;
HScrollBar HBar;
VScrollBar VBar;
#else
LayoutEventHandler resizeevent;
#endif
/// <summary>
/// Event to launch when the user click page setup icon
/// </summary>
public AlterReportEvent OnPageSetup;
/// <summary>
/// Event to launch when the user click report parameters icon
/// </summary>
public AlterReportEvent OnReportParams;
/// <summary>
/// Event to modify mail parameters before sending
/// </summary>
public SendMailEvent OnMail;
const int MAXENTIREPAGES = 128;
int FBarWidth;
int FBarHeight;
bool forcerefresh;
AutoScaleType FAutoScale;
float FPreviewScale;
private MetaFile FMetaFile;
Bitmap FBitmap;
Bitmap FIntBitmap;
int FPage = -1;
/// <summary>
/// Internal print driver
/// </summary>
protected PrintOutNet prdriver;
int FPageDrawn, FPagesDrawn;
float FScaleDrawn;
int dpix, dpiy;
int FEntirePageCount;
int FOldPage;
private Object[] aparams;
int pagecolsdrawn, pagerowsdrawn;
int pagewidthdrawn, pageheightdrawn;
#if REPMAN_COMPACT
#else
private IAsyncResult resultwork;
#endif
private int fprogress_records;
private int fprogress_pagecount;
#if REPMAN_COMPACT
#else
/// <summary>
/// Windows Metafile optimization
/// </summary>
public WMFOptimization OptimizeWMF;
#endif
/// <summary>
/// When the window is processing a report it stores the number of records processed
/// </summary>
public int ProgressRecords { get { return fprogress_records; } }
/// <summary>
/// When the window is processing a report it stores the number of pages processed
/// </summary>
public int ProgressPageCount { get { return fprogress_pagecount; } }
/// <summary>
/// When the window is processing a report it stores if the user canceled the report
/// </summary>
public bool ProgressCancel;
private bool FEntireTopDown;
/// <summary>
/// Event launched each time a page is drawn
/// </summary>
public event PageDrawnEvent OnPageDrawn;
#if REPMAN_COMPACT
public event EventHandler OnWorkProgress;
#else
/// <summary>
/// Event launched while processing the report, you can use it to provide information to the user
/// </summary>
public event MetaFileWorkProgress OnWorkProgress;
#endif
System.Windows.Forms.PictureBox image;
/// <summary>
/// MetaFile to process
/// </summary>
public MetaFile MetaFile
{
get { return FMetaFile; }
set { SetMetaFile(value); }
}
/// <summary>
/// Force the event OnWorkProgress, used internally while processing a report
/// </summary>
/// <param name="records">Current number of records processed</param>
/// <param name="pagecount">Current number of pages processed</param>
/// <param name="docancel">Reference variable, then it is set to true the report will be cancelled</param>
public void WorkProgress(int records, int pagecount, ref bool docancel)
{
fprogress_records = records;
fprogress_pagecount = pagecount;
ProgressCancel = docancel;
if (OnWorkProgress != null)
{
#if REPMAN_COMPACT
this.Invoke(OnWorkProgress);
// OnWorkProgress();
docancel=progress_cancel;
#else
//
// TODO: Only call Invoke if the process identifier have been created, place condition
//
if (this.InvokeRequired)
{
bool dobegin = true;
if (resultwork != null)
{
if (!resultwork.IsCompleted)
{
resultwork = null;
docancel = (bool)aparams[2];
if (docancel)
dobegin = false;
}
}
if (dobegin)
{
aparams[0] = records;
aparams[1] = pagecount;
aparams[2] = docancel;
resultwork = this.BeginInvoke(OnWorkProgress, aparams);
}
//This method should be executed in main thread so
// Sinchronous execution of WorkProgress is provided
// this.Invoke(OnWorkProgress, aparams);
}
else
if (IsHandleCreated)
OnWorkProgress(records, pagecount, ref docancel);
#endif
}
}
/// <summary>
/// Override print driver
/// </summary>
/// <param name="newdriver"></param>
public void SetDriver(PrintOutNet newdriver)
{
prdriver = newdriver;
}
/// <summary>
/// Number of pages drawn, when the mode is full page and EntirePageCount is greater than one.
/// </summary>
public int PagesDrawn
{
get { return FPagesDrawn; }
}
/// <summary>
/// When multiple pages are drawn (EntirePageCount), if you set this property the path to draw them will be
/// from top to bottom
/// </summary>
public bool EntireToDown
{
get { return FEntireTopDown; }
set
{
if (FEntireTopDown != value)
{
FEntireTopDown = value;
if (AutoScale == AutoScaleType.EntirePage)
{
FPagesDrawn = 0;
ReDrawPage();
}
}
}
}
/// <summary>
/// Draw multiple pages at a time, on modern high resolution monitors, two vertical standard pages can fit on screen
/// at a readable scale
/// </summary>
public int EntirePageCount
{
get { return FEntirePageCount; }
set
{
if (FEntirePageCount != value)
{
FEntirePageCount = value;
if (FEntirePageCount < 1)
FEntirePageCount = 1;
if (FEntirePageCount > MAXENTIREPAGES)
FEntirePageCount = MAXENTIREPAGES;
if (FEntirePageCount > 1)
{
FMetaFile.RequestPage(FPage + FEntirePageCount - 1);
int pcount = FEntirePageCount;
int i;
for (i = FMetaFile.Pages.CurrentCount - 1; i > FPage; i--)
{
pcount--;
}
int newpage = FPage;
for (i = 0; i < pcount - 1; i++)
newpage--;
if (newpage < 0)
newpage = 0;
FPage = newpage;
}
ReDrawPage();
}
}
}
/// <summary>
/// Determines how the page is scaled to the window
/// </summary>
public AutoScaleType AutoScale
{
get { return FAutoScale; }
set
{
if (FAutoScale != value)
{
FAutoScale = value;
#if REPMAN_COMPACT
FreeBars();
#endif
ReDrawPage();
}
}
}
/// <summary>
/// Get or set current page, first page index is 0.
/// </summary>
public int Page
{
get { return FPage; }
set
{
if (FMetaFile != null)
{
int request = value;
if (AutoScale == AutoScaleType.EntirePage)
request = value + FEntirePageCount - 1;
FMetaFile.RequestPage(request);
}
FPage = value;
ReDrawPage();
FOldPage = -1;
}
}
/// <summary>
/// Get or set scaling index, setting this property will automatically set also the AutoScale to AutoScaleType.Custom
/// </summary>
public float PreviewScale
{
get { return FPreviewScale; }
set
{
ChangePreviewScale(value);
FAutoScale = AutoScaleType.Custom;
FPageDrawn = -1;
ReDrawPage();
}
}
private void ChangePreviewScale(float newvalue)
{
const float MIN_SCALE = 0.01F;
const float MAX_SCALE = 10.0F;
if (newvalue < MIN_SCALE)
newvalue = MIN_SCALE;
if (newvalue > MAX_SCALE)
newvalue = MAX_SCALE;
FPreviewScale = newvalue;
prdriver.Scale = FPreviewScale;
#if REPMAN_COMPACT
FreeBars();
#endif
}
/// <summary>
/// Draws the next page
/// </summary>
public void NextPage()
{
int increment = 1;
int request = 1;
FOldPage = -1;
if (AutoScale == AutoScaleType.EntirePage)
{
increment = FPagesDrawn;
request = FPage + increment + FEntirePageCount - 1;
}
else
request = FPage + increment;
if (FMetaFile != null)
{
FMetaFile.RequestPage(request);
if ((FPage + increment) < FMetaFile.Pages.CurrentCount)
{
Page = FPage + increment;
}
else
{
if (FPage != (FMetaFile.Pages.CurrentCount - FPagesDrawn))
Page = FMetaFile.Pages.CurrentCount - 1;
}
}
}
/// <summary>
/// Draws the prior page
/// </summary>
public void PriorPage()
{
int decrement = 1;
FOldPage = -1;
if (AutoScale == AutoScaleType.EntirePage)
{
decrement = EntirePageCount;
}
if ((FPage - decrement) < 0)
Page = 0;
else
Page = FPage - decrement;
}
/// <summary>
/// Draws the last page
/// </summary>
public void LastPage()
{
FOldPage = -1;
FMetaFile.RequestPage(int.MaxValue);
if (FMetaFile != null)
{
int newpage = FMetaFile.Pages.Count;
int decrement = 1;
if (AutoScale == AutoScaleType.EntirePage)
{
decrement = EntirePageCount;
}
if ((newpage - decrement) < 0)
Page = 0;
else
Page = newpage - decrement;
}
}
#if REPMAN_COMPACT
#else
private void MyResizeHandler(object sender, LayoutEventArgs e)
{
ReDrawPage();
}
#endif
private void MyParentChanged(object sender, EventArgs e)
{
ReDrawPage();
#if REPMAN_COMPACT
Width=Parent.Width;
Height=Parent.Height;
#endif
}
private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if ((pagecolsdrawn <= 0) || (pagerowsdrawn <= 0))
return;
if (FAutoScale != AutoScaleType.EntirePage)
{
if (FOldPage >= 0)
FPage = FOldPage;
AutoScale = AutoScaleType.EntirePage;
return;
}
int offsetx, offsety;
FOldPage = FPage;
// Calculate the page number based on x/y position
int newpagenum = FPage;
int numrow = e.Y / pageheightdrawn;
int numcol = e.X / pagewidthdrawn;
offsetx = (int)((e.X - (numcol * pagewidthdrawn)) / FPreviewScale);
offsety = (int)((e.Y - (numrow * pageheightdrawn)) / FPreviewScale);
if (this.FEntireTopDown)
{
newpagenum = newpagenum + numcol * pagerowsdrawn + numrow;
}
else
{
newpagenum = newpagenum + numrow * pagecolsdrawn + numcol;
}
if (newpagenum >= FPage + this.PagesDrawn)
return;
FPage = newpagenum;
AutoScale = AutoScaleType.Real;
#if REPMAN_COMPACT
int posy=offsety-(Parent.Height-FBarHeight)/2;
int posx=offsetx-(Parent.Width-FBarWidth)/2;
if (HBar!=null)
{
HBar.Value=posx;
VBar.Value=posy;
}
#else
int barheight = 0;
int barwidth = 0;
if (HScroll)
barheight = FBarHeight;
if (VScroll)
barwidth = FBarWidth;
int posx = 0;
int posy = 0;
if (VScroll)
{
posy = offsety - (Parent.Height - barheight) / 2;
}
if (HScroll)
{
posx = offsetx - (Parent.Width - barwidth) / 2;
}
AutoScrollPosition = new Point(posx, posy);
#endif
}
private void DoInit()
{
#if REPMAN_COMPACT
#else
OptimizeWMF = WMFOptimization.None;
#endif
prdriver = new PrintOutNet();
image = new PictureBox();
image.MouseDown += new MouseEventHandler(MyMouseDown);
image.Parent = this;
FBitmap = new System.Drawing.Bitmap(10, 10);
prdriver.Output = FBitmap;
FEntirePageCount = 1;
aparams = new Object[3];
this.ParentChanged += new EventHandler(MyParentChanged);
#if REPMAN_COMPACT
BackColor=System.Drawing.SystemColors.ControlDark;
MyAutoScrollPosition=new Point(0,0);
dpix=PrintOutNet.DEFAULT_RESOLUTION;
dpiy=PrintOutNet.DEFAULT_RESOLUTION;
#else
Graphics gr = this.CreateGraphics();
try
{
dpix = (int)gr.DpiX;
dpiy = (int)gr.DpiY;
}
finally
{
gr.Dispose();
}
resizeevent = new LayoutEventHandler(MyResizeHandler);
Layout += resizeevent;
#endif
image.Image = FBitmap;
image.Top = 0;
image.Left = 0;
FPreviewScale = 1.0F;
#if REPMAN_COMPACT
FBarWidth=0;
FBarHeight=0;
#else
FBarWidth = System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
FBarHeight = System.Windows.Forms.SystemInformation.HorizontalScrollBarHeight;
#endif
}
#region Generated code by Desginer
/// <summary>
/// Needed by Designer
/// </summary>
private void InitializeComponent()
{
}
#endregion
/// <summary>
/// Constructor required by Windows.Forms
/// </summary>
public PreviewMetaFile(System.ComponentModel.IContainer container)
{
//
// Required for the designer in Windows.Forms
//
#if REPMAN_DESIGN
container.Add(this);
#endif
InitializeComponent();
//
//
DoInit();
}
/// <summary>
/// Constructor, initialization of the control
/// </summary>
public PreviewMetaFile()
{
AutoScroll = true;
//
// Required for the designer in Windows.Forms
//
InitializeComponent();
DoInit();
}
#if REPMAN_COMPACT
private void DoHBarChange(object sender,EventArgs e)
{
image.Left=-HBar.Value;
}
private void DoVBarChange(object sender,EventArgs e)
{
image.Top=-VBar.Value;
}
private void FreeBars()
{
if (HBar!=null)
{
HBar.Parent=null;
HBar.Dispose();
HBar=null;
VBar.Parent=null;
VBar.Dispose();
VBar=null;
image.Top=0;
image.Left=0;
}
}
#endif
private void DoResize()
{
#if REPMAN_COMPACT
if (Parent==null)
return;
if ((HBar==null) && (FAutoScale!=AutoScaleType.EntirePage))
{
HBar=new System.Windows.Forms.HScrollBar();
VBar=new System.Windows.Forms.VScrollBar();
HBar.Top=Height-HBar.Height;
HBar.Left=0;
HBar.Width=Width;
VBar.Left=Width-VBar.Width;
VBar.Top=0;
VBar.Height=Height-HBar.Height;
FBarWidth=VBar.Width;
FBarHeight=HBar.Height;
HBar.Parent=this;
VBar.Parent=this;
HBar.BringToFront();
VBar.BringToFront();
HBar.ValueChanged+=new EventHandler(DoHBarChange);
VBar.ValueChanged+=new EventHandler(DoVBarChange);
HBar.Maximum=image.Width-(Width-VBar.Width);
VBar.Maximum=image.Height-(Height-HBar.Height);
HBar.Minimum=0;
VBar.Minimum=0;
}
#else
int hoffset = FBarWidth;
int voffset = FBarHeight;
if (!HScroll)
voffset = 0;
if (!VScroll)
hoffset = 0;
int imLeft, imTop;
imLeft = 0;
imTop = 0;
if (Parent == null)
return;
if (image.Width < Parent.ClientSize.Width - hoffset)
{
imLeft = (Parent.ClientSize.Width - image.Width - hoffset) / 2;
}
if (image.Height < Parent.ClientSize.Height - voffset)
{
imTop = (Parent.ClientSize.Height - image.Height - voffset) / 2;
}
int newleft = imLeft - Math.Abs(AutoScrollPosition.X);
int newtop = imTop - Math.Abs(AutoScrollPosition.Y);
if ((image.Left != newleft) || (image.Top != newtop))
{
image.SetBounds(newleft, newtop, image.Width, image.Height);
}
#endif
}
/// <summary>
/// Internal main procedure performing the draw of the page at selected scale
/// </summary>
protected void ReDrawPage()
{
if (Parent == null)
return;
if (FMetaFile == null)
return;
#if REPMAN_COMPACT
#else
SuspendLayout();
prdriver.Previewing = true;
try
{
#endif
if (FPage < 0)
FPage = 0;
if (FPage >= FMetaFile.Pages.CurrentCount)
{
FMetaFile.RequestPage(FPage + FEntirePageCount - 1);
if (FPage >= FMetaFile.Pages.CurrentCount)
FPage = FMetaFile.Pages.CurrentCount - 1;
}
MetaPage metapage = FMetaFile.Pages[FPage];
int newwidth, newheight;
int pagerows = 1, pagecols = 1;
int intentirepcount = 1;
SizeF clients = new SizeF((float)(Parent.ClientSize.Width),
(float)(Parent.ClientSize.Height));
SizeF asize = new SizeF((float)metapage.PhysicWidth * dpix / 1440,
(float)metapage.PhysicHeight * dpiy / 1440);
switch (AutoScale)
{
case AutoScaleType.Real:
ChangePreviewScale(1.0F);
break;
case AutoScaleType.EntirePage:
float scalev, scaleh;
if (FEntirePageCount > 1)
{
FMetaFile.RequestPage(FPage + FEntirePageCount - 1);
intentirepcount = FMetaFile.Pages.CurrentCount - FPage;
if (intentirepcount > FEntirePageCount)
intentirepcount = FEntirePageCount;
if (intentirepcount < 1)
intentirepcount = 1;
// Look for diferent page sizes
int pagesequal = 1;
SizeF newsize;
MetaPage newpage;
while (pagesequal < intentirepcount)
{
newpage = FMetaFile.Pages[FPage + pagesequal];
newsize = new SizeF((float)newpage.PhysicWidth * dpix / 1440,
(float)newpage.PhysicHeight * dpiy / 1440);
if (newsize != asize)
{
intentirepcount = pagesequal;
break;
}
pagesequal++;
}
}
if (intentirepcount == 1)
{
scalev = clients.Height / asize.Height;
scaleh = clients.Width / asize.Width;
if (scalev < scaleh)
ChangePreviewScale(scalev);
else
ChangePreviewScale(scaleh);
}
else
{
// Determine how many pages will fit horizontal
int intpagerowsv = 0;
int intpagecolsv = intentirepcount;
int pixelwidth;
do
{
intpagerowsv++;
scalev = (clients.Height / intpagerowsv) / asize.Height;
pixelwidth = (int)(asize.Width * scalev);
if (pixelwidth <= 0)
{
scalev = 0.0F;
break;
}
intpagecolsv = (int)(clients.Width / pixelwidth);
} while (((intpagecolsv * intpagerowsv) < intentirepcount) || (intpagecolsv < 0));
while (((intpagecolsv - 1) * intpagerowsv) >= intentirepcount)
intpagecolsv--;
while (intpagerowsv > intentirepcount)
intpagerowsv--;
int intpagerowsh = intentirepcount;
int intpagecolsh = 0;
int pixelheight;
do
{
intpagecolsh++;
scaleh = (clients.Width / intpagecolsh) / asize.Width;
pixelheight = (int)(asize.Height * scaleh);
if (pixelheight <= 0)
{
scaleh = 0.0F;
break;
}
intpagerowsh = (int)(clients.Height / pixelheight);
} while (((intpagecolsh * intpagerowsh) < intentirepcount) || (intpagerowsh <= 0));
while (((intpagecolsh) * (intpagerowsh - 1)) >= intentirepcount)
intpagerowsh--;
while (intpagecolsh > intentirepcount)
intpagecolsh--;
if (scaleh > scalev)
{
pagerows = intpagerowsh;
pagecols = intpagecolsh;
scalev = scaleh;
}
else
{
pagerows = intpagerowsv;
pagecols = intpagecolsv;
}
if (pagecols < 0)
pagecols = 1;
if (pagerows < 0)
pagerows = 1;
ChangePreviewScale(scalev);
}
break;
case AutoScaleType.Wide:
float scalewide = (float)(clients.Width - FBarWidth) / asize.Width;
ChangePreviewScale(scalewide);
break;
case AutoScaleType.Height:
float scaleheight = (float)(clients.Height - FBarHeight) / asize.Height;
ChangePreviewScale(scaleheight);
break;
}
bool dorefresh = false;
int pwidth = (int)((float)metapage.PhysicWidth * FPreviewScale * dpix / 1440);
int pheight = (int)((float)metapage.PhysicHeight * FPreviewScale * dpiy / 1440);
if (pwidth <= 0)
pwidth = 2;
if (pheight <= 0)
pheight = 2;
pagewidthdrawn = pwidth;
pageheightdrawn = pheight;
newwidth = pwidth * pagecols;
newheight = pheight * pagerows;
if (newwidth<=0)
newwidth=1;
if (newheight<=0)
newheight=1;
if ((newheight != FBitmap.Height) || (newwidth != FBitmap.Width))
{
dorefresh = true;
if (FBitmap != null)
{
FBitmap.Dispose();
FBitmap = null;
}
FBitmap = new Bitmap(newwidth, newheight);
image.Image = FBitmap;
image.Width = newwidth;
image.Height = newheight;
}
else
if ((FPageDrawn != FPage) || (FScaleDrawn != FPreviewScale) || FPagesDrawn != intentirepcount)
dorefresh = true;
if ((dorefresh) || (forcerefresh))
{
FPagesDrawn = intentirepcount;
Graphics gr = Graphics.FromImage(FBitmap);
Pen apen = new Pen(BackColor);
pagecolsdrawn = pagecols;
pagerowsdrawn = pagerows;
if (intentirepcount == 1)
{
prdriver.Output = FBitmap;
#if REPMAN_COMPACT
#else
prdriver.OptimizeWMF = OptimizeWMF;
#endif
prdriver.DrawPage(FMetaFile, FMetaFile.Pages[FPage]);
gr.DrawLine(apen, FBitmap.Width - 1, 0, FBitmap.Width - 1, FBitmap.Height - 1);
gr.DrawLine(apen, 0, FBitmap.Height - 1, FBitmap.Width - 1, FBitmap.Height - 1);
}
else
{
if (FIntBitmap == null)
{
FIntBitmap = new Bitmap(pwidth, pheight);
}
else
{
if ((FIntBitmap.Width != pwidth) || (FIntBitmap.Height != pheight))
{
FIntBitmap.Dispose();
FIntBitmap = null;
FIntBitmap = new Bitmap(pwidth, pheight);
}
}
int pdrawn = 0;
int x;
int y;
if (FEntireTopDown)
{
for (x = 0; x < pagecols; x++)
for (y = 0; y < pagerows; y++)
{
if (pdrawn < intentirepcount)
{
prdriver.Output = FIntBitmap;
#if REPMAN_COMPACT
#else
prdriver.OptimizeWMF = OptimizeWMF;
#endif
prdriver.DrawPage(FMetaFile, FMetaFile.Pages[FPage + pdrawn]);
gr.DrawImage(FIntBitmap, x * pwidth, y * pheight);
gr.DrawLine(apen, (x + 1) * pwidth - 1, y * pheight - 1, (x + 1) * pwidth - 1, (y + 1) * pheight - 1);
gr.DrawLine(apen, (x) * pwidth - 1, (y + 1) * pheight - 1, (x + 1) * pwidth - 1, (y + 1) * pheight - 1);
}
else
{
Brush abrush = new SolidBrush(BackColor);
gr.FillRectangle(abrush, x * pwidth, y * pheight, (x + 1) * pwidth, (y + 1) * (pheight));
}
pdrawn++;
}
}
else
{
for (y = 0; y < pagerows; y++)
for (x = 0; x < pagecols; x++)
{
if (pdrawn < intentirepcount)
{
prdriver.Output = FIntBitmap;
#if REPMAN_COMPACT
#else
prdriver.OptimizeWMF = OptimizeWMF;
#endif
prdriver.DrawPage(FMetaFile, FMetaFile.Pages[FPage + pdrawn]);
gr.DrawImage(FIntBitmap, x * pwidth, y * pheight);
gr.DrawLine(apen, (x + 1) * pwidth - 1, y * pheight - 1, (x + 1) * pwidth - 1, (y + 1) * pheight - 1);
gr.DrawLine(apen, (x) * pwidth - 1, (y + 1) * pheight - 1, (x + 1) * pwidth - 1, (y + 1) * pheight - 1);
}
else
{
Brush abrush = new SolidBrush(BackColor);
gr.FillRectangle(abrush, x * pwidth, y * pheight, (x + 1) * pwidth, (y + 1) * (pheight));
}
pdrawn++;
}
}
}
FPageDrawn = FPage;
FScaleDrawn = FPreviewScale;
image.Refresh();
}
DoResize();
if (dorefresh)
{
if (OnPageDrawn != null)
OnPageDrawn(this);
}
#if REPMAN_COMPACT
#else
}
finally
{
prdriver.Previewing = false;
ResumeLayout();
}
#endif
}
/// <summary>
/// Redraws current page
/// </summary>
public void RefreshPage()
{
forcerefresh = true;
try
{
ReDrawPage();
}
finally
{
forcerefresh = false;
}
}
private void WorkAsyncError(string message)
{
MessageBox.Show("Error", message);
}
/// <summary>
/// Internal procedure initializing data when setting metafile, it draws also the first page
/// </summary>
protected void SetMetaFile(MetaFile meta)
{
if (meta == null)
{
FMetaFile = null;
return;
}
if (meta.Empty)
{
FMetaFile = meta;
return;
}
prdriver.NewDocument(meta);
FScaleDrawn = -1.0F;
FPageDrawn = -1;
FOldPage = -1;
FMetaFile = meta;
if (FPage < 0)
FPage = 0;
FAutoScale = FMetaFile.AutoScale;
FMetaFile.OnWorkAsyncError += new MetaFileWorkAsyncError(WorkAsyncError);
FMetaFile.OnWorkProgress += new MetaFileWorkProgress(WorkProgress);
ReDrawPage();
}
protected override void Dispose(bool disposing)
{
if (FMetaFile != null)
{
FMetaFile.Release();
FMetaFile = null;
}
if (FIntBitmap != null)
{
FIntBitmap.Dispose();
FIntBitmap = null;
}
if (FBitmap != null)
{
FBitmap.Dispose();
FBitmap = null;
}
base.Dispose(disposing);
}
}
public class SendMailEventArgs