Author Topic: Hide or disable custom tool.  (Read 4639 times)

mhalsig

  • Community Member
  • Posts: 16
  • Hero Points: 0
Hide or disable custom tool.
« on: September 17, 2020, 07:38:16 PM »
Is it possible to disable or hide a custom tool on a custom toolbar if an environment variable is not set?

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Hide or disable custom tool.
« Reply #1 on: September 19, 2020, 12:09:44 AM »
Is it possible to disable or hide a custom tool on a custom toolbar if an environment variable is not set?

Yes it is.  You can read an environment variable with get_env.  Do you want to do this at startup of slickedit or when a workspace opens?  Code below is untested.  Any function whose name starts with _workspace_opened_  gets called when a workspace is opened.

void _workspace_opened_set_my_toolbar()
{
   _str abc = get_env("whatever");
   if (abc :== "something")
      tbShow("MyFormName");
   else
      tbHide("MyFormName");
}


definit()
{
   // at startup or on module load
   _str abc = get_env("whatever");
   if (abc :== "something")
      tbShow("MyFormName");
   else
      tbHide("MyFormName");
}


Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Hide or disable custom tool.
« Reply #2 on: September 19, 2020, 02:02:23 AM »
Uh, hide custom tool...   for that, set the p_visible property of the tool to false to hide it.  If you want the positions of things on the toolbar to re-adjust you need to set the p_x and p_y properties of each item.  To disable a button etc., set the p_enabled property to false.

« Last Edit: September 19, 2020, 04:30:09 AM by Graeme »

mhalsig

  • Community Member
  • Posts: 16
  • Hero Points: 0
Re: Hide or disable custom tool.
« Reply #3 on: September 19, 2020, 09:21:49 PM »
Thank you for the quick reply.

I want this to happen at Startup.

Setting p_visible or p_enabled to true or false is not the issue. Using an environment variable to set either of them has been problematic. Both require a constant expression or at least that is the error I get. The code supplied to hide the toolbar is similar to what I want except I need it for a custom tool (button on toolbar).

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Hide or disable custom tool.
« Reply #4 on: September 20, 2020, 02:34:53 AM »
ok, so it looks like you can't give a name to any of the buttons on a custom toolbar.  From digging through the help I found that it says you can provide a callback (OnUpdate) to enable or disable a "menu item"  - but it might work for a toolbar button too.

If you right click on one of your toolbar buttons and select properties, then click the "auto enable" you get the auto enable properties dialog.  In the help, if you search for "Toolbar Control Properties Dialog" you'll see it links to "Auto Enable Properties Dialog".  If you search for "Macro Callbacks for Enabling Commands" you'll see the stuff below.

If that doesn't work you can wrap the command that the button calls with another command that checks the environment variable before calling the actual command.  If that doesn't help you could create a form with named buttons - but I don't know how to make it behave like a toolbar.


Code: [Select]
If the auto-enable attributes do not provide the features that you want, you can define the enable and
disable callback for the command. The name of the callback function you define is based on the name of
the command as shown in the following example:
#include "slick.sh"
static bool gSomeOtherState;
/*
This function gets called if your command is used in a menu or
toolbar.
You must return a combination of the MF_ flags ORed together.
BEWARE: If an _OnUpdate callback causes a Slick-C run-time error,
you
may not see the error. In addition, the timer used for toolbars,
Context Tagging(R), AutoSave, and some other features may be
automatically terminated. Exit and restart the editor to restart
this timer. Use the "say" function to debug your _OnUpdate
callback.
*/
int _OnUpdate_mycommand(CMDUI &cmdui,int target_wid,_str command)
{
//say('h1');
// Lets assume this command requires the target to be an editor
control
// with a selection.
// IF the target is not an editor control:
if ( !target_wid || !target_wid._isEditorCtl()) {
//say('disabled at h2');
return(MF_GRAYED);
}
//say('h3');
// IF the editor control does not have a selection:
if (!target_wid.select_active2()) {
//say('disabled at h4');
return(MF_GRAYED);
}
//say('h5');
Creating and Editing Menu
Resources
1457if (gSomeOtherState) {
//say('disabled at h6');
return(MF_GRAYED);
}
//say('enabled at h7')
return(MF_ENABLED);
}
_command void mycommand() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
// Some code here...
}
// This command affects the enable/disable of mycommand.
_command void mycommand2(_str argument="0")
{
gSomeOtherState=(argument)?1:0;
// Indicate that the enable state of the toolbar buttons must be
updated.
// The _tbSetRefreshBy function is very fast. Toolbars will be
updated
// after the macro terminates and the user stops typing fast.
_tbSetRefreshBy(VSTBREFRESHBY_USER);
}

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: Hide or disable custom tool.
« Reply #5 on: September 20, 2020, 07:33:43 AM »
Also an offbeat way to do it is to load the source for the toolbar at startup with different source according to the environment variable.  If you use the command "insert form or menu source" on the macro menu for your toolbar you will get code similar to the following.  You can load it programmatically using the "load" command.

Code: [Select]
_form toolbar1 {
   p_backcolor=0x80000005;
   p_border_style=BDS_SIZABLE;
   p_caption="toolbar1";
   p_CaptionClick=true;
   p_forecolor=0x80000008;
   p_height=476;
   p_tool_window=true;
   p_width=9954;
   p_x=-16440;
   p_y=3342;
   p_eventtab2=_qtoolbar_etab2;
   _image button2 {
      p_auto_size=true;
      p_backcolor=0x80000005;
      p_border_style=BDS_NONE;
      p_command="begin-line-text-toggle";
      p_forecolor=0x80000008;
      p_height=420;
      p_max_click=MC_SINGLE;
      p_message="Moves Cursor to the Beginning of the Current Line";
      p_Nofstates=1;
      p_picture="bbleft.svg";
      p_stretch=false;
      p_style=PSPIC_HIGHLIGHTED_BUTTON;
      p_tab_index=1;
      p_tab_stop=false;
      p_value=0;
      p_width=434;
      p_x=140;
      p_y=28;
      p_eventtab2=_ul2_picture;
   }
   _image button1 {
      p_auto_size=true;
      p_backcolor=0x80000005;
      p_border_style=BDS_NONE;
      p_command="end-line-text-toggle";
      p_forecolor=0x80000008;
      p_height=420;
      p_max_click=MC_SINGLE;
      p_message="Moves Cursor to the Eeginning of the Current Line";
      p_Nofstates=1;
      p_picture="bbright.svg";
      p_stretch=false;
      p_style=PSPIC_HIGHLIGHTED_BUTTON;
      p_tab_index=2;
      p_tab_stop=false;
      p_value=0;
      p_width=434;
      p_x=0;
      p_y=0;
      p_eventtab2=_ul2_picture;
   }
}

mhalsig

  • Community Member
  • Posts: 16
  • Hero Points: 0
Re: Hide or disable custom tool.
« Reply #6 on: September 20, 2020, 08:22:15 PM »
I will give these suggestions a try. Thanks for your help.