using System;
using System.ComponentModel;
using System.IO;
using Gdk;
using Gtk;
using dwf.gui;
using Pango;
using dwf.tool;
namespace DSBarCode
{
/// <summary>
/// Bar code ctrl.
/// http://www.codeproject.com/KB/miscctrl/barcodectl.aspx
/// </summary>
public class BarCodeCtrl : DrawingArea
{
public enum AlignType
{
Left,
Center,
Right
}
public enum BarCodeWeight
{
Small = 1,
Medium,
Large
}
private AlignType align = AlignType.Center;
private String code = "1234567890";
private int leftMargin = 10;
private int topMargin = 10;
private int height = 50;
private bool showHeader;
private bool showFooter;
private String headerText = "BarCode Demo";
private BarCodeWeight weight = BarCodeWeight.Small;
private FontDescription headerFont = FontDescription.FromString("Courier 18");
private FontDescription footerFont = FontDescription.FromString("Courier 8");
public AlignType VertAlign {
get { return align; }
set { align = value;
QueueDraw(); }
}
public String BarCode {
get { return code; }
set { code = value.ToUpper ();
QueueDraw(); }
}
public int BarCodeHeight {
get { return height; }
set { height = value;
QueueDraw(); }
}
public int LeftMargin {
get { return leftMargin; }
set { leftMargin = value;
QueueDraw(); }
}
public int TopMargin {
get { return topMargin; }
set { topMargin = value;
QueueDraw(); }
}
public bool ShowHeader {
get { return showHeader; }
set { showHeader = value;
QueueDraw(); }
}
public bool ShowFooter {
get { return showFooter; }
set { showFooter = value;
QueueDraw(); }
}
public String HeaderText {
get { return headerText; }
set { headerText = value;
QueueDraw(); }
}
public BarCodeWeight Weight {
get { return weight; }
set { weight = value;
QueueDraw(); }
}
public FontDescription HeaderFont {
get { return headerFont; }
set { headerFont = value;
QueueDraw(); }
}
public FontDescription FooterFont {
get { return footerFont; }
set { footerFont = value;
QueueDraw(); }
}
public BarCodeCtrl ()
{
this.Name = "BarCodeCtrl";
this.AddEvents ( (int)Gdk.EventMask.ExposureMask);
//this.Size = new System.Drawing.Size (424, 240);
}
// public void Print ()
// {
// PrintDialog pd = new PrintDialog ();
// pd.Document = printDocument1;
//
// if (pd.ShowDialog () == DialogResult.OK) {
// pd.Document.Print ();
// }
// }
public String alphabet39 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";
public String[] coded39Char =
{
/* 0 */ "000110100",
/* 1 */ "100100001",
/* 2 */ "001100001",
/* 3 */ "101100000",
/* 4 */ "000110001",
/* 5 */ "100110000",
/* 6 */ "001110000",
/* 7 */ "000100101",
/* 8 */ "100100100",
/* 9 */ "001100100",
/* A */ "100001001",
/* B */ "001001001",
/* C */ "101001000",
/* D */ "000011001",
/* E */ "100011000",
/* F */ "001011000",
/* G */ "000001101",
/* H */ "100001100",
/* I */ "001001100",
/* J */ "000011100",
/* K */ "100000011",
/* L */ "001000011",
/* M */ "101000010",
/* N */ "000010011",
/* O */ "100010010",
/* P */ "001010010",
/* Q */ "000000111",
/* R */ "100000110",
/* S */ "001000110",
/* T */ "000010110",
/* U */ "110000001",
/* V */ "011000001",
/* W */ "111000000",
/* X */ "010010001",
/* Y */ "110010000",
/* Z */ "011010000",
/* - */ "010000101",
/* . */ "110000100",
/*' '*/ "011000100",
/* $ */ "010101000",
/* / */ "010100010",
/* + */ "010001010",
/* % */ "000101010",
/* * */ "010010100"
};
protected override bool OnExposeEvent (EventExpose evnt)
{
bool basef = base.OnExposeEvent (evnt);
GraphContext gc = new GraphContext(Gtk.DotNet.Graphics.FromDrawable(evnt.Window));
Paint (gc, GuiTool.FromGdkRect( Allocation));
gc.Dispose();
return basef;
}
// private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
// {
// panel1_Paint(sender, new PaintEventArgs(e.Graphics, this.ClientRectangle));
// }
public void Paint (GraphContext gc, System.Drawing.RectangleF rect)
{
// String intercharacterGap = "0";
// String str = '*' + code.ToUpper () + '*';
// int strLength = str.Length;
//
// for (int i = 0; i < code.Length; i++) {
// if (alphabet39.IndexOf (code [i]) == -1 || code [i] == '*') {
// GuiService.PaintText (gc, "INVALID BAR CODE TEXT", rect,
// headerFont, new Cairo.Color (1, 1, 1), 0);
// return;
// }
// }
//
// String encodedString = "";
//
// for (int i = 0; i < strLength; i++) {
// if (i > 0)
// encodedString += intercharacterGap;
//
// encodedString += coded39Char [alphabet39.IndexOf (str [i])];
// }
//
// int encodedStringLength = encodedString.Length;
// int widthOfBarCodeString = 0;
// double wideToNarrowRatio = 3;
//
//
// if (align != AlignType.Left) {
// for (int i = 0; i < encodedStringLength; i++) {
// if (encodedString [i] == '1')
// widthOfBarCodeString += (int)(wideToNarrowRatio * (int)weight);
// else
// widthOfBarCodeString += (int)weight;
// }
// }
//
// int x = 0;
// int wid = 0;
// int yTop = 0;
//
// Size hSize = GuiService.Measure (this, this.Style.FontDescription, 0, headerText);
// Size fSize = GuiService.Measure (this, this.Style.FontDescription, 0, code);
//
// int headerX = 0;
// int footerX = 0;
//
// if (align == AlignType.Left) {
// x = leftMargin;
// headerX = leftMargin;
// footerX = leftMargin;
// } else if (align == AlignType.Center) {
// x = (rect.Width - widthOfBarCodeString) / 2;
// headerX = (rect.Width - (int)hSize.Width) / 2;
// footerX = (rect.Width - (int)fSize.Width) / 2;
// } else {
// x = rect.Width - widthOfBarCodeString - leftMargin;
// headerX = rect.Width - (int)hSize.Width - leftMargin;
// footerX = rect.Width - (int)fSize.Width - leftMargin;
// }
//
// if (showHeader) {
// yTop = (int)hSize.Height + topMargin;
// GuiService.PaintText (gc, headerText,
// new Gdk.Rectangle (headerX, topMargin, hSize.Width, hSize.Height),
// headerFont, new Cairo.Color (1, 1, 1),0);
// } else {
// yTop = topMargin;
// }
//
// for (int i = 0; i < encodedStringLength; i++) {
// if (encodedString [i] == '1')
// wid = (int)(wideToNarrowRatio * (int)weight);
// else
// wid = (int)weight;
// if (i % 2 == 0)
// gc.Cairo.SetSourceRGB (0, 0, 0);
// else
// gc.Cairo.SetSourceRGB (1, 1, 1);
// gc.Cairo.Rectangle (x, yTop, wid, height);
// gc.Cairo.Fill();
// x += wid;
// }
//
// yTop += height;
//
// if (showFooter)
// GuiService.PaintText (gc, code,
// new Gdk.Rectangle (footerX, yTop, fSize.Width, fSize.Height),
// footerFont, new Cairo.Color (1, 1, 1),0);
}
public void SaveImage (String file)
{
// Cairo.ImageSurface surf = new Cairo.ImageSurface (Cairo.Format.Argb32, Allocation.Width, Allocation.Height);
// Cairo.Context gr = new Cairo.Context (surf);
// GraphContext gc = new GraphContext(gr);
// Paint (gc, Allocation);
// gc.Dispose();
// surf.WriteToPng (file);
}
}
}