Mini Kabibi Habibi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Snap.Core.API;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;
using DevExpress.DXperience.Demos;
using DevExpress.Utils;
using System.Data.OleDb;
using SnapDemos.nwindDataSetTableAdapters;
using System.Text.RegularExpressions;
using RegexpMatch = System.Text.RegularExpressions.Match;
using DevExpress.Snap.Core.Fields;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraRichEdit.UI;
using DevExpress.Snap.Extensions.UI;
namespace SnapDemos.Modules {
public partial class API : TutorialControlBase {
public API() {
InitializeComponent();
using(WaitDialogForm dlg = new WaitDialogForm("Please wait", "Loading...")) {
object dataSource = GetDataSource();
gridControl1.DataSource = dataSource;
snapControl1.Document.DataSource = dataSource;
}
}
protected override void OnVisibleChanged(EventArgs e) {
base.OnVisibleChanged(e);
if(Visible)
MergeRibbon();
else
UnMergeRibbon();
}
void MergeRibbon() {
if(ParentFormMain == null)
return;
DevExpress.XtraBars.Ribbon.RibbonControl parentRibbon = ParentFormMain.Ribbon;
parentRibbon.MergeRibbon(this.ribbonControl1);
parentRibbon.Pages[0].Text = "Demo";
parentRibbon.ShowPageHeadersMode = DevExpress.XtraBars.Ribbon.ShowPageHeadersMode.ShowOnMultiplePages;
parentRibbon.ShowCustomizationMenu += OnShowCustomizationMenu;
}
void OnShowCustomizationMenu(object sender, RibbonCustomizationMenuEventArgs e) {
bool isTableGallery = e.Link.Item is GalleryChangeTableStyleItem;
if (isTableGallery)
galleryChangeTableStyleItem1.OnShowCustomizationMenu(sender, e);
bool isTableCellGallery = e.Link.Item is GalleryChangeTableCellStyleItem;
if (isTableCellGallery)
galleryChangeTableCellStyleItem1.OnShowCustomizationMenu(sender, e);
}
void UnMergeRibbon() {
if(ParentFormMain == null)
return;
DevExpress.XtraBars.Ribbon.RibbonControl parentRibbon = ParentFormMain.Ribbon;
parentRibbon.UnMergeRibbon();
parentRibbon.ShowPageHeadersMode = DevExpress.XtraBars.Ribbon.ShowPageHeadersMode.Hide;
parentRibbon.ShowCustomizationMenu -= OnShowCustomizationMenu;
}
object GetDataSource() {
string path = FilesHelper.FindingFileName(AppDomain.CurrentDomain.BaseDirectory, @"Data\nwind.mdb", false);
var dataSource = new nwindDataSet();
var connection = new OleDbConnection();
connection.ConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", path);
CategoriesTableAdapter categories = new CategoriesTableAdapter();
categories.Connection = connection;
categories.Fill(dataSource.Categories);
ProductsTableAdapter products = new ProductsTableAdapter();
products.Connection = connection;
products.Fill(dataSource.Products);
Order_DetailsTableAdapter orderDetails = new Order_DetailsTableAdapter();
orderDetails.Connection = connection;
orderDetails.Fill(dataSource.Order_Details);
var bindingSource = new BindingSource();
bindingSource.DataSource = dataSource;
bindingSource.DataMember = "Categories";
return bindingSource;
}
void AdjustSize(Table table) {
table.SetPreferredWidth(50 * 100, WidthType.FiftiethsOfPercent);
table.TableLayout = TableLayoutType.Fixed;
table.ForEachCell(AssignPreferredWidth);
}
void AssignPreferredWidth(TableCell cell, int rowIndex, int cellIndex) {
cell.PreferredWidthType = WidthType.FiftiethsOfPercent;
cell.PreferredWidth = (float)(50.0 * 100.0 / cell.Row.Cells.Count);
}
void ApplyDataSource(SnapList list, GridLevelNode node) {
if(!node.IsRootLevel) {
list.DataMember = node.RelationName;
}
}
void ApplyGroups(SnapList list, GridView grid) {
foreach(GridColumn col in grid.GroupedColumns) {
SnapListGroupInfo group = list.Groups.CreateSnapListGroupInfo(new SnapListGroupParam(col.FieldName, col.SortOrder));
SnapDocument groupHeader = group.CreateHeader();
Table box = groupHeader.InsertTable(groupHeader.Range.End, 1, 1);
AdjustSize(box);
groupHeader.CreateSnText(box.Cell(0, 0).Range.Start, col.FieldName);
groupHeader.InsertText(box.Cell(0, 0).Range.Start, String.Format("{0}: ", col.FieldName));
ApplySummary(group, grid);
list.Groups.Add(group);
}
}
static readonly Regex formatFinder = new Regex(@"(?:^|[^\{])(\{0(?:(?:,\d+)?(?::([\d]*[\w]?))?)?\})");
void BuildSummaryTemplate(SnapDocument template, TableCell box, GridSummaryItem source, SummaryRunning running) {
MatchCollection formatFields = formatFinder.Matches(source.DisplayFormat);
int k = 0;
template.InsertText(box.ContentRange.End, " ");
foreach(RegexpMatch match in formatFields) {
template.InsertText(box.ContentRange.End, source.DisplayFormat.Substring(k, match.Groups[1].Index - k));
k = match.Groups[1].Index + match.Groups[1].Length;
SnapText snText = template.CreateSnText(box.ContentRange.End, source.FieldName);
snText.BeginUpdate();
snText.SummaryRunning = running;
snText.SummaryFunc = source.SummaryType;
string format = match.Groups[2].Value;
if(!String.IsNullOrEmpty(format)) {
if(format.EndsWith("C", StringComparison.InvariantCultureIgnoreCase))
snText.FormatString = @"$0.00";
}
snText.EndUpdate();
}
template.InsertText(box.ContentRange.End, source.DisplayFormat.Substring(k));
}
void ApplySummary(SnapListGroupInfo group, GridView grid) {
foreach(GridSummaryItem item in grid.GroupSummary) {
GridGroupSummaryItem summary = item as GridGroupSummaryItem;
if(summary != null) {
if(summary.ShowInGroupColumnFooter == null) {
BuildSummaryTemplate(group.Header, group.Header.Tables[0].Cell(0, 0), summary, SummaryRunning.Group);
}
else {
int col = grid.VisibleColumns.IndexOf(summary.ShowInGroupColumnFooter);
if(col < 0)
continue;
SnapDocument footer = group.Footer;
if(footer == null)
footer = group.CreateFooter();
if(footer.Tables.Count == 0 || footer.Tables[0].Rows.Count == 0 || footer.Tables[0].Rows[0].Cells.Count != grid.VisibleColumns.Count) {
Table table = footer.InsertTable(footer.Range.Start, 1, grid.VisibleColumns.Count);
AdjustSize(table);
}
BuildSummaryTemplate(footer, footer.Tables[0].Cell(0, col), summary, SummaryRunning.Group);
}
}
}
}
void ApplySorting(SnapList list, GridView grid) {
foreach(GridColumn col in grid.SortedColumns) {
list.Sorting.Add(new SnapListGroupParam(col.FieldName, col.SortOrder));
}
}
void ApplyFilter(SnapList list, GridView grid) {
string filter = grid.ActiveFilterString;
if(!String.IsNullOrEmpty(filter))
list.Filters.Add(filter);
}
void MakeTemplate(SnapList list, GridView grid, out Table table, out SnapDocument template) {
template = list.RowTemplate;
SnapDocument header = list.ListHeader;
table = template.InsertTable(template.Range.End, 1, grid.VisibleColumns.Count);
Table caption = header.InsertTable(header.Range.End, 1, grid.VisibleColumns.Count);
AdjustSize(table);
AdjustSize(caption);
foreach(GridColumn col in grid.VisibleColumns) {
header.InsertText(caption.Cell(0, col.VisibleIndex).Range.Start, col.FieldName);
TableCell cell = table.Cell(0, col.VisibleIndex);
DocumentPosition pos = cell.Range.Start;
Type colType = GetColType(col);
if(colType == typeof(byte[]))
template.CreateSnImage(pos, col.FieldName);
else if(colType == typeof(bool))
template.CreateSnCheckBox(pos, col.FieldName);
else
template.CreateSnText(pos, col.FieldName);
}
}
Type GetColType(GridColumn gridCol) {
if(gridCol.View.DataSource != null)
return gridCol.ColumnType;
if(String.Equals(gridCol.View.Name, "detailView")) {
nwindDataSet.ProductsDataTable products = nwindDataSet.Products;
foreach(DataColumn dataCol in products.Columns) {
if(String.Equals(gridCol.FieldName, dataCol.ColumnName))
return dataCol.DataType;
}
}
return typeof(System.Object);
}
void MakeReportFooter(SnapList list, GridView grid) {
var tmp = new Dictionary<int, List<GridColumnSummaryItem>>();
foreach(GridColumn column in grid.VisibleColumns) {
int colNum = column.VisibleIndex;
foreach(GridColumnSummaryItem item in column.Summary) {
if(!tmp.ContainsKey(colNum))
tmp[colNum] = new List<GridColumnSummaryItem>(1);
tmp[colNum].Add(item);
}
}
if(tmp.Count == 0)
return;
SnapDocument footer = list.ListFooter;
Table table = footer.InsertTable(footer.Range.Start, tmp.Values.Max(o => o.Count), grid.VisibleColumns.Count);
AdjustSize(table);
foreach(KeyValuePair<int, List<GridColumnSummaryItem>> rec in tmp) {
int colNum = rec.Key;
int rowNum = 0;
foreach(GridColumnSummaryItem summary in rec.Value) {
BuildSummaryTemplate(footer, table.Cell(rowNum++, colNum), summary, SummaryRunning.Report);
}
}
}
void ApplyDetails(GridLevelNode node, Table table, SnapDocument template, int level) {
if(node.HasChildren)
foreach(GridLevelNode child in node.Nodes) {
TableRow detail = table.Rows.Append();
table.MergeCells(detail.FirstCell, detail.LastCell);
Convert(child, template, detail.Range.Start, level + 1);
}
}
void Convert(GridLevelNode node, SnapDocument document) {
Convert(node, document, document.Range.End, 1);
}
void Convert(GridLevelNode node, SnapDocument document, DocumentPosition position, int level) {
GridView grid = node.LevelTemplate as GridView;
if(grid == null || grid.VisibleColumns.Count == 0)
return;
SnapList list = document.CreateSnList(position, grid.Name);
list.BeginUpdate();
ApplyDataSource(list, node);
ApplyGroups(list, grid);
ApplySorting(list, grid);
ApplyFilter(list, grid);
Table table = null;
SnapDocument template = null;
MakeTemplate(list, grid, out table, out template);
MakeReportFooter(list, grid);
ApplyDetails(node, table, template, level);
list.ApplyTableStyles(level);
list.EndUpdate();
}
private void button1_Click(object sender, EventArgs e) {
using(WaitDialogForm dlg = new WaitDialogForm("Please wait", "Document processing...")) {
snapControl1.CreateNewDocument();
SnapDocument document = snapControl1.Document;
document.BeginUpdate();
Convert(gridControl1.LevelTree, document);
document.Fields.Update();
document.EndUpdate();
}
}
}
}