changeset 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
files FrmMainWindow.cs gtk-gui/IBBoard.WarFoundry.GTK.FrmMainWindow.cs gtk-gui/gui.stetic
diffstat 3 files changed, 73 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/FrmMainWindow.cs	Sat Aug 14 15:43:30 2010 +0000
+++ b/FrmMainWindow.cs	Sat Aug 14 19:25:08 2010 +0000
@@ -82,12 +82,12 @@
 			redoMenuButton = new MenuToolButton("gtk-redo");
 			redoMenuButton.Label = "Redo";
 			redoMenuButton.TooltipText = "Redo";
-			redoMenuButton.Clicked+= redoTBButtonActivated;
+			redoMenuButton.Clicked += redoTBButtonActivated;
 			toolbar.Insert(redoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX);
 			undoMenuButton = new MenuToolButton("gtk-undo");
 			undoMenuButton.Label = "Undo";
 			undoMenuButton.TooltipText = "Undo";
-			undoMenuButton.Clicked+= undoTBButtonActivated;
+			undoMenuButton.Clicked += undoTBButtonActivated;
 			toolbar.Insert(undoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX);
 			toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-1]);
 			toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-2]);
@@ -611,19 +611,19 @@
 			redoMenuButton.Sensitive = commandStack.CanRedo();
 			miRedo.Sensitive = redoMenuButton.Sensitive;
 			int redoLength = commandStack.RedoLength;
-			//TODO: Build menus for undo/redo and find way of adding tooltips
-			/*int maxRedo = Math.Min(10, redoLength);
-			MenuItem[] menuItems = null;
 
+			int maxRedo = Math.Min(10, redoLength);
+			
+			//TODO: Add tooltips
 			if (redoLength > 0)
 			{
-				menuItems = new MenuItem[maxRedo];
+				Menu menu = new Menu();
 				Command com;
 				MenuItem mi;
 
 				for (int i = 0; i < maxRedo; i++)
 				{
-					com = commandStack.PeekRedoCommand(i+1);
+					com = commandStack.PeekRedoCommand(i + 1);
 
 					if (com == null)
 					{
@@ -631,37 +631,30 @@
 					}
 
 					mi = new MenuItem(com.Description);
-					mi.Click+=new EventHandler(redoMenu_Click);
-					menuItems[i] = mi;
+					mi.Activated += new EventHandler(RedoMenuActivated);
+					menu.Append(mi);
 				}
+				
+				menu.ShowAll();
+				redoMenuButton.Menu = menu;
 			}
-
-			redoMenu.MenuItems.Clear();
-
-			if (menuItems!=null && menuItems[0]!=null)
+			else
 			{
-				bttnRedo.ToolTipText = menuItems[0].Text;
-				redoMenu.MenuItems.AddRange(menuItems);
-			}*/
-			//TODO: Put above code back when we have a dropdown version of the redo button
-			if (redoLength > 0)
-			{
-				//redoMenuButton.Tooltip = CommandStack.PeekRedoCommand().Description;
+				redoMenuButton.Menu = null;
 			}
-
+			
 			int undoLength = commandStack.UndoLength;
