ZedGraphControl.ContextMenu.cs 31.3 KB
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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//=============================================================================

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using System.Runtime.InteropServices;
using System.Globalization;
//using System.Diagnostics;

namespace ZedGraph
{
	partial class ZedGraphControl
	{

	#region ContextMenu

		// Revision: JCarpenter 10/06
		/// <summary>
		/// Public enumeration that specifies the type of 
		/// object present at the Context Menu's mouse location
		/// </summary>
		public enum ContextMenuObjectState
		{
			/// <summary>
			/// The object is an Inactive Curve Item at the Context Menu's mouse position
			/// </summary>
			InactiveSelection,
			/// <summary>
			/// The object is an active Curve Item at the Context Menu's mouse position
			/// </summary>
			ActiveSelection,
			/// <summary>
			/// There is no selectable object present at the Context Menu's mouse position
			/// </summary>
			Background
		}

		//Revision: JCarpenter 10/06
		/// <summary>
		/// Find the object currently under the mouse cursor, and return its state.
		/// </summary>
		private ContextMenuObjectState GetObjectState()
		{
			ContextMenuObjectState objState = ContextMenuObjectState.Background;

			// Determine object state
			Point mousePt = this.PointToClient( Control.MousePosition );
			int iPt;
			object nearestObj;

			using ( Graphics g = this.CreateGraphics() )
			{
				if ( this.MasterPane.FindNearestPaneObject( mousePt, g, out pane,
						out nearestObj, out iPt ) )
				{
					item = nearestObj as CurveItem;

					if ( item != null && iPt >= 0 )
					{
						if ( item.IsSelected )
							objState = ContextMenuObjectState.ActiveSelection;
						else
							objState = ContextMenuObjectState.InactiveSelection;
					}
				}
			}

			return objState;
		}

