using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;
using MoveGraphLibrary;
namespace UserDrivenApplications
{
public partial class Form_FuncXrYr : Form
{
int version = 605;
const string strAddRegKey = "Form_FuncXrYr";
FunctionDesigned funcSrc;
Mover mover;
Point ptMouse_Down, ptMouse_Up;
ElasticGroup groupLanguage, groupFunction, groupPressed;
MSPlot msplot;
Scale scalePressed;
List<Elem> Polish_Xr = new List<Elem> ();
List<Elem> Polish_Yr = new List<Elem> ();
bool bFuncCorrectAndReady = false;
string strName, strTextX, strTextY;
double fFrom, fTo, fStep;
bool bRestore;
bool bClearRegistry = false;
bool bAddResult = true;
Point ptMouseInit;
// -------------------------------------------------
public Form_FuncXrYr (FunctionDesigned func, Point ptMouseScreen)
{
InitializeComponent ();
funcSrc = func;
ptMouseInit = ptMouseScreen;
mover = new Mover (this);
mover .Clipping = Clipping .Safe;
}
// ------------------------------------------------- OnFormClosing
private void OnLoad (object sender, EventArgs e)
{
if (ptMouseInit != Point .Empty)
{
Location = ptMouseInit;
}
RestoreFromRegistry ();
if (!bRestore)
{
msplot = new MSPlot (this, new Rectangle (new Point (40, 20), new Size (ClientRectangle .Width - 60, textX .Top - 60)));
groupFunction = new ElasticGroup (this,
new ElasticGroupElement [] {
new ElasticGroupElement (new CommentedControl (this, textX, Resizing .WE, Side .W, "X(r)")),
new ElasticGroupElement (labelErrorXR),
new ElasticGroupElement (new CommentedControl (this, textY, Resizing .WE, Side .W, "Y(r)")),
new ElasticGroupElement (labelErrorYR),
new ElasticGroupElement (this, new CommentedControl [] {
new CommentedControl (this, textFrom, Resizing .WE, Side .E, "From"),
new CommentedControl (this, textTo, Resizing .WE, Side .E, "To"),
new CommentedControl (this, textStep, Resizing .WE, Side .E, "Step"),
}, "Parameter"),
new ElasticGroupElement (btnShowFunc),
new ElasticGroupElement (btnAddFunc),
new ElasticGroupElement (btnChangeFunc),
new ElasticGroupElement (new CommentedControl (this, textName, Resizing .WE, Side .W, "Name"))},
"Function");
CommentedControl ccOper = new CommentedControl (this, labelOperations, Side .W, "Operations");
CommentedControl ccFunc = new CommentedControl (this, labelFunctions, Side .W, "Functions");
int cxL = Math .Min (ccOper .Comment .RectAround .Left, ccFunc .Comment .RectAround .Left);
ccOper .Comment .Location = new Point (cxL + ccOper .Comment .Width / 2, ccOper .Comment .Location .Y);
ccFunc .Comment .Location = new Point (cxL + ccFunc .Comment .Width / 2, ccFunc .Comment .Location .Y);
groupLanguage = new ElasticGroup (this, new CommentedControl [] { ccOper, ccFunc }, "Language");
}
if (funcSrc == null)
{
btnChangeFunc .Enabled = false;
}
else
{
textName .Text = funcSrc .Name;
textX .Text = funcSrc .XText;
textY .Text = funcSrc .YText;
textFrom .Text = funcSrc .From .ToString ();
textTo .Text = funcSrc .To .ToString ();
textStep .Text = funcSrc .Step .ToString ();
msplot .CopyView (funcSrc .MSPlot);
CheckFunctionXYR ();
}
RenewMover ();
}
// ------------------------------------------------- OnFormClosing
private void OnFormClosing (object sender, FormClosingEventArgs e)
{
SaveInfoToRegistry ();
}
// ------------------------------------------------- RenewMover
private void RenewMover ()
{
mover .Clear ();
groupLanguage .IntoMover (mover, 0);
groupFunction .IntoMover (mover, 0);
msplot .IntoMover (mover, mover .Count);
}
// ------------------------------------------------- OnPaint
private void OnPaint (object sender, PaintEventArgs e)
{
Graphics grfx = e .Graphics;
msplot .Draw (grfx);
if (Polish_Xr .Count > 0 && Polish_Yr .Count > 0)
{
MSPlotAuxi auxi = new MSPlotAuxi (0, SegmentLocation .Partly_Inside);
auxi .ParamTrio (fFrom, fTo, fStep);
msplot .DrawParamFunc (grfx, auxi, Polish_Xr, Polish_Yr);
}
groupLanguage .Draw (grfx);
groupFunction .Draw (grfx);
}
// ------------------------------------------------- OnMouseDown
private void OnMouseDown (object sender, MouseEventArgs e)
{
ptMouse_Down = e .Location;
mover .Catch (e .Location, e .Button);
ContextMenuStrip = null;
}
// ------------------------------------------------- OnMouseUp
private void OnMouseUp (object sender, MouseEventArgs e)
{
ptMouse_Up = e .Location;
double nDist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);
if (e .Button == MouseButtons .Left)
{
mover .Release ();
}
else if (e .Button == MouseButtons .Right)
{
if (mover .Release ())
{
if (nDist <= 3)
{
MenuSelection (mover .WasCaughtObject);
}
}
else
{
if (nDist <= 3)
{
ContextMenuStrip = menuOnEmpty;
}
}
}
}
// ------------------------------------------------- MenuSelection
private void MenuSelection (int iInMover)
{
GraphicalObject grobj = mover [iInMover] .Source;
if (grobj is MSPlot)
{
ContextMenuStrip = menuOnPlot;
}
else if (grobj is Scale)
{
scalePressed = grobj as Scale;
ContextMenuStrip = menuOnScale;
}
else if (grobj is ElasticGroup)
{
groupPressed = grobj as ElasticGroup;
ContextMenuStrip = menuOnGroup;
}
}
// ------------------------------------------------- OnMouseMove
private void OnMouseMove (object sender, MouseEventArgs e)
{
if (mover .Move (e .Location))
{
if (mover .CaughtSource is ElasticGroup || mover .CaughtSource is FramedControl || mover .CaughtSource is CommentedControl)
{
Update ();
}
Invalidate ();
}
}
// ------------------------------------------------- OnContextMenuChanged
private void OnContextMenuChanged (object sender, EventArgs e)
{
if (ContextMenuStrip != null)
{
ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
}
}
// ------------------------------------------------- OnMouseDoubleClick
private void OnMouseDoubleClick (object sender, MouseEventArgs e)
{
if (mover .Catch (e .Location, MouseButtons .Left))
{
if (mover .CaughtSource is Scale)
{
(mover .CaughtSource as Scale) .ParametersDialog (this, RenewMover, ParametersChanged);
}
}
}
// ------------------------------------------------- ParametersChanged
private void ParametersChanged (object sender, EventArgs ea)
{
Invalidate ();
}
// ***** menuOnGroup *****
// ------------------------------------------------- Opening_menuOnGroup
private void Opening_menuOnGroup (object sender, CancelEventArgs e)
{
menuOnGroup .Items ["miFixUnfixElements"] .Text = groupPressed .ElementsMovable ? "Fix group's elements" : "Unfix group's elements";
}
// ------------------------------------------------- Click_miModifyGroup
private void Click_miModifyGroup (object sender, EventArgs e)
{
groupPressed .ParametersDialog (this, PointToScreen (ptMouse_Up), GroupParametersChanged);
}
// ------------------------------------------------- GroupParametersChanged
private void GroupParametersChanged (object sender, EventArgs ea)
{
if (groupPressed .ParentID != 0)
{
groupFunction .Update ();
}
else
{
groupPressed .Update ();
}
Invalidate ();
}
// ------------------------------------------------- Click_miFixUnfixElements
private void Click_miFixUnfixElements (object sender, EventArgs e)
{
groupPressed .ElementsMovable = !groupPressed .ElementsMovable;
RenewMover ();
}
// ------------------------------------------------- Click_miGroupDefaultView
private void Click_miGroupDefaultView (object sender, EventArgs e)
{
groupPressed .DefaultView ();
groupPressed .Update ();
RenewMover ();
Invalidate ();
}
// ***** menuOnPlot *****
// ------------------------------------------------- Click_miModifyHorScale
private void Click_miModifyHorScale (object sender, EventArgs e)
{
msplot .HorScales [0] .ParametersDialog (this, RenewMover, ParametersChanged);
}
// ------------------------------------------------- Click_miModifyVerScale
private void Click_miModifyVerScale (object sender, EventArgs e)
{
msplot .VerScales [0] .ParametersDialog (this, RenewMover, ParametersChanged);
}
// ***** menuOnScale *****
// ------------------------------------------------- Click_miModifyScale
private void Click_miModifyScale (object sender, EventArgs e)
{
scalePressed .ParametersDialog (this, RenewMover, ParametersChanged);
}
// ------------------------------------------------- Click_miFlipScale
private void Click_miFlipScale (object sender, EventArgs e)
{
scalePressed .Flip ();
Invalidate ();
}
// ***** menuOnEmpty *****
// ------------------------------------------------- Click_miDefaultView
private void Click_miDefaultView (object sender, EventArgs e)
{
bClearRegistry = true;
DialogResult = DialogResult .Retry;
Close ();
}
// ------------------------------------------------- Click_btnShowFunc
private void Click_btnShowFunc (object sender, EventArgs e)
{
if (!CheckFunctionXYR ()) return;
Invalidate ();
}
// ------------------------------------------------- CheckFunctionXYR
private bool CheckFunctionXYR ()
{
labelErrorXR .Text = "";
labelErrorYR .Text = "";
if (!double .TryParse (textFrom .Text .Trim (), out fFrom))
{
MessageBox .Show ("Problem with the \"From\" parameter", "Parameter", MessageBoxButtons .OK, MessageBoxIcon .Error);
return (false);
}
if (!double .TryParse (textTo .Text .Trim (), out fTo))
{
MessageBox .Show ("Problem with the \"To\" parameter", "Parameter", MessageBoxButtons .OK, MessageBoxIcon .Error);
return (false);
}
if (!double .TryParse (textStep .Text .Trim (), out fStep))
{
MessageBox .Show ("Problem with the \"Step\" parameter", "Parameter", MessageBoxButtons .OK, MessageBoxIcon .Error);
return (false);
}
if (fFrom == fTo)
{
MessageBox .Show ("There is no range of parameter", "Parameter", MessageBoxButtons .OK, MessageBoxIcon .Error);
return (false);
}
if ((fFrom < fTo && fStep < 0) || (fFrom > fTo && fStep > 0))
{
MessageBox .Show ("With such range of parameter you need to change the sign of the Step",
"Parameter", MessageBoxButtons .OK, MessageBoxIcon .Error);
return (false);
}
bool bRet = true;
try
{
int nDot = (int) ((fTo - fFrom) / fStep) + 1;
if (nDot <= 0)
{
MessageBox .Show ("Problem with the parameter", "Parameter", MessageBoxButtons .OK, MessageBoxIcon .Warning);
return (false);
}
else if (nDot > 100000)
{
if (DialogResult .Yes != MessageBox .Show ("Number of values for calculation is > 100000. Do you want to continue?",
"Parameter", MessageBoxButtons .YesNo,
MessageBoxIcon .Question, MessageBoxDefaultButton .Button1))
{
return (false);
}
}
Polish_Xr = new List<Elem> ();
string strX = textX .Text .Trim ();
if (strX .Length <= 0)
{
labelErrorXR .Text = "X(r) text is not prepared";
return (false);
}
Polish_Yr = new List<Elem> ();
string strY = textY .Text .Trim ();
if (strY .Length <= 0)
{
labelErrorYR .Text = "Y(r) text is not prepared";
return (false);
}
int iErr, iErrPlace;
bool bCorrectFunc = FunctionInterpreter .Analyse (strX, ref Polish_Xr, out iErr, out iErrPlace);
if (!bCorrectFunc)
{
labelErrorXR .Text = FunctionInterpreter .GetErrorMessage (iErr);
if (iErrPlace >= 0)
{
textX .Select (iErrPlace, 1);
}
return (false);
}
bCorrectFunc = FunctionInterpreter .Analyse (strY, ref Polish_Yr, out iErr, out iErrPlace);
if (!bCorrectFunc)
{
labelErrorYR .Text = FunctionInterpreter .GetErrorMessage (iErr);
if (iErrPlace >= 0)
{
textY .Select (iErrPlace, 1);
}
return (false);
}
bRet = true;
}
catch
{
bRet = false;
}
return (bRet);
}
// ------------------------------------------------- Click_btnAdd
private void Click_btnAdd (object sender, EventArgs e)
{
if (false == CheckResult ())
{
return;
}
bAddResult = true;
Close ();
}
// ------------------------------------------------- Click_btnChange
private void Click_btnChange (object sender, EventArgs e)
{
if (false == CheckResult ())
{
return;
}
bAddResult = false;
Close ();
}
// ------------------------------------------------- CheckResult
private bool CheckResult ()
{
if (!CheckFunctionXYR ())
{
return (false);
}
strName = textName .Text;
strName = strName .Trim ();
if (string .IsNullOrEmpty (strName))
{
MessageBox .Show ("Function name is not defined", "Name", MessageBoxButtons .OK, MessageBoxIcon .Error);
return (false);
}
strTextX = textX .Text .Trim ();
strTextY = textY .Text .Trim ();
bFuncCorrectAndReady = true;
return (true);
}
// ------------------------------------------------- NewFunctionReady
public bool NewFunctionReady
{
get { return (bFuncCorrectAndReady); }
}
// ------------------------------------------------- NewFunctionName
public string NewFunctionName
{
get { return (strName); }
}
// ------------------------------------------------- NewFunctionTextX
public string NewFunctionTextX
{
get { return (strTextX); }
}
// ------------------------------------------------- NewFunctionTextY
public string NewFunctionTextY
{
get { return (strTextY); }
}
// ------------------------------------------------- NewPolishX
public List<Elem> NewPolishX
{
get { return (Polish_Xr); }
}
// ------------------------------------------------- NewPolishY
public List<Elem> NewPolishY
{
get { return (Polish_Yr); }
}
// ------------------------------------------------- From
public double From
{
get { return (fFrom); }
set { fFrom = value; }
}
// ------------------------------------------------- To
public double To
{
get { return (fTo); }
set { fTo = value; }
}
// ------------------------------------------------- Step
public double Step
{
get { return (fStep); }
set { fStep = value; }
}
// ------------------------------------------------- MSPlot
public MSPlot MSPlot
{
get { return (msplot); }
}
// ------------------------------------------------- AddResult
public bool AddResult
{
get { return (bAddResult); }
}
const string nameSize = "Size";
// ------------------------------------------------- SaveInfoToRegistry
private void SaveInfoToRegistry ()
{
string strRegKey = Form_Main .strRegKey + strAddRegKey;
RegistryKey regkey = null;
try
{
if (bClearRegistry)
{
Registry .CurrentUser .DeleteSubKey (strRegKey, false);
}
else
{
regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
if (regkey != null)
{
regkey .SetValue (nameSize, new string [] { version .ToString (), // 0
ClientSize .Width .ToString (), // 1
ClientSize .Height .ToString () // 2
},
RegistryValueKind .MultiString);
groupFunction .IntoRegistry (regkey, "GroupFunction");
groupLanguage .IntoRegistry (regkey, "GroupLanguage");
msplot .IntoRegistry (regkey, "Plot");
}
}
}
catch
{
}
finally
{
if (regkey != null) regkey .Close ();
}
}
// ------------------------------------------------- RestoreFromRegistry
private void RestoreFromRegistry ()
{
string strkey = Form_Main .strRegKey + strAddRegKey;
RegistryKey regkey = null;
try
{
bRestore = false;
regkey = Registry .CurrentUser .OpenSubKey (strkey);
if (regkey != null)
{
string [] strs = (string []) regkey .GetValue (nameSize);
if (strs != null && strs .Length == 3 && Convert .ToInt32 (strs [0]) == 605)
{
ClientSize = Auxi_Convert .ToSize (strs, 1);
}
else
{
return;
}
groupFunction = ElasticGroup .FromRegistry (this, regkey, "GroupFunction",
new Control [] {textX, labelErrorXR, textY, labelErrorYR,
textFrom, textTo, textStep,
btnShowFunc, btnAddFunc, btnChangeFunc, textName } );
groupLanguage = ElasticGroup .FromRegistry (this, regkey, "GroupLanguage",
new Control [] { labelOperations, labelFunctions } );
msplot = MSPlot .FromRegistry (this, regkey, "Plot");
if (groupFunction == null || groupLanguage == null || msplot == null)
{
return;
}
bRestore = true;
}
}
catch
{
}
finally
{
if (regkey != null) regkey .Close ();
}
}
}
}