Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
smourier committed Apr 5, 2024
1 parent 21153f7 commit ac39d2c
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 155 deletions.
27 changes: 9 additions & 18 deletions SheetReader.AppTest/SheetControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,24 @@ public class SheetControl : FrameworkElement
private double GetRowHeight() => Math.Max(RowHeight, 10);
private double GetRowMargin() => 60;

private static string GetExcelColumnName(int index)
{
index++;
var name = string.Empty;
while (index > 0)
{
var mod = (index - 1) % 26;
name = (char)('A' + mod) + name;
index = (index - mod) / 26;
}
return name;
}
private bool IsSheetValid() => Sheet != null &&
Sheet.FirstColumnIndex.HasValue && Sheet.LastColumnIndex.HasValue &&
Sheet.FirstRowIndex.HasValue && Sheet.LastRowIndex.HasValue;

protected override Size MeasureOverride(Size availableSize)
{
if (Sheet == null)
if (!IsSheetValid())
return new Size();

var rowHeight = GetRowHeight();
var rowMargin = GetRowMargin();
var headerHeight = rowHeight;
return new Size(rowMargin + GetColSize() * (Sheet.LastColumnIndex + 1), headerHeight + rowHeight * (Sheet.LastRowIndex + 1));
return new Size(rowMargin + GetColSize() * (Sheet.LastColumnIndex!.Value + 1), headerHeight + rowHeight * (Sheet.LastRowIndex!.Value + 1));
}

protected override void OnRender(DrawingContext drawingContext)
{
if (Sheet == null)
if (!IsSheetValid())
return;

var dpi = VisualTreeHelper.GetDpi(this);
Expand All @@ -66,8 +57,8 @@ protected override void OnRender(DrawingContext drawingContext)
var headerHeight = rowHeight;

// header backgrounds
drawingContext.DrawRectangle(Brushes.LightGray, null, new Rect(0, headerHeight, rowMargin, (Sheet.LastRowIndex + 1) * rowHeight));
drawingContext.DrawRectangle(Brushes.LightGray, null, new Rect(rowMargin, 0, (Sheet.LastColumnIndex + 1) * colSize, headerHeight));
drawingContext.DrawRectangle(Brushes.LightGray, null, new Rect(0, headerHeight, rowMargin, (Sheet.LastRowIndex!.Value + 1) * rowHeight));
drawingContext.DrawRectangle(Brushes.LightGray, null, new Rect(rowMargin, 0, (Sheet.LastColumnIndex!.Value + 1) * colSize, headerHeight));

// includes col header
for (var i = 0; i < Sheet.LastColumnIndex + 2 + 1; i++)
Expand All @@ -86,7 +77,7 @@ protected override void OnRender(DrawingContext drawingContext)
// draw col name
if (i < Sheet.LastColumnIndex + 1)
{
var name = GetExcelColumnName(i);
var name = Row.GetExcelColumnName(i);
var formatted = new FormattedText(name, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, _typeFace, fontSize, Brushes.Black, dpi.PixelsPerDip)
{
MaxTextWidth = rowMargin,
Expand Down
Binary file modified SheetReader.Test/Book1.xlsx
Binary file not shown.
63 changes: 34 additions & 29 deletions SheetReader.Wpf.Test/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ protected override void OnKeyDown(KeyEventArgs e)
var lastRecent = Settings.Current.RecentFilesPaths?.FirstOrDefault();
if (lastRecent != null)
{
Load(lastRecent.FilePath);
LoadDocument(lastRecent.FilePath);
}
}
}

private void OpenWithExcel_Click(object sender, RoutedEventArgs e) => OpenWithExcel();
private void ClearRecentFiles_Click(object sender, RoutedEventArgs e) => Settings.Current.ClearRecentFiles();
private void Exit_Click(object sender, RoutedEventArgs e) => Close();
private void Exit_Click(object sender, RoutedEventArgs e) => CloseDocument();
private void About_Click(object sender, RoutedEventArgs e) => MessageBox.Show(Assembly.GetEntryAssembly()!.GetCustomAttribute<AssemblyTitleAttribute>()!.Title + " - " + (IntPtr.Size == 4 ? "32" : "64") + "-bit" + Environment.NewLine + "Copyright (C) 2021-" + DateTime.Now.Year + " Simon Mourier. All rights reserved.", Assembly.GetEntryAssembly()!.GetCustomAttribute<AssemblyTitleAttribute>()!.Title, MessageBoxButton.OK, MessageBoxImage.Information);
private void Open_Click(object sender, RoutedEventArgs e)
{
Expand All @@ -58,7 +58,7 @@ private void Open_Click(object sender, RoutedEventArgs e)
if (ofd.ShowDialog() != true)
return;

Load(ofd.FileName);
LoadDocument(ofd.FileName);
}

private void OpenWithExcel()
Expand All @@ -84,45 +84,50 @@ private void OnFileOpened(object sender, RoutedEventArgs e)
{
var item = new MenuItem { Header = recent.FilePath };
RecentFilesMenuItem.Items.Insert(RecentFilesMenuItem.Items.Count - fixedRecentItemsCount, item);
item.Click += (s, e) => Load(recent.FilePath);
item.Click += (s, e) => LoadDocument(recent.FilePath);
}
}

RecentFilesMenuItem.IsEnabled = RecentFilesMenuItem.Items.Count > fixedRecentItemsCount;
}

private void Load(string? fileName)
private void CloseDocument() => LoadDocument(null);
private void LoadDocument(string? fileName)
{
if (fileName == null || fileName.EqualsIgnoreCase(FileName))
return;

Sheets.Clear();
try
if (fileName != null)
{
var book = new BookDocument();
book.Load(fileName);
foreach (var sheet in book.Sheets)
try
{
Sheets.Add(sheet);
var book = new BookDocument();
book.Load(fileName);
foreach (var sheet in book.Sheets)
{
Sheets.Add(sheet);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.GetAllMessagesWithDots(), "Error");
return;
}

// not sure why, but with only 1 tab, binding doesn't work...
if (Sheets.Count == 1)
{
var first = Sheets[0];
Sheets.Add(first);
Sheets.RemoveAt(1);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.GetAllMessagesWithDots(), "Error");
return;
}

// not sure why, but with only 1 tab, binding doesn't work...
if (Sheets.Count == 1)
Title = "Sheet Reader - " + Path.GetFileName(fileName);
FileName = fileName;
Settings.Current.AddRecentFile(fileName);
}
else
{
var first = Sheets[0];
Sheets.Add(first);
Sheets.RemoveAt(1);
Title = "Sheet Reader";
}

Title = "Sheet Reader - " + Path.GetFileName(fileName);
FileName = fileName;
Settings.Current.AddRecentFile(fileName);
}

private void Grid_Drop(object sender, DragEventArgs e)
Expand All @@ -131,7 +136,7 @@ private void Grid_Drop(object sender, DragEventArgs e)
{
foreach (var file in files.Where(f => Book.IsSupportedFileExtension(Path.GetExtension(f))))
{
Load(file);
LoadDocument(file);
}
}
}
Expand Down
Loading

0 comments on commit ac39d2c

Please sign in to comment.