Drag & Drop with WPF in C#

// WPF Canvas Object
// Canvas LayoutCanvas;
//
// Events Required
//
// Canvas_PreviewMouseLeftButtonDown
// Canvas_PreviewMouseMove
// Canvas_PreviewMouseLeftButtonUp
// Parent_PreviewKeyDown
//
// LayoutCanvas.PreviewMouseLeftButtonDown += LayoutCanvas_PreviewMouseLeftButtonDown;
// LayoutCanvas.PreviewMouseMove += LayoutCanvas_PreviewMouseMove;
// LayoutCanvas.PreviewMouseLeftButtonUp += LayoutCanvas_PreviewMouseLeftButtonUp;
// WindowOrPage.PreviewKeyDown += WindowOrPage_PreviewKeyDown;
//
// Parameters Required
//
// For capturing the mouse position:
// Point ddStartPoint;
//
// The top left corner of the child object (left = x, top = y)
// double ddOriginalLeft;
// double ddOriginalTop;
//
// Properties for managing the state of the drag & drop process:
// bool ddIsMouseDown;
// bool ddIsBeingDragged;
//
// Our original UI element (in my case the children are all Image objects)
// UIElement ddOriginalElement;
//
// The container of the above element when we are in dragging mode
// System.Windows.Shapes.Rectangle ddOverlay;
//

//
// Canvas_PreviewMouseLeftButtonDown
//
// We assign this to our Canvas object as it will control
// catching whether or not we are clicking on the canvas itself
// or on one of its children
//
private void LayoutCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // If the source of the click is our canvas object then we want
    // to exit because we are looking for it's children to drag 
    // and not the canvas itself.
    if(e.Source == LayoutCanvas)
    {
	return;
    }
    
    // Identifies that we have started dragging
    ddIsMouseDown = true;

    // Captures the mouse position in the layout canvas
    ddStartPoint = e.GetPosition(LayoutCanvas);

    // Sets up our element that we will be dragging
    ddOriginalElement = (UIElement)e.Source;

    // Tells the Window to give the mouse to the LayoutCanvas
    // object.
    LayoutCanvas.CaptureMouse();

    e.Handled = true;
}	

//
// Canvas_PreviewMouseMove
// 
// Our event handler for updating the position of our
// dragged element
//
// This introduces two helper methods DragStarted() and DragMoved()
// They will be covered later on in the code.
//
private void LayoutCanvas_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
    if(ddIsMouseDown)
    {
	if(!ddIsBeingDragged)
	{
	    // Capture our mouse position relative to the LayoutCanvas object
	    var mousePosition = e.GetPosition(LayoutCanvas);

	    // Creates a transparent rectangle around our current drag point and we want to 
	    // check here that we are within that rectangle
	    if(Math.Abs(mousePosition.X - ddStartPoint.X) > SystemParameters.MinimumHorizontalDragDistance &&
		    Math.Abs(mousePosition.Y - ddStartPoint.Y) > SystemParameters.MinimumVeritcalDragDistance)
	    {
		DragStarted();
	    }
	}
	else
	{
	    DragMoved();
	}
    }
}

//
// Canvas_PreviewMouseLeftButtonUp
//
// Controls the functionality for finishing our drag and drop process.
// This will also introduce the call to DragFinished(bool state);
//
private void LayoutCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    // This is a fairly simple check. If we are still dragging
    // or starting to drag which means ddIsMouseDown would be 'True'
    // then we don't stop the drag
    if(ddIsMouseDown)
    {
	DragFinished(false);
    }
}

//
// Page_PreviewKeydown
//
// In my code I have my canvas in a Page, you can do this with a Window object as well.
//
private void Page_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if(e.Key == Key.Escape && ddIsBeingDragged)
    {
	DragFinished = true;
    }
}

// Helper Methods

//
// DragStarted()
//
//
private void DragStarted()
{
    // Capture our last remaining properties
    // needed to support our drag and drop
    // process
    ddIsBeingDragged = true;
    ddOriginalLeft = Canvas.GetLeft(ddOriginalElement);
    ddOriginalTop = Canvas.GetTop(ddOriginalElement);

    // What we are doing here is creating a semi-transparent
    // mirror image of the object we are dragging.
    //
    // This allows us to visually see that we have selected 
    // an object and are dragging it.
    var brush = new VisualBrush(ddOriginalElement);
    brush.Opacity = 0.5;

    ddOverlay = new System.Windows.Shapes.Rectangle();
    ddOverlay.Width = ddOriginalElement.RenderSize.Width;
    ddOverlay.Height = ddOriginalElement.RenderSize.Height;
    ddOverlay.Fill = brush;

    // Finally add the overlay to the LayoutCanvas for displaying
    LayoutCanvas.Children.Add(ddOverlay);
}


//
// DragMoved();
//
private void DragMoved()
{
    // Capture the current mouse position, this will be used
    // to redraw the overlay element we created in DragStarted()
    var currentPosition = System.Windows.Input.Mouse.GetPosition(LayoutCanvas);
    var elementLeft = (currentPosition.X - ddStartPoint.X) + ddOriginalLeft;
    var elementTop = (currentPosition.Y - ddStartPoint.Y) + ddOriginalTop;

    // We update the overlay's position on the LayoutCanvas
    // by setting it's top left corner position below
    Canvas.SetLeft(ddOverlay, elementLeft);
    Canvas.SetTop(ddOverlay, elementTop);
}

