コード例 #1
0
        /// <summary>
        /// Handle mouse up to handle selection and link click.
        /// </summary>
        /// <param name="parent">the control hosting the html to invalidate</param>
        /// <param name="leftMouseButton">is the left mouse button has been released</param>
        /// <returns>is the mouse up should be ignored</returns>
        public bool HandleMouseUp(RControl parent, bool leftMouseButton)
        {
            bool ignore = false;
            _mouseDownInControl = false;
            if (_root.HtmlContainer.IsSelectionEnabled)
            {
                ignore = _inSelection;
                if (!_inSelection && leftMouseButton && _mouseDownOnSelectedWord)
                {
                    ClearSelection();
                    parent.Invalidate();
                }

                _mouseDownOnSelectedWord = false;
                _inSelection = false;
            }
            ignore = ignore || (DateTime.Now - _lastMouseDown > TimeSpan.FromSeconds(1));
            return ignore;
        }
コード例 #2
0
 /// <summary>
 /// On mouse leave change the cursor back to default.
 /// </summary>
 /// <param name="parent">the control hosting the html to set cursor and invalidate</param>
 public void HandleMouseLeave(RControl parent)
 {
     if (_cursorChanged)
     {
         _cursorChanged = false;
         parent.SetCursorDefault();
     }
 }
コード例 #3
0
 /// <summary>
 /// Handle mouse move to handle hover cursor and text selection.
 /// </summary>
 /// <param name="parent">the control hosting the html to set cursor and invalidate</param>
 /// <param name="loc">the location of the mouse on the html</param>
 public void HandleMouseMove(RControl parent, RPoint loc)
 {
     if (_root.HtmlContainer.IsSelectionEnabled && _mouseDownInControl && parent.LeftMouseButton)
     {
         if (_mouseDownOnSelectedWord)
         {
             // make sure not to start drag-drop on click but when it actually moves as it f***s mouse-up
             if ((DateTime.Now - _lastMouseDown).TotalMilliseconds > 200)
                 StartDragDrop(parent);
         }
         else
         {
             HandleSelection(parent, loc, !_isDoubleClickSelect);
             _inSelection = _selectionStart != null && _selectionEnd != null && (_selectionStart != _selectionEnd || _selectionStartIndex != _selectionEndIndex);
         }
     }
     else
     {
         // Handle mouse hover over the html to change the cursor depending if hovering word, link of other.
         var link = DomUtils.GetLinkBox(_root, loc);
         if (link != null)
         {
             _cursorChanged = true;
             parent.SetCursorHand();
         }
         else if (_root.HtmlContainer.IsSelectionEnabled)
         {
             var word = DomUtils.GetCssBoxWord(_root, loc);
             _cursorChanged = word != null && !word.IsImage && !(word.Selected && (word.SelectedStartIndex < 0 || word.Left + word.SelectedStartOffset <= loc.X) && (word.SelectedEndOffset < 0 || word.Left + word.SelectedEndOffset >= loc.X));
             if (_cursorChanged)
                 parent.SetCursorIBeam();
             else
                 parent.SetCursorDefault();
         }
         else if (_cursorChanged)
         {
             parent.SetCursorDefault();
         }
     }
 }
コード例 #4
0
ファイル: RAdapter.cs プロジェクト: jcaillon/YamuiFramework
 /// <summary>
 /// Save the given image to file by showing save dialog to the client.<br/>
 /// Not relevant for platforms that don't render HTML on UI element.
 /// </summary>
 /// <param name="image">the image to save</param>
 /// <param name="name">the name of the image for save dialog</param>
 /// <param name="extension">the extension of the image for save dialog</param>
 /// <param name="control">optional: the control to show the dialog on</param>
 public void SaveToFile(RImage image, string name, string extension, RControl control = null)
 {
     SaveToFileInt(image, name, extension, control);
 }
