NodeCheckBox.cs :  » GUI » TreeViewAdv » Aga » Controls » Tree » NodeControls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » TreeViewAdv 
TreeViewAdv » Aga » Controls » Tree » NodeControls » NodeCheckBox.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Aga.Controls.Properties;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.ComponentModel;

namespace Aga.Controls.Tree.NodeControls{
  public class NodeCheckBox : InteractiveControl
  {
    public const int ImageSize = 13;

    private Bitmap _check;
    private Bitmap _uncheck;
    private Bitmap _unknown;

    #region Properties

    private bool _threeState;
    [DefaultValue(false)]
    public bool ThreeState
    {
      get { return _threeState; }
      set { _threeState = value; }
    }

    #endregion

    public NodeCheckBox()
      : this(string.Empty)
    {
    }

    public NodeCheckBox(string propertyName)
    {
      _check = Resources.check;
      _uncheck = Resources.uncheck;
      _unknown = Resources.unknown;
      DataPropertyName = propertyName;
      LeftMargin = 0;
    }

    public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
    {
      return new Size(ImageSize, ImageSize);
    }

    public override void Draw(TreeNodeAdv node, DrawContext context)
    {
      Rectangle bounds = GetBounds(node, context);
      CheckState state = GetCheckState(node);
      if (Application.RenderWithVisualStyles)
      {
        VisualStyleRenderer renderer;
        if (state == CheckState.Indeterminate)
          renderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.MixedNormal);
        else if (state == CheckState.Checked)
          renderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.CheckedNormal);
        else
          renderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.UncheckedNormal);
        renderer.DrawBackground(context.Graphics, new Rectangle(bounds.X, bounds.Y, ImageSize, ImageSize));
      }
      else
      {
        Image img;
        if (state == CheckState.Indeterminate)
          img = _unknown;
        else if (state == CheckState.Checked)
          img = _check;
        else
          img = _uncheck;
        context.Graphics.DrawImage(img, bounds.Location);
      }
    }

    protected virtual CheckState GetCheckState(TreeNodeAdv node)
    {
      object obj = GetValue(node);
      if (obj is CheckState)
        return (CheckState)obj;
      else if (obj is bool)
        return (bool)obj ? CheckState.Checked : CheckState.Unchecked;
      else
        return CheckState.Unchecked;
    }

    protected virtual void SetCheckState(TreeNodeAdv node, CheckState value)
    {
      if (VirtualMode)
      {
        SetValue(node, value);
        OnCheckStateChanged(node);
      }
      else
      {
        Type type = GetPropertyType(node);
        if (type == typeof(CheckState))
        {
          SetValue(node, value);
          OnCheckStateChanged(node);
        }
        else if (type == typeof(bool))
        {
          SetValue(node, value != CheckState.Unchecked);
          OnCheckStateChanged(node);
        }
      }
    }

    public override void MouseDown(TreeNodeAdvMouseEventArgs args)
    {
      if (args.Button == MouseButtons.Left && IsEditEnabled(args.Node))
      {
        DrawContext context = new DrawContext();
        context.Bounds = args.ControlBounds;
        Rectangle rect = GetBounds(args.Node, context);
        if (rect.Contains(args.ViewLocation))
        {
          CheckState state = GetCheckState(args.Node);
          state = GetNewState(state);
          SetCheckState(args.Node, state);
          Parent.UpdateView();
          args.Handled = true;
        }
      }
    }

    public override void MouseDoubleClick(TreeNodeAdvMouseEventArgs args)
    {
      args.Handled = true;
    }

    private CheckState GetNewState(CheckState state)
    {
      if (state == CheckState.Indeterminate)
        return CheckState.Unchecked;
      else if(state == CheckState.Unchecked)
        return CheckState.Checked;
      else 
        return ThreeState ? CheckState.Indeterminate : CheckState.Unchecked;
    }

    public override void KeyDown(KeyEventArgs args)
    {
      if (args.KeyCode == Keys.Space && EditEnabled)
      {
        Parent.BeginUpdate();
        try
        {
          if (Parent.CurrentNode != null)
          {
            CheckState value = GetNewState(GetCheckState(Parent.CurrentNode));
            foreach (TreeNodeAdv node in Parent.Selection)
              if (IsEditEnabled(node))
                SetCheckState(node, value);
          }
        }
        finally
        {
          Parent.EndUpdate();
        }
        args.Handled = true;
      }
    }

    public event EventHandler<TreePathEventArgs> CheckStateChanged;
    protected void OnCheckStateChanged(TreePathEventArgs args)
    {
      if (CheckStateChanged != null)
        CheckStateChanged(this, args);
    }

    protected void OnCheckStateChanged(TreeNodeAdv node)
    {
      TreePath path = this.Parent.GetPath(node);
      OnCheckStateChanged(new TreePathEventArgs(path));
    }

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.