//
// DragFinished();
//
private void DragFinished(bool canceled)
{
    if(ddOverlay != null)
    {
	// capture our current position
	var topLeft = Canvas.GetLeft(ddOverlay);
	var top = Canvas.GetTop(ddOverlay);

	if(ddIsBeingDragged)
	{
	    LayoutCanvas.Children.Remove(ddOverlay);

	    // If it wasn't prematurely canceled, then
	    // move the element to the current mouse position
	    if(!canceled)
	    {
		Canvas.SetLeft(ddOriginalElement, topLeft);
		Canvas.SetTop(ddOriginalElement, top);
	    }

	    // Release the mouse from the layoutcanvas.
	    // This is very important. If you do not release the mouse
	    // you have to set the focus of the mouse to another application
	    // and then back again to regain mouse control
	    LayoutCanvas.ReleaseMouseCapture();

	    // Reset our drag & drop properties
	    ddOverlay = null;
	    ddIsMouseBeingDragged = false;
	    ddIsMouseDown = false;
	}
    }
}

source

MySql Load Data

load data local infile <filepath> into table <table name> fields terminated by "	" lines terminated by "
" (fields)

source

Redirect

<script type="text/javascript">
<!--
window.location = "<a href="http://www.google.com/"" >http://www.google.com/"</a>
//-->
</script>

source

Encode string into Bytes

private byte[] EncodeMsg(string msg)
        {
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes(msg);

            return buffer;
        }

source

Migrate/move subversion repository

#In the old server shell
svnadmin dump /path/to/repository > repository-name.dmp

#In the new server
cd /path/to/new-repository
svnadmin create repository-name
svnadmin load repository-name < repository-name.dmp

source

get sel transition

float $fcfnXYZ[] = `xform -q -ws -t`;                    //get trans of select comp and obj

source

get current env

about -env

source

EzTube: Playing YouTube Videos without Flash

#! /usr/bin/env bash
#
#    eztube2.sh
#    Playing Youtube Videos without flash, using mplayer.
#    Revised and $(hopefully) better version.
#    ksaver (at identi.ca), Sep 2010.
#    Public Domain Code.
#    Dependences:  (Unix-like OS, python, bash implicit).
#                  Youtube-dl: <a href="http://bitbucket.org/rg3/youtube-dl/" >http://bitbucket.org/rg3/youtube-dl/</a>
#                  Zenity:     <a href="http://freshmeat.net/projects/zenity" >http://freshmeat.net/projects/zenity</a>
#                  MPlayer:    <a href="http://www.mplayerhq.hu/" >http://www.mplayerhq.hu/</a>
#
#    More Info: <a href="http://ur1.ca/0g3gu" >http://ur1.ca/0g3gu</a>

scriptname=$(basename $0)
version='0.2.1'
dependences='zenity mplayer youtube-dl'

#Edit this as needed/
workdir="$HOME/Video"
wait_sec=60
##/

function check_dep()
    {
        for DEPEND in "$@"
        do
            /usr/bin/which $DEPEND &> /dev/null ||
            (echo "[ERR]: Dependence '$DEPEND' Not found in your path!" &&
            return 1)
            
        done
    }

function dl_video()
    {
        /usr/bin/env youtube-dl -q --continue "$1" -o "$2" &
        DLPID="$!"
    }
    
function get_url()
    {
        YTURL=$(zdialog --entry --text='Youtube Video URL:')
    }

function kill_dl()
    {
        PID=$1
        SEC=$2
        /bin/ps   -p  $PID &> /dev/null && waitload $SEC
        /bin/kill -15 $PID &> /dev/null
    }

function play_video()
    {
        /usr/bin/env mplayer -zoom "$1"
    }
    
function saveordrop()
    {
        zdialog --question --text="Save downloaded video?"
    }

function waitload()
    {
     #Not actually checking progress video size, just waits.
        CNT=0
        SEC=$1
        while [[ $CNT -lt $SEC ]]
        do
            echo  $(($CNT*100/$SEC)) #%percent%
            let   CNT=$CNT+1
            sleep 1
            
        done | zdialog --progress --auto-close 
               --text="Loading video...
Please wait about $wait_sec secs."
    }
        
function zdialog()
    {
        /usr/bin/env zenity --title="$scriptname $version" "$@"
    }
    
function _main_()
    {
        check_dep $dependences || exit
        
        YTURL=''
        while [[ ! $YTURL ]]
        do
            get_url || exit
        done         

        VIDNAME="$workdir/$(echo $YTURL |cut -d '&' -f 1 |cut -d '=' -f 2).flv"
        dl_video "$YTURL" "$VIDNAME"

        # If file already exists play it
        if [[ -f $VIDNAME ]]
        then
            play_video "$VIDNAME"
            
        else
            waitload $wait_sec
            play_video "$VIDNAME"
            
        fi
                
        #Save or delete file
        saveordrop
        if [ $? -eq 0 ]
        then
            FILENAME=$(zdialog --file-selection --filename="$VIDNAME")
            
            #wait to download...
            kill_dl $DLPID $wait_sec
            
            #and move downloaded video to new destiny
            /bin/mv "$VIDNAME" "$FILENAME"
            
        else
            #kill download and delete file
            kill_dl $DLPID 0
            if [ -f "$VIDNAME" ]
            then
                /bin/rm "$VIDNAME"
            fi
            
        fi        
    }

#Run script...
while true
do
    _main_
done

source

Hello World Python

print 'Hello World
'

source

Javascript w3c validator

javascript:void(document.location='<a href="http://validator.w3.org/check?" >http://validator.w3.org/check?</a>
charset=%28detect+automatically%29&doctype=Inline&ss=1&group=0&
verbose=1&uri='+escape(document.location))

source