// WhatSize Demonstration Program

using System;
using System.Windows.Forms;

using IPlusPlus;
using IPlusPlus.Windows;
using IPlusPlus.Graphics;

enum Columns { Column1 = 25, Column2 = 8 };

class WhatSizeDemonstration : Form
{
    int WidthOfCharacter;
    int HeightOfCharacter;

    [STAThread]
    public static void Main()
    {
        application.Run(new WhatSizeDemonstration());
    }

    WhatSizeDemonstration()
    {
        Text = "WhatSize Demonstration";
        BackColor = System.Drawing.Color.white;

        DeviceContext deviceContext = new DeviceContext(this);

        deviceContext.Font = new Font((int)StandardFont.FixedSystem);

        TextMetrics TextMetricsGet = deviceContext.TextMetrics;
        WidthOfCharacter = TextMetricsGet.AverageCharacterWidth;
        HeightOfCharacter = TextMetricsGet.Height + TextMetricsGet.ExternalLeading;

        Paint += OnPaint;
        Resize += OnSize;
    }

    void OnPaint(object source, PaintEventArgs e)
    {
        DeviceContext deviceContext = new DeviceContext(this);

        System.Drawing.Size clientSize = ClientSize;

        Point deviceSize = new Point(clientSize.Width, clientSize.Height);

        deviceContext.Font = new Font((int)StandardFont.FixedSystem);

        deviceContext.TextOut(1,
                              1 * HeightOfCharacter,
                              "Mapping Mode");

        deviceContext.TextOut(1,
                              2 * HeightOfCharacter,
                              "------------");

        deviceContext.TextOut((int)Columns.Column1 * WidthOfCharacter,
                              1 * HeightOfCharacter,
                              "Right");

        deviceContext.TextOut((int)Columns.Column1 * WidthOfCharacter,
                              2 * HeightOfCharacter,
                              "-----");

        deviceContext.TextOut((int)Columns.Column1 * WidthOfCharacter + (int)Columns.Column2 * WidthOfCharacter,
                              1 * HeightOfCharacter,
                              "Bottom");

        deviceContext.TextOut((int)Columns.Column1 * WidthOfCharacter + (int)Columns.Column2 * WidthOfCharacter,
                              2 * HeightOfCharacter,
                              "------");

        ShowSize(deviceSize, deviceContext, new Point(1, 3 * HeightOfCharacter), Unit.Pixel,       "Pixels                  ");
        ShowSize(deviceSize, deviceContext, new Point(1, 4 * HeightOfCharacter), Unit.LowMetric,   "Low Metric (.1mm)       ");
        ShowSize(deviceSize, deviceContext, new Point(1, 5 * HeightOfCharacter), Unit.HighMetric,  "High Metric (.01mm)     ");
        ShowSize(deviceSize, deviceContext, new Point(1, 6 * HeightOfCharacter), Unit.LowEnglish,  "Low English (.01 inch)  ");
        ShowSize(deviceSize, deviceContext, new Point(1, 7 * HeightOfCharacter), Unit.HighEnglish, "High English (.001 inch)");
        ShowSize(deviceSize, deviceContext, new Point(1, 8 * HeightOfCharacter), Unit.Twips,       "TWIPS (1/1440 inch)     ");
    }

    void OnSize(object sender, EventArgs e)
    {
        Invalidate();
    }

    void ShowSize(Point deviceSize,
                  DeviceContext deviceContext,
                  Point textPosition,
                  Unit mappingMode,
                  String mappingModeString)
    {
        int saveIdentity = deviceContext.Save();

        deviceContext.MappingMode = mappingMode;

        Point logicalSize = deviceContext.DevicePointToLogicalPoint(deviceSize);

        deviceContext.Restore(saveIdentity);

        deviceContext.TextOut(textPosition.X,
                              textPosition.Y,
                              mappingModeString);

        deviceContext.TextOut(textPosition.X + (int)Columns.Column1 * WidthOfCharacter,
                              textPosition.Y,
                              (logicalSize.X).ToString());

        deviceContext.TextOut(textPosition.X + (int)Columns.Column1 * WidthOfCharacter + (int)Columns.Column2 * WidthOfCharacter,
                              textPosition.Y,
                              (logicalSize.Y).ToString());
    }  

}