2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: EditCalls.c /main/4 1996/03/26 19:53:27 drk $
24 **********************************<+>*************************************
25 ***************************************************************************
29 ** Project: DtEditor widget interface for text edit services.
31 ** Description: Contains the public functions related to undo,
32 ** cut, copy, paste, and the internal Modify/Verify callback.
35 *******************************************************************
36 * (c) Copyright 1996 Digital Equipment Corporation.
37 * (c) Copyright 1993, 1994, 1996 Hewlett-Packard Company.
38 * (c) Copyright 1993, 1994, 1996 International Business Machines Corp.
39 * (c) Copyright 1993, 1994, 1996 Sun Microsystems, Inc.
40 * (c) Copyright 1996 Novell, Inc.
41 * (c) Copyright 1996 FUJITSU LIMITED.
42 * (c) Copyright 1996 Hitachi.
43 * (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of Novell, Inc.
44 ********************************************************************
47 **************************************************************************
48 **********************************<+>*************************************/
51 #include "DtWidgetI.h"
52 #include <Xm/XmPrivate.h> /* _XmStringSourceGetString */
56 DtEditorWidget editor)
60 * Reset deletion & insertion contexts.
63 if( (M_deletedText(editor) != (char *)NULL) &&
64 (strlen(M_deletedText(editor)) != 0) )
66 XtFree(M_deletedText(editor));
67 M_deletedText(editor) = (char *) NULL;
70 M_deletionStart(editor) = NO_DELETION_IN_PROGRESS;
71 M_insertStart(editor) = 0;
72 M_insertionLength(editor) = 0;
74 } /* end _DtEditorResetUndo */
81 DtEditorWidget pPriv = (DtEditorWidget) widget;
82 _DtWidgetToAppContext(widget);
86 * Remove any insertion, and then put back any previous deletion.
87 * The tricky part is that removing the insertion looks like a deletion
88 * and the modifyVerify callback will save in the undo buffer, wiping
89 * out any previous deletion (before we can reinsert it). So we have to
90 * save the previous deletion before undoing the insertion so we can
91 * reinsert it. Make sense?
93 if(M_insertionLength(pPriv) != 0)
96 * There is an insertion so remove everything we just inserted
98 if( (M_deletedText(pPriv) != (char *)NULL) &&
99 (strlen(M_deletedText(pPriv)) != 0) )
102 * If there is a current deletion, save it before removing the
103 * insertion so we can put it back (i.e. undo the deletion).
105 int oldDeleteStart = M_deletionStart(pPriv);
106 char *oldDeletion = M_deletedText(pPriv);
107 M_deletedText(pPriv) = (char *)NULL;
110 * 1. Delete the last insertion.
112 XmTextSetSelection(M_text(pPriv), M_insertStart(pPriv),
113 M_insertStart(pPriv) + M_insertionLength(pPriv),
116 M_insertionLength(pPriv)=0;
117 XmTextRemove(M_text(pPriv));
120 * 2. Put back the previous deletion.
122 XmTextInsert(M_text(pPriv), oldDeleteStart, oldDeletion);
129 * There is no deletion, so we just have to remove the
132 XmTextSetSelection(M_text(pPriv), M_insertStart(pPriv),
133 M_insertStart(pPriv) + M_insertionLength(pPriv),
135 M_insertionLength(pPriv) = 0;
136 XmTextRemove(M_text(pPriv));
139 else if(M_deletedText(pPriv) != (char *)NULL)
142 * Nothing has been inserted so just undo the previous deletion.
145 char *oldDeletion = M_deletedText(pPriv);
146 M_deletedText(pPriv) = (char *)NULL;
148 XmTextInsert(M_text(pPriv), M_deletionStart(pPriv), oldDeletion);
155 * There is no insertion to remove or deletion to put back in
156 * (i.e. nothing to undo) so return False.
165 } /* end DtEditorUndoEdit */
168 DtEditorCutToClipboard(
172 DtEditorWidget editor = (DtEditorWidget) widget;
174 _DtWidgetToAppContext(widget);
178 * Create an event with a correct timestamp
180 event = (XEvent *) XtMalloc( sizeof(XEvent) );
181 event->xkey.time = XtLastTimestampProcessed( M_display(editor) );
184 * Call routine to cut selection to clipboard
186 XtCallActionProc(M_text(editor), "cut-clipboard", event, NULL, 0);
188 XtFree( (char *) event );
196 DtEditorCopyToClipboard(
199 DtEditorWidget editor = (DtEditorWidget) widget;
201 _DtWidgetToAppContext(widget);
205 * Create an event with a correct timestamp
207 event = (XEvent *) XtMalloc( sizeof(XEvent) );
208 event->xkey.time = XtLastTimestampProcessed( M_display(editor) );
211 * Call routine to copy selection to clipboard
213 XtCallActionProc(M_text(editor), "copy-clipboard", event, NULL, 0);
215 XtFree( (char *) event );
222 DtEditorPasteFromClipboard(
225 DtEditorWidget editor = (DtEditorWidget) widget;
227 _DtWidgetToAppContext(widget);
231 * Create an event with a correct timestamp
233 event = (XEvent *) XtMalloc( sizeof(XEvent) );
234 event->xkey.time = XtLastTimestampProcessed( M_display(editor) );
237 * Call routine to paste contents of clipboard at insertion cursor
239 XtCallActionProc(M_text(editor), "paste-clipboard", event, NULL, 0);
241 XtFree( (char *) event );
250 * SetUndoDeletionState maintains the contents of editStuff.undo related
251 * to deletion (the deleted text & its original starting position).
253 * SetUndoDeletionState can also reset/invalidate the undo contents related to
254 * insertion if it detects a "new" deletion. The idea is to treat consecutive
255 * deletions as atomic from the viewpoint of undo. A delete is
256 * non-consecutive if it's start or end position is not coincidental with
257 * the last deletion. One set of consecutive insertions is allowed following
258 * deletions, and will be undone by the DtEditorUndo(). Non-consecutive
259 * insertions will reset/invalidate the deletion undo buffer.
262 SetUndoDeletionState(
263 DtEditorWidget pPriv,
264 XmTextVerifyCallbackStruct *cb)
269 * Get the text which will be deleted from the text widget.
271 pDeletedText = (char *)_XmStringSourceGetString(
272 (XmTextWidget) M_text(pPriv),
277 if( M_deletedText(pPriv) != (char *)NULL &&
278 M_insertionLength(pPriv) == 0 &&
279 (cb->startPos == M_deletionStart(pPriv) ||
280 cb->endPos == M_deletionStart(pPriv))
284 * Continuation of the current deletion. For a continuation, there
285 * must have been no intervening insertions, and we must be deleting
286 * from the same point, either forward or backward.
288 char *oldUndo = M_deletedText(pPriv);
289 M_deletedText(pPriv) = XtMalloc( strlen(M_deletedText(pPriv)) +
290 strlen(pDeletedText) + 1 );
292 if(cb->startPos == M_deletionStart(pPriv)) {
294 * deleting forward - deletionStart remains the same.
296 strcpy(M_deletedText(pPriv), oldUndo);
297 strcat(M_deletedText(pPriv), pDeletedText);
301 * deleting backward (e.g. Backspace)
303 strcpy(M_deletedText(pPriv), pDeletedText);
304 strcat(M_deletedText(pPriv), oldUndo);
305 M_deletionStart(pPriv) = cb->startPos;
307 if(oldUndo != (char *)NULL)
313 * Starting a new deletion context. Replace the old deletion
314 * context, and remove the insertion context.
316 _DtEditorResetUndo( pPriv );
318 M_deletedText(pPriv) = XtMalloc(strlen(pDeletedText) + 1);
320 strcpy(M_deletedText(pPriv), pDeletedText);
321 M_deletionStart(pPriv) = cb->startPos;
322 M_insertStart(pPriv) = cb->startPos;
323 M_insertionLength(pPriv) = 0;
326 if( pDeletedText != (char *)NULL )
327 XtFree( pDeletedText );
328 } /* SetUndoDeletionState */
331 * SetUndoInsertionState maintains the contents of editStuff.undo related
332 * to insertions (the number of characters inserted & the position of the
336 SetUndoInsertionState(
337 DtEditorWidget pPriv,
338 XmTextVerifyCallbackStruct *cb)
340 if(M_insertionLength(pPriv) == 0) {
342 * We've started a new deletion context, so reset the insertion
345 M_insertStart(pPriv) = cb->startPos;
346 M_insertionLength(pPriv) = _DtEditor_CountCharacters(cb->text->ptr,
352 * Determine if we're continuing the current insertion context
353 * or beginning a new one.
355 if(cb->startPos == (M_insertStart(pPriv) + M_insertionLength(pPriv)))
356 M_insertionLength(pPriv) += _DtEditor_CountCharacters(
357 cb->text->ptr, cb->text->length);
361 * We're starting a new insertion context, so invalidate any
362 * existing deletion context, and reset the insertion context.
364 _DtEditorResetUndo( pPriv );
365 M_insertStart(pPriv) = cb->startPos;
366 M_insertionLength(pPriv) = _DtEditor_CountCharacters(
367 cb->text->ptr, cb->text->length);
370 } /* SetUndoInsertionState */
373 /************************************************************************
375 * _DtEditorModifyVerifyCB - The modify/verify callback
377 * The modify verify callback handles incoming data and the data which
378 * will be replaced. The replaced data is saved (for later undos).
381 * widget - the text widget
382 * client_data - the edit area widget
383 * call_data - callback structure
385 ************************************************************************/
389 _DtEditorModifyVerifyCB(
394 register XmTextVerifyCallbackStruct * cb =
395 (XmTextVerifyCallbackStruct *) call_data;
396 DtEditorWidget editor = (DtEditorWidget) client_data;
399 * Loading all new data so no need to set up the data for later undos
401 if (M_loadingAllNewData(editor) == True)
403 _DtEditorResetUndo( editor );
404 M_unreadChanges(editor) = False;
405 M_loadingAllNewData(editor) = False;
411 * Adding additional data, rather than replacing all of the contents.
413 * Mark that the contents have been modified since the last time the
414 * application requested a copy.
416 M_unreadChanges(editor) = True;
419 * First, account for any data which will be removed by the new data.
420 * If text is being deleted, then grab a copy for later undo's.
422 if(cb->endPos > cb->startPos)
423 SetUndoDeletionState(editor, cb);
426 * If text is being inserted, then change the undo insertion state.
428 if(cb->text->length > 0)
429 SetUndoInsertionState(editor, cb);
433 } /* end ModifyVerifyCB */