	    private CurveItem item;
	    private GraphPane pane;
        CultureInfo ci = Thread.CurrentThread.CurrentCulture;
		/// <summary>
		/// protected method to handle the popup context menu in the <see cref="ZedGraphControl"/>.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void contextMenuStrip1_Opening( object sender, CancelEventArgs e )
		{
			// disable context menu by default
			e.Cancel = true;
			this.MenuClick_RestoreScale(sender, EventArgs.Empty);
			return;
			ContextMenuStrip menuStrip = sender as ContextMenuStrip;
			//Revision: JCarpenter 10/06
			ContextMenuObjectState objState = GetObjectState();

			if ( _masterPane != null && menuStrip != null )
			{
				menuStrip.Items.Clear();

				_isZooming = false;
				_isPanning = false;
				Cursor.Current = Cursors.Default;

				_menuClickPt = this.PointToClient( Control.MousePosition );
				pane = _masterPane.FindPane( _menuClickPt );

				if ( _isShowContextMenu )
				{
					string menuStr = string.Empty;

				    ToolStripMenuItem item;

                    //if (_isShowRemark)
                    //{
                    //    item = new ToolStripMenuItem();
                    //    item.Name = "remark";
                    //    item.Tag = "remark";
                    //    item.Text = _resourceManager.GetString("remark");
                    //    item.Click += new System.EventHandler(this.MenuClick_AddRemark);
                    //    menuStrip.Items.Add(item);
                    //}

				    item = new ToolStripMenuItem();
					item.Name = "copy";
					item.Tag = "copy";
					item.Text = "Copy";

					if (getLanguage().Equals("zh-CN"))
                    {
                        item.Text = _resourceManager.GetString("copy");
                    }
                    else
                    {
                        item.Text = _resourceManager.GetString("copy", ci);
                    }
					item.Click += new System.EventHandler( this.MenuClick_Copy );
					menuStrip.Items.Add( item );

                    //item = new ToolStripMenuItem();
                    //item.Name = "save_as";
                    //item.Tag = "save_as";
                    //item.Text = _resourceManager.GetString( "save_as" );
                    //item.Click += new System.EventHandler( this.MenuClick_SaveAs );
                    //menuStrip.Items.Add( item );

                    //item = new ToolStripMenuItem();
                    //item.Name = "page_setup";
                    //item.Tag = "page_setup";
                    //item.Text = _resourceManager.GetString( "page_setup" );
                    //item.Click += new System.EventHandler( this.MenuClick_PageSetup );
                    //menuStrip.Items.Add( item );

                    //item = new ToolStripMenuItem();
                    //item.Name = "print";
                    //item.Tag = "print";
                    //item.Text = _resourceManager.GetString( "print" );
                    //item.Click += new System.EventHandler( this.MenuClick_Print );
                    //menuStrip.Items.Add( item );

					item = new ToolStripMenuItem();
					item.Name = "show_val";
					item.Tag = "show_val";
                    if (getLanguage().Equals("zh-CN"))
                    {
                        item.Text = _resourceManager.GetString("show_val");
                    }
                    else
                    {
                        item.Text = _resourceManager.GetString("show_val", ci);
                    }
					item.Click += new System.EventHandler( this.MenuClick_ShowValues );
					item.Checked = this.IsShowPointValues;
					menuStrip.Items.Add( item );

                    //item = new ToolStripMenuItem();
                    //item.Name = "unzoom";
                    //item.Tag = "unzoom";

                    //if ( pane == null || pane.ZoomStack.IsEmpty )
                    //    menuStr = _resourceManager.GetString( "unzoom" );
                    //else
                    //{
                    //    switch ( pane.ZoomStack.Top.Type )
                    //    {
                    //        case ZoomState.StateType.Zoom:
                    //        case ZoomState.StateType.WheelZoom:
                    //            menuStr = _resourceManager.GetString( "unzoom" );
                    //            break;
                    //        case ZoomState.StateType.Pan:
                    //            menuStr = _resourceManager.GetString( "unpan" );
                    //            break;
                    //        case ZoomState.StateType.Scroll:
                    //            menuStr = _resourceManager.GetString( "unscroll" );
                    //            break;
                    //    }
                    //}

                    ////menuItem.Text = "Un-" + ( ( pane == null || pane.zoomStack.IsEmpty ) ?
                    ////	"Zoom" : pane.zoomStack.Top.TypeString );
                    //item.Text = menuStr;
                    //item.Click += new EventHandler( this.MenuClick_ZoomOut );
                    //if ( pane == null || pane.ZoomStack.IsEmpty )
                    //    item.Enabled = false;
                    //menuStrip.Items.Add( item );

                    //item = new ToolStripMenuItem();
                    //item.Name = "undo_all";
                    //item.Tag = "undo_all";
                    //menuStr = _resourceManager.GetString( "undo_all" );
                    //item.Text = menuStr;
                    //item.Click += new EventHandler( this.MenuClick_ZoomOutAll );
                    //if ( pane == null || pane.ZoomStack.IsEmpty )
                    //    item.Enabled = false;
                    //menuStrip.Items.Add( item );

                    //item = new ToolStripMenuItem();
                    //item.Name = "to_move";
                    //item.Tag = "to_move";
                    //menuStr = _resourceManager.GetString("to_move");
                    //item.Text = menuStr;
                    //item.Click += new EventHandler(this.MenuClick_ZoomToMove);
                    //if (pane == null || pane.ZoomStack.IsEmpty)
                    //    item.Enabled = true;
                    //menuStrip.Items.Add(item);

					item = new ToolStripMenuItem();
					item.Name = "set_default";
					item.Tag = "set_default";
                    if (getLanguage().Equals("zh-CN"))
                    {
                        menuStr = _resourceManager.GetString("set_default");
                    }
                    else
                    {
                        menuStr = _resourceManager.GetString("set_default", ci);
                    }
					item.Text = menuStr;
					item.Click += new EventHandler( this.MenuClick_RestoreScale );
					if ( pane == null )
						item.Enabled = false;
					menuStrip.Items.Add( item );

					// if e.Cancel is set to false, the context menu does not display
					// it is initially set to false because the context menu has no items
					e.Cancel = false;

					// Provide Callback for User to edit the context menu
					//Revision: JCarpenter 10/06 - add ContextMenuObjectState objState
					if ( this.ContextMenuBuilder != null )
						this.ContextMenuBuilder( this, menuStrip, _menuClickPt, objState );
				}
			}
		}

