using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Haestad.WPFRibbonPrototype
{
    /// <summary>
    /// Interaction logic for TestWPFUserControl.xaml
    /// </summary>
    public partial class TestWPFUserControl : UserControl
    {
        public TestWPFUserControl()
        {
            InitializeComponent();

            // Note: <whar> Works around Telerik "issue" where it wants to put focus on the first child Control with IsTabStop == true,
            // for a newly created backstage user control.
            // We look at all the controls that have IsTabStop == true, record them, and temporarily disable IsTabStop.
            IEnumerable<Control> controls = Grid1.Children.OfType<Control>().Where(c => c.IsTabStop == true);
            TabStopControls = controls.GetEnumerator();
            while (TabStopControls.MoveNext())
                TabStopControls.Current.IsTabStop = false;
        }

        private void Button_GotFocus(object sender, RoutedEventArgs e)
        {
            //System.Windows.MessageBox.Show("I got focus: " + ((Button)sender).ToString());
        }

        private void Grid_GotFocus(object sender, RoutedEventArgs e)
        {
            // Note: <whar> When the parent grid gets focus, we restore the status of child controls.
            TabStopControls.Reset();
            while (TabStopControls.MoveNext())
                TabStopControls.Current.IsTabStop = true;
            TabStopControls = null;
        }

        private IEnumerator<Control> TabStopControls
        {
            get;
            set;
        }
    }
}
