/// <summary>
 ///  Adds an image adornment to the repository
 /// </summary>
 /// <param name="imageAdornment"></param>
 internal void Add(ImageAdornment imageAdornment)
 {
     if (imageAdornment != null && !this.images.Contains(imageAdornment))
     {
         this.images.Add(imageAdornment);
     }
 }
        /// <summary>
        /// Creates a new instance of <see cref="ImageAdornmentInfo"/> based on the image adornment.
        /// </summary>
        /// <param name="imageAdornment"></param>
        internal ImageAdornmentInfo(ImageAdornment imageAdornment)
        {
            this.Id = imageAdornment.Id;
            this.TextViewLineDelta = imageAdornment.TextViewLineDelta;
            this.Span = imageAdornment.TrackingSpan.GetSpan(imageAdornment.TrackingSpan.TextBuffer.CurrentSnapshot).Span;
            this.Bitmap = imageAdornment.VisualElement.Image.Tag as System.Drawing.Bitmap;

            this.Area = new Rect(
                imageAdornment.VisualElement.Left,
                imageAdornment.VisualElement.Top,
                imageAdornment.VisualElement.Width,
                imageAdornment.VisualElement.Height
                );
        }
        private void UpdateTargetLocation(ImageAdornment imageAdornment)
        {
            imageAdornment.RenderTrackingPoint = null;

            foreach (ITextViewLine line in View.TextViewLines)
            {
                var lineArea = new Rect(line.Left, line.Top, line.Width, line.Height);
                Rect imageAdornmentArea = imageAdornment.VisualElement.Area;
                // Use the height half to be able to move the image up and down
                imageAdornmentArea.Height = imageAdornmentArea.Height/2;

                if (line.Length > 0 && lineArea.IntersectsWith(imageAdornmentArea))
                {
                    imageAdornment.RenderTrackingPoint = View.TextSnapshot.CreateTrackingPoint(line.Start.Position,
                                                                                               PointTrackingMode.Negative);
                    imageAdornment.UpdateTrackingSpan(line);

                    return;
                }
            }
        }
        private void Show(ImageAdornment imageAdornment, ITextViewLine targetTextViewLine)
        {
            if (targetTextViewLine != null)
            {
                // Update the line delta
                imageAdornment.TextViewLineDelta = new Point(imageAdornment.VisualElement.Left - targetTextViewLine.Left,
                                                             imageAdornment.VisualElement.Top - targetTextViewLine.Top);
            }

            AdornmentLayer.RemoveAdornmentsByTag(imageAdornment);
            AdornmentLayer.AddAdornment(AdornmentPositioningBehavior.TextRelative,
                                        imageAdornment.TrackingSpan.GetSpan(View.TextSnapshot), imageAdornment,
                                        imageAdornment.VisualElement, null);

            UpdateTargetLocation(imageAdornment);
        }
 private void Show(ImageAdornment imageAdornment)
 {
     Show(imageAdornment, GetTargetTextViewLine(imageAdornment));
 }
 private void RemoveImageAdornment(ImageAdornment imageAdornment)
 {
     ImagesAdornmentsRepository.Remove(imageAdornment);
     AdornmentLayer.RemoveAdornment(imageAdornment.VisualElement);
 }
 private void InitializeImageAdornment(ImageAdornment imageAdornment)
 {
     imageAdornment.VisualElement.MouseMove += OnAdornmentVisualElementMouseMove;
     imageAdornment.VisualElement.MouseLeftButtonDown += OnAdornmentVisualElementMouseLeftButtonDown;
     imageAdornment.VisualElement.MouseLeftButtonUp += OnAdornmentVisualElementMouseLeftButtonUp;
     imageAdornment.VisualElement.MouseLeave += OnAdornmentVisualElementMouseLeave;
     imageAdornment.VisualElement.Deleted += OnAdornmentVisualElementDeleted;
     imageAdornment.VisualElement.Resizing += OnImageAdornmentResizing;
 }
        private void DisplayTextViewLine(ImageAdornment imageAdornment)
        {
            ITextViewLine textViewLine =
                View.TextViewLines.FirstOrDefault(line => imageAdornment.ApplyRenderTrackingPoint(View.TextSnapshot, line));

            if (textViewLine != null)
            {
                View.DisplayTextLineContainingBufferPosition(textViewLine.Start, textViewLine.Top, ViewRelativePosition.Top);
            }
            else
            {
                View.DisplayTextLineContainingBufferPosition(new SnapshotPoint(View.TextSnapshot, 0), 0.0, ViewRelativePosition.Top);
            }
        }
        internal ITextViewLine GetTargetTextViewLine(ImageAdornment imageAdornment)
        {
            if (View.TextViewLines == null)
                return null;

            return
                View.TextViewLines.GetTextViewLineContainingBufferPosition(
                    imageAdornment.TrackingSpan.GetStartPoint(View.TextSnapshot));
        }
        /// <summary>
        /// Creates and adds an image adornment for the image.
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        internal ImageAdornment AddImageAdornment(Image image)
        {
            ITextViewLine targetLine = GetTargetTextViewLine(image);

            if (targetLine != null && targetLine.Length > 0)
            {
                var imageAdornment = new ImageAdornment(
                    new SnapshotSpan(targetLine.Start, targetLine.Length),
                    image);

                // Initialize the image adornment
                InitializeImageAdornment(imageAdornment);

                // Add the image adornment to the repository
                ImagesAdornmentsRepository.Add(imageAdornment);
                ImagesAdornmentsRepository.EnsureRepositoryFileExists();

                // Add the repository file to the solution explorer
                AddFileToTheActiveDocument(ImagesAdornmentsRepository.RepositoryFilename);

                // Show the image
                Show(imageAdornment);

                DisplayTextViewLine(imageAdornment);

                return imageAdornment;
            }

            return null;
        }
        internal void Load()
        {
            if (File.Exists(this.RepositoryFilename))
            {
                using (IResourceReader reader = new ResourceReader(this.RepositoryFilename))
                {
                    try
                    {
                        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(OnCurrentAppDomainAssemblyResolve);

                        foreach (DictionaryEntry entry in reader)
                        {
                            ImageAdornmentInfo info = entry.Value as ImageAdornmentInfo;
                            // Convert the bitmap
                            ImageSource imageSource = GetImageSourceFromBitmap(info.Bitmap);

                            ImageAdornment imageAdornment = new ImageAdornment(
                                this.textBuffer.CurrentSnapshot, info, imageSource);

                            Add(imageAdornment);
                        }
                    }
                    finally
                    {
                        AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(OnCurrentAppDomainAssemblyResolve);
                    }
                }
            }
        }
 /// <summary>
 /// Removes an image adornement form the repository
 /// </summary>
 /// <param name="imageAdornment"></param>
 internal void Remove(ImageAdornment imageAdornment)
 {
     if (imageAdornment != null && this.images.Contains(imageAdornment))
     {
         this.images.Remove(imageAdornment);
     }
 }