Giter Site home page Giter Site logo

Drag text to change position about scintillanet HOT 2 CLOSED

xonyen avatar xonyen commented on September 13, 2024
Drag text to change position

from scintillanet.

Comments (2)

jacobslusser avatar jacobslusser commented on September 13, 2024

Native Scintilla supports dragging text within the control, however, this ability was disabled with the implementation of standard Windows Forms Drag & Drop support in issue #6.

from scintillanet.

gena03 avatar gena03 commented on September 13, 2024

I'm still a novice. That is written Drag & Drop ...

Public Class Form1
Const SCI_GETSELECTIONS = 2570
Const SCI_SELECTIONISRECTANGLE = 2372
Const SCI_GETFIRSTVISIBLELINE = 2152
Const SCI_LINESONSCREEN = 2370
Const SCI_TEXTHEIGHT = 2279

Private flag, flag_DragEnter, flag_ALT, flag_Move, flag_zone As Boolean
Private clipboard As String = ""
Private cursor_Width, cursor_Height, caret, pos_Start, pos_End, column_Start, column_End, line_sel, line_caret, column_caret, sing_sel As Integer

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Scintilla1.AllowDrop = True
    cursor_Width = Scintilla1.CaretWidth
    With Scintilla1.Styles(ScintillaNET.Style.Default) : .Font = "Lucida Console" : .Size = 12 : End With
End Sub

'The event - the beginning of a drag operation
Private Sub Scintilla1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Scintilla1.DragEnter
    cursor_Height = Scintilla1.DirectMessage(SCI_TEXTHEIGHT, IntPtr.Zero, IntPtr.Zero) - 1
    flag_DragEnter = True
    Scintilla1.CaretWidth = 0
    pos_Start = Scintilla1.CurrentPosition
    pos_End = Scintilla1.AnchorPosition
    If pos_End < pos_Start Then
        pos_Start = Scintilla1.AnchorPosition
        pos_End = Scintilla1.CurrentPosition
    End If
    If Scintilla1.DirectMessage(SCI_SELECTIONISRECTANGLE, IntPtr.Zero, IntPtr.Zero) Then
        column_Start = Scintilla1.GetColumn(pos_Start)
        column_End = Scintilla1.GetColumn(pos_End)
        flag_ALT = True 'the text has been selected frame (Alt - key)
    Else
        flag_ALT = False 'normal text selection
    End If
End Sub

