Exemple #1
0
    /// <summary>
    /// Used only by the Save class.
    /// </summary>
    bool _SaveStateNow()
    {
        if (DB == null)
        {
            return(true);
        }
        try {
            using (var trans = DB.Transaction()) {
                DB.Execute("REPLACE INTO _misc VALUES ('expanded',?)",
                           string.Join(" ", _control.AllNodes.Where(n => n.IsExpanded).Select(n => (n.Tag as FileNode).IdString)));

                using (new Au.Util.StringBuilder_(out var b)) {
                    var a = OpenFiles;
                    b.Append(a.IndexOf(_currentFile));
                    foreach (var v in a)
                    {
                        b.Append(' ').Append(v.IdString);                                     //FUTURE: also save current position and scroll position, eg "id.pos.scroll"
                    }
                    DB.Execute("REPLACE INTO _misc VALUES ('open',?)", b.ToString());
                }

                trans.Commit();
            }
            return(true);
        }
        catch (SLException ex) {
            ADebug.Print(ex);
            return(false);
        }
    }
Exemple #2
0
    /// <summary>
    /// Ends this task (kills process), if running.
    /// Returns false if fails, unlikely.
    /// </summary>
    /// <param name="onProgramExit">Called on program exit. Returns true even if fails. Does not wait.</param>
    public bool End(bool onProgramExit)
    {
        var p = _process;

        if (p != null)
        {
            var  h  = p.SafeWaitHandle.DangerousGetHandle();
            bool ok = Api.TerminateProcess(h, -1);
            if (onProgramExit)
            {
                return(true);
            }
            if (ok)
            {
                if (0 != Api.WaitForSingleObject(h, 2000))
                {
                    ADebug.Print("process not terminated"); return(false);
                }
            }
            else
            {
                var s = ALastError.Message;
                if (0 != Api.WaitForSingleObject(h, 0))
                {
                    ADebug.Print(s); return(false);
                }
            }
            //note: TerminateProcess kills process not immediately. Need at least several ms.
        }
        return(true);
        //SHOULDDO: release pressed keys.
    }
Exemple #3
0
    /// <summary>
    /// Removes an ended task from the 'running' list. If a task is queued and can run, starts it.
    /// When task ended, TaskEnded1 posts to MainForm message WM_TASK_ENDED with task id in wParam. MainForm calls this function.
    /// </summary>
    internal void TaskEnded2(IntPtr wParam)
    {
        if (_disposed)
        {
            return;
        }

        int taskId = (int)wParam;
        int i      = _Find(taskId);

        if (i < 0)
        {
            ADebug.Print("not found. It's OK, but should be very rare, mostly with 1-core CPU."); return;
        }

        _a.RemoveAt(i);

        for (int j = _q.Count - 1; j >= 0; j--)
        {
            var t = _q[j];
            if (_CanRunNow(t.f, t.r, out _))
            {
                _q.RemoveAt(j);
                RunCompiled(t.f, t.r, t.args, ignoreLimits: true);
                break;
            }
        }

        _updateUI = true;
    }
Exemple #4
0
    private void _eName_TextChanged(object sender, EventArgs e)
    {
        string name = _eName.Text;
        var    a    = new List <(string name, string code)>();

        int nWild = 0; for (int i = 0; i < name.Length; i++)

        {
            switch (name[i])
            {
            case '*':
            case '?': nWild++; break;
            }
        }

        if (name.Length > 0 && (nWild == 0 || name.Length - nWild >= 2))
        {
            string sql;
            if (name.Contains(' '))
            {
                sql = $"SELECT * FROM api WHERE name in ('{string.Join("', '", name.RegexFindAll(@"\b[A-Za-z_]\w\w+", 0))}')";
            }
            else if (name.FindAny("*?") >= 0)
            {
                sql = $"SELECT * FROM api WHERE name GLOB '{name}'";
            }
            else
            {
                sql = $"SELECT * FROM api WHERE name = '{name}'";
            }
            try {
                using var stat = _db.Statement(sql);
                //APerf.First();
                while (stat.Step())
                {
                    a.Add((stat.GetText(0), stat.GetText(1)));
                }
                //APerf.NW(); //30 ms cold, 10 ms warm. Without index.
            }
            catch (SLException ex) { ADebug.Print(ex.Message); }
        }

        string code = "";

        if (a.Count != 0)
        {
            code = a[0].code;
            if (a.Count > 1)
            {
                code = string.Join(code.Starts("internal const") ? "\r\n" : "\r\n\r\n", a.Select(o => o.code));
            }
            code += "\r\n";
        }
        _code.ZSetText(code);
    }
Exemple #5
0
 public _NetDocumentationProvider()
 {
     try {
         _db = EdDatabases.OpenDoc();                     //never mind: we don't dispose it on process exit
         if (_db.Get(out string s, "SELECT xml FROM doc WHERE name='.'"))
         {
             _refs = new HashSet <string>(s.SegSplit("\n"));
         }
     }
     catch (SLException ex) { ADebug.Print(ex.Message); }
 }
Exemple #6
0
    /// <summary>
    /// Enables or disables command's menu item and toolbar buttons.
    /// </summary>
    /// <param name="cmd">Command name. See Strips.xml.</param>
    /// <param name="enable"></param>
    public static void EnableCmd(string cmd, bool enable)
    {
        var a = _strips.Find(cmd);
        int i, n = a.Count;

        if (n == 0)
        {
            ADebug.Print("item not found: " + cmd); return;
        }
        for (i = 0; i < n; i++)
        {
            a[i].Enabled = enable;
        }
    }
Exemple #7
0
        LPARAM _WndProc(AWnd w, int message, LPARAM wParam, LPARAM lParam)
        {
            try {
                switch (message)
                {
                case Api.WM_USER + 1:
                    //_ht.Return((int)wParam, false); //test speed without _KeyMouseEvent
                    _KeyMouseEvent((int)wParam, (HooksThread.UsedEvents)(int) lParam);
                    return(0);

                case Api.WM_USER + 20:
                    _windowTriggers.SimulateNew_(wParam, lParam);
                    return(0);
                }
            }
            catch (Exception ex) { ADebug.Print(ex.Message); return(default); }
Exemple #8
0
    public static void UiLoaded()
    {
        //warm up
        //Task.Delay(100).ContinueWith(_1 => {
        Task.Run(() => {
            //var p1 = APerf.Create();
            try {
                var code = @"//.
using Au; using Au.Types; using System; using System.Collections.Generic;
class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) {
AOutput.Write(""t"" + 'c' + 1);
}}";

                var refs              = new MetaReferences().Refs;
                int position          = code.IndexOf('}');
                ProjectId projectId   = ProjectId.CreateNewId();
                DocumentId documentId = DocumentId.CreateNewId(projectId);
                using var ws          = new AdhocWorkspace();
                var sol = ws.CurrentSolution
                          .AddProject(projectId, "p", "p", LanguageNames.CSharp)
                          .AddMetadataReferences(projectId, refs)
                          .AddDocument(documentId, "f.cs", code);
                var document = sol.GetDocument(documentId);
                //p1.Next();
                //_ = document.GetSemanticModelAsync().Result;
                //p1.Next();
                Program.MainForm.BeginInvoke(new Action(() => {
                    //APerf.Next('w');
                    _isWarm = true;
                    ReadyForStyling?.Invoke();
                    Panels.Editor.ZActiveDocChanged += Stop;
                    Program.Timer025sWhenVisible    += _Timer025sWhenVisible;
                }));
                //p1.Next();
                //1000.ms();
                //p1.Next();
                //Compiler.Warmup(document); //don't need. Later fast enough. Now just uses more memory and CPU at startup.
                //p1.NW('w');
                //APerf.NW();

                //EdUtil.MinimizeProcessPhysicalMemory(500); //with this later significantly slower
            }
            catch (Exception ex) {
                ADebug.Print(ex);
            }
        });
    }
Exemple #9
0
 void _Write(string s)
 {
     try {
         try { _mutex.WaitOne(); } catch (AbandonedMutexException) { }
         try {
             _writer.BaseStream.Seek(0, SeekOrigin.End);
             _writer.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff", DateTimeFormatInfo.InvariantInfo));
             _writer.Write(" | ");
             _writer.Write(ATask.Name);
             _writer.Write(" | ");
             _writer.WriteLine(s);
             _writer.Flush();
         }
         finally { _mutex.ReleaseMutex(); }
     }
     catch (Exception ex) { ADebug.Print(ex.ToStringWithoutStack()); }
 }