        protected void MenuClick_AddRemark(System.Object sender, System.EventArgs e)
        {
            frmRemark frm=new frmRemark();
            frm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            frm.ShowDialog(this);
            if (frm.DialogResult == DialogResult.OK)
            {
                //Point mousePt = this.PointToClient(Control.MousePosition);
                float x = pane._chart._rect.Width/2;
                float y = pane._chart._rect.Height/2;
                float width = frm.text.Length*12;
                float height = 20;
                RectangleF rectangle = new RectangleF(x, y, width, height);
                PointF startPoint = new PointF(x + width/2, y + height + 40);

                Remark remark = new Remark(frm.backColor, frm.lineColor, rectangle, startPoint, frm.text);
                pane.AddRemark(remark);
                Refresh();
            }
        }

		/// <summary>
		/// Handler for the "Copy" context menu item.  Copies the current image to a bitmap on the
		/// clipboard.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void MenuClick_Copy( System.Object sender, System.EventArgs e )
		{
			Copy( _isShowCopyMessage );
		}

		/// <summary>
		/// Handler for the "Copy" context menu item.  Copies the current image to a bitmap on the
		/// clipboard.
		/// </summary>
		/// <param name="isShowMessage">boolean value that determines whether or not a prompt will be
		/// displayed.  true to show a message of "Image Copied to ClipBoard".</param>
		public void Copy( bool isShowMessage )
		{
			if ( _masterPane != null )
			{
				//Clipboard.SetDataObject( _masterPane.GetImage(), true );

				// Threaded copy mode to avoid crash with MTA
				// Contributed by Dave Moor
				Thread ct = new Thread( new ThreadStart( this.ClipboardCopyThread ) );
				//ct.ApartmentState = ApartmentState.STA;
				ct.SetApartmentState( ApartmentState.STA );
				ct.Start();
				ct.Join();

				if ( isShowMessage )
				{
                    string str = null;
                    if (getLanguage().Equals("zh-CN"))
                    {
                        str = _resourceManager.GetString("copied_to_clip");
                    }
                    else
                    {
                        str = _resourceManager.GetString("copied_to_clip", ci);
                    }
					//MessageBox.Show( "Image Copied to ClipBoard" );
					MessageBox.Show( str );
				}
			}
		}

		/// <summary>
		/// A threaded version of the copy method to avoid crash with MTA
		/// </summary>
		private void ClipboardCopyThread()
		{
			Clipboard.SetDataObject( ImageRender(), true );
		}

		// 
		/// <summary>
		/// Setup for creation of a new image, applying appropriate anti-alias properties and
		/// returning the resultant image file
		/// </summary>
		/// <returns></returns>
		private Image ImageRender()
		{
			return _masterPane.GetImage( _masterPane.IsAntiAlias );
		}

		/// <summary>
		/// Special handler that copies the current image to an Emf file on the clipboard.
		/// </summary>
		/// <remarks>This version is similar to the regular <see cref="Copy" /> method, except that
		/// it will place an Emf image (vector) on the ClipBoard instead of the regular bitmap.</remarks>
		/// <param name="isShowMessage">boolean value that determines whether or not a prompt will be
		/// displayed.  true to show a message of "Image Copied to ClipBoard".</param>
		public void CopyEmf(bool isShowMessage)
		{
			if (_masterPane != null)
			{
				// Threaded copy mode to avoid crash with MTA
				// Contributed by Dave Moor
				Thread ct = new Thread(new ThreadStart(this.ClipboardCopyThreadEmf));
				//ct.ApartmentState = ApartmentState.STA;
				ct.SetApartmentState(ApartmentState.STA);
				ct.Start();
				ct.Join();

				if (isShowMessage)
				{
                    string str = null;
                    if (getLanguage().Equals("zh-CN"))
                    {
                        str = _resourceManager.GetString("copied_to_clip");
                    }
                    else
                    {
                        str = _resourceManager.GetString("copied_to_clip", ci);
                    }
					MessageBox.Show(str);
				}
			}
		}

