Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CodeFormattingPolicyPanelWidget.cs « MonoDevelop.Ide.CodeFormatting « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63937a8c23fcf1a51b157cfba911de8facf7f8fb (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// 
// 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;
using MonoDevelop.Core.Gui;



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;
		
		CellRendererPixbuf pixbufCellRenderer;
		
		public TypedCodeFormattingPolicyPanelWidget ()
		{
			store = new Gtk.TreeStore (typeof (string), typeof (string), typeof (string), typeof (object), typeof (string), typeof(bool), typeof(bool), typeof(bool));
			
			TreeViewColumn column = new TreeViewColumn ();
			
			// pixbuf column
			pixbufCellRenderer = new CellRendererPixbuf ();
			column.PackStart (pixbufCellRenderer, false);
			column.SetCellDataFunc (pixbufCellRenderer, new Gtk.TreeCellDataFunc (RenderIcon));
			
			// text column
			CellRendererText cellRendererText = new CellRendererText ();
			cellRendererText.Ypad = 1;
			column.PackStart (cellRendererText, true);
			column.SetAttributes (cellRendererText, "text", keyColumn);
			
			TreeviewCategories.AppendColumn (column);
			
			column = new TreeViewColumn ();
			CellRendererCombo cellRendererCombo = new CellRendererCombo ();
			cellRendererCombo.Ypad = 1;
			cellRendererCombo.Mode = CellRendererMode.Editable;
			cellRendererCombo.TextColumn = 1;
			cellRendererCombo.Model = comboBoxStore;
			cellRendererCombo.HasEntry = false;
			cellRendererCombo.Editable = true;
			column.PackStart (cellRendererCombo, false);
			column.SetAttributes (cellRendererCombo, "markup", valueDisplayTextColumn, "visible", comboVisibleColumn);
			
			CellRendererToggle cellRendererToggle = new CellRendererToggle ();
			cellRendererToggle.Ypad = 1;
			cellRendererToggle.Toggled += CellRendererToggleToggled;
			column.PackStart (cellRendererToggle, false);
			column.SetAttributes (cellRendererToggle, "active", toggleColumn, "visible", toggleVisibleColumn);
			
			TreeviewCategories.AppendColumn (column);
			
			cellRendererCombo.EditingStarted += delegate(object o, EditingStartedArgs args) {
				CodeFormatType type = description.GetCodeFormatType (settings, option);
				comboBoxStore.Clear ();
				foreach (KeyValuePair<string, string> v in type.Values) {
					comboBoxStore.AppendValues (v.Key, GettextCatalog.GetString (v.Value));
				}
			};
			
			cellRendererCombo.Edited += delegate(object o, EditedArgs args) {
				CodeFormatType type = description.GetCodeFormatType (settings, option);
				foreach (KeyValuePair<string, string> v in type.Values) {
					if (args.NewText == GettextCatalog.GetString (v.Value)) {
						description.SetValue (settings, option, v.Key);
						TreeIter iter;
						if (store.GetIterFromString (out iter, args.Path)) {
							store.SetValue (iter, valueColumn, v.Key);
							store.SetValue (iter, valueDisplayTextColumn, args.NewText);
						}
						break;
					}
				}
				UpdateExample ();
			};
			
			TreeviewCategories.HeadersVisible = false;
			
			TreeviewCategories.Selection.Changed += TreeSelectionChanged;
			TreeviewCategories.Model = store;
		}

		void CellRendererToggleToggled (object o, ToggledArgs args)
		{
			TreeIter iter;
			if (store.GetIterFromString (out iter, args.Path)) {
				CodeFormatOption option = (CodeFormatOption) store.GetValue (iter, objectColumn);
				bool value = !(bool) store.GetValue (iter, toggleColumn);
				description.SetValue (settings, option, value ? "True" : "False");
				store.SetValue (iter, toggleColumn, value);
				UpdateExample ();
			}
			
		}
		
		void RenderIcon (TreeViewColumn col, CellRenderer cell, TreeModel model, TreeIter iter) 
		{
			object o = store.GetValue (iter, objectColumn);
			if (o is CodeFormatCategory) {
				pixbufCellRenderer.Pixbuf = ImageService.GetPixbuf (TreeviewCategories.GetRowExpanded (store.GetPath (iter)) ? MonoDevelop.Core.Gui.Stock.OpenFolder : MonoDevelop.Core.Gui.Stock.ClosedFolder, IconSize.Menu);
			} else {
				pixbufCellRenderer.Pixbuf = ImageService.GetPixbuf (MonoDevelop.Core.Gui.Stock.Property, IconSize.Menu);
			}
		}
		
		void UpdateExample ()
		{
			IPrettyPrinter printer = TextFileService.GetPrettyPrinter (description.MimeType);
			if (printer == null)
				return;
			DotNetAssemblyProject parent = new DotNetAssemblyProject ();
			parent.Policies.Set<T> (settings, description.MimeType);
			texteditor1.Document.Text  = printer.FormatText (parent, description.MimeType, texteditor1.Document.Text);
		}
		
		protected override void HandleChanged (object sender, EditedArgs e)
		{
		}
		
		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");
			options.Zoom = 0.8;
			texteditor1.Options = options;
			texteditor1.Document.ReadOnly = true;
			texteditor1.Document.MimeType = description.MimeType;
			store.Clear ();
			
			if (description != null) {
				foreach (CodeFormatCategory category in description.SubCategories) {
					AppendCategory (store, TreeIter.Zero, category);
				}
			}
			TreeviewCategories.ShowAll ();
		}
		
		const int keyColumn   = 0;
		const int valueColumn = 1;
		const int valueDisplayTextColumn = 2;
		const int objectColumn = 3;
		const int iconColumn = 4;
		const int toggleColumn = 5;
		const int toggleVisibleColumn = 6;
		const int comboVisibleColumn = 7;

		public new 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) {
				CodeFormatType type = description.GetCodeFormatType (settings, option);
				KeyValuePair<string, string> val = description.GetValue (settings, option);
				bool isBool = type.Name == "Bool";
				bool boolVal = isBool && val.Value == "True";
				store.AppendValues (categoryIter, 
				                    GettextCatalog.GetString (option.DisplayName), 
				                    val.Key,
				                    GettextCatalog.GetString (val.Value), 
				                    option,
				                    null,
				                    boolVal,
				                    isBool,
				                    !isBool);
			}
			foreach (CodeFormatCategory s in category.SubCategories) {
				AppendCategory (store, categoryIter, s);
			}
		}
		
		/*
		void AddCategoryPage (CodeFormatCategory category)
		{
			Gtk.Label label = new Gtk.Label (GettextCatalog.GetString (category.DisplayName));
			
			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);
			
			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 = "";
					return;
				}
				CodeFormatType type = description.GetCodeFormatType (settings, option);
				texteditor1.Document.Text = option.Example;
				
				comboBoxStore.Clear ();
				foreach (KeyValuePair<string, string> v in type.Values) {
				 	comboBoxStore.AppendValues (v.Key, GettextCatalog.GetString (v.Value));
				}
				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 Gtk.TreeView TreeviewCategories {
			get {
				return treeviewCategories;
			}
		}
		
		public CodeFormattingPolicyPanelWidget ()
		{
			this.Build();
			checkbuttonWhiteSpaces.Toggled += CheckbuttonWhiteSpacesToggled;
			comboBoxStore = new ListStore (typeof (string), typeof (string));
		/*	comboboxValue.Clear ();
			Gtk.CellRendererText ctx = new Gtk.CellRendererText ();
			comboboxValue.PackStart (ctx, true);
			comboboxValue.AddAttribute (ctx, "text", 1);
			
			
			comboboxValue.Model = comboBoxStore;
			
			comboboxValue.Changed += HandleChanged;*/
			scrolledwindow2.Child = texteditor1;
			ShowAll ();
		}
		
		protected virtual void HandleChanged (object sender, EditedArgs e)
		{
		}
		
		void CheckbuttonWhiteSpacesToggled (object sender, EventArgs e)
		{
			options.ShowSpaces = options.ShowTabs = options.ShowEolMarkers = checkbuttonWhiteSpaces.Active;
			this.texteditor1.QueueDraw ();
		}
		
		
	}
}