~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.SourceEditor/MonoDevelop.SourceEditor.Gui.OptionPanels/SyntaxHighlightingPanel.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using System;
2
 
using System.IO;
3
 
using System.Text;
4
 
using System.Collections;
5
 
using System.Collections.Generic;
6
 
 
7
 
using Gtk;
8
 
using Pango;
9
 
using GtkSourceView;
10
 
 
11
 
using MonoDevelop.Components;
12
 
 
13
 
using MonoDevelop.Core;
14
 
using MonoDevelop.Core.Gui.Dialogs;
15
 
 
16
 
using MonoDevelop.Ide.Gui.Content;
17
 
 
18
 
namespace MonoDevelop.SourceEditor.Gui.OptionPanels
19
 
{
20
 
        class SyntaxHighlightingPanel : AbstractOptionPanel
21
 
        {
22
 
                SyntaxHighlightingPanelWidget widget;
23
 
                
24
 
                public override void LoadPanelContents()
25
 
                {
26
 
                        Add (widget = new SyntaxHighlightingPanelWidget ());
27
 
                }
28
 
                
29
 
                public override bool StorePanelContents()
30
 
                {
31
 
                        widget.Store ();
32
 
                        return true;
33
 
                }
34
 
        
35
 
                class SyntaxHighlightingPanelWidget : GladeWidgetExtract 
36
 
                {       
37
 
                        [Glade.Widget] CheckButton enableSyntaxHighlighting;
38
 
                        [Glade.Widget] ComboBox sourceLanguages;
39
 
                        [Glade.Widget] Gtk.TreeView stylesTreeView;
40
 
                        [Glade.Widget] ToggleButton boldToggle;
41
 
                        [Glade.Widget] ToggleButton italicToggle;
42
 
                        [Glade.Widget] ToggleButton underlineToggle;
43
 
                        [Glade.Widget] ToggleButton strikeToggle;
44
 
                        [Glade.Widget] ColorButton fgColorButton;
45
 
                        [Glade.Widget] ColorButton bgColorButton;
46
 
                        [Glade.Widget] Button restoreDefaultButton;
47
 
                        [Glade.Widget] VBox childrenVBox;
48
 
                        [Glade.Widget] CheckButton checkColor;
49
 
                        [Glade.Widget] CheckButton checkBackground;
50
 
 
51
 
                        SourceViewService svs = (SourceViewService) ServiceManager.GetService (typeof (SourceViewService));
52
 
                        SourceLanguage currentLanguage;
53
 
                        SourceTagStyle currentStyle;
54
 
                        string styleid;
55
 
                        
56
 
                        Dictionary<SourceLanguage,Dictionary<string,SourceTagStyle>> changes = new Dictionary<SourceLanguage,Dictionary<string,SourceTagStyle>> ();
57
 
                        
58
 
                        public SyntaxHighlightingPanelWidget () :  base ("EditorBindings.glade", "SyntaxHighlightingPanel")
59
 
                        {
60
 
                                enableSyntaxHighlighting.Active = TextEditorProperties.SyntaxHighlight;
61
 
                                
62
 
                                // add available sourceLanguages
63
 
                                ListStore store = new ListStore (typeof (string));
64
 
                                foreach (SourceLanguage sl in svs.AvailableLanguages)
65
 
                                        store.AppendValues (sl.Name);
66
 
                                store.SetSortColumnId (0, SortType.Ascending);
67
 
                                sourceLanguages.Model = store;
68
 
 
69
 
                                CellRendererText cr = new CellRendererText ();
70
 
                                sourceLanguages.PackStart (cr, true);
71
 
                                sourceLanguages.AddAttribute (cr, "text", 0);
72
 
                                sourceLanguages.Active = 0;
73
 
 
74
 
                                stylesTreeView.AppendColumn ("styles", new CellRendererText (), "text", 0);
75
 
                                stylesTreeView.Selection.Changed += new EventHandler (OnStyleChanged);
76
 
                        }
77
 
 
78
 
                        public void Store ()
79
 
                        {
80
 
                                TextEditorProperties.SyntaxHighlight = enableSyntaxHighlighting.Active;
81
 
                                foreach (KeyValuePair<SourceLanguage,Dictionary<string,SourceTagStyle>> lang in changes) {
82
 
                                        foreach (KeyValuePair<string,SourceTagStyle> style in lang.Value) {
83
 
                                                lang.Key.SetTagStyle (style.Key, style.Value);
84
 
                                        }
85
 
                                }
86
 
                        }
87
 
 
88
 
                        void SetCurrentLanguage (string name)
89
 
                        {
90
 
                                currentLanguage = svs.FindLanguage (name);
91
 
                                SetTreeValues ();
92
 
                        }
93
 
 
94
 
                        void SetSourceTagStyle ()
95
 
                        {
96
 
                                SourceTagStyle sts = currentStyle;
97
 
                                boldToggle.Active = sts.Bold;
98
 
                                italicToggle.Active = sts.Italic;
99
 
                                underlineToggle.Active = sts.Underline;
100
 
                                strikeToggle.Active = sts.Strikethrough;
101
 
                                fgColorButton.Color = sts.Foreground;
102
 
                                bgColorButton.Color = sts.Background;
103
 
                                checkColor.Active = (sts.Mask & 2) != 0;
104
 
                                checkBackground.Active = (sts.Mask & 1) != 0;
105
 
                                fgColorButton.Sensitive = checkColor.Active;
106
 
                                bgColorButton.Sensitive = checkBackground.Active;
107
 
                                restoreDefaultButton.Sensitive = !sts.IsDefault;
108
 
                        }
109
 
 
110
 
                        void SetTreeValues ()
111
 
                        {
112
 
                                // name, id
113
 
                                ListStore store = new ListStore (typeof (string), typeof (string));
114
 
                                foreach (SourceTag t in currentLanguage.Tags)
115
 
                                        store.AppendValues (t.Name, t.Id);
116
 
                                stylesTreeView.Model = store;
117
 
 
118
 
                                TreeIter first;
119
 
                                store.GetIterFirst (out first);
120
 
                                stylesTreeView.Selection.SelectIter (first);
121
 
                        }
122
 
 
123
 
                        protected void OnButtonToggled (object sender, EventArgs a)
124
 
                        {
125
 
                                SourceTagStyle sts = currentStyle;
126
 
                                sts.Bold = boldToggle.Active;
127
 
                                sts.Italic = italicToggle.Active;
128
 
                                sts.Underline = underlineToggle.Active;
129
 
                                sts.Strikethrough = strikeToggle.Active;
130
 
                                sts.IsDefault = false;
131
 
                                SetTagStyle (currentLanguage, styleid, sts);
132
 
                                restoreDefaultButton.Sensitive = true;
133
 
                        }
134
 
 
135
 
                        protected void OnColorSet (object sender, EventArgs a)
136
 
                        {
137
 
                                SourceTagStyle sts = currentStyle;
138
 
                                sts.Foreground = fgColorButton.Color;
139
 
                                sts.Background = bgColorButton.Color;
140
 
                                sts.Mask = checkColor.Active ? 2u : 0u;
141
 
                                sts.Mask += checkBackground.Active ? 1u : 0u;
142
 
                                fgColorButton.Sensitive = checkColor.Active;
143
 
                                bgColorButton.Sensitive = checkBackground.Active;
144
 
                                sts.IsDefault = false;
145
 
                                SetTagStyle (currentLanguage, styleid, sts);
146
 
                                restoreDefaultButton.Sensitive = true;
147
 
                        }
148
 
 
149
 
                        protected void OnHighlightingToggled (object sender, EventArgs a)
150
 
                        {
151
 
                                CheckButton cb = sender as CheckButton;
152
 
                                childrenVBox.Sensitive = cb.Active;
153
 
                        }
154
 
 
155
 
                        protected void OnLanguageSelected (object sender, EventArgs a)
156
 
                        {
157
 
                                TreeIter iter;
158
 
                                if (sourceLanguages.GetActiveIter (out iter)) {
159
 
                                        SetCurrentLanguage ((string) sourceLanguages.Model.GetValue (iter, 0));
160
 
                                }
161
 
                        }
162
 
 
163
 
                        protected void OnRestoreClicked (object sender, EventArgs a)
164
 
                        {
165
 
                                currentLanguage = svs.RestoreDefaults (currentLanguage);
166
 
                                OnStyleChanged (stylesTreeView.Selection, EventArgs.Empty);
167
 
                        }
168
 
 
169
 
                        private void OnStyleChanged (object sender, EventArgs a)
170
 
                        {
171
 
                                TreeIter iter;
172
 
                                TreeModel model;
173
 
                                TreeSelection selection = sender as TreeSelection;
174
 
 
175
 
                                if (selection.GetSelected (out model, out iter)) {
176
 
                                        styleid = (string) model.GetValue (iter, 1);
177
 
                                        currentStyle = GetTagStyle (currentLanguage, styleid);
178
 
                                        SetSourceTagStyle ();
179
 
                                }
180
 
                        }
181
 
                        
182
 
                        SourceTagStyle GetTagStyle (SourceLanguage lang, string id)
183
 
                        {
184
 
                                Dictionary<string,SourceTagStyle> styles;
185
 
                                if (changes.TryGetValue (lang, out styles)) {
186
 
                                        SourceTagStyle style;
187
 
                                        if (styles.TryGetValue (id, out style))
188
 
                                                return style;
189
 
                                }
190
 
                                return currentLanguage.GetTagStyle (styleid);
191
 
                        }
192
 
                        
193
 
                        void SetTagStyle (SourceLanguage lang, string id, SourceTagStyle style)
194
 
                        {
195
 
                                Dictionary<string,SourceTagStyle> styles;
196
 
                                if (!changes.TryGetValue (lang, out styles)) {
197
 
                                        styles = new Dictionary<string,SourceTagStyle> ();
198
 
                                        changes [lang] = styles;
199
 
                                }
200
 
                                styles [id] = style;
201
 
                        }
202
 
                }
203
 
        }
204
 
}
205