Exemple #10
0
 void _SetTempKeybHook()
 {
     //AOutput.Write(". hook");
     if (_keyHook == null)
     {
         _keyHook = AHookWin.Keyboard(k => {
             if (ATime.WinMilliseconds >= _keyHookTimeout)
             {
                 _ResetUpAndUnhookTempKeybHook();
                 ADebug.Print("hook timeout");
             }
             else
             {
                 var mod = k.Mod;
                 if (0 != (mod & _upMod) && k.IsUp)
                 {
                     _upMod &= ~mod;
                     if (_upMod == 0 && _upEvent == 0 && _upTrigger != null)
                     {
                         _triggers.RunAction_(_upTrigger, _upArgs);
                         _ResetUp();
                     }
                 }
                 if (0 != (mod & _eatMod))
                 {
                     //AOutput.Write(k);
                     k.BlockEvent();
                     if (k.IsUp)
                     {
                         _eatMod &= ~mod;
                     }
                 }
                 if (0 == (_upMod | _eatMod))
                 {
                     _UnhookTempKeybHook();
                 }
             }
         }, setNow: false);
     }
     if (!_keyHook.IsSet)
     {
         _keyHook.Hook();
     }
     _keyHookTimeout = _keyHook.IgnoreModInOtherHooks_(5000);
 }
Exemple #11
0
        public bool GetData(IDataObject d)
        {
            //foreach(var v in d.GetFormats()) AOutput.Write(v, d.GetData(v, false)?.GetType()); AOutput.Write("--");
            try {
                if (d.GetDataPresent(DataFormats.FileDrop, false))                  //files
                {
                    files = d.GetData(DataFormats.FileDrop, false) as string[]; if (files == null)
                    {
                        return(false);
                    }
                }
                else if (d.GetDataPresent("Shell IDList Array", false))                    //any shell objects
                {
                    if (!(d.GetData("Shell IDList Array", false) is MemoryStream m))
                    {
                        return(false);
                    }
                    shell = m.ToArray();
                }
                else if (d.GetDataPresent("UnicodeText", false))                    //text or URL
                {
                    text = d.GetData("UnicodeText", false) as string; if (text == null)
                    {
                        return(false);
                    }
                    if (d.GetDataPresent("FileGroupDescriptorW", true))                      //link text. All browsers.
                    {
                        if (!(d.GetData("FileGroupDescriptorW", false) is MemoryStream m))
                        {
                            return(false);
                        }
                        linkName = m.ToArray();
                    }
                }
                else
                {
                    return(false);
                }
                return(true);
            }
            catch (Exception ex) { ADebug.Print(ex); return(false); }

            //note: MemoryStream.GetBuffer says "UnauthorizedAccessException: MemoryStream's internal buffer cannot be accessed"
            //info: if from IE, GetData returns null for all formats.
        }
Exemple #12
0
 protected internal override string GetDocumentationForSymbol(string documentationMemberID, CultureInfo preferredCulture, CancellationToken cancellationToken = default)
 {
     if (_db != null)
     {
         lock (_db) {                    //sometimes not in main thread
             try {
                 _stat ??= _db.Statement("SELECT xml FROM doc WHERE name=?");
                 if (_stat.Bind(1, documentationMemberID).Step())
                 {
                     return(_stat.GetText(0));
                 }
                 //ADebug.Print(documentationMemberID);
             }
             catch (SLException ex) { ADebug.Print(ex.Message); }
             finally { _stat?.Reset(); }
         }
     }
     return(null);
 }
Exemple #13
0
 /// <summary>
 /// Gets check state of this check box or radio button.
 /// Returns 0 if unchecked, 1 if checked, 2 if indeterminate. Also returns 0 if this is not a button or if failed to get state.
 /// </summary>
 /// <param name="useAcc">Use <see cref="AAcc.State"/>. If false (default) and this button has a standard checkbox style, uses API <msdn>BM_GETCHECK</msdn>.</param>
 public int GetCheckState(bool useAcc = false)
 {
     if (useAcc || !_IsCheckbox())
     {
         //info: Windows Forms controls are user-drawn and don't have one of the styles, therefore BM_GETCHECK does not work.
         try {                     //avoid exception in property-get functions
             using var a = AAcc.FromWindow(W, AccOBJID.CLIENT, flags: AWFlags.NoThrow);
             if (a == null)
             {
                 return(0);
             }
             return(_GetAccCheckState(a));
         }
         catch (Exception ex) { ADebug.Print(ex); }                    //CONSIDER: if fails, show warning. In all AWnd property-get functions.
         return(0);
     }
     else
     {
         return((int)W.Send(BM_GETCHECK));
     }
 }
Exemple #14
0
            public void Write(string s)
            {
                Debug.Assert(ATask.Role == ATRole.MiniProgram);                 //could be any, but currently we log only in miniProgram processes. Would be no task name if called in editor process.

                if (_openState != 3)
                {
                    //First time open and write async. Else it would be the slowest part of starting a preloaded assembly in Au.Task.exe.
                    if (0 == Interlocked.CompareExchange(ref _openState, 1, 0))
                    {
                        ThreadPool.QueueUserWorkItem(_ => {
                            lock (this) {
                                _openState = 2;
                                try {
                                    _mutex  = new Mutex(false, "Au.Mutex.LogFile");
                                    _writer = new StreamWriter(new FileStream(_Path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite));
                                }
                                catch (Exception ex) {
                                    ADebug.Print(ex.ToStringWithoutStack());
                                    _openState = -1;
                                    return;
                                }
                                _Write(s);
                                _openState = 3;
                            }
                        });
                        return;
                    }
                    while (_openState == 1)
                    {
                        Thread.Sleep(15);
                    }
                    lock (this) {
                        if (_openState < 0)
                        {
                            return;
                        }
                    }
                }
                _Write(s);
            }
Exemple #15
0
    /// <summary>
    /// Checks or unchecks command's menu item and toolbar buttons.
    /// </summary>
    /// <param name="cmd">Command name. See Strips.xml.</param>
    /// <param name="check"></param>
    public static void CheckCmd(string cmd, bool check)
    {
        var a = _strips.Find(cmd);
        int i, n = a.Count;

        if (n == 0)
        {
            ADebug.Print("item not found: " + cmd); return;
        }
        for (i = 0; i < n; i++)
        {
            switch (a[i])
            {
            case ToolStripMenuItem m:
                m.Checked = check;
                break;

            case ToolStripButton b:
                b.Checked = check;
                break;
            }
        }
    }
