PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/gui_at_sb.c

https://gitlab.com/Haritiana/vim
C | 1187 lines | 920 code | 138 blank | 129 comment | 133 complexity | 552c056f2dacf89a276d35de61f37b48 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(void);
  107. static void Initialize(Widget, Widget, ArgList, Cardinal *);
  108. static void Destroy(Widget);
  109. static void Realize(Widget, Mask *, XSetWindowAttributes *);
  110. static void Resize(Widget);
  111. static void Redisplay(Widget, XEvent *, Region);
  112. static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);
  113. static void HandleThumb(Widget, XEvent *, String *, Cardinal *);
  114. static void MoveThumb(Widget, XEvent *, String *, Cardinal *);
  115. static void NotifyThumb(Widget, XEvent *, String *, Cardinal *);
  116. static void NotifyScroll(Widget, XEvent *, String *, Cardinal *);
  117. static void EndScroll(Widget, XEvent *, String *, Cardinal *);
  118. static void ScrollOneLineUp(Widget, XEvent *, String *, Cardinal *);
  119. static void ScrollOneLineDown(Widget, XEvent *, String *, Cardinal *);
  120. static void ScrollPageUp(Widget, XEvent *, String *, Cardinal *);
  121. static void ScrollPageDown(Widget, XEvent *, String *, Cardinal *);
  122. static void ScrollSome(Widget w, XEvent *event, int call_data);
  123. static void _Xaw3dDrawShadows(Widget, XEvent *, Region, int);
  124. static void AllocTopShadowGC(Widget);
  125. static void AllocBotShadowGC(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(void)
  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(
  204. ScrollbarWidget sbw,
  205. Position top,
  206. Position bottom,
  207. int fill,
  208. int draw_shadow)
  209. {
  210. int tlen = bottom - top; /* length of thumb in pixels */
  211. int sw, margin, floor;
  212. int lx, ly, lw, lh;
  213. if (bottom <= 0 || bottom <= top)
  214. return;
  215. sw = sbw->scrollbar.shadow_width;
  216. if (sw < 0)
  217. sw = 0;
  218. margin = MARGIN (sbw);
  219. floor = sbw->scrollbar.length - margin + 2;
  220. if (sbw->scrollbar.orientation == XtorientHorizontal)
  221. {
  222. lx = ((top < margin) ? margin : top);
  223. ly = sw;
  224. lw = (((top + tlen) > floor) ? floor - top : tlen);
  225. lh = sbw->core.height - 2 * sw;
  226. }
  227. else
  228. {
  229. lx = sw;
  230. ly = ((top < margin) ? margin : top);
  231. lw = sbw->core.width - 2 * sw;
  232. lh = (((top + tlen) > floor) ? floor - top : tlen);
  233. }
  234. if (lh <= 0 || lw <= 0)
  235. return;
  236. if (draw_shadow)
  237. {
  238. if (!(sbw->scrollbar.orientation == XtorientHorizontal))
  239. {
  240. /* Top border */
  241. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  242. sbw->scrollbar.top_shadow_GC,
  243. lx, ly, lx + lw - 1, ly);
  244. /* Bottom border */
  245. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  246. sbw->scrollbar.bot_shadow_GC,
  247. lx, ly + lh - 1, lx + lw - 1, ly + lh - 1);
  248. }
  249. else
  250. {
  251. /* Left border */
  252. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  253. sbw->scrollbar.top_shadow_GC,
  254. lx, ly, lx, ly + lh - 1);
  255. /* Right border */
  256. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  257. sbw->scrollbar.bot_shadow_GC,
  258. lx + lw - 1, ly, lx + lw - 1, ly + lh - 1);
  259. }
  260. return;
  261. }
  262. if (fill)
  263. {
  264. XFillRectangle(XtDisplay((Widget) sbw), XtWindow((Widget) sbw),
  265. sbw->scrollbar.gc,
  266. lx, ly, (unsigned int) lw, (unsigned int) lh);
  267. if (!(sbw->scrollbar.orientation == XtorientHorizontal))
  268. {
  269. /* Left border */
  270. XDrawLine(XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  271. sbw->scrollbar.top_shadow_GC,
  272. lx, ly, lx, ly + lh - 1);
  273. /* Right border */
  274. XDrawLine(XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  275. sbw->scrollbar.bot_shadow_GC,
  276. lx + lw - 1, ly, lx + lw - 1, ly + lh - 1);
  277. }
  278. else
  279. {
  280. /* Top border */
  281. XDrawLine(XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  282. sbw->scrollbar.top_shadow_GC,
  283. lx, ly, lx + lw - 1, ly);
  284. /* Bottom border */
  285. XDrawLine(XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  286. sbw->scrollbar.bot_shadow_GC,
  287. lx, ly + lh - 1, lx + lw - 1, ly + lh - 1);
  288. }
  289. }
  290. else
  291. {
  292. XClearArea(XtDisplay((Widget) sbw), XtWindow((Widget) sbw),
  293. lx, ly, (unsigned int) lw, (unsigned int) lh, FALSE);
  294. }
  295. }
  296. /* Paint the thumb in the area specified by sbw->top and
  297. sbw->shown. The old area is erased. The painting and
  298. erasing is done cleverly so that no flickering will occur.
  299. */
  300. static void
  301. PaintThumb(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(ScrollbarWidget sbw)
  331. {
  332. XPoint point[6];
  333. Dimension thickness = sbw->scrollbar.thickness - 1;
  334. Dimension size;
  335. Dimension off;
  336. if (XtIsRealized((Widget) sbw))
  337. {
  338. if ((int)thickness * 2 > (int)sbw->scrollbar.length)
  339. {
  340. size = sbw->scrollbar.length / 2;
  341. off = (int)(thickness - size) / 2;
  342. }
  343. else
  344. {
  345. size = thickness;
  346. off = 0;
  347. }
  348. point[0].x = off + sbw->scrollbar.shadow_width;
  349. point[0].y = size;
  350. point[1].x = thickness - off - sbw->scrollbar.shadow_width;
  351. point[1].y = size;
  352. point[2].x = thickness / 2;
  353. point[2].y = sbw->scrollbar.shadow_width;
  354. point[3].x = off + sbw->scrollbar.shadow_width;
  355. point[3].y = sbw->scrollbar.length - size;
  356. point[4].x = thickness - off - sbw->scrollbar.shadow_width;
  357. point[4].y = sbw->scrollbar.length - size;
  358. point[5].x = thickness / 2;
  359. point[5].y = sbw->scrollbar.length - sbw->scrollbar.shadow_width - 1;
  360. /* horizontal arrows require that x and y coordinates be swapped */
  361. if (sbw->scrollbar.orientation == XtorientHorizontal)
  362. {
  363. int n;
  364. int swap;
  365. for (n = 0; n < 6; n++)
  366. {
  367. swap = point[n].x;
  368. point[n].x = point[n].y;
  369. point[n].y = swap;
  370. }
  371. }
  372. /* draw the up/left arrow */
  373. XFillPolygon (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  374. sbw->scrollbar.gc,
  375. point, 3,
  376. Convex, CoordModeOrigin);
  377. XDrawLines (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  378. sbw->scrollbar.bot_shadow_GC,
  379. point, 3,
  380. CoordModeOrigin);
  381. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  382. sbw->scrollbar.top_shadow_GC,
  383. point[0].x, point[0].y,
  384. point[2].x, point[2].y);
  385. /* draw the down/right arrow */
  386. XFillPolygon (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  387. sbw->scrollbar.gc,
  388. point+3, 3,
  389. Convex, CoordModeOrigin);
  390. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  391. sbw->scrollbar.top_shadow_GC,
  392. point[3].x, point[3].y,
  393. point[4].x, point[4].y);
  394. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  395. sbw->scrollbar.top_shadow_GC,
  396. point[3].x, point[3].y,
  397. point[5].x, point[5].y);
  398. XDrawLine (XtDisplay ((Widget) sbw), XtWindow ((Widget) sbw),
  399. sbw->scrollbar.bot_shadow_GC,
  400. point[4].x, point[4].y,
  401. point[5].x, point[5].y);
  402. }
  403. }
  404. static void
  405. Destroy(Widget w)
  406. {
  407. ScrollbarWidget sbw = (ScrollbarWidget) w;
  408. if (sbw->scrollbar.timer_id != (XtIntervalId) 0)
  409. XtRemoveTimeOut (sbw->scrollbar.timer_id);
  410. XtReleaseGC(w, sbw->scrollbar.gc);
  411. XtReleaseGC(w, sbw->scrollbar.top_shadow_GC);
  412. XtReleaseGC(w, sbw->scrollbar.bot_shadow_GC);
  413. }
  414. static void
  415. CreateGC(Widget w)
  416. {
  417. ScrollbarWidget sbw = (ScrollbarWidget) w;
  418. XGCValues gcValues;
  419. XtGCMask mask;
  420. unsigned int depth = 1;
  421. if (sbw->scrollbar.thumb == XtUnspecifiedPixmap)
  422. {
  423. sbw->scrollbar.thumb = XmuCreateStippledPixmap (XtScreen(w),
  424. (Pixel) 1, (Pixel) 0, depth);
  425. }
  426. else if (sbw->scrollbar.thumb != None)
  427. {
  428. Window root;
  429. int x, y;
  430. unsigned int width, height, bw;
  431. if (XGetGeometry (XtDisplay(w), sbw->scrollbar.thumb, &root, &x, &y,
  432. &width, &height, &bw, &depth) == 0)
  433. EMSG(_("Scrollbar Widget: Could not get geometry of thumb pixmap."));
  434. }
  435. gcValues.foreground = sbw->scrollbar.foreground;
  436. gcValues.background = sbw->core.background_pixel;
  437. mask = GCForeground | GCBackground;
  438. if (sbw->scrollbar.thumb != None)
  439. {
  440. gcValues.fill_style = FillSolid;
  441. mask |= GCFillStyle;
  442. }
  443. /* the creation should be non-caching, because */
  444. /* we now set and clear clip masks on the gc returned */
  445. sbw->scrollbar.gc = XtGetGC (w, mask, &gcValues);
  446. }
  447. static void
  448. SetDimensions(ScrollbarWidget sbw)
  449. {
  450. if (sbw->scrollbar.orientation == XtorientVertical)
  451. {
  452. sbw->scrollbar.length = sbw->core.height;
  453. sbw->scrollbar.thickness = sbw->core.width;
  454. }
  455. else
  456. {
  457. sbw->scrollbar.length = sbw->core.width;
  458. sbw->scrollbar.thickness = sbw->core.height;
  459. }
  460. }
  461. static void
  462. Initialize(
  463. Widget request UNUSED, /* what the client asked for */
  464. Widget new, /* what we're going to give him */
  465. ArgList args UNUSED,
  466. Cardinal *num_args UNUSED)
  467. {
  468. ScrollbarWidget sbw = (ScrollbarWidget) new;
  469. CreateGC(new);
  470. AllocTopShadowGC(new);
  471. AllocBotShadowGC(new);
  472. if (sbw->core.width == 0)
  473. sbw->core.width = (sbw->scrollbar.orientation == XtorientVertical)
  474. ? sbw->scrollbar.thickness : sbw->scrollbar.length;
  475. if (sbw->core.height == 0)
  476. sbw->core.height = (sbw->scrollbar.orientation == XtorientHorizontal)
  477. ? sbw->scrollbar.thickness : sbw->scrollbar.length;
  478. SetDimensions(sbw);
  479. sbw->scrollbar.scroll_mode = SMODE_NONE;
  480. sbw->scrollbar.timer_id = (XtIntervalId)0;
  481. sbw->scrollbar.topLoc = 0;
  482. sbw->scrollbar.shownLength = sbw->scrollbar.min_thumb;
  483. }
  484. static void
  485. Realize(
  486. Widget w,
  487. Mask *valueMask,
  488. XSetWindowAttributes *attributes)
  489. {
  490. /* The Simple widget actually stuffs the value in the valuemask. */
  491. (*vim_scrollbarWidgetClass->core_class.superclass->core_class.realize)
  492. (w, valueMask, attributes);
  493. }
  494. static Boolean
  495. SetValues(
  496. Widget current, /* what I am */
  497. Widget request UNUSED, /* what he wants me to be */
  498. Widget desired, /* what I will become */
  499. ArgList args UNUSED,
  500. Cardinal *num_args UNUSED)
  501. {
  502. ScrollbarWidget sbw = (ScrollbarWidget) current;
  503. ScrollbarWidget dsbw = (ScrollbarWidget) desired;
  504. Boolean redraw = FALSE;
  505. /*
  506. * If these values are outside the acceptable range ignore them...
  507. */
  508. if (dsbw->scrollbar.top < 0.0 || dsbw->scrollbar.top > 1.0)
  509. dsbw->scrollbar.top = sbw->scrollbar.top;
  510. if (dsbw->scrollbar.shown < 0.0 || dsbw->scrollbar.shown > 1.0)
  511. dsbw->scrollbar.shown = sbw->scrollbar.shown;
  512. /*
  513. * Change colors and stuff...
  514. */
  515. if (XtIsRealized(desired))
  516. {
  517. if (sbw->scrollbar.foreground != dsbw->scrollbar.foreground ||
  518. sbw->core.background_pixel != dsbw->core.background_pixel ||
  519. sbw->scrollbar.thumb != dsbw->scrollbar.thumb)
  520. {
  521. XtReleaseGC(desired, sbw->scrollbar.gc);
  522. CreateGC (desired);
  523. redraw = TRUE;
  524. }
  525. if (sbw->scrollbar.top != dsbw->scrollbar.top ||
  526. sbw->scrollbar.shown != dsbw->scrollbar.shown)
  527. redraw = TRUE;
  528. }
  529. return redraw;
  530. }
  531. static void
  532. Resize(Widget w)
  533. {
  534. /* ForgetGravity has taken care of background, but thumb may
  535. * have to move as a result of the new size. */
  536. SetDimensions ((ScrollbarWidget) w);
  537. Redisplay(w, (XEvent*) NULL, (Region)NULL);
  538. }
  539. static void
  540. Redisplay(Widget w, XEvent *event, Region region)
  541. {
  542. ScrollbarWidget sbw = (ScrollbarWidget) w;
  543. int x, y;
  544. unsigned int width, height;
  545. _Xaw3dDrawShadows(w, event, region, FALSE);
  546. if (sbw->scrollbar.orientation == XtorientHorizontal)
  547. {
  548. x = sbw->scrollbar.topLoc;
  549. y = 1;
  550. width = sbw->scrollbar.shownLength;
  551. height = sbw->core.height - 2;
  552. }
  553. else
  554. {
  555. x = 1;
  556. y = sbw->scrollbar.topLoc;
  557. width = sbw->core.width - 2;
  558. height = sbw->scrollbar.shownLength;
  559. }
  560. if (region == NULL ||
  561. XRectInRegion (region, x, y, width, height) != RectangleOut)
  562. {
  563. /* Forces entire thumb to be painted. */
  564. sbw->scrollbar.topLoc = -(sbw->scrollbar.length + 1);
  565. PaintThumb (sbw);
  566. }
  567. /* we'd like to be region aware here!!!! */
  568. PaintArrows(sbw);
  569. }
  570. static Boolean
  571. CompareEvents(XEvent *oldEvent, XEvent *newEvent)
  572. {
  573. #define Check(field) if (newEvent->field != oldEvent->field) return False;
  574. Check(xany.display);
  575. Check(xany.type);
  576. Check(xany.window);
  577. switch (newEvent->type)
  578. {
  579. case MotionNotify:
  580. Check(xmotion.state);
  581. break;
  582. case ButtonPress:
  583. case ButtonRelease:
  584. Check(xbutton.state);
  585. Check(xbutton.button);
  586. break;
  587. case KeyPress:
  588. case KeyRelease:
  589. Check(xkey.state);
  590. Check(xkey.keycode);
  591. break;
  592. case EnterNotify:
  593. case LeaveNotify:
  594. Check(xcrossing.mode);
  595. Check(xcrossing.detail);
  596. Check(xcrossing.state);
  597. break;
  598. }
  599. #undef Check
  600. return True;
  601. }
  602. struct EventData
  603. {
  604. XEvent *oldEvent;
  605. int count;
  606. };
  607. static Bool
  608. PeekNotifyEvent(Display *dpy, XEvent *event, char *args)
  609. {
  610. struct EventData *eventData = (struct EventData*)args;
  611. return ((++eventData->count == QLength(dpy)) /* since PeekIf blocks */
  612. || CompareEvents(event, eventData->oldEvent));
  613. }
  614. static Boolean
  615. LookAhead(Widget w, XEvent *event)
  616. {
  617. XEvent newEvent;
  618. struct EventData eventData;
  619. if (QLength (XtDisplay (w)) == 0)
  620. return False;
  621. eventData.count = 0;
  622. eventData.oldEvent = event;
  623. XPeekIfEvent (XtDisplay (w), &newEvent, PeekNotifyEvent, (char*)&eventData);
  624. return CompareEvents (event, &newEvent);
  625. }
  626. static void
  627. ExtractPosition(
  628. XEvent *event,
  629. Position *x, /* RETURN */
  630. Position *y, /* RETURN */
  631. unsigned int *state) /* RETURN */
  632. {
  633. switch (event->type)
  634. {
  635. case MotionNotify:
  636. *x = event->xmotion.x;
  637. *y = event->xmotion.y;
  638. if (state != NULL)
  639. *state = event->xmotion.state;
  640. break;
  641. case ButtonPress:
  642. case ButtonRelease:
  643. *x = event->xbutton.x;
  644. *y = event->xbutton.y;
  645. if (state != NULL)
  646. *state = event->xbutton.state;
  647. break;
  648. case KeyPress:
  649. case KeyRelease:
  650. *x = event->xkey.x;
  651. *y = event->xkey.y;
  652. if (state != NULL)
  653. *state = event->xkey.state;
  654. break;
  655. case EnterNotify:
  656. case LeaveNotify:
  657. *x = event->xcrossing.x;
  658. *y = event->xcrossing.y;
  659. if (state != NULL)
  660. *state = event->xcrossing.state;
  661. break;
  662. default:
  663. *x = 0; *y = 0;
  664. if (state != NULL)
  665. *state = 0;
  666. }
  667. }
  668. static void
  669. HandleThumb(
  670. Widget w,
  671. XEvent *event,
  672. String *params,
  673. Cardinal *num_params)
  674. {
  675. Position x, y, loc;
  676. ScrollbarWidget sbw = (ScrollbarWidget) w;
  677. ExtractPosition(event, &x, &y, (unsigned int *)NULL);
  678. loc = PICKLENGTH(sbw, x, y);
  679. /* if the motion event puts the pointer in thumb, call Move and Notify */
  680. /* also call Move and Notify if we're already in continuous scroll mode */
  681. if (sbw->scrollbar.scroll_mode == SMODE_CONT ||
  682. (loc >= sbw->scrollbar.topLoc &&
  683. loc <= sbw->scrollbar.topLoc + (int)sbw->scrollbar.shownLength))
  684. {
  685. XtCallActionProc(w, "MoveThumb", event, params, *num_params);
  686. XtCallActionProc(w, "NotifyThumb", event, params, *num_params);
  687. }
  688. }
  689. static void
  690. RepeatNotify(XtPointer client_data, XtIntervalId *idp UNUSED)
  691. {
  692. ScrollbarWidget sbw = (ScrollbarWidget) client_data;
  693. int call_data;
  694. char mode = sbw->scrollbar.scroll_mode;
  695. unsigned long rep;
  696. if (mode == SMODE_NONE || mode == SMODE_CONT)
  697. {
  698. sbw->scrollbar.timer_id = (XtIntervalId)0;
  699. return;
  700. }
  701. if (mode == SMODE_LINE_DOWN || mode == SMODE_LINE_UP)
  702. {
  703. call_data = ONE_LINE_DATA;
  704. rep = LINE_REPEAT;
  705. }
  706. else
  707. {
  708. call_data = ONE_PAGE_DATA;
  709. rep = PAGE_REPEAT;
  710. }
  711. if (mode == SMODE_PAGE_UP || mode == SMODE_LINE_UP)
  712. call_data = -call_data;
  713. XtCallCallbacks((Widget)sbw, XtNscrollProc, (XtPointer)(long_u)call_data);
  714. sbw->scrollbar.timer_id =
  715. XtAppAddTimeOut(XtWidgetToApplicationContext((Widget)sbw),
  716. rep,
  717. RepeatNotify,
  718. client_data);
  719. }
  720. /*
  721. * Same as above, but for floating numbers.
  722. */
  723. static float
  724. FloatInRange(float num, float small, float big)
  725. {
  726. return (num < small) ? small : ((num > big) ? big : num);
  727. }
  728. static void
  729. ScrollOneLineUp(
  730. Widget w,
  731. XEvent *event,
  732. String *params UNUSED,
  733. Cardinal *num_params UNUSED)
  734. {
  735. ScrollSome(w, event, -ONE_LINE_DATA);
  736. }
  737. static void
  738. ScrollOneLineDown(
  739. Widget w,
  740. XEvent *event,
  741. String *params UNUSED,
  742. Cardinal *num_params UNUSED)
  743. {
  744. ScrollSome(w, event, ONE_LINE_DATA);
  745. }
  746. static void
  747. ScrollPageDown(
  748. Widget w,
  749. XEvent *event,
  750. String *params UNUSED,
  751. Cardinal *num_params UNUSED)
  752. {
  753. ScrollSome(w, event, ONE_PAGE_DATA);
  754. }
  755. static void
  756. ScrollPageUp(
  757. Widget w,
  758. XEvent *event,
  759. String *params UNUSED,
  760. Cardinal *num_params UNUSED)
  761. {
  762. ScrollSome(w, event, -ONE_PAGE_DATA);
  763. }
  764. static void
  765. ScrollSome(
  766. Widget w,
  767. XEvent *event,
  768. int call_data)
  769. {
  770. ScrollbarWidget sbw = (ScrollbarWidget) w;
  771. if (sbw->scrollbar.scroll_mode == SMODE_CONT) /* if scroll continuous */
  772. return;
  773. if (LookAhead(w, event))
  774. return;
  775. sbw->scrollbar.scroll_mode = SMODE_LINE_UP;
  776. XtCallCallbacks(w, XtNscrollProc, (XtPointer)(long_u)call_data);
  777. }
  778. static void
  779. NotifyScroll(
  780. Widget w,
  781. XEvent *event,
  782. String *params UNUSED,
  783. Cardinal *num_params UNUSED)
  784. {
  785. ScrollbarWidget sbw = (ScrollbarWidget) w;
  786. Position x, y, loc;
  787. Dimension arrow_size;
  788. unsigned long delay = 0;
  789. int call_data = 0;
  790. unsigned int state;
  791. if (sbw->scrollbar.scroll_mode == SMODE_CONT) /* if scroll continuous */
  792. return;
  793. if (LookAhead (w, event))
  794. return;
  795. ExtractPosition(event, &x, &y, &state);
  796. loc = PICKLENGTH(sbw, x, y);
  797. if ((int)sbw->scrollbar.thickness * 2 > (int)sbw->scrollbar.length)
  798. arrow_size = sbw->scrollbar.length / 2;
  799. else
  800. arrow_size = sbw->scrollbar.thickness;
  801. /*
  802. * handle CTRL modifier
  803. */
  804. if (state & ControlMask)
  805. {
  806. if (loc > sbw->scrollbar.topLoc + (Position)sbw->scrollbar.shownLength)
  807. call_data = END_PAGE_DATA;
  808. else
  809. call_data = -END_PAGE_DATA;
  810. sbw->scrollbar.scroll_mode = SMODE_NONE;
  811. }
  812. /*
  813. * handle first arrow zone
  814. */
  815. else if (loc < (Position)arrow_size)
  816. {
  817. call_data = -ONE_LINE_DATA;
  818. sbw->scrollbar.scroll_mode = SMODE_LINE_UP;
  819. delay = LINE_DELAY;
  820. }
  821. /*
  822. * handle last arrow zone
  823. */
  824. else if (loc > (Position)(sbw->scrollbar.length - arrow_size))
  825. {
  826. call_data = ONE_LINE_DATA;
  827. sbw->scrollbar.scroll_mode = SMODE_LINE_DOWN;
  828. delay = LINE_DELAY;
  829. }
  830. /*
  831. * handle zone "above" the thumb
  832. */
  833. else if (loc < sbw->scrollbar.topLoc)
  834. {
  835. call_data = -ONE_PAGE_DATA;
  836. sbw->scrollbar.scroll_mode = SMODE_PAGE_UP;
  837. delay = PAGE_DELAY;
  838. }
  839. /*
  840. * handle zone "below" the thumb
  841. */
  842. else if (loc > sbw->scrollbar.topLoc + (Position)sbw->scrollbar.shownLength)
  843. {
  844. call_data = ONE_PAGE_DATA;
  845. sbw->scrollbar.scroll_mode = SMODE_PAGE_DOWN;
  846. delay = PAGE_DELAY;
  847. }
  848. if (call_data)
  849. XtCallCallbacks(w, XtNscrollProc, (XtPointer)(long_u)call_data);
  850. /* establish autoscroll */
  851. if (delay)
  852. sbw->scrollbar.timer_id =
  853. XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  854. delay, RepeatNotify, (XtPointer)w);
  855. }
  856. static void
  857. EndScroll(
  858. Widget w,
  859. XEvent *event UNUSED,
  860. String *params UNUSED,
  861. Cardinal *num_params UNUSED)
  862. {
  863. ScrollbarWidget sbw = (ScrollbarWidget) w;
  864. sbw->scrollbar.scroll_mode = SMODE_NONE;
  865. /* no need to remove any autoscroll timeout; it will no-op */
  866. /* because the scroll_mode is SMODE_NONE */
  867. /* but be sure to remove timeout in destroy proc */
  868. }
  869. static float
  870. FractionLoc(ScrollbarWidget sbw, int x, int y)
  871. {
  872. int margin;
  873. float height, width;
  874. margin = MARGIN(sbw);
  875. x -= margin;
  876. y -= margin;
  877. height = (float)sbw->core.height - 2 * margin;
  878. width = (float)sbw->core.width - 2 * margin;
  879. return PICKLENGTH(sbw, x / width, y / height);
  880. }
  881. static void
  882. MoveThumb(
  883. Widget w,
  884. XEvent *event,
  885. String *params UNUSED,
  886. Cardinal *num_params UNUSED)
  887. {
  888. ScrollbarWidget sbw = (ScrollbarWidget)w;
  889. Position x, y;
  890. float top;
  891. char old_mode = sbw->scrollbar.scroll_mode;
  892. sbw->scrollbar.scroll_mode = SMODE_CONT; /* indicate continuous scroll */
  893. if (LookAhead(w, event))
  894. return;
  895. if (!event->xmotion.same_screen)
  896. return;
  897. ExtractPosition(event, &x, &y, (unsigned int *)NULL);
  898. top = FractionLoc(sbw, x, y);
  899. if (old_mode != SMODE_CONT) /* start dragging: set offset */
  900. {
  901. if (event->xbutton.button == Button2)
  902. sbw->scrollbar.scroll_off = sbw->scrollbar.shown / 2.;
  903. else
  904. sbw->scrollbar.scroll_off = top - sbw->scrollbar.top;
  905. }
  906. top -= sbw->scrollbar.scroll_off;
  907. if (sbw->scrollbar.limit_thumb)
  908. top = FloatInRange(top, 0.0,
  909. sbw->scrollbar.max - sbw->scrollbar.shown + 0.000001);
  910. else
  911. top = FloatInRange(top, 0.0, sbw->scrollbar.max);
  912. sbw->scrollbar.top = top;
  913. PaintThumb(sbw);
  914. XFlush(XtDisplay(w)); /* re-draw it before Notifying */
  915. }
  916. static void
  917. NotifyThumb(
  918. Widget w,
  919. XEvent *event,
  920. String *params UNUSED,
  921. Cardinal *num_params UNUSED)
  922. {
  923. ScrollbarWidget sbw = (ScrollbarWidget)w;
  924. /* Use a union to avoid a warning for the weird conversion from float to
  925. * XtPointer. Comes from Xaw/Scrollbar.c. */
  926. union {
  927. XtPointer xtp;
  928. float xtf;
  929. } xtpf;
  930. if (LookAhead(w, event))
  931. return;
  932. /* thumbProc is not pretty, but is necessary for backwards
  933. compatibility on those architectures for which it work{s,ed};
  934. the intent is to pass a (truncated) float by value. */
  935. xtpf.xtf = sbw->scrollbar.top;
  936. XtCallCallbacks(w, XtNthumbProc, xtpf.xtp);
  937. XtCallCallbacks(w, XtNjumpProc, (XtPointer)&sbw->scrollbar.top);
  938. }
  939. static void
  940. AllocTopShadowGC(Widget w)
  941. {
  942. ScrollbarWidget sbw = (ScrollbarWidget) w;
  943. XtGCMask valuemask;
  944. XGCValues myXGCV;
  945. valuemask = GCForeground;
  946. myXGCV.foreground = sbw->scrollbar.top_shadow_pixel;
  947. sbw->scrollbar.top_shadow_GC = XtGetGC(w, valuemask, &myXGCV);
  948. }
  949. static void
  950. AllocBotShadowGC(Widget w)
  951. {
  952. ScrollbarWidget sbw = (ScrollbarWidget) w;
  953. XtGCMask valuemask;
  954. XGCValues myXGCV;
  955. valuemask = GCForeground;
  956. myXGCV.foreground = sbw->scrollbar.bot_shadow_pixel;
  957. sbw->scrollbar.bot_shadow_GC = XtGetGC(w, valuemask, &myXGCV);
  958. }
  959. static void
  960. _Xaw3dDrawShadows(
  961. Widget gw,
  962. XEvent *event UNUSED,
  963. Region region,
  964. int out)
  965. {
  966. XPoint pt[6];
  967. ScrollbarWidget sbw = (ScrollbarWidget) gw;
  968. Dimension s = sbw->scrollbar.shadow_width;
  969. /*
  970. * draw the shadows using the core part width and height,
  971. * and the scrollbar part shadow_width.
  972. *
  973. * no point to do anything if the shadow_width is 0 or the
  974. * widget has not been realized.
  975. */
  976. if (s > 0 && XtIsRealized(gw))
  977. {
  978. Dimension h = sbw->core.height;
  979. Dimension w = sbw->core.width;
  980. Dimension wms = w - s;
  981. Dimension hms = h - s;
  982. Display *dpy = XtDisplay (gw);
  983. Window win = XtWindow (gw);
  984. GC top, bot;
  985. if (out)
  986. {
  987. top = sbw->scrollbar.top_shadow_GC;
  988. bot = sbw->scrollbar.bot_shadow_GC;
  989. }
  990. else
  991. {
  992. top = sbw->scrollbar.bot_shadow_GC;
  993. bot = sbw->scrollbar.top_shadow_GC;
  994. }
  995. /* top-left shadow */
  996. if ((region == NULL) ||
  997. (XRectInRegion (region, 0, 0, w, s) != RectangleOut) ||
  998. (XRectInRegion (region, 0, 0, s, h) != RectangleOut))
  999. {
  1000. pt[0].x = 0; pt[0].y = h;
  1001. pt[1].x = pt[1].y = 0;
  1002. pt[2].x = w; pt[2].y = 0;
  1003. pt[3].x = wms; pt[3].y = s;
  1004. pt[4].x = pt[4].y = s;
  1005. pt[5].x = s; pt[5].y = hms;
  1006. XFillPolygon (dpy, win, top, pt, 6, Complex, CoordModeOrigin);
  1007. }
  1008. /* bottom-right shadow */
  1009. if ((region == NULL) ||
  1010. (XRectInRegion (region, 0, hms, w, s) != RectangleOut) ||
  1011. (XRectInRegion (region, wms, 0, s, h) != RectangleOut))
  1012. {
  1013. pt[0].x = 0; pt[0].y = h;
  1014. pt[1].x = w; pt[1].y = h;
  1015. pt[2].x = w; pt[2].y = 0;
  1016. pt[3].x = wms; pt[3].y = s;
  1017. pt[4].x = wms; pt[4].y = hms;
  1018. pt[5].x = s; pt[5].y = hms;
  1019. XFillPolygon (dpy, win, bot, pt, 6, Complex, CoordModeOrigin);
  1020. }
  1021. }
  1022. }
  1023. /*
  1024. * Set the scroll bar to the given location.
  1025. */
  1026. void
  1027. vim_XawScrollbarSetThumb(Widget w, double top, double shown, double max)
  1028. {
  1029. ScrollbarWidget sbw = (ScrollbarWidget) w;
  1030. if (sbw->scrollbar.scroll_mode == SMODE_CONT) /* if still thumbing */
  1031. return;
  1032. sbw->scrollbar.max = (max > 1.0) ? 1.0 :
  1033. (max >= 0.0) ? max : sbw->scrollbar.max;
  1034. sbw->scrollbar.top = (top > sbw->scrollbar.max) ? sbw->scrollbar.max :
  1035. (top >= 0.0) ? top : sbw->scrollbar.top;
  1036. sbw->scrollbar.shown = (shown > 1.0) ? 1.0 :
  1037. (shown >= 0.0) ? shown : sbw->scrollbar.shown;
  1038. PaintThumb(sbw);
  1039. }