コード例 #5
0
        /// <summary>
        /// Handle key down event for selection and copy.
        /// </summary>
        /// <param name="parent">the control hosting the html to invalidate</param>
        /// <param name="e">the pressed key</param>
        public void HandleKeyDown(RControl parent, RKeyEvent e)
        {
            ArgChecker.AssertArgNotNull(parent, "parent");
            ArgChecker.AssertArgNotNull(e, "e");

            try
            {
                if (e.Control && _selectionHandler != null)
                {
                    // select all
                    if (e.AKeyCode)
                    {
                        _selectionHandler.SelectAll(parent);
                    }

                    // copy currently selected text
                    if (e.CKeyCode)
                    {
                        _selectionHandler.CopySelectedHtml();
                    }
                }
            }
            catch (Exception ex)
            {
                ReportError(HtmlRenderErrorType.KeyboardMouse, "Failed key down handle", ex);
            }
        }
コード例 #6
0
        /// <summary>
        /// Calculate the character index and offset by characters for the given word and given offset.<br/>
        /// If the location is below the word line then set the selection to the end.<br/>
        /// If the location is to the right of the word then set the selection to the end.<br/>
        /// If the offset is to the left of the word set the selection to the beginning.<br/>
        /// Otherwise calculate the width of each substring to find the char the location is on.
        /// </summary>
        /// <param name="control">used to create graphics to measure string</param>
        /// <param name="word">the word to calculate its index and offset</param>
        /// <param name="loc">the location to calculate for</param>
        /// <param name="inclusive">is to include the first character in the calculation</param>
        /// <param name="selectionIndex">return the index of the char under the location</param>
        /// <param name="selectionOffset">return the offset of the char under the location</param>
        private static void CalculateWordCharIndexAndOffset(RControl control, CssRect word, RPoint loc, bool inclusive, out int selectionIndex, out double selectionOffset)
        {
            selectionIndex = 0;
            selectionOffset = 0f;
            var offset = loc.X - word.Left;
            if (word.Text == null)
            {
                // not a text word - set full selection
                selectionIndex = -1;
                selectionOffset = -1;
            }
            else if (offset > word.Width - word.OwnerBox.ActualWordSpacing || loc.Y > DomUtils.GetCssLineBoxByWord(word).LineBottom)
            {
                // mouse under the line, to the right of the word - set to the end of the word
                selectionIndex = word.Text.Length;
                selectionOffset = word.Width;
            }
            else if (offset > 0)
            {
                // calculate partial word selection
                int charFit;
                double charFitWidth;
                var maxWidth = offset + (inclusive ? 0 : 1.5f * word.LeftGlyphPadding);
                control.MeasureString(word.Text, word.OwnerBox.ActualFont, maxWidth, out charFit, out charFitWidth);

                selectionIndex = charFit;
                selectionOffset = charFitWidth;
            }
        }
