Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/WinFormsUI/Docking/VS2005AutoHideStrip.cs @ 2483

Last change on this file since 2483 was 2134, checked in by gkronber, 15 years ago

Added up to date source of Weifen Luo dock panel suit in a separate project (and added strong name key). Removed binary versions of Weifen Luo dock panel suite and references to it. #687 (Update AdvancedOptimizationFrontend to use more recent version of Weifen Luo Docking library)

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