Skip to content

Commit

Permalink
Merge branch 'feature/printing' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
cwensley committed Nov 18, 2012
2 parents 087c25f + e7e9818 commit a5b9c44
Show file tree
Hide file tree
Showing 55 changed files with 2,486 additions and 382 deletions.
58 changes: 58 additions & 0 deletions Source/Eto.Platform.Gtk/Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,63 @@ public static Gtk.MessageType ToGtk (this MessageBoxType type)
return Gtk.MessageType.Question;
}
}

public static Gtk.PageOrientation ToGtk (this PageOrientation value)
{
switch (value) {
case PageOrientation.Landscape:
return Gtk.PageOrientation.Landscape;
case PageOrientation.Portrait:
return Gtk.PageOrientation.Portrait;
default:
throw new NotSupportedException ();
}
}

public static PageOrientation ToEto (this Gtk.PageOrientation value)
{
switch (value) {
case Gtk.PageOrientation.Landscape:
return PageOrientation.Landscape;
case Gtk.PageOrientation.Portrait:
return PageOrientation.Portrait;
default:
throw new NotSupportedException ();
}
}

public static Gtk.PageRange ToGtkPageRange (this Range range)
{
return new Gtk.PageRange { Start = range.Start - 1, End = range.End - 1 };
}

public static Range ToEto (this Gtk.PageRange range)
{
return new Range (range.Start + 1, range.End - range.Start + 1);
}

public static Gtk.PrintPages ToGtk (this PrintSelection value)
{
switch (value) {
case PrintSelection.AllPages:
return Gtk.PrintPages.All;
case PrintSelection.SelectedPages:
return Gtk.PrintPages.Ranges;
default:
throw new NotSupportedException ();
}
}

