TermPrimSelect.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091
  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 libraries 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. /* *
  24. * (c) Copyright 1993, 1994, 1996 Hewlett-Packard Company *
  25. * (c) Copyright 1993, 1994, 1996 International Business Machines Corp. *
  26. * (c) Copyright 1993, 1994, 1996 Sun Microsystems, Inc. *
  27. * (c) Copyright 1993, 1994, 1996 Novell, Inc. *
  28. * (c) Copyright 1995, 1996 Digital Equipment Corporation. *
  29. * (c) Copyright 1996 FUJITSU LIMITED. *
  30. * (c) Copyright 1996 Hitachi. *
  31. */
  32. #include "TermHeader.h"
  33. #include <X11/Xatom.h>
  34. #include <Xm/Xm.h>
  35. #include <Xm/AtomMgr.h>
  36. #include <Xm/CutPaste.h>
  37. #include <Xm/XmPrivate.h>
  38. #include <Xm/ScrollBarP.h>
  39. #include "TermPrimAction.h"
  40. #include "TermPrimDebug.h"
  41. #include "TermPrimP.h"
  42. #include "TermPrimData.h"
  43. #include "TermPrimRender.h"
  44. #include "TermPrimSelectP.h"
  45. #include "TermPrimBufferP.h"
  46. #include <Xm/DropSMgr.h>
  47. #include <Xm/DropTrans.h>
  48. #include <ctype.h>
  49. #include <wctype.h>
  50. /* This is for Sun's two button mouse */
  51. static char _DtTermEventBindingsCDE[] = "\
  52. ~c ~s ~m ~a <Btn1Down>:process-press(grab-focus,process-bdrag)\n\
  53. ~c s ~m ~a <Btn1Down>:process-press(extend-start,process-bdrag)\n\
  54. ~c ~m ~a <Btn1Motion>:select-adjust()\n\
  55. ~c ~m ~a <Btn1Up>:extend-end()";
  56. static char _DtTermEventBindingsCDEBtn2[] = "\
  57. <Btn2Down>:extend-start()\n\
  58. <Btn2Motion>:select-adjust()\n\
  59. <Btn2Up>:extend-end()";
  60. static
  61. XmTextScanType defaultScanArray[] =
  62. {
  63. XmSELECT_POSITION,
  64. XmSELECT_WORD,
  65. XmSELECT_LINE,
  66. XmSELECT_ALL
  67. };
  68. static void RegisterDropSite( Widget w );
  69. static void doExtendedSelection (Widget w,Time eventTime);
  70. /*
  71. ** Get the current server time (I ripped this off from Xm/TextIn.c).
  72. */
  73. static Time
  74. getServerTime
  75. (
  76. Widget w
  77. )
  78. {
  79. XEvent event;
  80. EventMask shellMask;
  81. while(!XtIsShell(w))
  82. {
  83. w = XtParent(w);
  84. }
  85. shellMask = XtBuildEventMask(w);
  86. if (!(shellMask & PropertyChangeMask))
  87. {
  88. XSelectInput(XtDisplay(w), XtWindow(w), shellMask | PropertyChangeMask);
  89. }
  90. XChangeProperty(XtDisplay(w), XtWindow(w), XA_WM_HINTS, XA_WM_HINTS,
  91. 32, PropModeAppend, (unsigned char *)NULL, 0);
  92. XWindowEvent(XtDisplay(w), XtWindow(w), PropertyChangeMask, &event);
  93. if (!(shellMask & PropertyChangeMask))
  94. {
  95. XSelectInput(XtDisplay(w), XtWindow(w), shellMask);
  96. }
  97. return(event.xproperty.time);
  98. }
  99. static void
  100. setScanType
  101. (
  102. Widget w,
  103. XEvent *event
  104. )
  105. {
  106. TermSelectInfo selectInfo =
  107. ((DtTermPrimitiveWidget)w)->term.tpd->selectInfo;
  108. int multiClickTime;
  109. int i;
  110. multiClickTime = XtGetMultiClickTime(XtDisplay(w));
  111. if (event->xbutton.time > selectInfo->lastTime &&
  112. event->xbutton.time - selectInfo->lastTime <
  113. (multiClickTime == 200 ? 500 : multiClickTime))
  114. {
  115. i = 0;
  116. while (i < selectInfo->scanArraySize &&
  117. selectInfo->scanArray[i] != selectInfo->scanType)
  118. {
  119. i++;
  120. }
  121. if (++i >= selectInfo->scanArraySize)
  122. {
  123. i = 0;
  124. }
  125. selectInfo->scanType = selectInfo->scanArray[i];
  126. }
  127. else
  128. {
  129. /* single-click event */
  130. selectInfo->scanType = selectInfo->scanArray[0];
  131. }
  132. selectInfo->lastTime = event->xbutton.time;
  133. }
  134. /*
  135. ** convert a row,col pair into the equivalent XmTextPosition
  136. **
  137. ** NOTE:
  138. ** this routine assumes that the calling routine as already checked
  139. ** row and col to insure that they are within the bounds of the terminal
  140. ** buffer (see _DtTermPrimSelectGrabFocus)
  141. */
  142. XmTextPosition
  143. rowColToPos
  144. (
  145. DtTermPrimitiveWidget tw,
  146. short row,
  147. short col
  148. )
  149. {
  150. DtTermPrimData tpd = tw->term.tpd;
  151. return(((tpd->selectInfo->columns + 1) *
  152. (row + tpd->lastUsedHistoryRow)) + col);
  153. }
  154. /*
  155. ** getSelection
  156. */
  157. Boolean
  158. _DtTermPrimSelectGetSelection
  159. (
  160. Widget w,
  161. XmTextPosition *begin,
  162. XmTextPosition *end
  163. )
  164. {
  165. TermSelectInfo selectInfo =
  166. ((DtTermPrimitiveWidget)w)->term.tpd->selectInfo;
  167. if (selectInfo->ownPrimary &&
  168. (selectInfo->begin <= selectInfo->end) &&
  169. selectInfo->begin >= 0)
  170. {
  171. *begin = selectInfo->begin;
  172. *end = selectInfo->end;
  173. return(True);
  174. }
  175. else
  176. {
  177. *begin = 0;
  178. *end = 0;
  179. selectInfo->ownPrimary = False;
  180. return(False);
  181. }
  182. }
  183. /*
  184. ** convert an x,y pair into the appropriate text position
  185. **
  186. ** Since positions count the number of inter-character spaces, there is
  187. ** one more x position on a line than columns on a line; xPos can be in
  188. ** the range (0, selectInfo->columns + 1). The same is true for yPos,
  189. ** it can be in the range (0, tpd->lastUsedRow - tpd->topRow - 1)
  190. **
  191. ** In the case that we have a history buffer to deal with, text positions
  192. ** in the history buffer are forced to come before positions in the term
  193. ** buffer.
  194. **
  195. ** NOTE:
  196. ** this routine assumes that the calling routine as already checked
  197. ** x and y to insure that they are within the bounds of the terminal
  198. ** window (see _DtTermPrimSelectGrabFocus)
  199. ** NOTE:
  200. ** I believe I'm now doing all checking in this routine for confining
  201. ** the x,y to the window. Disregard the previous note. TMH
  202. */
  203. static
  204. XmTextPosition
  205. xyToPos
  206. (
  207. DtTermPrimitiveWidget tw,
  208. int x, /* pixel */
  209. int y /* pixel */
  210. )
  211. {
  212. DtTermPrimData tpd = tw->term.tpd;
  213. TermSelectInfo selectInfo = tpd->selectInfo;
  214. TermBuffer tb;
  215. short row;
  216. short yPos;
  217. short xPos;
  218. static short oldYPos = -1;
  219. static short oldXPos = -1;
  220. if ( x<0) x = 0;
  221. if ( x > (int) tw->core.width) x = tw->core.width;
  222. /*
  223. ** convert pixel units to character positions
  224. */
  225. yPos = (MAX(0, y) - tpd->offsetY) / tpd->cellHeight;
  226. /*
  227. ** yPos cannot exceed the buffer or screen
  228. */
  229. yPos = MIN(yPos, MIN(tw->term.rows, tpd->lastUsedRow - tpd->topRow) - 1) +
  230. tpd->topRow;
  231. /*
  232. ** consider the possibility that we have a history buffer
  233. */
  234. if (tpd->useHistoryBuffer)
  235. {
  236. if (yPos < 0)
  237. {
  238. /*
  239. ** yPos is not in the history buffer (order is important,
  240. ** step 2 must come before step 3):
  241. ** 1) point to history buffer
  242. ** 2) adjust yPos
  243. ** 3) decide which row of the buffer we are concerned
  244. ** with
  245. */
  246. tb = tpd->historyBuffer;
  247. yPos += tpd->lastUsedHistoryRow;
  248. row = yPos;
  249. }
  250. else
  251. {
  252. /*
  253. ** yPos is not in the history buffer (order is important,
  254. ** step 2 must come before step 3):
  255. ** 1) point to term buffer
  256. ** 2) decide which row of the buffer we are concerned
  257. ** with
  258. ** 3) adjust yPos
  259. */
  260. tb = tpd->termBuffer;
  261. row = yPos;
  262. yPos += tpd->lastUsedHistoryRow;
  263. }
  264. }
  265. else
  266. {
  267. tb = tpd->termBuffer;
  268. row = yPos;
  269. }
  270. xPos = (((x - tpd->offsetX) + (tpd->cellWidth / 2)) / tpd->cellWidth) ;
  271. if ( MB_CUR_MAX > 1 ) /* check if xPos splits a 2 col char */
  272. {
  273. TermCharInfoRec charInfoRec ;
  274. if (_DtTermPrimGetCharacterInfo(tb,row,xPos,&charInfoRec) )
  275. {
  276. if (charInfoRec.width == 2 && charInfoRec.startCol != xPos)
  277. {
  278. if (xPos*tpd->cellWidth < x - tpd->offsetX )
  279. xPos++ ; /* set to right of char */
  280. else
  281. xPos-- ; /* set to left of char */
  282. }
  283. }
  284. }
  285. if ((yPos != oldYPos) || (xPos != oldXPos))
  286. {
  287. oldYPos = yPos;
  288. oldXPos = xPos;
  289. }
  290. return (((selectInfo->columns + 1) * yPos) + xPos);
  291. }
  292. /*
  293. * Takes a linear position and return buffer, row, and col.
  294. * Since positions are between characters, this returns the col to
  295. * right of the position.
  296. */
  297. static void
  298. posToBufferRowCol
  299. (
  300. DtTermPrimitiveWidget tw,
  301. XmTextPosition pos,
  302. TermBuffer *pb,
  303. short *row,
  304. short *col
  305. )
  306. {
  307. DtTermPrimData tpd = tw->term.tpd;
  308. TermSelectInfo selectInfo = tpd->selectInfo;
  309. short lrow, lcol;
  310. lrow = pos / (selectInfo->columns + 1);
  311. lcol = pos - (lrow * (selectInfo->columns + 1));
  312. if ( tpd->useHistoryBuffer ) lrow -= tpd->lastUsedHistoryRow ;
  313. if ( lrow < 0 ) { /* in history buffer */
  314. *pb=tw->term.tpd->historyBuffer ;
  315. lrow += tpd->lastUsedHistoryRow ;
  316. }
  317. else
  318. {
  319. *pb = tpd->termBuffer ;
  320. }
  321. *row = lrow ;
  322. *col = lcol ;
  323. }
  324. /*
  325. * Takes a buffer, row and column and returns the linear position.
  326. */
  327. static XmTextPosition
  328. bufferRowColToPos
  329. (
  330. DtTermPrimitiveWidget tw,
  331. TermBuffer pb,
  332. short row,
  333. short col
  334. )
  335. {
  336. DtTermPrimData tpd = tw->term.tpd;
  337. TermSelectInfo selectInfo = tpd->selectInfo;
  338. short lrow, lcol;
  339. XmTextPosition pos;
  340. /* assume row, col in the history buffer or there is no history */
  341. pos = (tpd->selectInfo->columns + 1) * row + col;
  342. if ( tpd->useHistoryBuffer && pb == tpd->termBuffer)
  343. pos += (tpd->selectInfo->columns + 1) * (tpd->lastUsedHistoryRow) ;
  344. return(pos) ;
  345. }
  346. static XmTextPosition
  347. scan
  348. (
  349. DtTermPrimitiveWidget tw,
  350. XmTextPosition scanStart,
  351. XmTextScanType scanType,
  352. TermScanDirection scanDir,
  353. int count,
  354. Boolean inclusive
  355. )
  356. {
  357. int i;
  358. DtTermPrimData tpd = tw->term.tpd;
  359. TermSelectInfo selectInfo = tpd->selectInfo;
  360. XmTextPosition position = scanStart;
  361. short row;
  362. short col;
  363. TermBuffer pb ;
  364. switch(scanType)
  365. {
  366. case XmSELECT_POSITION:
  367. posToBufferRowCol(tw, position, &pb, &row, &col) ;
  368. if ( col > _DtTermPrimBufferGetLineWidth(pb, row) )
  369. {
  370. col = selectInfo->columns + 1;
  371. position = bufferRowColToPos(tw,pb,row,col) ;
  372. }
  373. break;
  374. case XmSELECT_WORD:
  375. {
  376. short width;
  377. posToBufferRowCol(tw, position, &pb, &row, &col) ;
  378. width = _DtTermPrimBufferGetLineWidth(pb,row);
  379. if ( col > width ) break;
  380. if ( MB_CUR_MAX > 1 )
  381. {
  382. TermCharInfoRec charInfoRec ;
  383. _DtTermPrimGetCharacterInfo(pb,row,col,&charInfoRec);
  384. col = charInfoRec.startCol ; /* align first */
  385. switch(scanDir)
  386. {
  387. case scanLeft:
  388. _DtTermPrimGetCharacterInfo(pb,row,col?--col:0,&charInfoRec);
  389. while( !iswspace(*(wchar_t *)charInfoRec.u.pwc) &&
  390. ((col=charInfoRec.startCol-1)>=0) )
  391. {
  392. _DtTermPrimGetCharacterInfo(pb,row,col,&charInfoRec) ;
  393. }
  394. col ++ ;
  395. break;
  396. case scanRight:
  397. _DtTermPrimGetCharacterInfo(pb,row,col,&charInfoRec);
  398. while(++col<=width && !iswspace(*(wchar_t *)charInfoRec.u.pwc))
  399. _DtTermPrimGetCharacterInfo(pb,row,col,&charInfoRec);
  400. col--;
  401. break;
  402. }
  403. }
  404. else
  405. {
  406. char pbuf[10];
  407. switch(scanDir)
  408. {
  409. case scanLeft:
  410. _DtTermPrimBufferGetText(pb, row, col?--col:0, 1, pbuf, False);
  411. while( !isspace(*pbuf) && --col >= 0)
  412. _DtTermPrimBufferGetText(pb, row, col, 1, pbuf, False );
  413. col++ ;
  414. break;
  415. case scanRight:
  416. _DtTermPrimBufferGetText(pb, row, col, 1, pbuf, False);
  417. while( ++col <= width && !isspace(*pbuf))
  418. _DtTermPrimBufferGetText(pb, row, col, 1, pbuf, False );
  419. col--;
  420. break;
  421. }
  422. }
  423. position = bufferRowColToPos(tw,pb,row,col) ;
  424. }
  425. break;
  426. case XmSELECT_LINE:
  427. {
  428. posToBufferRowCol(tw, position, &pb, &row, &col) ;
  429. col = 0;
  430. switch(scanDir)
  431. {
  432. case scanLeft:
  433. break;
  434. case scanRight:
  435. col = selectInfo->columns + 1;
  436. break;
  437. }
  438. position = bufferRowColToPos(tw,pb,row,col) ;
  439. }
  440. break;
  441. case XmSELECT_ALL:
  442. switch(scanDir)
  443. {
  444. case scanLeft:
  445. position = 0;
  446. break;
  447. case scanRight:
  448. pb = tpd->termBuffer ;
  449. row = tpd->lastUsedRow-1;
  450. col = _DtTermPrimBufferGetLineWidth(pb,row) ;
  451. position = bufferRowColToPos(tw,pb,row,col) ;
  452. break;
  453. }
  454. break;
  455. }
  456. return(position);
  457. }
  458. /*
  459. ** refresh all text from start up to stop
  460. **
  461. ** NOTE:
  462. ** We assume that start is always <= than stop
  463. */
  464. void
  465. _DtTermPrimRenderRefreshTextLinear
  466. (
  467. Widget w,
  468. XmTextPosition start,
  469. XmTextPosition stop
  470. )
  471. {
  472. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  473. DtTermPrimData tpd = tw->term.tpd;
  474. TermSelectInfo selectInfo = tpd->selectInfo;
  475. short startRow, startCol;
  476. short stopRow , stopCol;
  477. /*
  478. ** Turn XmTextPosition into a row and column
  479. */
  480. startRow = start / (selectInfo->columns + 1);
  481. startCol = start - (startRow * (selectInfo->columns + 1));
  482. stopRow = stop / (selectInfo->columns + 1);
  483. stopCol = stop - (stopRow * (selectInfo->columns + 1));
  484. /*
  485. ** Accomodate the history buffer as necessary
  486. */
  487. if (tpd->useHistoryBuffer)
  488. {
  489. startRow -= tpd->lastUsedHistoryRow;
  490. stopRow -= tpd->lastUsedHistoryRow;
  491. }
  492. /*
  493. ** Now adjust for the top of the window
  494. */
  495. startRow -= tpd->topRow;
  496. stopRow -= tpd->topRow;
  497. /*
  498. ** refresh the first (and possibly only) line
  499. */
  500. if (startRow == stopRow)
  501. {
  502. _DtTermPrimRefreshText((Widget)tw, startCol, startRow,
  503. stopCol, startRow);
  504. return;
  505. }
  506. _DtTermPrimRefreshText((Widget)tw, startCol, startRow,
  507. selectInfo->columns - 1, startRow);
  508. /*
  509. ** refresh the middle block (if there is one)
  510. */
  511. if (startRow++ < stopRow)
  512. {
  513. _DtTermPrimRefreshText((Widget)tw, 0, startRow,
  514. selectInfo->columns - 1, stopRow - 1);
  515. }
  516. /*
  517. ** refresh the last line
  518. */
  519. _DtTermPrimRefreshText((Widget)tw, 0, stopRow, stopCol, stopRow);
  520. }
  521. static void
  522. setSelection
  523. (
  524. DtTermPrimitiveWidget tw,
  525. XmTextPosition begin,
  526. XmTextPosition end,
  527. Time selectTime,
  528. Boolean fromLoseSelection
  529. )
  530. {
  531. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  532. XmTextPosition oldBegin, oldEnd;
  533. Boolean disJoint; /* true if new and current are disjoint */
  534. short selectLineBegin;
  535. short selectColBegin;
  536. short selectLineEnd;
  537. short selectColEnd;
  538. Debug('c', fprintf(stderr, ">>setSelection() starting\n"));
  539. if (selectInfo->ownPrimary == False &&
  540. begin > end)
  541. {
  542. Debug('c', fprintf(stderr, ">>setSelection() finishing a\n"));
  543. return;
  544. }
  545. if (begin < 0)
  546. {
  547. begin = 0;
  548. end = 0;
  549. }
  550. if (selectInfo->ownPrimary)
  551. {
  552. /*
  553. ** we own the selection see how much (if any) of the selected
  554. ** area needs to be unhighlighted...
  555. */
  556. if (selectInfo->begin < selectInfo->end)
  557. {
  558. /*
  559. ** We own the selection, and its highlighted...
  560. */
  561. if ((end <= selectInfo->begin) ||
  562. (begin >= selectInfo->end))
  563. {
  564. /*
  565. ** The two areas don't intersect, simply clear the old
  566. ** area...
  567. */
  568. Debug('c', fprintf(stderr, " new & old are disjoint\n"));
  569. selectInfo->ownPrimary = False;
  570. _DtTermPrimRenderRefreshTextLinear((Widget)tw,
  571. selectInfo->begin,
  572. selectInfo->end - 1);
  573. selectInfo->ownPrimary = True;
  574. disJoint = True;
  575. }
  576. else
  577. {
  578. /*
  579. ** There is some intersection, save the current begin
  580. ** and end so we can clean things up later.
  581. */
  582. Debug('c', fprintf(stderr, " new & old intersect\n"));
  583. oldBegin = selectInfo->begin;
  584. oldEnd = selectInfo->end;
  585. disJoint = False;
  586. }
  587. }
  588. else
  589. {
  590. /*
  591. ** We own the selection, but nothing is highlighted...
  592. */
  593. disJoint = True;
  594. }
  595. }
  596. else
  597. {
  598. /*
  599. ** we don't own the selection (yet), come up with some reasonable
  600. ** defaults
  601. */
  602. disJoint = True;
  603. oldBegin = begin;
  604. oldEnd = end;
  605. }
  606. selectInfo->begin = begin;
  607. selectInfo->end = end;
  608. if (begin <= end)
  609. {
  610. if (selectInfo->ownPrimary == False)
  611. {
  612. if (!XtOwnSelection((Widget)tw, XA_PRIMARY, selectTime,
  613. _DtTermPrimSelectConvert,
  614. _DtTermPrimSelectLoseSelection,
  615. (XtSelectionDoneProc) NULL))
  616. {
  617. /*
  618. ** XtOwnSelection failed, make a dummy call to setSelection
  619. ** (with begin > end) to clear things up...
  620. */
  621. setSelection(tw, 1, -99, selectTime, False);
  622. }
  623. else
  624. {
  625. selectInfo->ownPrimary = True;
  626. selectInfo->primaryTime = selectTime;
  627. }
  628. }
  629. /*
  630. ** now highlight the currently selected text...
  631. */
  632. if (selectInfo->ownPrimary)
  633. {
  634. if (disJoint == True)
  635. {
  636. /*
  637. ** the selections are disjoint, simply draw the new one
  638. */
  639. _DtTermPrimRenderRefreshTextLinear((Widget)tw, begin, end - 1);
  640. }
  641. else
  642. {
  643. if (begin != oldBegin)
  644. {
  645. if (begin < oldBegin)
  646. {
  647. /*
  648. ** refresh from the new beginning to the old
  649. ** beginning
  650. */
  651. _DtTermPrimRenderRefreshTextLinear((Widget)tw, begin,
  652. oldBegin - 1);
  653. }
  654. else if (oldBegin < begin)
  655. {
  656. /*
  657. ** refresh from the old beginning to the new
  658. ** beginning
  659. **
  660. ** NOTE: in this case we want to unhighlight
  661. ** previously selected text, so we
  662. ** temporarily set ownPrimary to false
  663. */
  664. selectInfo->ownPrimary = False;
  665. _DtTermPrimRenderRefreshTextLinear((Widget)tw, oldBegin,
  666. begin - 1);
  667. selectInfo->ownPrimary = True;
  668. }
  669. }
  670. if (end != oldEnd)
  671. {
  672. if (end < oldEnd)
  673. {
  674. /*
  675. ** refresh from the new end to the original end
  676. **
  677. ** NOTE: in this case we want to unhighlight
  678. ** previously selected text, so we
  679. ** temporarily set ownPrimary to false
  680. */
  681. selectInfo->ownPrimary = False;
  682. _DtTermPrimRenderRefreshTextLinear((Widget)tw, end,
  683. oldEnd - 1);
  684. selectInfo->ownPrimary = True;
  685. }
  686. else if (oldEnd < end)
  687. {
  688. /*
  689. ** refresh from the old end to the new end.
  690. */
  691. _DtTermPrimRenderRefreshTextLinear((Widget)tw, oldEnd,
  692. end - 1);
  693. }
  694. }
  695. }
  696. }
  697. }
  698. else
  699. {
  700. if (!fromLoseSelection)
  701. {
  702. XtDisownSelection((Widget)tw, XA_PRIMARY, selectTime);
  703. }
  704. selectInfo->ownPrimary = False;
  705. }
  706. selectLineBegin = selectInfo->begin / (selectInfo->columns + 1);
  707. selectColBegin = selectInfo->begin % (selectInfo->columns + 1);
  708. selectLineEnd = (selectInfo->end - 1) / (selectInfo->columns + 1);
  709. selectColEnd = (selectInfo->end - 1) % (selectInfo->columns + 1);
  710. DebugF('c', 1,
  711. fprintf(stderr, "set selection units: %ld-%ld lines: %d-%d\n",
  712. selectInfo->begin, selectInfo->end,
  713. selectLineBegin,
  714. selectLineEnd));
  715. if (tw->term.tpd->useHistoryBuffer && tw->term.tpd->lastUsedHistoryRow>0) {
  716. if (selectLineEnd > tw->term.tpd->lastUsedHistoryRow) {
  717. (void) _DtTermPrimBufferSetSelectLines(tw->term.tpd->historyBuffer,
  718. selectLineBegin, selectColBegin,
  719. tw->term.tpd->lastUsedHistoryRow - 1, selectInfo->columns);
  720. } else {
  721. (void) _DtTermPrimBufferSetSelectLines(tw->term.tpd->historyBuffer,
  722. selectLineBegin, selectColBegin,
  723. selectLineEnd, selectColEnd);
  724. }
  725. selectLineBegin -= tw->term.tpd->lastUsedHistoryRow;
  726. if (selectLineBegin < 0) {
  727. selectLineBegin = 0;
  728. selectColBegin = 0;
  729. }
  730. selectLineEnd -= tw->term.tpd->lastUsedHistoryRow;
  731. }
  732. if (selectLineEnd > tw->term.tpd->lastUsedRow) {
  733. (void) _DtTermPrimBufferSetSelectLines(tw->term.tpd->termBuffer,
  734. selectLineBegin, selectColBegin,
  735. tw->term.tpd->lastUsedRow, selectInfo->columns);
  736. } else {
  737. (void) _DtTermPrimBufferSetSelectLines(tw->term.tpd->termBuffer,
  738. selectLineBegin, selectColBegin,
  739. selectLineEnd, selectColEnd);
  740. }
  741. Debug('c', fprintf(stderr, ">>setSelection() finishing b\n"));
  742. }
  743. static void
  744. handleSelection
  745. (
  746. DtTermPrimitiveWidget tw,
  747. int x,
  748. int y,
  749. Time selectTime
  750. )
  751. {
  752. XmTextPosition position;
  753. XmTextPosition newBegin;
  754. XmTextPosition newEnd;
  755. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  756. Debug('c', fprintf(stderr, ">>handleSelection() starting\n"));
  757. position = xyToPos(tw, x, y);
  758. newBegin = scan(tw, position, selectInfo->scanType, scanLeft,
  759. 1, False);
  760. newEnd = scan(tw, position, selectInfo->scanType, scanRight,
  761. 1, selectInfo->scanType == XmSELECT_LINE);
  762. setSelection(tw, newBegin, newEnd, selectTime, False);
  763. if ((position - newBegin) <
  764. (newEnd - position))
  765. {
  766. selectInfo->extendDir = scanLeft;
  767. }
  768. else
  769. {
  770. selectInfo->extendDir = scanRight;
  771. }
  772. selectInfo->origBegin = newBegin;
  773. selectInfo->origEnd = newEnd;
  774. }
  775. /************************************************************************
  776. * *
  777. * browseScroll - timer proc that scrolls the list if the user has left *
  778. * the window with the button down. If the button has been *
  779. * released, call the standard click stuff. *
  780. * *
  781. ************************************************************************/
  782. /* ARGSUSED */
  783. static void
  784. browseScroll
  785. (
  786. XtPointer closure,
  787. XtIntervalId *id
  788. )
  789. {
  790. Widget w = (Widget) closure;
  791. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  792. DtTermPrimData tpd = tw->term.tpd;
  793. TermSelectInfo selectInfo = tpd->selectInfo ;
  794. XmScrollBarWidget vsb = (XmScrollBarWidget) tw->term.verticalScrollBar;
  795. unsigned long interval;
  796. if (selectInfo->cancel) {
  797. selectInfo->selectID = 0;
  798. return;
  799. }
  800. if (!selectInfo->selectID) return;
  801. _DtTermPrimScrollComplete(w, True);
  802. if ( selectInfo->isScrollUp ) {
  803. if ( tpd->lastUsedRow-1 >= tpd->topRow + tw->term.rows)
  804. _DtTermPrimScrollText(w, 1);
  805. }
  806. else
  807. _DtTermPrimScrollText(w, -1);
  808. _DtTermPrimScrollComplete(w, True);
  809. if (selectInfo->extending)
  810. doExtendedSelection(w, XtLastTimestampProcessed(XtDisplay(w)));
  811. if (vsb)
  812. interval = (unsigned long) vsb->scrollBar.repeat_delay;
  813. else
  814. interval = 100;
  815. XSync (XtDisplay(w), False);
  816. selectInfo->selectID = XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  817. interval, browseScroll, (XtPointer) w);
  818. }
  819. /* ARGSUSED */
  820. static Boolean
  821. CheckTimerScrolling
  822. (
  823. Widget w,
  824. XEvent *event
  825. )
  826. {
  827. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  828. DtTermPrimData tpd = tw->term.tpd;
  829. TermSelectInfo selectInfo = tpd->selectInfo;
  830. XmScrollBarWidget vsb = (XmScrollBarWidget) tw->term.verticalScrollBar;
  831. unsigned long interval;
  832. selectInfo->extend.x = event->xmotion.x;
  833. selectInfo->extend.y = event->xmotion.y;
  834. if ( (event->xmotion.y > (int) tpd->offsetY) &&
  835. (event->xmotion.y < (int) (tpd->offsetY + tw->term.rows *
  836. tpd->cellHeight))) {
  837. if (selectInfo->selectID) {
  838. XtRemoveTimeOut(selectInfo->selectID);
  839. selectInfo->selectID = 0;
  840. }
  841. } else {
  842. /* above the text */
  843. if (event->xmotion.y <= (int) tpd->offsetY) {
  844. selectInfo->extend.x = 0;
  845. selectInfo->extend.y = (int) (tpd->offsetY);
  846. selectInfo->isScrollUp = False ;
  847. /* below the text */
  848. } else if (event->xmotion.y >= (int) (tpd->offsetY + tw->term.rows *
  849. tpd->cellHeight)) {
  850. selectInfo->extend.x = tw->core.width;
  851. selectInfo->extend.y = (int) (tpd->offsetY + tw->term.rows *
  852. tpd->cellHeight);
  853. selectInfo->isScrollUp = True ;
  854. }
  855. if (vsb)
  856. interval = (unsigned long) vsb->scrollBar.initial_delay;
  857. else
  858. interval = 200;
  859. if (!selectInfo->selectID)
  860. selectInfo->selectID = XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  861. interval, browseScroll, (XtPointer) w);
  862. return True;
  863. }
  864. return False;
  865. }
  866. /*
  867. ** Create and initialize the selection specific information
  868. */
  869. TermSelectInfo
  870. _DtTermPrimSelectCreate
  871. (
  872. Widget w
  873. )
  874. {
  875. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  876. DtTermPrimData tpd = tw->term.tpd;
  877. TermSelectInfo selectInfo;
  878. int i;
  879. selectInfo = (TermSelectInfo)XtMalloc(sizeof(TermSelectInfoRec));
  880. selectInfo->begin = 0;
  881. selectInfo->end = 0;
  882. selectInfo->columns = tw->term.columns;
  883. selectInfo->rows = tpd->bufferRows + tpd->historyBufferRows;
  884. selectInfo->direction = (TermScanDirection) XmTEXT_FORWARD;
  885. selectInfo->extend.x = 0;
  886. selectInfo->extend.y = 0;
  887. selectInfo->extending = False;
  888. selectInfo->hint.x = 0;
  889. selectInfo->hint.y = 0;
  890. selectInfo->lastTime = 0;
  891. selectInfo->origBegin = 0;
  892. selectInfo->origEnd = 0;
  893. selectInfo->ownPrimary = False;
  894. selectInfo->threshold = 5;
  895. selectInfo->selectID = 0;
  896. selectInfo->selectType = TermSelect_NORMAL;
  897. selectInfo->scanType = defaultScanArray[0];
  898. selectInfo->scanArraySize = XtNumber(defaultScanArray);
  899. selectInfo->scanArray = (XmTextScanType *)
  900. XtMalloc(selectInfo->scanArraySize *
  901. sizeof(XmTextScanType));
  902. selectInfo->cancel = True; /* used by scroll selection */
  903. selectInfo->anchor = -1; /* in case extend happens before set*/
  904. selectInfo->sel_start = False;
  905. for (i = 0; i < selectInfo->scanArraySize; i++)
  906. {
  907. selectInfo->scanArray[i] = defaultScanArray[i];
  908. }
  909. RegisterDropSite(w);
  910. return(selectInfo);
  911. }
  912. void
  913. _DtTermPrimSelectInitBtnEvents(Widget w)
  914. {
  915. Boolean btn1_transfer = False;
  916. XtVaGetValues((Widget)XmGetXmDisplay(XtDisplay(w)), "enableBtn1Transfer",
  917. &btn1_transfer, NULL);
  918. if (btn1_transfer)
  919. XtOverrideTranslations(w,
  920. XtParseTranslationTable(_DtTermEventBindingsCDE));
  921. if (btn1_transfer == True) /* for btn2 extend case */
  922. XtOverrideTranslations(w,
  923. XtParseTranslationTable(_DtTermEventBindingsCDEBtn2));
  924. }
  925. void
  926. _DtTermPrimSelectDisown
  927. (
  928. Widget w
  929. )
  930. {
  931. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  932. DtTermPrimData tpd = tw->term.tpd;
  933. TermSelectInfo selectInfo = tpd->selectInfo;
  934. if (selectInfo->ownPrimary == True)
  935. {
  936. XtDisownSelection(w, XA_PRIMARY, getServerTime(w));
  937. selectInfo->ownPrimary = False ;
  938. }
  939. }
  940. void
  941. _DtTermPrimSelectDestroy
  942. (
  943. Widget w,
  944. TermSelectInfo selectInfo
  945. )
  946. {
  947. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  948. DtTermPrimData tpd = tw->term.tpd;
  949. if (selectInfo->ownPrimary == True)
  950. {
  951. XtDisownSelection(w, XA_PRIMARY, getServerTime(w));
  952. }
  953. selectInfo->ownPrimary = False ;
  954. XtFree((char *) selectInfo->scanArray);
  955. XtFree((char *) selectInfo);
  956. tpd->selectInfo = NULL ;
  957. }
  958. /*
  959. ** determine how much (if any) of the text is selected
  960. **
  961. ** NOTE:
  962. ** beginCol + width will never exceed the width of the terminal
  963. ** buffer
  964. */
  965. Boolean
  966. _DtTermPrimSelectIsInSelection
  967. (
  968. Widget w,
  969. int row,
  970. short startCol,
  971. short width,
  972. short *selWidth
  973. )
  974. {
  975. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  976. DtTermPrimData tpd = tw->term.tpd;
  977. TermSelectInfo selectInfo = tpd->selectInfo;
  978. Boolean inSelection = True;
  979. XmTextPosition endPosition;
  980. XmTextPosition position;
  981. XmTextPosition begin;
  982. XmTextPosition end;
  983. short beginRow, beginCol;
  984. short endRow , endCol;
  985. position = rowColToPos(tw, row, startCol);
  986. endPosition = position + width;
  987. begin = selectInfo->begin;
  988. end = selectInfo->end;
  989. if ((begin >= endPosition) || (end <= position))
  990. {
  991. /*
  992. ** outside of selection range...
  993. */
  994. inSelection = False;
  995. }
  996. else
  997. {
  998. /*
  999. ** we're in the selection range, clip endPosition as necessary...
  1000. */
  1001. if (position < begin)
  1002. {
  1003. /*
  1004. ** we start to the left of the selection...
  1005. */
  1006. inSelection = False;
  1007. endPosition = MIN(endPosition, begin);
  1008. }
  1009. else
  1010. {
  1011. /*
  1012. ** we must be in the selection, clip endPosition as
  1013. ** necessary...
  1014. */
  1015. endPosition = MIN(endPosition, end);
  1016. }
  1017. }
  1018. *selWidth = endPosition - position;
  1019. return(inSelection);
  1020. }
  1021. /*ARGSUSED*/
  1022. void
  1023. _DtTermPrimSelectDoSelection
  1024. (
  1025. Widget w,
  1026. XEvent *event,
  1027. String *params,
  1028. Cardinal *paramCount
  1029. )
  1030. {
  1031. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  1032. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectDoSelection() starting\n"));
  1033. handleSelection(tw, event->xbutton.x, event->xbutton.y,
  1034. event->xbutton.time);
  1035. }
  1036. /*ARGSUSED*/
  1037. void
  1038. _DtTermPrimSelectSetHint
  1039. (
  1040. Widget w,
  1041. XEvent *event,
  1042. String *params,
  1043. Cardinal *paramCount
  1044. )
  1045. {
  1046. TermSelectInfo selectInfo =
  1047. ((DtTermPrimitiveWidget)w)->term.tpd->selectInfo;
  1048. selectInfo->hint.x = event->xbutton.x;
  1049. selectInfo->hint.y = event->xbutton.y;
  1050. }
  1051. /*ARGSUSED*/
  1052. void
  1053. _DtTermPrimSelectStart
  1054. (
  1055. Widget w,
  1056. XEvent *event,
  1057. String *params,
  1058. Cardinal *paramCount
  1059. )
  1060. {
  1061. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1062. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  1063. XButtonEvent *btnEvent = (XButtonEvent *) event;
  1064. XmTextPosition begin;
  1065. XmTextPosition end;
  1066. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectStart() starting\n"));
  1067. /*
  1068. ** set the selection hints, and scan type
  1069. */
  1070. _DtTermPrimSelectSetHint(w, event, params, paramCount);
  1071. setScanType(w, event);
  1072. /*
  1073. ** Set the current anchor point
  1074. */
  1075. selectInfo->anchor = xyToPos(tw, btnEvent->x, btnEvent->y);
  1076. if (selectInfo->scanType != XmSELECT_POSITION ||
  1077. (_DtTermPrimSelectGetSelection(w, &begin, &end) && begin != end)
  1078. )
  1079. {
  1080. _DtTermPrimSelectDoSelection(w, event, params, paramCount);
  1081. }
  1082. }
  1083. /*ARGSUSED*/
  1084. void
  1085. _DtTermPrimSelectGrabFocus
  1086. (
  1087. Widget w,
  1088. XEvent *event,
  1089. String *params,
  1090. Cardinal *paramCount
  1091. )
  1092. {
  1093. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1094. DtTermPrimData tpd = tw->term.tpd;
  1095. TermSelectInfo selectInfo = tpd->selectInfo;
  1096. XButtonEvent *btnEvent = (XButtonEvent *) event;
  1097. /* setDebugFlags("c") ; */
  1098. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectGrabFocus() starting\n"));
  1099. /* turn off the cursor */
  1100. _DtTermPrimCursorOff(w);
  1101. selectInfo->cancel = False;
  1102. tw->term.allowOsfKeysyms = True; /* normal dtterm doesn't honor these*/
  1103. /*
  1104. ** constrain the button event to the terminal's text area
  1105. */
  1106. if (btnEvent->x <= (int) tpd->offsetX)
  1107. {
  1108. /* left */
  1109. btnEvent->x = (int)(tpd->offsetX + 1);
  1110. } else if (btnEvent->x >= (int)(tw->core.width - tpd->offsetX))
  1111. {
  1112. /* right */
  1113. btnEvent->x = (int)(tw->core.width - tpd->offsetX - 1);
  1114. }
  1115. if (btnEvent->y <= (int)tpd->offsetY)
  1116. {
  1117. /* above */
  1118. btnEvent->y = (int)(tpd->offsetY + 1);
  1119. }
  1120. else if (btnEvent->y - ((int)(tpd->offsetY +
  1121. ((tpd->lastUsedRow - tpd->topRow) *
  1122. tpd->cellHeight))) >= selectInfo->threshold)
  1123. {
  1124. /* below */
  1125. btnEvent->y = (int)(tpd->offsetY + ((tpd->lastUsedRow - tpd->topRow) *
  1126. tpd->cellHeight) - 1);
  1127. }
  1128. if (_XmGetFocusPolicy(w) == XmEXPLICIT)
  1129. (void) XmProcessTraversal(w, XmTRAVERSE_CURRENT);
  1130. _DtTermPrimSelectStart(w, event, params, paramCount);
  1131. }
  1132. static
  1133. Boolean
  1134. dragged
  1135. (
  1136. TermSelectionHint hint,
  1137. XEvent *event,
  1138. int threshold
  1139. )
  1140. {
  1141. return ((abs(hint.x - event->xbutton.x) > threshold) ||
  1142. (abs(hint.y - event->xbutton.y) > threshold));
  1143. }
  1144. /* ARGSUSED */
  1145. static void
  1146. doExtendedSelection
  1147. (
  1148. Widget w,
  1149. Time eventTime
  1150. )
  1151. {
  1152. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  1153. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  1154. XmTextPosition position;
  1155. XmTextPosition begin;
  1156. XmTextPosition end;
  1157. XmTextPosition cursorPos;
  1158. float midPoint;
  1159. if (selectInfo->cancel) {
  1160. if (selectInfo->selectID) XtRemoveTimeOut(selectInfo->selectID);
  1161. selectInfo->selectID = 0;
  1162. return;
  1163. }
  1164. position = xyToPos(tw, selectInfo->extend.x, selectInfo->extend.y);
  1165. if (!(_DtTermPrimSelectGetSelection(w, &begin, &end)) ||
  1166. (begin == end))
  1167. {
  1168. begin = position;
  1169. end = position;
  1170. if ( selectInfo->anchor <0) selectInfo->anchor = position;
  1171. selectInfo->origBegin = selectInfo->anchor;
  1172. selectInfo->origEnd = selectInfo->anchor;
  1173. midPoint = (float)selectInfo->anchor;
  1174. }
  1175. else
  1176. {
  1177. midPoint = (float)
  1178. (((float)(selectInfo->origEnd -
  1179. selectInfo->origBegin) / 2.0) +
  1180. (float)selectInfo->origBegin);
  1181. }
  1182. /*
  1183. ** shift anchor and direction to opposite end of the selection
  1184. */
  1185. if ((float)(position) <= midPoint)
  1186. {
  1187. selectInfo->anchor = selectInfo->origEnd;
  1188. if (!selectInfo->extending)
  1189. {
  1190. selectInfo->extendDir = scanLeft;
  1191. }
  1192. }
  1193. else if ((float)(position) > midPoint)
  1194. {
  1195. selectInfo->anchor = selectInfo->origBegin;
  1196. if (!selectInfo->extending)
  1197. {
  1198. selectInfo->extendDir = scanRight;
  1199. }
  1200. }
  1201. selectInfo->extending = TRUE;
  1202. /*
  1203. ** check for change in extend direction
  1204. */
  1205. if ((selectInfo->extendDir == scanRight &&
  1206. position < selectInfo->anchor) ||
  1207. (selectInfo->extendDir == scanLeft &&
  1208. position > selectInfo->anchor))
  1209. {
  1210. selectInfo->extendDir = (selectInfo->extendDir == scanRight) ?
  1211. scanLeft : scanRight;
  1212. begin = selectInfo->begin;
  1213. end = selectInfo->end;
  1214. }
  1215. if (selectInfo->extendDir == scanRight)
  1216. {
  1217. cursorPos = scan(tw, position, selectInfo->scanType, scanRight, 1,
  1218. selectInfo->scanType == XmSELECT_LINE);
  1219. end = cursorPos;
  1220. begin = selectInfo->anchor;
  1221. }
  1222. else
  1223. {
  1224. cursorPos = scan(tw, position, selectInfo->scanType, scanLeft, 1,
  1225. FALSE);
  1226. begin = cursorPos;
  1227. end = selectInfo->anchor;
  1228. if (selectInfo->scanType == XmSELECT_WORD &&
  1229. (int)tw->term.tpd->cellWidth > 1)
  1230. {
  1231. if (position == scan (tw, begin, selectInfo->scanType, scanRight, 1,
  1232. FALSE))
  1233. {
  1234. begin = cursorPos = position;
  1235. }
  1236. }
  1237. }
  1238. setSelection(tw, begin, end, eventTime, False);
  1239. }
  1240. void
  1241. _DtTermPrimSelectExtendStart(
  1242. Widget w,
  1243. XEvent *event,
  1244. String *params,
  1245. Cardinal *num_params )
  1246. {
  1247. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1248. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  1249. selectInfo->cancel = False;
  1250. tw->term.allowOsfKeysyms = True ;
  1251. _DtTermPrimSelectExtend(w, event, params, num_params);
  1252. }
  1253. /*ARGSUSED*/
  1254. void
  1255. _DtTermPrimSelectExtend
  1256. (
  1257. Widget w,
  1258. XEvent *event,
  1259. String *params,
  1260. Cardinal *paramCount
  1261. )
  1262. {
  1263. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1264. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  1265. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectExtend() starting\n"));
  1266. /* turn off the cursor */
  1267. _DtTermPrimCursorOff(w);
  1268. if (_XmGetFocusPolicy(w) == XmEXPLICIT)
  1269. (void) XmProcessTraversal(w, XmTRAVERSE_CURRENT);
  1270. if (selectInfo->cancel) return ;
  1271. if ((selectInfo->hint.x > 0) || (selectInfo->hint.y > 0))
  1272. {
  1273. if (dragged(selectInfo->hint, event, selectInfo->threshold))
  1274. {
  1275. /*
  1276. ** extend the selection
  1277. */
  1278. handleSelection(tw,selectInfo->hint.x,selectInfo->hint.y,
  1279. event->xbutton.time);
  1280. selectInfo->hint.x = 0;
  1281. selectInfo->hint.y = 0;
  1282. selectInfo->extending = True;
  1283. }
  1284. else
  1285. {
  1286. /*
  1287. ** do nothing
  1288. */
  1289. return;
  1290. }
  1291. }
  1292. /*
  1293. ** check for timer scrolling here
  1294. ** NOTE: CheckTimerScrolling(w,event) will set extend.[x|y]
  1295. * selectInfo->extend.x = event->xbutton.x;
  1296. * selectInfo->extend.y = event->xbutton.y;
  1297. */
  1298. if (!CheckTimerScrolling(w,event) )
  1299. doExtendedSelection(w, event->xbutton.time);
  1300. }
  1301. /*ARGSUSED*/
  1302. void
  1303. _DtTermPrimSelectExtendEnd
  1304. (
  1305. Widget w,
  1306. XEvent *event,
  1307. String *params,
  1308. Cardinal *paramCount
  1309. )
  1310. {
  1311. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1312. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  1313. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectExtendEnd() starting\n"));
  1314. selectInfo->cancel = True; /* used by scroll selection */
  1315. tw->term.allowOsfKeysyms = False;
  1316. if (selectInfo->extending)
  1317. {
  1318. _DtTermPrimSelectGetSelection(w, &selectInfo->origBegin,
  1319. &selectInfo->origEnd);
  1320. setSelection(tw, selectInfo->origBegin, selectInfo->origEnd,
  1321. event->xbutton.time, False);
  1322. /* _DtTermPrimSelectExtend(w, event, params, paramCount);*/
  1323. }
  1324. if (selectInfo->selectID > 0)
  1325. {
  1326. XtRemoveTimeOut(selectInfo->selectID);
  1327. selectInfo->selectID = 0;
  1328. }
  1329. selectInfo->extend.x = 0;
  1330. selectInfo->extend.y = 0;
  1331. selectInfo->extending = False;
  1332. selectInfo->hint.x = 0;
  1333. selectInfo->hint.y = 0;
  1334. /* turn off the cursor */
  1335. _DtTermPrimCursorOn(w);
  1336. }
  1337. /*ARGSUSED*/
  1338. static void
  1339. doHandleTargets
  1340. (
  1341. Widget w,
  1342. XtPointer closure,
  1343. Atom *seltype,
  1344. Atom *type,
  1345. XtPointer value,
  1346. unsigned long *length,
  1347. int *format
  1348. )
  1349. {
  1350. _TermSelectPrimaryRec *primSelect = (_TermSelectPrimaryRec *) closure;
  1351. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1352. DtTermPrimData tpd = tw->term.tpd;
  1353. TermSelectInfo selectInfo = tpd->selectInfo;
  1354. XTextProperty tmpProp;
  1355. XmTextBlockRec block;
  1356. int i, status;
  1357. char *pChar;
  1358. char *pCharEnd;
  1359. char *pCharFollow;
  1360. int malloc_size=0 , numVals ;
  1361. char *total_tmp_value ;
  1362. char **tmp_value ;
  1363. Debug('c', fprintf(stderr, ">>doHandleTargets() starting\n"));
  1364. if (_XmGetFocusPolicy(w) == XmEXPLICIT)
  1365. {
  1366. (void) XmProcessTraversal(w, XmTRAVERSE_CURRENT);
  1367. }
  1368. if (*type == XmInternAtom(XtDisplay(w), "COMPOUND_TEXT", False) ||
  1369. *type == XA_STRING)
  1370. {
  1371. tmpProp.value = (unsigned char *) value;
  1372. tmpProp.encoding = *type;
  1373. tmpProp.format = *format;
  1374. tmpProp.nitems = *length;
  1375. numVals = 0;
  1376. status = XmbTextPropertyToTextList(XtDisplay(w), &tmpProp,
  1377. &tmp_value, &numVals );
  1378. /*
  1379. ** if no conversions, numVals doesn't change
  1380. */
  1381. if (numVals && (status == Success || status > 0))
  1382. {
  1383. for (i = 0; i < numVals ; i++)
  1384. {
  1385. malloc_size += strlen(tmp_value[i]);
  1386. }
  1387. total_tmp_value = XtMalloc ((unsigned) malloc_size + 1);
  1388. total_tmp_value[0] = '\0';
  1389. for (i = 0; i < numVals ; i++)
  1390. {
  1391. strcat(total_tmp_value, tmp_value[i]);
  1392. }
  1393. block.ptr = total_tmp_value;
  1394. block.length = strlen(total_tmp_value);
  1395. block.format = XmFMT_8_BIT;
  1396. XFreeStringList(tmp_value);
  1397. }
  1398. else
  1399. {
  1400. malloc_size = 1; /* to force space to be freed */
  1401. total_tmp_value = XtMalloc ((unsigned)1);
  1402. *total_tmp_value = '\0';
  1403. block.ptr = total_tmp_value;
  1404. block.length = 0;
  1405. block.format = XmFMT_8_BIT;
  1406. }
  1407. } else {
  1408. block.ptr = (char*)value;
  1409. block.length = (int) *length; /* NOTE: this causes a truncation on
  1410. some architectures */
  1411. block.format = XmFMT_8_BIT;
  1412. }
  1413. pCharEnd = block.ptr + block.length;
  1414. pCharFollow = (char *)block.ptr;
  1415. for (pChar = (char *)block.ptr; pChar < pCharEnd; pChar++)
  1416. {
  1417. if (*pChar == '\n')
  1418. {
  1419. *pChar = '\r';
  1420. DtTermSubprocSend(w, (unsigned char *) pCharFollow,
  1421. pChar - pCharFollow + 1);
  1422. pCharFollow = pChar + 1;
  1423. }
  1424. }
  1425. if (pCharFollow < pCharEnd)
  1426. {
  1427. DtTermSubprocSend(w, (unsigned char *) pCharFollow,
  1428. pCharEnd - pCharFollow);
  1429. }
  1430. if (malloc_size != 0) XtFree(total_tmp_value);
  1431. XtFree((char *)value);
  1432. if (primSelect && (--primSelect->ref_count == 0))
  1433. {
  1434. XtFree((char *)primSelect);
  1435. }
  1436. value = NULL ;
  1437. }
  1438. /*
  1439. ** Look at the target list and determine what target to place in the
  1440. ** pair. it will then do any necessary conversions before "thrusting"
  1441. ** the selection value onto the receiver. this will guarantee the
  1442. ** best chance at a successful exchange.
  1443. */
  1444. /*ARGSUSED*/
  1445. static void
  1446. handleTargets
  1447. (
  1448. Widget w,
  1449. XtPointer closure,
  1450. Atom *selType,
  1451. Atom *type,
  1452. XtPointer value,
  1453. unsigned long *length,
  1454. int *format
  1455. )
  1456. {
  1457. Atom CS_OF_LOCALE;
  1458. Atom COMPOUND_TEXT;
  1459. Boolean supportsLocaleData;
  1460. Boolean supportsCompoundText;
  1461. Atom *atomPtr;
  1462. _TermSelectRec *tmpAction;
  1463. _TermSelectPrimaryRec *primSelect;
  1464. char *abcString;
  1465. XTextProperty tmpProp;
  1466. int status;
  1467. XtPointer closures[2];
  1468. Atom targets[2];
  1469. int i;
  1470. /*
  1471. ** make sure we have something to do...
  1472. */
  1473. tmpAction = (_TermSelectRec *) closure;
  1474. if (!length || *length == 0) {
  1475. XtFree((char *)value);
  1476. value = NULL;
  1477. XtFree((char *)tmpAction->event);
  1478. XtFree((char *)tmpAction);
  1479. return;
  1480. }
  1481. COMPOUND_TEXT = XmInternAtom(XtDisplay(w),"COMPOUND_TEXT", False);
  1482. supportsLocaleData = False;
  1483. supportsCompoundText = False;
  1484. abcString = "ABC"; /* characters in XPCS, so... safe */
  1485. atomPtr = (Atom *)value;
  1486. tmpProp.value = NULL;
  1487. status = XmbTextListToTextProperty(XtDisplay(w), &abcString, 1,
  1488. (XICCEncodingStyle)XTextStyle, &tmpProp);
  1489. if (status == Success)
  1490. {
  1491. CS_OF_LOCALE = tmpProp.encoding;
  1492. }
  1493. else
  1494. {
  1495. /*
  1496. ** Kludge for failure of XmbText... to
  1497. ** handle XPCS characters. Should never
  1498. ** happen, but this prevents a core dump
  1499. ** if X11 is broken.
  1500. */
  1501. CS_OF_LOCALE = (Atom)9999;
  1502. }
  1503. if (tmpProp.value != NULL)
  1504. {
  1505. XFree((char *)tmpProp.value);
  1506. }
  1507. for (i = 0; i < *length; i++, atomPtr++)
  1508. {
  1509. if (*atomPtr == CS_OF_LOCALE)
  1510. {
  1511. supportsLocaleData = True;
  1512. break;
  1513. }
  1514. if (*atomPtr == COMPOUND_TEXT)
  1515. {
  1516. supportsCompoundText = True;
  1517. }
  1518. }
  1519. primSelect = (_TermSelectPrimaryRec *)
  1520. XtMalloc((unsigned) sizeof(_TermSelectPrimaryRec));
  1521. /*
  1522. ** If owner and I are using the same codeset, ask for it. If not,
  1523. ** and if the owner supports compound text, ask for compound text.
  1524. ** If not, fall back position is to ask for STRING and try to
  1525. ** convert it locally.
  1526. */
  1527. if (supportsLocaleData)
  1528. {
  1529. primSelect->target = targets[0] = CS_OF_LOCALE;
  1530. }
  1531. else if (supportsCompoundText)
  1532. {
  1533. primSelect->target = targets[0] = COMPOUND_TEXT;
  1534. }
  1535. else
  1536. {
  1537. primSelect->target = targets[0] = XA_STRING;
  1538. }
  1539. closures[0] = (char *)primSelect;
  1540. primSelect->ref_count = 1;
  1541. /*
  1542. ** Make request to call doHandleTargets() with the primary selection.
  1543. */
  1544. XtGetSelectionValue(w, XA_PRIMARY, targets[0], doHandleTargets,
  1545. (XtPointer)primSelect,
  1546. tmpAction->event->xbutton.time);
  1547. XtFree((char *)value);
  1548. value = NULL;
  1549. XtFree((char *)tmpAction->event);
  1550. XtFree((char *)tmpAction);
  1551. }
  1552. static char *
  1553. getString
  1554. (
  1555. Widget w,
  1556. XmTextPosition begin,
  1557. XmTextPosition end,
  1558. Boolean needWideChar
  1559. )
  1560. {
  1561. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  1562. DtTermPrimData tpd = tw->term.tpd;
  1563. TermSelectInfo selectInfo = tpd->selectInfo;
  1564. TermBuffer tb;
  1565. short beginRow,
  1566. beginCol;
  1567. short endRow,
  1568. endCol;
  1569. short thisRow;
  1570. short numRows;
  1571. char *buffer;
  1572. char *pBuf;
  1573. short len;
  1574. beginRow = begin / (selectInfo->columns + 1);
  1575. beginCol = begin - (beginRow * (selectInfo->columns + 1));
  1576. endRow = end / (selectInfo->columns + 1);
  1577. endCol = end - (endRow * (selectInfo->columns + 1));
  1578. numRows = endRow - beginRow + 1;
  1579. /*
  1580. ** we need to store end - begin characters, a terminating byte, plus
  1581. ** a new line for each line...
  1582. **
  1583. ** NOTE: end - begin could result in a truncated long.
  1584. */
  1585. buffer = XtMalloc(((int)(end - begin) + 1 + numRows) * sizeof(char)
  1586. * BYTES_PER_CHAR(tpd->termBuffer));
  1587. /*
  1588. ** return a null string if there is nothing to do
  1589. */
  1590. if (begin == end)
  1591. {
  1592. *buffer = 0x00;
  1593. return(buffer);
  1594. }
  1595. /*
  1596. ** Accomodate the history buffer as necessary
  1597. */
  1598. if (tpd->useHistoryBuffer)
  1599. {
  1600. beginRow -= tpd->lastUsedHistoryRow;
  1601. endRow -= tpd->lastUsedHistoryRow;
  1602. }
  1603. /*
  1604. ** get the first (and possibly only) line of text
  1605. */
  1606. pBuf = buffer;
  1607. if (beginRow == endRow)
  1608. {
  1609. if (beginRow < 0)
  1610. {
  1611. tb = tpd->historyBuffer;
  1612. thisRow = beginRow + tpd->lastUsedHistoryRow;
  1613. }
  1614. else
  1615. {
  1616. tb = tpd->termBuffer;
  1617. thisRow = beginRow;
  1618. }
  1619. len = _DtTermPrimBufferGetText(tb, thisRow, beginCol,
  1620. endCol - beginCol, pBuf, needWideChar);
  1621. pBuf += len;
  1622. }
  1623. else
  1624. {
  1625. if (beginRow < 0)
  1626. {
  1627. tb = tpd->historyBuffer;
  1628. thisRow = beginRow + tpd->lastUsedHistoryRow;
  1629. }
  1630. else
  1631. {
  1632. tb = tpd->termBuffer;
  1633. thisRow = beginRow;
  1634. }
  1635. len = _DtTermPrimBufferGetText(tb, thisRow, beginCol,
  1636. selectInfo->columns - beginCol, pBuf,
  1637. needWideChar);
  1638. pBuf += len;
  1639. if ( !_DtTermPrimBufferTestLineWrapFlag(tb,thisRow) ) {
  1640. *pBuf = '\n'; /* newline */
  1641. pBuf++;
  1642. }
  1643. /*
  1644. ** get the middle block (if there is one)
  1645. */
  1646. beginRow++;
  1647. while(beginRow < endRow)
  1648. {
  1649. if (beginRow < 0)
  1650. {
  1651. tb = tpd->historyBuffer;
  1652. thisRow = beginRow + tpd->lastUsedHistoryRow;
  1653. }
  1654. else
  1655. {
  1656. tb = tpd->termBuffer;
  1657. thisRow = beginRow;
  1658. }
  1659. len = _DtTermPrimBufferGetText(tb, thisRow, 0,
  1660. selectInfo->columns , pBuf,
  1661. needWideChar);
  1662. pBuf += len;
  1663. /* if (len != 0 && len < selectInfo->columns ) { */
  1664. if ( !_DtTermPrimBufferTestLineWrapFlag(tb,thisRow) ) {
  1665. *pBuf = '\n'; /* newline */
  1666. pBuf++;
  1667. }
  1668. beginRow++;
  1669. }
  1670. /*
  1671. ** get the last line
  1672. */
  1673. if (endRow < 0)
  1674. {
  1675. tb = tpd->historyBuffer;
  1676. thisRow = endRow + tpd->lastUsedHistoryRow;
  1677. }
  1678. else
  1679. {
  1680. tb = tpd->termBuffer;
  1681. thisRow = endRow;
  1682. }
  1683. len = _DtTermPrimBufferGetText(tb, thisRow, 0, endCol, pBuf,
  1684. needWideChar);
  1685. pBuf += len;
  1686. }
  1687. *pBuf = 0x00;
  1688. return(buffer);
  1689. }
  1690. /*
  1691. ** Request targets from selection owner.
  1692. */
  1693. static void
  1694. getTargets
  1695. (
  1696. Widget w,
  1697. XEvent *event,
  1698. String *params,
  1699. Cardinal *paramCount
  1700. )
  1701. {
  1702. _TermSelectRec *tmp;
  1703. tmp = (_TermSelectRec*)XtMalloc(sizeof(_TermSelectRec));
  1704. /*
  1705. ** Request targets from the selection owner so you can decide what to
  1706. ** request. The decision process and request for the selection is
  1707. ** taken care of in handleTargets().
  1708. */
  1709. tmp->event = (XEvent *) XtMalloc(sizeof(XEvent));
  1710. memcpy((void *)tmp->event, (void *)event, sizeof(XEvent));
  1711. tmp->params = params;
  1712. tmp->num_params = paramCount;
  1713. XtGetSelectionValue(w, XA_PRIMARY,
  1714. XmInternAtom(XtDisplay(w), "TARGETS", False),
  1715. handleTargets, (XtPointer)tmp, event->xbutton.time);
  1716. }
  1717. Boolean
  1718. _DtTermPrimSelectConvert
  1719. (
  1720. Widget w,
  1721. Atom *selection,
  1722. Atom *target,
  1723. Atom *type,
  1724. XtPointer *value,
  1725. unsigned long *length,
  1726. int *format
  1727. )
  1728. {
  1729. Atom TARGETS = XmInternAtom(XtDisplay(w), "TARGETS", False);
  1730. Atom CS_OF_LOCALE;
  1731. Atom COMPOUND_TEXT = XmInternAtom(XtDisplay(w), "COMPOUND_TEXT", False);
  1732. Atom TEXT = XmInternAtom(XtDisplay(w), "TEXT", False);
  1733. Atom MOTIF_DROP = XmInternAtom(XtDisplay(w), "_MOTIF_DROP", False);
  1734. int maxTargets = 10;
  1735. int targetCount;
  1736. int status;
  1737. Widget widget;
  1738. Boolean ownPrimary;
  1739. XmTextPosition begin;
  1740. XmTextPosition end;
  1741. char *tmpValue;
  1742. char *tmpString = "ABC"; /* characters in XPCS, so... safe */
  1743. XTextProperty tmpProp;
  1744. XtPointer c_ptr;
  1745. Arg args[1];
  1746. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectConvert() starting\n"));
  1747. if (*selection == MOTIF_DROP) {
  1748. XtSetArg(args[0], XmNclientData, &c_ptr);
  1749. XtGetValues(w, args, 1);
  1750. widget = (Widget)c_ptr;
  1751. } else
  1752. widget = w;
  1753. if (widget == NULL) return False;
  1754. tmpProp.value = NULL;
  1755. status = XmbTextListToTextProperty(XtDisplay(widget), &tmpString, 1,
  1756. (XICCEncodingStyle)XTextStyle, &tmpProp);
  1757. if (status == Success)
  1758. {
  1759. CS_OF_LOCALE = tmpProp.encoding;
  1760. }
  1761. else
  1762. {
  1763. /*
  1764. ** XmbTextList... SHOULD never fail for
  1765. ** XPCS character. But if it does, this
  1766. ** prevents a core dump.
  1767. */
  1768. CS_OF_LOCALE = (Atom) 9999;
  1769. }
  1770. if (tmpProp.value != NULL)
  1771. XFree((char *) tmpProp.value);
  1772. if (*selection == XA_PRIMARY || *selection == MOTIF_DROP)
  1773. {
  1774. ownPrimary = _DtTermPrimSelectGetSelection(widget, &begin, &end);
  1775. }
  1776. else
  1777. {
  1778. return(False);
  1779. }
  1780. if (*target == TARGETS)
  1781. {
  1782. Atom *targets = (Atom *)XtMalloc((unsigned)(maxTargets * sizeof(Atom)));
  1783. /*
  1784. ** Xt should take care of TIME_STAMP for us.
  1785. */
  1786. targetCount = 0;
  1787. *value = (XtPointer)targets;
  1788. *targets++ = TARGETS; targetCount++;
  1789. if (!isDebugFSet('s', 1)) {
  1790. *targets++ = COMPOUND_TEXT; targetCount++;
  1791. }
  1792. if (!isDebugFSet('s', 2)) {
  1793. *targets++ = CS_OF_LOCALE; targetCount++;
  1794. }
  1795. if (!isDebugFSet('s', 3)) {
  1796. *targets++ = TEXT; targetCount++;
  1797. }
  1798. if (!isDebugFSet('s', 4)) {
  1799. *targets++ = XA_STRING; targetCount++;
  1800. }
  1801. *type = XA_ATOM;
  1802. *length = (targetCount * sizeof(Atom)) >> 2; /* convert to work count */
  1803. *format = 32;
  1804. }
  1805. else if (!ownPrimary)
  1806. {
  1807. return(False);
  1808. }
  1809. else if ((*target == XA_STRING && !isDebugFSet('s', 4)) ||
  1810. (*target == COMPOUND_TEXT && !isDebugFSet('s', 1)))
  1811. {
  1812. tmpValue = getString(widget, begin, end, False);
  1813. tmpProp.value = NULL;
  1814. if ((*target == XA_STRING) && !isDebugFSet('s', 4)) {
  1815. *type = (Atom) XA_STRING;
  1816. *format = 8;
  1817. status = XmbTextListToTextProperty(XtDisplay(widget), &tmpValue, 1,
  1818. (XICCEncodingStyle)XStringStyle,
  1819. &tmpProp);
  1820. }
  1821. else if ((*target == COMPOUND_TEXT) && !isDebugFSet('s',1)) {
  1822. *type = COMPOUND_TEXT;
  1823. *format = 8;
  1824. status = XmbTextListToTextProperty(XtDisplay(widget), &tmpValue, 1,
  1825. (XICCEncodingStyle)XCompoundTextStyle,
  1826. &tmpProp);
  1827. }
  1828. XtFree(tmpValue);
  1829. if (status == Success || status > 0)
  1830. {
  1831. /*
  1832. ** NOTE: casting tmpProp.nitems could result in a truncated long.
  1833. */
  1834. if (tmpProp.nitems > 0)
  1835. *value = (XtPointer) XtMalloc((unsigned)tmpProp.nitems);
  1836. else
  1837. *value = (XtPointer) XtMalloc(1);
  1838. *length = tmpProp.nitems;
  1839. memcpy((void*)*value, (void*)tmpProp.value,
  1840. (unsigned)tmpProp.nitems);
  1841. if (tmpProp.value != NULL)
  1842. XFree((char*)tmpProp.value);
  1843. }
  1844. else
  1845. {
  1846. *value = NULL;
  1847. *length = 0;
  1848. if (tmpProp.value != NULL)
  1849. XFree((char*)tmpProp.value);
  1850. return(False);
  1851. }
  1852. }
  1853. else if (((*target == CS_OF_LOCALE) && !isDebugFSet('s', 2)) ||
  1854. (*target == TEXT && !isDebugFSet('s', 3)))
  1855. {
  1856. *type = CS_OF_LOCALE;
  1857. *format = 8;
  1858. *value = (XtPointer)getString(widget, begin, end, False);
  1859. *length = strlen((char*) *value);
  1860. }
  1861. else
  1862. {
  1863. *value = NULL;
  1864. *length = 0;
  1865. return(False);
  1866. }
  1867. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectConvert() exiting\n"));
  1868. return(True);
  1869. }
  1870. void
  1871. _DtTermPrimSelectLoseSelection
  1872. (
  1873. Widget w,
  1874. Atom *selection
  1875. )
  1876. {
  1877. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1878. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  1879. Boolean restoreCursor = False;
  1880. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectLoseSelection() starting\n"));
  1881. if (*selection == XA_PRIMARY && selectInfo->ownPrimary)
  1882. {
  1883. /*
  1884. ** We've lost the primary selection, make a dummy call to
  1885. ** setSelection (with begin > end) to clear things up...
  1886. */
  1887. /* turn off the cursor */
  1888. if (tw->term.tpd->cursorState != CURSORoff) {
  1889. _DtTermPrimCursorOff(w);
  1890. restoreCursor = True;
  1891. }
  1892. setSelection(tw, 1, -99, XtLastTimestampProcessed(XtDisplay(w)), True);
  1893. /* turn on the cursor */
  1894. if (restoreCursor) {
  1895. _DtTermPrimCursorOn(w);
  1896. }
  1897. }
  1898. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectLoseSelection() exiting\n"));
  1899. }
  1900. /*ARGSUSED*/
  1901. void
  1902. _DtTermPrimSelectBDragRelease
  1903. (
  1904. Widget w,
  1905. XEvent *event,
  1906. String *params,
  1907. Cardinal *paramCount
  1908. )
  1909. {
  1910. TermSelectInfo selectInfo =
  1911. ((DtTermPrimitiveWidget)w)->term.tpd->selectInfo;
  1912. XButtonEvent *btnEvent = (XButtonEvent *) event;
  1913. Debug('c', fprintf(stderr, ">>_DtTermPrimSelectBDragRelease() starting\n"));
  1914. /* Work around for intrinsic bug. Remove once bug is fixed.
  1915. ** this is for drag/drop
  1916. */
  1917. XtUngrabPointer(w, btnEvent->time);
  1918. if ( selectInfo->sel_start ) getTargets(w, event, params, paramCount);
  1919. }
  1920. /*ARGSUSED*/
  1921. void
  1922. _DtTermPrimSelectInsert
  1923. (
  1924. Widget w,
  1925. XEvent *event,
  1926. String *params,
  1927. Cardinal *paramCount
  1928. )
  1929. {
  1930. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1931. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  1932. if (!selectInfo->cancel)
  1933. _DtTermPrimSelectBDragRelease(w, event, params, paramCount);
  1934. selectInfo->cancel = True ;
  1935. /* turn on the cursor */
  1936. _DtTermPrimCursorOn(w);
  1937. }
  1938. Boolean
  1939. _DtTermPrimSelectIsAboveSelection
  1940. (
  1941. Widget w,
  1942. short row,
  1943. short col
  1944. )
  1945. {
  1946. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1947. DtTermPrimData tpd = tw->term.tpd;
  1948. TermSelectInfo selectInfo = tpd->selectInfo;
  1949. XmTextPosition curPos, endPos;
  1950. endPos = selectInfo->end ;
  1951. curPos = rowColToPos(tw,row,col) ;
  1952. if ( curPos < endPos )
  1953. return(True) ;
  1954. else
  1955. return(False) ;
  1956. }
  1957. void
  1958. _DtTermPrimSelectResize
  1959. (
  1960. Widget w
  1961. )
  1962. {
  1963. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  1964. DtTermPrimData tpd = tw->term.tpd;
  1965. TermSelectInfo selectInfo = tpd->selectInfo;
  1966. _DtTermPrimSelectDisown(w) ;
  1967. selectInfo->columns = tw->term.columns ;
  1968. }
  1969. extern void
  1970. _DtTermPrimSelectMoveLines
  1971. (
  1972. Widget w,
  1973. short src,
  1974. short dest,
  1975. short len
  1976. )
  1977. {
  1978. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  1979. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  1980. short selectLineBegin;
  1981. short selectLineEnd;
  1982. TermBuffer pb;
  1983. short row,col;
  1984. posToBufferRowCol (tw, selectInfo->begin, &pb, &row, &col);
  1985. /* if there are no lines, etc. return... */
  1986. if ((len <= 0) || (src == dest) || !selectInfo->ownPrimary ||
  1987. pb == tw->term.tpd->historyBuffer ) {
  1988. return;
  1989. }
  1990. if (row >= src && row < (src + len)) {
  1991. selectInfo->begin -= (src - dest) * (selectInfo->columns + 1);
  1992. selectInfo->end -= (src - dest) * (selectInfo->columns + 1);
  1993. }
  1994. }
  1995. extern void
  1996. _DtTermPrimSelectDeleteLines
  1997. (
  1998. Widget w,
  1999. short src,
  2000. short len
  2001. )
  2002. {
  2003. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  2004. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  2005. short selectLineBegin;
  2006. short selectLineEnd;
  2007. TermBuffer pb;
  2008. short row,col;
  2009. posToBufferRowCol (tw, selectInfo->begin, &pb, &row, &col);
  2010. /* if there are no lines, etc. return... */
  2011. if ((len <= 0) || !selectInfo->ownPrimary ||
  2012. ((tw->term.tpd->scrollLockTopRow > 0 ||
  2013. (tw->term.tpd->scrollLockBottomRow < tw->term.rows-1)) &&
  2014. row < tw->term.tpd->scrollLockTopRow)) {
  2015. return;
  2016. }
  2017. /* figure out what the begin and end lines are... */
  2018. selectLineBegin = selectInfo->begin / (selectInfo->columns + 1);
  2019. selectLineEnd = (selectInfo->end - 1) / (selectInfo->columns + 1);
  2020. /* if the beginning of the selection is after the source, we need to
  2021. * move the selection up...
  2022. */
  2023. if (selectLineBegin > src) {
  2024. selectInfo->begin -= len * (selectInfo->columns + 1);
  2025. selectInfo->end -= len * (selectInfo->columns + 1);
  2026. if (selectInfo->begin < 0) {
  2027. (void) _DtTermPrimSelectDisown(w);
  2028. }
  2029. }
  2030. }
  2031. extern void
  2032. _DtTermPrimSelectInsertLines
  2033. (
  2034. Widget w,
  2035. short src,
  2036. short len
  2037. )
  2038. {
  2039. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  2040. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  2041. short selectLineBegin;
  2042. short selectLineEnd;
  2043. /* if there are no lines, return... */
  2044. if ((len <= 0) || !selectInfo->ownPrimary) {
  2045. return;
  2046. }
  2047. /* figure out what the begin and end lines are... */
  2048. selectLineBegin = selectInfo->begin / (selectInfo->columns + 1);
  2049. selectLineEnd = (selectInfo->end - 1) / (selectInfo->columns + 1);
  2050. /* if the beginning of the selection is at or after the source, we need to
  2051. * move the selection up...
  2052. */
  2053. if (selectLineBegin >= src) {
  2054. selectInfo->begin += len * (selectInfo->columns + 1);
  2055. selectInfo->end += len * (selectInfo->columns + 1);
  2056. }
  2057. }
  2058. void
  2059. _DtTermPrimSelectAll
  2060. (
  2061. Widget w,
  2062. XEvent *event,
  2063. String *params,
  2064. Cardinal *paramCount
  2065. )
  2066. {
  2067. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  2068. DtTermPrimData tpd = tw->term.tpd;
  2069. TermSelectInfo selectInfo = tpd->selectInfo;
  2070. XButtonEvent *btnEvent = (XButtonEvent *) event;
  2071. XmTextPosition begin;
  2072. XmTextPosition end;
  2073. /* position not used in XmSELECT_ALL case */
  2074. begin = scan(tw, (XmTextPosition) 0, XmSELECT_ALL, scanLeft,
  2075. 1, False);
  2076. end = scan(tw, (XmTextPosition) 0, XmSELECT_ALL, scanRight,
  2077. 1, selectInfo->scanType == XmSELECT_LINE);
  2078. /* turn off the cursor */
  2079. _DtTermPrimCursorOff(w);
  2080. setSelection(tw, begin, end, event->xbutton.time, False);
  2081. /* turn on the cursor */
  2082. _DtTermPrimCursorOn(w);
  2083. }
  2084. void
  2085. _DtTermPrimSelectPage
  2086. (
  2087. Widget w,
  2088. XEvent *event,
  2089. String *params,
  2090. Cardinal *paramCount
  2091. )
  2092. {
  2093. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  2094. DtTermPrimData tpd = tw->term.tpd;
  2095. TermSelectInfo selectInfo = tpd->selectInfo;
  2096. XButtonEvent *btnEvent = (XButtonEvent *) event;
  2097. XmTextPosition begin, end;
  2098. short lastRow, width;
  2099. TermBuffer pb;
  2100. begin = xyToPos(tw, 1, 1);
  2101. end = xyToPos(tw, tw->core.width-1, tw->core.height-1);
  2102. /* turn off the cursor */
  2103. _DtTermPrimCursorOff(w);
  2104. setSelection(tw, begin, end, event->xbutton.time, False);
  2105. /* turn on the cursor */
  2106. _DtTermPrimCursorOn(w);
  2107. }
  2108. /*
  2109. * DROP SITE code
  2110. */
  2111. static XContext _DtTermDNDContext = 0;
  2112. static void
  2113. DropTransferCallback(
  2114. Widget w,
  2115. XtPointer closure,
  2116. Atom *seltype,
  2117. Atom *type,
  2118. XtPointer value,
  2119. unsigned long *length,
  2120. int *format )
  2121. {
  2122. _DtTermDropTransferRec *transfer_rec = (_DtTermDropTransferRec *) closure;
  2123. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)transfer_rec->widget;
  2124. DtTermPrimData tpd = tw->term.tpd;
  2125. TermSelectInfo selectInfo = tpd->selectInfo;
  2126. /* When type = NULL, we are assuming a DELETE request has been requested */
  2127. if (*type == 0) {
  2128. if (value) {
  2129. XtFree((char *)value);
  2130. value = NULL;
  2131. }
  2132. return;
  2133. }
  2134. doHandleTargets((Widget)tw,NULL,seltype,type,value,length,format) ;
  2135. if (transfer_rec->move) {
  2136. XmDropTransferEntryRec transferEntries[1];
  2137. XmDropTransferEntryRec *transferList = NULL;
  2138. transferEntries[0].client_data = (XtPointer) transfer_rec;
  2139. transferEntries[0].target = XmInternAtom(XtDisplay(w),"DELETE",
  2140. False);
  2141. transferList = transferEntries;
  2142. XmDropTransferAdd(w, transferEntries, 1);
  2143. }
  2144. }
  2145. static void
  2146. DeleteDropContext(
  2147. Widget w )
  2148. {
  2149. Display *display = XtDisplay(w);
  2150. Screen *screen = XtScreen(w);
  2151. XDeleteContext(display, (Window)screen, _DtTermDNDContext);
  2152. }
  2153. static void
  2154. SetDropContext(
  2155. Widget w )
  2156. {
  2157. Display *display = XtDisplay(w);
  2158. Screen *screen = XtScreen(w);
  2159. _DtTermProcessLock();
  2160. if (_DtTermDNDContext == 0)
  2161. _DtTermDNDContext = XUniqueContext();
  2162. _DtTermProcessUnlock();
  2163. XSaveContext(display, (Window)screen,
  2164. _DtTermDNDContext, (XPointer)w);
  2165. }
  2166. typedef struct _dropDestroyCBClientData {
  2167. _DtTermDropTransferRec *transfer_rec;
  2168. XtCallbackRec *dropDestroyCB; }
  2169. dropDestroyCBClientData;
  2170. /* ARGSUSED */
  2171. static void
  2172. DropDestroyCB(
  2173. Widget w,
  2174. XtPointer clientData,
  2175. XtPointer callData )
  2176. {
  2177. dropDestroyCBClientData *ptr = (dropDestroyCBClientData *) clientData;
  2178. DeleteDropContext(w);
  2179. if (ptr) {
  2180. if (ptr->transfer_rec) XtFree((char *) ptr->transfer_rec);
  2181. if (ptr->dropDestroyCB) XtFree((char *) ptr->dropDestroyCB);
  2182. XtFree((char *) ptr);
  2183. }
  2184. }
  2185. static void
  2186. HandleDrop( Widget w, XmDropProcCallbackStruct *cb )
  2187. {
  2188. Widget drag_cont, initiator;
  2189. /* XmTextWidget tw = (XmTextWidget) w; */
  2190. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  2191. DtTermPrimData tpd = tw->term.tpd;
  2192. TermSelectInfo selectInfo = tpd->selectInfo;
  2193. Cardinal numExportTargets, n;
  2194. Atom *exportTargets;
  2195. Arg args[10];
  2196. XmTextPosition insert_pos, left, right;
  2197. XtCallbackRec *dropDestroyCB, *dd_cb;
  2198. dropDestroyCBClientData *clientData;
  2199. clientData = (dropDestroyCBClientData *) XtMalloc(sizeof(dropDestroyCBClientData));
  2200. dropDestroyCB = dd_cb = (XtCallbackRec *) XtMalloc(2 * sizeof (XtCallbackRec));
  2201. clientData->dropDestroyCB = dropDestroyCB;
  2202. dd_cb->callback = DropDestroyCB;
  2203. dd_cb->closure = NULL;
  2204. dd_cb++;
  2205. dd_cb->callback = (XtCallbackProc) NULL;
  2206. dd_cb->closure = NULL;
  2207. drag_cont = cb->dragContext;
  2208. n = 0;
  2209. XtSetArg(args[n], XmNsourceWidget, &initiator); n++;
  2210. XtSetArg(args[n], XmNexportTargets, &exportTargets); n++;
  2211. XtSetArg(args[n], XmNnumExportTargets, &numExportTargets); n++;
  2212. XtGetValues((Widget) drag_cont, args, n);
  2213. {
  2214. XmDropTransferEntryRec transferEntries[2];
  2215. XmDropTransferEntryRec *transferList = NULL;
  2216. Atom TEXT = XmInternAtom(XtDisplay(w), "TEXT", False);
  2217. Atom COMPOUND_TEXT = XmInternAtom(XtDisplay(w), "COMPOUND_TEXT", False);
  2218. Atom CS_OF_LOCALE;
  2219. char * tmp_string = "ABC"; /* these are characters in XPCS, so... safe */
  2220. XTextProperty tmp_prop;
  2221. _DtTermDropTransferRec *transfer_rec;
  2222. Cardinal numTransfers = 0;
  2223. Boolean locale_found = False;
  2224. Boolean c_text_found = False;
  2225. Boolean string_found = False;
  2226. Boolean text_found = False;
  2227. int status;
  2228. tmp_prop.value = NULL;
  2229. status = XmbTextListToTextProperty(XtDisplay(w), &tmp_string, 1,
  2230. (XICCEncodingStyle)XTextStyle, &tmp_prop);
  2231. if (status == Success)
  2232. CS_OF_LOCALE = tmp_prop.encoding;
  2233. else
  2234. CS_OF_LOCALE = 99999; /* XmbTextList... should never fail for XPCS
  2235. * characters. But just in case someones
  2236. * Xlib is broken, this prevents a core dump.
  2237. */
  2238. if (tmp_prop.value != NULL) XFree((char *)tmp_prop.value);
  2239. /* intialize data to send to drop transfer callback */
  2240. transfer_rec = (_DtTermDropTransferRec *)
  2241. XtMalloc(sizeof(_DtTermDropTransferRec));
  2242. clientData->transfer_rec = transfer_rec;
  2243. transfer_rec->widget = w;
  2244. /* don't actually need all of this for dtterm - it was from Text widget
  2245. *transfer_rec->insert_pos = insert_pos;
  2246. *transfer_rec->num_chars = 0;
  2247. *transfer_rec->timestamp = cb->timeStamp;
  2248. *
  2249. */
  2250. if (cb->operation & XmDROP_MOVE) {
  2251. transfer_rec->move = True;
  2252. } else {
  2253. transfer_rec->move = False;
  2254. }
  2255. transferEntries[0].client_data = (XtPointer) transfer_rec;
  2256. transferList = transferEntries;
  2257. numTransfers = 1;
  2258. for (n = 0; n < numExportTargets; n++) {
  2259. if (exportTargets[n] == CS_OF_LOCALE) {
  2260. transferEntries[0].target = CS_OF_LOCALE;
  2261. locale_found = True;
  2262. break;
  2263. }
  2264. if (exportTargets[n] == COMPOUND_TEXT) c_text_found = True;
  2265. if (exportTargets[n] == XA_STRING) string_found = True;
  2266. if (exportTargets[n] == TEXT) text_found = True;
  2267. }
  2268. n = 0;
  2269. if (locale_found || c_text_found || string_found || text_found) {
  2270. if (!locale_found) {
  2271. if (c_text_found)
  2272. transferEntries[0].target = COMPOUND_TEXT;
  2273. else if (string_found)
  2274. transferEntries[0].target = XA_STRING;
  2275. else
  2276. transferEntries[0].target = TEXT;
  2277. }
  2278. if ( cb->operation & (XmDROP_COPY|XmDROP_MOVE) ) {
  2279. XtSetArg(args[n], XmNdropTransfers, transferList); n++;
  2280. XtSetArg(args[n], XmNnumDropTransfers, numTransfers); n++;
  2281. } else {
  2282. XtSetArg(args[n], XmNtransferStatus, XmTRANSFER_FAILURE); n++;
  2283. XtSetArg(args[n], XmNnumDropTransfers, 0); n++;
  2284. }
  2285. } else {
  2286. XtSetArg(args[n], XmNtransferStatus, XmTRANSFER_FAILURE); n++;
  2287. XtSetArg(args[n], XmNnumDropTransfers, 0); n++;
  2288. }
  2289. dropDestroyCB->closure = (XtPointer) clientData;
  2290. XtSetArg(args[n], XmNdestroyCallback, dropDestroyCB); n++;
  2291. XtSetArg(args[n], XmNtransferProc, DropTransferCallback); n++;
  2292. }
  2293. SetDropContext(w);
  2294. XmDropTransferStart(drag_cont, args, n);
  2295. }
  2296. /* ARGSUSED */
  2297. static void
  2298. DropProcCallback(
  2299. Widget w,
  2300. XtPointer client,
  2301. XtPointer call )
  2302. {
  2303. XmDropProcCallbackStruct *cb = (XmDropProcCallbackStruct *) call;
  2304. if (cb->dropAction != XmDROP_HELP) {
  2305. HandleDrop(w, cb);
  2306. } else {
  2307. Arg args[2];
  2308. XtSetArg(args[0], XmNtransferStatus, XmTRANSFER_FAILURE);
  2309. XtSetArg(args[1], XmNnumDropTransfers, 0);
  2310. XmDropTransferStart(cb->dragContext, args, 2);
  2311. }
  2312. }
  2313. static void
  2314. RegisterDropSite(
  2315. Widget w )
  2316. {
  2317. Atom targets[4];
  2318. Arg args[10];
  2319. int n;
  2320. char * tmp_string = "ABC"; /* these are characters in XPCS, so... safe */
  2321. XTextProperty tmp_prop;
  2322. int status = 0;
  2323. tmp_prop.value = NULL;
  2324. status = XmbTextListToTextProperty(XtDisplay(w), &tmp_string, 1,
  2325. (XICCEncodingStyle)XTextStyle, &tmp_prop);
  2326. if (status == Success)
  2327. targets[0] = tmp_prop.encoding;
  2328. else
  2329. targets[0] = 99999; /* XmbTextList... should never fail for XPCS
  2330. * characters. But just in case someones
  2331. * Xlib is broken, this prevents a core dump.
  2332. */
  2333. if (tmp_prop.value != NULL) XFree((char *)tmp_prop.value);
  2334. targets[1] = XmInternAtom(XtDisplay(w), "COMPOUND_TEXT", False);
  2335. targets[2] = XA_STRING;
  2336. targets[3] = XmInternAtom(XtDisplay(w), "TEXT", False);
  2337. n = 0;
  2338. XtSetArg(args[n], XmNimportTargets, targets); n++;
  2339. XtSetArg(args[n], XmNnumImportTargets, 4); n++;
  2340. /* XtSetArg(args[n], XmNdragProc, DragProcCallback); n++; */
  2341. XtSetArg(args[n], XmNdropProc, DropProcCallback); n++;
  2342. XmDropSiteRegister(w, args, n);
  2343. }
  2344. /*
  2345. * DRAG SITE code
  2346. */
  2347. /* ARGSUSED */
  2348. static void
  2349. StartDrag(
  2350. Widget w,
  2351. XEvent *event,
  2352. String *params,
  2353. Cardinal *num_params )
  2354. {
  2355. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  2356. Atom targets[4];
  2357. char * tmp_string = "ABC"; /* these are characters in XPCS, so... safe */
  2358. XTextProperty tmp_prop;
  2359. int status = 0;
  2360. Cardinal num_targets = 0;
  2361. Widget drag_icon;
  2362. Arg args[10];
  2363. int n = 0;
  2364. tmp_prop.value = NULL;
  2365. status = XmbTextListToTextProperty(XtDisplay(w), &tmp_string, 1,
  2366. (XICCEncodingStyle)XTextStyle, &tmp_prop);
  2367. if (status == Success)
  2368. targets[num_targets++] = tmp_prop.encoding;
  2369. else
  2370. targets[num_targets++] = 99999; /* XmbTextList... should never fail
  2371. * for XPCS characters. But just in
  2372. * case someones Xlib is broken,
  2373. * this prevents a core dump.
  2374. */
  2375. if (tmp_prop.value != NULL) XFree((char *)tmp_prop.value);
  2376. targets[num_targets++] = XmInternAtom(XtDisplay(w), "COMPOUND_TEXT", False);
  2377. targets[num_targets++] = XA_STRING;
  2378. targets[num_targets++] = XmInternAtom(XtDisplay(w), "TEXT", False);
  2379. drag_icon = (Widget) XmeGetTextualDragIcon(w);
  2380. n = 0;
  2381. XtSetArg(args[n], XmNcursorBackground, tw->core.background_pixel); n++;
  2382. XtSetArg(args[n], XmNcursorForeground, tw->primitive.foreground); n++;
  2383. XtSetArg(args[n], XmNsourceCursorIcon, drag_icon); n++;
  2384. XtSetArg(args[n], XmNexportTargets, targets); n++;
  2385. XtSetArg(args[n], XmNnumExportTargets, num_targets); n++;
  2386. XtSetArg(args[n], XmNconvertProc, _DtTermPrimSelectConvert); n++;
  2387. XtSetArg(args[n], XmNclientData, w); n++;
  2388. XtSetArg(args[n], XmNdragOperations, ( XmDROP_COPY)); n++;
  2389. (void) XmDragStart(w, event, args, n);
  2390. }
  2391. static Position
  2392. GetXFromPos(Widget w, XmTextPosition pos)
  2393. {
  2394. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  2395. DtTermPrimData tpd = tw->term.tpd;
  2396. TermBuffer pb;
  2397. short row,col;
  2398. TermCharInfoRec charInfoRec ;
  2399. posToBufferRowCol(tw,pos,&pb,&row,&col) ;
  2400. return(tpd->offsetX+col*tpd->cellWidth);
  2401. }
  2402. /* ARGSUSED */
  2403. void
  2404. _DtTermPrimSelectProcessBDrag(
  2405. Widget w,
  2406. XEvent *event,
  2407. char **params,
  2408. Cardinal *num_params )
  2409. {
  2410. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
  2411. TermSelectInfo selectInfo =
  2412. ((DtTermPrimitiveWidget)w)->term.tpd->selectInfo;
  2413. XmTextPosition position, left, right;
  2414. Position left_x, left_y, right_x, right_y;
  2415. /* InputData data = tw->text.input->data; */
  2416. selectInfo->cancel = False;
  2417. position = xyToPos(tw, event->xbutton.x, event->xbutton.y);
  2418. if (_DtTermPrimSelectGetSelection(w, &left, &right) &&
  2419. (right != left)) {
  2420. if ((position > left && position < right)
  2421. || (position == left &&
  2422. event->xbutton.x > GetXFromPos(w, left))
  2423. || (position == right &&
  2424. event->xbutton.x < GetXFromPos(w, right))) {
  2425. selectInfo->sel_start = False;
  2426. StartDrag(w, event, params, num_params);
  2427. }
  2428. else
  2429. {
  2430. selectInfo->sel_start = True ;
  2431. }
  2432. }
  2433. else selectInfo->sel_start = True ;
  2434. }
  2435. /* This is the menu interface for copy clipboard */
  2436. Boolean
  2437. _DtTermPrimSelectCopyClipboard
  2438. (
  2439. Widget w,
  2440. Time copy_time
  2441. )
  2442. {
  2443. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  2444. XmTextPosition begin;
  2445. XmTextPosition end;
  2446. char *selected_string = NULL; /* text selection */
  2447. long item_id = 0L; /* clipboard item id */
  2448. long data_id = 0L; /* clipboard data id */
  2449. int status; /* clipboard status */
  2450. XmString clip_label;
  2451. XTextProperty tmp_prop;
  2452. Display *display = XtDisplay(w);
  2453. Window window = XtWindow(w);
  2454. char *atom_name;
  2455. if ( _DtTermPrimSelectGetSelection(w, &begin, &end) && begin != end ) {
  2456. selected_string = getString(w, begin, end, False);
  2457. }
  2458. /*
  2459. * Using the Xm clipboard facilities,
  2460. * copy the selected text to the clipboard
  2461. */
  2462. tmp_prop.value = NULL;
  2463. if (selected_string != NULL) {
  2464. clip_label = XmStringCreateLocalized ("XM_TERM");
  2465. /* start copy to clipboard */
  2466. status = XmClipboardStartCopy(display, window, clip_label, copy_time,
  2467. w, NULL, &item_id);
  2468. if (status != ClipboardSuccess) {
  2469. XtFree(selected_string);
  2470. XmStringFree(clip_label);
  2471. return False;
  2472. }
  2473. status = XmbTextListToTextProperty(display, &selected_string, 1,
  2474. (XICCEncodingStyle)XStdICCTextStyle,
  2475. &tmp_prop);
  2476. if (status != Success && status <= 0) {
  2477. XmClipboardCancelCopy(display, window, item_id);
  2478. XtFree(selected_string);
  2479. XmStringFree(clip_label);
  2480. return False;
  2481. }
  2482. atom_name = XGetAtomName(display, tmp_prop.encoding);
  2483. /* move the data to the clipboard */
  2484. status = XmClipboardCopy(display, window, item_id, atom_name,
  2485. (XtPointer)tmp_prop.value, tmp_prop.nitems,
  2486. 0, &data_id);
  2487. XtFree(atom_name);
  2488. if (status != ClipboardSuccess) {
  2489. XmClipboardCancelCopy(display, window, item_id);
  2490. XFree((char*)tmp_prop.value);
  2491. XmStringFree(clip_label);
  2492. return False;
  2493. }
  2494. /* end the copy to the clipboard */
  2495. status = XmClipboardEndCopy (display, window, item_id);
  2496. XtFree((char*)tmp_prop.value);
  2497. XmStringFree(clip_label);
  2498. if (status != ClipboardSuccess) return False;
  2499. } else
  2500. return False;
  2501. if (selected_string!=NULL)
  2502. XtFree(selected_string);
  2503. return True;
  2504. }
  2505. /* This is the event interface for copy clipboard */
  2506. void
  2507. _DtTermPrimSelectCopyClipboardEventIF
  2508. (
  2509. Widget w,
  2510. XEvent *event,
  2511. String *params,
  2512. Cardinal *num_params
  2513. )
  2514. {
  2515. _DtTermPrimSelectCopyClipboard(w,event->xkey.time) ;
  2516. }
  2517. /*
  2518. * Retrieves the current data from the clipboard
  2519. * and paste it at the current cursor position
  2520. */
  2521. Boolean
  2522. _DtTermPrimSelectPasteClipboard
  2523. (
  2524. Widget w
  2525. )
  2526. {
  2527. XmTextPosition sel_left = 0;
  2528. XmTextPosition sel_right = 0;
  2529. XmTextPosition paste_pos_left, paste_pos_right, cursorPos;
  2530. int status; /* clipboard status */
  2531. char * buffer; /* temporary text buffer */
  2532. unsigned long length; /* length of buffer */
  2533. unsigned long outlength = 0L; /* length of bytes copied */
  2534. long private_id = 0L; /* id of item on clipboard */
  2535. Boolean dest_disjoint = True;
  2536. XmTextBlockRec block, newblock;
  2537. Display *display = XtDisplay(w);
  2538. Window window = XtWindow(w);
  2539. Boolean get_ct = False;
  2540. Boolean freeBlock;
  2541. XTextProperty tmp_prop;
  2542. int malloc_size = 0;
  2543. int num_vals;
  2544. char **tmp_value;
  2545. char * total_tmp_value = NULL;
  2546. int i;
  2547. status = XmClipboardInquireLength(display, window, "STRING", &length);
  2548. if (status == ClipboardNoData || length == 0) {
  2549. status = XmClipboardInquireLength(display, window, "COMPOUND_TEXT",
  2550. &length);
  2551. if (status == ClipboardNoData || length == 0) return False;
  2552. get_ct = True;
  2553. }
  2554. /* malloc length of clipboard data */
  2555. buffer = XtMalloc((unsigned) length);
  2556. if (!get_ct) {
  2557. status = XmClipboardRetrieve(display, window, "STRING", buffer,
  2558. length, &outlength, &private_id);
  2559. } else {
  2560. status = XmClipboardRetrieve(display, window, "COMPOUND_TEXT",
  2561. buffer, length, &outlength, &private_id);
  2562. }
  2563. if (status != ClipboardSuccess) {
  2564. XmClipboardEndRetrieve(display, window);
  2565. XtFree(buffer);
  2566. return False;
  2567. }
  2568. tmp_prop.value = (unsigned char *) buffer;
  2569. if (!get_ct)
  2570. tmp_prop.encoding = XA_STRING;
  2571. else
  2572. tmp_prop.encoding = XmInternAtom(display, "COMPOUND_TEXT", False);
  2573. tmp_prop.format = 8;
  2574. tmp_prop.nitems = outlength;
  2575. num_vals = 0;
  2576. status = XmbTextPropertyToTextList(display, &tmp_prop, &tmp_value,
  2577. &num_vals);
  2578. /* if no conversions, num_vals doesn't change */
  2579. if (num_vals && (status == Success || status > 0)) {
  2580. for (i = 0; i < num_vals ; i++)
  2581. malloc_size += strlen(tmp_value[i]);
  2582. total_tmp_value = XtMalloc ((unsigned) malloc_size + 1);
  2583. total_tmp_value[0] = '\0';
  2584. for (i = 0; i < num_vals ; i++)
  2585. strcat(total_tmp_value, tmp_value[i]);
  2586. block.ptr = total_tmp_value;
  2587. block.length = strlen(total_tmp_value);
  2588. block.format = XmFMT_8_BIT;
  2589. XFreeStringList(tmp_value);
  2590. } else {
  2591. malloc_size = 1; /* to force space to be freed */
  2592. total_tmp_value = XtMalloc ((unsigned)1);
  2593. *total_tmp_value = '\0';
  2594. block.ptr = total_tmp_value;
  2595. block.length = 0;
  2596. block.format = XmFMT_8_BIT;
  2597. }
  2598. /* add new text */
  2599. if ( block.length )
  2600. {
  2601. char *pChar, *pCharEnd, *pCharFollow;
  2602. pCharEnd = block.ptr + block.length;
  2603. pCharFollow = (char *)block.ptr;
  2604. for (pChar = (char *)block.ptr; pChar < pCharEnd; pChar++)
  2605. {
  2606. if (*pChar == '\n')
  2607. {
  2608. *pChar = '\r';
  2609. DtTermSubprocSend(w, (unsigned char *) pCharFollow,
  2610. pChar - pCharFollow + 1);
  2611. pCharFollow = pChar + 1;
  2612. }
  2613. }
  2614. if (pCharFollow < pCharEnd)
  2615. {
  2616. DtTermSubprocSend(w, (unsigned char *) pCharFollow,
  2617. pCharEnd - pCharFollow);
  2618. }
  2619. }
  2620. XtFree(buffer);
  2621. if (malloc_size != 0) XtFree(total_tmp_value);
  2622. (void) _DtTermPrimCursorOn(w);
  2623. return True;
  2624. }
  2625. /* This is the event interface for paste clipboard */
  2626. void
  2627. _DtTermPrimSelectPasteClipboardEventIF
  2628. (
  2629. Widget w,
  2630. XEvent *event,
  2631. String *params,
  2632. Cardinal *num_params
  2633. )
  2634. {
  2635. _DtTermPrimSelectPasteClipboard(w) ;
  2636. }
  2637. /*
  2638. * This is for the SUN two button mouse
  2639. */
  2640. static Bool
  2641. LookForButton (
  2642. Display * display,
  2643. XEvent * event,
  2644. XPointer arg)
  2645. {
  2646. #define DAMPING 5
  2647. #define ABS_DELTA(x1, x2) (x1 < x2 ? x2 - x1 : x1 - x2)
  2648. if( event->type == MotionNotify) {
  2649. XEvent * press = (XEvent *) arg;
  2650. if (ABS_DELTA(press->xbutton.x_root, event->xmotion.x_root) > DAMPING ||
  2651. ABS_DELTA(press->xbutton.y_root, event->xmotion.y_root) > DAMPING)
  2652. return(True);
  2653. }
  2654. else if (event->type == ButtonRelease)
  2655. return(True);
  2656. return(False);
  2657. }
  2658. /* ARGSUSED */
  2659. static Boolean
  2660. XmTestInSelection(
  2661. Widget w,
  2662. XEvent *event )
  2663. {
  2664. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  2665. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  2666. XmTextPosition position, left, right;
  2667. Position left_x, right_x, dummy;
  2668. position = xyToPos(tw, event->xbutton.x, event->xbutton.y);
  2669. if ((!(_DtTermPrimSelectGetSelection(w, &left, &right) &&
  2670. (left != right && (position > left && position < right)))
  2671. || (position == left &&
  2672. event->xbutton.x > GetXFromPos(w, left))
  2673. || (position == right &&
  2674. event->xbutton.x < GetXFromPos(w, right)))
  2675. ||
  2676. /* or if it is part of a multiclick sequence */
  2677. (event->xbutton.time > selectInfo->lastTime &&
  2678. event->xbutton.time - selectInfo->lastTime <
  2679. XtGetMultiClickTime(XtDisplay((Widget)w))) )
  2680. return(False);
  2681. else {
  2682. /* The determination of whether this is a transfer drag cannot be made
  2683. until a Motion event comes in. It is not a drag as soon as a
  2684. ButtonUp event happens or the MultiClickTimeout expires. */
  2685. XEvent new;
  2686. XPeekIfEvent(XtDisplay(w), &new, LookForButton, (XPointer)event);
  2687. switch (new.type) {
  2688. case MotionNotify:
  2689. return(True);
  2690. break;
  2691. case ButtonRelease:
  2692. return(False);
  2693. break;
  2694. }
  2695. return(False);
  2696. }
  2697. }
  2698. #define SELECTION_ACTION 0
  2699. #define TRANSFER_ACTION 1
  2700. /* ARGSUSED */
  2701. void
  2702. _DtTermPrimSelect2ButtonMouse(
  2703. Widget w,
  2704. XEvent *event,
  2705. char **params,
  2706. Cardinal *num_params )
  2707. {
  2708. /* This action happens when Button1 is pressed and the Selection
  2709. and Transfer are integrated on Button1. It is passed two
  2710. parameters: the action to call when the event is a selection,
  2711. and the action to call when the event is a transfer. */
  2712. if (*num_params != 2 /* || !XmIsTextField(w) */)
  2713. return;
  2714. if (XmTestInSelection(w, event))
  2715. XtCallActionProc(w, params[TRANSFER_ACTION], event, params, *num_params);
  2716. else
  2717. XtCallActionProc(w, params[SELECTION_ACTION], event, params, *num_params);
  2718. }
  2719. void
  2720. _DtTermPrimSelectProcessCancel(
  2721. Widget w,
  2722. XEvent *event,
  2723. String *params,
  2724. Cardinal *num_params )
  2725. {
  2726. DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget)w;
  2727. TermSelectInfo selectInfo = tw->term.tpd->selectInfo;
  2728. XmTextPosition left_x, right_x;
  2729. XmParentInputActionRec p_event ;
  2730. if (!tw->term.allowOsfKeysyms) {
  2731. _DtTermPrimActionKeyInput(w,event,params,num_params);
  2732. return;
  2733. }
  2734. selectInfo->cancel = True ;
  2735. /* turn off the cursor */
  2736. _DtTermPrimCursorOff(w);
  2737. /* reset to origLeft and origRight */
  2738. setSelection (tw, selectInfo->origBegin, selectInfo->origEnd,
  2739. event->xkey.time, False) ;
  2740. /* turn on the cursor */
  2741. _DtTermPrimCursorOn(w);
  2742. if (selectInfo->selectID) {
  2743. XtRemoveTimeOut(selectInfo->selectID);
  2744. selectInfo->selectID = 0;
  2745. }
  2746. }