'Event - during all dragging
Private Sub Scintilla1_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Scintilla1.DragOver
    Dim new_pos = Scintilla1.CharPositionFromPoint(Scintilla1.PointToClient(New Point(e.X, e.Y)).X, Scintilla1.PointToClient(New Point(e.X, e.Y)).Y)
    If Not new_pos = caret Then
        Scintilla1.Refresh() 'delete the previous position of the new cursor
        Dim x = Scintilla1.PointXFromPosition(new_pos)
        Dim y = Scintilla1.PointYFromPosition(new_pos)
        Dim g As Graphics = Scintilla1.CreateGraphics()
        Dim pen1 As New System.Drawing.Pen(Color.Red, cursor_Width + 1) 'the color and the thickness of the new cursor
        g.DrawLine(pen1, x, y, x, y + cursor_Height) 'draw new cursor
    End If
    caret = new_pos
    Dim pos_None = False
    If (caret > pos_Start AndAlso caret < pos_End) Then
        If flag_ALT Then
            Dim curr = Scintilla1.GetColumn(caret)
            If (curr > column_Start AndAlso curr < column_End) Then
                pos_None = True
            End If
        Else
            pos_None = True
        End If
    End If
    flag_Move = False
    If pos_None Then ' "dead" zone
        e.Effect = DragDropEffects.None
        Scintilla1.Refresh()
    ElseIf ((e.KeyState And 8) = 8 And (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy) Then
        e.Effect = DragDropEffects.Copy ' Did you press the Ctrl - up.
    ElseIf ((e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then
        e.Effect = DragDropEffects.Move 'Default - Move.
        flag_Move = True
    Else
        e.Effect = DragDropEffects.None
    End If
    'If the target is outside the visible window:
    Dim line_Up As Integer = Scintilla1.DirectMessage(SCI_GETFIRSTVISIBLELINE, IntPtr.Zero, IntPtr.Zero)
    Dim line_Down = line_Up + Scintilla1.DirectMessage(SCI_LINESONSCREEN, IntPtr.Zero, IntPtr.Zero) - 1
    Dim line_cur = Scintilla1.LineFromPosition(caret)
    If line_cur = line_Up Then
        Scintilla1.LineScroll(-1, 0)
    ElseIf line_cur = line_Down Then
        Scintilla1.LineScroll(1, 0)
    End If
End Sub

'The event - with zaverschenii drag operation (not performed if DragDropEffects.None)
Private Sub Scintilla1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Scintilla1.DragDrop
    flag = True
    flag_zone = False
    sing_sel = pos_End - FindColumn(Scintilla1.LineFromPosition(pos_End), column_Start)
    Scintilla1.BeginUndoAction()
    clipboard = My.Computer.Clipboard.GetText
    Scintilla1.Copy()
    If flag_Move Then
        If flag_ALT Then
            'if you select the rectangle (Alt - key):
            line_sel = Scintilla1.DirectMessage(SCI_GETSELECTIONS, IntPtr.Zero, IntPtr.Zero) - 1
            line_caret = Scintilla1.LineFromPosition(caret)
            If Scintilla1.GetColumn(caret) >= column_End And (line_caret >= Scintilla1.LineFromPosition(pos_Start) AndAlso line_caret <= Scintilla1.LineFromPosition(pos_End)) Then
                column_caret = Scintilla1.Lines(line_caret).EndPosition
                flag_zone = True
            Else
                column_caret = Scintilla1.GetColumn(caret)
            End If
        ElseIf (caret > pos_Start OrElse caret >= pos_End) Then
            caret -= pos_End - pos_Start
        End If
    End If
End Sub

Private Sub Scintilla1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Scintilla1.MouseMove
    If flag_DragEnter Then
        Scintilla1.Refresh()
        Scintilla1.CaretWidth = cursor_Width
        flag_DragEnter = False
    End If
    If flag Then
        If flag_ALT Then
            If flag_Move Then
                If flag_zone Then
                    column_caret = Scintilla1.GetColumn(caret - (column_caret - Scintilla1.Lines(line_caret).EndPosition))
                End If
                caret = FindColumn(line_caret, column_caret)
            End If
            Scintilla1.SetSel(caret, caret)
            Scintilla1.Paste()
            ' Select the inserted text:
            'Scintilla1.RectangularSelectionAnchor = caret
            'Scintilla1.RectangularSelectionCaret = FindColumn(Scintilla1.LineFromPosition(caret) + line_sel, Scintilla1.GetColumn(caret + sing_sel))
        Else
            Scintilla1.SetSel(caret, caret)
            Scintilla1.Paste()
            'Select the inserted text:
            Scintilla1.SetSel(caret, caret + (pos_End - pos_Start))
        End If
        My.Computer.Clipboard.SetText(clipboard)
        Scintilla1.EndUndoAction()
        flag = False
    End If
End Sub

Private Sub Scintilla1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Scintilla1.MouseDoubleClick
    Scintilla1.Cursor = Cursors.Default
    Cursor.Position = New Point(Cursor.Position.X + 4, Cursor.Position.Y) 'ignore the third-click (select an entire row). So far, so ...
End Sub

'replacing the standard SCI_FINDCOLUMN:
Private Function FindColumn(ByVal line As Integer, ByVal column As Integer) As Integer
    Dim position = Scintilla1.Lines(line).Position
    Dim tab = Scintilla1.TabWidth
    Dim columnCurrent = 0
    For i = 0 To Scintilla1.Lines(line).Length - 1
        Dim ch = CChar(ChrW(Scintilla1.GetCharAt(position + i)))
        If columnCurrent >= column Then
            Return position + i
        ElseIf ch = ControlChars.Tab Then
            columnCurrent = (columnCurrent \ tab + 1) * tab
            If columnCurrent >= column Then Return position + i + 1
        Else
            columnCurrent += 1
        End If
    Next
    Return position + Scintilla1.Lines(line).Length
End Function

End Class

from scintillanet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.