PageRenderTime 42ms CodeModel.GetById 16ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/stable-1.2.0/Client/PHPPageItemControl.cs

#
C# | 270 lines | 224 code | 36 blank | 10 comment | 17 complexity | d75d84f1c3fa46f85e150d0e22712d78 MD5 | raw file
  1//-----------------------------------------------------------------------
  2// <copyright>
  3// Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
  4//
  5// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
  6// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
  7// </copyright>
  8//----------------------------------------------------------------------- 
  9
 10using System;
 11using System.Collections.Generic;
 12using System.Drawing;
 13using System.Security.Permissions;
 14using System.Text;
 15using System.Windows.Forms;
 16
 17namespace Web.Management.PHP
 18{
 19
 20    internal partial class PHPPageItemControl : UserControl
 21    {
 22        private const int WS_EX_NOINHERITLAYOUT = 0x100000;
 23        private const int WS_EX_LAYOUTRTL = 0x400000;
 24        private const string WarningLabelName = "warningLabel";
 25        private bool _rightToLeftLayout;
 26        private int _tlpRowCount;
 27
 28        private Action<int> _handler;
 29
 30        public PHPPageItemControl()
 31        {
 32            InitializeComponent();
 33        }
 34
 35        protected override CreateParams CreateParams
 36        {
 37            [
 38                SecurityPermission(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode),
 39                SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)
 40            ]
 41            get
 42            {
 43                CreateParams CP;
 44                CP = base.CreateParams;
 45                if (_rightToLeftLayout)
 46                {
 47                    CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;
 48                }
 49
 50                return CP;
 51            }
 52        }
 53
 54        public Image Image
 55        {
 56            get
 57            {
 58                return _pictureBox.Image;
 59            }
 60            set
 61            {
 62                _pictureBox.Image = value;
 63            }
 64        }
 65
 66        public bool RightToLeftLayout
 67        {
 68            get
 69            {
 70                return _rightToLeftLayout;
 71            }
 72            set
 73            {
 74                if (_rightToLeftLayout != value)
 75                {
 76                    _rightToLeftLayout = value;
 77                    if (IsHandleCreated)
 78                    {
 79                        base.OnRightToLeftChanged(EventArgs.Empty);
 80                    }
 81                }
 82            }
 83        }
 84
 85        public string Title
 86        {
 87            get
 88            {
 89                return _titleLabel.Text;
 90            }
 91            set
 92            {
 93                _titleLabel.Text = value;
 94            }
 95        }
 96
 97        public Font TitleFont
 98        {
 99            get
100            {
101                return _titleLabel.Font;
102            }
103            set
104            {
105                _titleLabel.Font = value;
106            }
107        }
108
109        public event LinkLabelLinkClickedEventHandler TitleClick
110        {
111            add
112            {
113                _titleLabel.LinkClicked += value;
114            }
115            remove
116            {
117                _titleLabel.LinkClicked -= value;
118            }
119        }
120
121        public void AddInfoRow(Label labelName, Label labelValue)
122        {
123            labelName.Dock = labelValue.Dock = DockStyle.Fill;
124            labelName.TextAlign = labelValue.TextAlign = ContentAlignment.MiddleLeft;
125            _infoTlp.Controls.Add(labelName, 0, _tlpRowCount);
126            _infoTlp.Controls.Add(labelValue, 1, _tlpRowCount);
127            _tlpRowCount++;
128        }
129
130        public void AddSpanRow(Label labelSpan)
131        {
132            labelSpan.Dock = DockStyle.Fill;
133            labelSpan.TextAlign = ContentAlignment.MiddleLeft;
134            _infoTlp.Controls.Add(labelSpan, 0, _tlpRowCount);
135            _infoTlp.SetColumnSpan(labelSpan, 2);
136            _tlpRowCount++;
137        }
138
139        public void AddTask(Action<int> handler, params string[] actionTitles)
140        {
141            if (handler == null)
142            {
143                throw new ArgumentNullException("handler");
144            }
145            if (_handler != null)
146            {
147                throw new InvalidOperationException();
148            }
149
150            _handler = handler;
151            StringBuilder sb = new StringBuilder();
152            bool first = true;
153            List<LinkLabel.Link> links = new List<LinkLabel.Link>();
154            foreach (string s in actionTitles)
155            {
156                if (!first)
157                {
158                    sb.Append(Resources.PHPPageItemTaskSeparator);
159                }
160                first = false;
161                links.Add(new LinkLabel.Link(sb.Length, s.Length, links.Count));
162
163                sb.Append(s);
164            }
165
166            _tasksLabel.Text = sb.ToString();
167            foreach (LinkLabel.Link l in links)
168            {
169                _tasksLabel.Links.Add(l);
170            }
171        }
172
173        public void ClearWarning()
174        {
175            _warningPanel.Controls.RemoveByKey(WarningLabelName);
176            _warningPanel.Visible = false;
177        }
178
179        private Size DoLayout(Size proposedSize, bool performLayout)
180        {
181            if (performLayout)
182            {
183                _titleLabel.Width = ClientRectangle.Width;
184            }
185
186            int nextTop = _titleLabel.Top + _titleLabel.Height + 8;
187
188            if (_warningPanel.Visible)
189            {
190                Size warningSize = _warningPanel.GetPreferredSize(new Size(proposedSize.Width - _warningPanel.Left, Int32.MaxValue));
191                if (performLayout)
192                {
193                    _warningPanel.Top = nextTop;
194                    _warningPanel.Size = warningSize;
195                }
196                nextTop += _warningPanel.Height + 8;
197            }
198
199            Size infoSize = _infoTlp.GetPreferredSize(new Size(proposedSize.Width - _infoTlp.Left, Int32.MaxValue));
200            if (performLayout)
201            {
202                _infoTlp.Top = nextTop;
203                _infoTlp.Size = infoSize;
204            }
205
206            nextTop += _infoTlp.Height + 10;
207
208            Size tasksSize = _tasksLabel.GetPreferredSize(new Size(proposedSize.Width - _tasksLabel.Left, Int32.MaxValue));
209            if (performLayout)
210            {
211                _tasksLabel.Top = nextTop;
212                _tasksLabel.Size = tasksSize;
213            }
214
215            int height = tasksSize.Height + nextTop + 12;
216
217            return new Size(proposedSize.Width, height);
218        }
219
220        public override Size GetPreferredSize(Size proposedSize)
221        {
222            Size size = DoLayout(proposedSize, false);
223            return size;
224        }
225
226        protected override void OnLayout(LayoutEventArgs e)
227        {
228            DoLayout(this.Size, true);
229        }
230
231        protected override void OnRightToLeftChanged(EventArgs e)
232        {
233            base.OnRightToLeftChanged(e);
234
235            RightToLeftLayout = RightToLeft == RightToLeft.Yes;
236        }
237
238        private void OnTasksLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
239        {
240            _handler((int)e.Link.LinkData);
241        }
242
243        public void SetTaskState(int index, bool enabled)
244        {
245            LinkLabel.Link l = _tasksLabel.Links[index];
246            if (l != null)
247            {
248                l.Enabled = enabled;
249            }
250        }
251
252        public void SetTitleState(bool enabled)
253        {
254            _titleLabel.Enabled = enabled;
255        }
256
257        public void SetWarning(Label warningLabel)
258        {
259            warningLabel.AutoSize = true;
260            warningLabel.Location = new Point(25, 6);
261            warningLabel.Name = WarningLabelName;
262            // Remove existing label from the warning panel in case it exists
263            _warningPanel.Controls.RemoveByKey(WarningLabelName);
264            // Add new label to the warning panel
265            _warningPanel.Controls.Add(warningLabel);
266            _warningPanel.Visible = true;
267        }
268
269    }
270}