Example #1
0
        public T CustomLoad <T>(bool focus = true)
            where T : class, IToolForm
        {
            IToolForm newTool = CreateInstance <T>("");

            if (newTool == null)
            {
                return(null);
            }

            if (newTool is Form)
            {
                (newTool as Form).Owner = GlobalWin.MainForm;
            }

            ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool);
            string toolType = typeof(T).ToString();

            // auto settings
            if (newTool is IToolFormAutoConfig)
            {
                ToolDialogSettings settings;
                if (!Global.Config.CommonToolSettings.TryGetValue(toolType, out settings))
                {
                    settings = new ToolDialogSettings();
                    Global.Config.CommonToolSettings[toolType] = settings;
                }

                AttachSettingHooks(newTool as IToolFormAutoConfig, settings);
            }
            return((T)newTool);
        }
Example #2
0
        private static void RefreshSettings(Form form, ToolStripItemCollection menu, ToolDialogSettings settings, int idx)
        {
            ((ToolStripMenuItem)menu[idx + 0]).Checked = settings.SaveWindowPosition;
            ((ToolStripMenuItem)menu[idx + 1]).Checked = settings.TopMost;
            ((ToolStripMenuItem)menu[idx + 2]).Checked = settings.FloatingWindow;
            ((ToolStripMenuItem)menu[idx + 3]).Checked = settings.AutoLoad;

            form.TopMost = settings.TopMost;

            // do we need to do this OnShown() as well?
            form.Owner = settings.FloatingWindow ? null : GlobalWin.MainForm;
        }
Example #3
0
        private void RefreshSettings(Form form, ToolStripItemCollection menu, ToolDialogSettings settings, int idx)
        {
            ((ToolStripMenuItem)menu[idx + 0]).Checked = settings.SaveWindowPosition;
            var stayOnTopItem = (ToolStripMenuItem)menu[idx + 1];

            stayOnTopItem.Checked = settings.TopMost;
            if (OSTailoredCode.IsUnixHost)
            {
                // This is the job of the WM, and is usually exposed in window decorations or a context menu on them
                stayOnTopItem.Enabled = false;
                stayOnTopItem.Visible = false;
            }
            else
            {
                form.TopMost = settings.TopMost;
            }
            ((ToolStripMenuItem)menu[idx + 2]).Checked = settings.FloatingWindow;
            ((ToolStripMenuItem)menu[idx + 3]).Checked = settings.AutoLoad;

            // do we need to do this OnShown() as well?
            form.Owner = settings.FloatingWindow ? null : _owner;
        }
Example #4
0
        /// <summary>
        /// Loads the tool dialog T (T must implement <see cref="IToolForm"/>) , if it does not exist it will be created, if it is already open, it will be focused
        /// </summary>
        /// <typeparam name="T">Type of tool you want to load</typeparam>
        /// <param name="toolPath">Path to the .dll of the external tool</param>
        /// <param name="focus">Define if the tool form has to get the focus or not (Default is true)</param>
        /// <returns>An instantiated <see cref="IToolForm"/></returns>
        public T Load <T>(string toolPath, bool focus = true)
            where T : class, IToolForm
        {
            bool isExternal = typeof(T) == typeof(IExternalToolForm);

            if (!IsAvailable <T>() && !isExternal)
            {
                return(null);
            }

            T existingTool;

            if (isExternal)
            {
                existingTool = (T)_tools.FirstOrDefault(t => t is T && t.GetType().Assembly.Location == toolPath);
            }
            else
            {
                existingTool = (T)_tools.FirstOrDefault(t => t is T);
            }

            if (existingTool != null)
            {
                if (existingTool.IsDisposed)
                {
                    _tools.Remove(existingTool);
                }
                else
                {
                    if (focus)
                    {
                        existingTool.Show();
                        existingTool.Focus();
                    }

                    return(existingTool);
                }
            }

            IToolForm newTool = CreateInstance <T>(toolPath);

            if (newTool == null)
            {
                return(null);
            }

            if (newTool is Form)
            {
                (newTool as Form).Owner = GlobalWin.MainForm;
            }

            if (isExternal)
            {
                ApiInjector.UpdateApis(GlobalWin.ApiProvider, newTool);
            }

            ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool);
            string toolType = typeof(T).ToString();

            // auto settings
            if (newTool is IToolFormAutoConfig)
            {
                ToolDialogSettings settings;
                if (!Global.Config.CommonToolSettings.TryGetValue(toolType, out settings))
                {
                    settings = new ToolDialogSettings();
                    Global.Config.CommonToolSettings[toolType] = settings;
                }

                AttachSettingHooks(newTool as IToolFormAutoConfig, settings);
            }

            // custom settings
            if (HasCustomConfig(newTool))
            {
                if (!Global.Config.CustomToolSettings.TryGetValue(toolType, out var settings))
                {
                    settings = new Dictionary <string, object>();
                    Global.Config.CustomToolSettings[toolType] = settings;
                }

                InstallCustomConfig(newTool, settings);
            }

            newTool.Restart();
            if (!OSTailoredCode.IsWindows() && newTool is RamSearch)
            {
                // the mono winforms implementation is buggy, skip to the return statement and call Show in MainForm instead
            }
            else
            {
                newTool.Show();
            }
            return((T)newTool);
        }
