Author Topic: How to tell if the cursor is visible in editor window  (Read 4319 times)

MindprisM

  • Senior Community Member
  • Posts: 126
  • Hero Points: 8
How to tell if the cursor is visible in editor window
« on: September 25, 2013, 09:53:10 am »
SE 17

I would expect p_cursor_y to return something I could compare to p_client_height, but for me it gives 0 when the cursor is at top of window, and 0 beyond that. Also, if the cursor has exceeded window bottom, it still is returning something less than p_client_height.
Actually, there are times when p_cursor_y returns positive numbers when it has exceeded client top. It has something to do with whether I adjust the window via mouse (indeterminate) or keyboard.

I need a reliable way to determine if cursor is in client on the y-axis, and a reliable way to determine the first visible line in the client, since this code is not reliable either when the cursor is not in client:

Code: [Select]
        void kt_get_topbot(int &top_,int &bot_){
          int cy=p_cursor_y;
          p_cursor_y=0;
          top_=p_line;
          p_cursor_y=(p_client_height-1);
          bot_=p_line;
          p_cursor_y=cy;
        }

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: How to tell if the cursor is visible in editor window
« Reply #1 on: September 25, 2013, 05:52:46 pm »
[EDITED]  I wasn't able to understand the question until I saw the post about the kt macro.  You want to know how to get the window scroll position.  All the other details are computed from the window scroll position.  I've spent hours trying to find a way to get the window's vertical scroll position, and I've not found how.  It's been a feature request for a long time, it's unfortunate if there's no way for macros to accurately assess the window state while scrolled vertically.
« Last Edit: September 25, 2013, 06:03:38 pm by chrisant »

MindprisM

  • Senior Community Member
  • Posts: 126
  • Hero Points: 8
Re: How to tell if the cursor is visible in editor window
« Reply #2 on: September 26, 2013, 08:38:26 am »
Thanks for your feedback. Rabbit holes are so dangerous - it is good to have someone say "can't be done". If you have time, maybe you can answer another question.

In the kt macro, I am creating many tip forms. I got it hooked to a timer now so it doesn't clobber the ui usability on load (se startup). The thing is, when a form is created via _load_template(kt_formindex, _mdi, "N"); .... it steals the focus from the se main window. I try to rectify this by capturing wid and restoring it to p_window_id, and then calling _set_focus(); but that doesn't quite focus the _mdi.p_child, but if I also do _mdi.p_child._set_focus() this makes slick grab focus when you are focused on another app, which is no good because the timers take a while to complete all their creation of tip forms.

I am new to forms, and I am not sure how to persist/autoload a form (like toolbars do), or even if this would work with a borderless form, or if it would be a good idea since there are so many of them. I am guessing that if they are persisted, then I would have to make a unique form for each, opposed to 'newing' them as I do now.

Back to the cursor in window problem, I was trying to figure out how it was done by looking at the 'up()' command, as in cursor-up (up arrow) ... since that will yank the view so that the cursor is visible, but it must be happening on the exe side because I do not see any detection code in the macro.

Graeme

  • Senior Community Member
  • Posts: 2136
  • Hero Points: 263
Re: How to tell if the cursor is visible in editor window
« Reply #3 on: September 26, 2013, 12:58:32 pm »
This code seems to set the "edit cursor" position to so that the cursor is actually in view, regardless of where the mouse cursor is on the screen and without scrolling the window.  So you could capture the current p_col and p_line values, call mou_click(), then figure out what the first and last visible lines are and decide whether the cursor was previously in view or not.  If it was you could restore it to where it was (perhaps).  You could also see the post by Clark here
http://community.slickedit.com/index.php/topic,3313.0/nowap.html
download mouse.e and search for def_panning  - but it probably doesn't help; just a thought.

_command void tgp1() name_info(',')
{
   mou_click();
   deselect();
}

MindprisM

  • Senior Community Member
  • Posts: 126
  • Hero Points: 8
Re: How to tell if the cursor is visible in editor window
« Reply #4 on: September 26, 2013, 01:55:45 pm »
In hacking fast_scroll() to use scroll_up() and scroll_down() instead of _scroll_page(<u|d>,1)

-- thinking that if I did that the only way to get the cursor out of view would be to actually use the scrollbar but using the wheel would always keep it in view --

I found out that scroll_<up|down>() doesn't work when cursor is not in view. In other words p_line doesn't change, so that is one way of determining if cursor is in view.

Does anyone have a skeleton slickedit 64 bit dll project for visual studio 2010? The scroll position could be gotten that way.

Graeme

  • Senior Community Member
  • Posts: 2136
  • Hero Points: 263
Re: How to tell if the cursor is visible in editor window
« Reply #5 on: September 26, 2013, 02:14:46 pm »
scroll_up won't change p_line when the cursor is in view either, unless it is going to move out of view.

MindprisM

  • Senior Community Member
  • Posts: 126
  • Hero Points: 8
Re: How to tell if the cursor is visible in editor window
« Reply #6 on: September 27, 2013, 07:49:17 pm »
Maybe I mean p_cursor_y :)

