Merge branch 'alexischr/nursery-canaries-managed-alloc'
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms.RTF / Style.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    (pbartok@novell.com)
24 //
25 //
26
27 // COMPLETE
28
29 namespace System.Windows.Forms.RTF {
30
31 #if RTF_LIB
32         public
33 #else
34         internal
35 #endif
36         class Style {
37                 #region Local Variables
38                 public const int        NoStyleNum = 222;
39                 public const int        NormalStyleNum = 0;
40
41                 private string          name;
42                 private StyleType       type;
43                 private bool            additive;
44                 private int             num;
45                 private int             based_on;
46                 private int             next_par;
47                 private bool            expanding;
48                 private StyleElement    elements;
49                 private Style           next;
50                 #endregion Local Variables
51
52                 #region Constructors
53                 public Style(RTF rtf) {
54                         num = -1;
55                         type = StyleType.Paragraph;
56                         based_on = NoStyleNum;
57                         next_par = -1;
58
59                         lock (rtf) {
60                                 if (rtf.Styles == null) {
61                                         rtf.Styles = this;
62                                 } else {
63                                         Style s = rtf.Styles;
64                                         while (s.next != null)
65                                                 s = s.next;
66                                         s.next = this;
67                                 }
68                         }
69                 }
70                 #endregion      // Constructors
71
72                 #region Properties
73                 public string Name {
74                         get {
75                                 return name;
76                         }
77
78                         set {
79                                 name = value;
80                         }
81                 }
82
83                 public StyleType Type {
84                         get {
85                                 return type;
86                         }
87
88                         set {
89                                 type = value;
90                         }
91                 }
92
93                 public bool Additive {
94                         get {
95                                 return additive;
96                         }
97
98                         set {
99                                 additive = value;
100                         }
101                 }
102
103                 public int BasedOn {
104                         get {
105                                 return based_on;
106                         }
107
108                         set {
109                                 based_on = value;
110                         }
111                 }
112
113                 public StyleElement Elements {
114                         get {
115                                 return elements;
116                         }
117
118                         set {
119                                 elements = value;
120                         }
121                 }
122
123                 public bool Expanding {
124                         get {
125                                 return expanding;
126                         }
127
128                         set {
129                                 expanding = value;
130                         }
131                 }
132
133                 public int NextPar {
134                         get {
135                                 return next_par;
136                         }
137
138                         set {
139                                 next_par = value;
140                         }
141                 }
142
143                 public int Num {
144                         get {
145                                 return num;
146                         }
147
148                         set {
149                                 num = value;
150                         }
151                 }
152                 #endregion      // Properties
153
154                 #region Methods
155                 public void Expand(RTF rtf) {
156                         StyleElement    se;
157
158                         if (num == -1) {
159                                 return;
160                         }
161
162                         if (expanding) {
163                                 throw new Exception("Recursive style expansion");
164                         }
165                         expanding = true;
166
167                         if (num != based_on) {
168                                 rtf.SetToken(TokenClass.Control, Major.ParAttr, Minor.StyleNum, based_on, "\\s");
169                                 rtf.RouteToken();
170                         }
171
172                         se = elements;
173                         while (se != null) {
174                                 rtf.TokenClass = se.TokenClass;
175                                 rtf.Major = se.Major;
176                                 rtf.Minor = se.Minor;
177                                 rtf.Param = se.Param;
178                                 rtf.Text = se.Text;
179                                 rtf.RouteToken();
180                         }
181
182                         expanding = false;
183                 }
184
185                 static public Style GetStyle(RTF rtf, int style_number) {
186                         Style s;
187
188                         lock (rtf) {
189                                 s = GetStyle(rtf.Styles, style_number);
190                         }
191                         return s;
192                 }
193
194                 static public Style GetStyle(Style start, int style_number) {
195                         Style   s;
196
197                         if (style_number == -1) {
198                                 return start;
199                         }
200
201                         s = start;
202
203                         while ((s != null) && (s.num != style_number)) {
204                                 s = s.next;
205                         }
206                         return s;
207                 }
208                 #endregion      // Methods
209         }
210 }