Subversion Repositories SoapBoxCore

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 scott 1
#region "SoapBox.Core License"
2
/// <header module="SoapBox.Core"> 
3
/// Copyright (C) 2009 SoapBox Automation Inc., All Rights Reserved.
4
/// Contact: SoapBox Automation Licencing (license@soapboxautomation.com)
5
/// 
6
/// This file is part of SoapBox Core.
7
/// 
8
/// Commercial Usage
9
/// Licensees holding valid SoapBox Automation Commercial licenses may use  
10
/// this file in accordance with the SoapBox Automation Commercial License
11
/// Agreement provided with the Software or, alternatively, in accordance 
12
/// with the terms contained in a written agreement between you and
13
/// SoapBox Automation Inc.
14
/// 
15
/// GNU Lesser General Public License Usage
16
/// SoapBox Core is free software: you can redistribute it and/or modify 
17
/// it under the terms of the GNU Lesser General Public License
18
/// as published by the Free Software Foundation, either version 3 of the
19
/// License, or (at your option) any later version.
20
/// 
21
/// SoapBox Core is distributed in the hope that it will be useful, 
22
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
23
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
/// GNU Lesser General Public License for more details.
25
/// 
26
/// You should have received a copy of the GNU Lesser General Public License 
27
/// along with SoapBox Core. If not, see <http://www.gnu.org/licenses/>.
28
/// </header>
29
#endregion
30
 
31
using System;
32
using System.Collections.Generic;
33
using System.Linq;
34
using System.Text;
35
using System.ComponentModel;
36
using SoapBox.Utilities;
37
using System.Windows.Media.Imaging;
38
using System.Windows;
39
using System.Collections.ObjectModel;
40
 
41
namespace SoapBox.Core
42
{
43
    /// <summary>
44
    /// Helper class for implementing a TreeViewItem ViewModel
45
    /// </summary>
46
    public abstract class AbstractTreeViewItem : AbstractLabel, ITreeViewItem
47
    {
48
 
49
        #region " IsSelected "
50
        /// <summary>
51
        /// True if the item is selected, false otherwise.
52
        /// Calls OnIsSelectedChanged(), which can be overridden in the
53
        /// derived class to take an action when the status
54
        /// is toggled.
55
        /// </summary>
56
        public bool IsSelected
57
        {
58
            get
59
            {
60
                return m_IsSelected;
61
            }
62
            set
63
            {
64
                if (m_IsSelected != value)
65
                {
66
                    m_IsSelected = value;
67
                    NotifyPropertyChanged(m_IsSelectedArgs);
68
                    OnIsSelectedChanged();
69
                }
70
            }
71
        }
72
        private bool m_IsSelected = false;
73
        static readonly PropertyChangedEventArgs m_IsSelectedArgs =
74
            NotifyPropertyChangedHelper.CreateArgs<AbstractTreeViewItem>(o => o.IsSelected);
75
 
76
 
77
        /// <summary>
78
        /// This method is called if
79
        /// the user changes the IsSelected property. Override it in 
80
        /// the derived class to take an action when it changes.
81
        /// </summary>
82
        protected virtual void OnIsSelectedChanged() { }
83
 
84
        #endregion
85
 
86
        #region " IsExpanded "
87
        /// <summary>
88
        /// True if the item is expanded, false otherwise.
89
        /// </summary>
90
        public bool IsExpanded
91
        {
92
            get
93
            {
94
                return m_IsExpanded;
95
            }
96
            set
97
            {
98
                if (m_IsExpanded != value)
99
                {
100
                    m_IsExpanded = value;
101
                    NotifyPropertyChanged(m_IsExpandedArgs);
102
                }
103
            }
104
        }
105
        private bool m_IsExpanded = false;
106
        static readonly PropertyChangedEventArgs m_IsExpandedArgs =
107
            NotifyPropertyChangedHelper.CreateArgs<AbstractTreeViewItem>(o => o.IsExpanded);
108
 
109
        #endregion
110
 
111
        #region " Items "
112
        public IEnumerable<ITreeViewItem> Items
113
        {
114
            get
115
            {
116
                return m_Items;
117
            }
118
            protected set
119
            {
120
                if (m_Items != value)
121
                {
122
                    m_Items = value;
123
                    NotifyPropertyChanged(m_ItemsArgs);
124
                }
125
            }
126
        }
127
        protected IEnumerable<ITreeViewItem> m_Items = null;
128
        static readonly PropertyChangedEventArgs m_ItemsArgs =
129
            NotifyPropertyChangedHelper.CreateArgs<AbstractTreeViewItem>(o => o.Items);
130
        #endregion
131
 
132
        #region " Icons "
133
        public ObservableCollection<object> Icons
134
        {
135
            get
136
            {
137
                return m_Icons;
138
            }
139
            protected set
140
            {
141
                if (m_Icons != value)
142
                {
143
                    m_Icons = value;
144
                    NotifyPropertyChanged(m_IconsArgs);
145
                }
146
            }
147
        }
148
        protected ObservableCollection<object> m_Icons = new ObservableCollection<object>();
149
        static readonly PropertyChangedEventArgs m_IconsArgs =
150
            NotifyPropertyChangedHelper.CreateArgs<AbstractTreeViewItem>(o => o.Icons);
151
 
152
        /// <summary>
153
        /// This is a helper function so you can add an Icon directly
154
        /// from a Bitmap, such as one from a resources file.
155
        /// Returns a reference to the new Icon in the Icons collection,
156
        /// so you can remove it later if you want.
157
        /// </summary>
158
        /// <param name="value"></param>
159
        public System.Windows.Controls.Image AddIconFromBitmap(System.Drawing.Bitmap value)
160
        {
161
            BitmapSource b = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
162
                value.GetHbitmap(),
163
                IntPtr.Zero,
164
                Int32Rect.Empty,
165
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
166
            System.Windows.Controls.Image img = new System.Windows.Controls.Image();
167
            img.Source = b;
168
            Icons.Add(img);
169
            return img;
170
        }
171
        #endregion
172
 
173
 
174
    }
175
}