0:   //  NewFileDialog.cs
1:   //  Copyright (C) 2001 Mike Krueger
2:   //
3:   //  This program is free software; you can redistribute it and/or modify
4:   //  it under the terms of the GNU General Public License as published by
5:   //  the Free Software Foundation; either version 2 of the License, or
6:   //  (at your option) any later version.
7:   //
8:   //  This program is distributed in the hope that it will be useful,
9:   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10:   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11:   //  GNU General Public License for more details.
12:   //
13:   //  You should have received a copy of the GNU General Public License
14:   //  along with this program; if not, write to the Free Software
15:   //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16:  
17:   using System;
18:   using System.Collections;
19:   using System.ComponentModel;
20:   using System.Drawing;
21:   using System.Reflection;
22:   using System.Resources;
23:   using System.Windows.Forms;
24:   using System.Xml;
25:   using System.IO;
26:  
27:  
28:   using SharpDevelop.Gui.Components;
29:   using SharpDevelop.Gui;
30:   using SharpDevelop.Gui.Window;
31:   using SharpDevelop.Tool.Data;
32:   using SharpDevelop.Tool.Function;
33:   using SharpDevelop.Tool.Text;
34:   using SharpDevelop.Internal.Templates;
35:  
36:   namespace SharpDevelop.Gui.Dialogs {
37:       
38:       /// <summary>
39:       ///  This class is for creating a new "empty" file
40:       /// </summary>
41:       public class NewFileDialog : FormINewFileCreator
42:       {
43:           Container components new Container();
44:           
45:           MainWindow window;
46:           
47:           ToolTip    tooltip;
48:           
49:           RadioButton smalliconbutton new RadioButton();
50:           RadioButton largeiconbutton new RadioButton();
51:           
52:           Label label1            =  new Label();
53:           Label label2            =  new Label();
54:           Label label3            =  new Label();
55:           Label descriptionlabel  =  new Label();
56:           
57:           TreeView categorytree   new TreeView();
58:           ListView templateview   new ListView();
59:           
60:           Button helpbutton   new Button();
61:           Button cancelbutton new Button();
62:           Button openbutton   new Button();
63:           
64:           ArrayList alltemplates new ArrayList();
65:           ArrayList categories   new ArrayList();
66:           Hashtable icons        new Hashtable();
67:           
68:           
69:           public NewFileDialog(MainWindow window)
70:           {
71:               try {
72:                   InitializeComponent();
73:                   this.window window;
74:                   this.Owner  window;
75:                   InitializeTemplates();
76:                   
77:                   InitializeView();
78:                   categorytree.Select();
79:                   
80:                   StartPosition FormStartPosition.CenterParent;
81:                   Icon null;
82:               catch (Exception e) {
83:                   Console.WriteLine(e.ToString());
84:               }
85:           }
86:           
87:           void InitializeView()
88:           {
89:               ImageList smalllist new ImageList();
90:               ImageList imglist new ImageList();
91:               
92:               imglist.ImageSize    new Size(3232);
93:               smalllist.ImageSize  new Size(1616);
94:               
95:               smalllist.Images.Add(Resource.GetBitmap("Icons.32x32.EmptyFileIcon"));
96:               imglist.Images.Add(Resource.GetBitmap("Icons.32x32.EmptyFileIcon"));
97:               
98:               int 0;
99:               Hashtable tmp new Hashtable(icons);
100:               foreach (DictionaryEntry entry in icons) {
101:                   Bitmap bitmap FileUtility.GetBitmap(entry.Key.ToString());
102:                   if (bitmap != null) {
103:                       smalllist.Images.Add(bitmap);
104:                       imglist.Images.Add(bitmap);
105:                       tmp[entry.Key] = ++i;
106:                   else 
107:                       Console.WriteLine("can't load bitmap " entry.Key.ToString() + " using default");
108:               }
109:               
110:               icons tmp;
111:               foreach (TemplateItem item in alltemplates) {
112:                   if (item.Template.Icon == null)
113:                       item.ImageIndex 0;
114:                   else
115:                       item.ImageIndex = (int)icons[item.Template.Icon];
116:               }
117:               
118:               templateview.LargeImageList imglist;
119:               templateview.SmallImageList smalllist;
120:               
121:               InsertCategories(nullcategories);
122:               if (categories.Count > 0)
123:                   categorytree.SelectedNode = (TreeNode)categorytree.Nodes[0];
124:               
125:           }
126:           
127:           void InsertCategories(TreeNode nodeArrayList catarray)
128:           {
129:               foreach (Category cat in catarray) {
130:                   if (node == null) {
131:                       categorytree.Nodes.Add(cat);
132:                   else {
133:                       node.Nodes.Add(cat);
134:                   }
135:                   InsertCategories(catcat.Categories);
136:               }
137:           }
138:           
139:           // TODO : insert sub categories
140:           Category GetCategory(string categoryname)
141:           {
142:               foreach (Category category in categories) {
143:                   if (category.Text == categoryname)
144:                       return category;
145:               }
146:               Category newcategory new Category(categoryname);
147:               categories.Add(newcategory);
148:               return newcategory;
149:           }
150:           
151:           void InitializeTemplates()
152:           {
153:               foreach (FileTemplate template in FileTemplate.FileTemplates) {
154:                   TemplateItem titem new TemplateItem(template);
155:                   if (titem.Template.Icon != null)
156:                       icons[titem.Template.Icon] = 0// "create template icon"
157:                   Category cat GetCategory(titem.Template.Category);
158:                   cat.Templates.Add(titem);
159:                   if (cat.Templates.Count == 1)
160:                       titem.Selected true;
161:                   alltemplates.Add(titem);
162:               }
163:           }
164:           
165:           // tree view event handlers
166:           void CategoryChange(object senderTreeViewEventArgs e)
167:           {
168:               templateview.Items.Clear();
169:               if (categorytree.SelectedNode != null) {
170:                   foreach (TemplateItem item in ((Category)categorytree.SelectedNode).Templates) {
171:                       templateview.Items.Add(item);
172:                   }
173:               }
174:           }
175:           
176:           void OnBeforeExpand(object senderTreeViewCancelEventArgs e)
177:           {
178:               e.Node.ImageIndex 1;
179:           }
180:           
181:           void OnBeforeCollapse(object senderTreeViewCancelEventArgs e)
182:           {
183:               e.Node.ImageIndex 0;
184:           }
185:           
186:           // list view event handlers
187:           void SelectedIndexChange(object senderEventArgs e)
188:           {
189:               if (templateview.SelectedItems.Count == 1) {
190:                   descriptionlabel.Text = ((TemplateItem)templateview.SelectedItems[0]).Template.Description;
191:                   openbutton.Enabled true;
192:               else {
193:                   descriptionlabel.Text "";
194:                   openbutton.Enabled false;
195:               }
196:           }
197:           
198:           // button events
199:           
200:           void CheckedChange(object senderEventArgs e)
201:           {
202:               templateview.View smalliconbutton.Checked View.List : View.LargeIcon;
203:           }
204:           
205:           public bool IsFilenameAvailable(string fileName)
206:           {
207:               return true;
208:           }
209:           
210:           public void SaveFile(string filenamestring contentstring languageNamebool showFile)
211:           {
212:                   ContentWindow twindow new ContentWindow(windowWindowContent.CSFilenulllanguageName);
213:                   twindow.DefaultName   filename;
214:                   twindow.Closing += new CancelEventHandler(window.WindowClosingEvt);
215:                   twindow.Show();
216:                   
217:                   window.OpenFileTab.SelectedTab twindow.TabItem// may be unnecessary in future versions
218:                   
219:                   twindow.Dirty false;
220:                   if (twindow.HasTextArea) {
221:   //                    if (item.Syntax != null)
222:   //                        twindow.TextArea.TextAreaPainter.Buffer.SetHighlightingTo(item.Syntax);
223:                       
224:                       if (content != null) {
225:                           twindow.TextArea.TextAreaPainter.Buffer.Text StringParser.Parse(contentnew string[,] {});
226:                           twindow.TextArea.BufferChange(nullnull);
227:                           twindow.TextArea.TextAreaPainter.Refresh();
228:                       }
229:                   }
230:                   DialogResult DialogResult.OK;
231:           }
232:           
233:           void OpenEvent(object senderEventArgs e)
234:           {
235:               if (templateview.SelectedItems.Count == 1) {
236:                   TemplateItem item = (TemplateItem)templateview.SelectedItems[0];
237:                   
238:                   WindowContent content WindowContent.Text;
239:                   
240:                   if (item.Template.LanguageName != null) {
241:                       content WindowContent.CSFile;
242:                   }
243:                   if (item.Template.Wizard != null) {
244:                       INewFileWizard wizard = (INewFileWizard)item.Template.Wizard.CreateInstance(item.Template.WizardClass);
245:                       wizard.StartWizard(windowthisitem.Template.WizardParameters);
246:                   else {
247:                       foreach (FileDescriptionTemplate newfile in item.Template.Files) {
248:           
249:                               ContentWindow twindow new ContentWindow(windowcontentnullitem.Template.LanguageName);
250:                               Console.WriteLine(newfile.Name);
251:                               twindow.DefaultName   newfile.Name;
252:                               twindow.Closing += new CancelEventHandler(window.WindowClosingEvt);
253:                               twindow.Show();
254:                               
255:                               window.OpenFileTab.SelectedTab twindow.TabItem// may be unnecessary in future versions
256:                               
257:                               twindow.Dirty false;
258:                               if (twindow.HasTextArea) {
259:               //                    if (item.Syntax != null)
260:               //                        twindow.TextArea.TextAreaPainter.Buffer.SetHighlightingTo(item.Syntax);
261:                                   
262:                                   if (newfile.Content != null) {
263:                                       twindow.TextArea.TextAreaPainter.Buffer.Text StringParser.Parse(newfile.Contentnew string[,] {});
264:                                       twindow.TextArea.TextAreaPainter.Refresh();
265:                                   }
266:                               }
267:                               DialogResult DialogResult.OK;
268:                       }
269:                   }
270:               }
271:           }
272:           
273:           /// <summary>
274:           ///  Represents a category
275:           /// </summary>
276:           internal class Category : TreeNode
277:           {
278:               ArrayList categories new ArrayList();
279:               ArrayList templates  new ArrayList();
280:               string name;
281:               
282:               public Category(string namebase(name)
283:               {
284:                   this.name name;
285:                   ImageIndex 1;
286:               }
287:               
288:               public string Name {
289:                   get {
290:                       return name;
291:                   }
292:               }
293:               public ArrayList Categories {
294:                   get {
295:                       return categories;
296:                   }
297:               }
298:               public ArrayList Templates {
299:                   get {
300:                       return templates;
301:                   }
302:               }
303:           }
304:           
305:           /// <summary>
306:           ///  Represents a new file template
307:           /// </summary>
308:           internal class TemplateItem : ListViewItem
309:           {
310:               FileTemplate template;
311:  
312:               public TemplateItem(FileTemplate templatebase(template.Name)
313:               {
314:                   this.template template;
315:                   ImageIndex    0;
316:               }
317:               
318:               public FileTemplate Template {
319:                   get {
320:                       return template;
321:                   }
322:               }
323:           }
324:           
325:           void InitializeComponent()
326:           {
327:               tooltip new ToolTip(components);
328:               tooltip.SetToolTip(largeiconbuttonResource.GetString("Global.LargeIconToolTip"));
329:               tooltip.SetToolTip(smalliconbuttonResource.GetString("Global.SmallIconToolTip"));
330:               tooltip.Active true;
331:               
332:               descriptionlabel.Location    new Point(8262);
333:               descriptionlabel.Text        "";
334:               descriptionlabel.Size        new Size(49618);
335:               descriptionlabel.BorderStyle System.Windows.Forms.BorderStyle.Fixed3D;
336:               descriptionlabel.TabIndex    9;
337:               
338:               label1.Location new Point(812);
339:               label1.Text     Resource.GetString("Dialog.NewFile.CategoryText");
340:               label1.Size     new Size(15216);
341:               label1.TabIndex 3;
342:               
343:               label2.Location new Point(22412);
344:               label2.Text     Resource.GetString("Dialog.NewFile.TemplateText");
345:               label2.Size     new Size(9616);
346:               label2.TabIndex 5;
347:               
348:               // this is my "boguslabel" (for the single line in the dialog)
349:               label3.Location    new Point(8290);
350:               label3.Size        new Size(4962);
351:               label3.BorderStyle System.Windows.Forms.BorderStyle.Fixed3D;
352:               label3.TabIndex    10;
353:               
354:               openbutton.Location     new Point(253300);
355:               openbutton.DialogResult DialogResult.OK;
356:               openbutton.Size         new Size(7523);
357:               openbutton.TabIndex     0;
358:               openbutton.Text         Re