Example #5
0
        private void AttachSettingHooks(IToolFormAutoConfig tool, ToolDialogSettings settings)
        {
            var form = (Form)tool;
            ToolStripItemCollection dest = null;
            var oldsize = form.Size;             // this should be the right time to grab this size

            foreach (Control c in form.Controls)
            {
                if (c is MenuStrip)
                {
                    var ms = c as MenuStrip;
                    foreach (ToolStripMenuItem submenu in ms.Items)
                    {
                        if (submenu.Text.Contains("Settings"))
                        {
                            dest = submenu.DropDownItems;
                            dest.Add(new ToolStripSeparator());
                            break;
                        }
                    }

                    if (dest == null)
                    {
                        var submenu = new ToolStripMenuItem("&Settings");
                        ms.Items.Add(submenu);
                        dest = submenu.DropDownItems;
                    }

                    break;
                }
            }

            if (dest == null)
            {
                throw new InvalidOperationException($"{nameof(IToolFormAutoConfig)} must have menu to bind to!");
            }

            int idx = dest.Count;

            dest.Add("Save Window &Position");
            dest.Add("Stay on &Top");
            dest.Add("&Float from Parent");
            dest.Add("&Autoload");
            dest.Add("Restore &Defaults");

            RefreshSettings(form, dest, settings, idx);

            if (settings.UseWindowPosition && IsOnScreen(settings.TopLeft))
            {
                form.StartPosition = FormStartPosition.Manual;
                form.Location      = settings.WindowPosition;
            }

            if (settings.UseWindowSize)
            {
                if (form.FormBorderStyle == FormBorderStyle.Sizable || form.FormBorderStyle == FormBorderStyle.SizableToolWindow)
                {
                    form.Size = settings.WindowSize;
                }
            }

            form.FormClosing += (o, e) =>
            {
                if (form.WindowState == FormWindowState.Normal)
                {
                    settings.Wndx = form.Location.X;
                    settings.Wndy = form.Location.Y;
                    if (settings.Wndx < 0)
                    {
                        settings.Wndx = 0;
                    }

                    if (settings.Wndy < 0)
                    {
                        settings.Wndy = 0;
                    }

                    settings.Width  = form.Right - form.Left;                    // why not form.Size.Width?
                    settings.Height = form.Bottom - form.Top;
                }
            };

            dest[idx + 0].Click += (o, e) =>
            {
                bool val = !((ToolStripMenuItem)o).Checked;
                settings.SaveWindowPosition    = val;
                ((ToolStripMenuItem)o).Checked = val;
            };
            dest[idx + 1].Click += (o, e) =>
            {
                bool val = !((ToolStripMenuItem)o).Checked;
                settings.TopMost = val;
                ((ToolStripMenuItem)o).Checked = val;
                form.TopMost = val;
            };
            dest[idx + 2].Click += (o, e) =>
            {
                bool val = !((ToolStripMenuItem)o).Checked;
                settings.FloatingWindow        = val;
                ((ToolStripMenuItem)o).Checked = val;
                form.Owner = val ? null : _owner;
            };
            dest[idx + 3].Click += (o, e) =>
            {
                bool val = !((ToolStripMenuItem)o).Checked;
                settings.AutoLoad = val;
                ((ToolStripMenuItem)o).Checked = val;
            };
            dest[idx + 4].Click += (o, e) =>
            {
                settings.RestoreDefaults();
                RefreshSettings(form, dest, settings, idx);
                form.Size = oldsize;

                form.GetType()
                .GetMethodsWithAttrib(typeof(RestoreDefaultsAttribute))
                .FirstOrDefault()
                ?.Invoke(form, new object[0]);
            };
        }
Example #6
0
        /// <summary>
        /// Loads a tool dialog of type toolType if it does not exist it will be
        /// created, if it is already open, it will be focused.
        /// </summary>
        public IToolForm Load(Type toolType, bool focus = true)
        {
            if (!typeof(IToolForm).IsAssignableFrom(toolType))
            {
                throw new ArgumentException(String.Format("Type {0} does not implement IToolForm.", toolType.Name));
            }

            if (!ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, toolType))
            {
                return(null);
            }

            var existingTool = _tools.FirstOrDefault(x => toolType.IsAssignableFrom(x.GetType()));

            if (existingTool != null)
            {
                if (existingTool.IsDisposed)
                {
                    _tools.Remove(existingTool);
                }
                else
                {
                    if (focus)
                    {
                        existingTool.Show();
                        existingTool.Focus();
                    }
                    return(existingTool);
                }
            }

            var newTool = CreateInstance(toolType);

            if (newTool is Form)
            {
                (newTool as Form).Owner = GlobalWin.MainForm;
            }

            ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool);

            // auto settings
            if (newTool is IToolFormAutoConfig)
            {
                ToolDialogSettings settings;
                if (!Global.Config.CommonToolSettings.TryGetValue(toolType.ToString(), out settings))
                {
                    settings = new ToolDialogSettings();
                    Global.Config.CommonToolSettings[toolType.ToString()] = settings;
                }
                AttachSettingHooks(newTool as IToolFormAutoConfig, settings);
            }

            // custom settings
            if (HasCustomConfig(newTool))
            {
                Dictionary <string, object> settings;
                if (!Global.Config.CustomToolSettings.TryGetValue(toolType.ToString(), out settings))
                {
                    settings = new Dictionary <string, object>();
                    Global.Config.CustomToolSettings[toolType.ToString()] = settings;
                }
                InstallCustomConfig(newTool, settings);
            }

            newTool.Restart();
            newTool.Show();
            return(newTool);
        }