| 0: | // WordCountDialog.cs | |
| 1: | // Copyright (C) 2000 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.IO; | |
| 19: | using System.Drawing; | |
| 20: | using System.ComponentModel; | |
| 21: | using System.Windows.Forms; | |
| 22: | ||
| 23: | using SharpDevelop.Gui; | |
| 24: | using SharpDevelop.Gui.Window; | |
| 25: | using SharpDevelop.Tool.Data; | |
| 26: | using SharpDevelop.Tool.Function; | |
| 27: | using SharpDevelop.Internal.Project; | |
| 28: | ||
| 29: | namespace SharpDevelop.Gui.Dialogs { | |
| 30: | ||
| 31: | /// <summary> | |
| 32: | /// Summary description for Win32Form1. | |
| 33: | /// </summary> | |
| 34: | public class WordCountDialog : System.Windows.Forms.Form | |
| 35: | { | |
| 36: | ||
| 37: | Container components; | |
| 38: | ||
| 39: | ColumnHeader lineHeader; | |
| 40: | ColumnHeader wordHeader; | |
| 41: | ColumnHeader charHeader; | |
| 42: | ColumnHeader fileHeader; | |
| 43: | ||
| 44: | Button button1; | |
| 45: | Button cancelButton; | |
| 46: | Button startButton; | |
| 47: | Label label1; | |
| 48: | ComboBox comboBox1; | |
| 49: | ListView listView1; | |
| 50: | ||
| 51: | MainWindow main; | |
| 52: | ||
| 53: | internal class Report | |
| 54: | { | |
| 55: | string name; | |
| 56: | long chars; | |
| 57: | long words; | |
| 58: | long lines; | |
| 59: | ||
| 60: | public Report(string name, long chars, long words, long lines) | |
| 61: | { | |
| 62: | this.name = name; | |
| 63: | this.chars = chars; | |
| 64: | this.words = words; | |
| 65: | this.lines = lines; | |
| 66: | } | |
| 67: | ||
| 68: | public ListViewItem ToListItem() | |
| 69: | { | |
| 70: | return new ListViewItem(new string[] { Path.GetFileName(name), chars.ToString(), words.ToString(), lines.ToString()}); | |
| 71: | } | |
| 72: | ||
| 73: | public static Report operator+(Report r, Report s) | |
| 74: | { | |
| 75: | Report tmp = new Report(Resource.GetString("Dialog.WordCountDialog.TotalText"), s.chars, s.words, s.lines); | |
| 76: | tmp.chars += r.chars; | |
| 77: | tmp.words += r.words; | |
| 78: | tmp.lines += r.lines; | |
| 79: | return tmp; | |
| 80: | } | |
| 81: | } | |
| 82: | ||
| 83: | Report GetReport(string filename) | |
| 84: | { | |
| 85: | long numLines = 0; | |
| 86: | long numWords = 0; | |
| 87: | long numChars = 0; | |
| 88: | ||
| 89: | FileStream istream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); | |
| 90: | StreamReader sr = new StreamReader(istream); | |
| 91: | string line = sr.ReadLine(); | |
| 92: | while (line != null) { | |
| 93: | ++numLines; | |
| 94: | numChars += line.Length; | |
| 95: | string[] words = line.Split(null); | |
| 96: | numWords += words.Length; | |
| 97: | line = sr.ReadLine(); | |
| 98: | } | |
| 99: | ||
| 100: | sr.Close(); | |
| 101: | return new Report(filename, numChars, numWords, numLines); | |
| 102: | } | |
| 103: | ||
| 104: | void startEvent(object sender, System.EventArgs e) | |
| 105: | { | |
| 106: | listView1.BeginUpdate(); | |
| 107: | listView1.Items.Clear(); | |
| 108: | switch (comboBox1.SelectedIndex) { | |
| 109: | case 0: {// current file | |
| 110: | ContentWindow window = main.ActiveContentWindow; | |
| 111: | if (window != null) { | |
| 112: | if (window.HasTextArea) { | |
| 113: | if (window.Untitled) { | |
| 114: | MessageBox.Show(Resource.GetString("Dialog.WordCountDialog.SaveTheFileWarning"), Resource.GetString("Global.WarningText"), | |
| 115: | MessageBoxButtons.OK, MessageBoxIcon.Warning); | |
| 116: | } else { | |
| 117: | if (FSTypeUtility.TestFileExists(window.FileName)) { | |
| 118: | listView1.Items.Add(GetReport(window.FileName).ToListItem()); | |
| 119: | } | |
| 120: | } | |
| 121: | } | |
| 122: | } | |
| 123: | break; | |
| 124: | } | |
| 125: | case 1: {// all open files | |
| 126: | if (main.MdiChildren.Length > 0) { | |
| 127: | string tmp = ""; | |
| 128: | ||
| 129: | Report all = new Report(Resource.GetString("Dialog.WordCountDialog.TotalText"), 0, 0, 0); | |
| 130: | foreach (ContentWindow window in main.MdiChildren) { | |
| 131: | if (window.Untitled) { | |
| 132: | MessageBox.Show(Resource.GetString("Dialog.WordCountDialog.SaveAllFileWarning"), Resource.GetString("Global.WarningText"), | |
| 133: | MessageBoxButtons.OK, MessageBoxIcon.Warning); | |
| 134: | continue; | |
| 135: | } else { | |
| 136: | if (window.HasTextArea && FSTypeUtility.TestFileExists(window.FileName)) { | |
| 137: | Report r = GetReport(window.FileName); | |
| 138: | tmp += r.ToString(); | |
| 139: | all += r; | |
| 140: | listView1.Items.Add(r.ToListItem()); | |
| 141: | } | |
| 142: | } | |
| 143: | } | |
| 144: | listView1.Items.Add(new ListViewItem("")); | |
| 145: | listView1.Items.Add(all.ToListItem()); | |
| 146: | } | |
| 147: | break; | |
| 148: | } | |
| 149: | case 2: {// whole project | |
| 150: | if (!main.ProjectMode) { | |
| 151: | MessageBox.Show(Resource.GetString("Dialog.WordCountDialog.MustBeInProtectedModeWarning"), Resource.GetString("Global.ErrorText"), MessageBoxButtons.OK, MessageBoxIcon.Error); | |
| 152: | break; | |
| 153: | } | |
| 154: | Report all = new Report(Resource.GetString("Dialog.WordCountDialog.TotalText"), 0, 0, 0); | |
| 155: | CountCombine(main.ProjectBrowser.Combine, ref all); | |
| 156: | listView1.Items.Add(new ListViewItem("")); | |
| 157: | listView1.Items.Add(all.ToListItem()); | |
| 158: | break; | |
| 159: | } | |
| 160: | } | |
| 161: | listView1.EndUpdate(); | |
| 162: | } | |
| 163: | ||
| 164: | void CountCombine(Combine combine, ref Report all) | |
| 165: | { | |
| 166: | foreach (CombineEntry entry in combine.Entries) { | |
| 167: | if (entry.Entry is IProject) { | |
| 168: | // string tmp = ""; | |
| 169: | foreach (FileInformation finfo in ((IProject)entry.Entry).Files) { | |
| 170: | if (finfo.BuildAction == BuildAction.Compile) { | |
| 171: | Report r = GetReport(finfo.Name); | |
| 172: | all += r; | |
| 173: | listView1.Items.Add(r.ToListItem()); | |
| 174: | } | |
| 175: | } | |
| 176: | } else | |
| 177: | CountCombine((Combine)entry.Entry, ref all); | |
| 178: | } | |
| 179: | } | |
| 180: | ||
| 181: | ||
| 182: | // TODO : Sorting | |
| 183: | void SortEvt(object sender, ColumnClickEventArgs e) | |
| 184: | { | |
| 185: | listView1.BeginUpdate(); | |
| 186: | switch (e.Column) { | |
| 187: | case 0: // files | |
| 188: | break; | |
| 189: | case 1: // chars | |
| 190: | break; | |
| 191: | case 2: // words | |
| 192: | break; | |
| 193: | case 3: // lines | |
| 194: | break; | |
| 195: | } | |
| 196: | listView1.EndUpdate(); | |
| 197: | } | |
| 198: | ||
| 199: | public WordCountDialog(MainWindow main) | |
| 200: | { | |
| 201: | InitializeComponent(); | |
| 202: | this.main = main; | |
| 203: | AcceptButton = startButton; | |
| 204: | CancelButton = cancelButton; | |
| 205: | startButton.Click += new System.EventHandler(startEvent); | |
| 206: | listView1.ColumnClick += new ColumnClickEventHandler(SortEvt); | |
| 207: | Owner = main; | |
| 208: | ShowInTaskbar = false; | |
| 209: | Icon = null; | |
| 210: | StartPosition = FormStartPosition.CenterParent; | |
| 211: | } | |
| 212: | ||
| 213: | /// <summary> | |
| 214: | /// Clean up any resources being used. | |
| 215: | /// </summary> | |
| 216: | protected override void Dispose(bool disposing) | |
| 217: | { | |
| 218: | if (disposing) { | |
| 219: | if (components != null){ | |
| 220: | components.Dispose(); | |
| 221: | } | |
| 222: | } | |
| 223: | base.Dispose(disposing); | |
| 224: | } | |
| 225: | ||
| 226: | private void InitializeComponent() | |
| 227: | { | |
| 228: | this.components = new System.ComponentModel.Container(); | |
| 229: | this.listView1 = new System.Windows.Forms.ListView(); | |
| 230: | this.charHeader = new System.Windows.Forms.ColumnHeader(); | |
| 231: | this.lineHeader = new System.Windows.Forms.ColumnHeader(); | |
| 232: | this.label1 = new System.Windows.Forms.Label(); | |
| 233: | this.wordHeader = new System.Windows.Forms.ColumnHeader(); | |
| 234: | this.cancelButton = new System.Windows.Forms.Button(); | |
| 235: | this.comboBox1 = new System.Windows.Forms.ComboBox(); | |
| 236: | this.startButton = new System.Windows.Forms.Button(); | |
| 237: | this.fileHeader = new System.Windows.Forms.ColumnHeader(); | |
| 238: | this.button1 = new System.Windows.Forms.Button(); | |
| 239: | ||
| 240: | listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Clickable; | |
| 241: | listView1.MultiSelect = false; | |
| 242: | listView1.Size = new System.Drawing.Size(368, 256); | |
| 243: | listView1.FullRowSelect = true; | |
| 244: | listView1.View = System.Windows.Forms.View.Details; | |
| 245: | listView1.ForeColor = System.Drawing.SystemColors.WindowText; | |
| 246: | listView1.GridLines = true; | |
| 247: | listView1.TabIndex = 0; | |
| 248: | listView1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left; | |
| 249: | listView1.Location = new System.Drawing.Point(8, 40); | |
| 250: | listView1.Columns.Add(fileHeader); | |
| 251: | listView1.Columns.Add(charHeader); | |
| 252: | listView1.Columns.Add(wordHeader); | |
| 253: | listView1.Columns.Add(lineHeader); | |
| 254: | ||
| 255: | charHeader.Text = Resource.GetString("Dialog.WordCountDialog.CharsText"); | |
| 256: | charHeader.Width = 60; | |
| 257: | charHeader.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; | |
| 258: | ||
| 259: | lineHeader.Text = Resource.GetString("Dialog.WordCountDialog.LinesText"); | |
| 260: | lineHeader.Width = 60; | |
| 261: | lineHeader.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; | |
| 262: | ||
| 263: | label1.Location = new System.Drawing.Point(8, 8); | |
| 264: | label1.Text = Resource.GetString("Dialog.WordCountDialog.Label1Text"); | |
| 265: | ||
| 266: | label1.Size = new System.Drawing.Size(80, 16); | |
| 267: | label1.TabIndex = 1; | |
| 268: | ||
| 269: | this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); | |
| 270: | this.Text = Resource.GetString("Dialog.WordCountDialog.DialogName"); | |
| 271: | this.MaximizeBox = false; | |
| 272: | //@design this.TrayLargeIcon = true; | |
| 273: | this.ShowInTaskbar = false; | |
| 274: | this.FormBorderStyle = FormBorderStyle.Sizable; | |
| 275: | //@design this.TrayHeight = 0; | |
| 276: | // this.TopMost = true; | |
| 277: | this.MinimizeBox = false; | |
| 278: | this.ClientSize = new System.Drawing.Size(456 + 10, 301); | |
| 279: | ||
| 280: | wordHeader.Text = Resource.GetString("Dialog.WordCountDialog.WordsText"); | |
| 281: | wordHeader.Width = 60; | |
| 282: | wordHeader.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; | |
| 283: | ||
| 284: | cancelButton.Location = new System.Drawing.Point(384, 40); | |
| 285: | cancelButton.Size = new System.Drawing.Size(74, 24); | |
| 286: | cancelButton.TabIndex = 4; | |
| 287: | cancelButton.Anchor = AnchorStyles.Top | AnchorStyles.Right; | |
| 288: | cancelButton.Text = Resource.GetString("Global.CancelButtonText"); | |
| 289: | cancelButton.DialogResult = DialogResult.Cancel; | |
| 290: | ||
| 291: | comboBox1.Location = new System.Drawing.Point(96, 8); | |
| 292: | comboBox1.Text = ""; | |
| 293: | comboBox1.Size = new System.Drawing.Size(176, 21); | |
| 294: | comboBox1.TabIndex = 1; | |
| 295: | comboBox1.Items.Add(Resource.GetString("Global.Location.currentfile")); | |
| 296: | comboBox1.Items.Add(Resource.GetString("Global.Location.allopenfiles")); | |
| 297: | comboBox1.Items.Add(Resource.GetString("Global.Location.wholeproject")); | |
| 298: | comboBox1.ValueMember = comboBox1.Items[0].ToString(); | |
| 299: | ||
| 300: | comboBox1.SelectedIndex = 0; | |
| 301: | startButton.Location = new System.Drawing.Point(384, 8); | |
| 302: | startButton.Size = new System.Drawing.Size(74, 24); | |
| 303: | startButton.TabIndex = 3; | |
| 304: | startButton.Anchor = AnchorStyles.Top | AnchorStyles.Right; | |
| 305: | startButton.Text = Resource.GetString("Dialog.WordCountDialog.StartButton"); | |
| 306: | ||
| 307: | fileHeader.Text = Resource.GetString("Dialog.WordCountDialog.FileText"); | |
| 308: | fileHeader.Width = 177; | |
| 309: | fileHeader.TextAlign = System.Windows.Forms.HorizontalAlignment.Left; | |
| 310: | ||
| 311: | button1.Location = new System.Drawing.Point(384, 80); | |
| 312: | button1.Size = new System.Drawing.Size(74, 24); | |
| 313: | button1.TabIndex = 5; | |
| 314: | button1.Anchor = AnchorStyles.Top | AnchorStyles.Right; | |
| 315: | button1.Text = Resource.GetString("Global.HelpButtonText"); | |
| 316: | ||
| 317: | this.Controls.Add(button1); | |
| 318: | this.Controls.Add(cancelButton); | |
| 319: | this.Controls.Add(startButton); | |
| 320: | this.Controls.Add(label1); | |
| 321: |