Hi all, i build a Reporttool (small and easy) maybe this is useful for someboby.
I just use Glade to make a Window with a VBox that include a Fixed-Widget for
each Header, detail, foot.
At the end it will be 3 Windows for First, N, LAST page
the coordinates are in pixel eg. i do not convert the pixel
if you place a label-widget at 100, 200 that will be
printed at 100 / (72 / 2.54) = 3.52777 cm!!!
so if you want to print the text of a label-widget at 5.0, 5.0 cm
that will be 5.0 * (72 / 2.54) = 141.732 pixel !!!
on each fixed you can place a label-widget and put some text on (include Pango
Markup)
if you want to use a database field just make a "[]" around your fieldname eg.
[number].
i also try'd to myke some logical fields like {Date}, {Page}.
i think i need a parser.
for a sample just take a loot at the glade file and my source.
my class is very very small and easy.
to get this work i use the "try and error" method, so please forgive my
coding. with no debugger on monodevelp and no working gtk on windows (need
gtk-sharp 2.8 !!! to get the printng work) this is very hard.
So this is only the first cast
HELP WELCOME
Wolfgang
PS:
don't forget, this is only working(or not ;-)) with bootstrap-2.8 !!!!!!
using System;
using Gtk;
using Gdk;
using Glade;
using Gnome;
using Pango;
using MySql.Data.MySqlClient;
namespace HW_Project
{
public class MyReport : PrintJob
{
private Glade.XML gxml = null;
private Gtk.Window thisWindow;
private Gtk.Fixed kopf;
private Gtk.Fixed detail;
private Gtk.Fixed fuss;
private Pango.Layout layout;
private double pageWidth;
private double pageHeight;
private Gnome.Font baseFont;
private string sqlKopf;
private string sqlDetail;
private string sqlFuss;
private int Page = 0;
public MyReport(PrintConfig config, string kopf, string detail, string fuss) : base (config)
{
gxml = new Glade.XML (null, HW_Project.mainClass.gladeFile, "Angebot_Seite1", null);
this.thisWindow = (Gtk.Window)gxml.GetWidget("Angebot_Seite1");
this.kopf = (Gtk.Fixed)gxml.GetWidget("kopf");
this.detail = (Gtk.Fixed)gxml.GetWidget("detail");
this.fuss = (Gtk.Fixed)gxml.GetWidget("fuss");
this.sqlKopf = kopf;
this.sqlDetail = detail;
this.sqlFuss = fuss;
DoKopf();
DoDetail();
DoFuss();
Gnome.Print.Showpage(Context);
base.Close();
}
internal void DoKopf()
{
this.Page++;
Gdk.Rectangle rect;
baseFont = Gnome.Font.FindClosest("Bitstream Vera Sans Mono Regular", 10);
Gnome.Print.Beginpage(Context, "");
Gnome.Print.Setfont(Context, baseFont);
GetPageSize (out pageWidth, out pageHeight);
layout = Gnome.Print.PangoCreateLayout(Context);
layout.FontDescription = Pango.FontDescription.FromString("Bitstream Vera Sans Mono 10");
MySqlCommand readerCommand = new MySqlCommand(sqlKopf, HW_Project.mainClass.connection);
MySqlDataReader reader = readerCommand.ExecuteReader();
reader.Read();
foreach (Gtk.Widget widget in this.kopf)
{
rect = widget.Allocation;
MoveContext(rect);
if (widget.GetType() == typeof(Gtk.Label))
{
Gtk.Label label = (Gtk.Label)widget;
label.Text = this.FindField(label.Text, reader);
// Window needs to be updated to get the correct rect
while (Application.EventsPending ())
Application.RunIteration ();
rect = label.Allocation;
MoveContext(rect);
label.UseMarkup = false;
layout.Alignment = label.Layout.Alignment;
this.layout.Width = Pango.Units.FromPixels(rect.Width);
layout.SetText(label.Text);
layout.SetMarkup(label.Text);
Gnome.Print.PangoLayout(Context, layout);
Gnome.Print.PangoLayoutPrint(Context, layout);
label.UseMarkup = true;
}
else if (widget.GetType() == typeof(Gtk.HSeparator) || widget.GetType() == typeof(Gtk.VSeparator))
{
Gnome.Print.LineStroked(Context, rect.X, pageHeight - rect.Y, rect.X + rect.Width, pageHeight - rect.Y);
}
else if (widget.GetType() == typeof(Gtk.VBox))
{
Gtk.VBox vbox = (Gtk.VBox)widget;
string strText = "";
rect = vbox.Allocation;
foreach (Gtk.Widget vboxwidget in vbox.Children)
{
try
{
int lcount = 1;
if (vboxwidget.GetType() == typeof(Gtk.Label))
{
Gtk.Label label = (Gtk.Label)vboxwidget;
label.Text = this.FindField(label.Text, reader);
strText += label.Text;
}
strText += "\n";
}
catch(Exception exception)
{
}
}
MoveContext(rect);
layout.Width = Pango.Units.FromPixels(rect.Width);
layout.SetText(strText);
layout.SetMarkup(strText);
Gnome.Print.PangoLayout(Context, layout);
Gnome.Print.PangoLayoutPrint(Context, layout);
}
}
reader.Close();
}
internal void DoDetail()
{
Gdk.Rectangle rect;
baseFont = Gnome.Font.FindClosest("Bitstream Vera Sans Mono Regular", 10);
Gnome.Print.Setfont(Context, baseFont);
GetPageSize (out pageWidth, out pageHeight);
layout = Gnome.Print.PangoCreateLayout(Context);
layout.FontDescription = Pango.FontDescription.FromString("Bitstream Vera Sans Mono 10");
rect = this.detail.Allocation;
int curPos = this.detail.Allocation.Y;
MySqlCommand readerCommand = new MySqlCommand(sqlDetail, HW_Project.mainClass.connection);
MySqlDataReader reader = readerCommand.ExecuteReader();
while(true == reader.Read())
{
try
{
int lineHeight = 0;
foreach (Gtk.Widget widget in this.detail)
{
string strText = "";
if (widget.GetType() == typeof(Gtk.Label))
{
strText = "";
Gtk.Label label = (Gtk.Label)widget;
label.UseMarkup = false;
string orgText = label.Text;
label.Text = this.FindField(label.Text, reader);
strText = label.Text;
label.UseMarkup = true;
// Window needs to be updated to get the correct rect
while (Application.EventsPending ())
Application.RunIteration ();
rect = label.Allocation;
label.UseMarkup = false;
label.Text = orgText;
lineHeight = Math.Max(lineHeight, rect.Height);
Gnome.Print.Moveto(Context, rect.Left, pageHeight - curPos);
layout.Alignment = label.Layout.Alignment;
layout.Width = Pango.Units.FromPixels(rect.Width);
layout.SetMarkup(strText);
Gnome.Print.PangoUpdateContext(layout.Context, Context);
Gnome.Print.PangoLayout(Context, layout);
Gnome.Print.PangoLayoutPrint(Context, layout);
}
else if (widget.GetType() == typeof(Gtk.VBox))
{
Gtk.VBox vbox = (Gtk.VBox)widget;
strText = "";
rect = vbox.Allocation;
foreach (Gtk.Widget vboxwidget in vbox.Children)
{
try
{
if (vboxwidget.GetType() == typeof(Gtk.Label))
{
Gtk.Label label = (Gtk.Label)vboxwidget;
label.UseMarkup = false;
string orgText = label.Text;
label.Text = FindField(label.Text, reader);
strText += label.Text;
label.UseMarkup = true;
// Window needs to be updated to get the correct rect
while (Application.EventsPending ())
Application.RunIteration ();
rect = label.Allocation;
label.UseMarkup = false;
label.Text = orgText;
lineHeight = Math.Max(lineHeight, rect.Height);
layout.Alignment = label.Layout.Alignment;
}
}
catch(Exception exception)
{
}
}
lineHeight = Math.Max(lineHeight, rect.Height);
Gnome.Print.Moveto(Context, rect.Left, pageHeight - curPos);
layout.Width = Pango.Units.FromPixels(rect.Width);
layout.SetMarkup(strText);
Gnome.Print.PangoUpdateContext(layout.Context, Context);
Gnome.Print.PangoLayout(Context, layout);
Gnome.Print.PangoLayoutPrint(Context, layout);
}
}
curPos += lineHeight;
}
catch(Exception e)
{
Gtk.MessageDialog dialog;
dialog = new Gtk.MessageDialog(this.thisWindow, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Close, e.Message);
dialog.Run();
dialog.Destroy();
}
}
reader.Close();
}
internal void DoFuss()
{
Gdk.Rectangle rect;
baseFont = Gnome.Font.FindClosest("Bitstream Vera Sans Mono Regular", 10);
Gnome.Print.Setfont(Context, baseFont);
GetPageSize (out pageWidth, out pageHeight);
layout = Gnome.Print.PangoCreateLayout(Context);
layout.FontDescription = Pango.FontDescription.FromString("Bitstream Vera Sans Mono 10");
MySqlCommand readerCommand = new MySqlCommand(sqlFuss, HW_Project.mainClass.connection);
MySqlDataReader reader = readerCommand.ExecuteReader();
reader.Read();
int lineHeight = 0;
int curPos = this.fuss.Allocation.Height;
foreach (Gtk.Widget widget in this.fuss)
{
rect = widget.Allocation;
if (widget.GetType() == typeof(Gtk.Label))
{
Gtk.Label label = (Gtk.Label)widget;
label.Text = this.FindField(label.Text, reader);
// Window needs to be updated to get the correct rect
while (Application.EventsPending ())
Application.RunIteration ();
rect = label.Allocation;
lineHeight = Math.Max(lineHeight, rect.Height);
Gnome.Print.Moveto(Context, rect.Left, curPos);
label.UseMarkup = false;
layout.Alignment = label.Layout.Alignment;
this.layout.Width = Pango.Units.FromPixels(rect.Width);
layout.SetText(label.Text);
layout.SetMarkup(label.Text);
Gnome.Print.PangoLayout(Context, layout);
Gnome.Print.PangoLayoutPrint(Context, layout);
label.UseMarkup = true;
curPos -= lineHeight;
}
else if (widget.GetType() == typeof(Gtk.HSeparator) || widget.GetType() == typeof(Gtk.VSeparator))
{
Gnome.Print.LineStroked(Context, rect.X, pageHeight - rect.Y, rect.X + rect.Width, pageHeight - rect.Y);
}
else if (widget.GetType() == typeof(Gtk.VBox))
{
Gtk.VBox vbox = (Gtk.VBox)widget;
string strText = "";
rect = vbox.Allocation;
foreach (Gtk.Widget vboxwidget in vbox.Children)
{
try
{
int lcount = 1;
if (vboxwidget.GetType() == typeof(Gtk.Label))
{
Gtk.Label label = (Gtk.Label)vboxwidget;
label.Text = this.FindField(label.Text, reader);
strText += label.Text;
}
strText += "\n";
}
catch(Exception exception)
{
}
}
MoveContext(rect);
layout.Width = Pango.Units.FromPixels(rect.Width);
layout.SetText(strText);
layout.SetMarkup(strText);
Gnome.Print.PangoLayout(Context, layout);
Gnome.Print.PangoLayoutPrint(Context, layout);
}
}
reader.Close();
}
internal string FindField(string text, MySqlDataReader reader)
{
string strRetval = text;
int start = 0;
int end = 0;
start = text.IndexOf("[");
if (start != -1)
{
end = text.IndexOf("]");
if (end != -1)
{
string field = text.Substring(start, end - start + 1);
string fieldName = text.Substring(start + 1, end - start - 1);
strRetval = text.Replace(field, String.Format("{0}", reader.GetValue(reader.GetOrdinal(fieldName))));
}
}
start = text.IndexOf("{");
if (start != -1)
{
end = text.IndexOf("}");
if (end != -1)
{
string field = text.Substring(start, end - start + 1);
if (field == "{Page}")
{
strRetval = text.Replace(field, String.Format("{0}", this.Page));
}
else if (field == "{Datum}")
{
strRetval = text.Replace(field, String.Format("{0:d}", DateTime.Now));
}
else if (field == "{Brutto}")
{
try
{
decimal menge = (decimal)reader.GetValue(reader.GetOrdinal("Menge"));
decimal verkauf = (decimal)reader.GetValue(reader.GetOrdinal("Verkauf"));
decimal brutto = menge * verkauf;
strRetval = text.Replace(field, String.Format("{0:N}", brutto));
}
catch(Exception exception)
{
strRetval = String.Empty;
}
}
}
}
return strRetval;
}
internal void MoveContext(Gdk.Rectangle rect)
{
Gnome.Print.Moveto(Context, rect.Left, pageHeight - rect.Top);
}
}
}
report.glade
Description: application/glade-2
_______________________________________________ Gtk-sharp-list maillist - [email protected] http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