		/// <summary>
		/// A threaded version of the copy method to avoid crash with MTA
		/// </summary>
		private void ClipboardCopyThreadEmf()
		{
			using (Graphics g = this.CreateGraphics())
			{
				IntPtr hdc = g.GetHdc();
				Metafile metaFile = new Metafile(hdc, EmfType.EmfPlusOnly);
				g.ReleaseHdc(hdc);

				using (Graphics gMeta = Graphics.FromImage(metaFile))
				{
					this._masterPane.Draw( gMeta );
				}

				//IntPtr hMeta = metaFile.GetHenhmetafile();
				ClipboardMetafileHelper.PutEnhMetafileOnClipboard( this.Handle, metaFile );
				//System.Windows.Forms.Clipboard.SetDataObject(hMeta, true);

				//g.Dispose();
			}
		}

		/// <summary>
		/// Handler for the "Save Image As" context menu item.  Copies the current image to the selected
		/// file.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void MenuClick_SaveAs( System.Object sender, System.EventArgs e )
		{
			SaveAs();
		}

		/// <summary>
		/// Handler for the "Save Image As" context menu item.  Copies the current image to the selected
		/// file in either the Emf (vector), or a variety of Bitmap formats.
		/// </summary>
		/// <remarks>
		/// Note that <see cref="SaveAsBitmap" /> and <see cref="SaveAsEmf" /> methods are provided
		/// which allow for Bitmap-only or Emf-only handling of the "Save As" context menu item.
		/// </remarks>
		public void SaveAs()
		{
			SaveAs( null );
		}

		/// <summary>
		/// Copies the current image to the selected file in  
		/// Emf (vector), or a variety of Bitmap formats.
		/// </summary>
		/// <param name="DefaultFileName">
		/// Accepts a default file name for the file dialog (if "" or null, default is not used)
		/// </param>
		/// <returns>
		/// The file name saved, or "" if cancelled.
		/// </returns>
		/// <remarks>
		/// Note that <see cref="SaveAsBitmap" /> and <see cref="SaveAsEmf" /> methods are provided
		/// which allow for Bitmap-only or Emf-only handling of the "Save As" context menu item.
		/// </remarks>
		public String SaveAs( String DefaultFileName )
		{
            _saveFileDialog.FileName = DefaultFileName;
            Stream myStream = _saveFileDialog.OpenFile();
            if (myStream != null)
            {
                ImageFormat format = ImageFormat.Png;
                ImageRender().Save(myStream, format);
                myStream.Close();
            }
            return DefaultFileName;
		}

		/// <summary>
		/// Handler for the "Save Image As" context menu item.  Copies the current image to the selected
		/// Bitmap file.
		/// </summary>
		/// <remarks>
		/// Note that this handler saves as a bitmap only.  The default handler is
		/// <see cref="SaveAs()" />, which allows for Bitmap or EMF formats
		/// </remarks>
		public void SaveAsBitmap()
		{
			if ( _masterPane != null )
			{
				_saveFileDialog.Filter =
					"PNG Format (*.png)|*.png|" +
					"Gif Format (*.gif)|*.gif|" +
					"Jpeg Format (*.jpg)|*.jpg|" +
					"Tiff Format (*.tif)|*.tif|" +
					"Bmp Format (*.bmp)|*.bmp";

				if ( _saveFileDialog.ShowDialog() == DialogResult.OK )
				{
					ImageFormat format = ImageFormat.Png;
					if ( _saveFileDialog.FilterIndex == 2 )
						format = ImageFormat.Gif;
					else if ( _saveFileDialog.FilterIndex == 3 )
						format = ImageFormat.Jpeg;
					else if ( _saveFileDialog.FilterIndex == 4 )
						format = ImageFormat.Tiff;
					else if ( _saveFileDialog.FilterIndex == 5 )
						format = ImageFormat.Bmp;

					Stream myStream = _saveFileDialog.OpenFile();
					if ( myStream != null )
					{
						//_masterPane.GetImage().Save( myStream, format );
						ImageRender().Save( myStream, format );
						myStream.Close();
					}
				}
			}
		}