public static PrintSelection ToEto (this Gtk.PrintPages value)
{
switch (value) {
case Gtk.PrintPages.All:
return PrintSelection.AllPages;
case Gtk.PrintPages.Ranges:
return PrintSelection.SelectedPages;
default:
throw new NotSupportedException ();
}
}
}
}
57 changes: 34 additions & 23 deletions Source/Eto.Platform.Gtk/Drawing/GraphicsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Eto.Platform.GtkSharp.Drawing
{
public class GraphicsHandler : WidgetHandler<Cairo.Context, Graphics>, IGraphics
{
Pango.Context pangoContext;
Gtk.Widget widget;
Gdk.Drawable drawable;
Image image;
Expand All @@ -13,6 +14,12 @@ public class GraphicsHandler : WidgetHandler<Cairo.Context, Graphics>, IGraphics
public GraphicsHandler ()
{
}

public GraphicsHandler (Cairo.Context context, Pango.Context pangoContext)
{
this.Control = context;
this.pangoContext = pangoContext;
}

public GraphicsHandler (Gtk.Widget widget, Gdk.Drawable drawable)
{
Expand All @@ -35,6 +42,16 @@ public ImageInterpolation ImageInterpolation {
get; set;
}

public Pango.Context PangoContext
{
get {
if (pangoContext == null && widget != null) {
pangoContext = widget.PangoContext;
}
return pangoContext;
}
}

public void CreateFromImage (Bitmap image)
{
this.image = image;
Expand All @@ -57,7 +74,7 @@ public void Flush ()
{
if (image != null) {
var handler = (BitmapHandler)image.Handler;
Gdk.Pixbuf pb = (Gdk.Pixbuf)image.ControlObject;
Gdk.Pixbuf pb = handler.GetPixbuf (Size.MaxValue);
if (pb != null) {

surface.Flush ();
Expand Down Expand Up @@ -210,33 +227,27 @@ public Region ClipRegion {

public void DrawText (Font font, Color color, int x, int y, string text)
{
if (widget != null) {
using (var layout = new Pango.Layout (widget.PangoContext)) {
layout.FontDescription = (Pango.FontDescription)font.ControlObject;
layout.SetText (text);
Control.Save ();
Control.Color = color.ToCairo ();
Control.MoveTo (x, y);
Pango.CairoHelper.LayoutPath (Control, layout);
Control.Fill ();
Control.Restore ();
}
using (var layout = new Pango.Layout (PangoContext)) {
layout.FontDescription = ((FontHandler)font.Handler).Control;
layout.SetText (text);
Control.Save ();
Control.Color = color.ToCairo ();
Control.MoveTo (x, y);
Pango.CairoHelper.LayoutPath (Control, layout);
Control.Fill ();
Control.Restore ();
}
}

public SizeF MeasureString (Font font, string text)
{
if (widget != null) {

Pango.Layout layout = new Pango.Layout (widget.PangoContext);
layout.FontDescription = (Pango.FontDescription)font.ControlObject;
layout.SetText (text);
int width, height;
layout.GetPixelSize (out width, out height);
layout.Dispose ();
return new SizeF (width, height);
}
return new SizeF ();
Pango.Layout layout = new Pango.Layout (PangoContext);
layout.FontDescription = ((FontHandler)font.Handler).Control;
layout.SetText (text);
int width, height;
layout.GetPixelSize (out width, out height);
layout.Dispose ();
return new SizeF (width, height);
}

protected override void Dispose (bool disposing)
Expand Down
6 changes: 6 additions & 0 deletions Source/Eto.Platform.Gtk/Eto.Platform.Gtk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@
<Compile Include="Drawing\FontsHandler.cs" />
<Compile Include="Drawing\FontFamilyHandler.cs" />
<Compile Include="Drawing\FontTypefaceHandler.cs" />
<Compile Include="Forms\Printing\PrintDialogHandler.cs" />
<Compile Include="Forms\Printing\PrintSettingsHandler.cs" />
<Compile Include="Forms\Printing\PrintDocumentHandler.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets" />
<PropertyGroup>
Expand All @@ -254,4 +257,7 @@
<ItemGroup />
<ItemGroup />
<ItemGroup />
<ItemGroup>
<Folder Include="Forms\Printing\" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions Source/Eto.Platform.Gtk/Forms/Controls/TextAreaHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public Range Selection
}
set {
sendSelectionChanged = false;
var start = Control.Buffer.GetIterAtOffset(value.Location);
var end = Control.Buffer.GetIterAtOffset(value.Location + value.Length);
var start = Control.Buffer.GetIterAtOffset(value.Start);
var end = Control.Buffer.GetIterAtOffset(value.Start + value.Length);
Control.Buffer.SelectRange (start, end);
Widget.OnSelectionChanged (EventArgs.Empty);
sendSelectionChanged = true;
Expand Down
89 changes: 89 additions & 0 deletions Source/Eto.Platform.Gtk/Forms/Printing/PrintDialogHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using Eto.Forms;

namespace Eto.Platform.GtkSharp.Forms.Printing
{
public class PrintDialogHandler : WidgetHandler<Gtk.PrintUnixDialog, PrintDialog>, IPrintDialog
{
PrintSettings settings;

public PrintDialogHandler ()
{
AllowPageRange =true;
}

public class CustomOptions : Gtk.VBox {
public Gtk.CheckButton SelectionOnly { get; private set; }

public CustomOptions ()
{
this.Spacing = 10;
SelectionOnly = new Gtk.CheckButton { Label = "Selection Only" };
this.PackStart (SelectionOnly, false, false, 10);
}
}

public DialogResult ShowDialog (Window parent)
{
var parentWindow = parent != null ? (Gtk.Window)parent.ControlObject : null;
Control = new Gtk.PrintUnixDialog(string.Empty, parentWindow);

if (parent != null)
{
Control.TransientFor = ((Gtk.Window)parent.ControlObject);
Control.Modal = true;
}

var caps = Gtk.PrintCapabilities.Preview
| Gtk.PrintCapabilities.Collate
| Gtk.PrintCapabilities.GeneratePdf
| Gtk.PrintCapabilities.Copies
| Gtk.PrintCapabilities.PageSet
| Gtk.PrintCapabilities.GeneratePs
| Gtk.PrintCapabilities.Scale
| Gtk.PrintCapabilities.NumberUp
| Gtk.PrintCapabilities.Reverse;
var printSettingsHandler = (PrintSettingsHandler)this.PrintSettings.Handler;

Control.PageSetup = this.PrintSettings.ToGtkPageSetup ();
Control.PrintSettings = this.PrintSettings.ToGtkPrintSettings ();
var customOptions = new CustomOptions();
customOptions.SelectionOnly.Active = printSettingsHandler.SelectionOnly;

if (AllowSelection)
Control.AddCustomTab (customOptions, new Gtk.Label { Text = "Other Options" });

Control.ManualCapabilities = caps;
Control.ShowAll ();
var response = (Gtk.ResponseType)Control.Run ();
Control.Hide ();

printSettingsHandler.Set(Control.PrintSettings, Control.PageSetup, customOptions.SelectionOnly.Active);
if (response == Gtk.ResponseType.Apply) {
printSettingsHandler.ShowPreview = true;
return DialogResult.Ok;
}

return response.ToEto ();
}
public PrintSettings PrintSettings {
get {
if (settings == null) settings = new PrintSettings(Widget.Generator);
return settings;
}
set {
settings = value;
}
}

// not supported in gtk
public bool AllowPageRange {
get; set;
}

public bool AllowSelection {
get; set;
}
}
}

81 changes: 81 additions & 0 deletions Source/Eto.Platform.Gtk/Forms/Printing/PrintDocumentHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using Eto.Forms;
using Eto.Drawing;
using Eto.Platform.GtkSharp.Drawing;

namespace Eto.Platform.GtkSharp.Forms.Printing
{
public class PrintDocumentHandler : WidgetHandler<Gtk.PrintOperation, PrintDocument>, IPrintDocument
{
PrintSettings settings;

public PrintDocumentHandler ()
{
Control = new Gtk.PrintOperation();
}

public void Print ()
{
var settingsHandler = (PrintSettingsHandler)this.PrintSettings.Handler;
Control.PrintSettings = settingsHandler.Control;
if (settingsHandler.ShowPreview)
Control.Run (Gtk.PrintOperationAction.Preview, null);
else
Control.Run (Gtk.PrintOperationAction.Print, null);
}

public override void AttachEvent (string id)
{
switch (id) {
case PrintDocument.BeginPrintEvent:
Control.BeginPrint += (o, args) => {
Widget.OnBeginPrint (EventArgs.Empty);
};
break;
case PrintDocument.EndPrintEvent:
Control.EndPrint += (o, args) => {
Widget.OnEndPrint (EventArgs.Empty);
};
break;

case PrintDocument.PrintPageEvent:
Control.DrawPage += (o, args) => {
using (var graphics = new Graphics(Widget.Generator, new GraphicsHandler(args.Context.CairoContext, args.Context.CreatePangoContext ()))) {
var width = args.Context.Width; //.PageSetup.GetPageWidth(Gtk.Unit.Points);
var height = args.Context.Height; //.PageSetup.GetPageHeight(Gtk.Unit.Points);
var e = new PrintPageEventArgs(graphics, new SizeF((float)width, (float)height), args.PageNr);
Widget.OnPrintPage (e);
}
};
break;
default:
base.AttachEvent (id);
break;
}
}

public string Name {
get { return Control.JobName; }
set { Control.JobName = value; }
}

public PrintSettings PrintSettings {
get {
if (settings == null)
settings = Control.PrintSettings.ToEto (Control.DefaultPageSetup, false, Widget.Generator);
return settings;
}
set {
settings = value;
Control.DefaultPageSetup = settings.ToGtkPageSetup ();
Control.PrintSettings = settings.ToGtkPrintSettings ();
}
}

public int PageCount {
get { return Control.NPages; }
set { Control.NPages = value; }
}
}
}

Loading

0 comments on commit a5b9c44

Please sign in to comment.