TransRepliShow: Dragging Replicants Transparently

Article contributed by Anonymous on Mon, 2005-03-28 05:00

This article follows up on Dr. Reh's previous newsletter article, RepliShow: A Replicable Image Viewer. The code discussed is available here for your convenience.

Just remember Seth Flaxman's RepliShow: While dragging the replicant you only see the border lines of an empty rectangle - not very Be-like. However, only a few lines of code are necessary to obtain a rectangle containing the dragged image and looking transparently. Because the dragging action is managed by the BDragger class we need to do some subclassing. We create a new RepliDragger class inherited from BDragger. When this is done we override the hook function virtual void MouseDown( BPoint where ):

void RepliDragger ::MouseDown( BPoint where )                            // mouse down
{
BPoint cursor;
uint32 buttons;

SetMouseEventMask( B_POINTER_EVENTS, 0 ); // view can receive mouse-events
GetMouse(&cursor,&buttons);

if ( (fBitmap != NULL) && (fArchive != NULL) && (buttons & B_PRIMARY_MOUSE_BUTTON) )
{
BPoint origBitmap;
BRect bitmapRect = fBitmap -> Bounds(); // get boundaries of image
BBitmap *bitmap = new BBitmap(bitmapRect, B_RGB32,true); // create bitmap for drag-and-drop
memcpy(bitmap->Bits(), fBitmap->Bits(), fBitmap->BitsLength()); // copy bitmap
origBitmap = BPoint(bitmapRect.Width(),bitmapRect.Height()); // position relative to bitmap
origBitmap = origBitmap - BPoint(7-where.x, 7-where.y);
DragMessage( fArchive, bitmap, B_OP_BLEND, origBitmap, NULL);
}

BDragger :: MouseDown(where); // BDragger mouse down events
}

void DragMessage(BMessage *message, BBitmap *image, drawing_mode dragMode, BPoint offset, replyTarget = NULL) is responsible for the drag-and-drop handling. message is a BMessage object containing all information that will be dragged and dropped to the BShelf container view. In our special case the message is fArchive, our archived view. Using the dragMode B_OP_BLEND lets you drag the BBitmap image transparently a