Author Topic: custom scroll wheel macro  (Read 8088 times)

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
custom scroll wheel macro
« on: June 20, 2019, 11:32:02 PM »
This macro code has commands for the wheel-up wheel-down events that I first posted here
https://community.slickedit.com/index.php/topic,16659.0.html

Wheel up/down events occur when you scroll the mouse wheel - slick allows you to bind your own commands to them.  These macros move the actual cursor position when you scroll the mouse wheel, as asked for in the other thread.  I've now added an event loop so as to allow a press of the ESC key to put the cursor position back to where you started before you scrolled the mouse wheel.  There is a command toggle_xscroll that swaps between the normal keybinding for wheel-up wheel-down (fast_scroll) and the new commands.  If you click the right mouse button when the event loop is active, the command toggle_xscroll is called to swap the keybindings i.e. to swap back to the normal keybindings. 

The main reason I did this was to allow my xretrace custom scrollbar to update the scrollbar position when the mouse wheel is scrolled.  For my own use I also manually bind the Ctrl-wheel-up/down events to xscroll-up/down events.  Holding the control key down while scrolling the mouse wheel makes it scroll faster.  With the normal keybindings, holding the shift key down makes it scroll faster.

If you run slick diff, you have to swap back to normal keybindings first because the diff tool complains about the custom keybindings for wheel-up/down.

To load the code, save it to a macro file and use the load module command on the macro menu.  To unload, use the unload command in the same menu.

Code: [Select]

#include "slick.sh"


#pragma option(strictsemicolons,on)
#pragma option(strict,on)
#pragma option(autodecl,off)
#pragma option(strictparens,on)


int def_scroll_up_with_cursor;
static int scroll_up_with_cursor_key_bindings;
bool block_scroll_flag;

void my_scroll_callback()
{
   block_scroll_flag = false;
}


void xscroll(bool is_up)
{
   bool xrs = false;
   bool try_call_key = false;
   _str ev;
   if ( block_scroll_flag ) {
      return;
   }

   if ( find_index('delete_xbar_windows', COMMAND_TYPE) != 0 ) {
      xrs = true;
   }

   if ( def_scroll_up_with_cursor && !_IsKeyDown(SHIFT) || _IsKeyDown(CTRL) ) {
      if ( p_window_id != null && _iswindow_valid(p_window_id) && p_window_id._isEditorCtl()) {

         if ( substr(p_window_id.p_buf_name, 1, 1) == "." ) {
            fast_scroll();
            return;
         }
         if ( p_window_id != _get_focus() ) {
            p_window_id._set_focus();
         }

         if (p_window_id.p_scroll_left_edge >= 0)
             p_window_id.p_scroll_left_edge = -1;

         int p2;
         save_pos(p2);
         if ( _IsKeyDown(CTRL) )
         {
            if ( is_up )
               cursor_up(8);
            else
               cursor_down(8);
         }
         else
         {
            if ( is_up )
               cursor_up();
            else
               cursor_down();
         }

         mou_mode(2);
         mou_capture();

         while ( 1 ) {
            ev = get_event('k');
            //say(event2name(ev));
            if ( xrs )
               check_update_xretrace_scrollbar();
            switch( ev ) {
            default:
               try_call_key = true;
               break;
            case RBUTTON_DOWN :
               toggle_xscroll();
               break;
            case ESC :
               restore_pos(p2);
               break;
            case ON_KEYSTATECHANGE :
            case MOUSE_MOVE :
               continue;
            case WHEEL_UP :
               if ( _IsKeyDown(CTRL) )
                  cursor_up(8);
               else
                  cursor_up();
               continue;

            case WHEEL_DOWN :
               if ( _IsKeyDown(CTRL) )
                  cursor_down(8);
               else
                  cursor_down();
               continue;

            }
            mou_mode(0);
            mou_release();
            break;
         }
         //center_line();
         block_scroll_flag = true;
         _set_timer(500, my_scroll_callback);
         if ( try_call_key ) {
            call_key(ev);
         }
         return;
      }
   }
   fast_scroll();
}


_command void xscroll_up() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   xscroll(true);
}


_command void xscroll_down() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   xscroll(false);
}

// https://devdocs.io/cpp/

// http://www.google.com/search?q=memcmp&as_sitesearch=cplusplus.com&btnI



_command void toggle_xscroll() name_info(',')
{
   if ( def_scroll_up_with_cursor == 0 ) {
      if (_message_box('Enable scroll with cursor', "", MB_YESNO) != IDYES)  {
         message("Cursor scrolling is disabled");
         return;
      }
      def_scroll_up_with_cursor = 1;
      scroll_up_with_cursor_key_bindings = 1;
   }
   scroll_up_with_cursor_key_bindings = (int)!scroll_up_with_cursor_key_bindings;

   if ( scroll_up_with_cursor_key_bindings ) {
      execute('bind-to-key -r fast_scroll 'event2index(name2event('WHEEL-UP')),"");
      execute('bind-to-key -r fast_scroll 'event2index(name2event('WHEEL-DOWN')),"");
      message("Bind to fast-scroll");
   }
   else {
      execute('bind-to-key -r xscroll_up 'event2index(name2event('WHEEL-UP')),"");
      execute('bind-to-key -r xscroll_down 'event2index(name2event('WHEEL-DOWN')),"");
      message("Bind to my-scroll");
   }
}



