/[RomCheater]/trunk/deps/WeifenLuo.WinFormsUI.Docking/DockSample/Customization/VS2003AutoHideStrip.cs
ViewVC logotype

Contents of /trunk/deps/WeifenLuo.WinFormsUI.Docking/DockSample/Customization/VS2003AutoHideStrip.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 140 - (show annotations) (download)
Fri May 11 11:29:29 2012 UTC (9 years, 2 months ago) by william
File size: 16719 byte(s)


1 using System;
2 using System.Drawing;
3 using System.Windows.Forms;
4 using System.Drawing.Drawing2D;
5 using System.ComponentModel;
6 using WeifenLuo.WinFormsUI.Docking;
7
8 namespace DockSample.Customization
9 {
10 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/ClassDef/*'/>
11 internal class VS2003AutoHideStrip : AutoHideStripBase
12 {
13 private class TabVS2003 : Tab
14 {
15 internal TabVS2003(IDockContent content)
16 : base(content)
17 {
18 }
19
20 private int m_tabX = 0;
21 protected internal int TabX
22 {
23 get { return m_tabX; }
24 set { m_tabX = value; }
25 }
26
27 private int m_tabWidth = 0;
28 protected internal int TabWidth
29 {
30 get { return m_tabWidth; }
31 set { m_tabWidth = value; }
32 }
33
34 }
35
36 private const int _ImageHeight = 16;
37 private const int _ImageWidth = 16;
38 private const int _ImageGapTop = 2;
39 private const int _ImageGapLeft = 4;
40 private const int _ImageGapRight = 4;
41 private const int _ImageGapBottom = 2;
42 private const int _TextGapLeft = 4;
43 private const int _TextGapRight = 10;
44 private const int _TabGapTop = 3;
45 private const int _TabGapLeft = 2;
46 private const int _TabGapBetween = 10;
47
48 private static Matrix _matrixIdentity;
49 private static DockState[] _dockStates;
50
51 #region Customizable Properties
52 private static StringFormat _stringFormatTabHorizontal = null;
53 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="StringFormatTabHorizontal"]/*'/>
54 protected virtual StringFormat StringFormatTabHorizontal
55 {
56 get
57 {
58 if (_stringFormatTabHorizontal == null)
59 {
60 _stringFormatTabHorizontal = new StringFormat();
61 _stringFormatTabHorizontal.Alignment = StringAlignment.Near;
62 _stringFormatTabHorizontal.LineAlignment = StringAlignment.Center;
63 _stringFormatTabHorizontal.FormatFlags = StringFormatFlags.NoWrap;
64 }
65 return _stringFormatTabHorizontal;
66 }
67 }
68
69 private static StringFormat _stringFormatTabVertical;
70 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="StringFormatTabVertical"]/*'/>
71 protected virtual StringFormat StringFormatTabVertical
72 {
73 get
74 {
75 if (_stringFormatTabVertical == null)
76 {
77 _stringFormatTabVertical = new StringFormat();
78 _stringFormatTabVertical.Alignment = StringAlignment.Near;
79 _stringFormatTabVertical.LineAlignment = StringAlignment.Center;
80 _stringFormatTabVertical.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.DirectionVertical;
81 }
82 return _stringFormatTabVertical;
83 }
84 }
85
86 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="ImageHeight"]/*'/>
87 protected virtual int ImageHeight
88 {
89 get { return _ImageHeight; }
90 }
91
92 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="ImageWidth"]/*'/>
93 protected virtual int ImageWidth
94 {
95 get { return _ImageWidth; }
96 }
97
98 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="ImageGapTop"]/*'/>
99 protected virtual int ImageGapTop
100 {
101 get { return _ImageGapTop; }
102 }
103
104 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="ImageGapLeft"]/*'/>
105 protected virtual int ImageGapLeft
106 {
107 get { return _ImageGapLeft; }
108 }
109
110 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="ImageGapRight"]/*'/>
111 protected virtual int ImageGapRight
112 {
113 get { return _ImageGapRight; }
114 }
115
116 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="ImageGapBottom"]/*'/>
117 protected virtual int ImageGapBottom
118 {
119 get { return _ImageGapBottom; }
120 }
121
122 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="TextGapLeft"]/*'/>
123 protected virtual int TextGapLeft
124 {
125 get { return _TextGapLeft; }
126 }
127
128 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="TextGapRight"]/*'/>
129 protected virtual int TextGapRight
130 {
131 get { return _TextGapRight; }
132 }
133
134 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="TabGapTop"]/*'/>
135 protected virtual int TabGapTop
136 {
137 get { return _TabGapTop; }
138 }
139
140 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="TabGapLeft"]/*'/>
141 protected virtual int TabGapLeft
142 {
143 get { return _TabGapLeft; }
144 }
145
146 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="TabGapBetween"]/*'/>
147 protected virtual int TabGapBetween
148 {
149 get { return _TabGapBetween; }
150 }
151
152 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="BrushTabBackground"]/*'/>
153 protected virtual Brush BrushTabBackground
154 {
155 get { return SystemBrushes.Control; }
156 }
157
158 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="PenTabBorder"]/*'/>
159 protected virtual Pen PenTabBorder
160 {
161 get { return SystemPens.GrayText; }
162 }
163
164 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Property[@name="BrushTabText"]/*'/>
165 protected virtual Brush BrushTabText
166 {
167 get { return SystemBrushes.FromSystemColor(SystemColors.ControlDarkDark); }
168 }
169 #endregion
170
171 private Matrix MatrixIdentity
172 {
173 get { return _matrixIdentity; }
174 }
175
176 private DockState[] DockStates
177 {
178 get { return _dockStates; }
179 }
180
181 static VS2003AutoHideStrip()
182 {
183 _matrixIdentity = new Matrix();
184
185 _dockStates = new DockState[4];
186 _dockStates[0] = DockState.DockLeftAutoHide;
187 _dockStates[1] = DockState.DockRightAutoHide;
188 _dockStates[2] = DockState.DockTopAutoHide;
189 _dockStates[3] = DockState.DockBottomAutoHide;
190 }
191
192 public VS2003AutoHideStrip(DockPanel panel) : base(panel)
193 {
194 SetStyle(ControlStyles.ResizeRedraw, true);
195 SetStyle(ControlStyles.UserPaint, true);
196 SetStyle(ControlStyles.AllPaintingInWmPaint, true);
197 BackColor = Color.WhiteSmoke;
198 }
199
200 /// <exclude/>
201 protected override void OnPaint(PaintEventArgs e)
202 {
203 Graphics g = e.Graphics;
204 DrawTabStrip(g);
205 }
206
207 /// <exclude/>
208 protected override void OnLayout(LayoutEventArgs levent)
209 {
210 CalculateTabs();
211 base.OnLayout (levent);
212 }
213
214 private void DrawTabStrip(Graphics g)
215 {
216 DrawTabStrip(g, DockState.DockTopAutoHide);
217 DrawTabStrip(g, DockState.DockBottomAutoHide);
218 DrawTabStrip(g, DockState.DockLeftAutoHide);
219 DrawTabStrip(g, DockState.DockRightAutoHide);
220 }
221
222 private void DrawTabStrip(Graphics g, DockState dockState)
223 {
224 Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);
225
226 if (rectTabStrip.IsEmpty)
227 return;
228
229 Matrix matrixIdentity = g.Transform;
230 if (dockState == DockState.DockLeftAutoHide || dockState == DockState.DockRightAutoHide)
231 {
232 Matrix matrixRotated = new Matrix();
233 matrixRotated.RotateAt(90, new PointF((float)rectTabStrip.X + (float)rectTabStrip.Height / 2,
234 (float)rectTabStrip.Y + (float)rectTabStrip.Height / 2));
235 g.Transform = matrixRotated;
236 }
237
238 foreach (Pane pane in GetPanes(dockState))
239 {
240 foreach (TabVS2003 tab in pane.AutoHideTabs)
241 DrawTab(g, tab);
242 }
243 g.Transform = matrixIdentity;
244 }
245
246 private void CalculateTabs()
247 {
248 CalculateTabs(DockState.DockTopAutoHide);
249 CalculateTabs(DockState.DockBottomAutoHide);
250 CalculateTabs(DockState.DockLeftAutoHide);
251 CalculateTabs(DockState.DockRightAutoHide);
252 }
253
254 private void CalculateTabs(DockState dockState)
255 {
256 Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);
257
258 int imageHeight = rectTabStrip.Height - ImageGapTop - ImageGapBottom;
259 int imageWidth = ImageWidth;
260 if (imageHeight > ImageHeight)
261 imageWidth = ImageWidth * (imageHeight/ImageHeight);
262
263 using (Graphics g = CreateGraphics())
264 {
265 int x = TabGapLeft + rectTabStrip.X;
266 foreach (Pane pane in GetPanes(dockState))
267 {
268 int maxWidth = 0;
269 foreach (TabVS2003 tab in pane.AutoHideTabs)
270 {
271 int width = imageWidth + ImageGapLeft + ImageGapRight +
272 (int)g.MeasureString(tab.Content.DockHandler.TabText, Font).Width + 1 +
273 TextGapLeft + TextGapRight;
274 if (width > maxWidth)
275 maxWidth = width;
276 }
277
278 foreach (TabVS2003 tab in pane.AutoHideTabs)
279 {
280 tab.TabX = x;
281 if (tab.Content == pane.DockPane.ActiveContent)
282 tab.TabWidth = maxWidth;
283 else
284 tab.TabWidth = imageWidth + ImageGapLeft + ImageGapRight;
285 x += tab.TabWidth;
286 }
287 x += TabGapBetween;
288 }
289 }
290 }
291
292 private void DrawTab(Graphics g, TabVS2003 tab)
293 {
294 Rectangle rectTab = GetTabRectangle(tab);
295 if (rectTab.IsEmpty)
296 return;
297
298 DockState dockState = tab.Content.DockHandler.DockState;
299 IDockContent content = tab.Content;
300
301 OnBeginDrawTab(tab);
302
303 Brush brushTabBackGround = BrushTabBackground;
304 Pen penTabBorder = PenTabBorder;
305 Brush brushTabText = BrushTabText;
306
307 g.FillRectangle(brushTabBackGround, rectTab);
308
309 g.DrawLine(penTabBorder, rectTab.Left, rectTab.Top, rectTab.Left, rectTab.Bottom);
310 g.DrawLine(penTabBorder, rectTab.Right, rectTab.Top, rectTab.Right, rectTab.Bottom);
311 if (dockState == DockState.DockTopAutoHide || dockState == DockState.DockRightAutoHide)
312 g.DrawLine(penTabBorder, rectTab.Left, rectTab.Bottom, rectTab.Right, rectTab.Bottom);
313 else
314 g.DrawLine(penTabBorder, rectTab.Left, rectTab.Top, rectTab.Right, rectTab.Top);
315
316 // Set no rotate for drawing icon and text
317 Matrix matrixRotate = g.Transform;
318 g.Transform = MatrixIdentity;
319
320 // Draw the icon
321 Rectangle rectImage = rectTab;
322 rectImage.X += ImageGapLeft;
323 rectImage.Y += ImageGapTop;
324 int imageHeight = rectTab.Height - ImageGapTop - ImageGapBottom;
325 int imageWidth = ImageWidth;
326 if (imageHeight > ImageHeight)
327 imageWidth = ImageWidth * (imageHeight/ImageHeight);
328 rectImage.Height = imageHeight;
329 rectImage.Width = imageWidth;
330 rectImage = GetTransformedRectangle(dockState, rectImage);
331 g.DrawIcon(((Form)content).Icon, rectImage);
332
333 // Draw the text
334 if (content == content.DockHandler.Pane.ActiveContent)
335 {
336 Rectangle rectText = rectTab;
337 rectText.X += ImageGapLeft + imageWidth + ImageGapRight + TextGapLeft;
338 rectText.Width -= ImageGapLeft + imageWidth + ImageGapRight + TextGapLeft;
339 rectText = GetTransformedRectangle(dockState, rectText);
340 if (dockState == DockState.DockLeftAutoHide || dockState == DockState.DockRightAutoHide)
341 g.DrawString(content.DockHandler.TabText, Font, brushTabText, rectText, StringFormatTabVertical);
342 else
343 g.DrawString(content.DockHandler.TabText, Font, brushTabText, rectText, StringFormatTabHorizontal);
344 }
345
346 // Set rotate back
347 g.Transform = matrixRotate;
348
349 OnEndDrawTab(tab);
350 }
351
352 private Rectangle GetLogicalTabStripRectangle(DockState dockState)
353 {
354 return GetLogicalTabStripRectangle(dockState, false);
355 }
356
357 private Rectangle GetLogicalTabStripRectangle(DockState dockState, bool transformed)
358 {
359 if (!DockHelper.IsDockStateAutoHide(dockState))
360 return Rectangle.Empty;
361
362 int leftPanes = GetPanes(DockState.DockLeftAutoHide).Count;
363 int rightPanes = GetPanes(DockState.DockRightAutoHide).Count;
364 int topPanes = GetPanes(DockState.DockTopAutoHide).Count;
365 int bottomPanes = GetPanes(DockState.DockBottomAutoHide).Count;
366
367 int x, y, width, height;
368
369 height = MeasureHeight();
370 if (dockState == DockState.DockLeftAutoHide && leftPanes > 0)
371 {
372 x = 0;
373 y = (topPanes == 0) ? 0 : height;
374 width = Height - (topPanes == 0 ? 0 : height) - (bottomPanes == 0 ? 0 :height);
375 }
376 else if (dockState == DockState.DockRightAutoHide && rightPanes > 0)
377 {
378 x = Width - height;
379 if (leftPanes != 0 && x < height)
380 x = height;
381 y = (topPanes == 0) ? 0 : height;
382 width = Height - (topPanes == 0 ? 0 : height) - (bottomPanes == 0 ? 0 :height);
383 }
384 else if (dockState == DockState.DockTopAutoHide && topPanes > 0)
385 {
386 x = leftPanes == 0 ? 0 : height;
387 y = 0;
388 width = Width - (leftPanes == 0 ? 0 : height) - (rightPanes == 0 ? 0 : height);
389 }
390 else if (dockState == DockState.DockBottomAutoHide && bottomPanes > 0)
391 {
392 x = leftPanes == 0 ? 0 : height;
393 y = Height - height;
394 if (topPanes != 0 && y < height)
395 y = height;
396 width = Width - (leftPanes == 0 ? 0 : height) - (rightPanes == 0 ? 0 : height);
397 }
398 else
399 return Rectangle.Empty;
400
401 if (!transformed)
402 return new Rectangle(x, y, width, height);
403 else
404 return GetTransformedRectangle(dockState, new Rectangle(x, y, width, height));
405 }
406
407 private Rectangle GetTabRectangle(TabVS2003 tab)
408 {
409 return GetTabRectangle(tab, false);
410 }
411
412 private Rectangle GetTabRectangle(TabVS2003 tab, bool transformed)
413 {
414 DockState dockState = tab.Content.DockHandler.DockState;
415 Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);
416
417 if (rectTabStrip.IsEmpty)
418 return Rectangle.Empty;
419
420 int x = tab.TabX;
421 int y = rectTabStrip.Y +
422 (dockState == DockState.DockTopAutoHide || dockState == DockState.DockRightAutoHide ?
423 0 : TabGapTop);
424 int width = tab.TabWidth;
425 int height = rectTabStrip.Height - TabGapTop;
426
427 if (!transformed)
428 return new Rectangle(x, y, width, height);
429 else
430 return GetTransformedRectangle(dockState, new Rectangle(x, y, width, height));
431 }
432
433 private Rectangle GetTransformedRectangle(DockState dockState, Rectangle rect)
434 {
435 if (dockState != DockState.DockLeftAutoHide && dockState != DockState.DockRightAutoHide)
436 return rect;
437
438 PointF[] pts = new PointF[1];
439 // the center of the rectangle
440 pts[0].X = (float)rect.X + (float)rect.Width / 2;
441 pts[0].Y = (float)rect.Y + (float)rect.Height / 2;
442 Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);
443 Matrix matrix = new Matrix();
444 matrix.RotateAt(90, new PointF((float)rectTabStrip.X + (float)rectTabStrip.Height / 2,
445 (float)rectTabStrip.Y + (float)rectTabStrip.Height / 2));
446 matrix.TransformPoints(pts);
447
448 return new Rectangle((int)(pts[0].X - (float)rect.Height / 2 + .5F),
449 (int)(pts[0].Y - (float)rect.Width / 2 + .5F),
450 rect.Height, rect.Width);
451 }
452
453 /// <exclude />
454 protected override IDockContent HitTest(Point ptMouse)
455 {
456 foreach(DockState state in DockStates)
457 {
458 Rectangle rectTabStrip = GetLogicalTabStripRectangle(state, true);
459 if (!rectTabStrip.Contains(ptMouse))
460 continue;
461
462 foreach(Pane pane in GetPanes(state))
463 {
464 foreach(TabVS2003 tab in pane.AutoHideTabs)
465 {
466 Rectangle rectTab = GetTabRectangle(tab, true);
467 rectTab.Intersect(rectTabStrip);
468 if (rectTab.Contains(ptMouse))
469 return tab.Content;
470 }
471 }
472 }
473
474 return null;
475 }
476
477 /// <exclude/>
478 protected override int MeasureHeight()
479 {
480 return Math.Max(ImageGapBottom +
481 ImageGapTop + ImageHeight,
482 Font.Height) + TabGapTop;
483 }
484
485 /// <exclude/>
486 protected override void OnRefreshChanges()
487 {
488 CalculateTabs();
489 Invalidate();
490 }
491
492 protected override AutoHideStripBase.Tab CreateTab(IDockContent content)
493 {
494 return new TabVS2003(content);
495 }
496
497 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Method[@name="OnBeginDrawTab(AutoHideTab)"]/*'/>
498 protected virtual void OnBeginDrawTab(Tab tab)
499 {
500 }
501
502 /// <include file='CodeDoc/AutoHideStripVS2003.xml' path='//CodeDoc/Class[@name="AutoHideStripVS2003"]/Method[@name="OnEndDrawTab(AutoHideTab)"]/*'/>
503 protected virtual void OnEndDrawTab(Tab tab)
504 {
505 }
506 }
507 }

  ViewVC Help
Powered by ViewVC 1.1.22