1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
//
// CodeFormattingPolicyPanel.cs
//
// Author:
// Mike Krüger <mkrueger@novell.com>
//
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using Gtk;
using MonoDevelop.Core;
using MonoDevelop.Core.Serialization;
using MonoDevelop.Projects.Gui.Dialogs;
using MonoDevelop.Projects.Text;
using MonoDevelop.Projects;
namespace MonoDevelop.Ide.CodeFormatting
{
public class TypedCodeFormattingPolicyPanelWidget<T> : CodeFormattingPolicyPanelWidget where T : class, IEquatable<T>, new ()
{
T settings;
CodeFormatDescription description;
Gtk.TreeStore store;
TreeModel model;
TreeIter iter;
CodeFormatOption option;
public TypedCodeFormattingPolicyPanelWidget ()
{
}
void UpdateExample ()
{
IPrettyPrinter printer = TextFileService.GetPrettyPrinter (description.MimeType);
if (printer == null)
return;
DotNetProject parent = new DotNetProject ();
parent.Policies.Set (settings);
texteditor1.Document.Text = printer.FormatText (parent, description.MimeType, texteditor1.Document.Text);
}
protected override void HandleChanged (object sender, EventArgs e)
{
CodeFormatType type = description.GetCodeFormatType (settings, option);
int a = ComboboxValue.Active;
if (type == null || a < 0 || a >= type.Values.Count)
return;
KeyValuePair<string, string> val = type.Values[a];
description.SetValue (settings, option, val.Key);
store.SetValue (iter, valueColumn, val.Key);
store.SetValue (iter, valueDisplayTextColumn, GettextCatalog.GetString (val.Value));
UpdateExample ();
}
public void SetFormat (CodeFormatDescription description, T settings)
{
this.description = description;
this.settings = settings;
options.ShowLineNumberMargin = false;
options.ShowFoldMargin = false;
options.ShowIconMargin = false;
options.ShowInvalidLines = false;
options.ShowSpaces = options.ShowTabs = options.ShowEolMarkers = false;
options.ColorScheme = PropertyService.Get ("ColorScheme", "Default");
texteditor1.Options = options;
texteditor1.Document.ReadOnly = true;
texteditor1.Document.MimeType = description.MimeType;
while (NotebookCategories.NPages > 0) {
NotebookCategories.RemovePage (0);
}
if (description != null) {
foreach (CodeFormatCategory category in description.SubCategories) {
AddCategoryPage (category);
}
}
NotebookCategories.ShowAll ();
}
const int keyColumn = 0;
const int valueColumn = 1;
const int valueDisplayTextColumn = 2;
const int objectColumn = 3;
public T Settings {
get {
return settings;
}
}
void AppendCategory (Gtk.TreeStore store, TreeIter iter, CodeFormatCategory category)
{
TreeIter categoryIter = iter.Equals (TreeIter.Zero)
? store.AppendValues (GettextCatalog.GetString (category.DisplayName), null, null, category)
: store.AppendValues (iter, GettextCatalog.GetString (category.DisplayName), null, null, category);
foreach (CodeFormatOption option in category.Options) {
KeyValuePair<string, string> val = description.GetValue (settings, option);
store.AppendValues (categoryIter,
GettextCatalog.GetString (option.DisplayName),
val.Key,
GettextCatalog.GetString (val.Value),
option);
}
foreach (CodeFormatCategory s in category.SubCategories) {
AppendCategory (store, categoryIter, s);
}
}
void AddCategoryPage (CodeFormatCategory category)
{
Gtk.Label label = new Gtk.Label (GettextCatalog.GetString (category.DisplayName));
Gtk.TreeStore store = new Gtk.TreeStore (typeof (string), typeof (string), typeof (string), typeof (object));
foreach (CodeFormatCategory cat in category.SubCategories) {
AppendCategory (store, TreeIter.Zero, cat);
}
Gtk.TreeView tree = new Gtk.TreeView (store);
tree.AppendColumn (GettextCatalog.GetString ("Key"), new CellRendererText (), "text", keyColumn);
tree.AppendColumn (GettextCatalog.GetString ("Value"), new CellRendererText (), "text", valueDisplayTextColumn);
tree.Selection.Changed += TreeSelectionChanged;
ScrolledWindow sw = new ScrolledWindow ();
sw.Child = tree;
NotebookCategories.AppendPage (sw, label);
}
void TreeSelectionChanged (object sender, EventArgs e)
{
Gtk.TreeSelection treeSelection = (Gtk.TreeSelection)sender;
if (treeSelection.GetSelected (out model, out iter)) {
option = model.GetValue (iter, objectColumn) as CodeFormatOption;
this.store = model as TreeStore;
if (option == null) {
texteditor1.Document.Text = "";
// comboboxentryValue.Sensitive = false;
return;
}
ComboboxValue.Changed -= HandleChanged;
// comboboxentryValue.Sensitive = true;
CodeFormatType type = description.GetCodeFormatType (settings, option);
texteditor1.Document.Text = option.Example;
comboBoxStore.Clear ();
int active = 0, i = 0;
KeyValuePair<string, string> curValue = description.GetValue (settings, option);
foreach (KeyValuePair<string, string> v in type.Values) {
if (v.Key == curValue.Key)
active = i;
comboBoxStore.AppendValues (v.Key, GettextCatalog.GetString (v.Value));
i++;
}
ComboboxValue.Active = active;
ComboboxValue.Changed += HandleChanged;
UpdateExample ();
}
}
}
[System.ComponentModel.ToolboxItem(true)]
public partial class CodeFormattingPolicyPanelWidget : Gtk.Bin
{
protected ListStore comboBoxStore;
protected Mono.TextEditor.TextEditor texteditor1 = new Mono.TextEditor.TextEditor ();
protected Mono.TextEditor.TextEditorOptions options = new Mono.TextEditor.TextEditorOptions ();
protected Notebook NotebookCategories {
get {
return notebookCategories;
}
}
protected ComboBox ComboboxValue {
get {
return comboboxValue;
}
}
public CodeFormattingPolicyPanelWidget ()
{
this.Build();
checkbuttonWhiteSpaces.Toggled += CheckbuttonWhiteSpacesToggled;
comboboxValue.Clear ();
Gtk.CellRendererText ctx = new Gtk.CellRendererText ();
comboboxValue.PackStart (ctx, true);
comboboxValue.AddAttribute (ctx, "text", 1);
comboBoxStore = new ListStore (typeof (string), typeof (string));
comboboxValue.Model = comboBoxStore;
comboboxValue.Changed += HandleChanged;
scrolledwindow2.Child = texteditor1;
ShowAll ();
}
protected virtual void HandleChanged (object sender, EventArgs e)
{
}
void CheckbuttonWhiteSpacesToggled (object sender, EventArgs e)
{
options.ShowSpaces = options.ShowTabs = options.ShowEolMarkers = checkbuttonWhiteSpaces.Active;
this.texteditor1.QueueDraw ();
}
}
}
|