ToolsControl.cs :  » GUI » Paint.net » PaintDotNet » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » Paint.net 
Paint.net » PaintDotNet » ToolsControl.cs
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors.     //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using PaintDotNet.SystemLayer;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace PaintDotNet{
    internal class ToolsControl 
        : UserControl, 
          IToolChooser
    {
        private ToolStripEx toolStripEx;
        private ImageList imageList;
        private const int tbWidth = 2; // two buttons per line in the toolbars
        private int ignoreToolClicked = 0;
        private Control onePxSpacingLeft;

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ToolsControl()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();
        }

        public event ToolClickedEventHandler ToolClicked;
        protected virtual void OnToolClicked(Type toolType)
        {
            if (this.ignoreToolClicked <= 0)
            {
                if (ToolClicked != null)
                {
                    ToolClicked(this, new ToolClickedEventArgs(toolType));
                }
            }
        }

        public void SetTools(ToolInfo[] toolInfos)
        {
            if (this.toolStripEx != null)
            {
                this.toolStripEx.Items.Clear();
            }

            this.imageList = new ImageList();
            this.imageList.ColorDepth = ColorDepth.Depth32Bit;
            this.imageList.TransparentColor = Utility.TransparentKey;

            this.toolStripEx.ImageList = this.imageList;

            ToolStripItem[] buttons = new ToolStripItem[toolInfos.Length];
            string toolTipFormat = PdnResources.GetString("ToolsControl.ToolToolTip.Format");

            for (int i = 0; i < toolInfos.Length; ++i)
            {
                ToolInfo toolInfo = toolInfos[i];
                ToolStripButton button = new ToolStripButton();

                int imageIndex = imageList.Images.Add(
                    toolInfo.Image.Reference,
                    imageList.TransparentColor);

                button.ImageIndex = imageIndex;
                button.Tag = toolInfo.ToolType;
                button.ToolTipText = string.Format(toolTipFormat, toolInfo.Name, char.ToUpperInvariant(toolInfo.HotKey).ToString());
                buttons[i] = button;
            }

            this.toolStripEx.Items.AddRange(buttons);
        }

        public void SelectTool(Type toolType)
        {
            SelectTool(toolType, true);
        }

        public void SelectTool(Type toolType, bool raiseEvent)
        {
            if (!raiseEvent)
            {
                ++this.ignoreToolClicked;
            }

            try
            {
                foreach (ToolStripButton button in this.toolStripEx.Items)
                {
                    if ((Type)button.Tag == toolType)
                    {
                        this.ToolStripEx_ItemClicked(this, new ToolStripItemClickedEventArgs(button));
                        return;
                    }
                }

                throw new ArgumentException("Tool type not found");
            }

            finally
            {
                if (!raiseEvent)
                {
                    --this.ignoreToolClicked;
                }
            }
        }

        protected override void OnLayout(LayoutEventArgs e)
        {
            int buttonWidth;

            if (this.toolStripEx.Items.Count > 0)
            {
                buttonWidth = this.toolStripEx.Items[0].Width;
            }
            else
            {
                buttonWidth = 0;
            }

            this.toolStripEx.Width =
                this.toolStripEx.Padding.Left +
                (buttonWidth * tbWidth) +
                (this.toolStripEx.Margin.Horizontal * tbWidth) +
                this.toolStripEx.Padding.Right;

            this.toolStripEx.Height = this.toolStripEx.GetPreferredSize(this.toolStripEx.Size).Height;

            this.Width = this.toolStripEx.Width + this.onePxSpacingLeft.Width;
            this.Height = this.toolStripEx.Height;

            base.OnLayout(e);
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                    components = null;
                }
            }

            base.Dispose(disposing);
        }

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.toolStripEx = new ToolStripEx();
            this.onePxSpacingLeft = new Control();
            this.SuspendLayout();
            //
            // toolStripEx
            //
            this.toolStripEx.Dock = System.Windows.Forms.DockStyle.Top;
            this.toolStripEx.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.toolStripEx.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;
            this.toolStripEx.ItemClicked += new ToolStripItemClickedEventHandler(ToolStripEx_ItemClicked);
            this.toolStripEx.Name = "toolStripEx";
            this.toolStripEx.AutoSize = true;
            this.toolStripEx.RelinquishFocus += new EventHandler(ToolStripEx_RelinquishFocus);
            //
            // onePxSpacingLeft
            //
            this.onePxSpacingLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.onePxSpacingLeft.Width = 1;
            this.onePxSpacingLeft.Name = "onePxSpacingLeft";
            // 
            // MainToolBar
            // 
            this.Controls.Add(this.toolStripEx);
            this.Controls.Add(this.onePxSpacingLeft);
            this.AutoScaleDimensions = new SizeF(96F, 96F);
            this.AutoScaleMode = AutoScaleMode.Dpi;
            this.Name = "MainToolBar";
            this.Size = new System.Drawing.Size(48, 328);
            this.ResumeLayout(false);
        }

        public event EventHandler RelinquishFocus;
        private void OnRelinquishFocus()
        {
            if (RelinquishFocus != null)
            {
                RelinquishFocus(this, EventArgs.Empty);
            }
        }

        private void ToolStripEx_RelinquishFocus(object sender, EventArgs e)
        {
            OnRelinquishFocus();
        }

        private void ToolStripEx_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            foreach (ToolStripButton button in this.toolStripEx.Items)
            {
                button.Checked = (button == e.ClickedItem);
            }

            OnToolClicked((Type)e.ClickedItem.Tag);
        }
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.