Author Topic: Centre text on open  (Read 1133 times)

bogedo

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: Centre text on open
« Reply #15 on: March 16, 2021, 12:11:10 am »

Quote
in total there should be two windows(is it even possible to accomplish this in one window ?) with multiple edit tabs. the default layout window with three tabs(.ini, .txt, .xml) and the minimal layout window with two tabs(.html and .html)

I can't tell what you mean by "window" or what you mean by "open a file".


apologies if i was not clear enough, by window i mean the SlickEdit program window so when i was talking about two windows i meant two SlickEdit instances and by "open a file" i meant opening a file in SlickEdit by double clicking it in Windows Explorer.


What setting do you have for the "one file per window" option
tools -> options -> editing -> editor window -> files per window.
I don't really know what this option does any more but it might make a difference to you.


it is the default "One file per window" though the description is not clear in what the selection is for.

Hopefully the clarification makes clear what i'd like to accomplish. Thank you for the sample code and the advice will try it out and see if it works out.

Graeme

  • Senior Community Member
  • Posts: 2518
  • Hero Points: 327
Re: Centre text on open
« Reply #16 on: March 16, 2021, 01:50:47 am »
ok, let me know if switchbuf will work for you.  If not, multiple instances might be possible using DDE.

bogedo

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: Centre text on open
« Reply #17 on: March 18, 2021, 10:52:41 pm »
got it to work when opening files from file explorer with this code:

