/// <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 { if (this._selectionHandler != null && IsMouseInContainer(location)) { this._selectionHandler.SelectWord(parent, OffsetByScroll(location)); } } catch (Exception ex) { ReportError(HtmlRenderErrorType.KeyboardMouse, "Failed mouse double click handle", ex); } }
/// <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); } }
/// <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 { } }
/// <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); } }
/// <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) { EventHandler <HtmlLinkClickedEventArgs> clickHandler = LinkClicked; if (clickHandler != null) { var args = new HtmlLinkClickedEventArgs(link.HrefLink, link.HtmlTag.Attributes); try { clickHandler(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) { EventHandler <HtmlScrollEventArgs> scrollHandler = ScrollChange; if (scrollHandler != null) { var rect = GetElementRectangle(link.HrefLink.Substring(1)); if (rect.HasValue) { scrollHandler(this, new HtmlScrollEventArgs(rect.Value.Location)); HandleMouseMove(parent, location); } } } /* * else * { * var nfo = new ProcessStartInfo(link.HrefLink); * nfo.UseShellExecute = true; * Process.Start(nfo); * } */ } }
/// <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(); } } }
/// <summary> /// Handle link hover going over <see cref="LinkHover"/> event. /// </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 hovered</param> internal void HandleLinkHover(RControl parent, RPoint location, CssBox link) { EventHandler <HtmlLinkHoverEventArgs> hoverHandler = LinkHover; if (hoverHandler != null) { var args = new HtmlLinkHoverEventArgs(link.HrefLink, location, link.HtmlTag.Attributes); try { hoverHandler(this, args); } catch (Exception ex) { throw new HtmlLinkClickedException("Error in link hover intercept", ex); } } }
/// <summary> /// Calculate the character index and offset by characters for the given word and given offset.<br/> /// <seealso cref="CalculateWordCharIndexAndOffset(RControl,HtmlRenderer.Core.Dom.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; } }
/// <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(); } }
/// <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); }
/// <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.CssBox.Rectangles.Values) { if (rect.Contains(loc)) { //Console.WriteLine("" + hoverBox.CssBlock.Class); hoverBox.CssBox.Color = "red"; refresh = true; } } } if (refresh) { RequestRefresh(true); } } } catch (Exception ex) { ReportError(HtmlRenderErrorType.KeyboardMouse, "Failed mouse move handle", ex); } }
/// <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 = this.OffsetByScroll(location); if (this._selectionHandler != null && this.IsMouseInContainer(location)) { this._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) { this.ReportError(HtmlRenderErrorType.KeyboardMouse, "Failed mouse move handle", ex); } }
/// <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); } }
/// <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(); } } }
/// <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(); // Give client a chance to customise the menu _htmlContainer.HandleContextMenuInvoked(_contextMenu); if (_contextMenu.ItemsCount > 0) { _contextMenu.AddDivider(); } 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); } }
public override void Show(RControl parent, RPoint location) { _contextMenu.Show(((ControlAdapter)parent).Control, Utils.ConvertRound(location)); }
public override void Show(RControl parent, RPoint location) { }
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); } } }
protected override void SaveToFileInt(RImage image, string name, string extension, RControl control = null) { var saveDialog = new SaveFileDialog(); saveDialog.Filter = "Images|*.png;*.bmp;*.jpg;*.tif;*.gif;*.wmp;"; saveDialog.FileName = name; saveDialog.DefaultExt = extension; var dialogResult = saveDialog.ShowDialog(); if (dialogResult.GetValueOrDefault()) { var encoder = Utils.GetBitmapEncoder(Path.GetExtension(saveDialog.FileName)); encoder.Frames.Add(BitmapFrame.Create(((ImageAdapter)image).Image)); using (FileStream stream = new FileStream(saveDialog.FileName, FileMode.OpenOrCreate)) encoder.Save(stream); } }
protected override void SaveToFileInt(RImage image, string name, string extension, RControl control = null) { using (var saveDialog = new SaveFileDialog()) { saveDialog.Filters.Add(new FileFilter("Images", ".png", ".bmp", ".jpg", ".jpeg", ".tiff")); saveDialog.FileName = name; //saveDialog.filter.DefaultExt = extension; var dialogResult = control == null?saveDialog.ShowDialog(null) : saveDialog.ShowDialog(((ControlAdapter)control).Control); if (dialogResult == DialogResult.Ok) { var ext = Path.GetExtension(saveDialog.FileName); ImageFormat format = ImageFormat.Png; switch (ext.ToLowerInvariant()) { case ".png": format = ImageFormat.Png; break; case ".bmp": format = ImageFormat.Bitmap; break; case ".tiff": format = ImageFormat.Tiff; break; case ".jpeg": case ".jpg": format = ImageFormat.Jpeg; break; } var realImage = ((IImageAdapter)image).Image; var bitmap = realImage as Bitmap; if (bitmap == null && realImage is Icon icon) { bitmap = icon.GetFrame(1).Bitmap; } bitmap?.Save(saveDialog.FileName, format); } } }
public override void Show(RControl parent, RPoint location) { _contextMenu.PlacementTarget = ((ControlAdapter)parent).Control; _contextMenu.PlacementRectangle = new Rect(Utils.ConvertRound(location), Size.Empty); _contextMenu.IsOpen = true; }
public RControlAdapter(RControl inner) => this.inner = inner;