예제 #1
0
		private void buttonAddNewProject_Click(object sender, System.EventArgs e)
		{
			// in case the Add New project button was default, change it to the OK button.
			this.AcceptButton = this.buttonOK;

			// get the delimiter for word boundaries and disallow the /"/ character
			m_strWordBoundaryDelimiter = this.textBoxWordBoundaryDelimiter.Text;
			if( m_strWordBoundaryDelimiter.IndexOf('"') != -1 )
			{
				MessageBox.Show("Can't use the double-quote character for the word boundary delimiter",SpellingFixerEC.cstrCaption);
				return;
			}

			bool bRewriteCCTable = false;

			string strPunctuation = EncodePunctuationForCC(this.textBoxAddlPunctuation.Text);
			if(strPunctuation == null )
				return; // it had a bad character

			else if( strPunctuation != m_strNonWordCharacters )
			{
				// this means the file must be re-written
				bRewriteCCTable = true;
				m_strNonWordCharacters = strPunctuation;
			}

			if( (!m_bLegacy) != this.checkBoxUnicode.Checked )
			{
				m_bLegacy = !this.checkBoxUnicode.Checked;
				bRewriteCCTable = true;
			}

			// check for existing EncConverter with this same project information
			string strCCTableSpec = null;
			string strPartialName = this.textBoxNewProjectName.Text;
			string strEncConverterName = FullName(strPartialName);
			EncConverters aECs = new EncConverters();
			IEncConverter aEC = aECs[strEncConverterName];
			if( aEC != null )
			{
				// if we're *not* in edit mode
				if( this.buttonAddNewProject.Text == cstrAddNewProjectButtonText )
				{
					if( MessageBox.Show(String.Format("A project already exists by the name {0}. Click 'Yes' to overwrite", this.textBoxNewProjectName.Text),SpellingFixerEC.cstrCaption, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
					{
						// take it out of the check box list
						checkedListBoxProjects.Items.Remove(strPartialName);
						strCCTableSpec = aEC.ConverterIdentifier;
						if( File.Exists(strCCTableSpec) )
						{
							File.Delete(strCCTableSpec);
							strCCTableSpec = null;
						}

						// remove the existing one and we'll add a new one next
						aECs.Remove(aEC.Name);
						aEC = null;
					}
					else
						return;
				}
				else    // edit mode
				{
					if( MessageBox.Show(String.Format("Do you want to update the '{0}' project?", this.textBoxNewProjectName.Text),SpellingFixerEC.cstrCaption, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
					{
						// take it out of the check box list
						checkedListBoxProjects.Items.Remove(strPartialName);

						// save the spec so that we don't make one below
						strCCTableSpec = aEC.ConverterIdentifier;

						// the remove the converter since we'll add it back again next
						aECs.Remove(aEC.Name);
						aEC = null;
					}
					else
					{
						// reset this in case we were just editing it.
						ResetNewProjectLook();
						return;
					}
				}
			}

			// if we're aren't using the old cc table, then...
			if( strCCTableSpec == null )
			{
				// now add it (put it in the normal 'MapsTables' folder in \pf\cf\sil\...)
				string strMapsTableDir = GetMapTableFolderPath;
				strCCTableSpec = strMapsTableDir + @"\" + strEncConverterName + ".cct";
				if( File.Exists(strCCTableSpec) )
				{
					// the converter doesn't exist, but a file with the name we would have
					//  given it does... ask the user if they want to overwrite it.
					// TODO: this doesn't allow for complete flexibility. It might be nicer to
					//  allow for any arbitrary name, but not if noone complains.
					if( MessageBox.Show(String.Format("A file exists by the name {0}. Click 'Yes' to overwrite", strCCTableSpec),SpellingFixerEC.cstrCaption, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
					{
						File.Delete(strCCTableSpec);
#if WriteOnAdd
// if the user goes to add the first record, it'd be better if the file didn't exist because now we do some
//  preliminary testing whether the CC table already changes a word before adding a new one, but this causes
//  a non-trapable error if there are no rules in the file. So just create it when it is actually needed
						CreateCCTable(strCCTableSpec,strEncConverterName,m_strNonWordCharacters, !this.m_bLegacy);
#endif
					}
				}
#if WriteOnAdd
				else
					CreateCCTable(strCCTableSpec,strEncConverterName,m_strNonWordCharacters, !this.m_bLegacy);
#endif
				bRewriteCCTable = false;
			}

			// now add the EncConverter
			// TODO: EncConverters needs a new interface to get the defining encodingID from
			//  a FontName (so we can use it in this call) just like we can 'try' to get the
			//  code page given a font name (see 'CodePage' below)
			ConvType eConvType = (m_bLegacy)
				? ConvType.Legacy_to_Legacy : ConvType.Unicode_to_Unicode;

			aECs.Add(strEncConverterName,strCCTableSpec,eConvType,null,null,SpellingFixerEC.SFProcessType);

			Font font = null;
			try
			{
				font = new Font(comboBoxFont.SelectedItem.ToString(),Convert.ToSingle(listBoxFontSize.SelectedItem));
			}
			catch
			{
				MessageBox.Show("Couldn't create the selected font. Contact support");
				return;
			}

			// add this 'displaying font' information to the converter as properties/attributes
			ECAttributes aECAttrs = aECs.Attributes(strEncConverterName,AttributeType.Converter);
			aECAttrs.Add(SpellingFixerEC.cstrAttributeFontToUse,font.Name);
			aECAttrs.Add(SpellingFixerEC.cstrAttributeFontSizeToUse, font.Size);
			aECAttrs.Add(SpellingFixerEC.cstrAttributeWordBoundaryDelimiter, m_strWordBoundaryDelimiter);
			aECAttrs.Add(SpellingFixerEC.cstrAttributeNonWordChars, m_strNonWordCharacters);

			// if it's not Unicode, then we need a code page in order to convert from wide to
			//  narrow (when writing to the file).
			int cp = 0;
			if( m_bLegacy )
			{
				// try to get the code page from EncConverters
				try
				{
					cp = aECs.CodePage(font.Name);
				}
				catch
				{
					// if it fails, it means we don't have a mapping, so add one here.
					// TODO: it would be nice to have an encoding, but I'm loath to query
					//  the user for it here since it isn't extremely relevant to this app.
					cp = Convert.ToInt32(this.textBoxCP.Text);
					aECs.AddFont(font.Name,cp,null);
				}
			}

			if(bRewriteCCTable)    // we are going to continue using the old file... so we must re-write it.
			{
				// if it was legacy encoded, then we need to convert the data to narrow using
				//  the code page the user specified (or we got out of the repository)
				Encoding enc = null;
				if( m_bLegacy )
				{
					if (cp == EncConverters.cnSymbolFontCodePage)
						cp = EncConverters.cnIso8859_1CodePage;
					enc = Encoding.GetEncoding(cp);
				}
				else
					enc = new UTF8Encoding();

				DataTable   myTable;
				if( SpellingFixerEC.InitializeDataTableFromCCTable(strCCTableSpec, enc, m_strWordBoundaryDelimiter, out myTable) )
				{
					ReWriteCCTableHeader(strCCTableSpec,m_strNonWordCharacters,enc);
					SpellingFixerEC.AppendCCTableFromDataTable(strCCTableSpec, enc, m_strWordBoundaryDelimiter, m_strNonWordCharacters, myTable);
				}
			}

			// finally, add the new project to the now-visible checkbox list
			this.buttonOK.Enabled = checkedListBoxProjects.Visible = true;
			ClearClickedItems();
			checkedListBoxProjects.Items.Add(strPartialName,CheckState.Checked);

			// reset this in case we were just editing it.
			ResetNewProjectLook();
		}
예제 #2
0
		internal bool LoadProject(string strProjectName)
		{
			// get the EncConverter that should have been added above by 'AddNewProject' button
			EncConverters aECs = new EncConverters();
			IEncConverter aEC = aECs[FullName(strProjectName)];
			if( aEC != null )
			{
				m_strEncConverterName = aEC.Name;
				m_strConverterSpec = aEC.ConverterIdentifier;
				ECAttributes aECAttrs = aECs.Attributes(aEC.Name,AttributeType.Converter);
				string strFontName = aECAttrs[SpellingFixerEC.cstrAttributeFontToUse];
				string sFontSize = aECAttrs[SpellingFixerEC.cstrAttributeFontSizeToUse];
				m_strWordBoundaryDelimiter = aECAttrs[SpellingFixerEC.cstrAttributeWordBoundaryDelimiter];
				m_strNonWordCharacters = aECAttrs[SpellingFixerEC.cstrAttributeNonWordChars];

				// new in 1.2 (so it might not exist)
				if( m_strNonWordCharacters == null )
					m_strNonWordCharacters = SpellingFixerEC.GetDefaultPunctuation;

				// if this was added (without having been made on this system), then
				//  these properties doesn't get added automatically. Must go to edit mode!
				if((strFontName == null)
					||  (sFontSize == null)
					||  (m_strWordBoundaryDelimiter == null) )
				{
					MessageBox.Show("It looks like this project was added to the repository incorrectly because it's missing some important properties. You'll need to edit it again to set the font, and other values.");
					DoEdit(aECs, aEC, strProjectName, strFontName, sFontSize);

					// make the "Update" button the default button
					this.AcceptButton = this.buttonAddNewProject;
					return false;
				}

				float fFontSize = (float)0.0;
				try
				{
					fFontSize = (float)Convert.ToSingle(sFontSize);
				}
				catch {}

				if( (strFontName != "") && (fFontSize != 0.0) )
					m_font = new Font(strFontName,fFontSize);

				m_bLegacy = (aEC.ConversionType == ConvType.Legacy_to_Legacy);
				if( m_bLegacy )
					m_cp = aECs.CodePage(strFontName);

				RegistryKey keyLastSFProject = Registry.CurrentUser.CreateSubKey(cstrProjectMemoryKey);
				keyLastSFProject.SetValue(cstrProjectMostRecentProject,strProjectName);
				return true;
			}

			return false;
		}
예제 #3
0
		private void menuItemEdit_Click(object sender, System.EventArgs e)
		{
			// Provide a way to edit the info (in case the user wants to change the
			//  font, size, or delimter.
			int nIndex = checkedListBoxProjects.IndexFromPoint(m_ptRightClicked);
			if( nIndex >= 0 )
			{
				string strProjectName = checkedListBoxProjects.Items[nIndex].ToString();
				EncConverters aECs = new EncConverters();
				IEncConverter aEC = aECs[FullName(strProjectName)];
				if( aEC != null )
				{
					m_strEncConverterName = aEC.Name;
					m_strConverterSpec = aEC.ConverterIdentifier;
					ECAttributes aECAttrs = aECs.Attributes(aEC.Name,AttributeType.Converter);

					string strFontName = aECAttrs[SpellingFixerEC.cstrAttributeFontToUse];
					string sFontSize = aECAttrs[SpellingFixerEC.cstrAttributeFontSizeToUse];
					m_strWordBoundaryDelimiter = aECAttrs[SpellingFixerEC.cstrAttributeWordBoundaryDelimiter];
					m_strNonWordCharacters = aECAttrs[SpellingFixerEC.cstrAttributeNonWordChars];

					DoEdit(aECs, aEC, strProjectName, strFontName, sFontSize);
				}

				// make the "Update" button the default button
				this.AcceptButton = this.buttonAddNewProject;
			}
		}