コード例 #7
0
        /// <summary>
        /// Handle html text selection by mouse move over the html with left mouse button pressed.<br/>
        /// Calculate the words in the selected range and set their selected property.
        /// </summary>
        /// <param name="control">the control hosting the html to invalidate</param>
        /// <param name="loc">the mouse location</param>
        /// <param name="allowPartialSelect">true - partial word selection allowed, false - only full words selection</param>
        private void HandleSelection(RControl control, RPoint loc, bool allowPartialSelect)
        {
            // get the line under the mouse or nearest from the top
            var lineBox = DomUtils.GetCssLineBox(_root, loc);
            if (lineBox != null)
            {
                // get the word under the mouse
                var word = DomUtils.GetCssBoxWord(lineBox, loc);

                // if no word found under the mouse use the last or the first word in the line
                if (word == null && lineBox.Words.Count > 0)
                {
                    if (loc.Y > lineBox.LineBottom)
                    {
                        // under the line
                        word = lineBox.Words[lineBox.Words.Count - 1];
                    }
                    else if (loc.X < lineBox.Words[0].Left)
                    {
                        // before the line
                        word = lineBox.Words[0];
                    }
                    else if (loc.X > lineBox.Words[lineBox.Words.Count - 1].Right)
                    {
                        // at the end of the line
                        word = lineBox.Words[lineBox.Words.Count - 1];
                    }
                }

                // if there is matching word
                if (word != null)
                {
                    if (_selectionStart == null)
                    {
                        // on start set the selection start word
                        _selectionStartPoint = loc;
                        _selectionStart = word;
                        if (allowPartialSelect)
                            CalculateWordCharIndexAndOffset(control, word, loc, true);
                    }

                    // always set selection end word
                    _selectionEnd = word;
                    if (allowPartialSelect)
                        CalculateWordCharIndexAndOffset(control, word, loc, false);

                    ClearSelection(_root);
                    if (CheckNonEmptySelection(loc, allowPartialSelect))
                    {
                        CheckSelectionDirection();
                        SelectWordsInRange(_root, _backwardSelection ? _selectionEnd : _selectionStart, _backwardSelection ? _selectionStart : _selectionEnd);
                    }
                    else
                    {
                        _selectionEnd = null;
                    }

                    _cursorChanged = true;
                    control.SetCursorIBeam();
                    control.Invalidate();
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Handle link clicked going over <see cref="LinkClicked"/> event and using <see cref="Process.Start()"/> if not canceled.
        /// </summary>
        /// <param name="parent">the control hosting the html to invalidate</param>
        /// <param name="location">the location of the mouse</param>
        /// <param name="link">the link that was clicked</param>
        internal void HandleLinkClicked(RControl parent, RPoint location, CssBox link)
        {
            if (LinkClicked != null)
            {
                var args = new HtmlLinkClickedEventArgs(link.HrefLink, link.HtmlTag.Attributes);
                try
                {
                    LinkClicked(this, args);
                }
                catch (Exception ex)
                {
                    throw new HtmlLinkClickedException("Error in link clicked intercept", ex);
                }
                if (args.Handled)
                    return;
            }

            if (!string.IsNullOrEmpty(link.HrefLink))
            {
                if (link.HrefLink.StartsWith("#") && link.HrefLink.Length > 1)
                {
                    if (ScrollChange != null)
                    {
                        var rect = GetElementRectangle(link.HrefLink.Substring(1));
                        if (rect.HasValue)
                        {
                            ScrollChange(this, new HtmlScrollEventArgs(rect.Value.Location));
                            HandleMouseMove(parent, location);
                        }
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(link.HrefLink) && Directory.Exists(link.HrefLink)) {
                        Process.Start("explorer.exe", "\"" + link.HrefLink + "\"");
                    }
                    else {
                        if (!string.IsNullOrEmpty(link.HrefLink) && File.Exists(link.HrefLink)) {
                            var process = new ProcessStartInfo(link.HrefLink) {UseShellExecute = true};
                            Process.Start(process);
                        } else {
                            if (new Regex(@"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$").Match(link.HrefLink).Success) {
                                Process.Start(link.HrefLink);
                            }
                        }
                    }
                }
            }
        }
コード例 #9
0
        protected override void SaveToFileInt(RImage image, string name, string extension, RControl control = null)
        {
            using (var saveDialog = new SaveFileDialog())
            {
                saveDialog.Filter = "Images|*.png;*.bmp;*.jpg";
                saveDialog.FileName = name;
                saveDialog.DefaultExt = extension;

                var dialogResult = control == null ? saveDialog.ShowDialog() : saveDialog.ShowDialog(((ControlAdapter)control).Control);
                if (dialogResult == DialogResult.OK)
                {
                    ((ImageAdapter)image).Image.Save(saveDialog.FileName);
                }
            }
        }
コード例 #10
0
        /// <summary>
        /// Handle mouse move to handle hover cursor and text selection.
        /// </summary>
        /// <param name="parent">the control hosting the html to set cursor and invalidate</param>
        /// <param name="location">the location of the mouse</param>
        public void HandleMouseMove(RControl parent, RPoint location)
        {
            ArgChecker.AssertArgNotNull(parent, "parent");

            try
            {
                var loc = OffsetByScroll(location);
                if (_selectionHandler != null && IsMouseInContainer(location))
                    _selectionHandler.HandleMouseMove(parent, loc);

                /*
                if( _hoverBoxes != null )
                {
                    bool refresh = false;
                    foreach(var hoverBox in _hoverBoxes)
                    {
                        foreach(var rect in hoverBox.Item1.Rectangles.Values)
                        {
                            if( rect.Contains(loc) )
                            {
                                //hoverBox.Item1.Color = "gold";
                                refresh = true;
                            }
                        }
                    }

                    if(refresh)
                        RequestRefresh(true);
                }
                 */
            }
            catch (Exception ex)
            {
                ReportError(HtmlRenderErrorType.KeyboardMouse, "Failed mouse move handle", ex);
            }
        }
コード例 #11
0
        /// <summary>
        /// Handle mouse up to handle selection and link click.
        /// </summary>
        /// <param name="parent">the control hosting the html to invalidate</param>
        /// <param name="location">the location of the mouse</param>
        /// <param name="e">the mouse event data</param>
        public void HandleMouseUp(RControl parent, RPoint location, RMouseEvent e)
        {
            ArgChecker.AssertArgNotNull(parent, "parent");

            try
            {
                HandleBoxClicked(1, location);

                if (_selectionHandler != null && IsMouseInContainer(location))
                {
                    var ignore = _selectionHandler.HandleMouseUp(parent, e.LeftButton);
                    if (!ignore && e.LeftButton)
                    {
                        var loc = OffsetByScroll(location);
                        var link = DomUtils.GetLinkBox(_root, loc);
                        if (link != null)
                        {
                            HandleLinkClicked(parent, location, link);
                        }
                    }
                }
            }
            catch (HtmlLinkClickedException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ReportError(HtmlRenderErrorType.KeyboardMouse, "Failed mouse up handle", ex);
            }
        }
コード例 #12
0
        /// <summary>
        /// Handle mouse leave to handle hover cursor.
        /// </summary>
        /// <param name="parent">the control hosting the html to set cursor and invalidate</param>
        public void HandleMouseLeave(RControl parent)
        {
            ArgChecker.AssertArgNotNull(parent, "parent");

            try
            {
                if (_selectionHandler != null)
                    _selectionHandler.HandleMouseLeave(parent);
            }
            catch (Exception ex)
            {
                ReportError(HtmlRenderErrorType.KeyboardMouse, "Failed mouse leave handle", ex);
            }
        }
コード例 #13
0
        /// <summary>
        /// Handle mouse down to handle selection.
        /// </summary>
        /// <param name="parent">the control hosting the html to invalidate</param>
        /// <param name="location">the location of the mouse</param>
        public void HandleMouseDown(RControl parent, RPoint location)
        {
            ArgChecker.AssertArgNotNull(parent, "parent");

            try
            {
                if (_selectionHandler != null)
                    _selectionHandler.HandleMouseDown(parent, OffsetByScroll(location), IsMouseInContainer(location));
            }
            catch (Exception ex)
            {
                ReportError(HtmlRenderErrorType.KeyboardMouse, "Failed mouse down handle", ex);
            }
        }
コード例 #14
0
        /// <summary>
        /// Handle mouse double click to select word under the mouse.
        /// </summary>
        /// <param name="parent">the control hosting the html to set cursor and invalidate</param>
        /// <param name="location">the location of the mouse</param>
        public void HandleMouseDoubleClick(RControl parent, RPoint location)
        {
            ArgChecker.AssertArgNotNull(parent, "parent");

            try
            {
                HandleBoxClicked(2, location);

                if (_selectionHandler != null && IsMouseInContainer(location))
                    _selectionHandler.SelectWord(parent, OffsetByScroll(location));
            }
            catch (Exception ex)
            {
                ReportError(HtmlRenderErrorType.KeyboardMouse, "Failed mouse double click handle", ex);
            }
        }
コード例 #15
0
 /// <summary>
 /// Select all the words in the html.
 /// </summary>
 /// <param name="control">the control hosting the html to invalidate</param>
 public void SelectAll(RControl control)
 {
     if (_root.HtmlContainer.IsSelectionEnabled)
     {
         ClearSelection();
         SelectAllWords(_root);
         control.Invalidate();
     }
 }
コード例 #16
0
        /// <summary>
        /// Show context menu clicked on given rectangle.
        /// </summary>
        /// <param name="parent">the parent control to show the context menu on</param>
        /// <param name="rect">the rectangle that was clicked to show context menu</param>
        /// <param name="link">the link that was clicked to show context menu on</param>
        public void ShowContextMenu(RControl parent, CssRect rect, CssBox link)
        {
            try
            {
                DisposeContextMenu();

                _parentControl = parent;
                _currentRect = rect;
                _currentLink = link;
                _contextMenu = _htmlContainer.Adapter.GetContextMenu();

                if (rect != null)
                {
                    bool isVideo = false;
                    if (link != null)
                    {
                        isVideo = link is CssBoxFrame && ((CssBoxFrame)link).IsVideo;
                        var linkExist = !string.IsNullOrEmpty(link.HrefLink);
                        _contextMenu.AddItem(isVideo ? _openVideo : _openLink, linkExist, OnOpenLinkClick);
                        if (_htmlContainer.IsSelectionEnabled)
                        {
                            _contextMenu.AddItem(isVideo ? _copyVideoUrl : _copyLink, linkExist, OnCopyLinkClick);
                        }
                        _contextMenu.AddDivider();
                    }

                    if (rect.IsImage && !isVideo)
                    {
                        _contextMenu.AddItem(_saveImage, rect.Image != null, OnSaveImageClick);
                        if (_htmlContainer.IsSelectionEnabled)
                        {
                            _contextMenu.AddItem(_copyImageLink, !string.IsNullOrEmpty(_currentRect.OwnerBox.GetAttribute("src")), OnCopyImageLinkClick);
                            _contextMenu.AddItem(_copyImage, rect.Image != null, OnCopyImageClick);
                        }
                        _contextMenu.AddDivider();
                    }

                    if (_htmlContainer.IsSelectionEnabled)
                    {
                        _contextMenu.AddItem(_copy, rect.Selected, OnCopyClick);
                    }
                }

                if (_htmlContainer.IsSelectionEnabled)
                {
                    _contextMenu.AddItem(_selectAll, true, OnSelectAllClick);
                }

                if (_contextMenu.ItemsCount > 0)
                {
                    _contextMenu.RemoveLastDivider();
                    _contextMenu.Show(parent, parent.MouseLocation);
                }
            }
            catch (Exception ex)
            {
                _htmlContainer.ReportError(HtmlRenderErrorType.ContextMenu, "Failed to show context menu", ex);
            }
        }
コード例 #17
0
 /// <summary>
 /// Select the word at the given location if found.
 /// </summary>
 /// <param name="control">the control hosting the html to invalidate</param>
 /// <param name="loc">the location to select word at</param>
 public void SelectWord(RControl control, RPoint loc)
 {
     if (_root.HtmlContainer.IsSelectionEnabled)
     {
         var word = DomUtils.GetCssBoxWord(_root, loc);
         if (word != null)
         {
             word.Selection = this;
             _selectionStartPoint = loc;
             _selectionStart = _selectionEnd = word;
             control.Invalidate();
         }
     }
 }
コード例 #18
0
 /// <summary>
 /// Dispose of the last used context menu.
 /// </summary>
 private void DisposeContextMenu()
 {
     try
     {
         if (_contextMenu != null)
             _contextMenu.Dispose();
         _contextMenu = null;
         _parentControl = null;
         _currentRect = null;
         _currentLink = null;
     }
     catch
     { }
 }
コード例 #19
0
        /// <summary>
        /// Calculate the character index and offset by characters for the given word and given offset.<br/>
        /// <seealso cref="CalculateWordCharIndexAndOffset(RControl,CssRect,RPoint,bool)"/>.
        /// </summary>
        /// <param name="control">used to create graphics to measure string</param>
        /// <param name="word">the word to calculate its index and offset</param>
        /// <param name="loc">the location to calculate for</param>
        /// <param name="selectionStart">to set the starting or ending char and offset data</param>
        private void CalculateWordCharIndexAndOffset(RControl control, CssRect word, RPoint loc, bool selectionStart)
        {
            int selectionIndex;
            double selectionOffset;
            CalculateWordCharIndexAndOffset(control, word, loc, selectionStart, out selectionIndex, out selectionOffset);

            if (selectionStart)
            {
                _selectionStartIndex = selectionIndex;
                _selectionStartOffset = selectionOffset;
            }
            else
            {
                _selectionEndIndex = selectionIndex;
                _selectionEndOffset = selectionOffset;
            }
        }
コード例 #20
0
 public override void Show(RControl parent, RPoint location)
 {
     _contextMenu.Show(((ControlAdapter)parent).Control, Utils.ConvertRound(location));
 }
コード例 #21
0
 /// <summary>
 /// Start drag & drop operation on the currently selected html segment.
 /// </summary>
 /// <param name="control">the control to start the drag & drop on</param>
 private void StartDragDrop(RControl control)
 {
     if (_dragDropData == null)
     {
         var html = DomUtils.GenerateHtml(_root, HtmlGenerationStyle.Inline, true);
         var plainText = DomUtils.GetSelectedPlainText(_root);
         _dragDropData = control.Adapter.GetClipboardDataObject(html, plainText);
     }
     control.DoDragDropCopy(_dragDropData);
 }
コード例 #22
0
        /// <summary>
        /// Handle mouse down to handle selection.
        /// </summary>
        /// <param name="parent">the control hosting the html to invalidate</param>
        /// <param name="loc">the location of the mouse on the html</param>
        /// <param name="isMouseInContainer"> </param>
        public void HandleMouseDown(RControl parent, RPoint loc, bool isMouseInContainer)
        {
            bool clear = !isMouseInContainer;
            if (isMouseInContainer)
            {
                _mouseDownInControl = true;
                _isDoubleClickSelect = (DateTime.Now - _lastMouseDown).TotalMilliseconds < 400;
                _lastMouseDown = DateTime.Now;
                _mouseDownOnSelectedWord = false;

                if (_root.HtmlContainer.IsSelectionEnabled && parent.LeftMouseButton)
                {
                    var word = DomUtils.GetCssBoxWord(_root, loc);
                    if (word != null && word.Selected)
                    {
                        _mouseDownOnSelectedWord = true;
                    }
                    else
                    {
                        clear = true;
                    }
                }
                else if (parent.RightMouseButton)
                {
                    var rect = DomUtils.GetCssBoxWord(_root, loc);
                    var link = DomUtils.GetLinkBox(_root, loc);
                    if (_root.HtmlContainer.IsContextMenuEnabled)
                    {
                        _contextMenuHandler.ShowContextMenu(parent, rect, link);
                    }
                    clear = rect == null || !rect.Selected;
                }
            }

            if (clear)
            {
                ClearSelection();
                parent.Invalidate();
            }
        }
コード例 #23
0
ファイル: RAdapter.cs プロジェクト: jcaillon/YamuiFramework
 /// <summary>
 /// Save the given image to file by showing save dialog to the client.
 /// </summary>
 /// <param name="image">the image to save</param>
 /// <param name="name">the name of the image for save dialog</param>
 /// <param name="extension">the extension of the image for save dialog</param>
 /// <param name="control">optional: the control to show the dialog on</param>
 protected virtual void SaveToFileInt(RImage image, string name, string extension, RControl control = null)
 {
     throw new NotImplementedException();
 }
コード例 #24
0
 /// <summary>
 /// Show the context menu in the given parent control at the given location.
 /// </summary><param name="parent">the parent control to show in</param><param name="location">the location to show at relative to the parent control</param>
 public abstract void Show(RControl parent, RPoint location);