Exemple #16
0
    string _FormatHtml(int iSel, bool userSelected)
    {
        _data.iSelected = iSel;
        if (userSelected)
        {
            _data.iUserSelected = iSel;
        }

        var     r           = _data.r;
        ISymbol currentItem = null;
        SignatureHelpParameter currentParameter = null;
        var b = new StringBuilder("<body>");

        //AOutput.Clear();
        for (int i = 0; i < r.Items.Count; i++)
        {
            var sh = r.Items[i];
            if (sh is AbstractSignatureHelpProvider.SymbolKeySignatureHelpItem kk)
            {
                var sym = kk.Symbol;
                using var li = new CiHtml.HtmlListItem(b, i == iSel);
                if (i != iSel)
                {
                    b.AppendFormat("<a href='^{0}'>", i);
                }
                else
                {
                    currentItem = sym;
                }
#if false
                CiHtml.TaggedPartsToHtml(b, sh.PrefixDisplayParts);                 //works, but formats not as I like (too much garbage). Has bugs with tuples.
#else
                //if(nt != null) {
                //	AOutput.Write(1, nt.IsGenericType, nt.IsTupleType, nt.IsUnboundGenericType, nt.Arity, nt.CanBeReferencedByName);
                //	AOutput.Write(2, nt.IsAnonymousType, nt.IsDefinition, nt.IsImplicitlyDeclared, nt.Kind, nt.TypeKind);
                //	AOutput.Write(3, nt.MemberNames);
                //	AOutput.Write(4, nt.Name, nt.MetadataName, nt.OriginalDefinition, nt.TupleUnderlyingType);
                //	AOutput.Write("TypeParameters:");
                //	AOutput.Write(nt.TypeParameters);
                //	AOutput.Write("TypeArguments:");
                //	AOutput.Write(nt.TypeArguments);
                //	AOutput.Write("TupleElements:");
                //	try { var te = nt.TupleElements; if(!te.IsDefault) AOutput.Write(te); } catch(Exception e1) { AOutput.Write(e1.ToStringWithoutStack()); }
                //	AOutput.Write("---");
                //}

                int isTuple = 0;                 //1 ValueTuple<...>, 2 (...)
                var nt      = sym as INamedTypeSymbol;
                if (nt != null && nt.IsTupleType)
                {
                    isTuple = nt.IsDefinition ? 1 : 2;
                }

                if (isTuple == 1)
                {
                    b.Append("ValueTuple");                              //SymbolWithoutParametersToHtml formats incorrectly
                }
                else if (isTuple == 0)
                {
                    CiHtml.SymbolWithoutParametersToHtml(b, sym);
                }
                string b1 = "(", b2 = ")";
                if (nt != null)
                {
                    if (nt.IsGenericType && isTuple != 2)
                    {
                        b1 = "&lt;"; b2 = "&gt;";
                    }
                }
                else if (sym is IPropertySymbol)
                {
                    b1 = "["; b2 = "]";
                }
                b.Append(b1);
#endif
                int iArg = r.ArgumentIndex, lastParam = sh.Parameters.Length - 1;
                int selParam = iArg <= lastParam ? iArg : (sh.IsVariadic ? lastParam : -1);
                if (!r.ArgumentName.NE())
                {
                    var pa = sh.Parameters;
                    for (int pi = 0; pi < pa.Length; pi++)
                    {
                        if (pa[pi].Name == r.ArgumentName)
                        {
                            selParam = pi; break;
                        }
                    }
                }
                CiHtml.ParametersToHtml(b, sym, selParam, sh);
                //CiHtml.ParametersToHtml(b, sh, selParam); //works, but formats not as I like (too much garbage)
#if false
                CiHtml.TaggedPartsToHtml(b, sh.SuffixDisplayParts);
#else
                b.Append(b2);
#endif
                if (i != iSel)
                {
                    b.Append("</a>");
                }
                else if (selParam >= 0)
                {
                    currentParameter = sh.Parameters[selParam];
                }
            }
            else
            {
                ADebug.Print(sh);
            }
        }

        if (currentItem != null)
        {
            var    tt        = r.Items[iSel].DocumentationFactory?.Invoke(default);
Exemple #17
0
        /// <summary>
        /// Loads image and returns its data in .bmp file format.
        /// Returns null if fails, for example file not found or invalid Base64 string.
        /// </summary>
        /// <param name="s">Depends on t. File path or resource name without prefix or Base64 image data without prefix.</param>
        /// <param name="t">Image type and string format.</param>
        /// <param name="searchPath">Use <see cref="AFile.SearchPath"/></param>
        /// <remarks>Supports environment variables etc. If not full path, searches in <see cref="AFolders.ThisAppImages"/>.</remarks>
        public static byte[] BmpFileDataFromString(string s, ImageType t, bool searchPath = false)
        {
            //AOutput.Write(t, s);
            try {
                switch (t)
                {
                case ImageType.Bmp:
                case ImageType.PngGifJpg:
                case ImageType.Cur:
                    if (searchPath)
                    {
                        s = AFile.SearchPath(s, AFolders.ThisAppImages);
                        if (s == null)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        if (!APath.IsFullPathExpandEnvVar(ref s))
                        {
                            return(null);
                        }
                        s = APath.Normalize(s, AFolders.ThisAppImages);
                        if (!AFile.ExistsAsFile(s))
                        {
                            return(null);
                        }
                    }
                    break;
                }

                switch (t)
                {
                case ImageType.Base64CompressedBmp:
                    return(AConvert.Decompress(Convert.FromBase64String(s)));

                case ImageType.Base64PngGifJpg:
                    using (var stream = new MemoryStream(Convert.FromBase64String(s), false)) {
                        return(_ImageToBytes(Image.FromStream(stream)));
                    }

                case ImageType.Resource:
                    return(_ImageToBytes(AResources.GetAppResource(s) as Image));

                case ImageType.Bmp:
                    return(File.ReadAllBytes(s));

                case ImageType.PngGifJpg:
                    return(_ImageToBytes(Image.FromFile(s)));

                case ImageType.Ico:
                case ImageType.IconLib:
                case ImageType.ShellIcon:
                case ImageType.Cur:
                    return(_IconToBytes(s, t == ImageType.Cur, searchPath));
                }
            }
            catch (Exception ex) { ADebug.Print(ex.Message + "    " + s); }
            return(null);
        }
Exemple #18
0
    protected override unsafe void WndProc(ref Message m)
    {
        AWnd w = (AWnd)m.HWnd; LPARAM wParam = m.WParam, lParam = m.LParam;

        //AWnd.More.PrintMsg(m, Api.WM_ENTERIDLE, Api.WM_SETCURSOR, Api.WM_GETTEXT, Api.WM_GETTEXTLENGTH, Api.WM_GETICON, Api.WM_NCMOUSEMOVE);

        switch (m.Msg)
        {
        case RunningTasks.WM_TASK_ENDED:         //WM_USER+900
            Program.Tasks.TaskEnded2(m.WParam);
            return;

        case Api.WM_ACTIVATE:
            int isActive = AMath.LoUshort(wParam);             //0 inactive, 1 active, 2 click-active
            if (isActive == 1 && !w.IsActive && !Api.SetForegroundWindow(w))
            {
                //Normally at startup always inactive, because started as admin from task scheduler. SetForegroundWindow sometimes works, sometimes not.
                //workaround for: If clicked a window after our app started but before w activated, w is at Z bottom and in some cases without taskbar button.
                ADebug.Print("window inactive");
                AWnd.More.TaskbarButton.Add(w);
                if (!w.ActivateLL())
                {
                    AWnd.More.TaskbarButton.Flash(w, 5);
                }
            }
            //restore focused control correctly
            if (isActive == 0)
            {
                _wFocus = AWnd.ThisThread.Focused;
            }
            else if (_wFocus.IsAlive)
            {
                AWnd.ThisThread.Focus(_wFocus);
            }
            return;

        case Api.WM_SYSCOMMAND:
            int sc = (int)wParam;
            if (sc >= 0xf000)              //system
            {
                sc &= 0xfff0;
                if (sc == Api.SC_CLOSE && Visible && Program.Settings.runHidden)
                {
                    this.WindowState = FormWindowState.Minimized;
                    this.Visible     = false;
                    EdUtil.MinimizeProcessPhysicalMemory(500);
                    return;
                    //initially this code was in OnFormClosing, but sometimes hides instead of closing, because .NET gives incorrect CloseReason. Cannot reproduce and debug.
                }
            }
            else                 //our
            {
                switch (sc)
                {
                case c_menuid_Exit: Strips.Cmd.File_Exit(); return;
                }
            }
            break;

        case Api.WM_POWERBROADCAST:
            if (wParam == 4)
            {
                Program.Tasks.EndTask();                         //PBT_APMSUSPEND
            }
            break;

        case Api.WM_WINDOWPOSCHANGING:
            var p = (Api.WINDOWPOS *)lParam;
            //AOutput.Write(p->flags);
            //workaround: if started maximized, does not receive WM_SHOWWINDOW. Then .NET at first makes visible, then creates controls and calls OnLoad.
            if (p->flags.Has(Native.SWP.SHOWWINDOW) && Program.Loaded == EProgramState.LoadedWorkspace)
            {
                //p->flags &= ~Native.SWP.SHOWWINDOW; //no, adds 5 duplicate messages
                var m2 = Message.Create(m.HWnd, Api.WM_SHOWWINDOW, (IntPtr)1, default);
                base.WndProc(ref m2);                 //creates controls and calls OnLoad and OnVisibleChanged
                return;
            }
            break;
        }
Exemple #19
0
        /// <summary>
        /// Makes triggers alive.
        /// </summary>
        /// <remarks>
        /// This function monitors hotkeys, activated windows and other events. When an event matches an added trigger, launches the thrigger's action, which runs in other thread by default.
        /// Does not return immediately. Runs until this process is terminated or <see cref="Stop"/> called.
        /// </remarks>
        /// <example>See <see cref="ActionTriggers"/>.</example>
        /// <exception cref="InvalidOperationException">Already running.</exception>
        /// <exception cref="AuException">Something failed.</exception>
        public unsafe void Run()
        {
            //ADebug.PrintLoadedAssemblies(true, true, true);

            ThrowIfRunning_();

            //bool haveTriggers = false;
            HooksThread.UsedEvents hookEvents = 0;
            _windowTriggers = null;
            for (int i = 0; i < _t.Length; i++)
            {
                var t = _t[i];
                if (t == null || !t.HasTriggers)
                {
                    continue;
                }
                //haveTriggers = true;
                switch ((TriggerType)i)
                {
                case TriggerType.Hotkey: hookEvents |= HooksThread.UsedEvents.Keyboard; break;

                case TriggerType.Autotext: hookEvents |= HooksThread.UsedEvents.Keyboard | HooksThread.UsedEvents.Mouse; break;

                case TriggerType.Mouse: hookEvents |= (t as MouseTriggers).UsedHookEvents_; break;

                case TriggerType.Window: _windowTriggers = t as WindowTriggers; break;
                }
            }
            //AOutput.Write(haveTriggers, (uint)llHooks);
            //if(!haveTriggers) return; //no. The message loop may be used for toolbars etc.

            if (!s_wasRun)
            {
                s_wasRun = true;
                AWnd.More.RegisterWindowClass(c_cn);
            }
            _wMsg             = AWnd.More.CreateMessageOnlyWindow(_WndProc, c_cn);
            _mainThreadId     = AThread.NativeId;
            _winTimerPeriod   = 0;
            _winTimerLastTime = 0;

            if (hookEvents != 0)
            {
                //prevent big delay later on first LL hook event while hook proc waits
                if (!s_wasKM)
                {
                    s_wasKM = true;
                    ThreadPool.QueueUserWorkItem(_ => {
                        try {
                            //using var p1 = APerf.Create();
                            new AWnd.Finder("*a").IsMatch(AWnd.GetWnd.Root);               //if used window scopes etc
                            _ = AHookWin.LowLevelHooksTimeout;                             //slow JIT of registry functions
                            Util.AJit.Compile(typeof(ActionTriggers), nameof(_WndProc), nameof(_KeyMouseEvent));
                            Util.AJit.Compile(typeof(TriggerHookContext), nameof(TriggerHookContext.InitContext), nameof(TriggerHookContext.PerfEnd), nameof(TriggerHookContext.PerfWarn));
                            Util.AJit.Compile(typeof(ActionTrigger), nameof(ActionTrigger.MatchScopeWindowAndFunc));
                            Util.AJit.Compile(typeof(HotkeyTriggers), nameof(HotkeyTriggers.HookProc));
                            AutotextTriggers.JitCompile();
                            MouseTriggers.JitCompile();
                        }
                        catch (Exception ex) { ADebug.Print(ex); }
                    });
                }

                _thc = new TriggerHookContext(this);

                _ht = new HooksThread(hookEvents, _wMsg);
            }

            try {
                _evStop = Api.CreateEvent(false);
                _StartStopAll(true);
                IntPtr h = _evStop;
                _Wait(&h, 1);
            }
            finally {
                if (hookEvents != 0)
                {
                    _ht.Dispose(); _ht = null;
                }
                Api.DestroyWindow(_wMsg); _wMsg = default;
                Stopping?.Invoke(this, EventArgs.Empty);
                _evStop.Dispose();
                _StartStopAll(false);
                _mainThreadId = 0;
                _threads?.Dispose(); _threads = null;
            }

            void _StartStopAll(bool start)
            {
                foreach (var t in _t)
                {
                    if (t?.HasTriggers ?? false)
                    {
                        t.StartStop(start);
                    }
                }
            }
        }
Exemple #20
0
        /// <summary>
        /// Creates a menu or toolbar item.
        /// </summary>
        /// <param name="x">XML element containing item properties.</param>
        /// <param name="isMenu">false if toolbar item, true if menu item (also if menu bar item).</param>
        ToolStripItem _CreateChildItem(XElement x, bool isMenu)
        {
            string s, tag = x.Name.LocalName;

            if (tag == "sep")
            {
                return new ToolStripSeparator()
                       {
                           Tag = x
                       }
            }
            ;

            ToolStripItem item; ToolStripMenuItem mi;
            bool          isControl = false, needHandler = false;

            if (isMenu)
            {
                mi = new ToolStripMenuItem();

                item = mi;
                if (x.HasElements || x.HasAttr("dd"))
                {
                    _Submenu(x, mi, tag);
                }
                else
                {
                    needHandler = true;
                }
            }
            else if (x.HasAttr("type"))
            {
                isControl = true;
                string cue = x.Attr("cue");
                s = x.Attr("type");
                switch (s)
                {
                case "edit":
                    var ed = new ToolStripSpringTextBox();
                    if (cue != null)
                    {
                        ed.ZSetCueBanner(cue);
                    }
                    item = ed;
                    break;

                case "combo":
                    var combo = new ToolStripSpringComboBox();
                    if (cue != null)
                    {
                        combo.ZSetCueBanner(cue);
                    }
                    item = combo;
                    break;

                default:
                    Debug.Assert(false);
                    return(null);
                }
                if (cue != null)
                {
                    item.AccessibleName = cue;
                }
            }
            else if (x.HasElements || x.HasAttr("dd"))
            {
                ToolStripDropDownItem ddi;
                var handler = _callbacks.GetClickHandler(tag);
                if (handler != null)
                {
                    var sb = new ToolStripSplitButton();
                    sb.ButtonClick += handler;
                    ddi             = sb;
                }
                else
                {
                    ddi = new ToolStripDropDownButton();
                }
                item = ddi;
                _Submenu(x, ddi, tag);
            }
            else
            {
                needHandler = true;
                var b = new ToolStripButton();
                item = b;
            }

            item.Name = tag;
            item.Tag  = x;

            _SetItemProperties(x, item, isMenu, isControl);

            if (needHandler)
            {
                var handler = _callbacks.GetClickHandler(tag);
                if (handler != null)
                {
                    item.Click += handler;
                }
                else
                {
                    ADebug.Print("no handler of " + tag);
                    //return null;
                    //item.Enabled = false;
                    item.Visible = false;
                }
            }

            return(item);
        }

        void _Submenu(XElement x, ToolStripDropDownItem ddItem, string tag)
        {
            var s = x.Attr("dd");

            if (!s.NE())
            {
                ddItem.DropDown = Submenus[s];
            }
            else
            {
                var dd = ddItem.DropDown as ToolStripDropDownMenu;                 //note: not ddItem.DropDown=new ToolStripDropDownMenu(). Then eg hotkeys don't work.
                dd.Name = tag;
                dd.Tag  = x;
                Submenus.Add(tag, dd);

                if (s != null)                              //attribute dd="". Will be filled later, eg on Opening event.
                {
                    dd.Items.Add(new ToolStripSeparator()); //else no drop arrow and no Opening event
                    return;
                }
#if !LAZY_MENUS
                dd.SuspendLayout();                 //with this don't need lazy menus
                _AddChildItems(x, dd, true);
                dd.ResumeLayout(false);
#else
                //Fill menu items later. This saves ~50 ms of startup time if not using dd.SuspendLayout. With dd.SuspendLayout - just 5 ms.
                //Can do it with Opening event or with timer. With timer easier. With event users cannot use MSAA etc to automate clicking menu items (with timer cannot use it only the first 1-2 seconds).
#if true
                ATimer.After(500, t =>
                {
                    dd.SuspendLayout();
                    _AddChildItems(x, dd, true);
                    dd.ResumeLayout(false);
                });
#else
                dd.Items.Add(new ToolStripSeparator());
                CancelEventHandler eh = null;
                eh = (sender, e) =>
                {
                    dd.Opening -= eh;
                    dd.Items.Clear();
                    _AddChildItems(x, dd, true);
                };
                dd.Opening += eh;
#endif
#endif
            }
        }

        void _SetItemProperties(XElement x, ToolStripItem item, bool isMenu, bool isControl)
        {
            string s, defaultText = null;

            var tt = x.Attr("tt");             //tooltip

            item.ToolTipText = tt ?? (isMenu ? null : (defaultText = _GetDefaultItemText(x)));

            if (!isMenu)
            {
                if (x.HasAttr("hide"))
                {
                    item.Overflow = ToolStripItemOverflow.Always;
                }
                else if (!_inBuildAll)
                {
                    item.Overflow = ToolStripItemOverflow.AsNeeded;
                }
            }
            if (isControl)
            {
                return;
            }

            Image im = null;

            if (x.Attr(out s, "i2"))              //custom image as icon file
            {
                im = AIcon.GetFileIconImage(s);
                if (im == null)
                {
                    AOutput.Write($"Failed to get {(isMenu ? "menu item" : "toolbar button")} {x.Name} icon from file {s}\n\tTo fix this, right-click it and select Properties...");
                }
                //SHOULDDO: async or cache
            }
            if (im == null && x.Attr(out s, "i"))
            {
                im = _callbacks.GetImage(s);                                              //image from resources
            }
            item.Image = im;

            if (x.Attr(out s, "color") && ColorInt.FromString(s, out var color))
            {
                item.ForeColor = (Color)color;
            }
            else if (!_inBuildAll)
            {
                item.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
            }

            bool hasCustomText = x.Attr(out s, "t2");             //custom text

            item.Text = s ?? defaultText ?? _GetDefaultItemText(x);

            if (isMenu)
            {
                var mi = item as ToolStripMenuItem;
                if (!_inBuildAll)
                {
                    mi.ShortcutKeys             = 0;
                    mi.ShortcutKeyDisplayString = null;
                }
                if (x.Attr(out s, "hk"))
                {
                    bool ok = AKeys.More.ParseHotkeyString(s, out var hk);
                    if (ok)
                    {
                        try { mi.ShortcutKeys = hk; } catch { ok = false; }
                    }
                    if (!ok)
                    {
                        ADebug.Print("Invalid hotkey: " + s);
                    }
                }
                if (x.Attr(out string ss, "hkText"))
                {
                    mi.ShortcutKeyDisplayString = (s == null) ? ss : s + ", " + ss;
                }
            }
            else
            {
                var style = item.Image == null ? ToolStripItemDisplayStyle.ImageAndText : (ToolStripItemDisplayStyle)x.Attr("style", 0);
                if (style == 0)
                {
                    style = hasCustomText ? ToolStripItemDisplayStyle.ImageAndText : ToolStripItemDisplayStyle.Image;                            //0 is ToolStripItemDisplayStyle.None
                }
                item.DisplayStyle = style;
            }
        }

        /// <summary>
        /// If x has attribute t, gets its value.
        /// Else gets its name and converts to text, eg "File_OneTwo" to "One Two".
        /// </summary>
        string _GetDefaultItemText(XElement x)
        {
            if (!x.Attr(out string s, "t"))
            {
                string tag = x.Name.LocalName;
                s = tag.Remove(0, tag.LastIndexOf('_') + 1);                 //eg "Edit_Copy" -> "Copy"
                s = s.RegexReplace("(?<=[^A-Z])(?=[A-Z])", " ");             //"OneTwoThree" -> "One Two Three". //speed: don't need to optimize.
            }
            return(s);
        }

        /// <summary>
        /// Merges custom attributes into default menubar or toolbar XML.
        /// Reorders toolbar buttons if need.
        /// </summary>
        /// <param name="xCustom">Root element of customizations file.</param>
        /// <param name="xtsDef">Default menustrip or toolstrip. For custom toolbars the function can replace it.</param>
        /// <param name="name">xtsDef name, just to avoid getting it again.</param>
        /// <param name="isMenu"></param>
        void _MergeCustom(XElement xCustom, ref XElement xtsDef, string name, bool isMenu)
        {
            var xtsCust = xCustom.Element(xtsDef.Name); if (xtsCust == null)

            {
                return;
            }

            if (isMenu)
            {
            }
            else if (name.Starts("Custom"))
            {
                var xc = xCustom.Element(name); if (xc == null)
                {
                    return;
                }
                xc.Remove();
                xtsDef.ReplaceWith(xc);
                xtsDef = xc;
                return;
                //MSDN: "if the new content has no parent, then the objects are simply attached to the XML tree. If the new content already is parented and is part of another XML tree, then the new content is cloned". Tested, it's true.
            }
            else
            {
                //reorder toolbar buttons
                if (xtsCust.Attr(out string s, "order"))
                {
                    xtsDef.Elements("sep").Remove();                     //remove all default <sep/>, because all separators now are in the 'order' attribute
                    var a = s.SegSplit(" ");
                    for (int i = a.Length - 1; i >= 0; i--)
                    {
                        if (a[i] == "sep")
                        {
                            xtsDef.AddFirst(new XElement("sep"));
                        }
                        else
                        {
                            var xb = xtsDef.Element(a[i]); if (xb == null)
                            {
                                continue;
                            }
                            xb.Remove(); xtsDef.AddFirst(xb);
                        }
                    }
                }
            }

            foreach (var xCust in xtsCust.Elements())
            {
                foreach (var xDef in xtsDef.Descendants(xCust.Name))
                {
                    foreach (var att in xCust.Attributes())
                    {
                        xDef.SetAttributeValue(att.Name, att.Value);
                    }
                }
            }
        }

        /// <summary>
        /// Extracts differences between _xmlFileDefault and _xStrips and saves to _xmlFileCustom.
        /// </summary>
        void _DiffCustom()
        {
            var    xStripsDefault = AExtXml.LoadElem(_xmlFileDefault);
            var    xStripsCustom  = new XElement("strips");
            string s;

            //menus
            _DiffDescendants(MenuBar.Tag as XElement, true);

            //standard toolbars
            foreach (var ts in Toolbars.Values)
            {
                if (ts == _tsCustom1 || ts == _tsCustom2)
                {
                    continue;
                }
                _DiffDescendants(ts.Tag as XElement, false);
            }

            void _DiffDescendants(XElement xts, bool isMenu)
            {
                XElement xtsDef = xStripsDefault.Element(xts.Name), xtsCust = null;

                foreach (var x in xts.Descendants())
                {
                    var name = x.Name; if (name == "sep")
                    {
                        continue;
                    }
                    XElement xDef = xtsDef.Desc(name), xCust = null;
                    foreach (var att in x.Attributes())
                    {
                        var aname = att.Name;
                        if (att.Value == xDef.Attr(aname))
                        {
                            continue;
                        }
                        if (xtsCust == null)
                        {
                            xStripsCustom.Add(xtsCust = new XElement(xts.Name));
                        }
                        if (xCust == null)
                        {
                            xtsCust.Add(xCust = new XElement(name));
                        }
                        xCust.SetAttributeValue(aname, att.Value);
                    }
                }
                if (isMenu)
                {
                    return;
                }
                //order
                var s1 = new StringBuilder();
                var s2 = new StringBuilder();

                foreach (var x in xts.Elements())
                {
                    if (s1.Length > 0)
                    {
                        s1.Append(' ');
                    }
                    s1.Append(x.Name);
                }
                foreach (var x in xtsDef.Elements())
                {
                    if (s2.Length > 0)
                    {
                        s2.Append(' ');
                    }
                    s2.Append(x.Name);
                }
                s = s1.ToString();
                if (s != s2.ToString())
                {
                    if (xtsCust == null)
                    {
                        xStripsCustom.Add(xtsCust = new XElement(xts.Name));
                    }
                    xtsCust.SetAttributeValue("order", s);
                }
            }

            //custom toolbars. Temporarily move them from _xStrips to xCustom.
            var xCust1 = _tsCustom1.Tag as XElement;
            var xCust2 = _tsCustom2.Tag as XElement;

            if (xCust1.HasElements)
            {
                xCust1.Remove(); xStripsCustom.Add(xCust1);
            }
            if (xCust2.HasElements)
            {
                xCust2.Remove(); xStripsCustom.Add(xCust2);
            }

            //AOutput.Clear();
            //AOutput.Write(xStripsCustom);
#if true
            //save
            try {
                AFile.CreateDirectoryFor(_xmlFileCustom);
                xStripsCustom.SaveElem(_xmlFileCustom);
            }
            catch (Exception e) {
                AOutput.Write("Failed to save XML file", _xmlFileCustom, e.Message);
            }
#endif

            if (xCust1.HasElements)
            {
                xCust1.Remove(); _xStrips.Add(xCust1);
            }
            if (xCust2.HasElements)
            {
                xCust2.Remove(); _xStrips.Add(xCust2);
            }
        }
Exemple #21
0
 public override int GetHashCode()
 {
     ADebug.Print("GetHashCode");
     return(1);
 }
Exemple #22
0
    /// <summary>
    /// Processes command line of this program.
    /// Returns true if this instance must exit: 1. If finds previous program instance; then sends the command line to it if need. 2. If incorrect command line.
    /// </summary>
    public static bool OnProgramStarted(string[] a)
    {
        string s           = null;
        int    cmd         = 0;
        bool   activateWnd = true;

        if (a.Length > 0)
        {
            //AOutput.Write(a);

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i].Starts('-'))
                {
                    a[i] = a[i].ReplaceAt(0, 1, "/");
                }
                if (a[i].Starts('/'))
                {
                    a[i] = a[i].Lower();
                }
            }

            s = a[0];
            if (s.Starts('/'))
            {
                for (int i = 0; i < a.Length; i++)
                {
                    s = a[i];
                    switch (s)
                    {
                    case "/test":
                        if (++i < a.Length)
                        {
                            TestArg = a[i];
                        }
                        break;

                    case "/v":
                        StartVisible = true;
                        break;

                    default:
                        ADialog.ShowError("Unknown command line parameter", s);
                        return(true);
                    }
                    //rejected: /h start hidden. Not useful.
                }
            }
            else                 //one or more files
            {
                if (a.Length == 1 && FilesModel.IsWorkspaceDirectory(s))
                {
                    switch (cmd = ADialog.Show("Workspace", s, "1 Open|2 Import|0 Cancel", footerText: FilesModel.GetSecurityInfo("v|")))
                    {
                    case 1: WorkspaceDirectory = s; break;

                    case 2: _importWorkspace = s; break;
                    }
                }
                else
                {
                    cmd          = 3;
                    _importFiles = a;
                }
            }
        }

        //single instance
        s_mutex = new Mutex(true, "Au.Mutex.1", out bool createdNew);
        if (createdNew)
        {
            return(false);
        }

        var w = AWnd.FindFast(null, "Au.Editor.Msg", true);

        if (!w.Is0)
        {
            if (activateWnd)
            {
                AWnd wMain = (AWnd)w.Send(Api.WM_USER);
                if (!wMain.Is0)
                {
                    try { wMain.Activate(); }
                    catch (Exception ex) { ADebug.Print(ex); }
                }
            }

            switch (cmd)
            {
            case 3:             //import files
                s = string.Join("\0", a);
                break;
            }
            if (cmd != 0)
            {
                AWnd.More.CopyDataStruct.SendString(w, cmd, s);
            }
        }
        return(true);
    }
