예제 #1
0
    /// <summary>
    /// Returns code to find window w and optionally control con in it. Without newline at the end.
    /// If w/con is same as previous and code of this control is modified and valid, gets code from this code control, from the start to ZReadonlyStart.
    /// Else creates code "var w = wnd.find(...);". If w is invalid, creates code "wnd w = default;".
    /// The returned wndVar is final wnd variable name (of window or control).
    /// </summary>
    public (string code, string wndVar) ZGetWndFindCode(bool test, wnd w, wnd con = default, bool private1 = false)
    {
        if (test)           //remove 'wait' and 'activate' from wnd.find and wnd.Child. If no 'wait', insert 0 to throw notfoundexception.
        {
            var k = ZGetWndFindCode(false, w, con, private1: true);
            var s = k.code;
            var p = _ParseWndFind(s, test: true);
            if (p?.wVar != null)
            {
                void _Replace(int end, int argsStart, int argsEnd, int nameStart, bool orRun = false, bool orRunReplace = false, int funcNameEnd = 0)
                {
                    if (orRun && !orRunReplace)
                    {
                        return;
                    }
                    s = s.ReplaceAt(argsEnd..end, ");");                                      //remove '.Activate()' etc. If orRun, removes run etc arguments.
                    s = s.ReplaceAt(argsStart..nameStart, nameStart < argsEnd ? "0, " : "0"); //remove 'waitS, ' and add '0, ' (to throw NotFoundException)
                    if (orRun)
                    {
                        s = s.Remove(funcNameEnd - 5, 5);                            //findOrRun -> find
                    }
                }

                if (p.cVar != null)
                {
                    _Replace(p.cEnd, p.cArgsStart, p.cArgsEnd, p.cNameStart);
                }
                _Replace(p.wEnd, p.wArgsStart, p.wArgsEnd, p.wNameStart, p.orRun, p.orRunReplace, p.wFuncNameEnd);
            }
            //print.it(s);
            k.code = s;
            return(k);
        }

        string R = null, sCode = null, wndVar = "w", conVar = "c";

        if (w != _wnd)
        {
            _userModified = false;
        }
        else if (!_userModified)
        {
            _userModified = 0 != Call(Sci.SCI_GETMODIFY);
        }
        if (!w.Is0)
        {
            if (_userModified)
            {
                sCode = zRangeText(false, 0, _ReadonlyStartUtf8);
                var p = _ParseWndFind(sCode, test: false);
                if (p?.wVar != null)
                {
                    bool isConCode = p.cVar != null;
                    //print.it(isConCode);
                    if (con == _con && !con.Is0 == isConCode)
                    {
                        //print.it(isConCode ? "same control" : "no control");
                        if (!private1 && p.wName != null)
                        {
                            //if window name changed and does not match the name in code, change it in code
                            var name = w.NameTL_;
                            if (name != null && !new wildex(p.wName, noException: true).Match(name))
                            {
                                var s = TUtil.EscapeWindowName(name, true);
                                if (!(TUtil.IsVerbatim(s, out _) || TUtil.MakeVerbatim(ref s)))
                                {
                                    s = s.Escape(quote: true);
                                }
                                sCode = sCode.ReplaceAt(p.wNameStart..p.wNameEnd, s);
                            }
                        }
                        return(sCode, p.cVar ?? p.wVar);
                    }
                    wndVar = p.wVar;
                    if (isConCode)
                    {
                        sCode = sCode[..p.cStart];