private void imageItem_Copy(object sender, RoutedEventArgs e)
        {
            GraphicalItem v = (GraphicalItem)((MenuItem)sender).DataContext;

            if (v.BmpImg != null)
            {
                Clipboard.SetImage(v.BmpImg);
            }
        }
 private void ResetAt(GraphicalItem item, int index)
 {
     ((System.ComponentModel.INotifyPropertyChanged)item).PropertyChanged += GraphicalItem_NameChanged;
     if (index < Variables.Count)
     {
         Variables.RemoveAt(index);
     }
     Variables.Insert(index, item);
 }
        private void imageItem_Reset(object sender, RoutedEventArgs e)
        {
            GraphicalItem v = (GraphicalItem)((MenuItem)sender).DataContext;

            if (v.BmpImg != null)
            {
                int i = dataGrid.Items.IndexOf(v);
                UpdateItem(false, i);
            }
        }
        private void GraphicalItem_NameChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //e.PropertyName == "Name"

            GraphicalItem variable = (GraphicalItem)sender;
            int           index    = Variables.IndexOf(variable);

            if (index < 0 || index >= dataGrid.Items.Count)
            {
                return;
            }

            if (variable.Name == null || variable.Name == "")
            {
                if (index < dataGrid.Items.Count - 1)
                {
                    Variables.RemoveAt(index);
                    if (index > 0)
                    {
                        Util.SelectDataGridItem(dataGrid, index - 1);
                    }
                }
            }
            else
            {
                UpdateItem(true, index);

                int next_index = index + 1;
                // insert new empty row if needed
                if (next_index == Variables.Count)
                {
                    ResetAt(new GraphicalItem(), next_index);
                }
                // select current row, move to next one is automatic
                Util.SelectDataGridItem(dataGrid, index);
            }
        }
        private void UpdateItem(bool load, int index)
        {
            GraphicalItem variable = Variables[index];

            Bitmap bmp  = null;
            string type = null;

            if (ExpressionLoader.Debugger.CurrentMode == dbgDebugMode.dbgBreakMode)
            {
                // Empty color - use default
                ExpressionDrawer.Settings settings = new ExpressionDrawer.Settings();
                settings.densify    = true;
                settings.showDir    = false;
                settings.showLabels = false;
                settings.showDots   = false;
                // Other settings
                int  imageWidth  = 100;
                int  imageHeight = 100;
                bool displayMultiPointsAsPlots = false;
                // Load settings from option page
                GraphicalWatchOptionPage optionPage = Util.GetDialogPage <GraphicalWatchOptionPage>();
                if (optionPage != null)
                {
                    if (optionPage.ValuePlot_EnableBars || optionPage.ValuePlot_EnableLines || optionPage.ValuePlot_EnablePoints)
                    {
                        settings.valuePlot_enableBars   = optionPage.ValuePlot_EnableBars;
                        settings.valuePlot_enableLines  = optionPage.ValuePlot_EnableLines;
                        settings.valuePlot_enablePoints = optionPage.ValuePlot_EnablePoints;
                    }
                    if (optionPage.PointPlot_EnableLines || optionPage.PointPlot_EnablePoints)
                    {
                        settings.pointPlot_enableLines  = optionPage.PointPlot_EnableLines;
                        settings.pointPlot_enablePoints = optionPage.PointPlot_EnablePoints;
                    }
                    settings.densify          = optionPage.Densify;
                    settings.showDir          = optionPage.EnableDirections;
                    settings.showLabels       = optionPage.EnableLabels;
                    settings.showDots         = false;
                    imageHeight               = Math.Max(optionPage.ImageHeight, 20);
                    imageWidth                = Math.Max(optionPage.ImageWidth, 20);
                    displayMultiPointsAsPlots = optionPage.MultiPointDisplayMode == GraphicalWatchOptionPage.MultiPointDisplayModeValue.PointPlot;
                }

                (dataGrid.Columns[1] as DataGridTemplateColumn).Width = imageWidth + 1;
                dataGrid.RowHeight = imageHeight + 1;

                if (load)
                {
                    variable.Drawable = null;
                    variable.Traits   = null;
                }

                if (variable.Name != null && variable.Name != "")
                {
                    var expression = ExpressionLoader.Debugger.GetExpression(variable.Name);
                    if (expression.IsValidValue)
                    {
                        // create bitmap
                        bmp = new Bitmap(imageWidth, imageHeight);

                        Graphics graphics = Graphics.FromImage(bmp);
                        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        graphics.Clear(m_colors.ClearColor);

                        try
                        {
                            if (variable.Drawable == null)
                            {
                                Geometry.Traits            traits   = null;
                                ExpressionDrawer.IDrawable drawable = null;
                                ExpressionLoader.Load(variable.Name, out traits, out drawable);

                                if (drawable != null && displayMultiPointsAsPlots && drawable is ExpressionDrawer.MultiPoint)
                                {
                                    drawable = new ExpressionDrawer.PointsContainer(drawable as ExpressionDrawer.MultiPoint);
                                }

                                variable.Drawable = drawable;
                                variable.Traits   = traits;
                            }
                            else
                            {
                                if (displayMultiPointsAsPlots && variable.Drawable is ExpressionDrawer.MultiPoint)
                                {
                                    variable.Drawable = new ExpressionDrawer.PointsContainer(variable.Drawable as ExpressionDrawer.MultiPoint);
                                }
                                else if (!displayMultiPointsAsPlots && variable.Drawable is ExpressionDrawer.PointsContainer)
                                {
                                    variable.Drawable = (variable.Drawable as ExpressionDrawer.PointsContainer).MultiPoint;
                                }
                            }

                            if (!ExpressionDrawer.Draw(graphics, variable.Drawable, variable.Traits, settings, m_colors))
                            {
                                bmp = null;
                            }
                        }
                        catch (Exception)
                        {
                            bmp = null;
                        }

                        type = expression.Type;
                    }
                }
            }

            // set new row
            ResetAt(new GraphicalItem(variable.Drawable, variable.Traits, variable.Name, bmp, type), index);
        }
        private void UpdateItem(bool load, int index)
        {
            GraphicalItem variable = Variables[index];

            if (ExpressionLoader.IsBreakMode)
            {
                if (load)
                {
                    ExpressionLoader.ReloadUserTypes(Util.GetDialogPage <GeneralOptionPage>());
                }

                // Load settings from option page
                ExpressionDrawer.Settings settings = GetOptions();

                (dataGrid.Columns[1] as DataGridTemplateColumn).Width = settings.imageWidth + 1;
                dataGrid.RowHeight = settings.imageHeight + 1;

                if (load)
                {
                    variable.Drawable = null;
                    variable.Traits   = null;
                    variable.Bmp      = null;
                    variable.Type     = null;
                    variable.Error    = null;
                }

                if (variable.Name != null && variable.Name != "")
                {
                    var expressions = ExpressionLoader.GetExpressions(variable.Name);
                    if (ExpressionLoader.AllValidValues(expressions))
                    {
                        // create bitmap
                        variable.Bmp = new Bitmap(settings.imageWidth, settings.imageHeight);

                        Graphics graphics = Graphics.FromImage(variable.Bmp);
                        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                        graphics.Clear(Util.Colors.ClearColor);

                        try
                        {
                            if (variable.Drawable == null)
                            {
                                Geometry.Traits            traits   = null;
                                ExpressionDrawer.IDrawable drawable = null;
                                ExpressionLoader.Load(variable.Name, out traits, out drawable);

                                if (drawable != null &&
                                    settings.displayMultiPointsAsPlots &&
                                    drawable is ExpressionDrawer.MultiPoint)
                                {
                                    drawable = new ExpressionDrawer.PointsContainer(drawable as ExpressionDrawer.MultiPoint);
                                }

                                variable.Drawable = drawable;
                                variable.Traits   = traits;
                            }
                            else
                            {
                                if (settings.displayMultiPointsAsPlots &&
                                    variable.Drawable is ExpressionDrawer.MultiPoint)
                                {
                                    variable.Drawable = new ExpressionDrawer.PointsContainer(variable.Drawable as ExpressionDrawer.MultiPoint);
                                }
                                else if (!settings.displayMultiPointsAsPlots &&
                                         variable.Drawable is ExpressionDrawer.PointsContainer)
                                {
                                    variable.Drawable = (variable.Drawable as ExpressionDrawer.PointsContainer).MultiPoint;
                                }
                            }

                            if (!ExpressionDrawer.Draw(graphics, variable.Drawable, variable.Traits, settings, Util.Colors))
                            {
                                variable.Bmp = null;
                            }

                            variable.Error = null;
                        }
                        catch (Exception e)
                        {
                            variable.Bmp = null;

                            variable.Error = e.Message;
                        }

                        variable.Type = ExpressionLoader.TypeFromExpressions(expressions);
                    }
                }
            }

            // set new row
            ResetAt(variable.ShallowCopy(), index);
        }