Exemple #23
0
    //HtmlRenderer bug: text-decoration underline is randomly too high or low if using 'Segoe UI' font of good size.
    //	Workaround: body { font: 10.5pt Calibri; }. Or could use it only for links, and use body { font: 9.8pt 'Segoe UI'; }. But Calibri is better.
    //HtmlRenderer's default font is 'Segoe UI' 11pt. WebBrowser's - 12pt (16px).

    public static void TaggedPartsToHtml(StringBuilder b, IEnumerable <TaggedText> tags)
    {
        if (tags == null)
        {
            return;
        }
        int i = -1, iBr = -2;

        foreach (var v in tags)
        {
            i++;
            //AOutput.Write($"{v.Tag}, '{v.Text}', {v.Style}");
            //AOutput.Write($"{v.Tag}, '{v.Text}', {v.Style}, navHint='{v.NavigationHint}', navTarget='{v.NavigationTarget}'");
            string s = v.Text, c = null;
            switch (v.Tag)
            {
            case TextTags.Class:
            case TextTags.Struct:
            case TextTags.Enum:
            case TextTags.Interface:
            case TextTags.Delegate:
            case TextTags.TypeParameter:
                c = "type";
                break;

            case TextTags.Keyword:
                c = "keyword";
                break;

            case TextTags.StringLiteral:
                c = "string";
                break;

            case TextTags.NumericLiteral:
                c = "number";
                break;

            case TextTags.Namespace:
                c = "namespace";
                break;

            case TextTags.LineBreak:
                b.Append(i == iBr + 1 ? "<div class='br2'></div>" : "<br>");
                iBr = i;
                continue;

            case TextTags.Punctuation:
            case TextTags.Method:             //eg operator <
            case TextTags.Operator:
                if (s.Length > 0 && (s[0] == '<' || s[0] == '>' || s[0] == '&'))
                {
                    s = WebUtility.HtmlEncode(s);                                                                             //eg < in X<T>
                }
                break;

            case TextTags.Text:
                s = WebUtility.HtmlEncode(s);
                //SHOULDDO: parse [][xref:topic_id]
                break;

#if DEBUG
            case TextTags.Space:
            case TextTags.Constant:
            case TextTags.EnumMember:
            case TextTags.Event:
            case TextTags.ExtensionMethod:
            case TextTags.Field:
            case TextTags.Local:
            case TextTags.Parameter:
            case TextTags.Property:
            case TextTags.RangeVariable:
            case TextTags.Alias:
            case TextTags.Label:
            case TextTags.ContainerStart:
            case TextTags.ContainerEnd:
            case TextTags.ErrorType:
                break;

            default:
                ADebug.Print($"{v.Tag}, '{v.Text}', {v.Style}");
                break;
#endif
            }

            switch (v.Style)
            {
            case TaggedTextStyle.Strong: b.Append("<b>"); break;

            case TaggedTextStyle.Emphasis: b.Append("<i>"); break;

            case TaggedTextStyle.Code: b.Append("<code>"); break;
            }

            if (c != null)
            {
                b.AppendFormat("<span class='{0}'>{1}</span>", c, s);
            }
            else
            {
                b.Append(s);
            }

            switch (v.Style)
            {
            case TaggedTextStyle.Code: b.Append("</code>"); break;

            case TaggedTextStyle.Emphasis: b.Append("</i>"); break;

            case TaggedTextStyle.Strong: b.Append("</b>"); break;
            }
        }
    }
