PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/src/gui_at_sb.c

https://github.com/Brijen/macvim
C | 1209 lines | 942 code | 138 blank | 129 comment | 133 complexity | c982e0f20da9c51b0c0d1ffd93360f64 MD5 | raw file
  1. /* vi:set ts=8 sts=4 sw=4: */
  2. /*
  3. * MODIFIED ATHENA SCROLLBAR (USING ARROWHEADS AT ENDS OF TRAVEL)
  4. * Modifications Copyright 1992 by Mitch Trachtenberg
  5. * Rights, permissions, and disclaimer of warranty are as in the DEC and MIT
  6. * notice below.
  7. * $XConsortium: Scrollbar.c,v 1.72 94/04/17 20:12:40 kaleb Exp $
  8. */
  9. /*
  10. * Modified for Vim by Bill Foster and Bram Moolenaar
  11. */
  12. /*
  13. Copyright (c) 1987, 1988, 1994 X Consortium
  14. Permission is hereby granted, free of charge, to any person obtaining a copy
  15. of this software and associated documentation files (the "Software"), to deal
  16. in the Software without restriction, including without limitation the rights
  17. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. copies of the Software, and to permit persons to whom the Software is
  19. furnished to do so, subject to the following conditions:
  20. The above copyright notice and this permission notice shall be included in all
  21. copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE X
  25. CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. Except as contained in this notice, the name of the X Consortium shall not be
  29. used in advertising or otherwise to promote the sale, use or other dealings in
  30. this Software without prior written authorization from the X Consortium.
  31. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
  32. All Rights Reserved
  33. Permission to use, copy, modify, and distribute this software and its
  34. documentation for any purpose and without fee is hereby granted, provided that
  35. the above copyright notice appear in all copies and that both that copyright
  36. notice and this permission notice appear in supporting documentation, and that
  37. the name of Digital not be used in advertising or publicity pertaining to
  38. distribution of the software without specific, written prior permission.
  39. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  40. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DIGITAL
  41. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  42. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  43. OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  44. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  45. */
  46. /* ScrollBar.c */
  47. /* created by weissman, Mon Jul 7 13:20:03 1986 */
  48. /* converted by swick, Thu Aug 27 1987 */
  49. #include <X11/IntrinsicP.h>
  50. #include <X11/StringDefs.h>
  51. #include <X11/Xaw/XawInit.h>
  52. #include "vim.h"
  53. #include "gui_at_sb.h"
  54. #include <X11/Xmu/Drawing.h>
  55. /* Private definitions. */
  56. static char defaultTranslations[] =
  57. "<Btn1Down>: NotifyScroll()\n\
  58. <Btn2Down>: MoveThumb() NotifyThumb()\n\
  59. <Btn3Down>: NotifyScroll()\n\
  60. <Btn4Down>: ScrollOneLineUp()\n\
  61. Shift<Btn4Down>: ScrollPageUp()\n\
  62. <Btn5Down>: ScrollOneLineDown()\n\
  63. Shift<Btn5Down>: ScrollPageDown()\n\
  64. <Btn1Motion>: HandleThumb()\n\
  65. <Btn3Motion>: HandleThumb()\n\
  66. <Btn2Motion>: MoveThumb() NotifyThumb()\n\
  67. <BtnUp>: EndScroll()";
  68. static float floatZero = 0.0;
  69. #define Offset(field) XtOffsetOf(ScrollbarRec, field)
  70. static XtResource resources[] =
  71. {
  72. {XtNlength, XtCLength, XtRDimension, sizeof(Dimension),
  73. Offset(scrollbar.length), XtRImmediate, (XtPointer) 1},
  74. {XtNthickness, XtCThickness, XtRDimension, sizeof(Dimension),
  75. Offset(scrollbar.thickness), XtRImmediate, (XtPointer) 14},
  76. {XtNorientation, XtCOrientation, XtROrientation, sizeof(XtOrientation),
  77. Offset(scrollbar.orientation), XtRImmediate, (XtPointer) XtorientVertical},
  78. {XtNscrollProc, XtCCallback, XtRCallback, sizeof(XtPointer),
  79. Offset(scrollbar.scrollProc), XtRCallback, NULL},
  80. {XtNthumbProc, XtCCallback, XtRCallback, sizeof(XtPointer),
  81. Offset(scrollbar.thumbProc), XtRCallback, NULL},
  82. {XtNjumpProc, XtCCallback, XtRCallback, sizeof(XtPointer),
  83. Offset(scrollbar.jumpProc), XtRCallback, NULL},
  84. {XtNthumb, XtCThumb, XtRBitmap, sizeof(Pixmap),
  85. Offset(scrollbar.thumb), XtRImmediate, (XtPointer) XtUnspecifiedPixmap},
  86. {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  87. Offset(scrollbar.foreground), XtRString, XtDefaultForeground},
  88. {XtNshown, XtCShown, XtRFloat, sizeof(float),
  89. Offset(scrollbar.shown), XtRFloat, (XtPointer)&floatZero},
  90. {XtNtopOfThumb, XtCTopOfThumb, XtRFloat, sizeof(float),
  91. Offset(scrollbar.top), XtRFloat, (XtPointer)&floatZero},
  92. {XtNmaxOfThumb, XtCMaxOfThumb, XtRFloat, sizeof(float),
  93. Offset(scrollbar.max), XtRFloat, (XtPointer)&floatZero},
  94. {XtNminimumThumb, XtCMinimumThumb, XtRDimension, sizeof(Dimension),
  95. Offset(scrollbar.min_thumb), XtRImmediate, (XtPointer) 7},
  96. {XtNshadowWidth, XtCShadowWidth, XtRDimension, sizeof(Dimension),
  97. Offset(scrollbar.shadow_width), XtRImmediate, (XtPointer) 1},
  98. {XtNtopShadowPixel, XtCTopShadowPixel, XtRPixel, sizeof(Pixel),
  99. Offset(scrollbar.top_shadow_pixel), XtRString, XtDefaultBackground},
  100. {XtNbottomShadowPixel, XtCBottomShadowPixel, XtRPixel, sizeof(Pixel),
  101. Offset(scrollbar.bot_shadow_pixel), XtRString, XtDefaultForeground},
  102. {XtNlimitThumb, XtCLimitThumb, XtRBool, sizeof(Bool),
  103. Offset(scrollbar.limit_thumb), XtRImmediate, (XtPointer)0}
  104. };
  105. #undef Offset
  106. static void ClassInitialize __ARGS((void));
  107. static void Initialize __ARGS((Widget, Widget, ArgList, Cardinal *));
  108. static void Destroy __ARGS((Widget));
  109. static void Realize __ARGS((Widget, Mask *, XSetWindowAttributes *));
  110. static void Resize __ARGS((Widget));
  111. static void Redisplay __ARGS((Widget, XEvent *, Region));
  112. static Boolean SetValues __ARGS((Widget, Widget, Widget, ArgList, Cardinal *));
  113. static void HandleThumb __ARGS((Widget, XEvent *, String *, Cardinal *));
  114. static void MoveThumb __ARGS((Widget, XEvent *, String *, Cardinal *));
  115. static void NotifyThumb __ARGS((Widget, XEvent *, String *, Cardinal *));
  116. static void NotifyScroll __ARGS((Widget, XEvent *, String *, Cardinal *));
  117. static void EndScroll __ARGS((Widget, XEvent *, String *, Cardinal *));
  118. static void ScrollOneLineUp __ARGS((Widget, XEvent *, String *, Cardinal *));
  119. static void ScrollOneLineDown __ARGS((Widget, XEvent *, String *, Cardinal *));
  120. static void ScrollPageUp __ARGS((Widget, XEvent *, String *, Cardinal *));
  121. static void ScrollPageDown __ARGS((Widget, XEvent *, String *, Cardinal *));
  122. static void ScrollSome __ARGS((Widget w, XEvent *event, int call_data));
  123. static void _Xaw3dDrawShadows __ARGS((Widget, XEvent *, Region, int));
  124. static void AllocTopShadowGC __ARGS((Widget));
  125. static void AllocBotShadowGC __ARGS((Widget));
  126. static XtActionsRec actions[] =
  127. {
  128. {"HandleThumb", HandleThumb},
  129. {"MoveThumb", MoveThumb},
  130. {"NotifyThumb", NotifyThumb},
  131. {"NotifyScroll", NotifyScroll},
  132. {"EndScroll", EndScroll},
  133. {"ScrollOneLineUp", ScrollOneLineUp},
  134. {"ScrollOneLineDown", ScrollOneLineDown},
  135. {"ScrollPageUp", ScrollPageUp},
  136. {"ScrollPageDown", ScrollPageDown}
  137. };
  138. ScrollbarClassRec vim_scrollbarClassRec =
  139. {
  140. { /* core fields */
  141. /* superclass */ (WidgetClass) &simpleClassRec,
  142. /* class_name */ "Scrollbar",
  143. /* size */ sizeof(ScrollbarRec),
  144. /* class_initialize */ ClassInitialize,
  145. /* class_part_init */ NULL,
  146. /* class_inited */ FALSE,
  147. /* initialize */ Initialize,
  148. /* initialize_hook */ NULL,
  149. /* realize */ Realize,
  150. /* actions */ actions,
  151. /* num_actions */ XtNumber(actions),
  152. /* resources */ resources,
  153. /* num_resources */ XtNumber(resources),
  154. /* xrm_class */ NULLQUARK,
  155. /* compress_motion */ TRUE,
  156. /* compress_exposure*/ TRUE,
  157. /* compress_enterleave*/ TRUE,
  158. /* visible_interest */ FALSE,
  159. /* destroy */ Destroy,
  160. /* resize */ Resize,
  161. /* expose */ Redisplay,
  162. /* set_values */ SetValues,
  163. /* set_values_hook */ NULL,
  164. /* set_values_almost */ XtInheritSetValuesAlmost,
  165. /* get_values_hook */ NULL,
  166. /* accept_focus */ NULL,
  167. /* version */ XtVersion,
  168. /* callback_private */ NULL,
  169. /* tm_table */ defaultTranslations,
  170. /* query_geometry */ XtInheritQueryGeometry,
  171. /* display_accelerator*/ XtInheritDisplayAccelerator,
  172. /* extension */ NULL
  173. },
  174. { /* simple fields */
  175. /* change_sensitive */ XtInheritChangeSensitive,
  176. #ifndef OLDXAW
  177. /* extension */ NULL
  178. #endif
  179. },
  180. { /* scrollbar fields */
  181. /* empty */ 0
  182. }
  183. };
  184. WidgetClass vim_scrollbarWidgetClass = (WidgetClass)&vim_scrollbarClassRec;
  185. #define NoButton -1
  186. #define PICKLENGTH(widget, x, y) \
  187. ((widget->scrollbar.orientation == XtorientHorizontal) ? (x) : (y))
  188. #define AT_MIN(x,y) ((x) < (y) ? (x) : (y))
  189. #define AT_MAX(x,y) ((x) > (y) ? (x) : (y))
  190. #define LINE_DELAY 300
  191. #define PAGE_DELAY 300
  192. #define LINE_REPEAT 50
  193. #define PAGE_REPEAT 250
  194. static void
  195. ClassInitialize()
  196. {
  197. XawInitializeWidgetSet();
  198. XtAddConverter( XtRString, XtROrientation, XmuCvtStringToOrientation,
  199. (XtConvertArgList)NULL, (Cardinal)0 );
  200. }
  201. #define MARGIN(sbw) (sbw)->scrollbar.thickness + (sbw)->scrollbar.shadow_width
  202. static void
  203. FillArea(sbw, top, bottom, fill, draw_shadow)
  204. ScrollbarWidget sbw;
  205. Position top, bottom;
  206. int fill;
  207. int draw_shadow;
  208. {
  209. int tlen = bottom - top; /* length of thumb in pixels */
  210. int sw, margin, floor;
  211. int lx, ly, lw, lh;
  212. if (bottom <= 0 || bottom <= top)
  213. return;
  214. sw = sbw->scrollbar.shadow_width;
  215. if (sw < 0)
  216. sw = 0;
  217. margin = MARGIN (sbw);
  218. floor = sbw->scrollbar.length - margin + 2;
  219. if (sbw->scrollbar.orientation == XtorientHorizontal)
  220. {
  221. lx = ((top < margin) ? margin : top);
  222. ly = sw;
  223. lw = (((top + tlen) > floor) ? floor - top : tlen);
  224. lh = sbw->core.height - 2 * sw;
  225. }
  226. else
  227. {
  228. lx = sw;
  229. ly = ((top < margin) ? margin : top);
  230. lw = sbw->core.width - 2 * sw;
  231. lh = (((top + tlen) > floor) ? floor - top : tlen);
  232. }
  233. if (lh <= 0 || lw <= 0)
  234. return;
  235. if (draw_shadow)
  236. {
  237. if (!(sbw->scrollbar.orientation == XtorientHorizontal))
  238. {
  239. /* Top border */
  240. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  241. sbw->scrollbar.top_shadow_GC,
  242. lx, ly, lx + lw - 1, ly);
  243. /* Bottom border */
  244. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  245. sbw->scrollbar.bot_shadow_GC,
  246. lx, ly + lh - 1, lx + lw - 1, ly + lh - 1);
  247. }
  248. else
  249. {
  250. /* Left border */
  251. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  252. sbw->scrollbar.top_shadow_GC,
  253. lx, ly, lx, ly + lh - 1);
  254. /* Right border */
  255. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  256. sbw->scrollbar.bot_shadow_GC,
  257. lx + lw - 1, ly, lx + lw - 1, ly + lh - 1);
  258. }
  259. return;
  260. }
  261. if (fill)
  262. {
  263. XFillRectangle(XtDisplay((Widget) sbw), XtWindow((Widget) sbw),
  264. sbw->scrollbar.gc,
  265. lx, ly, (unsigned int) lw, (unsigned int) lh);
  266. if (!(sbw->scrollbar.orientation == XtorientHorizontal))
  267. {
  268. /* Left border */
  269. XDrawLine(XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  270. sbw->scrollbar.top_shadow_GC,
  271. lx, ly, lx, ly + lh - 1);
  272. /* Right border */
  273. XDrawLine(XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  274. sbw->scrollbar.bot_shadow_GC,
  275. lx + lw - 1, ly, lx + lw - 1, ly + lh - 1);
  276. }
  277. else
  278. {
  279. /* Top border */
  280. XDrawLine(XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  281. sbw->scrollbar.top_shadow_GC,
  282. lx, ly, lx + lw - 1, ly);
  283. /* Bottom border */
  284. XDrawLine(XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  285. sbw->scrollbar.bot_shadow_GC,
  286. lx, ly + lh - 1, lx + lw - 1, ly + lh - 1);
  287. }
  288. }
  289. else
  290. {
  291. XClearArea(XtDisplay((Widget) sbw), XtWindow((Widget) sbw),
  292. lx, ly, (unsigned int) lw, (unsigned int) lh, FALSE);
  293. }
  294. }
  295. /* Paint the thumb in the area specified by sbw->top and
  296. sbw->shown. The old area is erased. The painting and
  297. erasing is done cleverly so that no flickering will occur.
  298. */
  299. static void
  300. PaintThumb(sbw)
  301. ScrollbarWidget sbw;
  302. {
  303. Position oldtop, oldbot, newtop, newbot;
  304. Dimension margin, tzl;
  305. margin = MARGIN (sbw);
  306. tzl = sbw->scrollbar.length - 2 * margin;
  307. newtop = margin + (int)(tzl * sbw->scrollbar.top);
  308. newbot = newtop + (int)(tzl * sbw->scrollbar.shown) + 1;
  309. if (newbot < newtop + (int)sbw->scrollbar.min_thumb)
  310. newbot = newtop + sbw->scrollbar.min_thumb;
  311. oldtop = sbw->scrollbar.topLoc;
  312. oldbot = oldtop + sbw->scrollbar.shownLength;
  313. sbw->scrollbar.topLoc = newtop;
  314. sbw->scrollbar.shownLength = newbot - newtop;
  315. if (XtIsRealized ((Widget) sbw))
  316. {
  317. if (newtop < oldtop)
  318. FillArea(sbw, newtop, AT_MIN(newbot, oldtop+1),1,0);
  319. if (newtop > oldtop)
  320. FillArea(sbw, oldtop, AT_MIN(newtop, oldbot ),0,0);
  321. if (newbot < oldbot)
  322. FillArea(sbw, AT_MAX(newbot, oldtop), oldbot, 0,0);
  323. if (newbot > oldbot)
  324. FillArea(sbw, AT_MAX(newtop, oldbot-1), newbot, 1,0);
  325. /* Only draw the missing shadows */
  326. FillArea(sbw, newtop, newbot, 0, 1);
  327. }
  328. }
  329. static void
  330. PaintArrows(sbw)
  331. ScrollbarWidget sbw;
  332. {
  333. XPoint point[6];
  334. Dimension thickness = sbw->scrollbar.thickness - 1;
  335. Dimension size;
  336. Dimension off;
  337. if (XtIsRealized((Widget) sbw))
  338. {
  339. if ((int)thickness * 2 > (int)sbw->scrollbar.length)
  340. {
  341. size = sbw->scrollbar.length / 2;
  342. off = (int)(thickness - size) / 2;
  343. }
  344. else
  345. {
  346. size = thickness;
  347. off = 0;
  348. }
  349. point[0].x = off + sbw->scrollbar.shadow_width;
  350. point[0].y = size;
  351. point[1].x = thickness - off - sbw->scrollbar.shadow_width;
  352. point[1].y = size;
  353. point[2].x = thickness / 2;
  354. point[2].y = sbw->scrollbar.shadow_width;
  355. point[3].x = off + sbw->scrollbar.shadow_width;
  356. point[3].y = sbw->scrollbar.length - size;
  357. point[4].x = thickness - off - sbw->scrollbar.shadow_width;
  358. point[4].y = sbw->scrollbar.length - size;
  359. point[5].x = thickness / 2;
  360. point[5].y = sbw->scrollbar.length - sbw->scrollbar.shadow_width - 1;
  361. /* horizontal arrows require that x and y coordinates be swapped */
  362. if (sbw->scrollbar.orientation == XtorientHorizontal)
  363. {
  364. int n;
  365. int swap;
  366. for (n = 0; n < 6; n++)
  367. {
  368. swap = point[n].x;
  369. point[n].x = point[n].y;
  370. point[n].y = swap;
  371. }
  372. }
  373. /* draw the up/left arrow */
  374. XFillPolygon (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  375. sbw->scrollbar.gc,
  376. point, 3,
  377. Convex, CoordModeOrigin);
  378. XDrawLines (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  379. sbw->scrollbar.bot_shadow_GC,
  380. point, 3,
  381. CoordModeOrigin);
  382. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  383. sbw->scrollbar.top_shadow_GC,
  384. point[0].x, point[0].y,
  385. point[2].x, point[2].y);
  386. /* draw the down/right arrow */
  387. XFillPolygon (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  388. sbw->scrollbar.gc,
  389. point+3, 3,
  390. Convex, CoordModeOrigin);
  391. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  392. sbw->scrollbar.top_shadow_GC,
  393. point[3].x, point[3].y,
  394. point[4].x, point[4].y);
  395. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  396. sbw->scrollbar.top_shadow_GC,
  397. point[3].x, point[3].y,
  398. point[5].x, point[5].y);
  399. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  400. sbw->scrollbar.bot_shadow_GC,
  401. point[4].x, point[4].y,
  402. point[5].x, point[5].y);
  403. }
  404. }
  405. static void
  406. Destroy(w)
  407. Widget w;
  408. {
  409. ScrollbarWidget sbw = (ScrollbarWidget) w;
  410. if (sbw->scrollbar.timer_id != (XtIntervalId) 0)
  411. XtRemoveTimeOut (sbw->scrollbar.timer_id);
  412. XtReleaseGC(w, sbw->scrollbar.gc);
  413. XtReleaseGC(w, sbw->scrollbar.top_shadow_GC);
  414. XtReleaseGC(w, sbw->scrollbar.bot_shadow_GC);
  415. }
  416. static void
  417. CreateGC(w)
  418. Widget w;
  419. {
  420. ScrollbarWidget sbw = (ScrollbarWidget) w;
  421. XGCValues gcValues;
  422. XtGCMask mask;
  423. unsigned int depth = 1;
  424. if (sbw->scrollbar.thumb == XtUnspecifiedPixmap)
  425. {
  426. sbw->scrollbar.thumb = XmuCreateStippledPixmap (XtScreen(w),
  427. (Pixel) 1, (Pixel) 0, depth);
  428. }
  429. else if (sbw->scrollbar.thumb != None)
  430. {
  431. Window root;
  432. int x, y;
  433. unsigned int width, height, bw;
  434. if (XGetGeometry (XtDisplay(w), sbw->scrollbar.thumb, &root, &x, &y,
  435. &width, &height, &bw, &depth) == 0)
  436. EMSG(_("Scrollbar Widget: Could not get geometry of thumb pixmap."));
  437. }
  438. gcValues.foreground = sbw->scrollbar.foreground;
  439. gcValues.background = sbw->core.background_pixel;
  440. mask = GCForeground | GCBackground;
  441. if (sbw->scrollbar.thumb != None)
  442. {
  443. gcValues.fill_style = FillSolid;
  444. mask |= GCFillStyle;
  445. }
  446. /* the creation should be non-caching, because */
  447. /* we now set and clear clip masks on the gc returned */
  448. sbw->scrollbar.gc = XtGetGC (w, mask, &gcValues);
  449. }
  450. static void
  451. SetDimensions(sbw)
  452. ScrollbarWidget sbw;
  453. {
  454. if (sbw->scrollbar.orientation == XtorientVertical)
  455. {
  456. sbw->scrollbar.length = sbw->core.height;
  457. sbw->scrollbar.thickness = sbw->core.width;
  458. }
  459. else
  460. {
  461. sbw->scrollbar.length = sbw->core.width;
  462. sbw->scrollbar.thickness = sbw->core.height;
  463. }
  464. }
  465. static void
  466. Initialize(request, new, args, num_args)
  467. Widget request UNUSED; /* what the client asked for */
  468. Widget new; /* what we're going to give him */
  469. ArgList args UNUSED;
  470. Cardinal *num_args UNUSED;
  471. {
  472. ScrollbarWidget sbw = (ScrollbarWidget) new;
  473. CreateGC(new);
  474. AllocTopShadowGC(new);
  475. AllocBotShadowGC(new);
  476. if (sbw->core.width == 0)
  477. sbw->core.width = (sbw->scrollbar.orientation == XtorientVertical)
  478. ? sbw->scrollbar.thickness : sbw->scrollbar.length;
  479. if (sbw->core.height == 0)
  480. sbw->core.height = (sbw->scrollbar.orientation == XtorientHorizontal)
  481. ? sbw->scrollbar.thickness : sbw->scrollbar.length;
  482. SetDimensions(sbw);
  483. sbw->scrollbar.scroll_mode = SMODE_NONE;
  484. sbw->scrollbar.timer_id = (XtIntervalId)0;
  485. sbw->scrollbar.topLoc = 0;
  486. sbw->scrollbar.shownLength = sbw->scrollbar.min_thumb;
  487. }
  488. static void
  489. Realize(w, valueMask, attributes)
  490. Widget w;
  491. Mask *valueMask;
  492. XSetWindowAttributes *attributes;
  493. {
  494. /* The Simple widget actually stuffs the value in the valuemask. */
  495. (*vim_scrollbarWidgetClass->core_class.superclass->core_class.realize)
  496. (w, valueMask, attributes);
  497. }
  498. static Boolean
  499. SetValues(current, request, desired, args, num_args)
  500. Widget current; /* what I am */
  501. Widget request UNUSED; /* what he wants me to be */
  502. Widget desired; /* what I will become */
  503. ArgList args UNUSED;
  504. Cardinal *num_args UNUSED;
  505. {
  506. ScrollbarWidget sbw = (ScrollbarWidget) current;
  507. ScrollbarWidget dsbw = (ScrollbarWidget) desired;
  508. Boolean redraw = FALSE;
  509. /*
  510. * If these values are outside the acceptable range ignore them...
  511. */
  512. if (dsbw->scrollbar.top < 0.0 || dsbw->scrollbar.top > 1.0)
  513. dsbw->scrollbar.top = sbw->scrollbar.top;
  514. if (dsbw->scrollbar.shown < 0.0 || dsbw->scrollbar.shown > 1.0)
  515. dsbw->scrollbar.shown = sbw->scrollbar.shown;
  516. /*
  517. * Change colors and stuff...
  518. */
  519. if (XtIsRealized(desired))
  520. {
  521. if (sbw->scrollbar.foreground != dsbw->scrollbar.foreground ||
  522. sbw->core.background_pixel != dsbw->core.background_pixel ||
  523. sbw->scrollbar.thumb != dsbw->scrollbar.thumb)
  524. {
  525. XtReleaseGC(desired, sbw->scrollbar.gc);
  526. CreateGC (desired);
  527. redraw = TRUE;
  528. }
  529. if (sbw->scrollbar.top != dsbw->scrollbar.top ||
  530. sbw->scrollbar.shown != dsbw->scrollbar.shown)
  531. redraw = TRUE;
  532. }
  533. return redraw;
  534. }
  535. static void
  536. Resize(w)
  537. Widget w;
  538. {
  539. /* ForgetGravity has taken care of background, but thumb may
  540. * have to move as a result of the new size. */
  541. SetDimensions ((ScrollbarWidget) w);
  542. Redisplay(w, (XEvent*) NULL, (Region)NULL);
  543. }
  544. static void
  545. Redisplay(w, event, region)
  546. Widget w;
  547. XEvent *event;
  548. Region region;
  549. {
  550. ScrollbarWidget sbw = (ScrollbarWidget) w;
  551. int x, y;
  552. unsigned int width, height;
  553. _Xaw3dDrawShadows(w, event, region, FALSE);
  554. if (sbw->scrollbar.orientation == XtorientHorizontal)
  555. {
  556. x = sbw->scrollbar.topLoc;
  557. y = 1;
  558. width = sbw->scrollbar.shownLength;
  559. height = sbw->core.height - 2;
  560. }
  561. else
  562. {
  563. x = 1;
  564. y = sbw->scrollbar.topLoc;
  565. width = sbw->core.width - 2;
  566. height = sbw->scrollbar.shownLength;
  567. }
  568. if (region == NULL ||
  569. XRectInRegion (region, x, y, width, height) != RectangleOut)
  570. {
  571. /* Forces entire thumb to be painted. */
  572. sbw->scrollbar.topLoc = -(sbw->scrollbar.length + 1);
  573. PaintThumb (sbw);
  574. }
  575. /* we'd like to be region aware here!!!! */
  576. PaintArrows(sbw);
  577. }
  578. static Boolean
  579. CompareEvents(oldEvent, newEvent)
  580. XEvent *oldEvent, *newEvent;
  581. {
  582. #define Check(field) if (newEvent->field != oldEvent->field) return False;
  583. Check(xany.display);
  584. Check(xany.type);
  585. Check(xany.window);
  586. switch (newEvent->type)
  587. {
  588. case MotionNotify:
  589. Check(xmotion.state);
  590. break;
  591. case ButtonPress:
  592. case ButtonRelease:
  593. Check(xbutton.state);
  594. Check(xbutton.button);
  595. break;
  596. case KeyPress:
  597. case KeyRelease:
  598. Check(xkey.state);
  599. Check(xkey.keycode);
  600. break;
  601. case EnterNotify:
  602. case LeaveNotify:
  603. Check(xcrossing.mode);
  604. Check(xcrossing.detail);
  605. Check(xcrossing.state);
  606. break;
  607. }
  608. #undef Check
  609. return True;
  610. }
  611. struct EventData
  612. {
  613. XEvent *oldEvent;
  614. int count;
  615. };
  616. static Bool
  617. PeekNotifyEvent(dpy, event, args)
  618. Display *dpy;
  619. XEvent *event;
  620. char *args;
  621. {
  622. struct EventData *eventData = (struct EventData*)args;
  623. return ((++eventData->count == QLength(dpy)) /* since PeekIf blocks */
  624. || CompareEvents(event, eventData->oldEvent));
  625. }
  626. static Boolean
  627. LookAhead(w, event)
  628. Widget w;
  629. XEvent *event;
  630. {
  631. XEvent newEvent;
  632. struct EventData eventData;
  633. if (QLength (XtDisplay (w)) == 0)
  634. return False;
  635. eventData.count = 0;
  636. eventData.oldEvent = event;
  637. XPeekIfEvent (XtDisplay (w), &newEvent, PeekNotifyEvent, (char*)&eventData);
  638. return CompareEvents (event, &newEvent);
  639. }
  640. static void
  641. ExtractPosition(event, x, y, state)
  642. XEvent *event;
  643. Position *x, *y; /* RETURN */
  644. unsigned int *state; /* RETURN */
  645. {
  646. switch (event->type)
  647. {
  648. case MotionNotify:
  649. *x = event->xmotion.x;
  650. *y = event->xmotion.y;
  651. if (state != NULL)
  652. *state = event->xmotion.state;
  653. break;
  654. case ButtonPress:
  655. case ButtonRelease:
  656. *x = event->xbutton.x;
  657. *y = event->xbutton.y;
  658. if (state != NULL)
  659. *state = event->xbutton.state;
  660. break;
  661. case KeyPress:
  662. case KeyRelease:
  663. *x = event->xkey.x;
  664. *y = event->xkey.y;
  665. if (state != NULL)
  666. *state = event->xkey.state;
  667. break;
  668. case EnterNotify:
  669. case LeaveNotify:
  670. *x = event->xcrossing.x;
  671. *y = event->xcrossing.y;
  672. if (state != NULL)
  673. *state = event->xcrossing.state;
  674. break;
  675. default:
  676. *x = 0; *y = 0;
  677. if (state != NULL)
  678. *state = 0;
  679. }
  680. }
  681. static void
  682. HandleThumb(w, event, params, num_params)
  683. Widget w;
  684. XEvent *event;
  685. String *params;
  686. Cardinal *num_params;
  687. {
  688. Position x, y, loc;
  689. ScrollbarWidget sbw = (ScrollbarWidget) w;
  690. ExtractPosition(event, &x, &y, (unsigned int *)NULL);
  691. loc = PICKLENGTH(sbw, x, y);
  692. /* if the motion event puts the pointer in thumb, call Move and Notify */
  693. /* also call Move and Notify if we're already in continuous scroll mode */
  694. if (sbw->scrollbar.scroll_mode == SMODE_CONT ||
  695. (loc >= sbw->scrollbar.topLoc &&
  696. loc <= sbw->scrollbar.topLoc + (int)sbw->scrollbar.shownLength))
  697. {
  698. XtCallActionProc(w, "MoveThumb", event, params, *num_params);
  699. XtCallActionProc(w, "NotifyThumb", event, params, *num_params);
  700. }
  701. }
  702. static void
  703. RepeatNotify(client_data, idp)
  704. XtPointer client_data;
  705. XtIntervalId *idp UNUSED;
  706. {
  707. ScrollbarWidget sbw = (ScrollbarWidget) client_data;
  708. int call_data;
  709. char mode = sbw->scrollbar.scroll_mode;
  710. unsigned long rep;
  711. if (mode == SMODE_NONE || mode == SMODE_CONT)
  712. {
  713. sbw->scrollbar.timer_id = (XtIntervalId)0;
  714. return;
  715. }
  716. if (mode == SMODE_LINE_DOWN || mode == SMODE_LINE_UP)
  717. {
  718. call_data = ONE_LINE_DATA;
  719. rep = LINE_REPEAT;
  720. }
  721. else
  722. {
  723. call_data = ONE_PAGE_DATA;
  724. rep = PAGE_REPEAT;
  725. }
  726. if (mode == SMODE_PAGE_UP || mode == SMODE_LINE_UP)
  727. call_data = -call_data;
  728. XtCallCallbacks((Widget)sbw, XtNscrollProc, (XtPointer)(long_u)call_data);
  729. sbw->scrollbar.timer_id =
  730. XtAppAddTimeOut(XtWidgetToApplicationContext((Widget)sbw),
  731. rep,
  732. RepeatNotify,
  733. client_data);
  734. }
  735. /*
  736. * Same as above, but for floating numbers.
  737. */
  738. static float
  739. FloatInRange(num, small, big)
  740. float num, small, big;
  741. {
  742. return (num < small) ? small : ((num > big) ? big : num);
  743. }
  744. static void
  745. ScrollOneLineUp(w, event, params, num_params)
  746. Widget w;
  747. XEvent *event;
  748. String *params UNUSED;
  749. Cardinal *num_params UNUSED;
  750. {
  751. ScrollSome(w, event, -ONE_LINE_DATA);
  752. }
  753. static void
  754. ScrollOneLineDown(w, event, params, num_params)
  755. Widget w;
  756. XEvent *event;
  757. String *params UNUSED;
  758. Cardinal *num_params UNUSED;
  759. {
  760. ScrollSome(w, event, ONE_LINE_DATA);
  761. }
  762. static void
  763. ScrollPageDown(w, event, params, num_params)
  764. Widget w;
  765. XEvent *event;
  766. String *params UNUSED;
  767. Cardinal *num_params UNUSED;
  768. {
  769. ScrollSome(w, event, ONE_PAGE_DATA);
  770. }
  771. static void
  772. ScrollPageUp(w, event, params, num_params)
  773. Widget w;
  774. XEvent *event;
  775. String *params UNUSED;
  776. Cardinal *num_params UNUSED;
  777. {
  778. ScrollSome(w, event, -ONE_PAGE_DATA);
  779. }
  780. static void
  781. ScrollSome(w, event, call_data)
  782. Widget w;
  783. XEvent *event;
  784. int call_data;
  785. {
  786. ScrollbarWidget sbw = (ScrollbarWidget) w;
  787. if (sbw->scrollbar.scroll_mode == SMODE_CONT) /* if scroll continuous */
  788. return;
  789. if (LookAhead(w, event))
  790. return;
  791. sbw->scrollbar.scroll_mode = SMODE_LINE_UP;
  792. XtCallCallbacks(w, XtNscrollProc, (XtPointer)(long_u)call_data);
  793. }
  794. static void
  795. NotifyScroll(w, event, params, num_params)
  796. Widget w;
  797. XEvent *event;
  798. String *params UNUSED;
  799. Cardinal *num_params UNUSED;
  800. {
  801. ScrollbarWidget sbw = (ScrollbarWidget) w;
  802. Position x, y, loc;
  803. Dimension arrow_size;
  804. unsigned long delay = 0;
  805. int call_data = 0;
  806. unsigned int state;
  807. if (sbw->scrollbar.scroll_mode == SMODE_CONT) /* if scroll continuous */
  808. return;
  809. if (LookAhead (w, event))
  810. return;
  811. ExtractPosition(event, &x, &y, &state);
  812. loc = PICKLENGTH(sbw, x, y);
  813. if ((int)sbw->scrollbar.thickness * 2 > (int)sbw->scrollbar.length)
  814. arrow_size = sbw->scrollbar.length / 2;
  815. else
  816. arrow_size = sbw->scrollbar.thickness;
  817. /*
  818. * handle CTRL modifier
  819. */
  820. if (state & ControlMask)
  821. {
  822. if (loc > sbw->scrollbar.topLoc + (Position)sbw->scrollbar.shownLength)
  823. call_data = END_PAGE_DATA;
  824. else
  825. call_data = -END_PAGE_DATA;
  826. sbw->scrollbar.scroll_mode = SMODE_NONE;
  827. }
  828. /*
  829. * handle first arrow zone
  830. */
  831. else if (loc < (Position)arrow_size)
  832. {
  833. call_data = -ONE_LINE_DATA;
  834. sbw->scrollbar.scroll_mode = SMODE_LINE_UP;
  835. delay = LINE_DELAY;
  836. }
  837. /*
  838. * handle last arrow zone
  839. */
  840. else if (loc > (Position)(sbw->scrollbar.length - arrow_size))
  841. {
  842. call_data = ONE_LINE_DATA;
  843. sbw->scrollbar.scroll_mode = SMODE_LINE_DOWN;
  844. delay = LINE_DELAY;
  845. }
  846. /*
  847. * handle zone "above" the thumb
  848. */
  849. else if (loc < sbw->scrollbar.topLoc)
  850. {
  851. call_data = -ONE_PAGE_DATA;
  852. sbw->scrollbar.scroll_mode = SMODE_PAGE_UP;
  853. delay = PAGE_DELAY;
  854. }
  855. /*
  856. * handle zone "below" the thumb
  857. */
  858. else if (loc > sbw->scrollbar.topLoc + (Position)sbw->scrollbar.shownLength)
  859. {
  860. call_data = ONE_PAGE_DATA;
  861. sbw->scrollbar.scroll_mode = SMODE_PAGE_DOWN;
  862. delay = PAGE_DELAY;
  863. }
  864. if (call_data)
  865. XtCallCallbacks(w, XtNscrollProc, (XtPointer)(long_u)call_data);
  866. /* establish autoscroll */
  867. if (delay)
  868. sbw->scrollbar.timer_id =
  869. XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  870. delay, RepeatNotify, (XtPointer)w);
  871. }
  872. static void
  873. EndScroll(w, event, params, num_params)
  874. Widget w;
  875. XEvent *event UNUSED;
  876. String *params UNUSED;
  877. Cardinal *num_params UNUSED;
  878. {
  879. ScrollbarWidget sbw = (ScrollbarWidget) w;
  880. sbw->scrollbar.scroll_mode = SMODE_NONE;
  881. /* no need to remove any autoscroll timeout; it will no-op */
  882. /* because the scroll_mode is SMODE_NONE */
  883. /* but be sure to remove timeout in destroy proc */
  884. }
  885. static float
  886. FractionLoc(sbw, x, y)
  887. ScrollbarWidget sbw;
  888. int x, y;
  889. {
  890. int margin;
  891. float height, width;
  892. margin = MARGIN(sbw);
  893. x -= margin;
  894. y -= margin;
  895. height = (float)sbw->core.height - 2 * margin;
  896. width = (float)sbw->core.width - 2 * margin;
  897. return PICKLENGTH(sbw, x / width, y / height);
  898. }
  899. static void
  900. MoveThumb(w, event, params, num_params)
  901. Widget w;
  902. XEvent *event;
  903. String *params UNUSED;
  904. Cardinal *num_params UNUSED;
  905. {
  906. ScrollbarWidget sbw = (ScrollbarWidget)w;
  907. Position x, y;
  908. float top;
  909. char old_mode = sbw->scrollbar.scroll_mode;
  910. sbw->scrollbar.scroll_mode = SMODE_CONT; /* indicate continuous scroll */
  911. if (LookAhead(w, event))
  912. return;
  913. if (!event->xmotion.same_screen)
  914. return;
  915. ExtractPosition(event, &x, &y, (unsigned int *)NULL);
  916. top = FractionLoc(sbw, x, y);
  917. if (old_mode != SMODE_CONT) /* start dragging: set offset */
  918. {
  919. if (event->xbutton.button == Button2)
  920. sbw->scrollbar.scroll_off = sbw->scrollbar.shown / 2.;
  921. else
  922. sbw->scrollbar.scroll_off = top - sbw->scrollbar.top;
  923. }
  924. top -= sbw->scrollbar.scroll_off;
  925. if (sbw->scrollbar.limit_thumb)
  926. top = FloatInRange(top, 0.0,
  927. sbw->scrollbar.max - sbw->scrollbar.shown + 0.000001);
  928. else
  929. top = FloatInRange(top, 0.0, sbw->scrollbar.max);
  930. sbw->scrollbar.top = top;
  931. PaintThumb(sbw);
  932. XFlush(XtDisplay(w)); /* re-draw it before Notifying */
  933. }
  934. static void
  935. NotifyThumb(w, event, params, num_params)
  936. Widget w;
  937. XEvent *event;
  938. String *params UNUSED;
  939. Cardinal *num_params UNUSED;
  940. {
  941. ScrollbarWidget sbw = (ScrollbarWidget)w;
  942. /* Use a union to avoid a warning for the weird conversion from float to
  943. * XtPointer. Comes from Xaw/Scrollbar.c. */
  944. union {
  945. XtPointer xtp;
  946. float xtf;
  947. } xtpf;
  948. if (LookAhead(w, event))
  949. return;
  950. /* thumbProc is not pretty, but is necessary for backwards
  951. compatibility on those architectures for which it work{s,ed};
  952. the intent is to pass a (truncated) float by value. */
  953. xtpf.xtf = sbw->scrollbar.top;
  954. XtCallCallbacks(w, XtNthumbProc, xtpf.xtp);
  955. XtCallCallbacks(w, XtNjumpProc, (XtPointer)&sbw->scrollbar.top);
  956. }
  957. static void
  958. AllocTopShadowGC(w)
  959. Widget w;
  960. {
  961. ScrollbarWidget sbw = (ScrollbarWidget) w;
  962. XtGCMask valuemask;
  963. XGCValues myXGCV;
  964. valuemask = GCForeground;
  965. myXGCV.foreground = sbw->scrollbar.top_shadow_pixel;
  966. sbw->scrollbar.top_shadow_GC = XtGetGC(w, valuemask, &myXGCV);
  967. }
  968. static void
  969. AllocBotShadowGC(w)
  970. Widget w;
  971. {
  972. ScrollbarWidget sbw = (ScrollbarWidget) w;
  973. XtGCMask valuemask;
  974. XGCValues myXGCV;
  975. valuemask = GCForeground;
  976. myXGCV.foreground = sbw->scrollbar.bot_shadow_pixel;
  977. sbw->scrollbar.bot_shadow_GC = XtGetGC(w, valuemask, &myXGCV);
  978. }
  979. static void
  980. _Xaw3dDrawShadows(gw, event, region, out)
  981. Widget gw;
  982. XEvent *event UNUSED;
  983. Region region;
  984. int out;
  985. {
  986. XPoint pt[6];
  987. ScrollbarWidget sbw = (ScrollbarWidget) gw;
  988. Dimension s = sbw->scrollbar.shadow_width;
  989. /*
  990. * draw the shadows using the core part width and height,
  991. * and the scrollbar part shadow_width.
  992. *
  993. * no point to do anything if the shadow_width is 0 or the
  994. * widget has not been realized.
  995. */
  996. if (s > 0 && XtIsRealized(gw))
  997. {
  998. Dimension h = sbw->core.height;
  999. Dimension w = sbw->core.width;
  1000. Dimension wms = w - s;
  1001. Dimension hms = h - s;
  1002. Display *dpy = XtDisplay (gw);
  1003. Window win = XtWindow (gw);
  1004. GC top, bot;
  1005. if (out)
  1006. {
  1007. top = sbw->scrollbar.top_shadow_GC;
  1008. bot = sbw->scrollbar.bot_shadow_GC;
  1009. }
  1010. else
  1011. {
  1012. top = sbw->scrollbar.bot_shadow_GC;
  1013. bot = sbw->scrollbar.top_shadow_GC;
  1014. }
  1015. /* top-left shadow */
  1016. if ((region == NULL) ||
  1017. (XRectInRegion (region, 0, 0, w, s) != RectangleOut) ||
  1018. (XRectInRegion (region, 0, 0, s, h) != RectangleOut))
  1019. {
  1020. pt[0].x = 0; pt[0].y = h;
  1021. pt[1].x = pt[1].y = 0;
  1022. pt[2].x = w; pt[2].y = 0;
  1023. pt[3].x = wms; pt[3].y = s;
  1024. pt[4].x = pt[4].y = s;
  1025. pt[5].x = s; pt[5].y = hms;
  1026. XFillPolygon (dpy, win, top, pt, 6, Complex, CoordModeOrigin);
  1027. }
  1028. /* bottom-right shadow */
  1029. if ((region == NULL) ||
  1030. (XRectInRegion (region, 0, hms, w, s) != RectangleOut) ||
  1031. (XRectInRegion (region, wms, 0, s, h) != RectangleOut))
  1032. {
  1033. pt[0].x = 0; pt[0].y = h;
  1034. pt[1].x = w; pt[1].y = h;
  1035. pt[2].x = w; pt[2].y = 0;
  1036. pt[3].x = wms; pt[3].y = s;
  1037. pt[4].x = wms; pt[4].y = hms;
  1038. pt[5].x = s; pt[5].y = hms;
  1039. XFillPolygon (dpy, win, bot, pt, 6, Complex, CoordModeOrigin);
  1040. }
  1041. }
  1042. }
  1043. /*
  1044. * Set the scroll bar to the given location.
  1045. */
  1046. void
  1047. vim_XawScrollbarSetThumb(w, top, shown, max)
  1048. Widget w;
  1049. double top, shown, max;
  1050. {
  1051. ScrollbarWidget sbw = (ScrollbarWidget) w;
  1052. if (sbw->scrollbar.scroll_mode == SMODE_CONT) /* if still thumbing */
  1053. return;
  1054. sbw->scrollbar.max = (max > 1.0) ? 1.0 :
  1055. (max >= 0.0) ? max : sbw->scrollbar.max;
  1056. sbw->scrollbar.top = (top > sbw->scrollbar.max) ? sbw->scrollbar.max :
  1057. (top >= 0.0) ? top : sbw->scrollbar.top;
  1058. sbw->scrollbar.shown = (shown > 1.0) ? 1.0 :
  1059. (shown >= 0.0) ? shown : sbw->scrollbar.shown;
  1060. PaintThumb(sbw);
  1061. }