source: branches/eraser6/Eraser/ToolBar.cs @ 903

Revision 903, 6.9 KB checked in by lowjoel, 5 years ago (diff)

Ran Static code analysis on the Eraser project and implemented a few recommendations.

-Use more specific exceptions to allow code to determine the type of exception.
-Made GUIProgram implement IDisposable since the global mutex must be freed.
-Mde AboutForm? implement IDisposable, clearing the caching bitmaps on dispose.
-Made a few CommandLineProgram? functions static since they don't reference this.
-Name parameters/local with more unique, descriptive names for clarity.
-Use EventHandler?<EventArg?> instead of declaring our own delegate and event types as the type of the evenr handler

  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29using System.Runtime.InteropServices;
30using System.Collections.ObjectModel;
31
32namespace Eraser
33{
34    public partial class ToolBar : Control
35    {
36        public ToolBar()
37        {
38            //Create the base component
39            InitializeComponent();
40
41            //Initialize the toolbar item list
42            items = new List<ToolBarItem>();
43
44            //Hook mouse move events to show the currently selected item
45            MouseMove += new MouseEventHandler(ToolBar_MouseMove);
46            MouseLeave += new EventHandler(ToolBar_MouseLeave);
47            MouseClick += new MouseEventHandler(ToolBar_MouseClick);
48        }
49
50        void ToolBar_MouseMove(object sender, MouseEventArgs e)
51        {
52            Redraw();
53        }
54
55        void ToolBar_MouseLeave(object sender, EventArgs e)
56        {
57            Redraw();
58        }
59
60        void ToolBar_MouseClick(object sender, MouseEventArgs e)
61        {
62            //See if the click was on any item's arrow.
63            Rectangle mouse_rect = new Rectangle(e.Location, new Size(1, 1));
64            foreach (ToolBarItem i in items)
65            {
66                if (i.Menu != null && mouse_rect.IntersectsWith(i.MenuRect))
67                {
68                    //Show the menu below the toolbar item.
69                    Point mouse_point = PointToScreen(i.Rectangle.Location);
70                    i.Menu.Show(mouse_point.X, mouse_point.Y + i.Rectangle.Height);
71                }
72                else if (mouse_rect.IntersectsWith(i.Rectangle))
73                {
74                    //Broadcast the item click event
75                    i.OnToolbarItemClicked(this);
76                }
77            }
78        }
79       
80        /// <summary>
81        /// Draws the Tool Bar on the given graphics object.
82        /// </summary>
83        /// <param name="dc">Graphics object to draw on.</param>
84        public void Draw(Graphics rawDC)
85        {
86            //Create a backing bitmap buffer to prevent flicker
87            Bitmap back_bmp = new Bitmap(Width, Height);
88            Graphics dc = Graphics.FromImage(back_bmp);
89
90            //Draw the parent background image. This is not portable in that it will render
91            //this code unreusable, but for the lack of anything better this will have to suffice!
92            dc.DrawImage(Properties.Resources.BackgroundGradient, new Point(-Left, -Top));
93           
94            Point mouse_pos = PointToClient(MousePosition);
95            int x = 0;
96
97            foreach (ToolBarItem i in items)
98            {
99                {
100                    Point pos = i.Rectangle.Location;
101                    pos.X = x;
102                    pos.Y = 0;
103                    i.Rectangle.Location = pos;
104                }
105
106                if (i.Bitmap != null)
107                {
108                    i.BitmapRect = new Rectangle(x, 0, i.Bitmap.Width, i.Bitmap.Height);
109                    dc.DrawImage(i.Bitmap, i.BitmapRect);
110
111                    x += i.BitmapRect.Width + 4;
112                }
113
114                //Draw the toolbar item text
115                SizeF string_size = dc.MeasureString(i.Text, Font);
116                i.TextRect = new Rectangle(x, (int)(Height - string_size.Height) / 2,
117                    (int)string_size.Width, (int)string_size.Height);
118                dc.DrawString(i.Text, Font, Brushes.White, i.TextRect.Location);
119                x += i.TextRect.Width;
120               
121                //If there is a menu associated draw a drop-down glyph
122                if (i.Menu != null)
123                {
124                    Bitmap menu_arrow = Properties.Resources.ToolbarArrow;
125                    i.MenuRect = new Rectangle(x += 6, i.TextRect.Y,
126                        menu_arrow.Width, menu_arrow.Height);
127                    dc.DrawImage(menu_arrow, i.MenuRect);
128                    x += i.MenuRect.Width;
129                }
130
131                //Update the size of the item rectangle
132                {
133                    Size size = i.Rectangle.Size;
134                    size.Width = x - i.Rectangle.Location.X;
135                    size.Height = Height;
136                    i.Rectangle.Size = size;
137                }
138
139                //If the mouse cursor intersects with the item then draw an underline.
140                if (i.Rectangle.IntersectsWith(new Rectangle(mouse_pos, new Size(1, 1))))
141                    dc.DrawLine(Pens.White, new Point(i.TextRect.Left, i.TextRect.Bottom + 1),
142                        new Point(i.TextRect.Right, i.TextRect.Bottom + 1));
143
144                //Padding between items.
145                x += 16;
146            }
147
148            rawDC.DrawImage(back_bmp, new Point(0, 0));
149        }
150
151        /// <summary>
152        /// Redraws the Tool Bar by creating a Graphics object.
153        /// </summary>
154        public void Redraw()
155        {
156            Draw(CreateGraphics());
157        }
158
159        /// <summary>
160        /// Paints the control.
161        /// </summary>
162        /// <param name="pe">Paint event object.</param>
163        protected override void OnPaint(PaintEventArgs e)
164        {
165            Draw(e.Graphics);
166
167            // Calling the base class OnPaint
168            base.OnPaint(e);
169        }
170
171        /// <summary>
172        /// Stores the items in the Tool Bar.
173        /// </summary>
174        private List<ToolBarItem> items;
175
176        /// <summary>
177        /// Accesses or modifies the items in the tool bar.
178        /// </summary>
179        /// <param name="index">Index of item.</param>
180        /// <returns>ToolBarItem describing the item at index i.</returns>
181        public ToolBarItem this[int index]
182        {
183            get
184            {
185                return items[index];
186            }
187
188            set
189            {
190                items[index] = value;
191                Redraw();
192            }
193        }
194
195        public ICollection<ToolBarItem> Items
196        {
197            get { return items; }
198        }
199    }
200
201    public class ToolBarItem
202    {
203        /// <summary>
204        /// Tool bar item text.
205        /// </summary>
206        [Description("Tool bar item text")]
207        public string Text
208        {
209            get { return text; }
210            set { text = value; }
211        }
212
213        /// <summary>
214        /// Item bitmap.
215        /// </summary>
216        [Description("Item Bitmap")]
217        public Bitmap Bitmap
218        {
219            get { return bitmap; }
220            set { bitmap = value; }
221        }
222
223        /// <summary>
224        /// Item drop-down menu
225        /// </summary>
226        public ContextMenuStrip Menu
227        {
228            get { return menu; }
229            set { menu = value; }
230        }
231
232        /// <summary>
233        /// Item click event handler
234        /// </summary>
235        public EventHandler<EventArgs> ToolbarItemClicked
236        {
237            get;
238            set;
239        }
240
241        internal void OnToolbarItemClicked(object sender)
242        {
243            if (ToolbarItemClicked != null)
244                ToolbarItemClicked(sender, new EventArgs());
245        }
246
247        private string text;
248        private Bitmap bitmap;
249        private ContextMenuStrip menu;
250
251        /// <summary>
252        /// Stores the rectangle of this item.
253        /// </summary>
254        internal Rectangle Rectangle = new Rectangle();
255
256        /// <summary>
257        /// Stores the rectangle of the bitmap.
258        /// </summary>
259        internal Rectangle BitmapRect;
260
261        /// <summary>
262        /// Stores the rectangle of the text.
263        /// </summary>
264        internal Rectangle TextRect;
265
266        /// <summary>
267        /// Stores the rectangle of the drop-down arrow.
268        /// </summary>
269        internal Rectangle MenuRect;
270    }
271}
Note: See TracBrowser for help on using the repository browser.