Exemple #24
0
    /// <summary>
    /// Executes the compiled assembly in new process.
    /// Returns: process id if started now, 0 if failed, (int)ATask.ERunResult.deferred if scheduled to run later.
    /// </summary>
    /// <param name="f"></param>
    /// <param name="r"></param>
    /// <param name="args"></param>
    /// <param name="noDefer">Don't schedule to run later. If cannot run now, just return 0.</param>
    /// <param name="wrPipeName">Pipe name for ATask.WriteResult.</param>
    /// <param name="ignoreLimits">Don't check whether the task can run now.</param>
    /// <param name="runFromEditor">Starting from the Run button or menu Run command.</param>
    public unsafe int RunCompiled(FileNode f, Compiler.CompResults r, string[] args,
                                  bool noDefer = false, string wrPipeName = null, bool ignoreLimits = false, bool runFromEditor = false)
    {
g1:
        if (!ignoreLimits && !_CanRunNow(f, r, out var running, runFromEditor))
        {
            var  ifRunning = r.ifRunning;
            bool same      = running.f == f;
            if (!same)
            {
                ifRunning = r.ifRunning2 switch
                {
                    EIfRunning2.cancel => EIfRunning.cancel,
                    EIfRunning2.wait => EIfRunning.wait,
                    EIfRunning2.warn => EIfRunning.warn,
                    _ => (ifRunning & ~EIfRunning._restartFlag) switch { EIfRunning.cancel => EIfRunning.cancel, EIfRunning.wait => EIfRunning.wait, _ => EIfRunning.warn }
                };
            }
            else if (ifRunning.Has(EIfRunning._restartFlag))
            {
                if (runFromEditor)
                {
                    ifRunning = EIfRunning.restart;
                }
                else
                {
                    ifRunning &= ~EIfRunning._restartFlag;
                }
            }
            //AOutput.Write(same, ifRunning);
            switch (ifRunning)
            {
            case EIfRunning.cancel:
                break;

            case EIfRunning.wait when !noDefer:
                _q.Insert(0, new _WaitingTask(f, r, args));
                return((int)ATask.ERunResult.deferred);                //-1

            case EIfRunning.restart when _EndTask(running):
                goto g1;

            default:             //warn
                string s1 = same ? "it" : $"{running.f.SciLink}";
                AOutput.Write($"<>Cannot start {f.SciLink} because {s1} is running. You may want to <+properties \"{f.IdStringWithWorkspace}\">change<> <c green>ifRunning<>, <c green>ifRunning2<>, <c green>runMode<>.");
                break;
            }
            return(0);
        }

        _SpUac uac = _SpUac.normal; int preIndex = 0;

        if (!AUac.IsUacDisabled)
        {
            //info: to completely disable UAC on Win7: gpedit.msc/Computer configuration/Windows settings/Security settings/Local policies/Security options/User Account Control:Run all administrators in Admin Approval Mode/Disabled. Reboot.
            //note: when UAC disabled, if our uac is System, IsUacDisabled returns false (we probably run as SYSTEM user). It's OK.
            var IL = AUac.OfThisProcess.IntegrityLevel;
            if (r.uac == EUac.inherit)
            {
                switch (IL)
                {
                case UacIL.High: preIndex = 1; break;

                case UacIL.UIAccess: uac = _SpUac.uiAccess; preIndex = 2; break;
                }
            }
            else
            {
                switch (IL)
                {
                case UacIL.Medium:
                case UacIL.UIAccess:
                    if (r.uac == EUac.admin)
                    {
                        uac = _SpUac.admin;
                    }
                    break;

                case UacIL.High:
                    if (r.uac == EUac.user)
                    {
                        uac = _SpUac.userFromAdmin;
                    }
                    break;

                case UacIL.Low:
                case UacIL.Untrusted:
                case UacIL.Unknown:
                //break;
                case UacIL.System:
                case UacIL.Protected:
                    AOutput.Write($"<>Cannot run {f.SciLink}. Meta comment option <c green>uac {r.uac}<> cannot be used when the UAC integrity level of this process is {IL}. Supported levels are Medium, High and uiAccess.");
                    return(0);
                    //info: cannot start Medium IL process from System process. Would need another function. Never mind.
                }
                if (r.uac == EUac.admin)
                {
                    preIndex = 1;
                }
            }
        }

        string     exeFile, argsString;
        _Preloaded pre = null; byte[] taskParams = null;
        bool       bit32 = r.prefer32bit || AVersion.Is32BitOS;

        if (r.notInCache)          //meta role exeProgram
        {
            exeFile    = Compiler.DllNameToAppHostExeName(r.file, bit32);
            argsString = args == null ? null : Au.Util.AStringUtil.CommandLineFromArray(args);
        }
        else
        {
            exeFile = AFolders.ThisAppBS + (bit32 ? "Au.Task32.exe" : "Au.Task.exe");

            //int iFlags = r.hasConfig ? 1 : 0;
            int iFlags = 0;
            if (r.mtaThread)
            {
                iFlags |= 2;
            }
            if (r.console)
            {
                iFlags |= 4;
            }
            taskParams = Au.Util.Serializer_.SerializeWithSize(r.name, r.file, r.pdbOffset, iFlags, args, r.fullPathRefs, wrPipeName, (string)AFolders.Workspace);
            wrPipeName = null;

            if (bit32 && !AVersion.Is32BitOS)
            {
                preIndex += 3;
            }
            pre        = s_preloaded[preIndex] ??= new _Preloaded(preIndex);
            argsString = pre.pipeName;
        }

        int pid; WaitHandle hProcess = null; bool disconnectPipe = false;

        try {
            //APerf.First();
            var pp = pre?.hProcess;
            if (pp != null && 0 != Api.WaitForSingleObject(pp.SafeWaitHandle.DangerousGetHandle(), 0))              //preloaded process exists
            {
                hProcess     = pp; pid = pre.pid;
                pre.hProcess = null; pre.pid = 0;
            }
            else
            {
                if (pp != null)
                {
                    pp.Dispose(); pre.hProcess = null; pre.pid = 0;
                }                                                                                  //preloaded process existed but somehow ended
                (pid, hProcess) = _StartProcess(uac, exeFile, argsString, wrPipeName);
            }
            Api.AllowSetForegroundWindow(pid);

            if (pre != null)
            {
                //APerf.First();
                var o = new Api.OVERLAPPED {
                    hEvent = pre.overlappedEvent
                };
                if (!Api.ConnectNamedPipe(pre.hPipe, &o))
                {
                    int e = ALastError.Code;
                    if (e != Api.ERROR_PIPE_CONNECTED)
                    {
                        if (e != Api.ERROR_IO_PENDING)
                        {
                            throw new AuException(e);
                        }
                        var ha = stackalloc IntPtr[2] {
                            pre.overlappedEvent, hProcess.SafeWaitHandle.DangerousGetHandle()
                        };
                        int wr = Api.WaitForMultipleObjectsEx(2, ha, false, -1, false);
                        if (wr != 0)
                        {
                            Api.CancelIo(pre.hPipe); throw new AuException("*start task. Preloaded task process ended");
                        }                                                                                                                                    //note: if fails when 32-bit process, rebuild solution with platform x86
                        disconnectPipe = true;
                        if (!Api.GetOverlappedResult(pre.hPipe, ref o, out _, false))
                        {
                            throw new AuException(0);
                        }
                    }
                }
                //APerf.Next();
                if (!Api.WriteFileArr(pre.hPipe, taskParams, out _))
                {
                    throw new AuException(0);
                }
                //APerf.Next();
                Api.DisconnectNamedPipe(pre.hPipe); disconnectPipe = false;
                //APerf.NW('e');

                //start preloaded process for next task. Let it wait for pipe connection.
                if (uac != _SpUac.admin)                  //we don't want second UAC consent
                {
                    try { (pre.pid, pre.hProcess) = _StartProcess(uac, exeFile, argsString, null); }
                    catch (Exception ex) { ADebug.Print(ex); }
                }
            }
        }
        catch (Exception ex) {
            AOutput.Write(ex);
            if (disconnectPipe)
            {
                Api.DisconnectNamedPipe(pre.hPipe);
            }
            hProcess?.Dispose();
            return(0);
        }

        var rt = new RunningTask(f, hProcess, r.runMode != ERunMode.green);

        _Add(rt);
        return(pid);
    }
