EditCalls.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  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
  22. */
  23. /* $XConsortium: EditCalls.c /main/4 1996/03/26 19:53:27 drk $
  24. **********************************<+>*************************************
  25. ***************************************************************************
  26. **
  27. ** File: EditCalls.c
  28. **
  29. ** Project: DtEditor widget interface for text edit services.
  30. **
  31. ** Description: Contains the public functions related to undo,
  32. ** cut, copy, paste, and the internal Modify/Verify callback.
  33. ** -----------
  34. **
  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. ********************************************************************
  45. **
  46. **
  47. **************************************************************************
  48. **********************************<+>*************************************/
  49. #include "EditorP.h"
  50. #include "DtWidgetI.h"
  51. #include <Xm/XmPrivate.h> /* _XmStringSourceGetString */
  52. void
  53. _DtEditorResetUndo(
  54. DtEditorWidget editor)
  55. {
  56. /*
  57. * Reset deletion & insertion contexts.
  58. */
  59. if( (M_deletedText(editor) != (char *)NULL) &&
  60. (strlen(M_deletedText(editor)) != 0) )
  61. {
  62. XtFree(M_deletedText(editor));
  63. M_deletedText(editor) = (char *) NULL;
  64. }
  65. M_deletionStart(editor) = NO_DELETION_IN_PROGRESS;
  66. M_insertStart(editor) = 0;
  67. M_insertionLength(editor) = 0;
  68. } /* end _DtEditorResetUndo */
  69. Boolean
  70. DtEditorUndoEdit(
  71. Widget widget)
  72. {
  73. DtEditorWidget pPriv = (DtEditorWidget) widget;
  74. _DtWidgetToAppContext(widget);
  75. _DtAppLock(app);
  76. /*
  77. * Remove any insertion, and then put back any previous deletion.
  78. * The tricky part is that removing the insertion looks like a deletion
  79. * and the modifyVerify callback will save in the undo buffer, wiping
  80. * out any previous deletion (before we can reinsert it). So we have to
  81. * save the previous deletion before undoing the insertion so we can
  82. * reinsert it. Make sense?
  83. */
  84. if(M_insertionLength(pPriv) != 0)
  85. {
  86. /*
  87. * There is an insertion so remove everything we just inserted
  88. */
  89. if( (M_deletedText(pPriv) != (char *)NULL) &&
  90. (strlen(M_deletedText(pPriv)) != 0) )
  91. {
  92. /*
  93. * If there is a current deletion, save it before removing the
  94. * insertion so we can put it back (i.e. undo the deletion).
  95. */
  96. int oldDeleteStart = M_deletionStart(pPriv);
  97. char *oldDeletion = M_deletedText(pPriv);
  98. M_deletedText(pPriv) = (char *)NULL;
  99. /*
  100. * 1. Delete the last insertion.
  101. */
  102. XmTextSetSelection(M_text(pPriv), M_insertStart(pPriv),
  103. M_insertStart(pPriv) + M_insertionLength(pPriv),
  104. CurrentTime);
  105. M_insertionLength(pPriv)=0;
  106. XmTextRemove(M_text(pPriv));
  107. /*
  108. * 2. Put back the previous deletion.
  109. */
  110. XmTextInsert(M_text(pPriv), oldDeleteStart, oldDeletion);
  111. XtFree(oldDeletion);
  112. }
  113. else
  114. {
  115. /*
  116. * There is no deletion, so we just have to remove the
  117. * insertion.
  118. */
  119. XmTextSetSelection(M_text(pPriv), M_insertStart(pPriv),
  120. M_insertStart(pPriv) + M_insertionLength(pPriv),
  121. CurrentTime);
  122. M_insertionLength(pPriv) = 0;
  123. XmTextRemove(M_text(pPriv));
  124. }
  125. }
  126. else if(M_deletedText(pPriv) != (char *)NULL)
  127. {
  128. /*
  129. * Nothing has been inserted so just undo the previous deletion.
  130. */
  131. char *oldDeletion = M_deletedText(pPriv);
  132. M_deletedText(pPriv) = (char *)NULL;
  133. XmTextInsert(M_text(pPriv), M_deletionStart(pPriv), oldDeletion);
  134. XtFree(oldDeletion);
  135. }
  136. else
  137. {
  138. /*
  139. * There is no insertion to remove or deletion to put back in
  140. * (i.e. nothing to undo) so return False.
  141. */
  142. _DtAppUnlock(app);
  143. return(False);
  144. }
  145. _DtAppUnlock(app);
  146. return(True);
  147. } /* end DtEditorUndoEdit */
  148. Boolean
  149. DtEditorCutToClipboard(
  150. Widget widget)
  151. {
  152. DtEditorWidget editor = (DtEditorWidget) widget;
  153. XEvent *event;
  154. _DtWidgetToAppContext(widget);
  155. _DtAppLock(app);
  156. /*
  157. * Create an event with a correct timestamp
  158. */
  159. event = (XEvent *) XtMalloc( sizeof(XEvent) );
  160. event->xkey.time = XtLastTimestampProcessed( M_display(editor) );
  161. /*
  162. * Call routine to cut selection to clipboard
  163. */
  164. XtCallActionProc(M_text(editor), "cut-clipboard", event, NULL, 0);
  165. XtFree( (char *) event );
  166. _DtAppUnlock(app);
  167. return(True);
  168. }
  169. Boolean
  170. DtEditorCopyToClipboard(
  171. Widget widget)
  172. {
  173. DtEditorWidget editor = (DtEditorWidget) widget;
  174. XEvent *event;
  175. _DtWidgetToAppContext(widget);
  176. _DtAppLock(app);
  177. /*
  178. * Create an event with a correct timestamp
  179. */
  180. event = (XEvent *) XtMalloc( sizeof(XEvent) );
  181. event->xkey.time = XtLastTimestampProcessed( M_display(editor) );
  182. /*
  183. * Call routine to copy selection to clipboard
  184. */
  185. XtCallActionProc(M_text(editor), "copy-clipboard", event, NULL, 0);
  186. XtFree( (char *) event );
  187. _DtAppUnlock(app);
  188. return(True);
  189. }
  190. Boolean
  191. DtEditorPasteFromClipboard(
  192. Widget widget)
  193. {
  194. DtEditorWidget editor = (DtEditorWidget) widget;
  195. XEvent *event;
  196. _DtWidgetToAppContext(widget);
  197. _DtAppLock(app);
  198. /*
  199. * Create an event with a correct timestamp
  200. */
  201. event = (XEvent *) XtMalloc( sizeof(XEvent) );
  202. event->xkey.time = XtLastTimestampProcessed( M_display(editor) );
  203. /*
  204. * Call routine to paste contents of clipboard at insertion cursor
  205. */
  206. XtCallActionProc(M_text(editor), "paste-clipboard", event, NULL, 0);
  207. XtFree( (char *) event );
  208. _DtAppUnlock(app);
  209. return(True);
  210. }
  211. /*
  212. * SetUndoDeletionState maintains the contents of editStuff.undo related
  213. * to deletion (the deleted text & its original starting position).
  214. *
  215. * SetUndoDeletionState can also reset/invalidate the undo contents related to
  216. * insertion if it detects a "new" deletion. The idea is to treat consecutive
  217. * deletions as atomic from the viewpoint of undo. A delete is
  218. * non-consecutive if it's start or end position is not coincidental with
  219. * the last deletion. One set of consecutive insertions is allowed following
  220. * deletions, and will be undone by the DtEditorUndo(). Non-consecutive
  221. * insertions will reset/invalidate the deletion undo buffer.
  222. */
  223. static void
  224. SetUndoDeletionState(
  225. DtEditorWidget pPriv,
  226. XmTextVerifyCallbackStruct *cb)
  227. {
  228. char *pDeletedText;
  229. /*
  230. * Get the text which will be deleted from the text widget.
  231. */
  232. pDeletedText = (char *)_XmStringSourceGetString(
  233. (XmTextWidget) M_text(pPriv),
  234. cb->startPos,
  235. cb->endPos,
  236. False);
  237. if( M_deletedText(pPriv) != (char *)NULL &&
  238. M_insertionLength(pPriv) == 0 &&
  239. (cb->startPos == M_deletionStart(pPriv) ||
  240. cb->endPos == M_deletionStart(pPriv))
  241. )
  242. {
  243. /*
  244. * Continuation of the current deletion. For a continuation, there
  245. * must have been no intervening insertions, and we must be deleting
  246. * from the same point, either forward or backward.
  247. */
  248. char *oldUndo = M_deletedText(pPriv);
  249. M_deletedText(pPriv) = XtMalloc( strlen(M_deletedText(pPriv)) +
  250. strlen(pDeletedText) + 1 );
  251. if(cb->startPos == M_deletionStart(pPriv)) {
  252. /*
  253. * deleting forward - deletionStart remains the same.
  254. */
  255. strcpy(M_deletedText(pPriv), oldUndo);
  256. strcat(M_deletedText(pPriv), pDeletedText);
  257. }
  258. else {
  259. /*
  260. * deleting backward (e.g. Backspace)
  261. */
  262. strcpy(M_deletedText(pPriv), pDeletedText);
  263. strcat(M_deletedText(pPriv), oldUndo);
  264. M_deletionStart(pPriv) = cb->startPos;
  265. }
  266. if(oldUndo != (char *)NULL)
  267. XtFree(oldUndo);
  268. }
  269. else
  270. {
  271. /*
  272. * Starting a new deletion context. Replace the old deletion
  273. * context, and remove the insertion context.
  274. */
  275. _DtEditorResetUndo( pPriv );
  276. M_deletedText(pPriv) = XtMalloc(strlen(pDeletedText) + 1);
  277. strcpy(M_deletedText(pPriv), pDeletedText);
  278. M_deletionStart(pPriv) = cb->startPos;
  279. M_insertStart(pPriv) = cb->startPos;
  280. M_insertionLength(pPriv) = 0;
  281. }
  282. if( pDeletedText != (char *)NULL )
  283. XtFree( pDeletedText );
  284. } /* SetUndoDeletionState */
  285. /*
  286. * SetUndoInsertionState maintains the contents of editStuff.undo related
  287. * to insertions (the number of characters inserted & the position of the
  288. * first one).
  289. */
  290. static void
  291. SetUndoInsertionState(
  292. DtEditorWidget pPriv,
  293. XmTextVerifyCallbackStruct *cb)
  294. {
  295. if(M_insertionLength(pPriv) == 0) {
  296. /*
  297. * We've started a new deletion context, so reset the insertion
  298. * context.
  299. */
  300. M_insertStart(pPriv) = cb->startPos;
  301. M_insertionLength(pPriv) = _DtEditor_CountCharacters(cb->text->ptr,
  302. cb->text->length);
  303. }
  304. else
  305. {
  306. /*
  307. * Determine if we're continuing the current insertion context
  308. * or beginning a new one.
  309. */
  310. if(cb->startPos == (M_insertStart(pPriv) + M_insertionLength(pPriv)))
  311. M_insertionLength(pPriv) += _DtEditor_CountCharacters(
  312. cb->text->ptr, cb->text->length);
  313. else
  314. {
  315. /*
  316. * We're starting a new insertion context, so invalidate any
  317. * existing deletion context, and reset the insertion context.
  318. */
  319. _DtEditorResetUndo( pPriv );
  320. M_insertStart(pPriv) = cb->startPos;
  321. M_insertionLength(pPriv) = _DtEditor_CountCharacters(
  322. cb->text->ptr, cb->text->length);
  323. }
  324. }
  325. } /* SetUndoInsertionState */
  326. /************************************************************************
  327. *
  328. * _DtEditorModifyVerifyCB - The modify/verify callback
  329. *
  330. * The modify verify callback handles incoming data and the data which
  331. * will be replaced. The replaced data is saved (for later undos).
  332. *
  333. * Parameters:
  334. * widget - the text widget
  335. * client_data - the edit area widget
  336. * call_data - callback structure
  337. *
  338. ************************************************************************/
  339. /* ARGSUSED */
  340. void
  341. _DtEditorModifyVerifyCB(
  342. Widget w,
  343. caddr_t client_data,
  344. caddr_t call_data )
  345. {
  346. register XmTextVerifyCallbackStruct * cb =
  347. (XmTextVerifyCallbackStruct *) call_data;
  348. DtEditorWidget editor = (DtEditorWidget) client_data;
  349. /*
  350. * Loading all new data so no need to set up the data for later undos
  351. */
  352. if (M_loadingAllNewData(editor) == True)
  353. {
  354. _DtEditorResetUndo( editor );
  355. M_unreadChanges(editor) = False;
  356. M_loadingAllNewData(editor) = False;
  357. }
  358. else
  359. {
  360. /*
  361. * Adding additional data, rather than replacing all of the contents.
  362. *
  363. * Mark that the contents have been modified since the last time the
  364. * application requested a copy.
  365. */
  366. M_unreadChanges(editor) = True;
  367. /*
  368. * First, account for any data which will be removed by the new data.
  369. * If text is being deleted, then grab a copy for later undo's.
  370. */
  371. if(cb->endPos > cb->startPos)
  372. SetUndoDeletionState(editor, cb);
  373. /*
  374. * If text is being inserted, then change the undo insertion state.
  375. */
  376. if(cb->text->length > 0)
  377. SetUndoInsertionState(editor, cb);
  378. }
  379. } /* end ModifyVerifyCB */