-			/*int maxUndo = Math.Min(10, undoLength);
-			MenuItem[] menuItemsUndo = null;
+			int maxUndo = Math.Min(10, undoLength);
 
 			if (undoLength > 0)
 			{
-				menuItemsUndo = new MenuItem[maxUndo];
+				Menu menu = new Menu();
 				Command com;
 				MenuItem mi;
 
 				for (int i = 0; i < maxUndo; i++)
 				{
-					com = commandStack.PeekUndoCommand(i+1);
+					com = commandStack.PeekUndoCommand(i + 1);
 
 					if (com == null)
 					{
@@ -669,26 +662,59 @@
 					}
 
 					mi = new MenuItem(com.UndoDescription);
-					mi.Click+=new EventHandler(undoMenu_Click);
-					menuItemsUndo[i] = mi;
+					mi.Activated += new EventHandler(UndoMenuActivated);
+					menu.Add(mi);
 				}
+				
+				menu.ShowAll();
+				undoMenuButton.Menu = menu;
+			}
+			else
+			{
+				undoMenuButton.Menu = null;
 			}
 
-			undoMenu.MenuItems.Clear();
-
-			if (menuItemsUndo!=null && menuItemsUndo[0]!=null)
+			saveArmyButton.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null;
+			miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy != null;
+		}
+		
+		private void RedoMenuActivated(object sender, EventArgs e)
+		{
+			if (sender is MenuItem)
 			{
-				bttnUndo.ToolTipText = menuItemsUndo[0].Text;
-				undoMenu.MenuItems.AddRange(menuItemsUndo);
-			}*/
-			//TODO: Put above code back when we have a dropdown version of the undo button
-			if (undoLength > 0)
+				MenuItem item = (MenuItem)sender;
+				//we know it's an redo menu item so find it's index and redo everything
+				
+				int max = Arrays.IndexOf(((Menu)redoMenuButton.Menu).Children, item);
+				
+				if (max >= 0)
+				{
+					for (int i = 0; i <= max; i++)
+					{
+						commandStack.Redo();
+					}
+				}
+			}
+		}
+
+		private void UndoMenuActivated(object sender, EventArgs e)
+		{
+			if (sender is MenuItem)
 			{
-				//undoMenuButton.Tooltip = CommandStack.PeekUndoCommand().UndoDescription;
+				
+				MenuItem item = (MenuItem)sender;
+				//we know it's an undo menu item so find it's index and undo everything
+				
+				int max = Arrays.IndexOf(((Menu)undoMenuButton.Menu).Children, item);
+				
+				if (max >= 0)
+				{
+					for (int i = 0; i <= max; i++)
+					{
+						commandStack.Undo();
+					}
+				}
 			}
-
-			saveArmyButton.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null;
-			miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null;
 		}
 
 		private bool SaveCurrentArmyOrSaveAs()
@@ -933,29 +959,21 @@
 			UnitDisplayWidget widget;
 			unitToWidgetMap.TryGetValue(unit, out widget);
 
-			if (widget!=null)
+			if (widget != null)
 			{
-				logger.DebugFormat("Selecting existing page for "+unit.Name);
+				logger.DebugFormat("Selecting existing page for " + unit.Name);
 				unitsNotebook.Page = unitsNotebook.PageNum(widget);
 			}
 			else
 			{
 				widget = new UnitDisplayWidget(unit, CommandStack);
-				logger.Debug("Adding page for "+unit.Name);
+				logger.Debug("Adding page for " + unit.Name);
 				unitToWidgetMap[unit] = widget;
-				widget.Destroyed+= new EventHandler(UnitWidgetDestroyed);
 				int pageNum = NotebookUtil.AddPageToNotebookWithCloseButton(unitsNotebook, widget, unit.Name);
-				logger.Debug("Page added at index "+pageNum);
+				logger.Debug("Page added at index " + pageNum);
 				unitsNotebook.ShowAll();
-				unitsNotebook.Page = pageNum;
-			}
-		}
-
-		private void UnitWidgetDestroyed(object sender, EventArgs e)
-		{
-			if (sender is UnitDisplayWidget)
-			{
-				unitToWidgetMap.Remove(((UnitDisplayWidget)sender).Unit);
+				unitsNotebook.CurrentPage = pageNum;
+				unitsNotebook.SetTabReorderable(widget, true);
 			}
 		}
 
--- a/gtk-gui/IBBoard.WarFoundry.GTK.FrmMainWindow.cs	Sat Aug 14 15:43:30 2010 +0000
+++ b/gtk-gui/IBBoard.WarFoundry.GTK.FrmMainWindow.cs	Sat Aug 14 19:25:08 2010 +0000
@@ -190,6 +190,7 @@
 			this.unitsNotebook.CanFocus = true;
 			this.unitsNotebook.Name = "unitsNotebook";
 			this.unitsNotebook.CurrentPage = -1;
+			this.unitsNotebook.Scrollable = true;
 			this.hpaned2.Add(this.unitsNotebook);
 			this.vbox1.Add(this.hpaned2);
 			global::Gtk.Box.BoxChild w6 = ((global::Gtk.Box.BoxChild)(this.vbox1[this.hpaned2]));
--- a/gtk-gui/gui.stetic	Sat Aug 14 15:43:30 2010 +0000
+++ b/gtk-gui/gui.stetic	Sat Aug 14 19:25:08 2010 +0000
@@ -246,6 +246,7 @@
                 <property name="MemberName" />
                 <property name="CanFocus">True</property>
                 <property name="CurrentPage">-1</property>
+                <property name="Scrollable">True</property>
                 <signal name="Removed" handler="NotebookPageRemoved" />
               </widget>
             </child>