Exemple #25
0
        bool _FillGrid(bool newWindow)
        {
            bool isCon = !_con.Is0;

            var g = _grid;

            if (newWindow)
            {
                g.ZClear();
            }
            else
            {
                g.RowsCount = 5;
            }

            _WinInfo f = default;

            if (!_GetClassName(_wnd, out f.wClass))
            {
                return(false);                                               //note: get even if !newWindow, to detect closed window
            }
            if (isCon && !_GetClassName(_con, out f.cClass))
            {
                return(false);
            }
            _propError = null;

            _noeventGridValueChanged = true;

            var wndName = _wnd.NameTL_;

            if (newWindow)
            {
                g.ZAddHeaderRow("Window");
                g.ZAdd(null, "name", TUtil.EscapeWindowName(wndName, true), true, info: "Window name.$");
                g.ZAdd(null, "class", TUtil.StripWndClassName(f.wClass, true), true, info: "Window class name.$");
                f.wProg = _wnd.ProgramName;
                var ap = new List <string> {
                    f.wProg, "WOwner.Process(processId)", "WOwner.Thread(threadId)"
                }; if (!_wnd.OwnerWindow.Is0)
                {
                    ap.Add("WOwner.Window(ow)");
                }
                g.ZAdd(null, "program", ap, wndName.NE(), info: "Program.$", etype: ParamGrid.EditType.ComboText, comboIndex: 0);
                g.ZAdd(null, "contains", (Func <string[]>)_ContainsCombo_DropDown, false, info: "An accessible object in the window. Format: 'role' name.\r\nName$$", etype: ParamGrid.EditType.ComboText);
            }
            else if (wndName != _wndName)
            {
                if (TUtil.ShouldChangeGridWildex(g.ZGetCellText("name", 1), wndName))
                {
                    g.ZSetCellText("name", 1, TUtil.EscapeWindowName(wndName, true));
                }
            }
            f.wName = _wndName = wndName;

            if (isCon)
            {
                g.ZAddHeaderRow("Control", check: !_uncheckControl);
                g.ZAddHidden = _uncheckControl;

                //name combo
                f.cName = _con.Name;
                int iSel = f.cName.NE() ? -1 : 0;
                var an   = new List <string> {
                    TUtil.EscapeWildex(f.cName)
                };
                _ConNameAdd("***wfName ", f.cWF = _con.NameWinForms);
                /*bool isAcc =*/
                _ConNameAdd("***accName ", f.cAcc = _con.NameAcc);
                //bool isLabel = _ConNameAdd("***label ", f.cLabel = _con.NameLabel);
                //if(isAcc && isLabel && iSel == an.Count - 2 && f.cAcc == f.cLabel) iSel++; //if label == accName, prefer label
                if (iSel < 0)
                {
                    iSel = 0;                          //never select text, even if all others unavailable
                }
                _ConNameAdd("***text ", f.cText = _con.ControlText);
                bool _ConNameAdd(string prefix, string value)
                {
                    if (value.NE())
                    {
                        return(false);
                    }
                    if (iSel < 0)
                    {
                        iSel = an.Count;
                    }
                    an.Add(prefix + TUtil.EscapeWildex(value));
                    return(true);
                }

                bool idUseful = TUtil.GetUsefulControlId(_con, _wnd, out f.cId);
                if (idUseful)
                {
                    g.ZAdd(null, "id", f.cId, true);
                }
                else
                {
                    an.Add("***id " + f.cId + " (probably not useful)");
                }
                g.ZAdd("nameC", "name", an, !idUseful, info: "Control name.$", etype: ParamGrid.EditType.ComboText, comboIndex: iSel);
                g.ZAdd("classC", "class", TUtil.StripWndClassName(f.cClass, true), !idUseful, info: "Control class name.$");
                g.ZAddHidden = false;
            }

            _uncheckControl          = false;
            _noeventGridValueChanged = false;
            g.ZAutoSize();
            _FillWindowInfo(f);
            return(true);

            string[] _ContainsCombo_DropDown()
            {
                try {
                    var a1 = new List <string>();
                    //child
                    foreach (var c in _wnd.Get.Children(onlyVisible: true))
                    {
                        var cn = c.Name; if (cn.NE())
                        {
                            continue;
                        }
                        cn = "c '" + TUtil.StripWndClassName(c.ClassName, true) + "' " + TUtil.EscapeWildex(cn);
                        if (!a1.Contains(cn))
                        {
                            a1.Add(cn);
                        }
                    }
                    //acc
                    var    a2       = new List <string>();
                    var    a3       = AAcc.FindAll(_wnd, name: "?*", prop: "notin=SCROLLBAR\0maxcc=100", flags: AFFlags.ClientArea);            //all that have a name
                    string prevName = null;
                    for (int i = a3.Length; --i >= 0;)
                    {
                        if (!a3[i].GetProperties("Rn", out var prop))
                        {
                            continue;
                        }
                        if (prop.Name == prevName && prop.Role == "WINDOW")
                        {
                            continue;
                        }
                        prevName = prop.Name;                                                                                      //skip parent WINDOW
                        string rn = "a '" + prop.Role + "' " + TUtil.EscapeWildex(prop.Name);
                        if (!a2.Contains(rn))
                        {
                            a2.Add(rn);
                        }
                    }
                    a2.Reverse();
                    a1.AddRange(a2);

                    return(a1.ToArray());
                    //rejected: sort
                }
                catch (Exception ex) { ADebug.Print(ex); return(null); }
            }

            bool _GetClassName(AWnd w, out string cn)
            {
                cn = w.ClassName;
                if (cn != null)
                {
                    return(true);
                }
                _propError = "Failed to get " + (w == _wnd ? "window" : "control") + " properties: \r\n" + ALastError.Message;
                _grid.ZClear();
                _grid.Invalidate();
                _winInfo.Z.ClearText();
                return(false);
            }
        }
