1 | ///
|
---|
2 | /// This file is part of ILNumerics Community Edition.
|
---|
3 | ///
|
---|
4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
6 | ///
|
---|
7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
9 | /// the Free Software Foundation.
|
---|
10 | ///
|
---|
11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | /// GNU General Public License for more details.
|
---|
15 | ///
|
---|
16 | /// You should have received a copy of the GNU General Public License
|
---|
17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ///
|
---|
20 | /// In addition this software uses the following components and/or licenses:
|
---|
21 | ///
|
---|
22 | /// =================================================================================
|
---|
23 | /// The Open Toolkit Library License
|
---|
24 | ///
|
---|
25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
26 | ///
|
---|
27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
29 | /// in the Software without restriction, including without limitation the rights to
|
---|
30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
32 | /// so, subject to the following conditions:
|
---|
33 | ///
|
---|
34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
35 | /// copies or substantial portions of the Software.
|
---|
36 | ///
|
---|
37 | /// =================================================================================
|
---|
38 | ///
|
---|
39 |
|
---|
40 | using System;
|
---|
41 | using System.Collections.Generic;
|
---|
42 | using System.Text;
|
---|
43 | using System.Windows.Forms;
|
---|
44 | using System.Drawing;
|
---|
45 | using System.Configuration;
|
---|
46 | using System.Reflection;
|
---|
47 | using ILNumerics.Algorithms;
|
---|
48 | using System.Diagnostics;
|
---|
49 | using System.IO;
|
---|
50 |
|
---|
51 | namespace ILNumerics.Drawing {
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// Plot control, provides ILPanel, colorbar and title
|
---|
55 | /// </summary>
|
---|
56 | public class ILSubfigure : UserControl {
|
---|
57 |
|
---|
58 | #region attributes
|
---|
59 | private ILPanel m_panel;
|
---|
60 | private ILColorBar m_colorbar;
|
---|
61 | private ILBorderFitLabel m_title;
|
---|
62 | private bool m_isInitializing;
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | #region properties
|
---|
66 | public override System.Drawing.Color BackColor {
|
---|
67 | get {
|
---|
68 | return base.BackColor;
|
---|
69 | }
|
---|
70 | set {
|
---|
71 | base.BackColor = value;
|
---|
72 | m_panel.BackColor = value;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | /// <summary>
|
---|
76 | /// Gives access to the panel of the figure
|
---|
77 | /// </summary>
|
---|
78 | public ILPanel Panel {
|
---|
79 | get {
|
---|
80 | return m_panel;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | /// <summary>
|
---|
84 | /// Gives access to the colorbar of the figure
|
---|
85 | /// </summary>
|
---|
86 | public ILColorBar ColorBar {
|
---|
87 | get {
|
---|
88 | return m_colorbar;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | /// <summary>
|
---|
92 | /// Gives the title control
|
---|
93 | /// </summary>
|
---|
94 | public ILBorderFitLabel Title {
|
---|
95 | get {
|
---|
96 | return m_title;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | #endregion
|
---|
100 |
|
---|
101 | #region constructors
|
---|
102 | public ILSubfigure () {
|
---|
103 | m_isInitializing = true;
|
---|
104 | // setup trace listener
|
---|
105 | string logfile = System.Configuration.ConfigurationManager.AppSettings["logfile"];
|
---|
106 | if (!String.IsNullOrEmpty(logfile)) {
|
---|
107 | File.Delete(logfile);
|
---|
108 | DefaultTraceListener defTraceListener = new DefaultTraceListener();
|
---|
109 | Trace.Listeners.Add(defTraceListener);
|
---|
110 | defTraceListener.LogFileName = logfile;
|
---|
111 | Trace.WriteLine(String.Format("{0} - ILSubfigure.ctor()",DateTime.Now));
|
---|
112 | }
|
---|
113 | this.Dock = DockStyle.Fill;
|
---|
114 | m_panel = ILPanel.Create(getDefaultDeviceType());
|
---|
115 | m_panel.DrawInactive = true;
|
---|
116 | if (Trace.IndentLevel > 0)
|
---|
117 | Trace.TraceInformation("{0} - ILSubfigure.ctor() - panel created {1}",DateTime.Now,m_panel.GraphicDeviceType);
|
---|
118 | m_colorbar = new ILColorBar(m_panel.Colormap);
|
---|
119 | m_colorbar.RegisterRangeSource(m_panel.Graphs.Limits);
|
---|
120 | m_panel.ColormapChanged += new EventHandler(m_panel_ColormapChanged);
|
---|
121 | m_title = new ILBorderFitLabel();
|
---|
122 | m_panel.Graphs.CollectionChanged += new ILGraphCollectionChangedEvent(Graphs_CollectionChanged);
|
---|
123 | SetDefaults();
|
---|
124 | m_panel.Invalidate();
|
---|
125 | m_isInitializing = false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | void m_panel_ColormapChanged(object sender, EventArgs e) {
|
---|
129 | m_colorbar.Colormap = m_panel.Colormap;
|
---|
130 | m_colorbar.Invalidate();
|
---|
131 | }
|
---|
132 |
|
---|
133 | void Graphs_CollectionChanged(object sender, ILGraphCollectionChangedEventArgs args) {
|
---|
134 | switch (args.Reason) {
|
---|
135 | case GraphCollectionChangeReason.Added:
|
---|
136 | if (args.Graph.Type == GraphType.Plot2D) {
|
---|
137 | m_colorbar.Visible = false;
|
---|
138 | m_panel.ClipViewData = true;
|
---|
139 | } else if (args.Graph.Type == GraphType.Surf) {
|
---|
140 | m_colorbar.Visible = true;
|
---|
141 | m_panel.ClipViewData = false;
|
---|
142 | } else if (args.Graph.Type == GraphType.Imagesc) {
|
---|
143 | m_colorbar.Visible = false;
|
---|
144 | m_panel.ClipViewData = false;
|
---|
145 | }
|
---|
146 | break;
|
---|
147 | case GraphCollectionChangeReason.Deleted:
|
---|
148 | break;
|
---|
149 | case GraphCollectionChangeReason.Changed:
|
---|
150 | break;
|
---|
151 | default:
|
---|
152 | break;
|
---|
153 | }
|
---|
154 |
|
---|
155 | }
|
---|
156 | #endregion
|
---|
157 |
|
---|
158 | #region overrides
|
---|
159 | protected override void OnLayout(LayoutEventArgs e) {
|
---|
160 | if (m_isInitializing) return;
|
---|
161 | base.OnLayout(e);
|
---|
162 | #if TRACE
|
---|
163 | Trace.TraceInformation("{0},{1} ILSubfigure.OnLayout(), m_isInitializing is false",DateTime.Now,Environment.TickCount);
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | layoutChilds();
|
---|
167 | }
|
---|
168 | protected override void OnLoad(EventArgs e) {
|
---|
169 | base.OnLoad(e);
|
---|
170 | #if TRACE
|
---|
171 | Trace.TraceInformation("{0},{1} ILSubfigure.OnLoad ",DateTime.Now,Environment.TickCount);
|
---|
172 | #endif
|
---|
173 | SuspendLayout();
|
---|
174 | Controls.Add(m_colorbar);
|
---|
175 | Controls.Add(m_title);
|
---|
176 | Controls.Add(m_panel);
|
---|
177 | #if TRACE
|
---|
178 | Trace.TraceInformation("{0},{1} ILSubfigure.OnLoad ",DateTime.Now,Environment.TickCount);
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | ResumeLayout();
|
---|
182 | m_isInitializing = false;
|
---|
183 | if (DesignMode) {
|
---|
184 | #if TRACE
|
---|
185 | Trace.TraceInformation("{0},{1} ILSubfigure.OnLoad: DesignMode detected",DateTime.Now,Environment.TickCount);
|
---|
186 | #endif
|
---|
187 | try {
|
---|
188 | SetStyle(ControlStyles.Opaque, true);
|
---|
189 | SetStyle(ControlStyles.ResizeRedraw, true);
|
---|
190 | m_panel.Graphs.AddSurfGraph(ILMath.tosingle(ILSpecialData.sinc(14,20)));
|
---|
191 | #if TRACE
|
---|
192 | Trace.TraceInformation("{0},{1} ILSubfigure.OnLoad:DesignMode: SurfGraph added",DateTime.Now,Environment.TickCount);
|
---|
193 | #else
|
---|
194 | } catch (Exception) {
|
---|
195 | #endif
|
---|
196 | #if TRACE
|
---|
197 | } catch (Exception exc) {
|
---|
198 | Trace.TraceWarning("{0},{1} ILSubfigure.OnLoad:DesignMode: failure added surf graph: {2}",DateTime.Now,Environment.TickCount,exc.ToString());
|
---|
199 | #endif
|
---|
200 | }
|
---|
201 | }
|
---|
202 | Invalidate(true);
|
---|
203 | }
|
---|
204 | protected void SetDefaults () {
|
---|
205 | BackColor = System.Drawing.Color.FromArgb(250,250,250);
|
---|
206 | m_title.Text = "ILNumerics.Drawing Subfigure";
|
---|
207 | m_title.Visible = true;
|
---|
208 | m_title.Dock = DockStyle.Top;
|
---|
209 | m_title.BackColor = Color.White;
|
---|
210 | m_title.ForeColor = Color.DarkBlue;
|
---|
211 | m_colorbar.Dock = DockStyle.Left;
|
---|
212 | }
|
---|
213 | /// <summary>
|
---|
214 | /// Draws content of this subfigure into predefined bitmap
|
---|
215 | /// </summary>
|
---|
216 | /// <param name="bitmap">predefined bitmap to draw content into. The size must have been initialized according to 'bounds'.</param>
|
---|
217 | /// <param name="bounds">Rectangle specifying the region to be copied.</param>
|
---|
218 | public virtual new void DrawToBitmap(Bitmap bitmap, Rectangle bounds) {
|
---|
219 | base.DrawToBitmap(bitmap,bounds);
|
---|
220 | Bitmap tmpbmp = new Bitmap(m_panel.Size.Width,m_panel.Size.Height);
|
---|
221 | m_panel.DrawToBitmap(tmpbmp,new Rectangle(0,0,tmpbmp.Width,tmpbmp.Height));
|
---|
222 | // merge both bitmaps
|
---|
223 | using (Graphics gr = Graphics.FromImage( bitmap )) {
|
---|
224 | gr.DrawImage(tmpbmp,m_panel.Location);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | #endregion
|
---|
228 |
|
---|
229 | #region helper functions
|
---|
230 | private GraphicDeviceType getDefaultDeviceType () {
|
---|
231 | GraphicDeviceType ret = GraphicDeviceType.OpenGL;
|
---|
232 | // lets see, if a configuration for the app is found
|
---|
233 | string useD3D = ConfigurationManager.AppSettings["useDirect3D"];
|
---|
234 | if (String.IsNullOrEmpty(useD3D)) {
|
---|
235 | // look for an ILNumerics.Drawing.dll.config file and probe this for the setting
|
---|
236 | string path = Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
|
---|
237 | Configuration c = ConfigurationManager.OpenExeConfiguration(path);
|
---|
238 | KeyValueConfigurationElement kvelem = c.AppSettings.Settings["useDirect3D"];
|
---|
239 | if (kvelem != null)
|
---|
240 | useD3D = kvelem.Value;
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (String.IsNullOrEmpty(useD3D))
|
---|
244 | return ret;
|
---|
245 | if (useD3D.Trim() == "1") {
|
---|
246 | switch (Environment.OSVersion.Platform) {
|
---|
247 | case PlatformID.Unix:
|
---|
248 | System.Diagnostics.Debug.WriteLine("The Direct3D device is not available on Unix platform! Switching to OpenGL.");
|
---|
249 | break;
|
---|
250 | default:
|
---|
251 | ret = GraphicDeviceType.Direct3D;
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | return ret;
|
---|
256 | }
|
---|
257 |
|
---|
258 | private void layoutMovingDock(ref Rectangle crect, ILMovingDockPanel panel
|
---|
259 | ,int width ,int height) {
|
---|
260 | panel.Visible = false;
|
---|
261 | switch (panel.Dock) {
|
---|
262 | case DockStyle.Bottom:
|
---|
263 | panel.Height = height;
|
---|
264 | panel.Width = crect.Width;
|
---|
265 | panel.Top = crect.Top + crect.Height - panel.Height;
|
---|
266 | panel.Left = crect.X;
|
---|
267 | crect.Height -= panel.Height;
|
---|
268 | break;
|
---|
269 | case DockStyle.Left:
|
---|
270 | panel.Height = crect.Height;
|
---|
271 | panel.Width = width;
|
---|
272 | panel.Top = crect.Y;
|
---|
273 | panel.Left = crect.X;
|
---|
274 | crect.Width -= panel.Width;
|
---|
275 | crect.X += panel.Width;
|
---|
276 | break;
|
---|
277 | case DockStyle.None:
|
---|
278 | //panel.Top = (int)(ClientSize.Height / 4);
|
---|
279 | //panel.Left = (int)(ClientSize.Width / 4);
|
---|
280 | //panel.Width = width;
|
---|
281 | //panel.Height = height;
|
---|
282 | break;
|
---|
283 | case DockStyle.Right:
|
---|
284 | panel.Width = width;
|
---|
285 | panel.Height = crect.Height;
|
---|
286 | panel.Top = crect.Y;
|
---|
287 | panel.Left = crect.Left + crect.Width - panel.Width;
|
---|
288 | crect.Width -= panel.Width;
|
---|
289 | break;
|
---|
290 | case DockStyle.Top:
|
---|
291 | panel.Height = height;
|
---|
292 | panel.Width = crect.Width;
|
---|
293 | panel.Top = crect.Y;
|
---|
294 | panel.Left = crect.X;
|
---|
295 | crect.Height -= panel.Height;
|
---|
296 | crect.Y += panel.Height;
|
---|
297 | break;
|
---|
298 | default:
|
---|
299 | break;
|
---|
300 | }
|
---|
301 | panel.Visible = true;
|
---|
302 | }
|
---|
303 |
|
---|
304 | private void layoutChilds() {
|
---|
305 | Rectangle crect = this.ClientRectangle;
|
---|
306 | if (m_title.Visible) {
|
---|
307 | layoutMovingDock(ref crect, m_title, m_title.Padding.Horizontal + m_title.Font.Height, m_title.Padding.Vertical + m_title.Font.Height);
|
---|
308 | }
|
---|
309 | if (m_colorbar.Visible) {
|
---|
310 | layoutMovingDock(ref crect, m_colorbar, 100, 40);
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (m_panel != null && crect.Width - Padding.Vertical > 2 && crect.Height - Padding.Horizontal > 2) {
|
---|
314 | m_panel.Location = crect.Location;
|
---|
315 | m_panel.Size = crect.Size;
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | #endregion
|
---|
320 |
|
---|
321 | }
|
---|
322 | }
|
---|