context: // *****************************************************************************
  // 
  //  (c) Crownwood Consulting Limited 2002-2003
  //  All rights reserved. The software and associated documentation 
  //  supplied hereunder are the proprietary information of Crownwood Consulting 
  //	Limited, Crownwood, Bracknell, Berkshire, England and are supplied subject 
  //  to licence terms.
  // 
  //  Magic Version 1.7.4.0 	www.dotnetmagic.com
  // *****************************************************************************
  
  using System;
  using System.IO;
  using System.Xml;
  using System.Text;
  using System.Data;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using Crownwood.Magic.Win32;
  using Crownwood.Magic.Common;
  using Crownwood.Magic.Controls;
  
  namespace Crownwood.Magic.Controls
  {
      [ToolboxBitmap(typeof(TabbedGroups))]
      public class TabbedGroups : UserControl, ISupportInitialize, IMessageFilter
  	{
  	    public class DragProvider
  	    {
              protected object _tag;
              
              public DragProvider()
              {
                  _tag = null;
              }
              
              public DragProvider(object tag)
              {
                  _tag = tag;
              }
              
              public object Tag
              {
                  get { return _tag; }
                  set { _tag = value; }
              }
  	    }
  	
  	    public enum DisplayTabModes
  	    {
              HideAll,
              ShowAll,
              ShowActiveLeaf,
              ShowMouseOver,
              ShowActiveAndMouseOver
  	    }
  	
  	    public enum CompactFlags
  	    {
  	        RemoveEmptyTabLeaf = 1,
  	        RemoveEmptyTabSequence = 2,
  	        ReduceSingleEntries = 4,
  	        ReduceSameDirection = 8,
  	        All = 15
  	    }
  	
  	    // Instance fields
  	    protected int _numLeafs;
          protected int _defMinWidth;
          protected int _defMinHeight;
  		protected int _resizeBarVector;
  		protected int _suspendLeafCount;
  		protected string _closeMenuText;
          protected string _prominentMenuText;
          protected string _rebalanceMenuText;
          protected string _movePreviousMenuText;
          protected string _moveNextMenuText;
          protected string _newVerticalMenuText;
          protected string _newHorizontalMenuText;
          protected ImageList _imageList;
          protected bool _dirty;
          protected bool _autoCalculateDirty;
          protected bool _saveControls;
          protected bool _initializing;
          protected bool _atLeastOneLeaf;
          protected bool _autoCompact;
          protected bool _compacting;
          protected bool _resizeBarLock;
  		protected bool _layoutLock;
  		protected bool _pageCloseWhenEmpty;
  		protected Color _resizeBarColor;
          protected Shortcut _closeShortcut;
          protected Shortcut _prominentShortcut;
          protected Shortcut _rebalanceShortcut;
          protected Shortcut _movePreviousShortcut;
          protected Shortcut _moveNextShortcut;
          protected Shortcut _splitVerticalShortcut;
          protected Shortcut _splitHorizontalShortcut;
          protected Shortcut _nextTabShortcut;
          protected CompactFlags _compactOptions;
          protected DisplayTabModes _displayTabMode;
          protected TabGroupLeaf _prominentLeaf;
          protected TabGroupLeaf _activeLeaf;
          protected TabGroupSequence _root;
          protected VisualStyle _style;
  	
  	    // Delegates for events
  	    public delegate void TabControlCreatedHandler(TabbedGroups tg, Controls.TabControl tc);
  	    public delegate void PageCloseRequestHandler(TabbedGroups tg, TGCloseRequestEventArgs e);
          public delegate void PageContextMenuHandler(TabbedGroups tg, TGContextMenuEventArgs e);
          public delegate void GlobalSavingHandler(TabbedGroups tg, XmlTextWriter xmlOut);
          public delegate void GlobalLoadingHandler(TabbedGroups tg, XmlTextReader xmlIn);
          public delegate void PageSavingHandler(TabbedGroups tg, TGPageSavingEventArgs e);
          public delegate void PageLoadingHandler(TabbedGroups tg, TGPageLoadingEventArgs e);
          public delegate void ExternalDropHandler(TabbedGroups tg, TabGroupLeaf tgl, Controls.TabControl tc, DragProvider dp);
  	
  	    // Instance events
  	    public event TabControlCreatedHandler TabControlCreated;
  	    public event PageCloseRequestHandler PageCloseRequest;
          public event PageContextMenuHandler PageContextMenu;
          public event GlobalSavingHandler GlobalSaving;
          public event GlobalLoadingHandler GlobalLoading;
          public event PageSavingHandler PageSaving;
          public event PageLoadingHandler PageLoading;
          public event EventHandler ProminentLeafChanged;
          public event EventHandler ActiveLeafChanged;
          public event EventHandler DirtyChanged;
          public event ExternalDropHandler ExternalDrop;
  	
          public TabbedGroups()
          {
              InternalConstruct(VisualStyle.IDE);
          }
              
          public TabbedGroups(VisualStyle style)
  		{
  		    InternalConstruct(style);
          }
          
          protected void InternalConstruct(VisualStyle style)
  		{
              // Prevent flicker with double buffering and all painting inside WM_PAINT
  			SetStyle(ControlStyles.DoubleBuffer | 
  					 ControlStyles.AllPaintingInWmPaint |
  					 ControlStyles.UserPaint, true);
  		
  		    // We want to act as a drop target
  		    this.AllowDrop = true;
  		
  		    // Remember parameters
  		    _style = style;
  		    
  		    // Define initial state
  		    _numLeafs = 0;
  		    _compacting = false;
  		    _initializing = false;
  			_suspendLeafCount = 0;
  		    
              // Create the root sequence that always exists
              _root = new TabGroupSequence(this);
  		    
              // Define default settings
  		    ResetProminentLeaf();
  		    ResetResizeBarVector();
  		    ResetResizeBarColor();
  		    ResetResizeBarLock();
  			ResetLayoutLock();
  			ResetCompactOptions();
  		    ResetDefaultGroupMinimumWidth();
              ResetDefaultGroupMinimumHeight();
              ResetActiveLeaf();
              ResetAutoCompact();
              ResetAtLeastOneLeaf();
              ResetCloseMenuText();
              ResetProminentMenuText();
              ResetRebalanceMenuText();
              ResetMovePreviousMenuText();
              ResetMoveNextMenuText();
              ResetNewVerticalMenuText();
              ResetNewHorizontalMenuText();
              ResetCloseShortcut();
              ResetProminentShortcut();
              ResetRebalanceShortcut();
              ResetMovePreviousShortcut();
              ResetMoveNextShortcut();
              ResetSplitVerticalShortcut();
              ResetSplitHorizontalShortcut();
              ResetImageList();
              ResetDisplayTabMode();
              ResetSaveControls();
              ResetAutoCalculateDirty();
              ResetDirty();
  			ResetPageCloseWhenEmpty();
              
              // Add ourself to the application filtering list 
              // (to snoop for shortcut combinations)
              Application.AddMessageFilter(this);
              
          }
          
  		protected override void Dispose( bool disposing )
  		{
  			if (disposing)
  			{
  				// Remove message filter to remove circular reference
  				Application.RemoveMessageFilter(this);
  			}
  
  			base.Dispose(disposing);
  		}
  		
  		[Category("TabbedGroups")]
          [DefaultValue(typeof(VisualStyle), "IDE")]
          public VisualStyle Style
          {
              get { return _style; }
              
              set
              {
                  if (_style != value)
                  {   
                      _style = value;
                      
                      // Propogate to all children
                      Notify(TabGroupBase.NotifyCode.StyleChanged);
                  }
              }
          }
  
          public void ResetStyle()
          {
              Style = VisualStyle.IDE;
          }
          
          [Browsable(false)]
          public TabGroupSequence RootSequence
          {
              get { return _root; }
          }
  
          [Category("TabbedGroups")]
          [DefaultValue(-1)]
          public int ResizeBarVector
          {
              get { return _resizeBarVector; }
              
              set
              {
                  if (_resizeBarVector != value)
                  {
                      _resizeBarVector = value;
                      
                      // Propogate to all children
                      Notify(TabGroupBase.NotifyCode.ResizeBarVectorChanged);
                  }
              }
          }
          
          public void ResetResizeBarVector()
          {
              ResizeBarVector = -1;
          }
          
          [Category("TabbedGroups")]
          public Color ResizeBarColor
          {
              get { return _resizeBarColor; }
              
              set
              {
                  if (!_resizeBarColor.Equals(value))
                  {
                      _resizeBarColor = value;
                      
                      // Propogate to all children
                      Notify(TabGroupBase.NotifyCode.ResizeBarColorChanged);
                  }
              }
          }
  
          protected bool ShouldSerializeResizeBackColor()
          {
              return _resizeBarColor != base.BackColor;
          }
          
          public void ResetResizeBarColor()
          {
              ResizeBarColor = base.BackColor;
          }
          
          [Category("TabbedGroups")]
          [DefaultValue(false)]
          public bool ResizeBarLock
          {
              get { return _resizeBarLock; }
              set { _resizeBarLock = value; }
          }
          
          public void ResetResizeBarLock()
          {
              ResizeBarLock = false;
          }
          
  		[Category("TabbedGroups")]
  		[DefaultValue(false)]
  		public bool LayoutLock
  		{
  			get { return _layoutLock; }
  			set { _layoutLock = value; }
  		}
          
  		public void ResetLayoutLock()
  		{
  			LayoutLock = false;
  		}
  		
  		[Category("TabbedGroups")]
          public TabGroupLeaf ProminentLeaf
          {
              get { return _prominentLeaf; }
              
              set
              {
                  if (_prominentLeaf != value)
                  {
                      _prominentLeaf = value;
  
                      // Mark layout as dirty
                      if (_autoCalculateDirty)
                          _dirty = true;
  
                      // Propogate to all children
                      Notify(TabGroupBase.NotifyCode.ProminentChanged);
                      
                      OnProminentLeafChanged(EventArgs.Empty);
                  }
              }
          }
          
          public void ResetProminentLeaf()
          {
              ProminentLeaf = null;
          }
          
          [Category("TabbedGroups")]
          [DefaultValue(typeof(CompactFlags), "All")]
          public CompactFlags CompactOptions
          {
              get { return _compactOptions; }
              set { _compactOptions = value; }
          }
  
          public void ResetCompactOptions()
          {
              CompactOptions = CompactFlags.All;
          }
          
          [Category("TabbedGroups")]
          [DefaultValue(4)]
          public int DefaultGroupMinimumWidth 
          {
              get { return _defMinWidth; }
              
              set
              {
                  if (_defMinWidth != value)
                  {
                      _defMinWidth = value;
                      
                      // Propogate to all children
                      Notify(TabGroupBase.NotifyCode.MinimumSizeChanged);
                  }
              }
          }        
          
          public void ResetDefaultGroupMinimumWidth()
          {
              DefaultGroupMinimumWidth = 4;
          }
          
          [Category("TabbedGroups")]
          [DefaultValue(4)]
          public int DefaultGroupMinimumHeight
          {
              get { return _defMinHeight; }
              
              set
              {
                  if (_defMinHeight != value)
                  {
                      _defMinHeight = value;
                      
                      // Propogate to all children
                      Notify(TabGroupBase.NotifyCode.MinimumSizeChanged);
                  }
              }
          }        
          
          public void ResetDefaultGroupMinimumHeight()
          {
              DefaultGroupMinimumHeight = 4;
          }
  
          [Localizable(true)]
          [Category("Text String")]
          [DefaultValue("&Close")]
          public string CloseMenuText
          {
              get { return _closeMenuText; }
              set { _closeMenuText = value; }
          }
          
          public void ResetCloseMenuText()
          {
              CloseMenuText = "&Close";
          }
  
          [Localizable(true)]
          [Category("Text String")]
          [DefaultValue("Pro&minent")]
          public string ProminentMenuText
          {
              get { return _prominentMenuText; }
              set { _prominentMenuText = value; }
          }
  
          public void ResetProminentMenuText()
          {
              ProminentMenuText = "Pro&minent";
          }
  
          [Localizable(true)]
          [Category("Text String")]
          [DefaultValue("&Rebalance")]
          public string RebalanceMenuText
          {
              get { return _rebalanceMenuText; }
              set { _rebalanceMenuText = value; }
          }
  
          public void ResetRebalanceMenuText()
          {
              RebalanceMenuText = "&Rebalance";
          }
  
          [Localizable(true)]
          [Category("Text String")]
          [DefaultValue("Move to &Previous Tab Group")]
          public string MovePreviousMenuText
          {
              get { return _movePreviousMenuText; }
              set { _movePreviousMenuText = value; }
          }
  
          public void ResetMovePreviousMenuText()
          {
              MovePreviousMenuText = "Move to &Previous Tab Group";
          }
  
          [Localizable(true)]
          [Category("Text String")]
          [DefaultValue("Move to &Next Tab Group")]
          public string MoveNextMenuText
          {
              get { return _moveNextMenuText; }
              set { _moveNextMenuText = value; }
          }
          
          public void ResetMoveNextMenuText()
          {
              MoveNextMenuText = "Move to &Next Tab Group";
          }
  
          [Localizable(true)]
          [Category("Text String")]
          [DefaultValue("New &Vertical Tab Group")]
          public string NewVerticalMenuText
          {
              get { return _newVerticalMenuText; }
              set { _newVerticalMenuText = value; }
          }
  
          public void ResetNewVerticalMenuText()
          {
              NewVerticalMenuText = "New &Vertical Tab Group";
          }
  
          [Localizable(true)]
          [Category("Text String")]
          [DefaultValue("New &Horizontal Tab Group")]
          public string NewHorizontalMenuText
          {
              get { return _newHorizontalMenuText; }
              set { _newHorizontalMenuText = value; }
          }
          
          public void ResetNewHorizontalMenuText()
          {
              NewHorizontalMenuText = "New &Horizontal Tab Group";
          }
  
          [Category("Shortcuts")]
          public Shortcut CloseShortcut
          {
              get { return _closeShortcut; }
              set { _closeShortcut = value; }
          }
  
          protected bool ShouldSerializeCloseShortcut()
          {
              return !_closeShortcut.Equals(Shortcut.CtrlShiftC);
          }
  
          public void ResetCloseShortcut()
          {
              CloseShortcut = Shortcut.CtrlShiftC;
          }
          
          [Category("Shortcuts")]
          public Shortcut ProminentShortcut
          {
              get { return _prominentShortcut; }
              set { _prominentShortcut = value; }
          }
  
          protected bool ShouldSerializeProminentShortcut()
          {
              return !_prominentShortcut.Equals(Shortcut.CtrlShiftT);
          }
  
          public void ResetProminentShortcut()
          {
              ProminentShortcut = Shortcut.CtrlShiftT;  
          }
          
          [Category("Shortcuts")]
          public Shortcut RebalanceShortcut
          {
              get { return _rebalanceShortcut; }
              set { _rebalanceShortcut = value; }
          }
  
          protected bool ShouldSerializeRebalanceShortcut()
          {
              return !_rebalanceShortcut.Equals(Shortcut.CtrlShiftR);
          }
  
          public void ResetRebalanceShortcut()
          {
              RebalanceShortcut = Shortcut.CtrlShiftR;
          }
  
          [Category("Shortcuts")]
          public Shortcut MovePreviousShortcut
          {
              get { return _movePreviousShortcut; }
              set { _movePreviousShortcut = value; }
          }
  
          protected bool ShouldSerializeMovePreviousShortcut()
          {
              return !_movePreviousShortcut.Equals(Shortcut.CtrlShiftP);
          }
  
          public void ResetMovePreviousShortcut()
          {
              MovePreviousShortcut = Shortcut.CtrlShiftP;
          }
          
          [Category("Shortcuts")]
          public Shortcut MoveNextShortcut
          {
              get { return _moveNextShortcut; }
              set { _moveNextShortcut = value; }
          }
  
          protected bool ShouldSerializeMoveNextShortcut()
          {
              return !_moveNextShortcut.Equals(Shortcut.CtrlShiftN);
          }
  
          public void ResetMoveNextShortcut()
          {
              MoveNextShortcut = Shortcut.CtrlShiftN;
          }
          
          [Category("Shortcuts")]
          public Shortcut SplitVerticalShortcut
          {
              get { return _splitVerticalShortcut; }
              set { _splitVerticalShortcut = value; }
          }
  
          protected bool ShouldSerializeSplitVerticalShortcut()
          {
              return !_splitVerticalShortcut.Equals(Shortcut.CtrlShiftV);
          }
  
          public void ResetSplitVerticalShortcut()
          {
              SplitVerticalShortcut = Shortcut.CtrlShiftV;
          }
          
          [Category("Shortcuts")]
          public Shortcut SplitHorizontalShortcut
          {
              get { return _splitHorizontalShortcut; }
              set { _splitHorizontalShortcut = value; }
          }
  
          protected bool ShouldSerializeSplitHorizontalShortcut()
          {
              return !_splitHorizontalShortcut.Equals(Shortcut.CtrlShiftH);
          }
  
          public void ResetSplitHorizontalShortcut()
          {
              SplitHorizontalShortcut = Shortcut.CtrlShiftH;
          }
          
          [Category("TabbedGroups")]
          public ImageList ImageList
          {
              get { return _imageList; }
              
              set 
              { 
                  if (_imageList != value)
                  {
                      // Propogate to all children
                      Notify(TabGroupBase.NotifyCode.ImageListChanging);
  
                      _imageList = value;
                      
                      // Propogate to all children
                      Notify(TabGroupBase.NotifyCode.ImageListChanged);
                  }
              }
          }
          
          protected bool ShouldSerializeImageList()
          {
              return _imageList != null;
          }
          
          public void ResetImageList()
          {
              ImageList = null;
          }
          
          [Category("TabbedGroups")]
          [DefaultValue(typeof(DisplayTabModes), "ShowAll")]
          public DisplayTabModes DisplayTabMode
          {
              get { return _displayTabMode; }
              
              set
              {
                  if (_displayTabMode != value)
                  {
                      _displayTabMode = value;
                      
                      // Propogate to all children
                      Notify(TabGroupBase.NotifyCode.DisplayTabMode);
                  }
              }
          }
          
          public void ResetDisplayTabMode()
          {
              DisplayTabMode = DisplayTabModes.ShowAll;
          }
  
          [Category("TabbedGroups")]
          [DefaultValue(true)]
          public bool SaveControls
          {
              get { return _saveControls; }
              set { _saveControls = value; }
          }
          
          public void ResetSaveControls()
          {
              SaveControls = true;
          }
  
          [Category("TabbedGroups")]
          public bool Dirty
          {
              get { return _dirty; }
              
              set 
              {
                  if (_dirty != value)
                  {
                      _dirty = value;
                      
                      OnDirtyChanged(EventArgs.Empty);
                  }
              }
          }
          
          protected bool ShouldSerializeDirty()
          {
              return false;
          }
          
          public void ResetDirty()
          {
              Dirty = false;
          }
  
  		[Category("TabbedGroups")]
  		[DefaultValue(false)]
  		public bool PageCloseWhenEmpty
  		{
  			get { return _pageCloseWhenEmpty; }
  			set { _pageCloseWhenEmpty = value; }
  		}
          
  		public void ResetPageCloseWhenEmpty()
  		{
  			PageCloseWhenEmpty = false;
  		}
  		
  		[Category("TabbedGroups")]
          [DefaultValue(true)]
          public bool AutoCalculateDirty
          {
              get { return _autoCalculateDirty; }
              set { _autoCalculateDirty = value; }
          }
          
          public void ResetAutoCalculateDirty()
          {
              AutoCalculateDirty = true;
          }
  
          [Category("TabbedGroups")]
          public TabGroupLeaf ActiveLeaf
          {
              get { return _activeLeaf; }
              
              set
              {
                  if (_activeLeaf != value)
                  {
  					// Cannot get rid of the active leaf when there must be one
  					if (_atLeastOneLeaf && (value == null))
  						return;
  					
  					// Mark layout as dirty
                      if (_autoCalculateDirty)
                          _dirty = true;
  
                      // Remove selection highlight from old leaf
                      if (_activeLeaf != null)
                      {
                          // Get access to the contained tab control
                          TabControl tc = _activeLeaf.GroupControl as Controls.TabControl;
                          
                          // Remove bold text for the selected page
                          tc.BoldSelectedPage = false;
                          
                          _activeLeaf = null;
                      }
  
                      // Set selection highlight on new active leaf            
                      if (value != null)
                      {
                          // Get access to the contained tab control
                          TabControl tc = value.GroupControl as Controls.TabControl;
                          
                          // Remove bold text for the selected page
                          tc.BoldSelectedPage = true;
                          
                          _activeLeaf = value;
                      }
  
                      // Is the tab mode dependant on the active leaf value
                      if ((_displayTabMode == DisplayTabModes.ShowActiveLeaf) ||
                          (_displayTabMode == DisplayTabModes.ShowActiveAndMouseOver))
                      {
                          // Yes, better notify a change in value so it can be applied
                          Notify(TabGroupBase.NotifyCode.DisplayTabMode);
                      }
                          
                      OnActiveLeafChanged(EventArgs.Empty);
                  }
              }
          }
          
          public void ResetActiveLeaf()
          {
              ActiveLeaf = null;
          }
  
  		private bool ShouldSerializeActiveLeaf()
  		{
  			// Never persist this information
  			return false;
  		}
  
          [Category("TabbedGroups")]
          public bool AtLeastOneLeaf
          {
              get { return _atLeastOneLeaf; }
              
              set
              {
                  if (_atLeastOneLeaf != value)
                  {
                      _atLeastOneLeaf = value;
                      
                      // Do always need at least one leaf?
                      if (_atLeastOneLeaf)
                      {
                          // Is there at least one?
                          if (_numLeafs == 0)
                          {
                              // No, create a default entry for the root sequence
                              _root.AddNewLeaf();
  
  							// Update the active leaf
  							ActiveLeaf = FirstLeaf();
  							
  							// Mark layout as dirty
                              if (_autoCalculateDirty)
                                  _dirty = true;
                          }
                      }
                      else
                      {
                          // Are there some potential leaves not needed
                          if (_numLeafs > 0)
                          {
                              // Use compaction so only needed ones are retained
                              if (_autoCompact)
                                  Compact();
                          }
                      }
                  }
              }
          }
          
          public void ResetAtLeastOneLeaf()
          {
              AtLeastOneLeaf = true;
          }
  
          [Category("TabbedGroups")]
          [DefaultValue(true)]
          public bool AutoCompact
          {
              get { return _autoCompact; }
              set { _autoCompact = value; }
          }
  
          public void ResetAutoCompact()
          {
              _autoCompact = true;
          }
  
          public void Rebalance()
          {
              _root.Rebalance(true);
          }
  
          public void Rebalance(bool recurse)
          {
              _root.Rebalance(recurse);
          }
          
          public void Compact()
          {
              Compact(_compactOptions);
          }
          
          public void Compact(CompactFlags flags)
          {
              // When  entries are removed because of compacting this may cause the container object 
              // to start a compacting request. Prevent this recursion by using a simple varible.
              if (!_compacting)
              {
                  // We never compact when loading/initializing the contents
                  if (!_initializing)
                  {
                      _compacting = true;
                      _root.Compact(flags);
                      _compacting = false;
                      
                      EnforceAtLeastOneLeaf();
                  }
              }
          }
          
          public TabGroupLeaf FirstLeaf()
          {
              return RecursiveFindLeafInSequence(_root, true);
          }
  
          public TabGroupLeaf LastLeaf()
          {
              return RecursiveFindLeafInSequence(_root, false);
          }
  
          public TabGroupLeaf NextLeaf(TabGroupLeaf current)
          {
              // Get parent of the provided leaf
              TabGroupSequence tgs = current.Parent as TabGroupSequence;
              
              // Must have a valid parent sequence
              if (tgs != null)
                  return RecursiveFindLeafInSequence(tgs, current, true);
              else
                  return null;
          }
          
          public TabGroupLeaf PreviousLeaf(TabGroupLeaf current)
          {
              // Get parent of the provided leaf
              TabGroupSequence tgs = current.Parent as TabGroupSequence;
              
              // Must have a valid parent sequence
              if (tgs != null)
                  return RecursiveFindLeafInSequence(tgs, current, false);
              else
                  return null;
          }
  
          internal void MoveActiveToNearestFromLeaf(TabGroupBase oldLeaf)
          {
              // Must have a reference to begin movement
              if (oldLeaf != null)
              {
                  // Find the parent sequence of leaf, remember that a 
                  // leaf must be contained within a sequence instance
                  TabGroupSequence tgs = oldLeaf.Parent as TabGroupSequence;
                  
                  // Must be valid, but had better check anyway
                  if (tgs != null)
                  {
                      // Move relative to given base in the sequence
                      MoveActiveInSequence(tgs, oldLeaf);
                  }
              }
          }
          
          internal void MoveActiveToNearestFromSequence(TabGroupSequence tgs)
          {
              // Is active leaf being moved from root sequence
              if (_root == tgs)
              {
                  // Then make nothing active
                  ActiveLeaf = null;
              }
              else
              {
                  // Find the parent sequence of given sequence
                  TabGroupSequence tgsParent = tgs.Parent as TabGroupSequence;
              
                  // Must be valid, but had better check anyway
                  if (tgs != null)
                  {
                      // Move relative to given base in the sequence
                      MoveActiveInSequence(tgsParent, tgs);
                  }
              }
          }
          
          public virtual void OnTabControlCreated(Controls.TabControl tc)
          {
  			// Only modify leaf count when not suspended
  			if (_suspendLeafCount == 0)
  			{
  				// Remember how many leafs there are
  				_numLeafs++;
  			}
  			
              // Define default values
              tc.Appearance = Magic.Controls.TabControl.VisualAppearance.MultiDocument;
              tc.BoldSelectedPage = false;
  			tc.DragOverSelect = false;
              tc.IDEPixelBorder = true;
              tc.ImageList = _imageList;
              tc.Style = _style;
  
              // Apply the current display tab mode setting            
              switch(_displayTabMode)
              {
                  case TabbedGroups.DisplayTabModes.ShowAll:
                      tc.HideTabsMode = Magic.Controls.TabControl.HideTabsModes.ShowAlways;
                      break;
                  case TabbedGroups.DisplayTabModes.HideAll:
                      tc.HideTabsMode = Magic.Controls.TabControl.HideTabsModes.HideAlways;
                      break;
              }
              
              // Has anyone registered for the event?
              if (TabControlCreated != null)
                  TabControlCreated(this, tc);
          }
          
          public virtual void OnPageCloseRequested(TGCloseRequestEventArgs e)
          {
              // Has anyone registered for the event?
              if (PageCloseRequest != null)
                  PageCloseRequest(this, e);
                  
          }
  
          public virtual void OnPageContextMenu(TGContextMenuEventArgs e)
          {
              // Has anyone registered for the event?
              if (PageContextMenu != null)
                  PageContextMenu(this, e);
          }
  
          public virtual void OnGlobalSaving(XmlTextWriter xmlOut)
          {
              // Has anyone registered for the event?
              if (GlobalSaving != null)
                  GlobalSaving(this, xmlOut);
          }
          
          public virtual void OnGlobalLoading(XmlTextReader xmlIn)
          {
              // Has anyone registered for the event?
              if (GlobalLoading != null)
                  GlobalLoading(this, xmlIn);
          }
          
          public virtual void OnPageSaving(TGPageSavingEventArgs e)
          {
              // Has anyone registered for the event?
              if (PageSaving != null)
                  PageSaving(this, e);
          }
          
          public virtual void OnPageLoading(TGPageLoadingEventArgs e)
          {
              // Has anyone registered for the event?
              if (PageLoading != null)
                  PageLoading(this, e);
          }
  
          public virtual void OnProminentLeafChanged(EventArgs e)
          {
              // Has anyone registered for the event?
  
gt:             if (ProminentLeafChanged != null)