1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include <Drag.h>
#include <Errors.h>
#include <TextEdit.h>
#include <QuickDraw.h>
extern TEHandle hTERec;
// Handle drag from newswatcher -EAD
/*----------------------------------------------------------------------------
DragText
Drag selected text.
Entry: ev = pointer to mouse down event record.
where = location of mouse down event in local coords.
theTE = handle to TextEdit record.
Exit: function result = error code.
*dragged =
true if text was dragged.
false if mouse down was not over text selection, or
user did not move the mouse before releasing the
mouse button.
*trashed = true if text was dragged to trash.
----------------------------------------------------------------------------*/
extern RgnHandle rgn;
//extern EventRecord theEvent;
Boolean DragText (EventRecord *ev)
{
DragReference dragRef;
OSErr err = noErr;
Boolean haveDragRef = false;
Handle hText;
RgnHandle dragRgn, tempRgn;
short selStart, selEnd;
char state;
Point theLoc;
GrafPtr curPort;
// if (!PtInTEHiliteRgn(where, hTERec)) return noErr;
if (!WaitMouseMoved(ev->where)) return noErr;
GetPort(&curPort);
CopyRgn(rgn, dragRgn = NewRgn());
SetPt(&theLoc, 0, 0);
LocalToGlobal(&theLoc);
OffsetRgn(dragRgn, theLoc.h, theLoc.v);
hText = (**hTERec).hText;
selStart = (**hTERec).selStart;
selEnd = (**hTERec).selEnd;
err = NewDrag(&dragRef);
if (err != noErr) goto exit;
haveDragRef = true;
state = HGetState(hText);
HLock(hText);
err = AddDragItemFlavor(dragRef, 1, 'TEXT', *hText + selStart, selEnd - selStart, 0);
HSetState(hText, state);
if (err != noErr) goto exit;
// dragRgn = NewRgn();
// err = TEGetHiliteRgn(dragRgn, hTERec);
// if (err != noErr) goto exit;
// LocalToGlobalRgn(dragRgn);
// OutlineRegion(dragRgn);
SetDragItemBounds(dragRef, 1, &(**dragRgn).rgnBBox);
tempRgn = NewRgn();
CopyRgn(dragRgn, tempRgn);
InsetRgn(tempRgn, 1, 1);
DiffRgn(dragRgn, tempRgn, dragRgn);
DisposeRgn(tempRgn);
err = TrackDrag(dragRef, ev, dragRgn);
if (err != noErr && err != userCanceledErr) goto exit;
//*trashed = DragTargetWasTrash(dragRef);
// DisposeRgn(dragRgn);
DisposeDrag(dragRef);
return true;
exit:
if (haveDragRef) DisposeDrag(dragRef);
// if (dragRgn != nil) DisposeRgn(dragRgn);
return false;
}
/*----------------------------------------------------------------------------
LocalToGlobalRgn
Convert a region from local to global coordinates.
Entry: rgn = handle to region.
----------------------------------------------------------------------------*/
void LocalToGlobalRgn (RgnHandle rgn)
{
Point where;
SetPt(&where, 0, 0);
LocalToGlobal(&where);
OffsetRgn(rgn, where.h, where.v);
}
/*----------------------------------------------------------------------------
OutlineRegion
Change a region into a tracing of its border which is appropriate
for normal dragging.
Entry: theRgn = handle to region.
Exit: Region changed to outline of region.
From Apple "HFS Drag Sample" sample code.
----------------------------------------------------------------------------*/
void OutlineRegion (RgnHandle theRgn)
{
RgnHandle tempRgn;
tempRgn = NewRgn();
CopyRgn(theRgn, tempRgn);
InsetRgn(tempRgn, 1, 1);
DiffRgn(theRgn, tempRgn, theRgn);
DisposeRgn(tempRgn);
}
/*----------------------------------------------------------------------------
PtInTEHiliteRgn
Determine whether or not a point is in the current TextEdit hilite
region.
Entry: where = point in local coords.
theTE = handle to TextEdit record.
Exit: function result = true if point is in the hilite region.
----------------------------------------------------------------------------*/
Boolean PtInTEHiliteRgn (Point where, TEHandle theTE)
{
Boolean result = false;
RgnHandle rgn = nil;
OSErr err = noErr;
//if (!HaveTEGetHiliteRgn()) return false;
rgn = NewRgn();
err = TEGetHiliteRgn(rgn, theTE);
if (err != noErr) goto exit;
result = PtInRgn(where, rgn);
exit:
if (rgn != nil) DisposeRgn(rgn);
return result;
}
|