1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
//
// MenuStrip.cs
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2006 Jonathan Pobst
//
// Authors:
// Jonathan Pobst (monkey@jpobst.com)
//
using System;
using System.Drawing;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace System.Windows.Forms
{
[ClassInterface (ClassInterfaceType.AutoDispatch)]
[ComVisible (true)]
public class MenuStrip : ToolStrip
{
private ToolStripMenuItem mdi_window_list_item;
public MenuStrip () : base ()
{
base.CanOverflow = false;
this.GripStyle = ToolStripGripStyle.Hidden;
this.Stretch = true;
this.Dock = DockStyle.Top;
}
#region Public Properties
[DefaultValue (false)]
[Browsable (false)]
public new bool CanOverflow {
get { return base.CanOverflow; }
set { base.CanOverflow = value; }
}
[DefaultValue (ToolStripGripStyle.Hidden)]
public new ToolStripGripStyle GripStyle {
get { return base.GripStyle; }
set { base.GripStyle = value; }
}
[DefaultValue (null)]
[MergableProperty (false)]
[TypeConverter (typeof (MdiWindowListItemConverter))]
public ToolStripMenuItem MdiWindowListItem {
get { return this.mdi_window_list_item; }
set {
if (this.mdi_window_list_item != value) {
this.mdi_window_list_item = value;
this.RefreshMdiItems ();
}
}
}
[DefaultValue (false)]
public new bool ShowItemToolTips {
get { return base.ShowItemToolTips; }
set { base.ShowItemToolTips = value; }
}
[DefaultValue (true)]
public new bool Stretch {
get { return base.Stretch; }
set { base.Stretch = value; }
}
#endregion
#region Protected Properties
protected override Padding DefaultGripMargin { get { return new Padding (2, 2, 0, 2); } }
protected override Padding DefaultPadding { get { return new Padding (6, 2, 0, 2); } }
protected override bool DefaultShowItemToolTips { get { return false; } }
protected override Size DefaultSize { get { return new Size (200, 24); } }
#endregion
#region Protected Methods
protected override AccessibleObject CreateAccessibilityInstance ()
{
return new MenuStripAccessibleObject ();
}
protected internal override ToolStripItem CreateDefaultItem (string text, Image image, EventHandler onClick)
{
return new ToolStripMenuItem (text, image, onClick);
}
protected virtual void OnMenuActivate (EventArgs e)
{
EventHandler eh = (EventHandler)(Events [MenuActivateEvent]);
if (eh != null)
eh (this, e);
}
protected virtual void OnMenuDeactivate (EventArgs e)
{
EventHandler eh = (EventHandler)(Events [MenuDeactivateEvent]);
if (eh != null)
eh (this, e);
}
protected override bool ProcessCmdKey (ref Message m, Keys keyData)
{
return base.ProcessCmdKey (ref m, keyData);
}
protected override void WndProc (ref Message m)
{
base.WndProc (ref m);
}
#endregion
#region Public Events
static object MenuActivateEvent = new object ();
static object MenuDeactivateEvent = new object ();
public event EventHandler MenuActivate {
add { Events.AddHandler (MenuActivateEvent, value); }
remove { Events.RemoveHandler (MenuActivateEvent, value); }
}
public event EventHandler MenuDeactivate {
add { Events.AddHandler (MenuDeactivateEvent, value); }
remove { Events.RemoveHandler (MenuDeactivateEvent, value); }
}
#endregion
#region Internal Properties
internal override bool KeyboardActive {
get { return base.KeyboardActive; }
set {
if (base.KeyboardActive != value) {
base.KeyboardActive = value;
if (value)
this.OnMenuActivate (EventArgs.Empty);
else
this.OnMenuDeactivate (EventArgs.Empty);
}
}
}
internal bool MenuDroppedDown {
get { return this.menu_selected; }
set { this.menu_selected = value; }
}
#endregion
#region Internal Methods
internal override void Dismiss (ToolStripDropDownCloseReason reason)
{
// Make sure we don't auto-dropdown next time we're activated
this.MenuDroppedDown = false;
base.Dismiss (reason);
this.FireMenuDeactivate ();
}
internal void FireMenuActivate ()
{
// The tracker lets us know when the form is clicked or loses focus
ToolStripManager.AppClicked += new EventHandler (ToolStripMenuTracker_AppClicked);
ToolStripManager.AppFocusChange += new EventHandler (ToolStripMenuTracker_AppFocusChange);
this.OnMenuActivate (EventArgs.Empty);
}
internal void FireMenuDeactivate ()
{
// Detach from the tracker
ToolStripManager.AppClicked -= new EventHandler (ToolStripMenuTracker_AppClicked); ;
ToolStripManager.AppFocusChange -= new EventHandler (ToolStripMenuTracker_AppFocusChange);
this.OnMenuDeactivate (EventArgs.Empty);
}
internal override bool OnMenuKey ()
{
// Set ourselves active and select our first item
ToolStripManager.SetActiveToolStrip (this, true);
ToolStripItem tsi = this.SelectNextToolStripItem (null, true);
if (tsi == null)
return false;
if (tsi is MdiControlStrip.SystemMenuItem)
this.SelectNextToolStripItem (tsi, true);
return true;
}
private void ToolStripMenuTracker_AppFocusChange (object sender, EventArgs e)
{
this.GetTopLevelToolStrip ().Dismiss (ToolStripDropDownCloseReason.AppFocusChange);
}
private void ToolStripMenuTracker_AppClicked (object sender, EventArgs e)
{
this.GetTopLevelToolStrip ().Dismiss (ToolStripDropDownCloseReason.AppClicked);
}
internal void RefreshMdiItems ()
{
if (this.mdi_window_list_item == null)
return;
Form parent_form = this.FindForm ();
if (parent_form == null || parent_form.MainMenuStrip != this)
return;
MdiClient mdi = parent_form.MdiContainer;
// If there isn't a MdiContainer, we don't need to worry about MdiItems :)
if (mdi == null)
return;
// Make a copy so we can delete from the real one
ToolStripItem[] loopitems = new ToolStripItem[this.mdi_window_list_item.DropDownItems.Count];
this.mdi_window_list_item.DropDownItems.CopyTo (loopitems, 0);
// If the mdi child has been removed, remove our menu item
foreach (ToolStripItem tsi in loopitems)
if (tsi is ToolStripMenuItem && (tsi as ToolStripMenuItem).IsMdiWindowListEntry)
if (!mdi.mdi_child_list.Contains ((tsi as ToolStripMenuItem).MdiClientForm) || !(tsi as ToolStripMenuItem).MdiClientForm.Visible)
this.mdi_window_list_item.DropDownItems.Remove (tsi);
// Add the new forms and update state
for (int i = 0; i < mdi.mdi_child_list.Count; i++) {
Form mdichild = (Form)mdi.mdi_child_list[i];
ToolStripMenuItem tsi;
if (!mdichild.Visible)
continue;
if ((tsi = FindMdiMenuItemOfForm (mdichild)) == null) {
if (CountMdiMenuItems () == 0 && this.mdi_window_list_item.DropDownItems.Count > 0 && !(this.mdi_window_list_item.DropDownItems[this.mdi_window_list_item.DropDownItems.Count - 1] is ToolStripSeparator))
this.mdi_window_list_item.DropDownItems.Add (new ToolStripSeparator ());
tsi = new ToolStripMenuItem ();
tsi.MdiClientForm = mdichild;
this.mdi_window_list_item.DropDownItems.Add (tsi);
}
tsi.Text = string.Format ("&{0} {1}", i + 1, mdichild.Text);
tsi.Checked = parent_form.ActiveMdiChild == mdichild;
}
// Check that everything is in the correct order
if (NeedToReorderMdi ())
ReorderMdiMenu ();
}
private ToolStripMenuItem FindMdiMenuItemOfForm (Form f)
{
// Not terribly efficient, but Mdi window lists shouldn't get too big
foreach (ToolStripItem tsi in this.mdi_window_list_item.DropDownItems)
if (tsi is ToolStripMenuItem && (tsi as ToolStripMenuItem).MdiClientForm == f)
return (ToolStripMenuItem)tsi;
return null;
}
private int CountMdiMenuItems ()
{
int count = 0;
foreach (ToolStripItem tsi in this.mdi_window_list_item.DropDownItems)
if (tsi is ToolStripMenuItem && (tsi as ToolStripMenuItem).IsMdiWindowListEntry)
count++;
return count;
}
private bool NeedToReorderMdi ()
{
// Mdi menus must be: User Items, Separator, Mdi Items
bool seenMdi = false;
foreach (ToolStripItem tsi in this.mdi_window_list_item.DropDownItems) {
if (tsi is ToolStripMenuItem) {
if (!(tsi as ToolStripMenuItem).IsMdiWindowListEntry) {
if (seenMdi)
return true;
} else
seenMdi = true;
}
}
return false;
}
private void ReorderMdiMenu ()
{
ToolStripItem[] loopitems = new ToolStripItem[this.mdi_window_list_item.DropDownItems.Count];
this.mdi_window_list_item.DropDownItems.CopyTo (loopitems, 0);
this.mdi_window_list_item.DropDownItems.Clear ();
foreach (ToolStripItem tsi in loopitems)
if (tsi is ToolStripSeparator || !(tsi as ToolStripMenuItem).IsMdiWindowListEntry)
this.mdi_window_list_item.DropDownItems.Add (tsi);
int count = this.mdi_window_list_item.DropDownItems.Count;
if (count > 0 && !(this.mdi_window_list_item.DropDownItems[count - 1] is ToolStripSeparator))
this.mdi_window_list_item.DropDownItems.Add (new ToolStripSeparator ());
foreach (ToolStripItem tsi in loopitems)
if (tsi is ToolStripMenuItem && (tsi as ToolStripMenuItem).IsMdiWindowListEntry)
this.mdi_window_list_item.DropDownItems.Add (tsi);
}
#endregion
#region MenuStripAccessibleObject
private class MenuStripAccessibleObject : AccessibleObject
{
}
#endregion
}
#region MdiWindowListItemConverter
internal class MdiWindowListItemConverter : TypeConverter
{
}
#endregion
}
|