		/// <summary>
		/// Handler for the "Save Image As" context menu item.  Copies the current image to the selected
		/// Emf format file.
		/// </summary>
		/// <remarks>
		/// Note that this handler saves as an Emf format only.  The default handler is
		/// <see cref="SaveAs()" />, which allows for Bitmap or EMF formats.
		/// </remarks>
		public void SaveAsEmf()
		{
			if ( _masterPane != null )
			{
				_saveFileDialog.Filter = "Emf Format (*.emf)|*.emf";

				if ( _saveFileDialog.ShowDialog() == DialogResult.OK )
				{
					Stream myStream = _saveFileDialog.OpenFile();
					if ( myStream != null )
					{
						myStream.Close();
						//_masterPane.GetMetafile().Save( _saveFileDialog.FileName );
						SaveEmfFile(_saveFileDialog.FileName);
					}
				}
			}
		}

		/// <summary>
		/// Save the current Graph to the specified filename in EMF (vector) format.
		/// See <see cref="SaveAsEmf()" /> for public access.
		/// </summary>
		/// <remarks>
		/// Note that this handler saves as an Emf format only.  The default handler is
		/// <see cref="SaveAs()" />, which allows for Bitmap or EMF formats.
		/// </remarks>
		internal void SaveEmfFile( string fileName )
		{
			using (Graphics g = this.CreateGraphics())
			{
				IntPtr hdc = g.GetHdc();
				Metafile metaFile = new Metafile(hdc, EmfType.EmfPlusOnly);
				using (Graphics gMeta = Graphics.FromImage(metaFile))
				{
					//PaneBase.SetAntiAliasMode( gMeta, IsAntiAlias );
					//gMeta.CompositingMode = CompositingMode.SourceCopy; 
					//gMeta.CompositingQuality = CompositingQuality.HighQuality;
					//gMeta.InterpolationMode = InterpolationMode.HighQualityBicubic;
					//gMeta.SmoothingMode = SmoothingMode.AntiAlias;
					//gMeta.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
					this._masterPane.Draw(gMeta);
					//gMeta.Dispose();
				}

				ClipboardMetafileHelper.SaveEnhMetafileToFile(metaFile, fileName );

				g.ReleaseHdc(hdc);
				//g.Dispose();
			}

		}

		internal class ClipboardMetafileHelper
		{
			[DllImport("user32.dll")]
			static extern bool OpenClipboard(IntPtr hWndNewOwner);
			[DllImport("user32.dll")]
			static extern bool EmptyClipboard();
			[DllImport("user32.dll")]
			static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
			[DllImport("user32.dll")]
			static extern bool CloseClipboard();
			[DllImport("gdi32.dll")]
			static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, System.Text.StringBuilder hNULL);
			[DllImport("gdi32.dll")]
			static extern bool DeleteEnhMetaFile(IntPtr hemf);

			static internal bool SaveEnhMetafileToFile( Metafile mf, string fileName )
			{
				bool bResult = false;
				IntPtr hEMF;
				hEMF = mf.GetHenhmetafile(); // invalidates mf 
				if (!hEMF.Equals(new IntPtr(0)))
				{
					StringBuilder tempName = new StringBuilder(fileName);
					CopyEnhMetaFile(hEMF, tempName);
					DeleteEnhMetaFile(hEMF);
				}
				return bResult;
			}

