Saturday, March 2, 2013

WPF UniformGrid variant

I've been using the WPF UniformGrid to layout the UI representation of collections in some of the applications I work on.  The apps generally follow the MVVM pattern and to get the layout I require I usually need to apply a converter to a bound viewmodel property to set the number of rows or columns.

This usually works ok but in a application where the aspect ratio of the area occupied by the UniformGrid can be changed by the user the UniformGrid doesn't doesn't alter it's row or column count to optimise the layout.

I have searched for a WPF layout that is similar to the UniformGrid but that is more flexible in it's automatic calculation of rows and columns without success so have written my own.

The following AutoUniformGrid checks its constrained size and if this is fixed (i.e. neither the width or height is infinite) it will dynamically set the number of rows and columns to maximise the height & width of its cell.

So, for example, if the AutoUniformGrid has 8 children and is sized to an area that is twice as wide as it is high, it will set its column count to 4 and its row count to 2.

In this situation, the UniformGrid would default to a 3 x 3 grid which would result in badly proportioned cells.

The AutoUniformGrid is similar to a WrapPanel except that the children all occupy the same sized area.  In my usage of this control, the children are all wrapped in a ViewBox to ensure they make full use of their allocated cell.


public class AutoUniformGrid : Panel
    {
        // Fields
        private int _columns;
        private int _rows;
        public static readonly DependencyProperty ColumnsProperty = 
            DependencyProperty.Register("Columns"typeof(int), typeof(AutoUniformGrid), 
                        new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsMeasure), 
                        new ValidateValueCallback(AutoUniformGrid.ValidateColumns));
        public static readonly DependencyProperty RowsProperty = DependencyProperty.Register("Rows"typeof(int), typeof(AutoUniformGrid), 
                        new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsMeasure), 
                        new ValidateValueCallback(AutoUniformGrid.ValidateRows));
 
        
        protected override Size ArrangeOverride(Size arrangeSize)
        {
            double width = OptimalCalculator.CalculateMinDimensionValue(arrangeSize, this._rows, this._columns);
            Size requiredSize = new Size(width * this._columns, width * this._rows);
 
            double xOffset = (arrangeSize.Width - requiredSize.Width) / 2;
            double yOffset = (arrangeSize.Height - requiredSize.Height) / 2;
 
            Rect finalRect = new Rect(xOffset, yOffset, width, width);
            double rightBoundary = xOffset + requiredSize.Width - 1.0;
            foreach (UIElement element in base.InternalChildren)
            {
                element.Arrange(finalRect);
                if (element.Visibility != Visibility.Collapsed)
                {
                    finalRect.X += width;
                    if (finalRect.X >= rightBoundary)
                    {
                        finalRect.Y += finalRect.Height;
                        finalRect.X = xOffset;
                    }
                }
            }
            return arrangeSize;
        }
 
        protected override Size MeasureOverride(Size constraint)
        {
            this.UpdateComputedValues(constraint);
 
            Size availableSize = new Size(constraint.Width / ((double)this._columns), constraint.Height / ((double)this._rows));
            double width = 0.0;
            double height = 0.0;
            int childIndex = 0;
            int count = base.InternalChildren.Count;
            while (childIndex < count)
            {
                UIElement element = base.InternalChildren[childIndex];
                element.Measure(availableSize);
                Size desiredSize = element.DesiredSize;
                if (width < desiredSize.Width)
                {
                    width = desiredSize.Width;
                }
                if (height < desiredSize.Height)
                {
                    height = desiredSize.Height;
                }
                childIndex++;
            }
            return new Size(width * this._columns, height * this._rows);
        }
 
 
        int CountVisibleChildren()
        {
            int visibleCount = 0;
            int childIndex = 0;
            int count = base.InternalChildren.Count;
            while (childIndex < count)
            {
                UIElement element = base.InternalChildren[childIndex];
                if (element.Visibility != Visibility.Collapsed)
                {
                    visibleCount++;
                }
                childIndex++;
            }
            return visibleCount;
        }
 
 
        private void UpdateComputedValues(Size constraint)
        {
            this._columns = this.Columns;
            this._rows = this.Rows;
            if ((this._rows == 0) || (this._columns == 0))
            {
                int visibleCount = CountVisibleChildren();
                if (visibleCount == 0)
                {
                    visibleCount = 1;
                }
                if (this._rows == 0)
                {
                    if (this._columns > 0)
                    {
                        this._rows = (visibleCount + (this._columns - 1)) / this._columns;
                    }
                    else
                    {
                        OptimalCalculator optimal = new OptimalCalculator( constraint, visibleCount);
                        this._rows = optimal.OptimalRows;
                        this._columns = optimal.OptimalColumns;
                    }
                } else if (this._columns == 0)
                {
                    this._columns = (visibleCount + (this._rows - 1)) / this._rows;
                }
            }
        }
 
        private static bool ValidateColumns(object o)
        {
            return (((int)o) >= 0);
        }
 
        private static bool ValidateRows(object o)
        {
            return (((int)o) >= 0);
        }
 
        public int Columns
        {
            get
            {
                return (int)base.GetValue(ColumnsProperty);
            }
            set
            {
                base.SetValue(ColumnsProperty, value);
            }
        }
 
        public int Rows
        {
            get
            {
                return (int)base.GetValue(RowsProperty);
            }
            set
            {
                base.SetValue(RowsProperty, value);
            }
        }
 
        class OptimalCalculator
        {
 
            public int OptimalRows { getprivate set; }
            public int OptimalColumns { getprivate set; }
 
            double DimensionAtOptimal { getset; }
            Size AvailableSize { getset; }
            int VisibleChildCount { getset; }
 
            public OptimalCalculator( Size availableSize, int visibleChildCount )
            {
                AvailableSize = availableSize;
                VisibleChildCount = visibleChildCount;
                DimensionAtOptimal = 0.0;
                Calculate( );
            }
 
            private void Calculate()
            {
                OptimalRows = 0;
                OptimalColumns = 0;
 
                double area = AvailableSize.Height * AvailableSize.Width;
 
                if (!double.IsInfinity(area) && !double.IsNaN(area) && area != 0.0 && VisibleChildCount > 1)
                {
                    double aspect = AvailableSize.Width / AvailableSize.Height;
                    int columns = 1, rows = 1;
 
                    if (aspect > 1)
                    {
                        for (; columns <= VisibleChildCount; columns++)
                        {
                            rows = (intMath.Ceiling( VisibleChildCount/(double)columns );                            
                            MaybeUpdateOptimal(rows, columns);
                        }
                    }
                    else
                    {
                        for (; rows <= VisibleChildCount; rows++)
                        {
                            columns = (intMath.Ceiling( VisibleChildCount / (double) rows);                            
                            MaybeUpdateOptimal(rows, columns);
                        }
                    }
                }
 
                if (OptimalRows <= 0)
                {
                    OptimalRows = (int)Math.Round(Math.Sqrt((double)VisibleChildCount));
                    if (OptimalRows * OptimalRows < VisibleChildCount)
                        OptimalRows++;
                    OptimalColumns = OptimalRows;
                }                
            }
 
            private void MaybeUpdateOptimal(int rows, int columns)
            {
                double dimensionValue = CalculateMinDimensionValue(AvailableSize, rows, columns);
                if (dimensionValue > DimensionAtOptimal)
                {
                    DimensionAtOptimal = dimensionValue;
                    OptimalRows = rows;
                    OptimalColumns = columns;
                }
            }   
        
            static internal double CalculateMinDimensionValue(Size availableSize,  int rows, int columns)
            {
                if (rows <= 0 || columns <= 0)
                    return 0.0;
                double width = availableSize.Width / columns;
                double height = availableSize.Height / rows;
                return Math.Min(width, height);
            }
        }
 
    }

Monday, January 17, 2011

WPF StringFormat Problems

If you're struggling to make StringFormat on a WPF binding work consider whether or not the data type you are binding to is matches the StringFormat you are using.

I've spent too long today trying to make StringFormat work on a DataGrid column bound to a DataTable column which I thought was a double. It didn't seem to matter what I used as the StringFormat, the data would always display the same. I'd forgotten to specify the DataColumn DataType and so when I assigned values to the column, they were stored a string and didn't respond to StringFormats that expected double values.

Sunday, October 31, 2010

Animated sine wave addition

My son is studying physics at school and one of topics was "waves". The physics study book he is using is printed and so doesn't include any wave animations. I thought animations would help with understanding but couldn't find any on-line animation where the user could change the wave frequency, phase.

I've wrote a WPF application where the user could control the frequency, phase of two sine wave and see the result of adding these two wave together. I then ported this to silverlight (which required a few small changes to the code) and published the result on www.nzdev.com

Since then, I've added the ability to also change the wave amplitude.

Sunday, October 24, 2010

WPF DocumentViewer

The WPF DocumentViewer control has some annoying features.

1. A find control ("Type text to find") that doesn't work.
2. A print function with limited configuration.

I work around these features by subclassing the DocumentViewer as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows;
using System.Printing;
using System.Windows.Xps;

namespace FixedDocPrinting
{
public class PrintDocumentViewer : DocumentViewer
{
PageOrientation _pageOrientation = PageOrientation.Portrait;
public PageOrientation PageOrientation
{
get { return _pageOrientation; }
set { _pageOrientation = value; }
}

Visibility _findControlVisibility = Visibility.Collapsed;
public Visibility FindControlVisibility
{
get
{
return _findControlVisibility;
}
set
{
_findControlVisibility = value;
UpdateFindControlVisibility();
}
}

private void UpdateFindControlVisibility()
{
object toolbar = this.Template.FindName("PART_FindToolBarHost", this);
ContentControl cc = toolbar as ContentControl;
if (cc != null)
{
HeaderedItemsControl itemsControl = cc.Content as HeaderedItemsControl;
if (itemsControl != null)
itemsControl.Visibility = FindControlVisibility;
}
}

public PrintDocumentViewer()
{
Loaded += new RoutedEventHandler(PrintDocumentViewer_Loaded);
}

void PrintDocumentViewer_Loaded(object sender, RoutedEventArgs e)
{
UpdateFindControlVisibility();
}

protected override void OnPrintCommand()
{
PrintDialog printDialog = new PrintDialog();
printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;

printDialog.PrintTicket.PageOrientation = PageOrientation;

if (printDialog.ShowDialog() == true)
{
// Code assumes this.Document will either by a FixedDocument or a FixedDocumentSequence
FixedDocument fixedDocument = this.Document as FixedDocument;
FixedDocumentSequence fixedDocumentSequence = this.Document as FixedDocumentSequence;

if (fixedDocument != null)
fixedDocument.PrintTicket = printDialog.PrintTicket;

if (fixedDocumentSequence!= null)
fixedDocumentSequence.PrintTicket = printDialog.PrintTicket;

XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);

if (fixedDocument != null)
writer.WriteAsync(fixedDocument, printDialog.PrintTicket);

if (fixedDocumentSequence != null)
writer.WriteAsync(fixedDocumentSequence, printDialog.PrintTicket);
}
}
}
}