Exemple #26
0
            LPARAM _WndProc(AWnd w, int message, LPARAM wParam, LPARAM lParam)
            {
                //AWnd.More.PrintMsg(w, message, wParam, lParam);

                switch (message)
                {
                //case Api.WM_DESTROY:
                //	Api.ChangeClipboardChain(w, _wPrevClipViewer);
                //	break;
                case Api.WM_RENDERFORMAT:
                    if (_paste && !Success)
                    {
                        IsBadWindow = !_IsTargetWindow();

                        //note: need to set clipboard data even if bad window.
                        //	Else the clipboard program may retry in loop. Eg Ditto. Then often pasting fails.
                        //	If IsBadWindow, we'll then sleep briefly.
                        //	Good clipboard programs get clipboard data with a delay. Eg Ditto default is 100 ms and can be changed. Therefore usually they don't interfere, unless the target app is very slow.
                        //	Also, after setting clipboard data we cannot wait for good window, because we'll not receive second WM_RENDERFORMAT.

                        try { _SetClipboard(_data, false); }
                        catch (Exception ex) { FailedToSetData = ex; }                        //cannot throw in wndproc, will throw later
                        waitVar = true;
                    }
                    return(0);

                case Api.WM_CLIPBOARDUPDATE:
                    //this message was added in Vista. It is posted, not sent. Once, not for each format. QM2 used SetClipboardViewer/WM_DRAWCLIPBOARD.
                    if (!_paste)
                    {
                        waitVar = true;
                    }
                    return(0);
                }

                return(Api.DefWindowProc(w, message, wParam, lParam));

                //Returns false if probably not the target app reads from the clipboard. Probably a clipboard viewer/manager/etc.
                bool _IsTargetWindow()
                {
                    AWnd wOC = Api.GetOpenClipboardWindow();

                    //int color = 0; if(wOC != _wFocus) color = wOC.ProcessId == _wFocus.ProcessId ? 0xFF0000 : 0xFF;
                    //AOutput.Write($"<><c {color}>{wOC}</c>");

                    if (wOC == _wFocus)
                    {
                        return(true);
                    }
                    if (wOC.Is0)
                    {
                        return(true);                            //tested: none of tested apps calls OpenClipboard(0)
                    }
                    if (wOC.ProcessId == _wFocus.ProcessId)
                    {
                        return(true);                                                       //often classnamed "CLIPBRDWNDCLASS". Some clipboard managers are classnamed so too, eg Ditto.
                    }
                    if (wOC.ProgramName.Eqi("RuntimeBroker.exe"))
                    {
                        return(true);                                                             //Edge, Store apps
                    }
                    //CONSIDER: option to return true for user-known windows, eg using a callback. Print warning that includes wOC info.

                    ADebug.Print(wOC.ToString());
                    return(false);

                    //BlueStacks problems:
                    //	Uses an aggressive viewer. Always debugprints while it is running, even when other apps are active.
                    //	Sometimes pastes old text, usually after starting BlueStacks or after some time of not using it.
                    //		With or without clipboard restoring.
                    //		Then starts to work correctly always. Difficult to debug.
                    //		KeySpeedClipboard=100 usually helps, but sometimes even 300 does not help.
                }
            }
