~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to samples/ComponentInspector/ComponentInspector.Core/Src/ObjectBrowser/GuiDesigner/ImagePanel.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// <file>
 
2
//       <copyright see="prj:///doc/copyright.txt"/>
 
3
//       <license see="prj:///doc/license.txt"/>
 
4
//       <owner name="Oakland Software Incorporated" email="general@oaklandsoftware.com"/>
 
5
//       <version>$Revision$</version>
 
6
// </file>
 
7
 
 
8
using System;
 
9
using System.ComponentModel.Design;
 
10
using System.Drawing;
 
11
using System.Windows.Forms;
 
12
 
 
13
using ICSharpCode.Core;
 
14
using NoGoop.Controls;
 
15
using NoGoop.Obj;
 
16
using NoGoop.ObjBrowser.GuiDesigner;
 
17
 
 
18
namespace NoGoop.ObjBrowser
 
19
{
 
20
        internal class ImagePanel : Panel
 
21
        {
 
22
                Panel             _wrapperPanel;
 
23
                Label             _checkLabel;
 
24
                CheckBox          _designCheck;
 
25
 
 
26
                Panel             _designPanel;
 
27
                Panel             _nonDesignPanel;
 
28
                Panel             _desLabelPanel = new Panel();
 
29
 
 
30
                internal static ImagePanel CreateImagePanel(int width, bool showDesignModeCheckButton)
 
31
                {
 
32
                        ObjectBrowser objB = ObjectBrowser.ObjBrowser;
 
33
 
 
34
                        ImagePanel panel = new ImagePanel();
 
35
                        panel._nonDesignPanel = new Panel();
 
36
                        panel._nonDesignPanel.Name = "Non-Design Surface";
 
37
                        panel._nonDesignPanel.Dock = DockStyle.Fill;
 
38
 
 
39
                        panel._designPanel = new Panel();
 
40
                        panel._designPanel.Name = "Design Surface";
 
41
                        panel._designPanel.Dock = DockStyle.Fill;
 
42
 
 
43
                        panel.WrapImagePanel(width, showDesignModeCheckButton);
 
44
                        return panel;
 
45
                }
 
46
                
 
47
                internal Panel DesignPanel {
 
48
                        get {
 
49
                                return _designPanel;
 
50
                        }
 
51
                }
 
52
                
 
53
                internal Panel WrapperPanel {
 
54
                        get {
 
55
                                return _wrapperPanel;
 
56
                        }
 
57
                }
 
58
 
 
59
                void WrapImagePanel(int width, bool showDesignModeCheckButton)
 
60
                {
 
61
                        // Create the label for the check box to control design time
 
62
                        _desLabelPanel = new Panel();
 
63
                        _desLabelPanel.Dock = DockStyle.Left;
 
64
                        _desLabelPanel.Width = width;
 
65
                        _desLabelPanel.Height = TreeListPanel.BASE_HEADER_HEIGHT;
 
66
 
 
67
                        if (showDesignModeCheckButton) {
 
68
                                _checkLabel = new Label();
 
69
                                _checkLabel.Dock = DockStyle.Left;
 
70
                                _checkLabel.Text = StringParser.Parse("${res:ComponentInspector.ImagePanel.DesignModeLabel}");
 
71
                                _checkLabel.AutoSize = true;
 
72
                                _desLabelPanel.Controls.Add(_checkLabel);
 
73
 
 
74
                                _designCheck = new CheckBox();
 
75
                                _designCheck.Checked = true;
 
76
                                _designCheck.Dock = DockStyle.Left;
 
77
                        
 
78
                                // This does not work because the checkbox alignment of the text
 
79
                                // does not match that of text outside of the checkbox, sigh
 
80
                                //_designCheck.Text = "Design mode";
 
81
                                //_designCheck.TextAlign = ContentAlignment.TopRight;
 
82
                                _designCheck.CheckAlign = ContentAlignment.TopLeft;
 
83
                                _designCheck.Width = 15;
 
84
                                _designCheck.CheckedChanged += new EventHandler(DesignChecked);
 
85
                                _desLabelPanel.Controls.Add(_designCheck);
 
86
                        }
 
87
 
 
88
                        Label panelName = new Label();
 
89
                        panelName.Dock = DockStyle.Right;
 
90
                        panelName.Text = StringParser.Parse("${res:ComponentInspector.ImagePanel.ControlDesignSurfaceLabel}");
 
91
                        panelName.AutoSize = true;
 
92
                        _desLabelPanel.Controls.Add(panelName);
 
93
 
 
94
                        _wrapperPanel = new Panel();
 
95
                        _wrapperPanel.Width = width;
 
96
                        _wrapperPanel.Controls.Add(_designPanel);
 
97
                        _wrapperPanel.BorderStyle = BorderStyle.Fixed3D;
 
98
                        new PanelLabel(_wrapperPanel, _desLabelPanel);
 
99
                }
 
100
 
 
101
                void SwitchWindowTarget(Control con)
 
102
                {
 
103
                        IWindowTarget wt;
 
104
 
 
105
                        foreach (Control c in con.Controls) {
 
106
                                if (c.Site is DesignerSite) {
 
107
                                        if (_designCheck.Checked) {
 
108
                                                wt = ((DesignerSite)c.Site).DesignWindowTarget;
 
109
                                                if (wt != null) {
 
110
                                                        // The designer gets the windows events
 
111
                                                        c.WindowTarget = wt;
 
112
                                                }
 
113
                                        } else {
 
114
                                                wt = ((DesignerSite)c.Site).OrigWindowTarget;
 
115
                                                if (wt != null) {
 
116
                                                        // So the control gets the windows events 
 
117
                                                        // instead of the designer
 
118
                                                        c.WindowTarget = wt;
 
119
                                                }
 
120
                                        }
 
121
                                }
 
122
                                SwitchWindowTarget(c);
 
123
                        }
 
124
                }
 
125
 
 
126
                void DesignChecked(Object sender, EventArgs args)
 
127
                {
 
128
                        Control selUIService = DesignerHost.Host.SelectionUIService;
 
129
 
 
130
                        // Swap the design/non design panels by rebuilding the control
 
131
                        // list for the wrapper panel.
 
132
                        ObjectBrowser.DesignerHost.DesignMode = _designCheck.Checked;
 
133
                        _wrapperPanel.Controls.Clear();
 
134
 
 
135
                        if (_designCheck.Checked) {
 
136
                                // Move the controls from the non-design panel to the 
 
137
                                // design panel
 
138
                                for (int i = 0; i < _nonDesignPanel.Controls.Count; ) {
 
139
                                        Control c = (Control)_nonDesignPanel.Controls[i];
 
140
                                        _nonDesignPanel.Controls.Remove(c);
 
141
 
 
142
                                        // This Control could have been added to the design 
 
143
                                        // surface while it was not in design mode
 
144
                                        if (c.Site == null) {
 
145
                                                IDesigner designer = DesignerHost.Host.GetDesigner(c);
 
146
                                                if (designer != null)
 
147
                                                        designer.Initialize(c);
 
148
                                        }
 
149
 
 
150
                                        _designPanel.Controls.Add(c);
 
151
                                }
 
152
 
 
153
                                SwitchWindowTarget(_designPanel);
 
154
                                _wrapperPanel.Controls.Add(_designPanel);
 
155
                        } else {
 
156
                                // Move the controls from the design panel to the 
 
157
                                // non-design panel
 
158
                                for (int i = 0; i < _designPanel.Controls.Count; ) {
 
159
                                        Control c = (Control)_designPanel.Controls[i];
 
160
                                        if (selUIService.Equals(c)) {
 
161
                                                i++;
 
162
                                                continue;
 
163
                                        }
 
164
                                        _designPanel.Controls.Remove(c);
 
165
                                        try {
 
166
                                                _nonDesignPanel.Controls.Add(c);
 
167
                                        } catch (Exception ex) {
 
168
                                                ErrorDialog.Show(ex,
 
169
                                                                                 "Error adding control " + c + " to "
 
170
                                                                                 + "non-design panel",
 
171
                                                                                 MessageBoxIcon.Warning);
 
172
                                                i++;
 
173
                                        }
 
174
                                }
 
175
                                SwitchWindowTarget(_nonDesignPanel);
 
176
                                _wrapperPanel.Controls.Add(_nonDesignPanel);
 
177
                        }
 
178
                        ResetSize(selUIService);
 
179
 
 
180
                        new PanelLabel(_wrapperPanel, _desLabelPanel);
 
181
                }
 
182
 
 
183
                internal void ResetSize(Control selUIService)
 
184
                {
 
185
                        if (_designCheck == null || _designCheck.Checked) {
 
186
                                selUIService.Height = _designPanel.ClientSize.Height;
 
187
                                selUIService.Width = _designPanel.ClientSize.Width;
 
188
                        } else {
 
189
                                selUIService.Height = 0;
 
190
                                selUIService.Width = 0;
 
191
                        }
 
192
                }
 
193
 
 
194
                // This is used to add a control to the design surface
 
195
                // when it was added to the object tree, this is not used
 
196
                // when the control is dragged directly to the design surface
 
197
                internal void AddControl(ObjectInfo objInfo, Control control)
 
198
                {
 
199
                        // For some reason, property grid gets an error when
 
200
                        // initially associated with the image panel, associated
 
201
                        // it with another panel first.
 
202
                        //if (control is PropertyGrid)
 
203
                        //      ObjectBrowser._testPanel.Controls.Add(control);
 
204
 
 
205
                        IDesigner designer = null;
 
206
                        DesignerHost host = DesignerHost.Host;
 
207
                        if (host.DesignMode) {
 
208
                                host.AddingControls = true;
 
209
                                designer = host.GetDesigner(control, objInfo);
 
210
                                if (designer != null)
 
211
                                        designer.Initialize(control);
 
212
 
 
213
                                // If no designer, can't add it to design surface
 
214
                                if (designer != null)
 
215
                                        _designPanel.Controls.Add(control);
 
216
                                host.AddingControls = false;
 
217
                        } else {
 
218
                                _nonDesignPanel.Controls.Add(control);
 
219
                        }
 
220
 
 
221
 
 
222
                        // FIXME - Hack
 
223
                        //if (control is PropertyGrid)
 
224
                        //      designer.Initialize(control);
 
225
                }
 
226
 
 
227
                // returns true if the control is found
 
228
                bool RemoveControlFromControl(Control parent, Control control)
 
229
                {
 
230
                        if (parent.Controls.Contains(control)) {
 
231
                                parent.Controls.Remove(control);
 
232
                                return true;
 
233
                        }
 
234
 
 
235
                        foreach (Control c in parent.Controls) {
 
236
                                if (RemoveControlFromControl(c, control))
 
237
                                        return true;
 
238
                        }
 
239
                        return false;
 
240
                }
 
241
 
 
242
                internal void RemoveControl(Control control)
 
243
                {
 
244
                        RemoveControlFromControl(_designPanel, control);
 
245
                        RemoveControlFromControl(_nonDesignPanel, control);
 
246
                }
 
247
        }
 
248
}