Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MdiToolBar.cs « Gui « src « doctools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3bdc16e8b781570f0cb4e4c0ed0f53c8ce3d1070 (plain)
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
// MdiToolBar.cs
// John Barnette (jbarn@httcb.net)
// 
// Copyright (c) 2002 John Barnette
//
// This file is part of Monodoc, a multilingual API documentation tool.
//
// Monodoc is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// Monodoc 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 General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Monodoc; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Mono.Doc.Gui
{
	public class MdiToolBar : ToolBar
	{
		#region Private Instance Fields

		private Hashtable mdiChildren     = null;
		private Form      currentMdiChild = null;

		#endregion // Private Instance Fields

		#region Constructors and Destructors

		public MdiToolBar() : base()
		{
			this.mdiChildren = new Hashtable();
		}

		#endregion // Constructors and Destructors

		#region Event Handlers

		private void mdiParent_MdiChildActivate(object sender, EventArgs args)
		{
			Form activeMdiChild = ((Form) sender).ActiveMdiChild;

			if (activeMdiChild != null)
			{
				if (this.mdiChildren[activeMdiChild] == null) 
				{
					// need a new button
					ToolBarButton newButton           = new ToolBarButton(activeMdiChild.Text);
					newButton.Style                   = ToolBarButtonStyle.ToggleButton;
					this.mdiChildren[activeMdiChild]  = newButton;
					activeMdiChild.Closing           += new CancelEventHandler(this.mdiChild_Closing);

					// tooltip
					newButton.ToolTipText = activeMdiChild.Text;

					// separators
					if ((this.Appearance == ToolBarAppearance.Flat) && (this.Buttons.Count > 0)) 
					{
						ToolBarButton sep = new ToolBarButton();
						sep.Style         = ToolBarButtonStyle.Separator;
						
						this.Buttons.Add(sep);
					}

					// image index.  if the mdi child form's Tag property is a string
					// and contains the text "mditoolbarimageindex[n]", where n is
					// an integer, the associated button's ImageIndex property will be
					// set to n.

					String formTag = activeMdiChild.Tag as string;

					if (formTag != null)
					{
						string lookFor = "mditoolbarimageindex[";
						int    index   = formTag.IndexOf(lookFor);

						if (index != -1)
						{
							int    startIndex    = index + lookFor.Length;
							int    endIndex      = formTag.IndexOf("]", startIndex);
							string imageIndexStr = formTag.Substring(startIndex, endIndex - startIndex);

							MessageBox.Show("mditoolbar: imageIndexStr: " + imageIndexStr);
						
							try
							{
								newButton.ImageIndex = int.Parse(imageIndexStr);
							}
							catch
							{
							}
						}
					}
					else
					{
						// use default index
						newButton.ImageIndex = 0;
					}

					this.Buttons.Add(newButton);
				}

				this.CurrentMdiChild = activeMdiChild;
			}
			else
			{
				// last MDI child removed; clean up
				this.mdiChildren.Clear();
				this.Buttons.Clear();
				this.currentMdiChild = null;
			}
		}

		private void mdiChild_Closing(object sender, CancelEventArgs args)
		{
			ToolBarButton b = (ToolBarButton) this.mdiChildren[sender];

			// deal with separators
			if ((this.Appearance == ToolBarAppearance.Flat) && (this.Buttons.Count > 1))
			{
				int bIndex = this.Buttons.IndexOf(b);

				if (bIndex == 0) 
				{
					this.Buttons.RemoveAt(bIndex + 1);
				} 
				else 
				{
					this.Buttons.RemoveAt(bIndex - 1);
				}
			}

			this.Buttons.Remove(b);
			this.mdiChildren.Remove(sender);
		}

		#endregion // Event Handlers

		#region Overridden Event Handlers

		protected override void OnButtonClick(ToolBarButtonClickEventArgs args)
		{
			base.OnButtonClick(args);

			args.Button.Pushed = true;

			// linear search, but double-hashing seems worthless.
			foreach (Form keyChild in this.mdiChildren.Keys) 
			{
				if (args.Button == this.mdiChildren[keyChild])
				{
					keyChild.Activate();
					break;
				}
			}
		}

		protected override void OnParentChanged(EventArgs args)
		{
			base.OnParentChanged(args);

			// TODO: potential for multiple registration of event handler?
			if (this.Parent is Form && ((Form) this.Parent).IsMdiContainer) 
			{
				((Form) this.Parent).MdiChildActivate
					+= new EventHandler(this.mdiParent_MdiChildActivate);
			}
		}

		#endregion // Overridden Event Handlers

		#region Private Instance Properties

		private Form CurrentMdiChild 
		{
			get { return this.currentMdiChild; }
			set 
			{
				if (this.currentMdiChild != null) 
				{
					ToolBarButton child = this.mdiChildren[this.currentMdiChild] as ToolBarButton;

					if (child != null)
					{
						child.Pushed = false;
					}
				}

				((ToolBarButton) this.mdiChildren[value]).Pushed = true;
				this.currentMdiChild = value;
			}
		}

		#endregion // Private Instance Properties
	}
}