using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using System .IO;
using Microsoft .Win32;
using MoveGraphLibrary;
namespace UserDrivenApplications
{
public class ButtonsSketch : GraphicalObject
{
int version = 606;
int idCode;
bool bSelected;
Rectangle rc, rcSelect;
int nBtns;
int [] places;
Point [] ptBtn;
int w = 16; // sizes for the "buttons"
int h = 8;
int spaceInside = 4; // spaces between "buttons"
int spaceOnBorders = 6; // spaces on the sides
int sideSelectSquare = 14;
SolidBrush brushBtns;
Pen penFrame = new Pen (SystemColors .Highlight);
// -------------------------------------------------
public ButtonsSketch (int nView, bool bSelect, Point pt, int [] placement, SolidBrush brushButtons)
{
idCode = nView;
bSelected = bSelect;
int cxL = pt .X;
int cyT = pt .Y;
int cxR = cxL;
int cyB = cyT;
nBtns = placement .Length;
places = placement;
ptBtn = new Point [nBtns];
int iRow, jCol;
int maxPlacement = nBtns * nBtns - 1;
for (int i = 0; i < nBtns; i++)
{
places [i] = Math .Min (Math .Max (0, places [i]), maxPlacement);
iRow = places [i] / nBtns;
jCol = places [i] % nBtns;
ptBtn [i] = new Point (cxL + spaceOnBorders + jCol * (w + spaceInside), cyT + spaceOnBorders + iRow * (h + spaceInside));
cxR = Math .Max (cxR, ptBtn [i] .X + w + spaceOnBorders);
cyB = Math .Max (cyB, ptBtn [i] .Y + h + spaceOnBorders);
}
rc = Rectangle .FromLTRB (cxL, cyT, cxR, cyB);
rc .Width += sideSelectSquare + 4;
rcSelect = new Rectangle (rc .Right - (sideSelectSquare + 4), rc .Top + 4, sideSelectSquare, sideSelectSquare);
brushBtns = brushButtons;
}
// ------------------------------------------------- Code
public int Code
{
get { return (idCode); }
}
// ------------------------------------------------- Selected
public bool Selected
{
get { return (bSelected); }
set { bSelected = value; }
}
// ------------------------------------------------- RectAround
new public Rectangle RectAround
{
get { return (rc); }
}
// ------------------------------------------------- Places
public int [] Places
{
get { return (places); }
}
// ------------------------------------------------- Draw
public void Draw (Graphics grfx)
{
Pen penConnect = new Pen (brushBtns .Color, 2);
Auxi_Drawing .CurvedFrame (grfx, rc, penFrame);
for (int i = 0; i < ptBtn .Length; i++)
{
grfx .FillRectangle (brushBtns, ptBtn [i] .X, ptBtn [i] .Y, w, h);
grfx .DrawRectangle (Pens .DarkGray, ptBtn [i] .X, ptBtn [i] .Y, w, h);
if (i > 0)
{
if (ptBtn [i] .X == ptBtn [i - 1] .X || ptBtn [i] .Y == ptBtn [i - 1] .Y)
{
grfx .DrawLine (penConnect, ptBtn [i] .X + w / 2, ptBtn [i] .Y + h / 2,
ptBtn [i - 1] .X + w / 2, ptBtn [i - 1] .Y + h / 2);
}
}
}
ControlPaint .DrawCheckBox (grfx, rcSelect, bSelected ? ButtonState .Checked : ButtonState .Normal);
}
// ------------------------------------------------- DefineCover
public override void DefineCover ()
{
CoverNode [] nodes = new CoverNode [] { new CoverNode (0, rcSelect, Cursors .Hand), new CoverNode (1, rc, Cursors .SizeAll) };
cover = new Cover (nodes);
}
// ------------------------------------------------- Move
public override void Move (int dx, int dy)
{
rc .X += dx;
rc .Y += dy;
for (int i = 0; i < ptBtn .Length; i++)
{
ptBtn [i] .X += dx;
ptBtn [i] .Y += dy;
}
rcSelect .X += dx;
rcSelect .Y += dy;
}
// ------------------------------------------------- MoveNode
public override bool MoveNode (int i, int dx, int dy, Point ptM, MouseButtons catcher)
{
bool bRet = false;
if (catcher == MouseButtons .Left)
{
if (i == 1)
{
Move (dx, dy);
bRet = true;
}
}
return (bRet);
}
const string nameMain = "BtnsSketch_";
const string namePlaces = "BtnsSketchPlaces_";
// ------------------------------------------------- IntoRegistry
public void IntoRegistry (RegistryKey regkey, string strA)
{
try
{
Color clr = brushBtns .Color;
regkey .SetValue (nameMain + strA, new string [] {version .ToString (), // 0
idCode .ToString (), // 1
bSelected .ToString (), // 2
rc .Left .ToString (), // 3
rc .Top .ToString (), // 4
nBtns .ToString (), // 5
((int) (clr .A)) .ToString (), // 6
((int) (clr .R)) .ToString (), // 7
((int) (clr .G)) .ToString (), // 8
((int) (clr .B)) .ToString (), // 9
},
RegistryValueKind .MultiString);
string [] strsPl = new string [nBtns];
for (int i = 0; i < nBtns; i++)
{
strsPl [i] = places [i] .ToString ();
}
regkey .SetValue (namePlaces + strA, strsPl, RegistryValueKind .MultiString);
}
catch
{
}
finally
{
}
}
// ------------------------------------------------- FromRegistry
public static ButtonsSketch FromRegistry (RegistryKey regkey, string strA)
{
try
{
string [] strs = (string []) regkey .GetValue (nameMain + strA);
string [] strsPl = (string []) regkey .GetValue (namePlaces + strA);
if (strs != null && strs .Length == 10 && Convert .ToInt32 (strs [0]) == 606 &&
strsPl != null && strsPl .Length == Convert .ToInt32 (strs [5]))
{
int nButtons = strsPl .Length;
int [] placement = new int [nButtons];
for (int i = 0; i < nButtons; i++)
{
placement [i] = Convert .ToInt32 (strsPl [i]);
}
ButtonsSketch sketch = new ButtonsSketch (Convert .ToInt32 (strs [1]), // code
Convert .ToBoolean (strs [2]), // bSelect
Auxi_Convert .ToPoint (strs, 3), // pt
placement,
new SolidBrush (Auxi_Convert .ToColor (strs, 6)));
return (sketch);
}
else
{
return (null);
}
}
catch
{
return (null);
}
finally
{
}
}
// ------------------------------------------------- IntoFile
public void IntoFile (BinaryWriter bw)
{
try
{
bw .Write (version); // 0
bw .Write (idCode); // 1
bw .Write (bSelected); // 2
Auxi_IO .Write_Point (bw, rc .Location); // 3, 4
bw .Write (nBtns); // 5
Auxi_IO .Write_Color (bw, brushBtns .Color); // 6 - 9
for (int i = 0; i < nBtns; i++)
{
bw .Write (places [i]);
}
}
catch
{
}
finally
{
}
}
// ------------------------------------------------- FromFile
public static ButtonsSketch FromFile (Form form, BinaryReader br)
{
try
{
int ver = br .ReadInt32 (); // 0
if (ver != 606)
{
return (null);
}
int code = br .ReadInt32 (); // 1
bool bSelect = br .ReadBoolean (); // 2
Point pt = Auxi_IO .Read_Point (br); // 3, 4
int nButtons = br .ReadInt32 (); // 5
SolidBrush brushButtons = new SolidBrush (Auxi_IO .Read_Color (br)); // 6 - 9
int [] placement = new int [nButtons];
for (int i = 0; i < nButtons; i++)
{
placement [i] = br .ReadInt32 ();
}
ButtonsSketch sketch = new ButtonsSketch (code, bSelect, pt, placement, brushButtons);
return (sketch);
}
catch
{
return (null);
}
finally
{
}
}
}
}