This hides the find control and gives me the ability to print in Landscape.

To use this code, you will need to add a reference to the ReachFramework dll which contains useful classes in the System.Printing and System.Windows.Xps name spaces.


Thursday, July 8, 2010

Compiling Matlab mpgwrite with VS2010 for 64 bit Windows

I was having trouble producing Matlab movies of reasonable quality on my 64 bit Windows 7 laptop until I was able to compile and run mpgwrite (source available from Matlab Central http://www.mathworks.com/matlabcentral/fileexchange/309-mpgwrite).

It wasn't obvious how to get this to compile and run so in case someone else would like to do this I've documented what I did:

1. Ensure Visual Studio 2010 installation includes the "X64 Compilers and Tools" (and option under the Visual C++ feature in the Visual Studio 2010 installer).

2. Configure the mex script for VS2010.
Matlab R2009b and earlier doesn't directly support Visual Studio 2010 but you can still use it by running
mex -setup
from Matlab.
a. Answer n to the first question (Would you like mex to locate installed compilers [y]/n? )
b. Select "Microsoft Visual C++ 2008 SP1 " (option 5)
c. The script will output:
The default location for Microsoft Visual C++ 2008 SP1 compilers is
C:\Program Files (x86)\Microsoft Visual Studio 9.0,
but that directory does not exist on this machine.
Use C:\Program Files (x86)\Microsoft Visual Studio 9.0 anyway [y]/n?

Answer n
d. Locate your Visual Studio 2010 installation folder and enter this in response to the next question. Visual Studio 2010 was probably installed to C:\Program Files (x86)\Microsoft Visual Studio 10.0
e. Finally, you will be asked to confirm the settings and Matlab will create a file mexopts.bat with the configuration information.

3. From Microsoft Visual Studio 2010\Visual Studio Tools open "Visual Studio x64 Win64 Command prompt (2010)".

4. Change to the src folder of the downloaded the mpgwrite source code.

5. Edit the file makefile and replace $(MCC) $(MCFLAGS) after the call to mex with
-v -DWIN32
That is, your makefile should contain:

mpgread:
mex -v -DWIN32 mpgwrite.c mfwddct.c \
postdct.c huff.c bitio.c mheaders.c iframe.c \
pframe.c bframe.c psearch.c bsearch.c block.c \
mpeg.c subsampl.c jrevdct.c frame.c fsize.c

5. Run
nmake -f makefile

6. This should generate the file
mpgwrite.mexw64
Copy this file to a folder on the matlab path.

You should now be able to generate mpeg movies from matlab.


% Mpeg generation example
clear F;
fig=figure;
t = (0:1000)./10;
h = line( ...
'color','b', ...
'LineStyle','-', ...
'erase','xor', ...
'LineWidth',0.5, ...
'xdata',t,'ydata',[]);
for k=1:100
set(h,'ydata', sin(t-k).*t )
drawnow
F(k) = getframe(gca);
end
mpgwrite(F,F(1).colormap,'sine.mpg')


Friday, July 2, 2010

Outlook 2010 fails to retrieve email from pop3 server

After upgrading from Outlook 2007 to Outlook 2010 we were unable to connect to pop servers on port 110.

A network monitoring utility (SmartSniff) showed that Outlook 2010 starts it's communication with a pop3 server by issuing a CAPA command. This command is supposed to retrieve a list of the server capabilities but when issued on the our network the TCP connection is immediately dropped when CAPA is sent. This can be seen by starting a telnet session with the pop server and sending the CAPA command.

Further investigation showed that Cisco routers can be configured to block invalid pop3 commands and that the router considers CAPA to be an invalid command.

The Cisco router has probably been configured to do this by running a variation of the following:

ip inspect name inspection-name pop3 [alert {on | off}] [audit-trail {on | off}] [reset] [secure-login] [timeout number]


We should just need to turn off the inspection of pop3 packets to resolve this problem.

Thursday, November 19, 2009

Calling a service from a silverlight application

I was working on a Silverlight application that I intended to use on my GoDaddy hosted site and having trouble with calls to my WCF service (hosted on the same GoDaddy site).

Eventually, I found the following to work:

Uri GetServiceUri(String serviceName)
{
String fullPath = HtmlPage.Document.DocumentUri.ToString();
int index = fullPath.LastIndexOf('/');
Uri uri = new Uri(fullPath.Substring(0, index + 1) +
serviceName);
return uri;
}


Service2Client CreateService2Client()
{
return new Service2Client(new BasicHttpBinding(),
new EndpointAddress(GetServiceUri("Service2.svc")));
}