comparison FrmMainWindow.cs @ 46:3314f6a46661

Fixes #138: Re-implement menu for undo/redo * Replace WinForms code with equivalent GTK objects to construct menu and handle multi-undo/redo Re #86: Complete initial GTK# UI * Make tabs re-orderable * Make tabs scrollable when we get too many
author IBBoard <dev@ibboard.co.uk>
date Sat, 14 Aug 2010 19:25:08 +0000
parents 5584860fb44b
children 07fd9f7b0dd1
comparison
equal deleted inserted replaced
45:5584860fb44b 46:3314f6a46661
80 Build(); 80 Build();
81 //Replace the undo/redo buttons with menu versions, which Monodevelop's GUI editor doesn't currently support 81 //Replace the undo/redo buttons with menu versions, which Monodevelop's GUI editor doesn't currently support
82 redoMenuButton = new MenuToolButton("gtk-redo"); 82 redoMenuButton = new MenuToolButton("gtk-redo");
83 redoMenuButton.Label = "Redo"; 83 redoMenuButton.Label = "Redo";
84 redoMenuButton.TooltipText = "Redo"; 84 redoMenuButton.TooltipText = "Redo";
85 redoMenuButton.Clicked+= redoTBButtonActivated; 85 redoMenuButton.Clicked += redoTBButtonActivated;
86 toolbar.Insert(redoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX); 86 toolbar.Insert(redoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX);
87 undoMenuButton = new MenuToolButton("gtk-undo"); 87 undoMenuButton = new MenuToolButton("gtk-undo");
88 undoMenuButton.Label = "Undo"; 88 undoMenuButton.Label = "Undo";
89 undoMenuButton.TooltipText = "Undo"; 89 undoMenuButton.TooltipText = "Undo";
90 undoMenuButton.Clicked+= undoTBButtonActivated; 90 undoMenuButton.Clicked += undoTBButtonActivated;
91 toolbar.Insert(undoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX); 91 toolbar.Insert(undoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX);
92 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-1]); 92 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-1]);
93 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-2]); 93 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-2]);
94 toolbar.ShowAll(); 94 toolbar.ShowAll();
95 95
609 undoMenuButton.Sensitive = commandStack.CanUndo(); 609 undoMenuButton.Sensitive = commandStack.CanUndo();
610 miUndo.Sensitive = undoMenuButton.Sensitive; 610 miUndo.Sensitive = undoMenuButton.Sensitive;
611 redoMenuButton.Sensitive = commandStack.CanRedo(); 611 redoMenuButton.Sensitive = commandStack.CanRedo();
612 miRedo.Sensitive = redoMenuButton.Sensitive; 612 miRedo.Sensitive = redoMenuButton.Sensitive;
613 int redoLength = commandStack.RedoLength; 613 int redoLength = commandStack.RedoLength;
614 //TODO: Build menus for undo/redo and find way of adding tooltips 614
615 /*int maxRedo = Math.Min(10, redoLength); 615 int maxRedo = Math.Min(10, redoLength);
616 MenuItem[] menuItems = null; 616
617 617 //TODO: Add tooltips
618 if (redoLength > 0) 618 if (redoLength > 0)
619 { 619 {
620 menuItems = new MenuItem[maxRedo]; 620 Menu menu = new Menu();
621 Command com; 621 Command com;
622 MenuItem mi; 622 MenuItem mi;
623 623
624 for (int i = 0; i < maxRedo; i++) 624 for (int i = 0; i < maxRedo; i++)
625 { 625 {
626 com = commandStack.PeekRedoCommand(i+1); 626 com = commandStack.PeekRedoCommand(i + 1);
627 627
628 if (com == null) 628 if (com == null)
629 { 629 {
630 break; 630 break;
631 } 631 }
632 632
633 mi = new MenuItem(com.Description); 633 mi = new MenuItem(com.Description);
634 mi.Click+=new EventHandler(redoMenu_Click); 634 mi.Activated += new EventHandler(RedoMenuActivated);
635 menuItems[i] = mi; 635 menu.Append(mi);
636 } 636 }
637 } 637
638 638 menu.ShowAll();
639 redoMenu.MenuItems.Clear(); 639 redoMenuButton.Menu = menu;
640 640 }
641 if (menuItems!=null && menuItems[0]!=null) 641 else
642 { 642 {
643 bttnRedo.ToolTipText = menuItems[0].Text; 643 redoMenuButton.Menu = null;
644 redoMenu.MenuItems.AddRange(menuItems); 644 }
645 }*/ 645
646 //TODO: Put above code back when we have a dropdown version of the redo button
647 if (redoLength > 0)
648 {
649 //redoMenuButton.Tooltip = CommandStack.PeekRedoCommand().Description;
650 }
651
652 int undoLength = commandStack.UndoLength; 646 int undoLength = commandStack.UndoLength;
653 /*int maxUndo = Math.Min(10, undoLength); 647 int maxUndo = Math.Min(10, undoLength);
654 MenuItem[] menuItemsUndo = null;
655 648
656 if (undoLength > 0) 649 if (undoLength > 0)
657 { 650 {
658 menuItemsUndo = new MenuItem[maxUndo]; 651 Menu menu = new Menu();
659 Command com; 652 Command com;
660 MenuItem mi; 653 MenuItem mi;
661 654
662 for (int i = 0; i < maxUndo; i++) 655 for (int i = 0; i < maxUndo; i++)
663 { 656 {
664 com = commandStack.PeekUndoCommand(i+1); 657 com = commandStack.PeekUndoCommand(i + 1);
665 658
666 if (com == null) 659 if (com == null)
667 { 660 {
668 break; 661 break;
669 } 662 }
670 663
671 mi = new MenuItem(com.UndoDescription); 664 mi = new MenuItem(com.UndoDescription);
672 mi.Click+=new EventHandler(undoMenu_Click); 665 mi.Activated += new EventHandler(UndoMenuActivated);
673 menuItemsUndo[i] = mi; 666 menu.Add(mi);
674 } 667 }
675 } 668
676 669 menu.ShowAll();
677 undoMenu.MenuItems.Clear(); 670 undoMenuButton.Menu = menu;
678 671 }
679 if (menuItemsUndo!=null && menuItemsUndo[0]!=null) 672 else
680 { 673 {
681 bttnUndo.ToolTipText = menuItemsUndo[0].Text; 674 undoMenuButton.Menu = null;
682 undoMenu.MenuItems.AddRange(menuItemsUndo); 675 }
683 }*/ 676
684 //TODO: Put above code back when we have a dropdown version of the undo button 677 saveArmyButton.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null;
685 if (undoLength > 0) 678 miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null;
686 { 679 }
687 //undoMenuButton.Tooltip = CommandStack.PeekUndoCommand().UndoDescription; 680
688 } 681 private void RedoMenuActivated(object sender, EventArgs e)
689 682 {
690 saveArmyButton.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null; 683 if (sender is MenuItem)
691 miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null; 684 {
685 MenuItem item = (MenuItem)sender;
686 //we know it's an redo menu item so find it's index and redo everything
687
688 int max = Arrays.IndexOf(((Menu)redoMenuButton.Menu).Children, item);
689
690 if (max >= 0)
691 {
692 for (int i = 0; i <= max; i++)
693 {
694 commandStack.Redo();
695 }
696 }
697 }
698 }
699
700 private void UndoMenuActivated(object sender, EventArgs e)
701 {
702 if (sender is MenuItem)
703 {
704
705 MenuItem item = (MenuItem)sender;
706 //we know it's an undo menu item so find it's index and undo everything
707
708 int max = Arrays.IndexOf(((Menu)undoMenuButton.Menu).Children, item);
709
710 if (max >= 0)
711 {
712 for (int i = 0; i <= max; i++)
713 {
714 commandStack.Undo();
715 }
716 }
717 }
692 } 718 }
693 719
694 private bool SaveCurrentArmyOrSaveAs() 720 private bool SaveCurrentArmyOrSaveAs()
695 { 721 {
696 if (loadedArmyPath != null) 722 if (loadedArmyPath != null)
931 private void ShowUnitWidget(WFObjects.Unit unit) 957 private void ShowUnitWidget(WFObjects.Unit unit)
932 { 958 {
933 UnitDisplayWidget widget; 959 UnitDisplayWidget widget;
934 unitToWidgetMap.TryGetValue(unit, out widget); 960 unitToWidgetMap.TryGetValue(unit, out widget);
935 961
936 if (widget!=null) 962 if (widget != null)
937 { 963 {
938 logger.DebugFormat("Selecting existing page for "+unit.Name); 964 logger.DebugFormat("Selecting existing page for " + unit.Name);
939 unitsNotebook.Page = unitsNotebook.PageNum(widget); 965 unitsNotebook.Page = unitsNotebook.PageNum(widget);
940 } 966 }
941 else 967 else
942 { 968 {
943 widget = new UnitDisplayWidget(unit, CommandStack); 969 widget = new UnitDisplayWidget(unit, CommandStack);
944 logger.Debug("Adding page for "+unit.Name); 970 logger.Debug("Adding page for " + unit.Name);
945 unitToWidgetMap[unit] = widget; 971 unitToWidgetMap[unit] = widget;
946 widget.Destroyed+= new EventHandler(UnitWidgetDestroyed);
947 int pageNum = NotebookUtil.AddPageToNotebookWithCloseButton(unitsNotebook, widget, unit.Name); 972 int pageNum = NotebookUtil.AddPageToNotebookWithCloseButton(unitsNotebook, widget, unit.Name);
948 logger.Debug("Page added at index "+pageNum); 973 logger.Debug("Page added at index " + pageNum);
949 unitsNotebook.ShowAll(); 974 unitsNotebook.ShowAll();
950 unitsNotebook.Page = pageNum; 975 unitsNotebook.CurrentPage = pageNum;
951 } 976 unitsNotebook.SetTabReorderable(widget, true);
952 }
953
954 private void UnitWidgetDestroyed(object sender, EventArgs e)
955 {
956 if (sender is UnitDisplayWidget)
957 {
958 unitToWidgetMap.Remove(((UnitDisplayWidget)sender).Unit);
959 } 977 }
960 } 978 }
961 979
962 protected virtual void OnMiExportAsBasicHtmlActivated (object sender, System.EventArgs e) 980 protected virtual void OnMiExportAsBasicHtmlActivated (object sender, System.EventArgs e)
963 { 981 {