niexin.liu

  • Community Member
  • Posts: 17
  • Hero Points: 0
Re: custom scroll wheel macro
« Reply #1 on: February 12, 2022, 08:21:43 AM »
Thank you very much.
xretrace is a very good plugin.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: custom scroll wheel macro
« Reply #2 on: February 12, 2022, 09:36:13 AM »
Thanks.  This macro actually comes with xretrace so you probably don't need to load both of them.  I changed some names slightly.

Code: [Select]
int xretrace_def_scroll_up_with_cursor;

static int scroll_up_with_cursor_key_bindings;
static bool block_scroll_flag;

void xretrace_scroll_callback()
{
   block_scroll_flag = false;
}


static void xscroll(bool is_up)
{
   bool xrs = false;
   bool try_call_key = false;
   _str ev;
   if ( block_scroll_flag ) {
      return;
   }

   if ( find_index('xretrace_delete_scrollbar_windows', COMMAND_TYPE) != 0 ) {
      xrs = true;
   }

   if ( xretrace_def_scroll_up_with_cursor && !_IsKeyDown(SHIFT) || _IsKeyDown(CTRL) ) {
      if ( p_window_id != null && _iswindow_valid(p_window_id) && p_window_id._isEditorCtl()) {

         if ( substr(p_window_id.p_buf_name, 1, 1) == "." ) {
            fast_scroll();
            return;
         }
         if ( p_window_id != _get_focus() ) {
            p_window_id._set_focus();
         }

         if (p_window_id.p_scroll_left_edge >= 0)
             p_window_id.p_scroll_left_edge = -1;

         int p2;
         save_pos(p2);
         if ( _IsKeyDown(CTRL) )
         {
            if ( is_up )
               cursor_up(8);
            else
               cursor_down(8);
         }
         else
         {
            if ( is_up )
               cursor_up();
            else
               cursor_down();
         }

         mou_mode(2);
         mou_capture();

         while ( 1 ) {
            ev = get_event('k');
            //say(event2name(ev));
            if ( xrs )
               check_update_xretrace_scrollbar();
            switch( ev ) {
            default:
               try_call_key = true;
               break;
            case RBUTTON_DOWN :
               xretrace_toggle_xscroll();
               break;
            case ESC :
               restore_pos(p2);
               break;
            case ON_KEYSTATECHANGE :
            case MOUSE_MOVE :
               continue;
            case WHEEL_UP :
               if ( _IsKeyDown(CTRL) )
                  cursor_up(8);
               else
                  cursor_up();
               continue;

            case WHEEL_DOWN :
               if ( _IsKeyDown(CTRL) )
                  cursor_down(8);
               else
                  cursor_down();
               continue;

            }
            mou_mode(0);
            mou_release();
            break;
         }
         //center_line();
         block_scroll_flag = true;
         _set_timer(500, xretrace_scroll_callback);
         if ( try_call_key ) {
            call_key(ev);
         }
         return;
      }
   }
   fast_scroll();
}


_command void xretrace_scroll_up() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   xscroll(true);
}


_command void xretrace_scroll_down() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
   xscroll(false);
}

// https://devdocs.io/cpp/

// http://www.google.com/search?q=memcmp&as_sitesearch=cplusplus.com&btnI



_command void xretrace_toggle_xscroll(bool force_xscroll_off = false) name_info(',')
{
   if ( force_xscroll_off ) {
      execute('bind-to-key -r fast_scroll 'event2index(name2event('WHEEL-UP')),"");
      execute('bind-to-key -r fast_scroll 'event2index(name2event('WHEEL-DOWN')),"");
      return;
   }
   if ( xretrace_def_scroll_up_with_cursor == 0 ) {
      if (_message_box('Enable scroll with cursor', "", MB_YESNO) != IDYES)  {
         message("Cursor scrolling is disabled");
         return;
      }
      // xretrace_def_scroll_up_with_cursor is don't care if the keybindings
      xretrace_def_scroll_up_with_cursor = 1;
      scroll_up_with_cursor_key_bindings = 1;  // bind to xscroll
   }
   scroll_up_with_cursor_key_bindings = (int)!scroll_up_with_cursor_key_bindings;

   if ( scroll_up_with_cursor_key_bindings ) {
      execute('bind-to-key -r fast_scroll 'event2index(name2event('WHEEL-UP')),"");
      execute('bind-to-key -r fast_scroll 'event2index(name2event('WHEEL-DOWN')),"");
      message("Bind to fast-scroll");
   }
   else {
      execute('bind-to-key -r xretrace_scroll_up 'event2index(name2event('WHEEL-UP')),"");
      execute('bind-to-key -r xretrace_scroll_down 'event2index(name2event('WHEEL-DOWN')),"");
      message("Bind to xscroll");
   }
}