using System; using System.Collections.Generic; using System.ComponentModel; using System.Collections.Specialized; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Leah.Speech.text; using System.Speech.Synthesis; namespace ContentEditor { public partial class frmContentEdit : Form { private int currentItem = 0; private DialogResult frmResult = DialogResult.Cancel; private System.Speech.Synthesis.SpeechSynthesizer Speech = new SpeechSynthesizer(); private bool bIsPlaying = false; public frmContentEdit() { InitializeComponent(); this.FormClosed += FrmContentEdit_FormClosed; this.FormClosing += FrmContentEdit_FormClosing; this.dgvTags.UserAddedRow += DgvTags_UserAddedRow; this.dgvOptionalTags.UserAddedRow += DgvOptionalTags_UserAddedRow; this.KeyDown += FrmContentEdit_KeyDown; this.txtContent.KeyDown += FrmContentEdit_KeyDown; this.FormClosing += FrmContentEdit_FormClosing1; Speech.SpeakStarted += Speech_SpeakStarted; Speech.SpeakCompleted += Speech_SpeakCompleted; Speech.SelectVoiceByHints(VoiceGender.Female); InstalledVoice[] voices = Speech.GetInstalledVoices().ToArray(); foreach(InstalledVoice voice in voices) { string gender = "None"; if (voice.VoiceInfo.Gender == VoiceGender.Female) gender = "female"; else if (voice.VoiceInfo.Gender == VoiceGender.Male) gender = "male"; else if (voice.VoiceInfo.Gender == VoiceGender.Neutral) gender = "neutral"; System.Diagnostics.Debug.WriteLine( "{" + "id: " + voice.VoiceInfo.Id + ", " + "name: " + voice.VoiceInfo.Name + ", " + "age: " + voice.VoiceInfo.Age + ", " + "Gender: " + gender + ", " + "description: " + voice.VoiceInfo.Description + "}" ); } } private void FrmContentEdit_FormClosing1(object sender, FormClosingEventArgs e) { if(this.bIsPlaying) { this.Speech.SpeakAsyncCancelAll(); } } private void FrmContentEdit_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.F3) { this.Speak(); } } private void Speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e) { this.bIsPlaying = false; this.cmdPlay.Enabled = true; } private void Speech_SpeakStarted(object sender, SpeakStartedEventArgs e) { this.bIsPlaying = true; this.cmdPlay.Enabled = false; } private void DgvOptionalTags_UserAddedRow(object sender, DataGridViewRowEventArgs e) { e.Row.Cells[0].Value = true; } private void DgvTags_UserAddedRow(object sender, DataGridViewRowEventArgs e) { e.Row.Cells[0].Value = true; } private void FrmContentEdit_FormClosing(object sender, FormClosingEventArgs e) { this.DialogResult = this.frmResult; } private void FrmContentEdit_FormClosed(object sender, FormClosedEventArgs e) { this.DialogResult = this.frmResult; } public static ContentEntry EditItem(int i) { frmContentEdit frm = new frmContentEdit(); /*frm.lstTags.Items.Clear(); frm.lstTags.Items.AddRange(frmMain.Content.Tags); frm.lstOptionalTags.Items.Clear(); frm.lstOptionalTags.Items.AddRange(frmMain.Content.OptionalTags);*/ frm.dgvTags.Rows.Clear(); for(int ti=0,c=frmMain.Content.Tags.Length;ti 1 || i == -1; //this.cmdNextItem.Enabled = i + 1 < frmMain.Content.Count || i==-1; this.cmdPrevItem.Enabled = frmMain.Content.Count > 1 || (frmMain.Content.Count == 1 && i == -1); this.cmdNextItem.Enabled = frmMain.Content.Count > 1 || (frmMain.Content.Count == 1 && i == -1); this.itemNumChangeEvent = true; } private void cmdMood_None_Click(object sender, EventArgs e) { this.MoodSelect(false); } public void CheckTags(ushort[] tags) { foreach(DataGridViewRow r in this.dgvTags.Rows) { if (r.Tag != null) { ushort t; string s = r.Tag.ToString(); if (ushort.TryParse(s, out t)) { r.Cells[0].Value = tags.Contains(t); } } } } public void CheckTags(bool All) { foreach (DataGridViewRow r in this.dgvTags.Rows) { r.Cells[0].Value = All; } } public void CheckOptionalTags(ushort[] tags) { foreach (DataGridViewRow r in this.dgvOptionalTags.Rows) { if (r.Tag != null) { ushort t; string s = r.Tag.ToString(); if (ushort.TryParse(s, out t)) { r.Cells[0].Value = tags.Contains(t); } } } } public void CheckOptionalTags(bool All) { foreach (DataGridViewRow r in this.dgvOptionalTags.Rows) { r.Cells[0].Value = All; } } public void CheckMood(eMood mood) { chkMood_Angry.Checked = ((mood | Leah.Speech.text.eMood.angry) == mood); chkMood_Annoyed.Checked = ((mood | Leah.Speech.text.eMood.annoyed) == mood); chkMood_Aroused.Checked = ((mood | Leah.Speech.text.eMood.aroused) == mood); chkMood_Bored.Checked = ((mood | Leah.Speech.text.eMood.bored) == mood); chkMood_Compfortable.Checked = ((mood | Leah.Speech.text.eMood.compfortable) == mood); chkMood_Depressive.Checked = ((mood | Leah.Speech.text.eMood.depressive) == mood); chkMood_Energy.Checked = ((mood | Leah.Speech.text.eMood.energy) == mood); chkMood_Excited.Checked = ((mood | Leah.Speech.text.eMood.excited) == mood); chkMood_Exhausted.Checked = ((mood | Leah.Speech.text.eMood.exhausted) == mood); chkMood_Freezy.Checked = ((mood | Leah.Speech.text.eMood.freezy) == mood); chkMood_Funny.Checked = ((mood | Leah.Speech.text.eMood.funny) == mood); chkMood_Happyness.Checked = ((mood | Leah.Speech.text.eMood.happyness) == mood); chkMood_Hot.Checked = ((mood | Leah.Speech.text.eMood.hot) == mood); chkMood_Humanity.Checked = ((mood | Leah.Speech.text.eMood.humanity) == mood); chkMood_Hungry.Checked = ((mood | Leah.Speech.text.eMood.hungry) == mood); chkMood_Intelligent.Checked = ((mood | Leah.Speech.text.eMood.intelligent) == mood); chkMood_Irritated.Checked = ((mood | Leah.Speech.text.eMood.irritated) == mood); chkMood_Lonely.Checked = ((mood | Leah.Speech.text.eMood.lonely) == mood); chkMood_Naiv.Checked = ((mood | Leah.Speech.text.eMood.naiv) == mood); chkMood_Playful.Checked = ((mood | Leah.Speech.text.eMood.playful) == mood); chkMood_Sad.Checked = ((mood | Leah.Speech.text.eMood.sad) == mood); chkMood_Shy.Checked = ((mood | Leah.Speech.text.eMood.shy) == mood); chkMood_Sleepy.Checked = ((mood | Leah.Speech.text.eMood.sleepy) == mood); chkMood_Soziality.Checked = ((mood | Leah.Speech.text.eMood.soziality) == mood); chkMood_Temperate.Checked = ((mood | Leah.Speech.text.eMood.temperate) == mood); chkMood_ToLoud.Checked = ((mood | Leah.Speech.text.eMood.toLoud) == mood); chkMood_ToQuiet.Checked = ((mood | Leah.Speech.text.eMood.toQuiet) == mood); chkMood_Usefull.Checked = ((mood | Leah.Speech.text.eMood.usefull) == mood); chkMood_Useless.Checked = ((mood | Leah.Speech.text.eMood.useless) == mood); } public eMood GetMood() { eMood res = eMood.None; if (chkMood_Angry.Checked) res |= eMood.angry; if (chkMood_Annoyed.Checked) res |= eMood.annoyed; if (chkMood_Aroused.Checked) res |= eMood.aroused; if (chkMood_Bored.Checked) res |= eMood.bored; if (chkMood_Compfortable.Checked) res |= eMood.compfortable; if (chkMood_Depressive.Checked) res |= eMood.depressive; if (chkMood_Energy.Checked) res |= eMood.energy; if (chkMood_Excited.Checked) res |= eMood.excited; if (chkMood_Exhausted.Checked) res |= eMood.exhausted; if (chkMood_Freezy.Checked) res |= eMood.freezy; if (chkMood_Funny.Checked) res |= eMood.funny; if (chkMood_Happyness.Checked) res |= eMood.happyness; if (chkMood_Hot.Checked) res |= eMood.hot; if (chkMood_Humanity.Checked) res |= eMood.humanity; if (chkMood_Hungry.Checked) res |= eMood.hungry; if (chkMood_Intelligent.Checked) res |= eMood.intelligent; if (chkMood_Irritated.Checked) res |= eMood.irritated; if (chkMood_Lonely.Checked) res |= eMood.lonely; if (chkMood_Naiv.Checked) res |= eMood.naiv; if (chkMood_Playful.Checked) res |= eMood.playful; if (chkMood_Sad.Checked) res |= eMood.sad; if (chkMood_Shy.Checked) res |= eMood.shy; if (chkMood_Sleepy.Checked) res |= eMood.sleepy; if (chkMood_Soziality.Checked) res |= eMood.soziality; if (chkMood_Temperate.Checked) res |= eMood.temperate; if (chkMood_ToLoud.Checked) res |= eMood.toLoud; if (chkMood_ToQuiet.Checked) res |= eMood.toQuiet; if (chkMood_Usefull.Checked) res |= eMood.usefull; if (chkMood_Useless.Checked) res |= eMood.useless; return res; } public ContentEntry GetEntry() { ContentEntry res = new ContentEntry(this.Text.Trim(), this.GetMood()); return res; } /*private void SelectListBox(ListBox lst, bool select) { if(select) { //lst.selected } else { lst.ClearSelected(); } }*/ private void MoodSelect(bool select) { chkMood_Angry.Checked = select; chkMood_Annoyed.Checked = select; chkMood_Aroused.Checked = select; chkMood_Bored.Checked = select; chkMood_Compfortable.Checked = select; chkMood_Depressive.Checked = select; chkMood_Energy.Checked = select; chkMood_Excited.Checked = select; chkMood_Exhausted.Checked = select; chkMood_Freezy.Checked = select; chkMood_Funny.Checked = select; chkMood_Happyness.Checked = select; chkMood_Hot.Checked = select; chkMood_Humanity.Checked = select; chkMood_Hungry.Checked = select; chkMood_Intelligent.Checked = select; chkMood_Irritated.Checked = select; chkMood_Lonely.Checked = select; chkMood_Naiv.Checked = select; chkMood_Playful.Checked = select; chkMood_Sad.Checked = select; chkMood_Shy.Checked = select; chkMood_Sleepy.Checked = select; chkMood_Soziality.Checked = select; chkMood_Temperate.Checked = select; chkMood_ToLoud.Checked = select; chkMood_ToQuiet.Checked = select; chkMood_Usefull.Checked = select; chkMood_Useless.Checked = select; } private void cmdMood_All_Click(object sender, EventArgs e) { this.MoodSelect(true); } private void cmdNextItem_Click(object sender, EventArgs e) { if (this.currentItem + 1 < frmMain.Content.Count) this.LoadIndex(this.currentItem + 1); else this.LoadIndex(0); } private void cmdPrevItem_Click(object sender, EventArgs e) { if(this.currentItem>0) this.LoadIndex(this.currentItem - 1); else this.LoadIndex(frmMain.Content.Count-1); } private bool itemNumChangeEvent = true; private void txtItemNum_TextChanged(object sender, EventArgs e) { if (this.itemNumChangeEvent) { int page = 0; if (int.TryParse(this.txtItemNum.Text, out page)) { this.LoadIndex(page); } else if(this.txtItemNum.Text.ToLower().Trim()=="new") { this.LoadIndex(-1); } else { this.itemNumChangeEvent = false; txtItemNum.Text = this.currentItem.ToString(); this.itemNumChangeEvent = true; } } } private void cmdTags_None_Click(object sender, EventArgs e) { //this.SelectListBox(lstTags, false); this.CheckOptionalTags(false); } private void cmdTags_All_Click(object sender, EventArgs e) { //this.SelectListBox(lstTags, true); this.CheckOptionalTags(true); } private void cmdOptionalTags_None_Click(object sender, EventArgs e) { //this.SelectListBox(lstOptionalTags, false); this.CheckOptionalTags(false); } private void cmdOptionalTags_All_Click(object sender, EventArgs e) { //this.SelectListBox(lstOptionalTags, true); this.CheckOptionalTags(true); } private void cmdNew_Click(object sender, EventArgs e) { //this.DialogResult = DialogResult.OK; this.SaveEntry(); this.LoadIndex(-1); } private void SaveEntry() { ContentEntry entry; if(this.currentItem>=0) { System.Diagnostics.Debug.WriteLine("Fetch entry: " + this.currentItem); entry = frmMain.Content.GetEntry(this.currentItem); } else { entry = new ContentEntry(); if(frmMain.Content.Add(entry)) { this.currentItem = frmMain.Content.Count - 1; System.Diagnostics.Debug.WriteLine("new item added with id: "+this.currentItem.ToString()); } else { System.Diagnostics.Debug.WriteLine("Failed to add item"); } } if(entry!=null) { frmMain.bNeedAutoSave = true; entry.Text = this.txtContent.Text.Trim(); entry.Mood = this.GetMood(); entry.Tags = new ushort[] { }; foreach (DataGridViewRow r in this.dgvTags.Rows) { if(r.Tag==null && r.Cells[1].Value!=null && r.Cells[1].Value.ToString()!="" && r.Cells[1].Value.ToString().StartsWith("{")==false && (bool)r.Cells[0].Value == true) { r.Tag = frmMain.Content.Tag_Add((string)r.Cells[1].Value); } if (r.Tag != null && r.Cells[0].Value != null) { ushort tagId; if (ushort.TryParse(r.Tag.ToString(), out tagId)) { if ((bool)r.Cells[0].Value == true) { entry.Tag_Add(tagId); } } } } entry.OptionalTags = new ushort[] { }; foreach (DataGridViewRow r in this.dgvOptionalTags.Rows) { if (r.Tag == null && r.Cells[1].Value != null && r.Cells[1].Value.ToString() != "" && r.Cells[1].Value.ToString().StartsWith("{") == false && (bool)r.Cells[0].Value == true) { r.Tag = frmMain.Content.OptionalTag_Add((string)r.Cells[1].Value); } if (r.Tag != null && r.Cells[0].Value != null) { ushort tagId; if (ushort.TryParse(r.Tag.ToString(), out tagId)) { if ((bool)r.Cells[0].Value == true) { entry.OptionalTag_Add(tagId); } } } } frmMain.Content.Entries[this.currentItem] = entry; } else { System.Diagnostics.Debug.WriteLine("no entry"); } } private void cmdOk_Click(object sender, EventArgs e) { this.frmResult = DialogResult.OK; this.SaveEntry(); this.Close(); } private void cmdAbort_Click(object sender, EventArgs e) { this.Close(); } private void cmdAdd_Click(object sender, EventArgs e) { this.frmResult = DialogResult.OK; this.SaveEntry(); this.LoadIndex(-1); } private void cmdPlay_Click(object sender, EventArgs e) { this.Speak(); } private void Speak() { frmMain.Speak(this.txtContent.Text); } private void toolStripMenuItem1_Click(object sender, EventArgs e) { } private void mnuText_SelectAll_Click(object sender, EventArgs e) { this.txtContent.SelectAll(); } private void mnuText_Paste_Click(object sender, EventArgs e) { /*string txtBefore = this.txtContent.Text.Substring(0, this.txtContent.SelectionStart); string txtAfter = this.txtContent.Text.Substring(this.txtContent.SelectionStart + this.txtContent.SelectionLength); string clipboard = Clipboard.GetText(); this.txtContent.Text = txtBefore + clipboard + txtAfter;*/ this.txtContent.Paste(); } private void mnuText_Copy_Click(object sender, EventArgs e) { //Clipboard.SetText(this.txtContent.Text.Substring(this.txtContent.SelectionStart, this.txtContent.SelectionLength)); this.txtContent.Copy(); } private void mnuText_Cur_Click(object sender, EventArgs e) { /*Clipboard.SetText(this.txtContent.Text.Substring(this.txtContent.SelectionStart, this.txtContent.SelectionLength)); string txtBefore = this.txtContent.Text.Substring(0, this.txtContent.SelectionStart); string txtAfter = this.txtContent.Text.Substring(this.txtContent.SelectionStart + this.txtContent.SelectionLength); this.txtContent.Text = txtBefore + txtAfter;*/ this.txtContent.Cut(); } private void mnuText_Undo_Click(object sender, EventArgs e) { this.txtContent.Undo(); } private void mnuTextBox_Opening(object sender, CancelEventArgs e) { this.mnuText_Undo.Enabled = txtContent.CanUndo; this.mnuText_Redo.Enabled = false; this.mnuText_Cur.Enabled = txtContent.SelectionLength > 0 && txtContent.Enabled; this.mnuText_Copy.Enabled = txtContent.SelectionLength > 0; this.mnuText_Paste.Enabled = Clipboard.ContainsText(); this.mnuText_SelectAll.Enabled = txtContent.CanSelect; this.mnuText_Add.Enabled = txtContent.Enabled; this.mnuText_Speech.Enabled = txtContent.Enabled; setupMenuItem(this.mnuText_Add, mnuTextAdd_Click); setupMenuItem(this.mnuText_Speech, mnuTextAdd_Click); } private void mnuTextAdd_Click(object sender, EventArgs e) { ToolStripMenuItem mnu = (ToolStripMenuItem)sender; if (mnu != null) { bool bHasClipboard = Clipboard.ContainsText(); string t=""; if(bHasClipboard) t = Clipboard.GetText(); //string txtBefore = this.txtContent.Text.Substring(0, this.txtContent.SelectionStart); //string txtAfter = this.txtContent.Text.Substring(this.txtContent.SelectionStart + this.txtContent.SelectionLength); string selectedText = this.txtContent.Text.Substring(this.txtContent.SelectionStart, this.txtContent.SelectionLength); string tag = mnu.Tag.ToString().Replace("%",selectedText); //this.txtContent.Text = txtBefore + tag + txtAfter; this.txtContent.Paste(tag); if(bHasClipboard) { Clipboard.SetText(t); } else { Clipboard.Clear(); } } } private void setupMenuItem(ToolStripMenuItem mnu, EventHandler e) { foreach(object o in mnu.DropDownItems) { if (o.GetType() == typeof(ToolStripMenuItem)) { ToolStripMenuItem m = (ToolStripMenuItem)o; if (m.Tag == null) { if (m.DropDownItems.Count == 0) { m.Enabled = false; } else { m.Enabled = true; setupMenuItem(m, e); } } else { m.Enabled = true; if (String.IsNullOrEmpty(m.ToolTipText)) { m.Click += e; m.ToolTipText = m.Text; } m.Text = m.ToolTipText + ": " + System.Speech.SpeechFormat.Format(m.Tag.ToString()); } } } } } }