1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/External/Aga.Controls/Tree/TreeViewAdv.Input.cs Sun May 27 15:16:19 2012 +0000
1.3 @@ -0,0 +1,558 @@
1.4 +using System;
1.5 +using System.Collections.Generic;
1.6 +using System.Text;
1.7 +using System.Windows.Forms;
1.8 +using System.Drawing;
1.9 +using Aga.Controls.Tree.NodeControls;
1.10 +using System.Drawing.Imaging;
1.11 +using System.Threading;
1.12 +
1.13 +namespace Aga.Controls.Tree
1.14 +{
1.15 + public partial class TreeViewAdv
1.16 + {
1.17 + #region Keys
1.18 +
1.19 + protected override bool IsInputChar(char charCode)
1.20 + {
1.21 + return true;
1.22 + }
1.23 +
1.24 + protected override bool IsInputKey(Keys keyData)
1.25 + {
1.26 + if (((keyData & Keys.Up) == Keys.Up)
1.27 + || ((keyData & Keys.Down) == Keys.Down)
1.28 + || ((keyData & Keys.Left) == Keys.Left)
1.29 + || ((keyData & Keys.Right) == Keys.Right))
1.30 + return true;
1.31 + else
1.32 + return base.IsInputKey(keyData);
1.33 + }
1.34 +
1.35 + internal void ChangeInput()
1.36 + {
1.37 + if ((ModifierKeys & Keys.Shift) == Keys.Shift)
1.38 + {
1.39 + if (!(Input is InputWithShift))
1.40 + Input = new InputWithShift(this);
1.41 + }
1.42 + else if ((ModifierKeys & Keys.Control) == Keys.Control)
1.43 + {
1.44 + if (!(Input is InputWithControl))
1.45 + Input = new InputWithControl(this);
1.46 + }
1.47 + else
1.48 + {
1.49 + if (!(Input.GetType() == typeof(NormalInputState)))
1.50 + Input = new NormalInputState(this);
1.51 + }
1.52 + }
1.53 +
1.54 + protected override void OnKeyDown(KeyEventArgs e)
1.55 + {
1.56 + base.OnKeyDown(e);
1.57 + if (!e.Handled)
1.58 + {
1.59 + if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.ControlKey)
1.60 + ChangeInput();
1.61 + Input.KeyDown(e);
1.62 + if (!e.Handled)
1.63 + {
1.64 + foreach (NodeControlInfo item in GetNodeControls(CurrentNode))
1.65 + {
1.66 + item.Control.KeyDown(e);
1.67 + if (e.Handled)
1.68 + break;
1.69 + }
1.70 + }
1.71 + }
1.72 + }
1.73 +
1.74 + protected override void OnKeyUp(KeyEventArgs e)
1.75 + {
1.76 + base.OnKeyUp(e);
1.77 + if (!e.Handled)
1.78 + {
1.79 + if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.ControlKey)
1.80 + ChangeInput();
1.81 + if (!e.Handled)
1.82 + {
1.83 + foreach (NodeControlInfo item in GetNodeControls(CurrentNode))
1.84 + {
1.85 + item.Control.KeyUp(e);
1.86 + if (e.Handled)
1.87 + return;
1.88 + }
1.89 + }
1.90 + }
1.91 + }
1.92 +
1.93 + protected override void OnKeyPress(KeyPressEventArgs e)
1.94 + {
1.95 + base.OnKeyPress(e);
1.96 + if (!e.Handled)
1.97 + _search.Search(e.KeyChar);
1.98 + }
1.99 +
1.100 + #endregion
1.101 +
1.102 + #region Mouse
1.103 +
1.104 + private TreeNodeAdvMouseEventArgs CreateMouseArgs(MouseEventArgs e)
1.105 + {
1.106 + TreeNodeAdvMouseEventArgs args = new TreeNodeAdvMouseEventArgs(e);
1.107 + args.ViewLocation = new Point(e.X + OffsetX,
1.108 + e.Y + _rowLayout.GetRowBounds(FirstVisibleRow).Y - ColumnHeaderHeight);
1.109 + args.ModifierKeys = ModifierKeys;
1.110 + args.Node = GetNodeAt(e.Location);
1.111 + NodeControlInfo info = GetNodeControlInfoAt(args.Node, e.Location);
1.112 + args.ControlBounds = info.Bounds;
1.113 + args.Control = info.Control;
1.114 + return args;
1.115 + }
1.116 +
1.117 + protected override void OnMouseWheel(MouseEventArgs e)
1.118 + {
1.119 + _search.EndSearch();
1.120 + if (SystemInformation.MouseWheelScrollLines > 0)
1.121 + {
1.122 + int lines = e.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1.123 + int newValue = _vScrollBar.Value - lines;
1.124 + newValue = Math.Min(_vScrollBar.Maximum - _vScrollBar.LargeChange + 1, newValue);
1.125 + newValue = Math.Min(_vScrollBar.Maximum, newValue);
1.126 + _vScrollBar.Value = Math.Max(_vScrollBar.Minimum, newValue);
1.127 + }
1.128 + base.OnMouseWheel(e);
1.129 + }
1.130 +
1.131 + protected override void OnMouseDown(MouseEventArgs e)
1.132 + {
1.133 + if (CurrentEditorOwner != null)
1.134 + {
1.135 + CurrentEditorOwner.EndEdit(true);
1.136 + return;
1.137 + }
1.138 +
1.139 + if (!Focused)
1.140 + Focus();
1.141 +
1.142 + _search.EndSearch();
1.143 + if (e.Button == MouseButtons.Left)
1.144 + {
1.145 + TreeColumn c;
1.146 + c = GetColumnDividerAt(e.Location);
1.147 + if (c != null)
1.148 + {
1.149 + Input = new ResizeColumnState(this, c, e.Location);
1.150 + return;
1.151 + }
1.152 + c = GetColumnAt(e.Location);
1.153 + if (c != null)
1.154 + {
1.155 + Input = new ClickColumnState(this, c, e.Location);
1.156 + UpdateView();
1.157 + return;
1.158 + }
1.159 + }
1.160 +
1.161 + ChangeInput();
1.162 + TreeNodeAdvMouseEventArgs args = CreateMouseArgs(e);
1.163 +
1.164 + if (args.Node != null && args.Control != null)
1.165 + args.Control.MouseDown(args);
1.166 +
1.167 + if (!args.Handled)
1.168 + Input.MouseDown(args);
1.169 +
1.170 + base.OnMouseDown(e);
1.171 + }
1.172 +
1.173 + protected override void OnMouseClick(MouseEventArgs e)
1.174 + {
1.175 + //TODO: Disable when click on plusminus icon
1.176 + TreeNodeAdvMouseEventArgs args = CreateMouseArgs(e);
1.177 + if (args.Node != null)
1.178 + OnNodeMouseClick(args);
1.179 +
1.180 + base.OnMouseClick(e);
1.181 + }
1.182 +
1.183 + protected override void OnMouseDoubleClick(MouseEventArgs e)
1.184 + {
1.185 + TreeNodeAdvMouseEventArgs args = CreateMouseArgs(e);
1.186 +
1.187 + if (args.Node != null && args.Control != null)
1.188 + args.Control.MouseDoubleClick(args);
1.189 +
1.190 + if (!args.Handled)
1.191 + {
1.192 + if (args.Node != null)
1.193 + OnNodeMouseDoubleClick(args);
1.194 + else
1.195 + Input.MouseDoubleClick(args);
1.196 +
1.197 + if (!args.Handled)
1.198 + {
1.199 + if (args.Node != null && args.Button == MouseButtons.Left)
1.200 + args.Node.IsExpanded = !args.Node.IsExpanded;
1.201 + }
1.202 + }
1.203 +
1.204 + base.OnMouseDoubleClick(e);
1.205 + }
1.206 +
1.207 + protected override void OnMouseUp(MouseEventArgs e)
1.208 + {
1.209 + TreeNodeAdvMouseEventArgs args = CreateMouseArgs(e);
1.210 + if (Input is ResizeColumnState)
1.211 + Input.MouseUp(args);
1.212 + else
1.213 + {
1.214 + if (args.Node != null && args.Control != null)
1.215 + args.Control.MouseUp(args);
1.216 + if (!args.Handled)
1.217 + Input.MouseUp(args);
1.218 +
1.219 + base.OnMouseUp(e);
1.220 + }
1.221 + }
1.222 +
1.223 + protected override void OnMouseMove(MouseEventArgs e)
1.224 + {
1.225 + if (Input.MouseMove(e))
1.226 + return;
1.227 +
1.228 + base.OnMouseMove(e);
1.229 + SetCursor(e);
1.230 + UpdateToolTip(e);
1.231 + if (ItemDragMode && Dist(e.Location, ItemDragStart) > ItemDragSensivity
1.232 + && CurrentNode != null && CurrentNode.IsSelected)
1.233 + {
1.234 + ItemDragMode = false;
1.235 + _toolTip.Active = false;
1.236 + OnItemDrag(e.Button, Selection.ToArray());
1.237 + }
1.238 + }
1.239 +
1.240 + protected override void OnMouseLeave(EventArgs e)
1.241 + {
1.242 + _hotColumn = null;
1.243 + UpdateHeaders();
1.244 + base.OnMouseLeave(e);
1.245 + }
1.246 +
1.247 + private void SetCursor(MouseEventArgs e)
1.248 + {
1.249 + TreeColumn col;
1.250 + col = GetColumnDividerAt(e.Location);
1.251 + if (col == null)
1.252 + _innerCursor = null;
1.253 + else
1.254 + {
1.255 + if (col.Width == 0)
1.256 + _innerCursor = ResourceHelper.DVSplitCursor;
1.257 + else
1.258 + _innerCursor = Cursors.VSplit;
1.259 + }
1.260 +
1.261 + col = GetColumnAt(e.Location);
1.262 + if (col != _hotColumn)
1.263 + {
1.264 + _hotColumn = col;
1.265 + UpdateHeaders();
1.266 + }
1.267 + }
1.268 +
1.269 + internal TreeColumn GetColumnAt(Point p)
1.270 + {
1.271 + if (p.Y > ColumnHeaderHeight)
1.272 + return null;
1.273 +
1.274 + int x = -OffsetX;
1.275 + foreach (TreeColumn col in Columns)
1.276 + {
1.277 + if (col.IsVisible)
1.278 + {
1.279 + Rectangle rect = new Rectangle(x, 0, col.Width, ColumnHeaderHeight);
1.280 + x += col.Width;
1.281 + if (rect.Contains(p))
1.282 + return col;
1.283 + }
1.284 + }
1.285 + return null;
1.286 + }
1.287 +
1.288 + internal int GetColumnX(TreeColumn column)
1.289 + {
1.290 + int x = -OffsetX;
1.291 + foreach (TreeColumn col in Columns)
1.292 + {
1.293 + if (col.IsVisible)
1.294 + {
1.295 + if (column == col)
1.296 + return x;
1.297 + else
1.298 + x += col.Width;
1.299 + }
1.300 + }
1.301 + return x;
1.302 + }
1.303 +
1.304 + internal TreeColumn GetColumnDividerAt(Point p)
1.305 + {
1.306 + if (p.Y > ColumnHeaderHeight)
1.307 + return null;
1.308 +
1.309 + int x = -OffsetX;
1.310 + TreeColumn prevCol = null;
1.311 + Rectangle left, right;
1.312 + foreach (TreeColumn col in Columns)
1.313 + {
1.314 + if (col.IsVisible)
1.315 + {
1.316 + if (col.Width > 0)
1.317 + {
1.318 + left = new Rectangle(x, 0, DividerWidth / 2, ColumnHeaderHeight);
1.319 + right = new Rectangle(x + col.Width - (DividerWidth / 2), 0, DividerWidth / 2, ColumnHeaderHeight);
1.320 + if (left.Contains(p) && prevCol != null)
1.321 + return prevCol;
1.322 + else if (right.Contains(p))
1.323 + return col;
1.324 + }
1.325 + prevCol = col;
1.326 + x += col.Width;
1.327 + }
1.328 + }
1.329 +
1.330 + left = new Rectangle(x, 0, DividerWidth / 2, ColumnHeaderHeight);
1.331 + if (left.Contains(p) && prevCol != null)
1.332 + return prevCol;
1.333 +
1.334 + return null;
1.335 + }
1.336 +
1.337 + TreeColumn _tooltipColumn;
1.338 + private void UpdateToolTip(MouseEventArgs e)
1.339 + {
1.340 + TreeColumn col = GetColumnAt(e.Location);
1.341 + if (col != null)
1.342 + {
1.343 + if (col != _tooltipColumn)
1.344 + SetTooltip(col.TooltipText);
1.345 + }
1.346 + else
1.347 + DisplayNodesTooltip(e);
1.348 + _tooltipColumn = col;
1.349 + }
1.350 +
1.351 + TreeNodeAdv _hotNode;
1.352 + NodeControl _hotControl;
1.353 + private void DisplayNodesTooltip(MouseEventArgs e)
1.354 + {
1.355 + if (ShowNodeToolTips)
1.356 + {
1.357 + TreeNodeAdvMouseEventArgs args = CreateMouseArgs(e);
1.358 + if (args.Node != null && args.Control != null)
1.359 + {
1.360 + if (args.Node != _hotNode || args.Control != _hotControl)
1.361 + SetTooltip(GetNodeToolTip(args));
1.362 + }
1.363 + else
1.364 + _toolTip.SetToolTip(this, null);
1.365 +
1.366 + _hotControl = args.Control;
1.367 + _hotNode = args.Node;
1.368 + }
1.369 + else
1.370 + _toolTip.SetToolTip(this, null);
1.371 + }
1.372 +
1.373 + private void SetTooltip(string text)
1.374 + {
1.375 + if (!String.IsNullOrEmpty(text))
1.376 + {
1.377 + _toolTip.Active = false;
1.378 + _toolTip.SetToolTip(this, text);
1.379 + _toolTip.Active = true;
1.380 + }
1.381 + else
1.382 + _toolTip.SetToolTip(this, null);
1.383 + }
1.384 +
1.385 + private string GetNodeToolTip(TreeNodeAdvMouseEventArgs args)
1.386 + {
1.387 + string msg = args.Control.GetToolTip(args.Node);
1.388 +
1.389 + BaseTextControl btc = args.Control as BaseTextControl;
1.390 + if (btc != null && btc.DisplayHiddenContentInToolTip && String.IsNullOrEmpty(msg))
1.391 + {
1.392 + Size ms = btc.GetActualSize(args.Node, _measureContext);
1.393 + if (ms.Width > args.ControlBounds.Size.Width || ms.Height > args.ControlBounds.Size.Height
1.394 + || args.ControlBounds.Right - OffsetX > DisplayRectangle.Width)
1.395 + msg = btc.GetLabel(args.Node);
1.396 + }
1.397 +
1.398 + if (String.IsNullOrEmpty(msg) && DefaultToolTipProvider != null)
1.399 + msg = DefaultToolTipProvider.GetToolTip(args.Node, args.Control);
1.400 +
1.401 + return msg;
1.402 + }
1.403 +
1.404 + #endregion
1.405 +
1.406 + #region DragDrop
1.407 +
1.408 + private bool _dragAutoScrollFlag = false;
1.409 + private Bitmap _dragBitmap = null;
1.410 + private System.Threading.Timer _dragTimer;
1.411 +
1.412 + private void StartDragTimer()
1.413 + {
1.414 + if (_dragTimer == null)
1.415 + _dragTimer = new System.Threading.Timer(new TimerCallback(DragTimerTick), null, 0, 100);
1.416 + }
1.417 +
1.418 + private void StopDragTimer()
1.419 + {
1.420 + if (_dragTimer != null)
1.421 + {
1.422 + _dragTimer.Dispose();
1.423 + _dragTimer = null;
1.424 + }
1.425 + }
1.426 +
1.427 + private void SetDropPosition(Point pt)
1.428 + {
1.429 + TreeNodeAdv node = GetNodeAt(pt);
1.430 + OnDropNodeValidating(pt, ref node);
1.431 + _dropPosition.Node = node;
1.432 + if (node != null)
1.433 + {
1.434 + Rectangle first = _rowLayout.GetRowBounds(FirstVisibleRow);
1.435 + Rectangle bounds = _rowLayout.GetRowBounds(node.Row);
1.436 + float pos = (pt.Y + first.Y - ColumnHeaderHeight - bounds.Y) / (float)bounds.Height;
1.437 + if (pos < TopEdgeSensivity)
1.438 + _dropPosition.Position = NodePosition.Before;
1.439 + else if (pos > (1 - BottomEdgeSensivity))
1.440 + _dropPosition.Position = NodePosition.After;
1.441 + else
1.442 + _dropPosition.Position = NodePosition.Inside;
1.443 + }
1.444 + }
1.445 +
1.446 + private void DragTimerTick(object state)
1.447 + {
1.448 + _dragAutoScrollFlag = true;
1.449 + }
1.450 +
1.451 + private void DragAutoScroll()
1.452 + {
1.453 + _dragAutoScrollFlag = false;
1.454 + Point pt = PointToClient(MousePosition);
1.455 + if (pt.Y < 20 && _vScrollBar.Value > 0)
1.456 + _vScrollBar.Value--;
1.457 + else if (pt.Y > Height - 20 && _vScrollBar.Value <= _vScrollBar.Maximum - _vScrollBar.LargeChange)
1.458 + _vScrollBar.Value++;
1.459 + }
1.460 +
1.461 + public void DoDragDropSelectedNodes(DragDropEffects allowedEffects)
1.462 + {
1.463 + if (SelectedNodes.Count > 0)
1.464 + {
1.465 + TreeNodeAdv[] nodes = new TreeNodeAdv[SelectedNodes.Count];
1.466 + SelectedNodes.CopyTo(nodes, 0);
1.467 + DoDragDrop(nodes, allowedEffects);
1.468 + }
1.469 + }
1.470 +
1.471 + private void CreateDragBitmap(IDataObject data)
1.472 + {
1.473 + if (UseColumns || !DisplayDraggingNodes)
1.474 + return;
1.475 +
1.476 + TreeNodeAdv[] nodes = data.GetData(typeof(TreeNodeAdv[])) as TreeNodeAdv[];
1.477 + if (nodes != null && nodes.Length > 0)
1.478 + {
1.479 + Rectangle rect = DisplayRectangle;
1.480 + Bitmap bitmap = new Bitmap(rect.Width, rect.Height);
1.481 + using (Graphics gr = Graphics.FromImage(bitmap))
1.482 + {
1.483 + gr.Clear(BackColor);
1.484 + DrawContext context = new DrawContext();
1.485 + context.Graphics = gr;
1.486 + context.Font = Font;
1.487 + context.Enabled = true;
1.488 + int y = 0;
1.489 + int maxWidth = 0;
1.490 + foreach (TreeNodeAdv node in nodes)
1.491 + {
1.492 + if (node.Tree == this)
1.493 + {
1.494 + int x = 0;
1.495 + int height = _rowLayout.GetRowBounds(node.Row).Height;
1.496 + foreach (NodeControl c in NodeControls)
1.497 + {
1.498 + Size s = c.GetActualSize(node, context);
1.499 + if (!s.IsEmpty)
1.500 + {
1.501 + int width = s.Width;
1.502 + rect = new Rectangle(x, y, width, height);
1.503 + x += (width + 1);
1.504 + context.Bounds = rect;
1.505 + c.Draw(node, context);
1.506 + }
1.507 + }
1.508 + y += height;
1.509 + maxWidth = Math.Max(maxWidth, x);
1.510 + }
1.511 + }
1.512 +
1.513 + if (maxWidth > 0 && y > 0)
1.514 + {
1.515 + _dragBitmap = new Bitmap(maxWidth, y, PixelFormat.Format32bppArgb);
1.516 + using (Graphics tgr = Graphics.FromImage(_dragBitmap))
1.517 + tgr.DrawImage(bitmap, Point.Empty);
1.518 + BitmapHelper.SetAlphaChanelValue(_dragBitmap, 150);
1.519 + }
1.520 + else
1.521 + _dragBitmap = null;
1.522 + }
1.523 + }
1.524 + }
1.525 +
1.526 + protected override void OnDragOver(DragEventArgs drgevent)
1.527 + {
1.528 + ItemDragMode = false;
1.529 + Point pt = PointToClient(new Point(drgevent.X, drgevent.Y));
1.530 + if (_dragAutoScrollFlag)
1.531 + DragAutoScroll();
1.532 + SetDropPosition(pt);
1.533 + UpdateView();
1.534 + base.OnDragOver(drgevent);
1.535 + }
1.536 +
1.537 + protected override void OnDragEnter(DragEventArgs drgevent)
1.538 + {
1.539 + _search.EndSearch();
1.540 + DragMode = true;
1.541 + CreateDragBitmap(drgevent.Data);
1.542 + base.OnDragEnter(drgevent);
1.543 + }
1.544 +
1.545 + protected override void OnDragLeave(EventArgs e)
1.546 + {
1.547 + DragMode = false;
1.548 + UpdateView();
1.549 + base.OnDragLeave(e);
1.550 + }
1.551 +
1.552 + protected override void OnDragDrop(DragEventArgs drgevent)
1.553 + {
1.554 + DragMode = false;
1.555 + UpdateView();
1.556 + base.OnDragDrop(drgevent);
1.557 + }
1.558 +
1.559 + #endregion
1.560 + }
1.561 +}