private void dragContainerControl_DragControlRemoved(DragContainerControl container, DragControl dragControl)
        {
            if (dragControl.ControlContained == _checkedControl)
            {
                ChangeCheckedControl(null);
                return;
            }

            if (dragControl.ControlContained == null)
            {
                return;
            }

            DialogResult result = MessageBox.Show("Remove component?" + Environment.NewLine + "- Click [Yes] to Remove" + Environment.NewLine + "- Click [No] to Send to Tabbed", "Open Forex Platform", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {// Remove.
                CommonBaseControl control = (CommonBaseControl)dragControl.ControlContained;
                dragControl.ControlContained = null;
                RemoveControl(control);
            }
            else if (result == DialogResult.No)
            {// Send to tabbed.
                DoRemoveControl((CommonBaseControl)dragControl.ControlContained);
                AddTabbedControl((CommonBaseControl)dragControl.ControlContained);
            }
        }
        void dragStrip_DoubleClick(object sender, EventArgs e)
        {
            CommonBaseControl control = ((DragStripControl)sender).ParentControl.ControlContained as CommonBaseControl;

            ((DragStripControl)sender).ParentControl.ControlContained = null;
            dragContainerControl.RemoveDragControl(((DragStripControl)sender).ParentControl);
            SetControlFloating(control);
        }
        /// <summary>
        ///
        /// </summary>
        public void ClearControls()
        {
            _checkedControl = null;
            _floatingFormsControls.Clear();
            _tabbedButtonsControls.Clear();

            dragContainerControl.ClearControls();
        }
 /// <summary>
 ///
 /// </summary>
 Image GetControlImage(CommonBaseControl control)
 {
     if (_imageList == null || _imageList.Images.ContainsKey(control.ImageName) == false)
     {
         return(null);
     }
     else
     {
         return(_imageList.Images[control.ImageName]);
     }
 }
        /// <summary>
        /// 
        /// </summary>
        public CombinedHostingForm(CommonBaseControl control)
        {
            InitializeComponent();

            this.Text = Application.ProductName + " - " + control.Name;

            this.Controls.Add(control);
            _control = control;

            //this.TopLevel = false;
        }
        /// <summary>
        ///
        /// </summary>
        public CombinedHostingForm(CommonBaseControl control)
        {
            InitializeComponent();

            this.Text = Application.ProductName + " - " + control.Name;

            this.Controls.Add(control);
            _control = control;

            //this.TopLevel = false;
        }
        void PersistControlData(CommonBaseControl control, Component containingComponent)
        {
            control.SaveState();

            Point?location = null;
            Size? size     = null;

            if (containingComponent == null)
            {
                return;
            }

            // Since this info will be cleared on next line, store here for reference.
            bool isChecked = control.PersistenceData.ContainsValue("combinedContainer.Checked") &&
                             control.PersistenceData.GetBoolean("combinedContainer.Checked");

            // Clear all previous persistence info related to "combinedContainer".
            control.PersistenceData.ClearByNamePart("combinedContainer.");

            if (containingComponent is ToolStripButton ||
                control == _checkedControl)
            {
                control.PersistenceData.AddValue("combinedContainer.Tabbed", true);
                control.PersistenceData.AddValue("combinedContainer.Checked", isChecked);
                control.PersistenceData.AddValue("combinedContainer.TabIndex", toolStripMain.Items.IndexOf((ToolStripButton)containingComponent));
            }
            else if (containingComponent is CombinedHostingForm)
            {
                location = ((CombinedHostingForm)containingComponent).Location;
                size     = ((CombinedHostingForm)containingComponent).Size;
                control.PersistenceData.AddValue("combinedContainer.Floating", true);
            }
            else if (containingComponent is DragControl)
            {
                control.PersistenceData.AddValue("combinedContainer.Docked", true);
                control.PersistenceData.AddValue("combinedContainer.Guid", ((DragControl)containingComponent).Guid);
            }
            else
            {
                SystemMonitor.Warning("Unrecognized case.");
                return;
            }

            if (location.HasValue)
            {
                control.PersistenceData.AddValue("combinedContainer.Location", location.Value);
            }

            if (size.HasValue)
            {
                control.PersistenceData.AddValue("combinedContainer.Size", size.Value);
            }
        }
        /// <summary>
        /// Helper.
        /// </summary>
        void SetControlToDragControl(CommonBaseControl control, DragControl dragControl, Point location)
        {
            SystemMonitor.CheckError(dragControl.ControlContained == null, "Drag control already has a contained control.");

            dragControl.ControlContained = control;
            dragControl.Name             = control.Name;
            dragControl.Location         = location;

            dragControl.Image = GetControlImage(control);
            dragControl.dragStrip.DoubleClick += new EventHandler(dragStrip_DoubleClick);

            UpdateUI();
        }
        void combinedHostingForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            CombinedHostingForm form = sender as CombinedHostingForm;

            if (form.ContainedControl == null)
            {
                _floatingFormsControls.RemoveByKey(form);
                return;
            }

            // Unsubscribe events.
            form.ResizeBegin -= new EventHandler(combinedHostingForm_ResizeBegin);
            form.ResizeEnd   -= new EventHandler(combinedHostingForm_ResizeEnd);

            form.FormClosing -= new FormClosingEventHandler(combinedHostingForm_FormClosing);
            form.Activated   -= new EventHandler(combinedHostingForm_Activated);

            if (e.CloseReason != CloseReason.UserClosing)
            {// We are doing a major application close, do not ask anything.
                // Persist data form this form since it is being closed and will no longer have it properly assigned.
                PersistControlData(form.ContainedControl, form);
                CommonBaseControl control = form.ContainedControl;
                form.ContainedControl = null;

                // Clear from list of internal controls.
                DoRemoveControl(control);

                return;
            }


            DialogResult result = MessageBox.Show("Remove component?" + Environment.NewLine + "+ Click [Yes] to Remove" + Environment.NewLine + "- Click [No] to Send to Tabbed", "Open Forex Platform", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {// Remove.
                CommonBaseControl containedControl = form.ContainedControl;
                form.ContainedControl = null;
                RemoveControl(containedControl);
            }
            else if (result == DialogResult.No)
            {// Send to tabbed.
                _floatingFormsControls.RemoveByKey(form);
                form.Controls.Clear();
                AddTabbedControl(form.ContainedControl);
            }
            else if (result == DialogResult.Cancel)
            {// Keep it unchanged.
                e.Cancel = true;
            }
        }
        /// <summary>
        ///
        /// </summary>
        public bool RemoveControl(CommonBaseControl control)
        {
            // We shall not invoke control.UnInitializeControl(); here, since user may ultimately wish to use the control
            // further in another context.

            DoRemoveControl(control);

            if (ContainedControlRemovedEvent != null)
            {
                ContainedControlRemovedEvent(this, control);
            }

            // Raise control removed event.
            return(true);
        }
        /// <summary>
        ///
        /// </summary>
        public void ChangeCheckedControl(CommonBaseControl control)
        {
            if (_checkedControl == control)
            {
                return;
            }

            if (_checkedControlContainer == null)
            {// Creating a new checked container.
                _checkedControlContainer = new DragControl();
                _checkedControlContainer.ShowDragStrip = false;
                dragContainerControl.AddDragControl(_checkedControlContainer);
                dragContainerControl.DockControl(_checkedControlContainer, DockStyle.Fill);
            }

            if (control == null)
            {
                _checkedControlContainer.ControlContained = null;
            }
            else
            {
                _checkedControlContainer.ControlContained = control;
                _checkedControlContainer.Name             = control.Name;
            }

            if (_checkedControl != null && _checkedControl.PersistenceData != null && control != null)
            {
                _checkedControl.PersistenceData.AddValue("combinedContainer.Checked", false);
            }

            _checkedControl = control;

            if (_checkedControl != null && _checkedControl.PersistenceData != null)
            {
                _checkedControl.PersistenceData.AddValue("combinedContainer.Checked", true);
            }

            if (SelectedTabbedControlChangedEvent != null)
            {
                SelectedTabbedControlChangedEvent(this, _checkedControl);
            }

            UpdateUI();
        }
        /// <summary>
        /// Add a floating control to the workspace area.
        /// location considered only for floating (dockStyle = none)
        /// </summary>
        public DragControl AddWorkspaceControl(CommonBaseControl control, DockStyle dockPane, Point location)
        {
            if (dragContainerControl.GetDragControlFromContainedControl(control) != null)
            {
                SystemMonitor.Warning("Misuse.");
                return(null);
            }

            DragControl dragControl = new DragControl();

            SetControlToDragControl(control, dragControl, location);

            dragContainerControl.AddDragControl(dragControl);
            dragContainerControl.DockControl(dragControl, dockPane);

            UpdateUI();

            return(dragControl);
        }
        /// <summary>
        /// Actual remove the control from managed controls.
        /// </summary>
        void DoRemoveControl(CommonBaseControl control)
        {
            if (control == _checkedControl)
            {
                // Try to change the current checked control.
                foreach (ToolStripButton button in _tabbedButtonsControls.Keys)
                {
                    if (_tabbedButtonsControls.GetByKey(button) != control)
                    {
                        ChangeCheckedControl(_tabbedButtonsControls.GetByKey(button));
                        break;
                    }
                }

                if (control == _checkedControl)
                {// If we failed to change it to another one, change to null.
                    ChangeCheckedControl(null);
                }
            }


            if (_tabbedButtonsControls.ContainsValue(control))
            {
                toolStripMain.Items.Remove(_tabbedButtonsControls.GetByValue(control));
                _tabbedButtonsControls.RemoveByValue(control);
            }

            if (dragContainerControl.GetDragControlFromContainedControl(control) != null)
            {
                dragContainerControl.RemoveDragControl(dragContainerControl.GetDragControlFromContainedControl(control));
            }

            if (_floatingFormsControls.ContainsValue(control))
            {
                _floatingFormsControls.GetByValue(control).Close();
                _floatingFormsControls.RemoveByValue(control);
            }

            UpdateUI();
        }
        /// <summary>
        /// Will create a control using reflection, corresponding to the object passed it. In order for the control to be recognized
        /// it must take as a constructor paramterer the type passed in.
        /// </summary>
        /// <param name="component"></param>
        /// <param name="allowComponentBaseTypes">This indicates whether the search for corresponding control should also cover parent types of the given type</param>
        /// <returns></returns>
        static public CommonBaseControl CreateCorrespondingControl(object component, bool allowComponentBaseTypes)
        {
            Type componentType           = component.GetType();
            ListEx <Assembly> assemblies = ReflectionHelper.GetReferencedAndInitialAssembly(Assembly.GetEntryAssembly());

            assemblies.Add(Assembly.GetAssembly(componentType));
            List <Type> types = ReflectionHelper.GatherTypeChildrenTypesFromAssemblies(typeof(CommonBaseControl), true, false, assemblies, new Type[] { componentType });

            if (types.Count == 0 && allowComponentBaseTypes)
            {
                while (componentType != typeof(object) && types.Count == 0)
                {
                    componentType = componentType.BaseType;
                    types         = ReflectionHelper.GatherTypeChildrenTypesFromAssemblies(typeof(CommonBaseControl), true, false, assemblies, new Type[] { componentType });
                }
            }

            if (types.Count == 0)
            {// Type not found.
                return(null);
            }

            string typesNames = string.Empty;

            if (types.Count > 1)
            {
                foreach (Type type in types)
                {
                    typesNames += type.Name + "();";
                }

                SystemMonitor.CheckWarning(types.Count == 1, "More than 1 control found for this type [" + component.GetType().Name + "][" + typesNames + "] of component, creating the first one.");
            }

            // Return the first proper object.
            CommonBaseControl control = (CommonBaseControl)types[0].GetConstructor(new Type[] { componentType }).Invoke(new object[] { component });

            control.Tag = component;
            return(control);
        }
        private void dragContainerControl_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ToolStripButton)))
            {
                ToolStripButton button = (ToolStripButton)e.Data.GetData(typeof(ToolStripButton));
                button.Enabled = false;

                CommonBaseControl control = _tabbedButtonsControls.GetByKey(button);

                toolStripMain.Items.Remove(_dragSeparator);

                DockStyle?dockStyle = dragContainerControl.DoEndDrag(control, new Point(e.X, e.Y));
                if (dockStyle == null)
                {
                    SetControlFloating(control);
                }
                else
                {
                    DoRemoveControl(control);
                    AddWorkspaceControl(control, dockStyle.Value, Point.Empty);
                }
            }
        }
        /// <summary>
        /// Resets the user interface placement of controls, by sending them all to the tab control.
        /// </summary>
        public void ResetControlPlacement()
        {
            foreach (CombinedHostingForm form in GeneralHelper.EnumerableToList <CombinedHostingForm>(_floatingFormsControls.Keys))
            {
                CommonBaseControl control = form.ContainedControl;
                control.Parent = null;

                // This makes sure no ask dialogs are shown or closing the forms.
                form.ContainedControl = null;

                DoRemoveControl(control);

                AddTabbedControl(control);
            }

            SystemMonitor.CheckError(_floatingFormsControls.Count == 0, "Not all floating forms were replaced with tabbed controls.");

            foreach (DragControl drag in GeneralHelper.EnumerableToList <DragControl>(dragContainerControl.DragControls))
            {
                if (drag == _checkedControlContainer)
                {
                    continue;
                }

                CommonBaseControl control = (CommonBaseControl)drag.ControlContained;
                control.Parent = null;

                // This makes sure no ask dialogs are shown or closing the forms.
                drag.ControlContained = null;

                dragContainerControl.RemoveDragControl(drag);

                AddTabbedControl(control);
            }

            //SystemMonitor.CheckError(dragContainerControl.DragControls.Count == 1, "Not all docked forms were replaced with tabbed controls.");
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="control"></param>
        /// <param name="location">Location to float to, in screen coordinates.</param>
        /// <param name="newWindow"></param>
        public CombinedHostingForm SetControlFloating(CommonBaseControl control)
        {
            DoRemoveControl(control);

            CombinedHostingForm form = new CombinedHostingForm(control);

            _floatingFormsControls.Add(form, control);

            form.Location         = Cursor.Position;
            form.ResizeBegin     += new EventHandler(combinedHostingForm_ResizeBegin);
            form.LocationChanged += new EventHandler(combinedHostingForm_LocationChanged);
            //form.MdiParent = this.ParentForm;
            form.ResizeEnd   += new EventHandler(combinedHostingForm_ResizeEnd);
            form.FormClosing += new FormClosingEventHandler(combinedHostingForm_FormClosing);
            form.Activated   += new EventHandler(combinedHostingForm_Activated);

            form.ShowInTaskbar = true;
            form.Show(this.ParentForm);
            form.BringToFront();

            UpdateUI();

            return(form);
        }
        /// <summary>
        /// Add a control to the tabbed controls list.
        /// </summary>
        public void AddTabbedControl(CommonBaseControl control)
        {
            if (_tabbedButtonsControls.ContainsValue(control))
            {
                SystemMonitor.Error("Control already added.");
                return;
            }

            #region Establish Index
            // Since the components enter one by one, it will happen that the index of the incoming control
            // is beyond the current maximum, so we look for the existing controls, one by one, and see where
            // the current one fits - this way they are restored places one by one and everyone is properly placed.

            int?index = null;
            if (control.PersistenceData.ContainsValue("combinedContainer.TabIndex"))
            {
                index = control.PersistenceData.GetValue <int>("combinedContainer.TabIndex");
            }

            int?actualIndex = null;
            if (index.HasValue)
            {
                foreach (ToolStripItem item in toolStripMain.Items)
                {
                    int itemIndex = 0;
                    if (item is ToolStripButton)
                    {
                        if (_tabbedButtonsControls.ContainsKey(item as ToolStripButton))
                        {
                            CommonBaseControl itemControl = _tabbedButtonsControls[item as ToolStripButton];
                            if (itemControl != null && itemControl.PersistenceData.ContainsValue("combinedContainer.TabIndex"))
                            {
                                itemIndex = itemControl.PersistenceData.GetValue <int>("combinedContainer.TabIndex");
                            }
                        }
                    }

                    if (index.Value <= itemIndex)
                    {
                        actualIndex = toolStripMain.Items.IndexOf(item);
                        break;
                    }
                }
            }
            #endregion

            string nameRepaired = control.Name.Replace("&", "&&");

            ToolStripButton controlButton = new ToolStripButton(nameRepaired);
            Image           image         = GetControlImage(control);
            if (image == null)
            {
                image = Resources.dot;
            }

            controlButton.ToolTipText  = control.Text;
            controlButton.Image        = image;
            controlButton.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            controlButton.CheckOnClick = false;
            controlButton.Checked      = false;
            controlButton.MouseDown   += new MouseEventHandler(controlButton_MouseDown);
            controlButton.MouseUp     += new MouseEventHandler(controlButton_MouseUp);
            controlButton.MouseMove   += new MouseEventHandler(controlButton_MouseMove);

            if (actualIndex.HasValue)
            {
                toolStripMain.Items.Insert(actualIndex.Value, controlButton);
            }
            else
            {
                toolStripMain.Items.Add(controlButton);
            }

            _tabbedButtonsControls.Add(controlButton, control);

            bool isChecked = false;
            if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Checked"))
            {
                isChecked = control.PersistenceData.GetBoolean("combinedContainer.Checked");
            }

            if (_checkedControl == null || isChecked)
            {
                ChangeCheckedControl(control);
            }

            UpdateUI();
        }
        /// <summary>
        /// Actual remove the control from managed controls.
        /// </summary>
        void DoRemoveControl(CommonBaseControl control)
        {
            if (control == _checkedControl)
            {
                // Try to change the current checked control.
                foreach (ToolStripButton button in _tabbedButtonsControls.Keys)
                {
                    if (_tabbedButtonsControls.GetByKey(button) != control)
                    {
                        ChangeCheckedControl(_tabbedButtonsControls.GetByKey(button));
                        break;
                    }
                }

                if (control == _checkedControl)
                {// If we failed to change it to another one, change to null.
                    ChangeCheckedControl(null);
                }
            }

            if (_tabbedButtonsControls.ContainsValue(control))
            {
                toolStripMain.Items.Remove(_tabbedButtonsControls.GetByValue(control));
                _tabbedButtonsControls.RemoveByValue(control);
            }

            if (dragContainerControl.GetDragControlFromContainedControl(control) != null)
            {
                dragContainerControl.RemoveDragControl(dragContainerControl.GetDragControlFromContainedControl(control));
            }

            if (_floatingFormsControls.ContainsValue(control))
            {
                _floatingFormsControls.GetByValue(control).Close();
                _floatingFormsControls.RemoveByValue(control);
            }

            UpdateUI();
        }
        /// <summary>
        ///
        /// </summary>
        public void AddControl(CommonBaseControl control)
        {
            Point?location = null;
            Size? size     = null;

            if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Size"))
            {
                size = control.PersistenceData.GetValue <Size>("combinedContainer.Size");
            }

            if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Location"))
            {
                location = control.PersistenceData.GetValue <Point>("combinedContainer.Location");
            }

            if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Floating"))
            {
                CombinedHostingForm form = SetControlFloating(control);
                if (location.HasValue)
                {
                    form.Location = location.Value;
                }

                // Sometimes persistence may be wrong, so make sure to bring control to view when added.
                form.Left = Math.Max(form.Left, 0);
                form.Top  = Math.Max(form.Top, 0);

                form.Left = Math.Min(Screen.PrimaryScreen.WorkingArea.Width, form.Left);
                form.Top  = Math.Min(Screen.PrimaryScreen.WorkingArea.Height, form.Top);

                form.Width  = Math.Max(form.Width, 200);
                form.Height = Math.Max(form.Height, 150);

                if (size.HasValue)
                {
                    form.Size = size.Value;
                }
            }
            else if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Tabbed"))
            {
                AddTabbedControl(control);
            }
            else if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Docked") &&
                     control.PersistenceData.ContainsValue("combinedContainer.Guid"))
            {
                Guid        guid        = control.PersistenceData.GetValue <Guid>("combinedContainer.Guid");
                DragControl dragControl = dragContainerControl.GetDragControlByGuid(guid);
                if (dragControl == null)
                {
                    SystemMonitor.OperationError("Guid drag control not found. Using a default new one.");
                    AddTabbedControl(control);
                }
                else
                {// Reuse the existing drag control and place the new control inside it.
                    SetControlToDragControl(control, dragControl, Point.Empty);
                    dragControl.Visible = true;
                }
            }
            else
            {// By default add tabbed.
                AddTabbedControl(control);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void ChangeCheckedControl(CommonBaseControl control)
        {
            if (_checkedControl == control)
            {
                return;
            }

            if (_checkedControlContainer == null)
            {// Creating a new checked container.
                _checkedControlContainer = new DragControl();
                _checkedControlContainer.ShowDragStrip = false;
                dragContainerControl.AddDragControl(_checkedControlContainer);
                dragContainerControl.DockControl(_checkedControlContainer, DockStyle.Fill);
            }

            if (control == null)
            {
                _checkedControlContainer.ControlContained = null;
            }
            else
            {
                _checkedControlContainer.ControlContained = control;
                _checkedControlContainer.Name = control.Name;
            }

            if (_checkedControl != null && _checkedControl.PersistenceData != null && control != null)
            {
                _checkedControl.PersistenceData.AddValue("combinedContainer.Checked", false);
            }

            _checkedControl = control;

            if (_checkedControl != null && _checkedControl.PersistenceData != null)
            {
                _checkedControl.PersistenceData.AddValue("combinedContainer.Checked", true);
            }

            if (SelectedTabbedControlChangedEvent != null)
            {
                SelectedTabbedControlChangedEvent(this, _checkedControl);
            }

            UpdateUI();
        }
        /// <summary>
        /// Helper function to create a new properly assigned tab page for the given component control.
        /// </summary>
        void AddComponentControl(CommonBaseControl componentControl, bool focus)
        {
            combinedContainerControl.AddControl(componentControl);

            if (componentControl is PlatformComponentControl)
            {
                ((PlatformComponentControl)componentControl).SetApplicationStatusStrip(this.statusStripMain);
            }

            if (focus)
            {
                combinedContainerControl.ChangeCheckedControl(componentControl);
            }
        }
 /// <summary>
 /// 
 /// </summary>
 Image GetControlImage(CommonBaseControl control)
 {
     if (_imageList == null || _imageList.Images.ContainsKey(control.ImageName) == false)
     {
         return null;
     }
     else
     {
         return _imageList.Images[control.ImageName];
     }
 }
        void PersistControlData(CommonBaseControl control, Component containingComponent)
        {
            control.SaveState();

            Point? location = null;
            Size? size = null;

            if (containingComponent == null)
            {
                return;
            }

            // Since this info will be cleared on next line, store here for reference.
            bool isChecked = control.PersistenceData.ContainsValue("combinedContainer.Checked") &&
                control.PersistenceData.GetBoolean("combinedContainer.Checked");

            // Clear all previous persistence info related to "combinedContainer".
            control.PersistenceData.ClearByNamePart("combinedContainer.");

            if (containingComponent is ToolStripButton ||
                control == _checkedControl)
            {
                control.PersistenceData.AddValue("combinedContainer.Tabbed", true);
                control.PersistenceData.AddValue("combinedContainer.Checked", isChecked);
                control.PersistenceData.AddValue("combinedContainer.TabIndex", toolStripMain.Items.IndexOf((ToolStripButton)containingComponent));
            }
            else if (containingComponent is CombinedHostingForm)
            {
                location = ((CombinedHostingForm)containingComponent).Location;
                size = ((CombinedHostingForm)containingComponent).Size;
                control.PersistenceData.AddValue("combinedContainer.Floating", true);
            }
            else if (containingComponent is DragControl)
            {
                control.PersistenceData.AddValue("combinedContainer.Docked", true);
                control.PersistenceData.AddValue("combinedContainer.Guid", ((DragControl)containingComponent).Guid);
            }
            else
            {
                SystemMonitor.Warning("Unrecognized case.");
                return;
            }

            if (location.HasValue)
            {
                control.PersistenceData.AddValue("combinedContainer.Location", location.Value);
            }

            if (size.HasValue)
            {
                control.PersistenceData.AddValue("combinedContainer.Size", size.Value);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="control"></param>
        /// <param name="location">Location to float to, in screen coordinates.</param>
        /// <param name="newWindow"></param>
        public CombinedHostingForm SetControlFloating(CommonBaseControl control)
        {
            DoRemoveControl(control);

            CombinedHostingForm form = new CombinedHostingForm(control);
            _floatingFormsControls.Add(form, control);

            form.Location = Cursor.Position;
            form.ResizeBegin += new EventHandler(combinedHostingForm_ResizeBegin);
            form.LocationChanged += new EventHandler(combinedHostingForm_LocationChanged);
            //form.MdiParent = this.ParentForm;
            form.ResizeEnd += new EventHandler(combinedHostingForm_ResizeEnd);
            form.FormClosing += new FormClosingEventHandler(combinedHostingForm_FormClosing);
            form.Activated += new EventHandler(combinedHostingForm_Activated);

            form.ShowInTaskbar = true;
            form.Show(this.ParentForm);
            form.BringToFront();

            UpdateUI();

            return form;
        }
        /// <summary>
        /// 
        /// </summary>
        public void AddControl(CommonBaseControl control)
        {
            Point? location = null;
            Size? size = null;

            if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Size"))
            {
                size = control.PersistenceData.GetValue<Size>("combinedContainer.Size");
            }

            if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Location"))
            {
                location = control.PersistenceData.GetValue<Point>("combinedContainer.Location");
            }

            if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Floating"))
            {
                CombinedHostingForm form = SetControlFloating(control);
                if (location.HasValue)
                {
                    form.Location = location.Value;
                }

                // Sometimes persistence may be wrong, so make sure to bring control to view when added.
                form.Left = Math.Max(form.Left, 0);
                form.Top = Math.Max(form.Top, 0);

                form.Left = Math.Min(Screen.PrimaryScreen.WorkingArea.Width, form.Left);
                form.Top = Math.Min(Screen.PrimaryScreen.WorkingArea.Height, form.Top);

                form.Width = Math.Max(form.Width, 200);
                form.Height = Math.Max(form.Height, 150);

                if (size.HasValue)
                {
                    form.Size = size.Value;
                }

            }
            else if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Tabbed"))
            {
                AddTabbedControl(control);
            }
            else if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Docked")
                && control.PersistenceData.ContainsValue("combinedContainer.Guid"))
            {
                Guid guid = control.PersistenceData.GetValue<Guid>("combinedContainer.Guid");
                DragControl dragControl = dragContainerControl.GetDragControlByGuid(guid);
                if (dragControl == null)
                {
                    SystemMonitor.OperationError("Guid drag control not found. Using a default new one.");
                    AddTabbedControl(control);
                }
                else
                {// Reuse the existing drag control and place the new control inside it.
                    SetControlToDragControl(control, dragControl, Point.Empty);
                    dragControl.Visible = true;
                }
            }
            else
            {// By default add tabbed.
                AddTabbedControl(control);
            }
        }
        /// <summary>
        /// Helper.
        /// </summary>
        void SetControlToDragControl(CommonBaseControl control, DragControl dragControl, Point location)
        {
            SystemMonitor.CheckError(dragControl.ControlContained == null, "Drag control already has a contained control.");

            dragControl.ControlContained = control;
            dragControl.Name = control.Name;
            dragControl.Location = location;

            dragControl.Image = GetControlImage(control);
            dragControl.dragStrip.DoubleClick += new EventHandler(dragStrip_DoubleClick);

            UpdateUI();
        }
        /// <summary>
        /// Add a floating control to the workspace area.
        /// location considered only for floating (dockStyle = none)
        /// </summary>
        public DragControl AddWorkspaceControl(CommonBaseControl control, DockStyle dockPane, Point location)
        {
            if (dragContainerControl.GetDragControlFromContainedControl(control) != null)
            {
                SystemMonitor.Warning("Misuse.");
                return null;
            }

            DragControl dragControl = new DragControl();
            SetControlToDragControl(control, dragControl, location);

            dragContainerControl.AddDragControl(dragControl);
            dragContainerControl.DockControl(dragControl, dockPane);

            UpdateUI();

            return dragControl;
        }
 void combinedContainerControl_FocusedControlChangedEvent(CombinedContainerControl containerControl, CommonBaseControl control)
 {
     WinFormsHelper.BeginFilteredManagedInvoke(this, TimeSpan.FromMilliseconds(250), new GeneralHelper.GenericDelegate<PlatformComponent>(UpdateFormText), control != null ? control.Tag as PlatformComponent : null);
 }
        /// <summary>
        /// 
        /// </summary>
        public void ClearControls()
        {
            _checkedControl = null;
            _floatingFormsControls.Clear();
            _tabbedButtonsControls.Clear();

            dragContainerControl.ClearControls();
        }
        /// <summary>
        /// 
        /// </summary>
        public bool RemoveControl(CommonBaseControl control)
        {
            // We shall not invoke control.UnInitializeControl(); here, since user may ultimately wish to use the control
            // further in another context.

            DoRemoveControl(control);

            if (ContainedControlRemovedEvent != null)
            {
                ContainedControlRemovedEvent(this, control);
            }

            // Raise control removed event.
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="expertHost"></param>
        void Initialize(ExpertHost expertHost)
        {
            this.Name = expertHost.Name;

            _expertHost = expertHost;
            _expertHost.OperationalStateChangedEvent += new OperationalStateChangedDelegate(expertHost_OperationalStatusChangedEvent);

            // Create early here, to be able to use the image name.
            _expertControl = CommonBaseControl.CreateCorrespondingControl(_expertHost.Expert, true);
            this.ImageName = _expertControl.ImageName;

            //_host.SessionsUpdateEvent += new GeneralHelper.GenericDelegate<ISourceManager>(_expertHost_SessionsUpdateEvent);
            //_host.SourcesUpdateEvent += new GeneralHelper.GenericDelegate<ISourceManager>(_expertHost_SourcesUpdateEvent);
            //DoUpdateUI();
        }
        /// <summary>
        /// Add a control to the tabbed controls list.
        /// </summary>
        public void AddTabbedControl(CommonBaseControl control)
        {
            if (_tabbedButtonsControls.ContainsValue(control))
            {
                SystemMonitor.Error("Control already added.");
                return;
            }

            #region Establish Index
            // Since the components enter one by one, it will happen that the index of the incoming control
            // is beyond the current maximum, so we look for the existing controls, one by one, and see where
            // the current one fits - this way they are restored places one by one and everyone is properly placed.

            int? index = null;
            if (control.PersistenceData.ContainsValue("combinedContainer.TabIndex"))
            {
                index = control.PersistenceData.GetValue<int>("combinedContainer.TabIndex");
            }

            int? actualIndex = null;
            if (index.HasValue)
            {
                foreach (ToolStripItem item in toolStripMain.Items)
                {
                    int itemIndex = 0;
                    if (item is ToolStripButton)
                    {
                        if (_tabbedButtonsControls.ContainsKey(item as ToolStripButton))
                        {
                            CommonBaseControl itemControl = _tabbedButtonsControls[item as ToolStripButton];
                            if (itemControl != null && itemControl.PersistenceData.ContainsValue("combinedContainer.TabIndex"))
                            {
                                itemIndex = itemControl.PersistenceData.GetValue<int>("combinedContainer.TabIndex");
                            }
                        }
                    }

                    if (index.Value <= itemIndex)
                    {
                        actualIndex = toolStripMain.Items.IndexOf(item);
                        break;
                    }
                }
            }
            #endregion

            string nameRepaired = control.Name.Replace("&", "&&");

            ToolStripButton controlButton = new ToolStripButton(nameRepaired);
            Image image = GetControlImage(control);
            if (image == null)
            {
                image = Resources.dot;
            }

            controlButton.ToolTipText = control.Text;
            controlButton.Image = image;
            controlButton.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            controlButton.CheckOnClick = false;
            controlButton.Checked = false;
            controlButton.MouseDown += new MouseEventHandler(controlButton_MouseDown);
            controlButton.MouseUp += new MouseEventHandler(controlButton_MouseUp);
            controlButton.MouseMove += new MouseEventHandler(controlButton_MouseMove);

            if (actualIndex.HasValue)
            {
                toolStripMain.Items.Insert(actualIndex.Value, controlButton);
            }
            else
            {
                toolStripMain.Items.Add(controlButton);
            }

            _tabbedButtonsControls.Add(controlButton, control);

            bool isChecked = false;
            if (control.PersistenceData != null && control.PersistenceData.ContainsValue("combinedContainer.Checked"))
            {
                isChecked = control.PersistenceData.GetBoolean("combinedContainer.Checked");
            }

            if (_checkedControl == null || isChecked)
            {
                ChangeCheckedControl(control);
            }

            UpdateUI();
        }