HyperText.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these librararies and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: HyperText.c /main/9 1999/10/14 14:45:38 mgreess $ */
  24. /************************************<+>*************************************
  25. ****************************************************************************
  26. **
  27. ** File: HyperText.c
  28. **
  29. ** Project: Text Graphic Display Library
  30. **
  31. ** Description: This body of code does all the work for hypertext links
  32. **
  33. **
  34. ** (c) Copyright 1987, 1988, 1989, 1990, 1991, 1992 Hewlett-Packard Company
  35. **
  36. ** (c) Copyright 1993, 1994 Hewlett-Packard Company
  37. ** (c) Copyright 1993, 1994 International Business Machines Corp.
  38. ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  39. ** (c) Copyright 1993, 1994 Novell, Inc.
  40. **
  41. **
  42. **
  43. ****************************************************************************
  44. ************************************<+>*************************************/
  45. /*
  46. * system includes
  47. */
  48. #include <errno.h>
  49. #include <signal.h>
  50. #include <stdlib.h>
  51. #ifdef __hpux
  52. #include <time.h>
  53. #else /* SUN and IBM */
  54. #ifdef _AIX
  55. #include <sys/select.h>
  56. #endif
  57. #include <sys/time.h>
  58. #include <sys/types.h>
  59. #endif
  60. #include <unistd.h>
  61. #include <sys/wait.h>
  62. #include <Xm/DialogS.h>
  63. /*
  64. * Canvas Engine
  65. */
  66. #include "CanvasP.h"
  67. /*
  68. * private includes
  69. */
  70. #include "HelpErrorP.h"
  71. #include "DisplayAreaP.h"
  72. #include "DisplayAreaI.h"
  73. #include "FontI.h"
  74. #include "HourGlassI.h"
  75. #include "HyperTextI.h"
  76. #include "HelposI.h"
  77. #ifdef NLS16
  78. #endif
  79. /******** Private Function Declarations ********/
  80. static void ProcessEvents (
  81. DtHelpDispAreaStruct *pDAS,
  82. pid_t child_pid,
  83. Boolean child_flag);
  84. /******** End Private Function Declarations ********/
  85. /*********************************************************************
  86. * Private Variables
  87. *********************************************************************/
  88. typedef struct {
  89. char *link_spec;
  90. int type;
  91. char *descrip;
  92. } HyperList;
  93. /*********************************************************************
  94. * Private Functions
  95. *********************************************************************/
  96. /*********************************************************************
  97. * ProcessEvents
  98. *
  99. * ProcessEvents will do a select on the socket until the child
  100. * dies.
  101. *
  102. *********************************************************************/
  103. static void
  104. ProcessEvents (
  105. DtHelpDispAreaStruct *pDAS,
  106. pid_t child_pid,
  107. Boolean child_flag)
  108. {
  109. int result;
  110. int rMask;
  111. int myFd;
  112. pid_t pid;
  113. struct timeval *topPtr;
  114. struct timeval toStruct;
  115. Display *dpy = XtDisplay (pDAS->dispWid);
  116. XEvent event;
  117. /*
  118. * set a small time out.
  119. */
  120. toStruct.tv_usec = 500000; /* 500 miliseconds */
  121. toStruct.tv_sec = 0;
  122. topPtr = &toStruct;
  123. /*
  124. * get the socket's file descriptor
  125. */
  126. myFd = ConnectionNumber (dpy);
  127. do
  128. {
  129. rMask = (1 << (myFd % 32)); /* on t.o. select will clear */
  130. if (!XPending(dpy))
  131. {
  132. #if 0
  133. result = select(myFd+1, &rMask, 0, 0, topPtr);
  134. #else
  135. result = select(myFd+1, ((fd_set *)&rMask), 0, 0, topPtr);
  136. #endif
  137. /*
  138. * check to see if the select happened because of
  139. * a system interrupt.
  140. */
  141. /*
  142. * otherwise an exposure event happened.
  143. * fall through and XPending will be true,
  144. * forcing us to go to the XmUpdateDisplay call.
  145. */
  146. }
  147. else
  148. {
  149. /*
  150. * SYSTEM - Use XtDisplatchEvent????
  151. */
  152. XNextEvent (dpy, &event);
  153. if (event.type == Expose)
  154. XmUpdateDisplay (pDAS->dispWid);
  155. }
  156. /*
  157. * check to see if the child is still going
  158. */
  159. pid = waitpid (child_pid, (int *) 0, WNOHANG);
  160. if (pid == child_pid || pid == -1)
  161. child_flag = True;
  162. } while (!child_flag);
  163. }
  164. /*********************************************************************
  165. * Internal Public Functions
  166. *********************************************************************/
  167. /*********************************************************************
  168. * _DtHelpExecProcedure
  169. *
  170. * _DtHelpExecProcedure will fork/exec the command passed in. It will
  171. * then allow only exposure events to be processed until the
  172. * child dies.
  173. *
  174. *********************************************************************/
  175. void
  176. _DtHelpExecProcedure (
  177. XtPointer client_data,
  178. char *cmd )
  179. {
  180. pid_t pid;
  181. XWindowAttributes attr;
  182. char *pShell = "sh";
  183. Widget shellWidget;
  184. Boolean childFlag = False;
  185. pid_t childPid;
  186. DtHelpDispAreaStruct *pDAS = (DtHelpDispAreaStruct *) client_data;
  187. /*
  188. * Turn on the wait cursor.
  189. */
  190. shellWidget = pDAS->dispWid;
  191. while (!XtIsSubclass(shellWidget, xmDialogShellWidgetClass))
  192. shellWidget = XtParent(shellWidget);
  193. _DtHelpTurnOnHourGlass(shellWidget);
  194. /*
  195. * Get the window event mask via the window attributes. Remember it.
  196. * Set the Input to only Exposure events.
  197. */
  198. XGetWindowAttributes (XtDisplay(pDAS->dispWid), XtWindow (pDAS->dispWid),
  199. &attr);
  200. XSelectInput (XtDisplay(pDAS->dispWid), XtWindow (pDAS->dispWid),
  201. ExposureMask);
  202. XSync (XtDisplay(pDAS->dispWid), 0);
  203. /*
  204. * initialize the global flag and variable.
  205. */
  206. childPid = -1;
  207. childFlag = False;
  208. /*
  209. * fork a child process.
  210. */
  211. #ifdef __hpux
  212. childPid = vfork ();
  213. #else
  214. childPid = fork ();
  215. #endif /* __hpux */
  216. /*
  217. * If the child, exec the cmd with a shell parent
  218. * so if the cmd ends in an ampersand, the command
  219. * will be put in the background and the shell will
  220. * die and return, creating a SIGCLD for us to catch.
  221. */
  222. if (childPid == 0)
  223. {
  224. execlp (pShell, pShell, "-c", cmd, ((char *) 0));
  225. _exit (1);
  226. }
  227. /*
  228. * Check to make sure the vfork was successful.
  229. */
  230. if (childPid != -1)
  231. {
  232. /*
  233. * check to see if the child is still going
  234. */
  235. pid = waitpid (childPid, (int *) 0, WNOHANG);
  236. if (!(pid == childPid || pid == -1))
  237. /*
  238. * process the exposure events in a special routine.
  239. */
  240. ProcessEvents (pDAS, childPid, childFlag);
  241. }
  242. /*
  243. * reset the input mask
  244. */
  245. XSelectInput (XtDisplay(pDAS->dispWid), XtWindow (pDAS->dispWid),
  246. (attr.your_event_mask));
  247. /*
  248. * turn off the wait cursor
  249. */
  250. _DtHelpTurnOffHourGlass(shellWidget);
  251. } /* End _DtHelpExecProcedure */
  252. /*********************************************************************
  253. * Function: _DtHelpProcessHyperSelection
  254. *
  255. * Determine if the user selected a segment that is a hypertext
  256. * link. If so, call the appropriate function to process it.
  257. *
  258. *********************************************************************/
  259. void
  260. _DtHelpProcessHyperSelection (
  261. XtPointer client_data,
  262. int downX,
  263. int downY,
  264. XEvent *event )
  265. {
  266. int upX;
  267. int upY;
  268. _DtCvLinkInfo ceHyper;
  269. DtHelpHyperTextStruct callData;
  270. DtHelpDispAreaStruct *pDAS = (DtHelpDispAreaStruct *) client_data;
  271. if (pDAS->hyperCall == NULL)
  272. return;
  273. downY = downY + pDAS->firstVisible - pDAS->decorThickness;
  274. downX = downX + pDAS->virtualX - pDAS->decorThickness;
  275. upX = event->xbutton.x + pDAS->virtualX - pDAS->decorThickness;
  276. upY = event->xbutton.y + pDAS->firstVisible - pDAS->decorThickness;
  277. /*
  278. * turn off the old traversal
  279. */
  280. if (pDAS->neededFlags & _DT_HELP_FOCUS_FLAG)
  281. {
  282. pDAS->toc_flag |= _DT_HELP_DRAW_TOC_IND;
  283. _DtCanvasMoveTraversal(pDAS->canvas, _DtCvTRAVERSAL_OFF, False,
  284. (XtIsRealized(pDAS->dispWid) ? True : False),
  285. NULL, NULL, NULL, NULL, NULL);
  286. }
  287. if (_DtCvSTATUS_OK ==
  288. _DtCanvasGetPosLink(pDAS->canvas,downX,downY,upX,upY,&ceHyper))
  289. {
  290. /*
  291. * turn the traversal on for the selected hyptext link.
  292. */
  293. if (pDAS->neededFlags & _DT_HELP_FOCUS_FLAG)
  294. {
  295. pDAS->toc_flag |= _DT_HELP_DRAW_TOC_IND;
  296. _DtCanvasMoveTraversal(pDAS->canvas, _DtCvTRAVERSAL_ID, False,
  297. (XtIsRealized(pDAS->dispWid) ? True : False),
  298. ceHyper.specification,
  299. NULL, NULL, NULL, NULL);
  300. }
  301. callData.reason = XmCR_ACTIVATE;
  302. callData.event = event;
  303. callData.window = XtWindow (pDAS->dispWid);
  304. callData.specification = ceHyper.specification;
  305. callData.hyper_type = ceHyper.hyper_type;
  306. callData.window_hint = ceHyper.win_hint;
  307. (*(pDAS->hyperCall)) (pDAS, pDAS->clientData, &callData);
  308. }
  309. else if (pDAS->neededFlags & _DT_HELP_FOCUS_FLAG)
  310. /*
  311. * turn the traversal back on
  312. */
  313. _DtCanvasMoveTraversal(pDAS->canvas, _DtCvTRAVERSAL_ON, False,
  314. (XtIsRealized(pDAS->dispWid) ? True : False),
  315. NULL, NULL, NULL, NULL, NULL);
  316. } /* End _DtHelpProcessHyperSelection */