			static internal bool SaveEnhMetafileToFile(Metafile mf)
			{
				bool bResult = false;
				IntPtr hEMF;
				hEMF = mf.GetHenhmetafile(); // invalidates mf 
				if (!hEMF.Equals(new IntPtr(0)))
				{
					SaveFileDialog sfd = new SaveFileDialog();
					sfd.Filter = "Extended Metafile (*.emf)|*.emf";
					sfd.DefaultExt = ".emf";
					if (sfd.ShowDialog() == DialogResult.OK)
					{
						StringBuilder temp = new StringBuilder(sfd.FileName);
						CopyEnhMetaFile(hEMF, temp);
					}
					DeleteEnhMetaFile(hEMF);
				}
				return bResult;
			}

			// Metafile mf is set to a state that is not valid inside this function. 
			static internal bool PutEnhMetafileOnClipboard(IntPtr hWnd, Metafile mf)
			{
				bool bResult = false;
				IntPtr hEMF, hEMF2;
				hEMF = mf.GetHenhmetafile(); // invalidates mf 
				if (!hEMF.Equals(new IntPtr(0)))
				{
					hEMF2 = CopyEnhMetaFile(hEMF, null);
					if (!hEMF2.Equals(new IntPtr(0)))
					{
						if (OpenClipboard(hWnd))
						{
							if (EmptyClipboard())
							{
								IntPtr hRes = SetClipboardData(14 /*CF_ENHMETAFILE*/, hEMF2);
								bResult = hRes.Equals(hEMF2);
								CloseClipboard();
							}
						}
					}
					DeleteEnhMetaFile(hEMF);
				}
				return bResult;
			}
		}

		/// <summary>
		/// Handler for the "Show Values" context menu item.  Toggles the <see cref="IsShowPointValues"/>
		/// property, which activates the point value tooltips.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void MenuClick_ShowValues( object sender, System.EventArgs e )
		{
			ToolStripMenuItem item = sender as ToolStripMenuItem;
			if ( item != null )
				this.IsShowPointValues = !item.Checked;
		}

		/// <summary>
		/// Handler for the "Set Scale to Default" context menu item.  Sets the scale ranging to
		/// full auto mode for all axes.
		/// </summary>
		/// <remarks>
		/// This method differs from the <see cref="ZoomOutAll" /> method in that it sets the scales
		/// to full auto mode.  The <see cref="ZoomOutAll" /> method sets the scales to their initial
		/// setting prior to any user actions (which may or may not be full auto mode).
		/// </remarks>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void MenuClick_RestoreScale( object sender, EventArgs e )
		{
			if ( _masterPane != null )
			{
				GraphPane pane = _masterPane.FindPane( _menuClickPt );
				RestoreScale( pane );
			}
		}

		/// <summary>
		/// Handler for the "Set Scale to Default" context menu item.  Sets the scale ranging to
		/// full auto mode for all axes.
		/// </summary>
		/// <remarks>
		/// This method differs from the <see cref="ZoomOutAll" /> method in that it sets the scales
		/// to full auto mode.  The <see cref="ZoomOutAll" /> method sets the scales to their initial
		/// setting prior to any user actions (which may or may not be full auto mode).
		/// </remarks>
		/// <param name="primaryPane">The <see cref="GraphPane" /> object which is to have the
		/// scale restored</param>
		public void RestoreScale( GraphPane primaryPane )
		{
			if ( primaryPane != null )
			{
                GraphPane.IsFirstMove = true;

                GraphPane.RemarkList=new System.Collections.Generic.List<Remark>();
                foreach (Remark remark in GraphPane.RemarkOldList)
                {
                    GraphPane.RemarkList.Add(remark);
                }
				//Go ahead and save the old zoomstates, which provides an "undo"-like capability
				//ZoomState oldState = primaryPane.ZoomStack.Push( primaryPane, ZoomState.StateType.Zoom );
				ZoomState oldState = new ZoomState( primaryPane, ZoomState.StateType.Zoom );

				using ( Graphics g = this.CreateGraphics() )
				{
					if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
					{
						foreach ( GraphPane pane in _masterPane._paneList )
						{
							pane.ZoomStack.Push( pane, ZoomState.StateType.Zoom );
							ResetAutoScale( pane, g );
						}
					}
					else
					{
						primaryPane.ZoomStack.Push( primaryPane, ZoomState.StateType.Zoom );
						ResetAutoScale( primaryPane, g );
					}

					// Provide Callback to notify the user of zoom events
					if ( this.ZoomEvent != null )
						this.ZoomEvent( this, oldState, new ZoomState( primaryPane, ZoomState.StateType.Zoom ) );

					//g.Dispose();
				}
				Refresh();
			}
		}