Btw, is there a way to preserve the selection during scroll_down and up? I am surprised to find that it goes away, definitely not CUA.

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: How to tell if the cursor is visible in editor window
« Reply #7 on: September 28, 2013, 07:27:04 am »
Maybe I mean p_cursor_y :)

Btw, is there a way to preserve the selection during scroll_down and up? I am surprised to find that it goes away, definitely not CUA.
save_selection

MindprisM

  • Senior Community Member
  • Posts: 126
  • Hero Points: 8
Re: How to tell if the cursor is visible in editor window
« Reply #8 on: September 28, 2013, 08:44:22 pm »
Yea, that doesn't seem to work:

Code: [Select]
_command void scroll_down() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL)
{
  #if 1
   int hadsel=0;
   _str old_mark='';
   _str markid='';
   if (select_active(markid)) {

     int ss=save_selection(old_mark);
     //say('hadsel:'ss);
     hadsel=1;
     //_str mark=_duplicate_selection('');
     //_deselect(mark);
   }
  #endif

   boolean last_line=(p_line==p_Noflines);
   _str old_scroll=_scroll_style();
   _scroll_style('S 0');
   int cursor_y= p_cursor_y;
   int left_edge= p_left_edge;
   int last=last_index('','C');
   int old_updown_col=def_updown_col;def_updown_col=0;
   cursor_down();
   def_updown_col=old_updown_col;
   last_index(last,'C');
   if ( rc==BOTTOM_OF_FILE_RC ) {
      cursor_y=cursor_y-1;
   }
   set_scroll_pos(left_edge,cursor_y);
   top_of_window();
   int top_cursor_y=p_cursor_y;
   p_cursor_y=cursor_y;
   if (p_cursor_y!=top_cursor_y) {
      cursor_up();
   }
   if (last_line) {
      cursor_down();
   }
   _scroll_style(old_scroll);
   _set_scroll_optimization(1);
   #if 1
   if (hadsel) {
     //say('hadsel back');
     restore_selection(old_mark);
   }
   #endif
}

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: How to tell if the cursor is visible in editor window
« Reply #9 on: September 29, 2013, 07:24:40 pm »
If you're saying that save_selection isn't working, then maybe it isn't being used correctly, because it works in all of my many macros that use it, even when the view is scrolled.

I noticed that save_selection is only called in the posted code after calling select_active.  I think the code may be over-complicated, and that may be causing problems.  Try using it as follows instead.

Code: [Select]
typeless ss;
save_selection(ss);
_deselect(); // if you want to clear the selection do this, but don't specify which mark
... other code ...
restore_selection(ss);

The conditionals and other stuff aren't needed, the save/restore functions handle the special cases for you.

MindprisM

  • Senior Community Member
  • Posts: 126
  • Hero Points: 8
Re: How to tell if the cursor is visible in editor window
« Reply #10 on: September 30, 2013, 11:03:53 am »
Yes, I can't even get save_selection, restore_selection to work raw. Im on SE17. Try this code:

Code: [Select]
typeless old_mark=-1;
int ss=save_selection(old_mark);
say(ss);//will be 0, success;
_deselect();//aka deselect();
//do a lot of nothing right here... then
restore_selection(old_mark);// result is no selection restored

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: How to tell if the cursor is visible in editor window
« Reply #11 on: September 30, 2013, 05:29:33 pm »
I don't know what to suggest next.  That code works fine for me on SE 14, 15, 16, and 17.  I haven't upgraded to SE 18 yet, I'm waiting for certain things that are coming in 18.0.2 if I understand correctly.

MindprisM

  • Senior Community Member
  • Posts: 126
  • Hero Points: 8
Re: How to tell if the cursor is visible in editor window
« Reply #12 on: October 01, 2013, 03:30:03 pm »
Thanks for the sanity check Chrisant. I wonder what I have wrong in my settings. I will recheck the next time I do a virgin boot of SE. Did you check on 17 64-bit? My settings are not so off-base, but I have recompiled a lot of standard .e -- about a dozen. I don't see how that would be it though since these two calls are so close to core.

MindprisM

  • Senior Community Member
  • Posts: 126
  • Hero Points: 8
Re: How to tell if the cursor is visible in editor window
« Reply #13 on: October 01, 2013, 03:42:36 pm »
Got it now.
Code: [Select]
_command void scroll_up() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)
_command void scroll_down() name_info(','VSARG2_READ_ONLY|VSARG2_REQUIRES_EDITORCTL|VSARG2_MARK)

The source in util.e was missing VSARG2_MARK. With it, there is no need for save/restore code.

chrisant

  • Senior Community Member
  • Posts: 1413
  • Hero Points: 131
Re: How to tell if the cursor is visible in editor window
« Reply #14 on: October 01, 2013, 04:59:10 pm »
And without it, the command clears the selection before invoking the command code, which is why saving and restoring "doesn't work".  By the way, stepping through code in the macro debugger or use messageNwait() can be a great way to diagnose stuff.  In this case it would have revealed that the selection was already gone before the code reached save_selection.