Code: [Select]
void _buffer_add_xhz_center_text()
{
   if ( !(get_extension(p_buf_name) == "html")
     load_named_layout("default");
   else{
      load_named_layout("minimal");
      select_all();
      arg_shift_selection("R", 65);
      }
}

when html files are opened the "minimal" layout is loaded(this is a stripped down layout with only the menu bar visible). when other filetypes are opened the "default" layout(this is a layout i created with all the toolbars and tool-windows i'd like to use and have named default) is used. now the challenge would be:

- switching layouts when switching document tabs. lets say i open a html file, the "minimal" layout is loaded; then i open a txt file, the "default" layout is loaded. when i switch from the txt file to the html file it will be displayed in the "default" layout because the txt file was the last file to be opened and i would like it to be displayed in the "minimal" layout and the reverse; when a html file is opened and is using the "minimal" layout, i would like all other open files that are not html to load the "default" layout when you switch to them. you may have had a point about using switchbuf to accomplish this but i need help to be able to use it to switch layouts when a user switches tabs.

Thank you for your continued support.


Graeme

  • Senior Community Member
  • Posts: 2518
  • Hero Points: 327
Re: Centre text on open
« Reply #18 on: March 18, 2021, 11:15:03 pm »
just use the code I posted here
https://community.slickedit.com/index.php/topic,18107.msg71322.html#msg71322

For _switchbuf_ you probably want to avoid switching layouts every time you switch buffers if the correct layout is already open, so you need a local static variable.

Code: [Select]
void _switchbuf_xselect_twlayout(_str oldbuffname, _str flag)
{
    static bool layout_is_default;
    if ( (get_extension(p_buf_name) == "html"))
    {
        if (layout_is_default || oldbuffname :== "")
              load_named_layout("minimal");
         layout_is_default = false;
         return;
    }

    if (!layout_is_default || oldbuffname :== "")
          load_named_layout("minimal");
     layout_is_default = true;

}

There's a potential problem with the above code - when the editor first starts and the first buffer is opened, the static variable might not match the layout.  The check for oldbuffname being empty may or may not fix it so you should call switchbuf from buffer_add like this

void _buffer_add_xhz_center_text()
{
     _switchbuf_xselect_twlayout("", "");
}

 

bogedo

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: Centre text on open
« Reply #19 on: March 19, 2021, 12:37:25 am »
hmm, tried it but it did not work, perhaps i'm doing it wrong. is this how i should combine it with my existing code ?

Code: [Select]
void _buffer_add_xhz_center_text()
{
   if ( !(get_extension(p_buf_name) == "html")
     load_named_layout("default");
   else{
      load_named_layout("minimal");
      select_all();
      arg_shift_selection("R", 65);
      }
}

void _switchbuf_xselect_twlayout(_str oldbuffname, _str flag)
{
    static bool layout_is_default;
    if ( (get_extension(p_buf_name) == "html"))
    {
        if (layout_is_default || oldbuffname :== "")
              load_named_layout("minimal");
         layout_is_default = false;
         return;
    }

    if (!layout_is_default || oldbuffname :== "")
          load_named_layout("minimal");
     layout_is_default = true;

}

Graeme

  • Senior Community Member
  • Posts: 2518
  • Hero Points: 327
Re: Centre text on open
« Reply #20 on: March 19, 2021, 11:28:31 am »
You actually have a syntax error here

void _buffer_add_xhz_center_text()
{
   if ( !(get_extension(p_buf_name) == "html")     <<<<<<<<<<<<<< missing a closing parenthesis


so when you load your macro it's not actually loading.  The error gets reported on the command line - it's easy to miss.  I actually sometimes miss these - it would be better if it was a modal dialog.

However, the code is crashing slickedit  - so I'm going to do this another way using a timer.  I'll post the code tomorrow.  In the meantime, don't try this code any more - you might get a crash.



bogedo

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: Centre text on open
« Reply #21 on: March 19, 2021, 12:40:01 pm »
apologies, it seems i did not paste it correctly and i missed the closing parentheses, that part of the code is working well.
i'm seeing an issue when switchbuf is called. i get a crash when loading the macro, when trying to do this:

Code: [Select]
void _buffer_add_xhz_center_text()
{
   if ( !(get_extension(p_buf_name) == "html"))
     load_named_layout("default");
   else{
      load_named_layout("minimal");
      select_all();
      arg_shift_selection("R", 65);
      }
}
void _switchbuf_loadlayout()
{
   if ( !(get_extension(p_buf_name) == "html"))
   load_named_layout("default");
}

but when i test it with this:

Code: [Select]
void _buffer_add_xhz_center_text()
{
   if ( !(get_extension(p_buf_name) == "html"))
     load_named_layout("default");
   else{
      load_named_layout("minimal");
      select_all();
      arg_shift_selection("R", 65);
      }
}
void _switchbuf_loadlayout()
{
   if ( !(get_extension(p_buf_name) == "html"))
   select_all();
}

it works as expected.
« Last Edit: March 19, 2021, 12:43:30 pm by bogedo »

bogedo

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: Centre text on open
« Reply #22 on: March 24, 2021, 08:16:27 pm »
switchbuf was unstable for me. got it working using "_on_document_tab_left_click()" this works the way i want it to. here is the code including he center text bit. in summary, when a configured file is opened(in this case html files) it is centered in the middle of the editor and loaded in a "minimal" environment with no toolbars or tool-windows. the "_on_document_tab_left_click()" handles switching layouts between document tabs that contain different filetypes.

Code: [Select]
#include "slick.sh"

void _buffer_add_xhz_center_text()
{
   if ( !(get_extension(p_buf_name) == "html"))
     load_named_layout("default");
   else{
      load_named_layout("minimal");
      select_all();
      arg_shift_selection("R", 65);
      }
}

void _on_document_tab_left_click()
{
   if ( (get_extension(p_buf_name) == "html"))
      load_named_layout("minimal");
   else
   {
      load_named_layout("default");
   }
}

now to the challenges:

- i'd like to be able to also switch layouts when using the key sequence(Ctrl+Tab) which is the default sequence for switching between document tabs.

- when opening SlickEdit i get this error:



this also happens when SlickEdit is opened from a file explorer by double clicking an associated file.

Graeme

  • Senior Community Member
  • Posts: 2518
  • Hero Points: 327
Re: Centre text on open
« Reply #23 on: March 24, 2021, 10:41:10 pm »
I didn't get back to this yet, sorry.  I'll have a go at doing it with a timer in the next couple of days.

Graeme

Graeme

  • Senior Community Member
  • Posts: 2518
  • Hero Points: 327
Re: Centre text on open
« Reply #24 on: March 25, 2021, 04:44:57 am »
I'm unable to get load_named_layout working reliably from a timer callback either, sorry.  From the look of the error you're getting in restore.ex, you won't be able to call load_named_layout from the _buffer_add function reliably either.  You can easily write a command macro to switch to a particular layout and bind it to a key though.

Ctrl+TAB runs the command next_window so you just need to write you own macro and bind ctrl+tab to it.

_command void my_ctrl_tab()
{
   next_window();
   // do whatever you want here
   if ( (get_extension(p_buf_name) == "html"))
      load_named_layout("minimal");
}

For ctrl+shift+tab the function is prev_window.


I guess you copied _on_document_tab_left_click from slick source window.e  ??
 
Code: [Select]
void _on_document_tab_left_click()
{
   if ( !p_mdi_child || !_isEditorCtl(false) ) {
      return;
   }
   // Here we simulate the old file tabs tool window auto-reload which occurs
   // when you click on a tab (because edit command was called on the filename).
   // Don't need/have the old_buffer_name. Just pass null. That way, get a Slick-C
   // stack if the old_buffer_name is used.
   _switchbuf_files(null,'E');
   //say('_on_document_tab_middle_click : wid='p_buf_name);
}

Did you realise that your code will override the code in window.e for _on_document_tab_left_click  -  whichever is last loaded wins.  Maybe you should combine the two

Code: [Select]
void _on_document_tab_left_click()
{
   if ( !p_mdi_child || !_isEditorCtl(false) ) {
      return;
   }
   // Here we simulate the old file tabs tool window auto-reload which occurs
   // when you click on a tab (because edit command was called on the filename).
   // Don't need/have the old_buffer_name. Just pass null. That way, get a Slick-C
   // stack if the old_buffer_name is used.
   _switchbuf_files(null,'E');
   //say('_on_document_tab_middle_click : wid='p_buf_name);

   if ( (get_extension(p_buf_name) == "html"))
      load_named_layout("minimal");
   else
   {
      load_named_layout("default");
   }

}

If a hotfix for window.e comes along one day, it will override your code when you install the hotfix.



bogedo

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: Centre text on open
« Reply #25 on: April 01, 2021, 08:23:19 pm »
I didn't get back to this yet, sorry.  I'll have a go at doing it with a timer in the next couple of days.

Graeme

no worries, you've gone above and beyond with this issue and i sincerely thank you for your efforts.

I'm unable to get load_named_layout working reliably from a timer callback either, sorry.  From the look of the error you're getting in restore.ex, you won't be able to call load_named_layout from the _buffer_add function reliably either.  You can easily write a command macro to switch to a particular layout and bind it to a key though.


well it seems that the issue with load_named_layout is in the hands of the developers now. it is a shame it works unreliably since it would be wonderful to have the layout change automatically without using hotkeys. that being said it is really impressive the amount of customization a user can do with Slick-C and Thank you for helping me get into it.

Ctrl+TAB runs the command next_window so you just need to write you own macro and bind ctrl+tab to it.

_command void my_ctrl_tab()
{
   next_window();
   // do whatever you want here
   if ( (get_extension(p_buf_name) == "html"))
      load_named_layout("minimal");
}

For ctrl+shift+tab the function is prev_window.


works beautifully, Thank you.

I guess you copied _on_document_tab_left_click from slick source window.e  ??
 
Code: [Select]
void _on_document_tab_left_click()
{
   if ( !p_mdi_child || !_isEditorCtl(false) ) {
      return;
   }
   // Here we simulate the old file tabs tool window auto-reload which occurs
   // when you click on a tab (because edit command was called on the filename).
   // Don't need/have the old_buffer_name. Just pass null. That way, get a Slick-C
   // stack if the old_buffer_name is used.
   _switchbuf_files(null,'E');
   //say('_on_document_tab_middle_click : wid='p_buf_name);
}

Did you realise that your code will override the code in window.e for _on_document_tab_left_click  -  whichever is last loaded wins.  Maybe you should combine the two

Code: [Select]
void _on_document_tab_left_click()
{
   if ( !p_mdi_child || !_isEditorCtl(false) ) {
      return;
   }
   // Here we simulate the old file tabs tool window auto-reload which occurs
   // when you click on a tab (because edit command was called on the filename).
   // Don't need/have the old_buffer_name. Just pass null. That way, get a Slick-C
   // stack if the old_buffer_name is used.
   _switchbuf_files(null,'E');
   //say('_on_document_tab_middle_click : wid='p_buf_name);

   if ( (get_extension(p_buf_name) == "html"))
      load_named_layout("minimal");
   else
   {
      load_named_layout("default");
   }

}

If a hotfix for window.e comes along one day, it will override your code when you install the hotfix.




i'm grateful for the tip and the heads-up will modify my code accordingly.

bogedo

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: Centre text on open
« Reply #26 on: April 01, 2021, 08:39:48 pm »
final issue, i've found that when tabs are closed the next tab is loaded with the layout the previous tab had.

lets say you have a tab with a txt file opened and a tab with a html file opened.
when the html file is opened, the "minimal" layout is applied and when a txt file is opened, the "default" layout is applied.
when the txt file is the active file and it is closed SlickEdit moves on to the next open file which is the html file and it is opened with the "default" layout.
since i would like the html file to always be loaded in a "minimal" layout, how would i be able to accomplish this given the use case laid out above ? i have a feeling that switchbuf may be the command for this but it appears to be unreliable when paired with load_named_layout. is there another command that can be used to run a macro when a tab is closed so that:

- close a tab by clicking the 'x' on the tab
- SlickEdit checks the extension of the next file that will load and applies a layout

Graeme

  • Senior Community Member
  • Posts: 2518
  • Hero Points: 327
Re: Centre text on open
« Reply #27 on: April 02, 2021, 06:37:33 am »

There's another way to show / hide tool-windows.  For most / all tool-windows there is an activate-command to show the window and a toggle command to show/ hide.  So you can make a "hide" command by combining them.

Code: [Select]
_command void hide_clipboards() name_info(',')
{
   activate_clipboards();
   toggle_clipboards();
}

This doesn't let you set the location or size of the window though.  Use the keybindings dialog and find commands that start with activate to find them all  - activate_defs etc.


I've sent the code below to slickedit support  - they should be able to reproduce a crash I think - in a clean config using V25.0.1.

Code: [Select]
#include "slick.sh"

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



#if 1
static bool layout_is_default;
static int my_layout_timer_handle;
static int my_layout_startup_counter;
static _str my_layout_current_buf_name;



static void my_layout_timer_callback1()
{
   if (!_use_timers || my_layout_timer_handle < 0)
      return;

   if (++my_layout_startup_counter < 6)
      return;

   my_layout_startup_counter = 6;

   if (_no_child_windows()) {
      return;
   }

   if (my_layout_current_buf_name != _mdi.p_child.p_buf_name){
      my_layout_current_buf_name = _mdi.p_child.p_buf_name;
   }
   else {
      if ( get_extension(_mdi.p_child.p_buf_name) == "html" )
      {
         //say("html " :+ layout_is_default :+ " x " :+ oldbuffname );
         if (layout_is_default)
            load_named_layout("minimal");
         layout_is_default = false;
      }
      else
      {
         //say("nothtml " :+ layout_is_default :+ " x " :+ oldbuffname );
         if (!layout_is_default)
            load_named_layout("default");
         layout_is_default = true;
      }
   }

   _kill_timer(my_layout_timer_handle);
   my_layout_timer_handle = _set_timer(1000, my_layout_timer_callback1);
}


void _switchbuf_my_layout_timer()
{
   if (my_layout_startup_counter < 6)
      return;

   _kill_timer(my_layout_timer_handle);
   my_layout_timer_handle = _set_timer(50, my_layout_timer_callback1);
}


definit()
{
   if (arg(1) == "L") {
      _kill_timer(my_layout_timer_handle);
   }
   my_layout_timer_handle = _set_timer(1000, my_layout_timer_callback1);
   my_layout_startup_counter = 0;
   my_layout_current_buf_name = "";
}

#endif
« Last Edit: April 02, 2021, 08:41:58 am by Graeme »

bogedo

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: Centre text on open
« Reply #28 on: April 02, 2021, 01:00:58 pm »

There's another way to show / hide tool-windows.  For most / all tool-windows there is an activate-command to show the window and a toggle command to show/ hide.  So you can make a "hide" command by combining them.

Code: [Select]
_command void hide_clipboards() name_info(',')
{
   activate_clipboards();
   toggle_clipboards();
}

This doesn't let you set the location or size of the window though.  Use the keybindings dialog and find commands that start with activate to find them all  - activate_defs etc.

[/code]
interesting, good to know this bit of information. though i can see the code being a bit long if i choose to hide all tool-windows. is there also a command to hide toolbars ? if it can be combined with the hide tool-windows code, a minimal layout can be achieved in the same way loading the "minimal" layout does.

I've sent the code below to slickedit support  - they should be able to reproduce a crash I think - in a clean config using V25.0.1.

Code: [Select]
#include "slick.sh"

#pragma option(strictsemicolons,on)
#pragma option(strict,on)
#pragma option(autodecl,off)
#pragma option(strictparens,on)
...
Thank you for reporting this to them, hopefully they can get to the bottom of the load_named_layout issues..