Exemple #27
0
            /// <summary>
            /// Creates documentation provider for assembly <i>asmPath</i>.
            /// Returns null if its xml file does not exist.
            /// Returns _DocumentationProvider if xml file is big and found or successfully created and successfully loaded database for it.
            /// Else returns XmlDocumentationProvider.
            /// </summary>
            public static DocumentationProvider Create(string asmPath)
            {
                if (s_d.TryGetValue(asmPath, out var dp))
                {
                    return(dp);
                }

                var xmlPath = Path.ChangeExtension(asmPath, "xml");

                if (!AFile.GetProperties(xmlPath, out var px))
                {
                    return(null);
                }

                if (px.Size >= 10_000)
                {
                    var md5    = new Au.Util.AHash.MD5(); md5.Add(xmlPath.Lower());
                    var dbPath = AFolders.ThisAppTemp + md5.Hash.ToString() + ".db";
                    try {
                        if (!AFile.GetProperties(dbPath, out var pd) || pd.LastWriteTimeUtc != px.LastWriteTimeUtc)
                        {
                            //ADebug.Print($"creating db: {asmPath}  ->  {dbPath}");
                            AFile.Delete(dbPath);
                            using (var d = new ASqlite(dbPath)) {
                                using var trans = d.Transaction();
                                d.Execute("CREATE TABLE doc (name TEXT PRIMARY KEY, xml TEXT)");
                                using var statInsert = d.Statement("INSERT INTO doc VALUES (?, ?)");

                                var xr = AExtXml.LoadElem(xmlPath);
                                foreach (var e in xr.Descendants("member"))
                                {
                                    var name = e.Attr("name");

                                    //remove <remarks> and <example>.
                                    foreach (var v in e.Descendants("remarks").ToArray())
                                    {
                                        v.Remove();
                                    }
                                    foreach (var v in e.Descendants("example").ToArray())
                                    {
                                        v.Remove();
                                    }

                                    using var reader = e.CreateReader();
                                    reader.MoveToContent();
                                    var xml = reader.ReadInnerXml();
                                    //AOutput.Write(name, xml);

                                    statInsert.BindAll(name, xml).Step();
                                    statInsert.Reset();
                                }
                                trans.Commit();
                                d.Execute("VACUUM");
                            }
                            File.SetLastWriteTimeUtc(dbPath, px.LastWriteTimeUtc);
                        }
                        var db = new ASqlite(dbPath, SLFlags.SQLITE_OPEN_READONLY);                         //never mind: we don't dispose it on process exit
                        s_d[asmPath] = dp = new _DocumentationProvider {
                            _db = db
                        };
                        return(dp);
                    }
                    catch (Exception ex) { ADebug.Print(ex.ToStringWithoutStack()); }
                }
                return(XmlDocumentationProvider.CreateFromFile(xmlPath));
            }