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.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-29  |  26.0 KB  |  913 lines

  1. /* Generic scrollbar implementation.
  2.    Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
  3.    Copyright (C) 1995 Amdahl 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. /* This file has been Mule-ized. */
  26.  
  27. #include <config.h>
  28. #include "lisp.h"
  29.  
  30. #include "buffer.h"
  31. #include "commands.h"
  32. #include "scrollbar.h"
  33. #include "device.h"
  34. #include "frame.h"
  35. #include "window.h"
  36.  
  37. Lisp_Object Qinit_scrollbar_from_resources;
  38.  
  39. Lisp_Object Qscrollbar_line_up;
  40. Lisp_Object Qscrollbar_line_down;
  41. Lisp_Object Qscrollbar_page_up;
  42. Lisp_Object Qscrollbar_page_down;
  43. Lisp_Object Qscrollbar_to_top;
  44. Lisp_Object Qscrollbar_to_bottom;
  45. Lisp_Object Qscrollbar_vertical_drag;
  46.  
  47. Lisp_Object Qscrollbar_char_left;
  48. Lisp_Object Qscrollbar_char_right;
  49. Lisp_Object Qscrollbar_page_left;
  50. Lisp_Object Qscrollbar_page_right;
  51. Lisp_Object Qscrollbar_to_left;
  52. Lisp_Object Qscrollbar_to_right;
  53. Lisp_Object Qscrollbar_horizontal_drag;
  54.  
  55. #define DEFAULT_SCROLLBAR_WIDTH 15
  56. #define DEFAULT_SCROLLBAR_HEIGHT 15
  57.  
  58. /* Width of the scrollbar. */
  59. Lisp_Object Vscrollbar_width;
  60.  
  61. /* Height of the scrollbar. */
  62. Lisp_Object Vscrollbar_height;
  63.  
  64. static void update_scrollbar_instance (struct window *w, int vertical,
  65.                        struct scrollbar_instance *instance);
  66.  
  67.  
  68. static void
  69. free_scrollbar_instance (struct scrollbar_instance *instance,
  70.              struct frame *frame)
  71. {
  72.   if (!instance)
  73.     return;
  74.   else
  75.     {
  76.       struct device *d = XDEVICE (frame->device);
  77.  
  78.       MAYBE_DEVMETH (d, free_scrollbar_instance, (instance));
  79.     }
  80. }
  81.  
  82. static void
  83. free_window_mirror_scrollbars (struct window_mirror *mir)
  84. {
  85.   struct frame *f = mir->frame;
  86.   free_scrollbar_instance (mir->scrollbar_vertical_instance, f);
  87.   mir->scrollbar_vertical_instance = 0;
  88.   free_scrollbar_instance (mir->scrollbar_horizontal_instance, f);
  89.   mir->scrollbar_horizontal_instance = 0;
  90. }
  91.  
  92. static struct window_mirror *
  93. free_scrollbars_loop (Lisp_Object window, struct window_mirror *mir)
  94. {
  95.   struct window_mirror *retval = NULL;
  96.  
  97.   while (mir)
  98.     {
  99.       struct scrollbar_instance *vinst = mir->scrollbar_vertical_instance;
  100.       struct scrollbar_instance *hinst = mir->scrollbar_horizontal_instance;
  101.       struct frame *f;
  102.  
  103.       assert (!NILP (window));
  104.       f = XFRAME (XWINDOW (window)->frame);
  105.  
  106.       if (mir->vchild)
  107.     {
  108.       retval = free_scrollbars_loop (XWINDOW (window)->vchild,
  109.                      mir->vchild);
  110.     }
  111.       else if (mir->hchild)
  112.     {
  113.       retval = free_scrollbars_loop (XWINDOW (window)->hchild,
  114.                      mir->hchild);
  115.     }
  116.  
  117.       if (retval != NULL)
  118.     return retval;
  119.  
  120.       if (hinst || vinst)
  121.     free_window_mirror_scrollbars (mir);
  122.  
  123.       mir = mir->next;
  124.       window = XWINDOW (window)->next;
  125.     }
  126.  
  127.   return NULL;
  128. }
  129.  
  130. /* Destroy all scrollbars associated with FRAME.  Only called from
  131.    delete_frame_internal.
  132.  */
  133. #define FREE_FRAME_SCROLLBARS_INTERNAL(cache)                \
  134.   do {                                    \
  135.     while (FRAME_SB_##cache (f))                    \
  136.       {                                    \
  137.     struct scrollbar_instance *tofree = FRAME_SB_##cache (f);    \
  138.     FRAME_SB_##cache (f) = FRAME_SB_##cache (f)->next;        \
  139.     tofree->next = NULL;                        \
  140.     free_scrollbar_instance (tofree, f);                \
  141.       }                                    \
  142.   } while (0)
  143.  
  144. void
  145. free_frame_scrollbars (struct frame *f)
  146. {
  147.   struct device *d = XDEVICE (f->device);
  148.  
  149.   if (!HAS_DEVMETH_P (d, create_scrollbar_instance))
  150.     return;
  151.  
  152.   if (f->mirror_dirty)
  153.     update_frame_window_mirror (f);
  154.  
  155.   free_scrollbars_loop (f->root_window, f->root_mirror);
  156.  
  157.   FREE_FRAME_SCROLLBARS_INTERNAL (VCACHE);
  158.   FREE_FRAME_SCROLLBARS_INTERNAL (HCACHE);
  159. }
  160. #undef FREE_FRAME_SCROLLBARS_INTERNAL
  161.  
  162.  
  163. static struct scrollbar_instance *
  164. create_scrollbar_instance (struct frame *f, int vertical)
  165. {
  166.   struct device *d = XDEVICE (f->device);
  167.   struct scrollbar_instance *instance =
  168.     (struct scrollbar_instance *) xmalloc (sizeof (*instance));
  169.  
  170.   memset (instance, 0, sizeof (*instance));
  171.   MAYBE_DEVMETH (d, create_scrollbar_instance, (f, vertical, instance));
  172.  
  173.   return instance;
  174. }
  175.  
  176.  
  177. #define GET_SCROLLBAR_INSTANCE_INTERNAL(cache)                \
  178.   do {                                    \
  179.     if (FRAME_SB_##cache (f))                        \
  180.       {                                    \
  181.         struct scrollbar_instance *retval = FRAME_SB_##cache (f);    \
  182.         FRAME_SB_##cache (f) = FRAME_SB_##cache (f)->next;        \
  183.         retval->next = NULL;                        \
  184.         return retval;                            \
  185.       }                                    \
  186.   } while (0)
  187.  
  188. static struct scrollbar_instance *
  189. get_scrollbar_instance (struct frame *f, int vertical)
  190. {
  191.   /* Check if there are any available scrollbars already in existence. */
  192.   if (vertical)
  193.     GET_SCROLLBAR_INSTANCE_INTERNAL (VCACHE);
  194.   else
  195.     GET_SCROLLBAR_INSTANCE_INTERNAL (HCACHE);
  196.  
  197.   return create_scrollbar_instance (f, vertical);
  198. }
  199. #undef GET_SCROLLBAR_INSTANCE_INTERNAL
  200.  
  201. #define RELEASE_SCROLLBAR_INSTANCE_INTERNAL(cache)            \
  202.   do {                                    \
  203.     if (!FRAME_SB_##cache (f))                        \
  204.       {                                    \
  205.     instance->next = NULL;                        \
  206.     FRAME_SB_##cache (f) = instance;                \
  207.       }                                    \
  208.     else                                \
  209.       {                                    \
  210.     instance->next = FRAME_SB_##cache (f);                \
  211.     FRAME_SB_##cache (f) = instance;                \
  212.       }                                    \
  213.   } while (0)
  214.  
  215. static void
  216. release_scrollbar_instance (struct frame *f, int vertical,
  217.                 struct scrollbar_instance *instance)
  218. {
  219.   /* #### should we do "instance->mir = 0;" for safety? */
  220.   if (vertical)
  221.     RELEASE_SCROLLBAR_INSTANCE_INTERNAL (VCACHE);
  222.   else
  223.     RELEASE_SCROLLBAR_INSTANCE_INTERNAL (HCACHE);
  224. }
  225. #undef RELEASE_SCROLLBAR_INSTANCE_INTERNAL
  226.  
  227. void
  228. update_window_scrollbars (struct window *w, struct window_mirror *mirror,
  229.               int active, int horiz_only)
  230. {
  231.   struct frame *f = XFRAME (w->frame);
  232.   struct device *d = XDEVICE (f->device);
  233.  
  234.   if (!HAS_DEVMETH_P (d, create_scrollbar_instance))
  235.     return;
  236.  
  237.   in_display++;
  238.  
  239.   /* It is possible for this to get called from the mirror update
  240.      routines.  In that case the structure is in an indeterminate
  241.      state but we know exactly what struct we are working with.  So we
  242.      pass it in in that case.  We also take advantage of it at some
  243.      other points where we know what the mirror struct is. */
  244.   if (!mirror)
  245.     mirror = find_window_mirror (w);
  246.  
  247.   if (!mirror->scrollbar_vertical_instance && active)
  248.     mirror->scrollbar_vertical_instance = get_scrollbar_instance (f, 1);
  249.  
  250.   if (!mirror->scrollbar_horizontal_instance && active)
  251.     mirror->scrollbar_horizontal_instance = get_scrollbar_instance (f, 0);
  252.  
  253.   if (!horiz_only && mirror->scrollbar_vertical_instance)
  254.     {
  255.       int size = (active ? window_scrollbar_width (w) : 0);
  256.       struct scrollbar_instance *instance;
  257.  
  258.       instance = mirror->scrollbar_vertical_instance;
  259.       instance->scrollbar_is_active = active;
  260.       instance->mirror = mirror;
  261.  
  262.       if (active && size)
  263.     update_scrollbar_instance (w, 1, instance);
  264.       MAYBE_DEVMETH (d, update_scrollbar_instance_status,
  265.              (w, active, size, instance));
  266.  
  267.       if (!active)
  268.      {
  269.        release_scrollbar_instance (f, 1, instance);
  270.        mirror->scrollbar_vertical_instance = NULL;
  271.      }
  272.     }
  273.  
  274.   if (mirror->scrollbar_horizontal_instance)
  275.     {
  276.       int size = (active ? window_scrollbar_height (w) : 0);
  277.       struct scrollbar_instance *instance;
  278.  
  279.       instance = mirror->scrollbar_horizontal_instance;
  280.       instance->scrollbar_is_active = active;
  281.       instance->mirror = mirror;
  282.  
  283.       if (active && size)
  284.     update_scrollbar_instance (w, 0, instance);
  285.       MAYBE_DEVMETH (d, update_scrollbar_instance_status,
  286.              (w, active, size, instance));
  287.  
  288.       if (!active)
  289.      {
  290.        release_scrollbar_instance (f, 0, instance);
  291.        mirror->scrollbar_horizontal_instance = NULL;
  292.      }
  293.     }
  294.  
  295.   in_display--;
  296. }
  297.  
  298. void
  299. release_window_mirror_scrollbars (struct window_mirror *mir)
  300. {
  301.   struct device *d = XDEVICE (mir->frame->device);
  302.  
  303.   if (!HAS_DEVMETH_P (d, create_scrollbar_instance))
  304.     return;
  305.  
  306.   if (mir->scrollbar_vertical_instance)
  307.     {
  308.       release_scrollbar_instance (mir->frame, 1,
  309.                   mir->scrollbar_vertical_instance);
  310.       MAYBE_DEVMETH (d, release_scrollbar_instance,
  311.              (mir->scrollbar_vertical_instance));
  312.     }
  313.   mir->scrollbar_vertical_instance = 0;
  314.  
  315.   if (mir->scrollbar_horizontal_instance)
  316.     {
  317.       release_scrollbar_instance (mir->frame, 0,
  318.                   mir->scrollbar_horizontal_instance);
  319.       MAYBE_DEVMETH (d, release_scrollbar_instance,
  320.              (mir->scrollbar_horizontal_instance));
  321.     }
  322.   mir->scrollbar_horizontal_instance = 0;
  323. }
  324.  
  325. /* This check needs to be done in the device-specific side. */
  326. #define UPDATE_DATA_FIELD(field, value) \
  327.   if (instance->field != value) {\
  328.     instance->field = value;\
  329.     instance->scrollbar_instance_changed = 1;\
  330.   }\
  331.  
  332. /*
  333.  * If w->sb_point is on the top line then return w->sb_point else
  334.  * return w->start.  If flag, then return beginning point of line
  335.  * which w->sb_point lies on.
  336.  */
  337. static Bufpos
  338. scrollbar_point (struct window *w, int flag)
  339. {
  340.   Bufpos start_pos, end_pos, sb_pos;
  341.   Lisp_Object buf;
  342.   struct buffer *b;
  343.  
  344.   if (NILP (w->buffer)) /* non-leaf window */
  345.     return 0;
  346.  
  347.   start_pos = marker_position (w->start[CURRENT_DISP]);
  348.   sb_pos = marker_position (w->sb_point);
  349.  
  350.   if (!flag && sb_pos < start_pos)
  351.     return start_pos;
  352.  
  353.   buf = get_buffer (w->buffer, 0);
  354.   if (!NILP (buf))
  355.     b = XBUFFER (buf);
  356.   else
  357.     return start_pos;
  358.  
  359.   if (flag)
  360.     end_pos = find_next_newline_no_quit (b, sb_pos, -1);
  361.   else
  362.     end_pos = find_next_newline_no_quit (b, start_pos, 1);
  363.  
  364.   if (flag)
  365.     return end_pos;
  366.   else if (sb_pos > end_pos)
  367.     return start_pos;
  368.   else
  369.     return sb_pos;
  370. }
  371.  
  372. /*
  373.  * Update a window's horizontal or vertical scrollbar.
  374.  */
  375. static void
  376. update_scrollbar_instance (struct window *w, int vertical,
  377.                struct scrollbar_instance *instance)
  378. {
  379.   struct frame *f = XFRAME (w->frame);
  380.   struct device *d = XDEVICE (f->device);
  381.   struct buffer *b = XBUFFER (w->buffer);
  382.   Bufpos start_pos, end_pos, sb_pos;
  383.   int scrollbar_width = window_scrollbar_width (w);
  384.   int scrollbar_height = window_scrollbar_height (w);
  385.  
  386.   int new_line_increment = -1, new_page_increment = -1;
  387.   int new_minimum = -1, new_maximum = -1;
  388.   int new_slider_size = -1, new_slider_position = -1;
  389.   int new_width = -1, new_height = -1, new_x = -1, new_y = -1;
  390.   struct window *new_window = 0;    /* kludge city */
  391.  
  392.   end_pos = BUF_Z (b) - w->window_end_pos[CURRENT_DISP];
  393.   sb_pos = scrollbar_point (w, 0);
  394.   start_pos = sb_pos;
  395.  
  396.   /* The end position must be strictly greater than the start
  397.      position, at least for the Motify scrollbar.  It shouldn't hurt
  398.      anything for other scrollbar implementations. */
  399.   if (end_pos <= start_pos)
  400.     end_pos = start_pos + 1;
  401.  
  402.   if (vertical)
  403.     {
  404.       new_height = WINDOW_TEXT_HEIGHT (w);
  405.       new_width = scrollbar_width;
  406.     }
  407.   else
  408.     {
  409.       new_height = scrollbar_height;
  410.       new_width = WINDOW_TEXT_WIDTH (w);
  411.     }
  412.  
  413.   /* If the height and width are not greater than 0, then later on the
  414.      Motif widgets will bitch and moan. */
  415.   if (new_height <= 0)
  416.     new_height = 1;
  417.   if (new_width <= 0)
  418.     new_width = 1;
  419.  
  420.   assert (instance->mirror && XWINDOW (real_window(instance->mirror, 0)) == w);
  421.  
  422.   /* Only character-based scrollbars are implemented at the moment.
  423.      Line-based will be implemented in the future. */
  424.  
  425.   instance->scrollbar_is_active = 1;
  426.   new_line_increment = 1;
  427.   new_page_increment = 1;
  428.  
  429.   if (!DEVMETH_OR_GIVEN (d, inhibit_scrollbar_thumb_size_change, (), 0))
  430.     {
  431.       int x_offset, y_offset;
  432.  
  433.       /* Scrollbars are always the farthest from the text area. */
  434.       if (vertical)
  435.     {
  436.       x_offset = (f->scrollbar_on_left
  437.               ? WINDOW_LEFT (w)
  438.               : WINDOW_RIGHT (w) - scrollbar_width);
  439.       y_offset = WINDOW_TEXT_TOP (w) + f->scrollbar_y_offset;
  440.     }
  441.       else
  442.     {
  443.       x_offset = WINDOW_TEXT_LEFT (w);
  444.       y_offset = f->scrollbar_y_offset +
  445.         (f->scrollbar_on_top
  446.          ? WINDOW_TOP (w)
  447.          : WINDOW_TEXT_BOTTOM (w) + window_bottom_toolbar_height (w));
  448.     }
  449.  
  450.       new_x = x_offset;
  451.       new_y = y_offset;
  452.     }
  453.  
  454.   /* A disabled scrollbar has its slider sized to the entire height of
  455.      the scrollbar.  Currently the minibuffer scrollbar is
  456.      disabled. */
  457.   if (!MINI_WINDOW_P (w) && vertical)
  458.     {
  459.       if (!DEVMETH_OR_GIVEN (d, inhibit_scrollbar_thumb_size_change, (), 0))
  460.     {
  461.       new_minimum = BUF_BEGV (b);
  462.       new_maximum = max (BUF_ZV (b), new_minimum + 1);
  463.       new_slider_size = min ((end_pos - start_pos),
  464.                  (new_maximum - new_minimum));
  465.       new_slider_position = sb_pos;
  466.       new_window = w;
  467.     }
  468.     }
  469.   else if (!MINI_WINDOW_P (w))
  470.     {
  471.       /* The minus one is to account for the truncation glyph. */
  472.       int wcw = window_char_width (w, 0) - 1;
  473.       int max_width, max_slide;
  474.  
  475.       if (w->max_line_len < wcw)
  476.     {
  477.       max_width = 1;
  478.       max_slide = 1;
  479.       wcw = 1;
  480.     }
  481.       else
  482.     {
  483.       max_width = w->max_line_len + 2;
  484.       max_slide = max_width - wcw;
  485.     }
  486.  
  487.       new_minimum = 0;
  488.       new_maximum = max_width;
  489.       new_slider_size = wcw;
  490.       new_slider_position = min (w->hscroll, max_slide);
  491.     }
  492.   else
  493.     {
  494.       new_minimum = 1;
  495.       new_maximum = 2;
  496.       new_slider_size = 1;
  497.       new_slider_position = 1;
  498.       instance->scrollbar_is_active = 0;
  499.     }
  500.  
  501.   DEVMETH (d, update_scrollbar_instance_values, (w, instance,
  502.                          new_line_increment,
  503.                          new_page_increment,
  504.                          new_minimum,
  505.                          new_maximum,
  506.                          new_slider_size,
  507.                          new_slider_position,
  508.                          new_width, new_height,
  509.                          new_x, new_y));
  510. }
  511.  
  512. void
  513. init_frame_scrollbars (struct frame *f)
  514. {
  515.   struct device *d = XDEVICE (f->device);
  516.  
  517.   if (HAS_DEVMETH_P (d, create_scrollbar_instance))
  518.     {
  519.       Lisp_Object frame = Qnil;
  520.  
  521.       XSETFRAME (frame, f);
  522.       call_critical_lisp_code (XDEVICE (FRAME_DEVICE (f)),
  523.                    Qinit_scrollbar_from_resources,
  524.                    frame);
  525.     }
  526. }
  527.  
  528. void
  529. init_device_scrollbars (struct device *d)
  530. {
  531.   if (HAS_DEVMETH_P (d, create_scrollbar_instance))
  532.     {
  533.       Lisp_Object device = Qnil;
  534.  
  535.       XSETDEVICE (device, d);
  536.       call_critical_lisp_code (d,
  537.                    Qinit_scrollbar_from_resources,
  538.                    device);
  539.     }
  540. }
  541.  
  542. void
  543. init_global_scrollbars (struct device *d)
  544. {
  545.   if (HAS_DEVMETH_P (d, create_scrollbar_instance))
  546.     {
  547.       call_critical_lisp_code (d,
  548.                    Qinit_scrollbar_from_resources,
  549.                    Qglobal);
  550.     }
  551. }
  552.  
  553.  
  554. /* This function is called as a result of a change to the
  555.    `scrollbar-width' specifier. */
  556. static void
  557. scrollbar_width_changed_in_frame (Lisp_Object specifier, struct frame *f,
  558.                   Lisp_Object oldval)
  559. {
  560.   struct device *d = XDEVICE (f->device);
  561.  
  562.   MAYBE_DEVMETH (d, scrollbar_width_changed_in_frame, (specifier, f, oldval));
  563. }
  564.  
  565. /* This function is called as a result of a change to the
  566.    `scrollbar-height' specifier.  */
  567. static void
  568. scrollbar_height_changed_in_frame (Lisp_Object specifier, struct frame *f,
  569.                    Lisp_Object oldval)
  570. {
  571.   struct device *d = XDEVICE (f->device);
  572.  
  573.   MAYBE_DEVMETH (d, scrollbar_height_changed_in_frame, (specifier, f, oldval));
  574. }
  575.  
  576. /* ####
  577.  
  578.    All of the following stuff is functions that handle scrollbar
  579.    actions.  All of it should be moved into Lisp.  This may require
  580.    adding some badly-needed primitives. */
  581.  
  582. /********** vertical scrollbar stuff **********/
  583.  
  584. /*
  585.  * If the original point is still visible, put the cursor back there.
  586.  * Otherwise, when scrolling down stick it at the beginning of the
  587.  * first visible line and when scrolling up stick it at the beginning
  588.  * of the last visible line.
  589.  */
  590.  
  591. /* #### This function should be moved into Lisp */
  592. static void
  593. scrollbar_reset_cursor (Lisp_Object win, Lisp_Object orig_pt)
  594. {
  595.   /* When this function is called we know that start is already
  596.      accurate.  We know this because either set-window-start or
  597.      recenter was called immediately prior to it being called. */
  598.   Lisp_Object buf;
  599.   Bufpos start_pos = XINT (Fwindow_start (win));
  600.   Bufpos ptint = XINT (orig_pt);
  601.   struct window *w = XWINDOW (win);
  602.   int selected = ((w == XWINDOW (Fselected_window (XFRAME (w->frame)->device)))
  603.           ? 1
  604.           : 0);
  605.  
  606.   buf = Fwindow_buffer (win);
  607.   if (NILP (buf))
  608.     return;    /* the window was deleted out from under us */
  609.  
  610.   if (ptint < XINT (Fwindow_start (win)))
  611.     {
  612.       if (selected)
  613.     Fgoto_char (make_number (start_pos), buf);
  614.       else
  615.     Fset_window_point (win, make_number (start_pos));
  616.     }
  617.   else if (!point_would_be_visible (XWINDOW (win), start_pos, ptint))
  618.     {
  619.       Fmove_to_window_line (make_number (-1), win);
  620.  
  621.       if (selected)
  622.     Fbeginning_of_line (Qnil, buf);
  623.       else
  624.     {
  625.       /* #### Taken from forward-line. */
  626.       Bufpos pos;
  627.  
  628.       pos = find_next_newline (XBUFFER (buf),
  629.                    marker_position (w->pointm[CURRENT_DISP]),
  630.                    -1);
  631.       Fset_window_point (win, make_number (pos));
  632.     }
  633.     }
  634.   else
  635.     {
  636.       if (selected)
  637.     Fgoto_char (orig_pt, buf);
  638.       else
  639.     Fset_window_point (win, orig_pt);
  640.     }
  641. }
  642.  
  643. DEFUN ("scrollbar-line-up", Fscrollbar_line_up, Sscrollbar_line_up, 1, 1, 0,
  644.   "Function called when the line-up arrow on the scrollbar is clicked.\n\
  645. This is the little arrow at the top of the scrollbar.  One argument, the\n\
  646. scrollbar's window.  You can advise this function to change the scrollbar\n\
  647. behavior.")
  648.      (window)
  649.      Lisp_Object window;
  650. {
  651.   CHECK_LIVE_WINDOW (window, 0);
  652.   window_scroll (window, make_number (1), -1, 1);
  653.   zmacs_region_stays = 1;
  654.   return Qnil;
  655. }
  656.  
  657. DEFUN ("scrollbar-line-down", Fscrollbar_line_down, Sscrollbar_line_down,
  658.        1, 1, 0,
  659.   "Function called when the line-down arrow on the scrollbar is clicked.\n\
  660. This is the little arrow at the bottom of the scrollbar.  One argument, the\n\
  661. scrollbar's window.  You can advise this function to change the scrollbar\n\
  662. behavior.")
  663.      (window)
  664.      Lisp_Object window;
  665. {
  666.   CHECK_LIVE_WINDOW (window, 0);
  667.   window_scroll (window, make_number (1), 1, 1);
  668.   zmacs_region_stays = 1;
  669.   return Qnil;
  670. }
  671.  
  672. DEFUN ("scrollbar-page-up", Fscrollbar_page_up, Sscrollbar_page_up,
  673.        1, 1, 0,
  674.   "Function called when the user gives the \"page-up\" scrollbar action.\n\
  675. (The way this is done can vary from scrollbar to scrollbar.) One argument,\n\
  676. a cons containing the scrollbar's window and a value (#### document me!\n\
  677. This value is nil for Motif/Lucid scrollbars and a number for Athena\n\
  678. scrollbars).  You can advise this function to change the scrollbar\n\
  679. behavior.")
  680.      (object)
  681.      Lisp_Object object;
  682. {
  683.   Lisp_Object window = Fcar (object);
  684.  
  685.   CHECK_LIVE_WINDOW (window, 0);
  686.   /* Motif and Athena scrollbars behave differently, but in accordance
  687.      with their standard behaviors.  It is not possible to hide the
  688.      differences down in lwlib because knowledge of XEmacs buffer and
  689.      cursor motion routines is necessary. */
  690. #if defined (LWLIB_SCROLLBARS_MOTIF) || defined (LWLIB_SCROLLBARS_LUCID)
  691.   window_scroll (window, Qnil, -1, 1);
  692. #else /* Athena */
  693.   {
  694.     Bufpos bufpos;
  695.     Lisp_Object value = Fcdr (object);
  696.  
  697.     CHECK_INT (value, 0);
  698.     Fmove_to_window_line (Qzero, window);
  699.     /* can't use Fvertical_motion() because it moves the buffer point
  700.        rather than the window's point.
  701.  
  702.        #### It does?  Why does it take a window argument then? */
  703.     bufpos = vmotion (XWINDOW (window), XINT (Fwindow_point (window)),
  704.               XINT (value), 0);
  705.     Fset_window_point (window, make_number (bufpos));
  706.     Frecenter (Qzero, window);
  707.   }
  708. #endif /* Athena */
  709.   zmacs_region_stays = 1;
  710.   return Qnil;
  711. }
  712.  
  713. DEFUN ("scrollbar-page-down", Fscrollbar_page_down, Sscrollbar_page_down,
  714.        1, 1, 0,
  715.   "Function called when the user gives the \"page-down\" scrollbar action.\n\
  716. (The way this is done can vary from scrollbar to scrollbar.) One argument,\n\
  717. a cons containing the scrollbar's window and a value (#### document me!\n\
  718. This value is nil for Motif/Lucid scrollbars and a number for Athena\n\
  719. scrollbars).  You can advise this function to change the scrollbar\n\
  720. behavior.")
  721.      (object)
  722.      Lisp_Object object;
  723. {
  724.   Lisp_Object window = Fcar (object);
  725.  
  726.   CHECK_LIVE_WINDOW (window, 0);
  727.   /* Motif and Athena scrollbars behave differently, but in accordance
  728.      with their standard behaviors.  It is not possible to hide the
  729.      differences down in lwlib because knowledge of XEmacs buffer and
  730.      cursor motion routines is necessary. */
  731. #if defined (LWLIB_SCROLLBARS_MOTIF) || defined (LWLIB_SCROLLBARS_LUCID)
  732.   window_scroll (window, Qnil, 1, 1);
  733. #else /* Athena */
  734.   {
  735.     Lisp_Object value = Fcdr (object);
  736.     CHECK_INT (value, 0);
  737.     Fmove_to_window_line (value, window);
  738.     Frecenter (Qzero, window);
  739.   }
  740. #endif /* Athena */
  741.   zmacs_region_stays = 1;
  742.   return Qnil;
  743. }
  744.  
  745. DEFUN ("scrollbar-to-top", Fscrollbar_to_top, Sscrollbar_to_top,
  746.        1, 1, 0,
  747.   "Function called when the user gives the \"to-top\" scrollbar action.\n\
  748. (The way this is done can vary from scrollbar to scrollbar.). One argument,\n\
  749. the scrollbar's window.  You can advise this function to change the\n\
  750. scrollbar behavior.")
  751.      (window)
  752.      Lisp_Object window;
  753. {
  754.   Lisp_Object orig_pt;
  755.  
  756.   orig_pt = Fwindow_point (window);
  757.   Fset_window_point (window, Fpoint_min (Fwindow_buffer (window)));
  758.   Frecenter (Qzero, window);
  759.   scrollbar_reset_cursor (window, orig_pt);
  760.   zmacs_region_stays = 1;
  761.   return Qnil;
  762. }
  763.  
  764. DEFUN ("scrollbar-to-bottom", Fscrollbar_to_bottom, Sscrollbar_to_bottom,
  765.        1, 1, 0,
  766.   "Function called when the user gives the \"to-bottom\" scrollbar action.\n\
  767. (The way this is done can vary from scrollbar to scrollbar.). One argument,\n\
  768. the scrollbar's window.  You can advise this function to change the\n\
  769. scrollbar behavior.")
  770.      (window)
  771.      Lisp_Object window;
  772. {
  773.   Lisp_Object orig_pt;
  774.  
  775.   orig_pt = Fwindow_point (window);
  776.   Fset_window_point (window, Fpoint_max (Fwindow_buffer (window)));
  777.   Frecenter (Qzero, window);
  778.   scrollbar_reset_cursor (window, orig_pt);
  779.   zmacs_region_stays = 1;
  780.   return Qnil;
  781. }
  782.  
  783. DEFUN ("scrollbar-vertical-drag", Fscrollbar_vertical_drag,
  784.        Sscrollbar_vertical_drag, 1, 1, 0,
  785.   "Function called when the user drags the vertical scrollbar thumb.\n\
  786. One argument, a cons containing the scrollbar's window and a value\n\
  787. (#### document me!).  You can advise this function to change the\n\
  788. scrollbar behavior.")
  789.      (object)
  790.      Lisp_Object object;
  791. {
  792.   Bufpos start_pos;
  793.   Lisp_Object orig_pt;
  794.   Lisp_Object window = Fcar (object);
  795.   Lisp_Object value = Fcdr (object);
  796.  
  797.   orig_pt = Fwindow_point (window);
  798.   Fset_marker (XWINDOW (window)->sb_point, value, Fwindow_buffer (window));
  799.   start_pos = scrollbar_point (XWINDOW (window), 1);
  800.   Fset_window_start (window, make_number (start_pos), Qnil);
  801.   scrollbar_reset_cursor (window, orig_pt);
  802.   zmacs_region_stays = 1;
  803.   return Qnil;
  804. }
  805.  
  806. DEFUN ("scrollbar-set-hscroll", Fscrollbar_set_hscroll, Sscrollbar_set_hscroll,
  807.        2, 2, 0,
  808.   "Sets WINDOW's hscroll position to VALUE.\n\
  809. This ensures that VALUE is in the proper range for the horizontal scrollbar.")
  810.     (window, value)
  811.     Lisp_Object window, value;
  812. {
  813.   struct window *w;
  814.   int hscroll, wcw, max_len;
  815.  
  816.   CHECK_LIVE_WINDOW (window, 0);
  817.   if (!EQ (value, Qmax))
  818.     CHECK_INT (value, 0);
  819.  
  820.   w = XWINDOW (window);
  821.   wcw = window_char_width (w, 0) - 1;
  822.   max_len = w->max_line_len + 1;
  823.  
  824.   if (EQ (value, Qmax) || (XINT (value) > (max_len - wcw)))
  825.     hscroll = max_len - wcw;
  826.   else
  827.     hscroll = XINT (value);
  828.  
  829.   /* Can't allow this out of set-window-hscroll's acceptable range. */
  830.   if (hscroll < 0)
  831.     hscroll = 0;
  832.   else if (hscroll >= (1 << (SHORTBITS - 1)))
  833.     hscroll = (1 << (SHORTBITS - 1)) - 1;
  834.  
  835.   if (hscroll != w->hscroll)
  836.     Fset_window_hscroll (window, make_number (hscroll));
  837.  
  838.   return Qnil;
  839. }
  840.  
  841.  
  842. /************************************************************************/
  843. /*                            initialization                            */
  844. /************************************************************************/
  845.  
  846. void
  847. syms_of_scrollbar (void)
  848. {
  849.   defsymbol (&Qscrollbar_line_up, "scrollbar-line-up");
  850.   defsymbol (&Qscrollbar_line_down, "scrollbar-line-down");
  851.   defsymbol (&Qscrollbar_page_up, "scrollbar-page-up");
  852.   defsymbol (&Qscrollbar_page_down, "scrollbar-page-down");
  853.   defsymbol (&Qscrollbar_to_top, "scrollbar-to-top");
  854.   defsymbol (&Qscrollbar_to_bottom, "scrollbar-to-bottom");
  855.   defsymbol (&Qscrollbar_vertical_drag, "scrollbar-vertical-drag");
  856.   
  857.   defsymbol (&Qscrollbar_char_left, "scrollbar-char-left");
  858.   defsymbol (&Qscrollbar_char_right, "scrollbar-char-right");
  859.   defsymbol (&Qscrollbar_page_left, "scrollbar-page-left");
  860.   defsymbol (&Qscrollbar_page_right, "scrollbar-page-right");
  861.   defsymbol (&Qscrollbar_to_left, "scrollbar-to-left");
  862.   defsymbol (&Qscrollbar_to_right, "scrollbar-to-right");
  863.   defsymbol (&Qscrollbar_horizontal_drag, "scrollbar-horizontal-drag");
  864.  
  865.   defsymbol (&Qinit_scrollbar_from_resources, "init-scrollbar-from-resources");
  866.  
  867.   /* #### All these functions should be moved into Lisp.
  868.      See comment above. */
  869.   defsubr (&Sscrollbar_line_up);
  870.   defsubr (&Sscrollbar_line_down);
  871.   defsubr (&Sscrollbar_page_up);
  872.   defsubr (&Sscrollbar_page_down);
  873.   defsubr (&Sscrollbar_to_top);
  874.   defsubr (&Sscrollbar_to_bottom);
  875.   defsubr (&Sscrollbar_vertical_drag);
  876.  
  877.   defsubr (&Sscrollbar_set_hscroll);
  878. }
  879.  
  880. void
  881. specifier_vars_of_scrollbar (void)
  882. {
  883.   DEFVAR_SPECIFIER ("scrollbar-width", &Vscrollbar_width,
  884.     "*Width of vertical scrollbars.\n\
  885. This is a specifier; use `set-specifier' to change it.");
  886.   Vscrollbar_width = Fmake_specifier (Qnatnum);
  887.   set_specifier_fallback
  888.     (Vscrollbar_width,
  889.      list1 (Fcons (Qnil, make_number (DEFAULT_SCROLLBAR_WIDTH))));
  890.   set_specifier_caching (Vscrollbar_width,
  891.              slot_offset (struct window,
  892.                       scrollbar_width),
  893.              some_window_value_changed,
  894.              slot_offset (struct frame,
  895.                       scrollbar_width),
  896.              scrollbar_width_changed_in_frame);
  897.  
  898.   DEFVAR_SPECIFIER ("scrollbar-height", &Vscrollbar_height,
  899.     "*Width of vertical scrollbars.\n\
  900. This is a specifier; use `set-specifier' to change it.");
  901.   Vscrollbar_height = Fmake_specifier (Qnatnum);
  902.   set_specifier_fallback
  903.     (Vscrollbar_height,
  904.      list1 (Fcons (Qnil, make_number (DEFAULT_SCROLLBAR_HEIGHT))));
  905.   set_specifier_caching (Vscrollbar_height,
  906.              slot_offset (struct window,
  907.                       scrollbar_height),
  908.              some_window_value_changed,
  909.              slot_offset (struct frame,
  910.                       scrollbar_height),
  911.              scrollbar_height_changed_in_frame);
  912. }
  913.