		private void ResetAutoScale( GraphPane pane, Graphics g )
		{
			pane.XAxis.ResetAutoScale( pane, g );
			pane.X2Axis.ResetAutoScale( pane, g );
			foreach ( YAxis axis in pane.YAxisList )
				axis.ResetAutoScale( pane, g );
			foreach ( Y2Axis axis in pane.Y2AxisList )
				axis.ResetAutoScale( pane, g );
		}

		/*
				public void RestoreScale( GraphPane primaryPane )
				{
					if ( primaryPane != null )
					{
						Graphics g = this.CreateGraphics();
						ZoomState oldState = new ZoomState( primaryPane, ZoomState.StateType.Zoom );
						//ZoomState newState = null;

						if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
						{
							foreach ( GraphPane pane in _masterPane._paneList )
							{
								if ( pane == primaryPane )
								{
									pane.XAxis.ResetAutoScale( pane, g );
									foreach ( YAxis axis in pane.YAxisList )
										axis.ResetAutoScale( pane, g );
									foreach ( Y2Axis axis in pane.Y2AxisList )
										axis.ResetAutoScale( pane, g );
								}
							}
						}
						else
						{
							primaryPane.XAxis.ResetAutoScale( primaryPane, g );
							foreach ( YAxis axis in primaryPane.YAxisList )
								axis.ResetAutoScale( primaryPane, g );
							foreach ( Y2Axis axis in primaryPane.Y2AxisList )
								axis.ResetAutoScale( primaryPane, g );
						}

						// Provide Callback to notify the user of zoom events
						if ( this.ZoomEvent != null )
							this.ZoomEvent( this, oldState, new ZoomState( primaryPane, ZoomState.StateType.Zoom ) );

						g.Dispose();
						Refresh();
					}
				}
		*/
		/*
				public void ZoomOutAll( GraphPane primaryPane )
				{
					if ( primaryPane != null && !primaryPane.ZoomStack.IsEmpty )
					{
						ZoomState.StateType type = primaryPane.ZoomStack.Top.Type;

						ZoomState oldState = new ZoomState( primaryPane, type );
						//ZoomState newState = pane.ZoomStack.PopAll( pane );
						ZoomState newState = null;
						if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
						{
							foreach ( GraphPane pane in _masterPane._paneList )
							{
								ZoomState state = pane.ZoomStack.PopAll( pane );
								if ( pane == primaryPane )
									newState = state;
							}
						}
						else
							newState = primaryPane.ZoomStack.PopAll( primaryPane );

						// Provide Callback to notify the user of zoom events
						if ( this.ZoomEvent != null )
							this.ZoomEvent( this, oldState, newState );

						Refresh();
					}
				}

		*/

		/// <summary>
		/// Handler for the "UnZoom/UnPan" context menu item.  Restores the scale ranges to the values
		/// before the last zoom or pan operation.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void MenuClick_ZoomOut( System.Object sender, System.EventArgs e )
		{
			if ( _masterPane != null )
			{
				GraphPane pane = _masterPane.FindPane( _menuClickPt );
				ZoomOut( pane );
			}
		}

