How to check if NPP is in rtl or ltr direction?

  • Hi
    I’m in the middle of developing a plugin that will remember the text direction for each tab/file and will switch to the right direction when the file is reopened or the user switches back to the tab.
    In order to do that my plugin needs to know at a given moment whether NPP is currently in rtl or ltr.
    I tried to ‘steal’ the following function form NPP’s source code:
    bool isTextDirectionRTL()
    long exStyle = static_cast<long>(::GetWindowLongPtr(nppData._scintillaMainHandle, GWL_EXSTYLE));
    return (exStyle & WS_EX_LAYOUTRTL) != 0;
    It worked for a while but after doing some additional tests I saw that it doesn’t always work. (I guess it has something to do with the fact that the ‘main handle’ doesn’t always point to the required scintilla or something like that)

    For now I’ve decided to create a keyboard shortcut that will call a function in my plugin that will change the text direction (and ask the user to change text direction only this way), this way my plugin can monitor and keep track of the text direction, if anybody knows about a better solution I’d be glad to here.

  • Hello Ezra,

    You should check first whether the current view is MAIN_VIEW or SUB_VIEW.

    inline HWND getCurrentView()
    	return (::SendMessage(nppData._nppHandle, NPPM_GETCURRENTVIEW, 0, 0) == MAIN_VIEW) ?
    			nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    (From NppHelpers.h in Compare Plugin. Thank you @pnedev).

    And then:

    bool isTextDirectionRTL() const
    	long exStyle = static_cast<long>(::GetWindowLongPtr(getCurrentView(), GWL_EXSTYLE));
    	return (exStyle & WS_EX_LAYOUTRTL) != 0;

    This plugin would be important to RTL users.
    Thank you and best of luck.

    How about adding an option to set the default text direction to LTR even if NPP layout is RTL?

  • Thanks, that worked!

  • Great.
    You’re welcome.

Log in to reply