# Drag and Drop and clipboard handling module

Copyright © 1995-2012 Opera Software AS. All rights reserved. This file is part of the Opera web browser. It may not be distributed under any circumstances.

## API documentation

## Introduction

The dragdrop module contains classes such as: `DragDropManager`, `DragDrop_Data_Utils`, `DragDropEventsManager`, `PrivateDragData`, `DragScroller`, `DragDropVisualFeedback`, `ClipboardManager`

## D'n'd classes diagram and code flow

## Drag and Drop

### DragDropManager

The class `DragDropManager` is the drag'n'drop interface for CORE. Whenever CORE needs something done with regards to d'n'd it should call DragDropManager's methods. Internally DragDropManager takes care of:

- keeping track of the d'n'd operation state e.g. what are the current target element or the currently selected element,
- communication with the platform's drag manager (PI's OpDragManager),
- sending d'n'd javascript events and handling their default actions (also periodically using a timer as the spec requires),
- filling the drag'n'drop data,
- scrolling of scrollable containers during d'n'd
- and creating the d'n'd visual feedback bitmap.

All mentioned actions are at least partially done using helper classes described below.

### DragDrop_Data_Utils

The class `DragDrop_Data_Utils` is the utility class wrapping some operations on the data store represented by PI's OpDragObject class. It essentially encloses operations which would have to be repeated in various code parts into proper methods. It makes operations on the data easier as well.

### DragDropEventsManager

The class `DragDropEventsManager` has a role of the drag'n'drop events queue. It's responsible for queuing events which come when some other event is being handled and for replaying them when the current event is handled. It also tries to make sure no important event is lost in various situations (e.g. a document unload).

### PrivateDragData

The class `PrivateDragData`, as it's name suggests, stores some private d'n'd data. The private data is the data `DragDropManager` needs in order to drive the drag'n'drop operation correctly e.g. the flag indicating that the dragged data comes from a text selection on a page.

### DragScroller

The class `DragScroller` is responsible for scrolling scrollable containers during d'n'd when the drag gets close to the scroll margin. The scrolling functionality is configurable via: TWEAK_DND_SCROLL_MARGIN, TWEAK_DND_SCROLL_MARGIN_MIN_PX, TWEAK_DND_SCROLL_DELTA_MIN,
TWEAK_DND_SCROLL_DELTA_MAX and TWEAK_DND_SCROLL_INTERVAL tweaks. See module.tweaks for more details.
Note that this functionality uses the OpTimer to do its job.

### DragDropVisualFeedback

The class `DragDropVisualFeedback` allows to create the d'n'd visual feedback bitmap out of elements or a text selection being dragged.

## Clipboard

### ClipboardManager

The class `ClipboardManager` is the clipboard interface for CORE. Core objects should call it when they want to put/get anything to/from the clipboard or copying/cutting/pasting takes place. Internally it takes care of:

- placing the data in the system's clipboard,
- getting the data from the system's clipboard
- sending clipboard DOM events such as oncopy, oncut and onpaste.

## Memory handling

### Used OOM policies

Majority of functions which may fail due to OOM either return failure status or raise OOM situation in MemoryManager.

### Heap memory usage

May allocate big objects on the heap mainly when starting the drag'n'drop opertation e.g. when creating the visual feedback bitmap.

### Stack memory usage

Temporary objects and recursive functions: e.g. getting the microdata from dragged elements in DragDropManager::OnDragStart() and scrolling scrollables: DragDropManager::ScrollIfNeededInternal().

### Freeing memory on exit

All is freed.

### Temp buffers

Uses temp buffers passed in by callers or created on the stack.

### Tests

Selftests and the d'n'd testsuite and the clipboard events testsuite

### Suggestions of improvements

Multi-drag , the visual feedback bitmap generation, the text/html data generation out of a text selection and updating the feedback bitmap when e.g. the video element is being dragged.

## Specifications

### HTML5 Drag'n'drop specification

### Clipboard events specification