public void onCloseTab(object sender, EventArgs e)
        {
            TabItem item = null;

            if (sender is TabItem)
            {
                item = sender as TabItem;
            }
            else
            {
                item = ((Grid)((Button)sender).Parent).Parent as TabItem;
            }
            if (item != null)
            {
                IDEEditor editor = item.Content as IDEEditor;
                if (editor != null)
                {
                    if (editor.IsDirty)
                    {
                        MessageBoxResult res = ModernDialog.ShowMessage("Save file changes before closing?", "Close?", MessageBoxButton.YesNoCancel);
                        if (res == MessageBoxResult.Yes)
                        {
                            editor.Save();
                        }
                        else if (res == MessageBoxResult.Cancel)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    if (item.Content is Control && ((Control)item.Content).Tag is PluginLib.IExternalControlData)
                    {
                        PluginLib.IExternalControlData data = ((Control)item.Content).Tag as PluginLib.IExternalControlData;
                        if (data.IsDirty)
                        {
                            MessageBoxResult res = ModernDialog.ShowMessage("Save file changes before closing?", "Close?", MessageBoxButton.YesNoCancel);
                            if (res == MessageBoxResult.Yes)
                            {
                                data.SaveData();
                            }
                            else if (res == MessageBoxResult.Cancel)
                            {
                                return;
                            }
                        }
                    }
                }
                tabs.Items.Remove(item);
            }
        }
 void EditorTabs_MouseUp(object sender, MouseButtonEventArgs e)
 {
     if (e.ChangedButton == MouseButton.Middle)
     {
         if (sender is TabItem)
         {
             IDEEditor editor = (sender as TabItem).Content as IDEEditor;
             TabItem   item   = sender as TabItem;
             if (editor != null)
             {
                 if (editor.IsDirty)
                 {
                     MessageBoxResult res = ModernDialog.ShowMessage("Save file changes before closing?", "Close?", MessageBoxButton.YesNoCancel);
                     if (res == MessageBoxResult.OK)
                     {
                         editor.Save();
                     }
                     else if (res == MessageBoxResult.Cancel)
                     {
                         return;
                     }
                 }
             }
             else
             {
                 if (item.Content is Control && ((Control)item.Content).Tag is PluginLib.IExternalControlData)
                 {
                     PluginLib.IExternalControlData data = ((Control)item.Content).Tag as PluginLib.IExternalControlData;
                     if (data.IsDirty)
                     {
                         MessageBoxResult res = ModernDialog.ShowMessage("Save file changes before closing?", "Close?", MessageBoxButton.YesNoCancel);
                         if (res == MessageBoxResult.Yes)
                         {
                             data.SaveData();
                         }
                         else if (res == MessageBoxResult.Cancel)
                         {
                             return;
                         }
                     }
                 }
             }
             tabs.Items.Remove(sender);
         }
     }
 }
 public void SaveAll()
 {
     foreach (TabItem o in tabs.Items)
     {
         // Easy case
         IDEEditor editor = o.Content as IDEEditor;
         if (editor != null)
         {
             editor.Save();
         }
         else // Plugin editors
         {
             if (o.Content is Control && ((Control)o.Content).Tag is PluginLib.IExternalControlData)
             {
                 PluginLib.IExternalControlData data = ((Control)o.Content).Tag as PluginLib.IExternalControlData;
                 data.SaveData();
             }
         }
     }
 }
        public IDEEditor OpenFile(FileBaseItem aFile)
        {
            if (Intellisense.Sources.SourceBuilder.HandlesExtension(System.IO.Path.GetExtension(aFile.Path)))
            {
                foreach (TabItem item in tabs.Items)
                {
                    if (item.Tag.Equals(aFile.Path))
                    {
                        tabs.SelectedItem = item;
                        ((IDEEditor)((TabItem)tabs.SelectedItem).Content).SetCode(aFile);
                        return((IDEEditor)((TabItem)tabs.SelectedItem).Content);
                    }
                }
                Grid grid = new Grid();
                grid.ColumnDefinitions.Add(new ColumnDefinition());
                grid.ColumnDefinitions.Add(new ColumnDefinition());

                IDEEditor ideEditor = new IDEEditor(aFile);

                TextBlock txt = new TextBlock {
                    Text = aFile.Name
                };
                txt.DataContext = ideEditor.changeChecker;
                txt.Foreground  = FindResource("ButtonText") as Brush;
                txt.Style       = FindResource("IDETabHeader") as Style;

                grid.Children.Add(txt);
                Button close = new Button {
                    Content = "X", Padding = new Thickness(0), Foreground = new SolidColorBrush(Colors.LightGray), FontWeight = FontWeights.Bold, VerticalAlignment = System.Windows.VerticalAlignment.Top, HorizontalAlignment = System.Windows.HorizontalAlignment.Right
                };
                close.MinHeight  = close.MinWidth = 18;
                close.MaxHeight  = close.MaxWidth = 18;
                close.Background = close.BorderBrush = null;
                close.Click     += onCloseTab;
                Grid.SetColumn(txt, 0);
                Grid.SetColumn(close, 1);
                grid.Children.Add(close);

                tabs.Items.Add(new TabItem
                {
                    Tag     = aFile.Path,
                    Header  = grid,
                    Content = ideEditor,
                });
                ((TabItem)tabs.Items[tabs.Items.Count - 1]).MouseUp += EditorTabs_MouseUp;
                tabs.SelectedItem = tabs.Items[tabs.Items.Count - 1];
                return((IDEEditor)((TabItem)tabs.SelectedItem).Content);
            }
            else
            {
                foreach (PluginLib.IFileEditor editor in PluginManager.inst().FileEditors)
                {
                    if (editor.CanEditFile(aFile.Path, System.IO.Path.GetExtension(aFile.Name)))
                    {
                        object ud = null;
                        PluginLib.IExternalControlData externalControl = editor.CreateEditorContent(aFile.Path);

                        Grid grid = new Grid();
                        grid.ColumnDefinitions.Add(new ColumnDefinition());
                        grid.ColumnDefinitions.Add(new ColumnDefinition());

                        TextBlock txt = new TextBlock {
                            Text = aFile.Name
                        };
                        txt.DataContext = externalControl;
                        txt.Foreground  = FindResource("ButtonText") as Brush;
                        txt.Style       = FindResource("IDETabHeader") as Style;

                        grid.Children.Add(txt);
                        Button close = new Button {
                            Content = "X", Padding = new Thickness(0), Foreground = new SolidColorBrush(Colors.LightGray), FontWeight = FontWeights.Bold, VerticalAlignment = System.Windows.VerticalAlignment.Top, HorizontalAlignment = System.Windows.HorizontalAlignment.Right
                        };
                        close.MinHeight  = close.MinWidth = 18;
                        close.MaxHeight  = close.MaxWidth = 18;
                        close.Background = close.BorderBrush = null;
                        close.Click     += onCloseTab;
                        Grid.SetColumn(txt, 0);
                        Grid.SetColumn(close, 1);
                        grid.Children.Add(close);

                        tabs.Items.Add(new TabItem
                        {
                            Tag     = aFile.Path,
                            Header  = grid,
                            Content = externalControl.Control,
                        });
                        ((TabItem)tabs.Items[tabs.Items.Count - 1]).MouseUp += EditorTabs_MouseUp;
                        tabs.SelectedItem = tabs.Items[tabs.Items.Count - 1];
                        return(null);
                    }
                }
                return(null);
            }
        }
        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            if (e.NavigationType == FirstFloor.ModernUI.Windows.Navigation.NavigationType.New)
            {
                if (IDEProject.inst() == null)
                {
                    IDEProject.open();
                    System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
                    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        IDEProject.inst().Settings   = IDESettings.GetOrCreate(dlg.SelectedPath);
                        IDEProject.inst().ProjectDir = dlg.SelectedPath;
                        UserData.inst().AddRecentFile(dlg.SelectedPath);
                    }
                    else
                    {
                        MainWindow.inst().ContentSource = new Uri("Screens/LaunchScreen.xaml", UriKind.Relative);
                        IDEProject.inst().destroy();
                        return;
                    }
                }

                if (IDEProject.inst() == null)
                {
                    project_ = new IDEProject();
                }
                else
                {
                    project_ = IDEProject.inst();
                }
                if (folder_ == null)
                {
                    folder_ = new Folder {
                        Path = project_.ProjectDir
                    }
                }
                ;
                if (fileTree.DataContext == null)
                {
                    fileTree.DataContext = folder_;
                }
                Action <object> searchAction = delegate(object o)
                {
                    if (o == null)
                    {
                        return;
                    }
                    leftSideTabs.SelectedItem = classesTab;
                    objectTree.SelectItemNamed(((PropInfo)o).Type.Name);
                };
                objectTree.DataContext    = IDEProject.inst();
                objectTree.ItemBinding    = new Binding("GlobalTypes.TypeInfo");
                objectTree.CallOnViewType = searchAction;

                globalsTree.DataContext    = IDEProject.inst();
                globalsTree.CallOnViewType = searchAction;
                globalsTree.ItemBinding    = new Binding("GlobalTypes.UIView");

                localsTree.DataContext    = IDEProject.inst();
                localsTree.CallOnViewType = searchAction;
                localsTree.ItemBinding    = new Binding("LocalTypes.AllUIView");

                txtConsole.DataContext       = IDEProject.inst();
                gridErrors.DataContext       = IDEProject.inst();
                errorTabCount.DataContext    = IDEProject.inst();
                stackErrorHeader.DataContext = IDEProject.inst();

                if (infoTabs.Items.Count == 0)
                {
                    foreach (PluginLib.IInfoTab infoTab in PluginManager.inst().InfoTabs)
                    {
                        PluginLib.IExternalControlData data = infoTab.CreateTabContent(IDEProject.inst().ProjectDir);
                        if (data == null)
                        {
                            continue;
                        }
                        TabItem item = new TabItem
                        {
                            Header  = infoTab.GetTabName(),
                            Tag     = data,
                            Content = data.Control
                        };
                        infoTabs.Items.Add(item);
                    }
                }
            }
            else
            {
                infoTabs.Items.Clear();
            }
        }