		/// <summary>
		/// Handler for the "UnZoom/UnPan" context menu item.  Restores the scale ranges to the values
		/// before the last zoom, pan, or scroll operation.
		/// </summary>
		/// <remarks>
		/// Triggers a <see cref="ZoomEvent" /> for any type of undo (including pan, scroll, zoom, and
		/// wheelzoom).  This method will affect all the
		/// <see cref="GraphPane" /> objects in the <see cref="MasterPane" /> if
		/// <see cref="IsSynchronizeXAxes" /> or <see cref="IsSynchronizeYAxes" /> is true.
		/// </remarks>
		/// <param name="primaryPane">The primary <see cref="GraphPane" /> object which is to be
		/// zoomed out</param>
		public void ZoomOut( GraphPane primaryPane )
		{
			if ( primaryPane != null && !primaryPane.ZoomStack.IsEmpty )
			{
				ZoomState.StateType type = primaryPane.ZoomStack.Top.Type;

				ZoomState oldState = new ZoomState( primaryPane, type );
				ZoomState newState = null;
				if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
				{
					foreach ( GraphPane pane in _masterPane._paneList )
					{
						ZoomState state = pane.ZoomStack.Pop( pane );
						if ( pane == primaryPane )
							newState = state;
					}
				}
				else
					newState = primaryPane.ZoomStack.Pop( primaryPane );

				// Provide Callback to notify the user of zoom events
				if ( this.ZoomEvent != null )
					this.ZoomEvent( this, oldState, newState );

				Refresh();
			}
		}

		/// <summary>
		/// Handler for the "Undo All Zoom/Pan" context menu item.  Restores the scale ranges to the values
		/// before all zoom and pan operations
		/// </summary>
		/// <remarks>
		/// This method differs from the <see cref="RestoreScale" /> method in that it sets the scales
		/// to their initial setting prior to any user actions.  The <see cref="RestoreScale" /> method
		/// sets the scales to full auto mode (regardless of what the initial setting may have been).
		/// </remarks>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void MenuClick_ZoomOutAll( System.Object sender, System.EventArgs e )
		{
			if ( _masterPane != null )
			{
				GraphPane pane = _masterPane.FindPane( _menuClickPt );
				//ZoomOutAll( pane );
                //_isRevert = true;
			}
		}

        protected void MenuClick_ZoomToMove( System.Object sender, System.EventArgs e )
        {
            if (_masterPane != null)
            {
                this.Cursor = Cursors.Hand;
            }
        }
		/// <summary>
		/// Handler for the "Undo All Zoom/Pan" context menu item.  Restores the scale ranges to the values
		/// before all zoom and pan operations
		/// </summary>
		/// <remarks>
		/// This method differs from the <see cref="RestoreScale" /> method in that it sets the scales
		/// to their initial setting prior to any user actions.  The <see cref="RestoreScale" /> method
		/// sets the scales to full auto mode (regardless of what the initial setting may have been).
		/// </remarks>
		/// <param name="primaryPane">The <see cref="GraphPane" /> object which is to be zoomed out</param>
		public void ZoomOutAll( GraphPane primaryPane )
		{
			if ( primaryPane != null && !primaryPane.ZoomStack.IsEmpty )
			{
				ZoomState.StateType type = primaryPane.ZoomStack.Top.Type;

				ZoomState oldState = new ZoomState( primaryPane, type );
				//ZoomState newState = pane.ZoomStack.PopAll( pane );
				ZoomState newState = null;
				if ( _isSynchronizeXAxes || _isSynchronizeYAxes )
				{
					foreach ( GraphPane pane in _masterPane._paneList )
					{
						ZoomState state = pane.ZoomStack.PopAll( pane );
						if ( pane == primaryPane )
							newState = state;
					}
				}
				else
					newState = primaryPane.ZoomStack.PopAll( primaryPane );

				// Provide Callback to notify the user of zoom events
				if ( this.ZoomEvent != null )
					this.ZoomEvent( this, oldState, newState );

				Refresh();
			}
		}

	#endregion
	}
}