home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / scrollbar-x.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-11  |  23.8 KB  |  829 lines

  1. /* scrollbar implementation -- X interface.
  2.    Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
  3.    Copyright (C) 1994 Amdhal Corporation.
  4.    Copyright (C) 1995 Sun Microsystems.
  5.    Copyright (C) 1995 Darrell Kindred <dkindred+@cmu.edu>.
  6.  
  7. This file is part of XEmacs.
  8.  
  9. XEmacs is free software; you can redistribute it and/or modify it
  10. under the terms of the GNU General Public License as published by the
  11. Free Software Foundation; either version 2, or (at your option) any
  12. later version.
  13.  
  14. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  15. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17. for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with XEmacs; see the file COPYING.  If not, write to the Free
  21. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. /* Synched up with: Not in FSF. */
  24.  
  25. #include <config.h>
  26. #include "lisp.h"
  27.  
  28. #include "frame.h"
  29. #include "device-x.h"
  30. #include "frame-x.h"
  31. #include "glyphs-x.h" /* for kludge in Fx_set_scrollbar_pointer */
  32. #include "EmacsFrame.h"
  33. #include "EmacsManager.h"
  34. #include "lwlib.h"
  35. #include "scrollbar-x.h"
  36. #include "window.h"
  37.  
  38. static void x_update_vertical_scrollbar_callback (Widget widget, LWLIB_ID id,
  39.                           XtPointer client_data);
  40. static void x_update_horizontal_scrollbar_callback (Widget widget, LWLIB_ID id,
  41.                             XtPointer client_data);
  42.  
  43. /* Used to prevent changing the size of the thumb while drag
  44.    scrolling, under Motif.  This is necessary because the Motif
  45.    scrollbar is incredibly stupid about updating the thumb and causes
  46.    lots of flicker if it is done too often.  */
  47. static int inhibit_thumb_size_change;
  48.  
  49. #if defined (LWLIB_SCROLLBARS_MOTIF) || defined (LWLIB_SCROLLBARS_LUCID)
  50. static int vertical_drag_in_progress;
  51. #endif
  52.  
  53.  
  54. /* A device method. */
  55. static int
  56. x_inhibit_scrollbar_thumb_size_change (void)
  57. {
  58. #if defined (LWLIB_SCROLLBARS_MOTIF) || defined (LWLIB_SCROLLBARS_LUCID)
  59.   return inhibit_thumb_size_change;
  60. #else
  61.   return 0;
  62. #endif
  63. }
  64.  
  65. /* A device method. */
  66. static void
  67. x_free_scrollbar_instance (struct scrollbar_instance *instance)
  68. {
  69.   if (SCROLLBAR_X_NAME (instance))
  70.     xfree (SCROLLBAR_X_NAME (instance));
  71.  
  72.   if (SCROLLBAR_X_WIDGET (instance))
  73.     {
  74.       if (XtIsManaged (SCROLLBAR_X_WIDGET (instance)))
  75.     XtUnmanageChild (SCROLLBAR_X_WIDGET (instance));
  76.  
  77.       lw_destroy_all_widgets (SCROLLBAR_X_ID (instance));
  78.     }
  79. }
  80.  
  81. /* A device method. */
  82. static void
  83. x_release_scrollbar_instance (struct scrollbar_instance *instance)
  84. {
  85.   if (XtIsManaged (SCROLLBAR_X_WIDGET (instance)))
  86.     XtUnmanageChild (SCROLLBAR_X_WIDGET (instance));
  87. }
  88.  
  89. /* Defined in menubar-x.c */
  90. extern LWLIB_ID new_lwlib_id (void);
  91.  
  92. /* A device method. */
  93. static void
  94. x_create_scrollbar_instance (struct frame *f, int vertical,
  95.                  struct scrollbar_instance *instance)
  96. {
  97.   char buffer[32];
  98.  
  99.   /* initialize the X specific data section. */
  100.   instance->scrollbar_data = malloc_type_and_zero (struct x_scrollbar_data);
  101.  
  102.   SCROLLBAR_X_ID (instance) = new_lwlib_id ();
  103.   sprintf (buffer, "scrollbar_%d", SCROLLBAR_X_ID (instance));
  104.   SCROLLBAR_X_NAME (instance) = xstrdup (buffer);
  105. #if defined (LWLIB_SCROLLBARS_MOTIF) || defined (LWLIB_SCROLLBARS_LUCID)
  106.   SCROLLBAR_X_VDRAG_ORIG_VALUE (instance) = -1;
  107. #endif
  108.  
  109.   if (vertical)
  110.     {
  111.       SCROLLBAR_X_WIDGET (instance) =
  112.     lw_create_widget ("vertical-scrollbar", SCROLLBAR_X_NAME (instance),
  113.               SCROLLBAR_X_ID (instance),
  114.               NULL, FRAME_X_CONTAINER_WIDGET (f), 0,
  115.               x_update_vertical_scrollbar_callback, NULL, NULL);
  116.     }
  117.   else
  118.     {
  119.       SCROLLBAR_X_WIDGET (instance) =
  120.     lw_create_widget ("horizontal-scrollbar", SCROLLBAR_X_NAME (instance),
  121.               SCROLLBAR_X_ID (instance),
  122.               NULL, FRAME_X_CONTAINER_WIDGET (f), 0,
  123.               x_update_horizontal_scrollbar_callback, NULL, NULL);
  124.     }
  125. }
  126.  
  127. #define UPDATE_DATA_FIELD(field)                    \
  128.   if (new_##field >= 0 &&                        \
  129.       SCROLLBAR_X_POS_DATA (inst).field != new_##field) {        \
  130.     SCROLLBAR_X_POS_DATA (inst).field = new_##field;            \
  131.     inst->scrollbar_instance_changed = 1;                \
  132.   }
  133.  
  134. /* A device method. */
  135. /* #### The -1 check is such a hack. */
  136. static void
  137. x_update_scrollbar_instance_values (struct window *w,
  138.                     struct scrollbar_instance *inst,
  139.                     int new_line_increment,
  140.                     int new_page_increment,
  141.                     int new_minimum, int new_maximum,
  142.                     int new_slider_size,
  143.                     int new_slider_position,
  144.                     int new_scrollbar_width,
  145.                     int new_scrollbar_height,
  146.                     int new_scrollbar_x, int new_scrollbar_y)
  147. {
  148.   UPDATE_DATA_FIELD (line_increment);
  149.   UPDATE_DATA_FIELD (page_increment);
  150.   UPDATE_DATA_FIELD (minimum);
  151.   UPDATE_DATA_FIELD (maximum);
  152.   UPDATE_DATA_FIELD (slider_size);
  153.   UPDATE_DATA_FIELD (slider_position);
  154.   UPDATE_DATA_FIELD (scrollbar_width);
  155.   UPDATE_DATA_FIELD (scrollbar_height);
  156.   UPDATE_DATA_FIELD (scrollbar_x);
  157.   UPDATE_DATA_FIELD (scrollbar_y);
  158.  
  159. #if defined (LWLIB_SCROLLBARS_MOTIF) || defined (LWLIB_SCROLLBARS_LUCID)
  160.   if (w && !vertical_drag_in_progress)
  161.     {
  162.       int new_vov = SCROLLBAR_X_POS_DATA (inst).slider_position;
  163.       int new_vows = marker_position (w->start[CURRENT_DISP]);
  164.  
  165.       if (SCROLLBAR_X_VDRAG_ORIG_VALUE (inst) != new_vov)
  166.     {
  167.       SCROLLBAR_X_VDRAG_ORIG_VALUE (inst) = new_vov;
  168.       inst->scrollbar_instance_changed = 1;
  169.     }
  170.       if (SCROLLBAR_X_VDRAG_ORIG_WINDOW_START (inst) != new_vows)
  171.     {
  172.       SCROLLBAR_X_VDRAG_ORIG_WINDOW_START (inst) = new_vows;
  173.       inst->scrollbar_instance_changed = 1;
  174.     }
  175.     }
  176. #endif
  177. }
  178.  
  179. /* Used by x_update_scrollbar_instance_status. */
  180. static void
  181. update_one_scrollbar_bs (struct frame *f, Widget sb_widget)
  182. {
  183.   Boolean use_backing_store;
  184.  
  185.   XtVaGetValues (FRAME_X_TEXT_WIDGET (f),
  186.          XtNuseBackingStore, &use_backing_store, 0);
  187.  
  188.   if (use_backing_store && sb_widget)
  189.     {
  190.       unsigned long mask = CWBackingStore;
  191.       XSetWindowAttributes attrs;
  192.           
  193.       attrs.backing_store = Always;
  194.       XChangeWindowAttributes (XtDisplay (sb_widget),
  195.                    XtWindow (sb_widget),
  196.                    mask,
  197.                    &attrs);
  198.     }
  199. }
  200.  
  201. /* Create a widget value structure for passing down to lwlib so that
  202.    it can update the scrollbar widgets.  Used by
  203.    x_update_scrollbar_instance_status. */
  204. static widget_value *
  205. scrollbar_instance_to_widget_value (struct scrollbar_instance *instance)
  206. {
  207.   widget_value *wv;
  208.  
  209.   wv = xmalloc_widget_value ();
  210.   /* #### maybe should add malloc_scrollbar_values to resource these? */
  211.   wv->scrollbar_data = (scrollbar_values *)
  212.     xmalloc (sizeof (scrollbar_values));
  213.  
  214.   wv->name = SCROLLBAR_X_NAME (instance);
  215.   wv->value = 0;
  216.   wv->key = 0;
  217.   wv->enabled = instance->scrollbar_is_active;
  218.   wv->selected = 0;
  219.   wv->call_data = NULL;
  220.  
  221.   *wv->scrollbar_data = SCROLLBAR_X_POS_DATA (instance);
  222.  
  223.   wv->next = NULL;
  224.  
  225.   return wv;
  226. }
  227.  
  228. /* Used by x_update_scrollbar_instance_status. */
  229. static void
  230. update_one_widget_scrollbar_pointer (struct frame *f, Widget wid)
  231. {
  232.   Lisp_Object cursor = f->scrollbar_pointer;
  233.  
  234.   if (CURSORP (cursor)) /* #### kludge */
  235.     {
  236.       XDefineCursor (XtDisplay (wid), XtWindow (wid), 
  237.              XCURSOR (cursor)->cursor);
  238.       XSync (XtDisplay (wid), False);
  239.     }
  240. }
  241.  
  242. /* A device method. */
  243. static void
  244. x_update_scrollbar_instance_status (struct window *w, int active, int size,
  245.                     struct scrollbar_instance *instance)
  246. {
  247.   struct frame *f = XFRAME (w->frame);
  248.   char managed = XtIsManaged (SCROLLBAR_X_WIDGET (instance));
  249.  
  250.   if (active && size)
  251.     {
  252.       widget_value *wv = scrollbar_instance_to_widget_value (instance);
  253.  
  254.       if (instance->scrollbar_instance_changed)
  255.     {
  256.       lw_modify_all_widgets (SCROLLBAR_X_ID (instance), wv, 0);
  257.       instance->scrollbar_instance_changed = 0;
  258.     }
  259.  
  260.       if (!managed)
  261.     {
  262.       XtManageChild (SCROLLBAR_X_WIDGET (instance));
  263.       if (XtWindow (SCROLLBAR_X_WIDGET (instance)))
  264.         {
  265.           /* Raise this window so that it's visible on top of the
  266.                  text window below it. */
  267.           XRaiseWindow (XtDisplay (SCROLLBAR_X_WIDGET (instance)),
  268.                 XtWindow (SCROLLBAR_X_WIDGET (instance)));
  269.           update_one_widget_scrollbar_pointer (f,
  270.                            SCROLLBAR_X_WIDGET (instance));
  271.           if (!SCROLLBAR_X_BACKING_STORE_INITIALIZED (instance))
  272.         {
  273.           update_one_scrollbar_bs (f, SCROLLBAR_X_WIDGET (instance));
  274.           SCROLLBAR_X_BACKING_STORE_INITIALIZED (instance) = 1;
  275.         }
  276.         }
  277.     }
  278.  
  279.       if (!wv->scrollbar_data) abort ();
  280.       xfree (wv->scrollbar_data);
  281.       wv->scrollbar_data = 0;
  282.       free_widget_value (wv);
  283.     }
  284.   else if (managed)
  285.     {
  286.       XtUnmanageChild (SCROLLBAR_X_WIDGET (instance));
  287.     }
  288. }
  289.  
  290. /* A device method. */
  291. static void
  292. x_scrollbar_width_changed_in_frame (Lisp_Object specifier, struct frame *f,
  293.                     Lisp_Object oldval)
  294. {
  295.   XtWidgetGeometry req, repl;
  296.   Lisp_Object newval = f->scrollbar_width;
  297.  
  298.   in_specifier_change_function++;
  299.  
  300.   /* We want the text area to stay the same size.  So, we query the
  301.      current size and then adjust it for the change in the scrollbar
  302.      width. */
  303.  
  304.   /* mirror the value in the frame resources, unless it was already
  305.      done. */
  306.   if (!in_resource_setting)
  307.     XtVaSetValues (FRAME_X_TEXT_WIDGET (f), XtNscrollBarWidth,
  308.            XINT (newval), 0);
  309.  
  310.   if (XtIsRealized (FRAME_X_CONTAINER_WIDGET (f)))
  311.     {
  312.       req.request_mode = 0;
  313.  
  314.       /* the query-geometry method looks at the current value of
  315.      f->scrollbar_width, so temporarily set it back to the old
  316.      one. */
  317.       f->scrollbar_width = oldval;
  318.       XtQueryGeometry (FRAME_X_CONTAINER_WIDGET (f), &req, &repl);
  319.       f->scrollbar_width = newval;
  320.       
  321.       repl.width += XINT (newval) - XINT (oldval);
  322.       EmacsManagerChangeSize (FRAME_X_CONTAINER_WIDGET (f), repl.width,
  323.                   repl.height);
  324.     }
  325.  
  326.   in_specifier_change_function--;
  327. }
  328.  
  329. /* A device method. */
  330. static void
  331. x_scrollbar_height_changed_in_frame (Lisp_Object specifier, struct frame *f,
  332.                      Lisp_Object oldval)
  333. {
  334.   XtWidgetGeometry req, repl;
  335.   Lisp_Object newval = f->scrollbar_height;
  336.  
  337.   in_specifier_change_function++;
  338.  
  339.   /* We want the text area to stay the same size.  So, we query the
  340.      current size and then adjust it for the change in the scrollbar
  341.      height. */
  342.  
  343.     /* mirror the value in the frame resources, unless it was already
  344.        done.  Also don't do it if this is the when the frame is being
  345.        created -- the widgets don't even exist yet, and even if they
  346.        did, we wouldn't want to overwrite the resource information
  347.        (which might specify a user preference). */
  348.   if (!in_resource_setting)
  349.     XtVaSetValues (FRAME_X_TEXT_WIDGET (f), XtNscrollBarHeight,
  350.            XINT (newval), 0);
  351.  
  352.   if (XtIsRealized (FRAME_X_CONTAINER_WIDGET (f)))
  353.     {
  354.       req.request_mode = 0;
  355.  
  356.       /* the query-geometry method looks at the current value of
  357.      f->scrollbar_height, so temporarily set it back to the old
  358.      one. */
  359.       f->scrollbar_height = oldval;
  360.       XtQueryGeometry (FRAME_X_CONTAINER_WIDGET (f), &req, &repl);
  361.       f->scrollbar_height = newval;
  362.       
  363.       repl.height += XINT (newval) - XINT (oldval);
  364.       EmacsManagerChangeSize (FRAME_X_CONTAINER_WIDGET (f), repl.width,
  365.                   repl.height);
  366.     }
  367.  
  368.   in_specifier_change_function--;
  369. }
  370.  
  371. enum x_scrollbar_loop
  372. {
  373.   X_FIND_SCROLLBAR_WINDOW_MIRROR,
  374.   X_SET_SCROLLBAR_POINTER,
  375.   X_WINDOW_IS_SCROLLBAR,
  376.   X_UPDATE_FRAME_SCROLLBARS
  377. };
  378.  
  379. static struct window_mirror *
  380. x_scrollbar_loop (enum x_scrollbar_loop type, Lisp_Object window,
  381.           struct window_mirror *mir,
  382.           LWLIB_ID id, Window x_win)
  383. {
  384.   struct window_mirror *retval = NULL;
  385.  
  386.   while (mir)
  387.     {
  388.       struct scrollbar_instance *vinstance = mir->scrollbar_vertical_instance;
  389.       struct scrollbar_instance *hinstance =
  390.     mir->scrollbar_horizontal_instance;
  391.       struct frame *f;
  392.  
  393.       assert (!NILP (window));
  394.       f = XFRAME (XWINDOW (window)->frame);
  395.  
  396.       if (mir->vchild)
  397.     {
  398.       retval = x_scrollbar_loop (type, XWINDOW (window)->vchild,
  399.                      mir->vchild, id, x_win);
  400.     }
  401.       else if (mir->hchild)
  402.     {
  403.       retval = x_scrollbar_loop (type, XWINDOW (window)->hchild,
  404.                      mir->hchild, id, x_win);
  405.     }
  406.  
  407.       if (retval != NULL)
  408.     return retval;
  409.  
  410.       if (hinstance || vinstance)
  411.     {
  412.       switch (type)
  413.         {
  414.         case X_FIND_SCROLLBAR_WINDOW_MIRROR:
  415.           if ((vinstance && SCROLLBAR_X_ID (vinstance) == id)
  416.           || (hinstance && SCROLLBAR_X_ID (hinstance) == id))
  417.         {
  418.           return mir;
  419.         }
  420.           break;
  421.         case X_UPDATE_FRAME_SCROLLBARS:
  422.           if (!mir->vchild && !mir->hchild)
  423.         update_window_scrollbars (XWINDOW (window), mir, 1, 0);
  424.           break;
  425.         case X_SET_SCROLLBAR_POINTER:
  426.           if (!mir->vchild && !mir->hchild)
  427.         {
  428.           int loop;
  429.  
  430.           for (loop = 0; loop < 2; loop++)
  431.             {
  432.               Widget widget;
  433.  
  434.               if (loop)
  435.             widget = SCROLLBAR_X_WIDGET (vinstance);
  436.               else
  437.             widget = SCROLLBAR_X_WIDGET (hinstance);
  438.  
  439.               if (widget && XtIsManaged (widget))
  440.             {
  441.               update_one_widget_scrollbar_pointer (f, widget);
  442.             }
  443.             }
  444.         }
  445.           break;
  446.         case X_WINDOW_IS_SCROLLBAR:
  447.           if (!mir->vchild && !mir->hchild)
  448.         {
  449.           int loop;
  450.  
  451.           for (loop = 0; loop < 2; loop++)
  452.             {
  453.               Widget widget;
  454.  
  455.               if (loop)
  456.             widget = SCROLLBAR_X_WIDGET (vinstance);
  457.               else
  458.             widget = SCROLLBAR_X_WIDGET (hinstance);
  459.  
  460.               if (widget && XtIsManaged (widget))
  461.             {
  462.               if (XtWindow (widget) == x_win)
  463.                 return (struct window_mirror *) 1;
  464.             }
  465.             }
  466.         }
  467.           break;
  468.         default:
  469.           abort ();
  470.         }
  471.     }
  472.  
  473.       mir = mir->next;
  474.       window = XWINDOW (window)->next;
  475.     }
  476.  
  477.   return NULL;
  478. }
  479.  
  480. /* Used by callbacks. */
  481. static struct window_mirror *
  482. find_scrollbar_window_mirror (struct frame *f, LWLIB_ID id)
  483. {
  484.   if (f->mirror_dirty)
  485.     update_frame_window_mirror (f);
  486.   return x_scrollbar_loop (X_FIND_SCROLLBAR_WINDOW_MIRROR, f->root_window,
  487.                f->root_mirror, id, (Window) NULL);
  488. }
  489.  
  490. /*
  491.  * This is the only callback provided for vertical scrollbars.  It
  492.  * should be able to handle all of the scrollbar events in
  493.  * scroll_action (see lwlib.h).  The client data will be of type
  494.  * scroll_event (see lwlib.h). */
  495. static void
  496. x_update_vertical_scrollbar_callback (Widget widget, LWLIB_ID id,
  497.                       XtPointer client_data)
  498. {
  499.   /* This function can GC */
  500.   scroll_event *data = (scroll_event *) client_data;
  501.   struct device *d = get_device_from_display (XtDisplay (widget));
  502.   struct frame *f = x_any_window_to_frame (d, XtWindow (widget));
  503.   Lisp_Object win;
  504.   struct scrollbar_instance *instance;
  505.   struct window_mirror *mirror;
  506.  
  507.   if (!f)
  508.     return;
  509.  
  510.   mirror = find_scrollbar_window_mirror (f, id);
  511.   win = real_window (mirror, 1);
  512.  
  513.   if (NILP (win))
  514.     return;
  515.   instance = mirror->scrollbar_vertical_instance;
  516.  
  517.   /* It seems that this is necessary whenever signal_special_Xt_user_event()
  518.      is called.  #### Why??? */
  519.   DEVICE_X_MOUSE_TIMESTAMP (d) = DEVICE_X_GLOBAL_MOUSE_TIMESTAMP (d);
  520.  
  521.   switch (data->action)
  522.     {
  523.     case SCROLLBAR_LINE_UP:
  524.       signal_special_Xt_user_event (Qscrollbar_line_up, win);
  525.       break;
  526.  
  527.     case SCROLLBAR_LINE_DOWN:
  528.       signal_special_Xt_user_event (Qscrollbar_line_down, win);
  529.       break;
  530.  
  531.       /* The Athena scrollbar paging behavior is that of xterms.
  532.          Depending on where you click the size of the page varies.
  533.          Motif always does a standard Emacs page. */
  534.     case SCROLLBAR_PAGE_UP:
  535. #if !defined (LWLIB_SCROLLBARS_MOTIF) && !defined (LWLIB_SCROLLBARS_LUCID)
  536.       {
  537.     double tmp = ((double) data->slider_value /
  538.               (double) instance->data.scrollbar_height);
  539.     double line = tmp *
  540.       (double) window_displayed_height (XWINDOW (win));
  541.     
  542.     if (line > -1.0)
  543.       line = -1.0;
  544.     signal_special_Xt_user_event (Qscrollbar_page_up,
  545.                       Fcons (win, make_number ((int) line)));
  546.       }
  547. #else
  548.       signal_special_Xt_user_event (Qscrollbar_page_up, Fcons (win, Qnil));
  549. #endif
  550.       break;
  551.  
  552.     case SCROLLBAR_PAGE_DOWN:
  553. #if !defined (LWLIB_SCROLLBARS_MOTIF) && !defined (LWLIB_SCROLLBARS_LUCID)
  554.       {
  555.     double tmp = ((double) data->slider_value /
  556.               (double) instance->data.scrollbar_height);
  557.     double line = tmp *
  558.       (double) window_displayed_height (XWINDOW (win));
  559.  
  560.     if (instance->data.maximum >
  561.         (instance->data.slider_size + instance->data.slider_position))
  562.       {
  563.         if (line < 1.0)
  564.           line = 1.0;
  565.         signal_special_Xt_user_event (Qscrollbar_page_down,
  566.                       Fcons (win,
  567.                          make_number ((int) line)));
  568.       }
  569.       }
  570. #else
  571.       signal_special_Xt_user_event (Qscrollbar_page_down, Fcons (win, Qnil));
  572. #endif
  573.       break;
  574.  
  575.     case SCROLLBAR_TOP:
  576.       signal_special_Xt_user_event (Qscrollbar_to_top, win);
  577.       break;
  578.  
  579.     case SCROLLBAR_BOTTOM:
  580.       signal_special_Xt_user_event (Qscrollbar_to_bottom, win);
  581.       break;
  582.  
  583.  
  584.     case SCROLLBAR_CHANGE:
  585.       inhibit_thumb_size_change = 0;
  586. #if defined (LWLIB_SCROLLBARS_MOTIF) || defined (LWLIB_SCROLLBARS_LUCID)
  587.       vertical_drag_in_progress = 0;
  588.       SCROLLBAR_X_VDRAG_ORIG_VALUE (instance) = data->slider_value;
  589.       SCROLLBAR_X_VDRAG_ORIG_WINDOW_START (instance) =
  590.     XINT (Fwindow_start (win));
  591. #endif
  592.       break;
  593.  
  594.     case SCROLLBAR_DRAG:
  595.       {
  596.     int value;
  597.  
  598.     inhibit_thumb_size_change = 1;
  599.  
  600. #if defined (LWLIB_SCROLLBARS_MOTIF) || defined (LWLIB_SCROLLBARS_LUCID)
  601.     /* Doing drags with Motif-like scrollbars is a mess, since we
  602.        want to avoid having the window position jump when you
  603.        first grab the scrollbar, but we also want to ensure that
  604.        you can scroll all the way to the top or bottom of the
  605.        buffer.  This can all be replaced with something sane when
  606.        we get line-based scrolling. */
  607.  
  608.     vertical_drag_in_progress = 1;
  609.     
  610.     if (SCROLLBAR_X_VDRAG_ORIG_VALUE (instance) < 0) 
  611.       {
  612.         SCROLLBAR_X_VDRAG_ORIG_VALUE (instance) = data->slider_value;
  613.         SCROLLBAR_X_VDRAG_ORIG_WINDOW_START (instance) =
  614.           XINT (Fwindow_start (win));
  615.       }
  616.  
  617.     /* Could replace this piecewise linear scrolling with a
  618.        quadratic through the three points, but I'm not sure that
  619.        would feel any nicer in practice. */
  620.     if (data->slider_value < SCROLLBAR_X_VDRAG_ORIG_VALUE (instance)) 
  621.       {
  622.         /* We've dragged up; slide linearly from original position to
  623.            window-start=data.minimum, slider-value=data.minimum. */
  624.  
  625.         if (SCROLLBAR_X_VDRAG_ORIG_VALUE (instance)
  626.         <= SCROLLBAR_X_POS_DATA (instance).minimum) 
  627.           {
  628.         /* shouldn't get here, but just in case */
  629.         value = SCROLLBAR_X_POS_DATA (instance).minimum;
  630.           }
  631.         else
  632.           {
  633.         value = (SCROLLBAR_X_POS_DATA (instance).minimum
  634.              + (((double) (SCROLLBAR_X_VDRAG_ORIG_WINDOW_START (instance) 
  635.                        - SCROLLBAR_X_POS_DATA (instance).minimum)
  636.                  * (data->slider_value -
  637.                 SCROLLBAR_X_POS_DATA (instance).minimum)) 
  638.                 / (SCROLLBAR_X_VDRAG_ORIG_VALUE (instance)
  639.                    - SCROLLBAR_X_POS_DATA (instance).minimum)));
  640.           }
  641.       }
  642.     else 
  643.       {
  644.         /* We've dragged down; slide linearly from original position to
  645.            window-start=data.maximum, slider-value=data.maximum. */
  646.  
  647.         if (SCROLLBAR_X_VDRAG_ORIG_VALUE (instance) 
  648.         >= (SCROLLBAR_X_POS_DATA (instance).maximum -
  649.             SCROLLBAR_X_POS_DATA (instance).slider_size))
  650.           {
  651.         /* avoid divide by zero */
  652.         value = SCROLLBAR_X_VDRAG_ORIG_WINDOW_START (instance);
  653.           } 
  654.         else 
  655.           {
  656.         value = (SCROLLBAR_X_VDRAG_ORIG_WINDOW_START (instance)
  657.              + (((double) (SCROLLBAR_X_POS_DATA (instance).maximum
  658.                        - SCROLLBAR_X_VDRAG_ORIG_WINDOW_START (instance))
  659.                  * (data->slider_value 
  660.                 - SCROLLBAR_X_VDRAG_ORIG_VALUE (instance))) 
  661.                 / (SCROLLBAR_X_POS_DATA (instance).maximum
  662.                    - SCROLLBAR_X_POS_DATA (instance).slider_size
  663.                    - SCROLLBAR_X_VDRAG_ORIG_VALUE (instance))));
  664.           }
  665.       }
  666. #else
  667.     value = data->slider_value;
  668. #endif
  669.  
  670.     if (value >= SCROLLBAR_X_POS_DATA (instance).maximum)
  671.       value = SCROLLBAR_X_POS_DATA (instance).maximum - 1;
  672.     if (value < SCROLLBAR_X_POS_DATA (instance).minimum)
  673.       value = SCROLLBAR_X_POS_DATA (instance).minimum;
  674.  
  675.     signal_special_Xt_user_event (Qscrollbar_vertical_drag,
  676.                       Fcons (win, make_number (value)));
  677.       }
  678.       break;
  679.  
  680.     }
  681. }
  682.  
  683. /*
  684.  * This is the only callback provided for horizontal scrollbars.  It
  685.  * should be able to handle all of the scrollbar events in
  686.  * scroll_action (see lwlib.h).  The client data will be of type
  687.  * scroll_event (see lwlib.h). */
  688. static void
  689. x_update_horizontal_scrollbar_callback (Widget widget, LWLIB_ID id,
  690.                     XtPointer client_data)
  691. {
  692.   scroll_event *data = (scroll_event *) client_data;
  693.   struct device *d = get_device_from_display (XtDisplay (widget));
  694.   struct frame *f = x_any_window_to_frame (d, XtWindow (widget));
  695.   Lisp_Object win;
  696.   struct window_mirror *mirror;
  697.  
  698.   if (!f)
  699.     return;
  700.  
  701.   mirror = find_scrollbar_window_mirror (f, id);
  702.   win = real_window (mirror, 1);
  703.  
  704.   if (NILP (win))
  705.     return;
  706.  
  707.   /* It seems that this is necessary whenever signal_special_Xt_user_event()
  708.      is called.  #### Why??? */
  709.   DEVICE_X_MOUSE_TIMESTAMP (d) = DEVICE_X_GLOBAL_MOUSE_TIMESTAMP (d);
  710.  
  711.   switch (data->action)
  712.     {
  713.     case SCROLLBAR_LINE_UP:
  714.       signal_special_Xt_user_event (Qscrollbar_char_left, win);
  715.       break;
  716.     case SCROLLBAR_LINE_DOWN:
  717.       signal_special_Xt_user_event (Qscrollbar_char_right, win);
  718.       break;
  719.     case SCROLLBAR_PAGE_UP:
  720.       signal_special_Xt_user_event (Qscrollbar_page_left, win);
  721.       break;
  722.     case SCROLLBAR_PAGE_DOWN:
  723.       signal_special_Xt_user_event (Qscrollbar_page_right, win);
  724.       break;
  725.     case SCROLLBAR_TOP:
  726.       signal_special_Xt_user_event (Qscrollbar_to_left, win);
  727.       break;
  728.     case SCROLLBAR_BOTTOM:
  729.       signal_special_Xt_user_event (Qscrollbar_to_right, win);
  730.       break;
  731.     case SCROLLBAR_CHANGE:
  732.       inhibit_thumb_size_change = 0;
  733.       break;
  734.     case SCROLLBAR_DRAG:
  735.       inhibit_thumb_size_change = 1;
  736.       /* #### Fix the damn toolkit code so they all work the same way.
  737.          Lucid is the one mostly wrong.*/
  738. #if defined (LWLIB_SCROLLBARS_LUCID)
  739.       signal_special_Xt_user_event (Qscrollbar_horizontal_drag,
  740.                     (Fcons
  741.                      (win, make_number (data->slider_value))));
  742. #else
  743.       signal_special_Xt_user_event (Qscrollbar_horizontal_drag,
  744.                     (Fcons
  745.                      (win,
  746.                       make_number (data->slider_value - 1))));
  747. #endif
  748.       break;
  749.     default:
  750.       break;
  751.     }
  752. }
  753.  
  754. /* Called directly by Fx_set_scrollbar_pointer in frame-x.c */
  755. void
  756. x_set_scrollbar_pointer (struct frame *f, Lisp_Object cursor)
  757. {
  758.   if (!FRAME_IS_X (f))
  759.     return;
  760.  
  761.   if (!EQ (f->scrollbar_pointer, cursor))
  762.     {
  763.       /* #### If the user cuts this pointer, we'll get X errors.
  764.          This needs to be rethunk. */
  765.       f->scrollbar_pointer = cursor;
  766.  
  767.       if (f->mirror_dirty)
  768.     update_frame_window_mirror (f);
  769.       x_scrollbar_loop (X_SET_SCROLLBAR_POINTER, f->root_window,
  770.             f->root_mirror, 0, (Window) NULL);
  771.     }
  772. }
  773.  
  774. /* Called directly from x_any_window_to_frame in frame-x.c */
  775. int
  776. x_window_is_scrollbar (struct frame *f, Window win)
  777. {
  778.   if (!FRAME_IS_X (f))
  779.     return 0;
  780.  
  781.   if (f->mirror_dirty)
  782.     update_frame_window_mirror (f);
  783.   return (int) x_scrollbar_loop (X_WINDOW_IS_SCROLLBAR, f->root_window,
  784.                  f->root_mirror, 0, win);
  785. }
  786.  
  787. /* Make sure that all scrollbars on frame are up-to-date.  Called
  788.    directly from x_set_frame_params_1 in frame-x.c*/
  789. void
  790. x_update_frame_scrollbars (struct frame *f)
  791. {
  792.   /* Consider this code to be "in_display" so that we abort() if Fsignal()
  793.      gets called. */
  794.   in_display++;
  795.   x_scrollbar_loop (X_UPDATE_FRAME_SCROLLBARS, f->root_window, f->root_mirror,
  796.             0, (Window) NULL);
  797.   in_display--;
  798.   if (in_display < 0) abort ();
  799. }
  800.  
  801. /************************************************************************/
  802. /*                            initialization                            */
  803. /************************************************************************/
  804.  
  805. void
  806. device_type_create_scrollbar_x (void)
  807. {
  808.   DEVICE_HAS_METHOD (x, inhibit_scrollbar_thumb_size_change);
  809.   DEVICE_HAS_METHOD (x, free_scrollbar_instance);
  810.   DEVICE_HAS_METHOD (x, release_scrollbar_instance);
  811.   DEVICE_HAS_METHOD (x, create_scrollbar_instance);
  812.   DEVICE_HAS_METHOD (x, update_scrollbar_instance_values);
  813.   DEVICE_HAS_METHOD (x, update_scrollbar_instance_status);
  814.   DEVICE_HAS_METHOD (x, scrollbar_width_changed_in_frame);
  815.   DEVICE_HAS_METHOD (x, scrollbar_height_changed_in_frame);
  816. }
  817.  
  818. void
  819. vars_of_scrollbar_x (void)
  820. {
  821. #if defined (LWLIB_SCROLLBARS_LUCID)
  822.   Fprovide (intern ("lucid-scrollbars"));
  823. #elif defined (LWLIB_SCROLLBARS_MOTIF)
  824.   Fprovide (intern ("motif-scrollbars"));
  825. #elif defined (LWLIB_SCROLLBARS_ATHENA)
  826.   Fprovide (intern ("athena-scrollbars"));
  827. #endif
  828. }
  829.