using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
/// This code file contains the functions used to implement all the commands
/// in the Draw/* command hierarchy.
/// These commands change the currently-live card template atomically, and are all associated with a single undo step
namespace CardStudio
{
public partial class FormMain : Form
{
#region Draw/Create/*
///
/// Draw/Create/Element adds a basic TemplateElement to the current card template. It appears on top of all other controls.
/// Parameters are X Y W H floats, the dimensions of the Element in points
///
protected CardStudio.CommandResults cmdDraw_Create_Element(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 5) { return CommandResults.BadParameter; }
float X, Y, W, H;
try {
X = float.Parse(cmdparameters[1]);
Y = float.Parse(cmdparameters[2]);
W = float.Parse(cmdparameters[3]);
H = float.Parse(cmdparameters[4]);
}
catch
{
return CommandResults.BadParameter;
}
TemplateElement te = new TemplateElement( Program.CurrentProject.NextID() );
te.PointsX = X;
te.PointsY = Y;
te.PointsHeight = H;
te.PointsWidth = W;
tppTemplatePanel.TemplateCard.AddChild(te);
Program.CurrentProject.Elements.Add(te.ID, te);
te.RecalculatePixels();
undocommand = "Draw/Delete " + te.ID.ToString();
redocommand = "Draw/Undelete " + te.ID.ToString();
return CommandResults.Ok;
}
///
/// Draw/Create/FixedImage adds a TemplateFixedImage to the current card template. It appears on top of all other controls.
/// Parameters are X Y W H floats, the dimensions of the FixedImage in points
///
protected CardStudio.CommandResults cmdDraw_Create_FixedImage(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 5) { return CommandResults.BadParameter; }
float X, Y, W, H;
try
{
X = float.Parse(cmdparameters[1]);
Y = float.Parse(cmdparameters[2]);
W = float.Parse(cmdparameters[3]);
H = float.Parse(cmdparameters[4]);
}
catch
{
return CommandResults.BadParameter;
}
TemplateFixedImage te = new TemplateFixedImage( Program.CurrentProject.NextID() );
te.PointsX = X;
te.PointsY = Y;
te.PointsHeight = H;
te.PointsWidth = W;
tppTemplatePanel.TemplateCard.AddChild(te);
Program.CurrentProject.Elements.Add(te.ID, te);
te.RecalculatePixels();
undocommand = "Draw/Delete " + te.ID.ToString();
redocommand = "Draw/Undelete " + te.ID.ToString();
return CommandResults.Ok;
}
///
/// Draw/Create/FixedLabel adds a TemplateFixedLabel to the current card template. It appears on top of all other controls.
/// Parameters are X Y W H floats, the dimensions of the FixedLabel in points
///
protected CardStudio.CommandResults cmdDraw_Create_FixedLabel(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 5) { return CommandResults.BadParameter; }
float X, Y, W, H;
try
{
X = float.Parse(cmdparameters[1]);
Y = float.Parse(cmdparameters[2]);
W = float.Parse(cmdparameters[3]);
H = float.Parse(cmdparameters[4]);
}
catch
{
return CommandResults.BadParameter;
}
TemplateFixedLabel te = new TemplateFixedLabel(Program.CurrentProject.NextID());
te.PointsX = X;
te.PointsY = Y;
te.PointsHeight = H;
te.PointsWidth = W;
tppTemplatePanel.TemplateCard.AddChild(te);
Program.CurrentProject.Elements.Add(te.ID, te);
te.RecalculatePixels();
undocommand = "Draw/Delete " + te.ID.ToString();
redocommand = "Draw/Undelete " + te.ID.ToString();
return CommandResults.Ok;
}
#endregion Draw/Create/*
#region Draw/Adjust/*
///
/// Draw/Adjust/MoveTo moves a named block to a new location.
/// Parameters are X Y ID (float float id), the location to move to (in points) and the internal id of the block to move.
///
protected CardStudio.CommandResults cmdDraw_Adjust_MoveTo(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 4) { return CommandResults.BadParameter; }
float tX, tY;
int id;
TemplateElement te;
try
{
tX = float.Parse(cmdparameters[1]);
tY = float.Parse(cmdparameters[2]);
id = Int32.Parse(cmdparameters[3]);
te = Program.CurrentProject.Elements[id];
if (te == null) { return CommandResults.BadParameter; }
if (!tppTemplatePanel.TemplateCard.ChildElements.Contains(te)) { return CommandResults.BadParameter; }
}
catch
{
return CommandResults.BadParameter;
}
StringBuilder undosb = new StringBuilder("Draw/Adjust/MoveTo ");
undosb.Append(te.PointsX);
undosb.Append(" ");
undosb.Append(te.PointsY);
undosb.Append(" ");
undosb.Append(te.ID.ToString());
te.PointsX = tX;
te.PointsY = tY;
undocommand = undosb.ToString();
return CommandResults.Ok;
}
///
/// Draw/Adjust/MoveBy moves a named block by a given amount.
/// Parameters are dX dY ID1 ID2 ID3 ... (float float id id id...), the delta to move by (in points) and the internal id(s) of the block(s) to move.
///
protected CardStudio.CommandResults cmdDraw_Adjust_MoveBy(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 4) { return CommandResults.BadParameter; }
float dX, dY;
int id;
try
{
dX = float.Parse(cmdparameters[1]);
dY = float.Parse(cmdparameters[2]);
}
catch
{
return CommandResults.BadParameter;
}
TemplateElement te;
StringBuilder undosb = new StringBuilder("Draw/Adjust/MoveBy ");
undosb.Append(-dX);
undosb.Append(" ");
undosb.Append(-dY);
for (int i = 3; i < cmdparameters.Length; i++)
{
try
{
id = Int32.Parse(cmdparameters[i]);
te = Program.CurrentProject.Elements[id];
}
catch (Exception) { continue; }
if (te == null) { continue; }
if (!tppTemplatePanel.TemplateCard.ChildElements.Contains(te)) { continue; }
te.PointsX += dX;
te.PointsY += dY;
undosb.Append(" ");
undosb.Append(te.ID.ToString());
}
undocommand = undosb.ToString();
return CommandResults.Ok;
}
///
/// Draw/Adjust/RotateTo rotates a given block in place to a specific angle
/// Parameters are D ID (int id), the rotation to move to (in degrees counterclockwise) and the internal id of the block to move.
///
protected CardStudio.CommandResults cmdDraw_Adjust_RotateTo(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 3) { return CommandResults.BadParameter; }
int id, deg;
TemplateElement te;
TemplateElement.RightAngle ra;
try
{
deg = Int32.Parse(cmdparameters[1]);
id = Int32.Parse(cmdparameters[2]);
te = Program.CurrentProject.Elements[id];
if (te == null) { return CommandResults.BadParameter; }
if (!tppTemplatePanel.TemplateCard.ChildElements.Contains(te)) { return CommandResults.BadParameter; }
ra = (TemplateElement.RightAngle)deg;
}
catch
{
return CommandResults.BadParameter;
}
undocommand = "Draw/Adjust/Rotate " + ((int)te.Orientation).ToString() + " " + te.ID.ToString();
te.Orientation = ra;
return CommandResults.Ok;
}
///
/// Draw/Adjust/SizeTo stretches a given block in place to a specific size
/// Parameters are W H ID (float float id), the width and height to size to (in points) and the internal id of the block to resize.
///
protected CardStudio.CommandResults cmdDraw_Adjust_SizeTo(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 4) { return CommandResults.BadParameter; }
int id;
float w, h;
TemplateElement te;
try
{
w = float.Parse(cmdparameters[1]);
h = float.Parse(cmdparameters[2]);
id = Int32.Parse(cmdparameters[3]);
te = Program.CurrentProject.Elements[id];
if (te == null) { return CommandResults.BadParameter; }
if (!tppTemplatePanel.TemplateCard.ChildElements.Contains(te)) { return CommandResults.BadParameter; }
}
catch
{
return CommandResults.BadParameter;
}
undocommand = "Draw/Adjust/SizeTo " + te.PointsWidth.ToString() + " " + te.PointsHeight.ToString() + " " + te.ID.ToString();
te.PointsHeight = h;
te.PointsWidth = w;
return CommandResults.Ok;
}
///
/// Draw/Adjust/Font sets the font on a block. The block must implement TemplateHasFont
/// Parameters are Size Style FamilyName ID (float int string id).
/// This is the new font size (points), the new font style as a bitmap, the new font family name, and the id of the block
///
protected CardStudio.CommandResults cmdDraw_Adjust_Font(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 5) { return CommandResults.BadParameter; }
int id;
float size;
FontStyle style;
string family;
TemplateElement te;
TemplateHasFont tf;
try
{
size = float.Parse(cmdparameters[1]);
style = ((FontStyle) Int32.Parse(cmdparameters[2]));
family = UI.UnmarshalStringParameter(cmdparameters[3]);
id = Int32.Parse(cmdparameters[4]);
te = Program.CurrentProject.Elements[id];
tf = te as TemplateHasFont;
if (tf == null) { return CommandResults.BadParameter; }
if (!tppTemplatePanel.TemplateCard.ChildElements.Contains(te)) { return CommandResults.BadParameter; }
}
catch
{
return CommandResults.BadParameter;
}
StringBuilder undosb = new StringBuilder("Draw/Adjust/Font ");
undosb.Append(tf.Font.Size);
undosb.Append(" ");
undosb.Append((int) tf.Font.Style);
undosb.Append(" ");
undosb.Append( UI.MarshalStringParameter(tf.Font.FontFamily.Name) );
undosb.Append(" ");
undosb.Append(te.ID);
undocommand = undosb.ToString();
tf.Font = new Font(family, size, (FontStyle)style, GraphicsUnit.Point);
return CommandResults.Ok;
}
///
/// Draw/Adjust/FontColor sets the color of the font on a block. The block must implement TemplateHasFont
/// Parameters are R G B ID (int int int id). R,G,B are the color components in 0-255, and ID is the id of the block
///
protected CardStudio.CommandResults cmdDraw_Adjust_FontColor(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 5) { return CommandResults.BadParameter; }
int id;
TemplateElement te;
TemplateHasFont tf;
Color c;
try
{
c = Color.FromArgb( Int32.Parse(cmdparameters[1]), Int32.Parse(cmdparameters[2]), Int32.Parse(cmdparameters[3]));
id = Int32.Parse(cmdparameters[4]);
te = Program.CurrentProject.Elements[id];
tf = te as TemplateHasFont;
if (tf == null) { return CommandResults.BadParameter; }
if (!tppTemplatePanel.TemplateCard.ChildElements.Contains(te)) { return CommandResults.BadParameter; }
}
catch
{
return CommandResults.BadParameter;
}
StringBuilder undosb = new StringBuilder("Draw/Adjust/FontColor ");
undosb.Append(tf.Color.R);
undosb.Append(" ");
undosb.Append(tf.Color.G);
undosb.Append(" ");
undosb.Append(tf.Color.B);
undosb.Append(" ");
undosb.Append(te.ID);
undocommand = undosb.ToString();
tf.Color = c;
return CommandResults.Ok;
}
///
/// Draw/Adjust/Text sets the fixed text value on a block. The block must implement TemplateHasText
/// Parameters are Text ID (string id). Text is the new string, and ID is the id of the block
///
protected CardStudio.CommandResults cmdDraw_Adjust_Text(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 3) { return CommandResults.BadParameter; }
int id;
TemplateElement te;
TemplateHasText tt;
string text;
try
{
text = UI.UnmarshalStringParameter(cmdparameters[1]);
id = Int32.Parse(cmdparameters[2]);
te = Program.CurrentProject.Elements[id];
tt = te as TemplateHasText;
if (tt == null) { return CommandResults.BadParameter; }
if (!tppTemplatePanel.TemplateCard.ChildElements.Contains(te)) { return CommandResults.BadParameter; }
}
catch
{
return CommandResults.BadParameter;
}
StringBuilder undosb = new StringBuilder("Draw/Adjust/Text ");
undosb.Append(UI.MarshalStringParameter(tt.Text));
undosb.Append(" ");
undosb.Append(te.ID);
undocommand = undosb.ToString();
tt.Text = text;
return CommandResults.Ok;
}
#endregion Draw/Adjust/*
#region Draw/Delete & Draw/Undelete
///
/// Draw/Delete removes the named blocks
/// Parameters are Name1 Name2 Name3..., the names of the elements to delete, minimum 1
/// Deleted blocks are kept in a special deleted collection attached to the CardTemplate, for undo purposes
/// Deleted blocks are not saved with the template.
///
protected CardStudio.CommandResults cmdDraw_Delete(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 2) { return CommandResults.MissingCommand; }
TemplateElement te;
int id;
StringBuilder undosb = new StringBuilder("Draw/Undelete");
for (int i = 1; i < cmdparameters.Length; i++)
{
try
{
id = Int32.Parse(cmdparameters[i]);
te = Program.CurrentProject.Elements[id];
}
catch (Exception) { continue; }
if (te == null) { continue; }
if (!tppTemplatePanel.TemplateCard.ChildElements.Contains(te)) { continue; }
Program.ConsoleForm.LineOut(" [deleting #" + te.ID + "]");
tppTemplatePanel.TemplateCard.RemoveChild(te);
Program.CurrentProject.DeletedElements.Add(id, te);
Program.CurrentProject.Elements.Remove(id);
undosb.Append(" ");
undosb.Append(id.ToString());
}
undocommand = undosb.ToString();
return CommandResults.Ok;
}
///
/// Draw/Undelete restores deleted blocks that are still cached in the CardTemplate
/// Parameters are Name1 Name2 Name3..., the names of the elements to restore, minimum 1
/// Deleted blocks are kept in a special deleted collection attached to the CardTemplate, for undo purposes
/// Deleted blocks are not saved with the template.
///
protected CardStudio.CommandResults cmdDraw_Undelete(string[] cmdparameters, out string undocommand, out string redocommand)
{
undocommand = null; redocommand = null;
if (cmdparameters.Length < 2) { return CommandResults.MissingCommand; }
TemplateElement te;
int id;
StringBuilder undosb = new StringBuilder("Draw/Delete");
for (int i = 1; i < cmdparameters.Length; i++)
{
try {
id = Int32.Parse( cmdparameters[i] );
} catch (Exception) { continue; }
if (!Program.CurrentProject.DeletedElements.ContainsKey(id)) { continue; }
te = Program.CurrentProject.DeletedElements[id];
Program.CurrentProject.DeletedElements.Remove(id);
Program.CurrentProject.Elements.Add(id, te);
tppTemplatePanel.TemplateCard.ReinsertChild(te);
undosb.Append(" ");
undosb.Append(te.ID.ToString());
}
undocommand = undosb.ToString();
return CommandResults.Ok;
}
#endregion Draw/Delete & Draw/Undelete
}
}