Community
    • Login

    Are the shortcut key pointers when initialising your menu maent to be const ?

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    5 Posts 3 Posters 173 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ThosRTannerT
      ThosRTanner
      last edited by

      When you create your menu definition for notepad++, you return an array of FuncItems where FuncItem is defined as

      struct FuncItem
      {
      	TCHAR _itemName[menuItemSize] = { '\0' };
      	PFUNCPLUGINCMD _pFunc = nullptr;
      	int _cmdID = 0;
      	bool _init2Check = false;
      	ShortcutKey *_pShKey = nullptr;
      };
      

      which implies that the shortcut keys can be overwritten by notepad++ (actually the API makes it look like the whole array can be overwritten).

      Is this true or are the pointers actually pointer to consts?

      CoisesC 1 Reply Last reply Reply Quote 0
      • CoisesC
        Coises @ThosRTanner
        last edited by

        @ThosRTanner said in Are the shortcut key pointers when initialising your menu maent to be const ?:

        When you create your menu definition for notepad++, you return an array of FuncItems where FuncItem is defined as

        struct FuncItem
        {
        	TCHAR _itemName[menuItemSize] = { '\0' };
        	PFUNCPLUGINCMD _pFunc = nullptr;
        	int _cmdID = 0;
        	bool _init2Check = false;
        	ShortcutKey *_pShKey = nullptr;
        };
        

        which implies that the shortcut keys can be overwritten by notepad++ (actually the API makes it look like the whole array can be overwritten).

        Is this true or are the pointers actually pointer to consts?

        It looks like the only member of that structure (or anything it points to) that is modified is _cmdID; the code just after that appears to create a new copy of the shortcut information to add to a container.

        For what it’s worth, I tried changing the structure in PluginInterface.h to:

        struct FuncItem
        {
        	const wchar_t _itemName[menuItemSize] = { '\0' };
        	const PFUNCPLUGINCMD _pFunc = nullptr;
        	int _cmdID = 0;
        	const bool _init2Check = false;
        	const ShortcutKey * const _pShKey = nullptr;
        };
        

        and line 607 in PluginsManager.cpp to:

        const ShortcutKey & sKey = *(_pluginInfos[i]->_funcItems[j]._pShKey);
        

        and it builds without an error message. So I think that’s fairly good evidence that Notepad++ uses _pShKey as const pointer to const.

        ThosRTannerT 1 Reply Last reply Reply Quote 1
        • ThosRTannerT
          ThosRTanner @Coises
          last edited by ThosRTanner

          @Coises Thanks. That’s good to know (and it’d be nice if the definition in the headers was changed to match that). It’s also interesting to know that there is one word of each item that is modified.

          It does raise another question though - does that mean your menu structure have to remain unchanged once you’ve registered it with notepad++? If it was allocated in a piece of temporary memory which got discarded for instance it would be a little hard to track down!

          Or does notepad++ copy the table and shortcut keys to its own memory?

          Addendum: I tried updating the array I’d passed to N++ and it appears that after updating with the command IDs it does nothing with it - I changed menu strings, function pointers, and shortcut keys, and nothing had any effect.

          This seems - odd.

          CoisesC PeterJonesP 2 Replies Last reply Reply Quote 0
          • CoisesC
            Coises @ThosRTanner
            last edited by Coises

            @ThosRTanner said in Are the shortcut key pointers when initialising your menu maent to be const ?:

            Or does notepad++ copy the table and shortcut keys to its own memory?

            As best I can follow the code, the pointer and count returned from getFuncsArray are saved here as a part of the PluginInfo structure, pointers to which are in turn stored in a vector in PluginsManager.

            I don’t see anywhere that those pointers are used after the plugins are loaded and the information is copied to other structures (and the menu Ids are updated), but the pointers appear to be kept until plugins are unloaded.

            Edit to add: As @PeterJones comment implies, it would be pretty disastrous for Notepad++ to make any use of the information passed in getFuncsArray after initialization, because plugins can and do change their menus, using ordinary Windows functions. Columns++ does that to create a submenu that can have shortcuts, to remove a command that won’t work on older versions of Windows, and optionally to move Columns++ to the main menu bar. It’s all ugly, messy code, because it’s straight Win32 API.

            1 Reply Last reply Reply Quote 1
            • PeterJonesP
              PeterJones @ThosRTanner
              last edited by PeterJones

              @ThosRTanner said in Are the shortcut key pointers when initialising your menu maent to be const ?:

              It does raise another question though - does that mean your menu structure have to remain unchanged once you’ve registered it with notepad++? If it was allocated in a piece of temporary memory which got discarded for instance it would be a little hard to track down!

              No. Plugins like NppExec and PythonScript change the number of menu entries on the fly, though I don’t know whether they can give those commands shortcuts when generated after-the-fact or not. You might be able to look through their source code to see how they dynamically add a new menu entry after-the-fact.

              As far as shortcuts are concerned, after the plugin’s initial request when it’s loaded, I believe the expectation is that the Shortcut Mapper will be used for any future changes to keyboard shortcuts (and that those are restricted to a plugin’s top-level of menu). And when PythonScript or NppExec dynamically adds a command to its top-level menu, Shortcut Mapper makes you wait until the next Notepad++ launch – when the entry is initialized at plugin load, rather than dynamically after – to be able to assign the keystroke.

              update: And when Shortcut Mapper changes any command’s shortcut, you can actually process NPPN_SHORTCUTREMAPPED to see how Shortcut Mapper changed your menu’s command.

              1 Reply Last reply Reply Quote 1
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors