frmContentEdit.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Collections.Specialized;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Leah.Speech.text;
  12. using System.Speech.Synthesis;
  13. namespace ContentEditor {
  14. public partial class frmContentEdit : Form {
  15. private int currentItem = 0;
  16. private DialogResult frmResult = DialogResult.Cancel;
  17. private System.Speech.Synthesis.SpeechSynthesizer Speech = new SpeechSynthesizer();
  18. private bool bIsPlaying = false;
  19. public frmContentEdit() {
  20. InitializeComponent();
  21. this.FormClosed += FrmContentEdit_FormClosed;
  22. this.FormClosing += FrmContentEdit_FormClosing;
  23. this.dgvTags.UserAddedRow += DgvTags_UserAddedRow;
  24. this.dgvOptionalTags.UserAddedRow += DgvOptionalTags_UserAddedRow;
  25. this.KeyDown += FrmContentEdit_KeyDown;
  26. this.txtContent.KeyDown += FrmContentEdit_KeyDown;
  27. this.FormClosing += FrmContentEdit_FormClosing1;
  28. Speech.SpeakStarted += Speech_SpeakStarted;
  29. Speech.SpeakCompleted += Speech_SpeakCompleted;
  30. Speech.SelectVoiceByHints(VoiceGender.Female);
  31. InstalledVoice[] voices = Speech.GetInstalledVoices().ToArray();
  32. foreach(InstalledVoice voice in voices) {
  33. string gender = "None";
  34. if (voice.VoiceInfo.Gender == VoiceGender.Female)
  35. gender = "female";
  36. else if (voice.VoiceInfo.Gender == VoiceGender.Male)
  37. gender = "male";
  38. else if (voice.VoiceInfo.Gender == VoiceGender.Neutral)
  39. gender = "neutral";
  40. System.Diagnostics.Debug.WriteLine(
  41. "{" +
  42. "id: " + voice.VoiceInfo.Id + ", " +
  43. "name: " + voice.VoiceInfo.Name + ", " +
  44. "age: " + voice.VoiceInfo.Age + ", " +
  45. "Gender: " + gender + ", " +
  46. "description: " + voice.VoiceInfo.Description +
  47. "}"
  48. );
  49. }
  50. }
  51. private void FrmContentEdit_FormClosing1(object sender, FormClosingEventArgs e) {
  52. if(this.bIsPlaying) {
  53. this.Speech.SpeakAsyncCancelAll();
  54. }
  55. }
  56. private void FrmContentEdit_KeyDown(object sender, KeyEventArgs e) {
  57. if(e.KeyCode == Keys.F3) {
  58. this.Speak();
  59. }
  60. }
  61. private void Speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e) {
  62. this.bIsPlaying = false;
  63. this.cmdPlay.Enabled = true;
  64. }
  65. private void Speech_SpeakStarted(object sender, SpeakStartedEventArgs e) {
  66. this.bIsPlaying = true;
  67. this.cmdPlay.Enabled = false;
  68. }
  69. private void DgvOptionalTags_UserAddedRow(object sender, DataGridViewRowEventArgs e) {
  70. e.Row.Cells[0].Value = true;
  71. }
  72. private void DgvTags_UserAddedRow(object sender, DataGridViewRowEventArgs e) {
  73. e.Row.Cells[0].Value = true;
  74. }
  75. private void FrmContentEdit_FormClosing(object sender, FormClosingEventArgs e) {
  76. this.DialogResult = this.frmResult;
  77. }
  78. private void FrmContentEdit_FormClosed(object sender, FormClosedEventArgs e) {
  79. this.DialogResult = this.frmResult;
  80. }
  81. public static ContentEntry EditItem(int i) {
  82. frmContentEdit frm = new frmContentEdit();
  83. /*frm.lstTags.Items.Clear();
  84. frm.lstTags.Items.AddRange(frmMain.Content.Tags);
  85. frm.lstOptionalTags.Items.Clear();
  86. frm.lstOptionalTags.Items.AddRange(frmMain.Content.OptionalTags);*/
  87. frm.dgvTags.Rows.Clear();
  88. for(int ti=0,c=frmMain.Content.Tags.Length;ti<c;ti++) {
  89. DataGridViewRow row = new DataGridViewRow();
  90. row.Tag = ti;
  91. row.Cells.AddRange(
  92. new DataGridViewCheckBoxCell(),
  93. new DataGridViewTextBoxCell() { Value = frmMain.Content.Tags[ti] }
  94. );
  95. frm.dgvTags.Rows.Add(row);
  96. }
  97. frm.dgvOptionalTags.Rows.Clear();
  98. for (int ti = 0, c = frmMain.Content.OptionalTags.Length; ti < c; ti++) {
  99. DataGridViewRow row = new DataGridViewRow();
  100. row.Tag = ti;
  101. row.Cells.AddRange(
  102. new DataGridViewCheckBoxCell(),
  103. new DataGridViewTextBoxCell() { Value = frmMain.Content.OptionalTags[ti] }
  104. );
  105. frm.dgvOptionalTags.Rows.Add(row);
  106. }
  107. frm.LoadIndex(i);
  108. frm.DialogResult = DialogResult.Cancel;
  109. if (frm.ShowDialog() == DialogResult.OK) {
  110. return frm.GetEntry();
  111. } else {
  112. return null;
  113. }
  114. }
  115. public static ContentEntry NewItem() {
  116. frmContentEdit frm = new frmContentEdit();
  117. /*frm.lstTags.Items.Clear();
  118. frm.lstTags.Items.AddRange(frmMain.Content.Tags);
  119. frm.lstOptionalTags.Items.Clear();
  120. frm.lstOptionalTags.Items.AddRange(frmMain.Content.OptionalTags);*/
  121. frm.dgvTags.Rows.Clear();
  122. for (int ti = 0, c = frmMain.Content.Tags.Length; ti < c; ti++) {
  123. DataGridViewRow row = new DataGridViewRow();
  124. row.Tag = ti;
  125. row.Cells.AddRange(
  126. new DataGridViewCheckBoxCell(),
  127. new DataGridViewTextBoxCell() { Value = frmMain.Content.Tags[ti] }
  128. );
  129. frm.dgvTags.Rows.Add(row);
  130. }
  131. frm.dgvOptionalTags.Rows.Clear();
  132. for (int ti = 0, c = frmMain.Content.OptionalTags.Length; ti < c; ti++) {
  133. DataGridViewRow row = new DataGridViewRow();
  134. row.Tag = ti;
  135. row.Cells.AddRange(
  136. new DataGridViewCheckBoxCell(),
  137. new DataGridViewTextBoxCell() { Value = frmMain.Content.OptionalTags[ti] }
  138. );
  139. frm.dgvOptionalTags.Rows.Add(row);
  140. }
  141. frm.LoadIndex(-1);
  142. frm.DialogResult = DialogResult.Cancel;
  143. if (frm.ShowDialog()== DialogResult.OK) {
  144. return frm.GetEntry();
  145. } else {
  146. return null;
  147. }
  148. }
  149. public void LoadIndex(int i) {
  150. System.Diagnostics.Debug.WriteLine("LoadIndex(" + i + ")");
  151. System.Diagnostics.Debug.WriteLine("- Entry Count: "+ frmMain.Content.Count);
  152. if (frmMain.Content.Count==0) {
  153. i = -1;
  154. }
  155. this.itemNumChangeEvent = false;
  156. if (i == -1) {
  157. System.Diagnostics.Debug.WriteLine("- Entry: New");
  158. this.txtItemNum.Text = "New";
  159. this.currentItem = -1;
  160. } else {
  161. ContentEntry entry = frmMain.Content.GetEntry(i);
  162. System.Diagnostics.Debug.WriteLine("- Entry: " + (entry==null?"Null":"Found"));
  163. if (entry != null) {
  164. System.Diagnostics.Debug.WriteLine("- Entry Text: " + entry.Text);
  165. System.Diagnostics.Debug.WriteLine("- Entry Mood: " + entry.MoodText);
  166. this.txtContent.Text = entry.Text;
  167. this.CheckMood(entry.Mood);
  168. this.CheckTags(entry.Tags);
  169. this.CheckOptionalTags(entry.OptionalTags);
  170. } else {
  171. this.txtContent.Text = "";
  172. }
  173. this.currentItem = i;
  174. this.txtItemNum.Text = (i + 1).ToString();
  175. }
  176. this.lblItemCount.Text = "| " + frmMain.Content.Count.ToString();
  177. //this.cmdPrevItem.Enabled = i > 1 || i == -1;
  178. //this.cmdNextItem.Enabled = i + 1 < frmMain.Content.Count || i==-1;
  179. this.cmdPrevItem.Enabled = frmMain.Content.Count > 1 || (frmMain.Content.Count == 1 && i == -1);
  180. this.cmdNextItem.Enabled = frmMain.Content.Count > 1 || (frmMain.Content.Count == 1 && i == -1);
  181. this.itemNumChangeEvent = true;
  182. }
  183. private void cmdMood_None_Click(object sender, EventArgs e) {
  184. this.MoodSelect(false);
  185. }
  186. public void CheckTags(ushort[] tags) {
  187. foreach(DataGridViewRow r in this.dgvTags.Rows) {
  188. if (r.Tag != null) {
  189. ushort t;
  190. string s = r.Tag.ToString();
  191. if (ushort.TryParse(s, out t)) {
  192. r.Cells[0].Value = tags.Contains(t);
  193. }
  194. }
  195. }
  196. }
  197. public void CheckTags(bool All) {
  198. foreach (DataGridViewRow r in this.dgvTags.Rows) {
  199. r.Cells[0].Value = All;
  200. }
  201. }
  202. public void CheckOptionalTags(ushort[] tags) {
  203. foreach (DataGridViewRow r in this.dgvOptionalTags.Rows) {
  204. if (r.Tag != null) {
  205. ushort t;
  206. string s = r.Tag.ToString();
  207. if (ushort.TryParse(s, out t)) {
  208. r.Cells[0].Value = tags.Contains(t);
  209. }
  210. }
  211. }
  212. }
  213. public void CheckOptionalTags(bool All) {
  214. foreach (DataGridViewRow r in this.dgvOptionalTags.Rows) {
  215. r.Cells[0].Value = All;
  216. }
  217. }
  218. public void CheckMood(eMood mood) {
  219. chkMood_Angry.Checked = ((mood | Leah.Speech.text.eMood.angry) == mood);
  220. chkMood_Annoyed.Checked = ((mood | Leah.Speech.text.eMood.annoyed) == mood);
  221. chkMood_Aroused.Checked = ((mood | Leah.Speech.text.eMood.aroused) == mood);
  222. chkMood_Bored.Checked = ((mood | Leah.Speech.text.eMood.bored) == mood);
  223. chkMood_Compfortable.Checked = ((mood | Leah.Speech.text.eMood.compfortable) == mood);
  224. chkMood_Depressive.Checked = ((mood | Leah.Speech.text.eMood.depressive) == mood);
  225. chkMood_Energy.Checked = ((mood | Leah.Speech.text.eMood.energy) == mood);
  226. chkMood_Excited.Checked = ((mood | Leah.Speech.text.eMood.excited) == mood);
  227. chkMood_Exhausted.Checked = ((mood | Leah.Speech.text.eMood.exhausted) == mood);
  228. chkMood_Freezy.Checked = ((mood | Leah.Speech.text.eMood.freezy) == mood);
  229. chkMood_Funny.Checked = ((mood | Leah.Speech.text.eMood.funny) == mood);
  230. chkMood_Happyness.Checked = ((mood | Leah.Speech.text.eMood.happyness) == mood);
  231. chkMood_Hot.Checked = ((mood | Leah.Speech.text.eMood.hot) == mood);
  232. chkMood_Humanity.Checked = ((mood | Leah.Speech.text.eMood.humanity) == mood);
  233. chkMood_Hungry.Checked = ((mood | Leah.Speech.text.eMood.hungry) == mood);
  234. chkMood_Intelligent.Checked = ((mood | Leah.Speech.text.eMood.intelligent) == mood);
  235. chkMood_Irritated.Checked = ((mood | Leah.Speech.text.eMood.irritated) == mood);
  236. chkMood_Lonely.Checked = ((mood | Leah.Speech.text.eMood.lonely) == mood);
  237. chkMood_Naiv.Checked = ((mood | Leah.Speech.text.eMood.naiv) == mood);
  238. chkMood_Playful.Checked = ((mood | Leah.Speech.text.eMood.playful) == mood);
  239. chkMood_Sad.Checked = ((mood | Leah.Speech.text.eMood.sad) == mood);
  240. chkMood_Shy.Checked = ((mood | Leah.Speech.text.eMood.shy) == mood);
  241. chkMood_Sleepy.Checked = ((mood | Leah.Speech.text.eMood.sleepy) == mood);
  242. chkMood_Soziality.Checked = ((mood | Leah.Speech.text.eMood.soziality) == mood);
  243. chkMood_Temperate.Checked = ((mood | Leah.Speech.text.eMood.temperate) == mood);
  244. chkMood_ToLoud.Checked = ((mood | Leah.Speech.text.eMood.toLoud) == mood);
  245. chkMood_ToQuiet.Checked = ((mood | Leah.Speech.text.eMood.toQuiet) == mood);
  246. chkMood_Usefull.Checked = ((mood | Leah.Speech.text.eMood.usefull) == mood);
  247. chkMood_Useless.Checked = ((mood | Leah.Speech.text.eMood.useless) == mood);
  248. }
  249. public eMood GetMood() {
  250. eMood res = eMood.None;
  251. if (chkMood_Angry.Checked) res |= eMood.angry;
  252. if (chkMood_Annoyed.Checked) res |= eMood.annoyed;
  253. if (chkMood_Aroused.Checked) res |= eMood.aroused;
  254. if (chkMood_Bored.Checked) res |= eMood.bored;
  255. if (chkMood_Compfortable.Checked) res |= eMood.compfortable;
  256. if (chkMood_Depressive.Checked) res |= eMood.depressive;
  257. if (chkMood_Energy.Checked) res |= eMood.energy;
  258. if (chkMood_Excited.Checked) res |= eMood.excited;
  259. if (chkMood_Exhausted.Checked) res |= eMood.exhausted;
  260. if (chkMood_Freezy.Checked) res |= eMood.freezy;
  261. if (chkMood_Funny.Checked) res |= eMood.funny;
  262. if (chkMood_Happyness.Checked) res |= eMood.happyness;
  263. if (chkMood_Hot.Checked) res |= eMood.hot;
  264. if (chkMood_Humanity.Checked) res |= eMood.humanity;
  265. if (chkMood_Hungry.Checked) res |= eMood.hungry;
  266. if (chkMood_Intelligent.Checked) res |= eMood.intelligent;
  267. if (chkMood_Irritated.Checked) res |= eMood.irritated;
  268. if (chkMood_Lonely.Checked) res |= eMood.lonely;
  269. if (chkMood_Naiv.Checked) res |= eMood.naiv;
  270. if (chkMood_Playful.Checked) res |= eMood.playful;
  271. if (chkMood_Sad.Checked) res |= eMood.sad;
  272. if (chkMood_Shy.Checked) res |= eMood.shy;
  273. if (chkMood_Sleepy.Checked) res |= eMood.sleepy;
  274. if (chkMood_Soziality.Checked) res |= eMood.soziality;
  275. if (chkMood_Temperate.Checked) res |= eMood.temperate;
  276. if (chkMood_ToLoud.Checked) res |= eMood.toLoud;
  277. if (chkMood_ToQuiet.Checked) res |= eMood.toQuiet;
  278. if (chkMood_Usefull.Checked) res |= eMood.usefull;
  279. if (chkMood_Useless.Checked) res |= eMood.useless;
  280. return res;
  281. }
  282. public ContentEntry GetEntry() {
  283. ContentEntry res = new ContentEntry(this.Text.Trim(), this.GetMood());
  284. return res;
  285. }
  286. /*private void SelectListBox(ListBox lst, bool select) {
  287. if(select) {
  288. //lst.selected
  289. } else {
  290. lst.ClearSelected();
  291. }
  292. }*/
  293. private void MoodSelect(bool select) {
  294. chkMood_Angry.Checked = select;
  295. chkMood_Annoyed.Checked = select;
  296. chkMood_Aroused.Checked = select;
  297. chkMood_Bored.Checked = select;
  298. chkMood_Compfortable.Checked = select;
  299. chkMood_Depressive.Checked = select;
  300. chkMood_Energy.Checked = select;
  301. chkMood_Excited.Checked = select;
  302. chkMood_Exhausted.Checked = select;
  303. chkMood_Freezy.Checked = select;
  304. chkMood_Funny.Checked = select;
  305. chkMood_Happyness.Checked = select;
  306. chkMood_Hot.Checked = select;
  307. chkMood_Humanity.Checked = select;
  308. chkMood_Hungry.Checked = select;
  309. chkMood_Intelligent.Checked = select;
  310. chkMood_Irritated.Checked = select;
  311. chkMood_Lonely.Checked = select;
  312. chkMood_Naiv.Checked = select;
  313. chkMood_Playful.Checked = select;
  314. chkMood_Sad.Checked = select;
  315. chkMood_Shy.Checked = select;
  316. chkMood_Sleepy.Checked = select;
  317. chkMood_Soziality.Checked = select;
  318. chkMood_Temperate.Checked = select;
  319. chkMood_ToLoud.Checked = select;
  320. chkMood_ToQuiet.Checked = select;
  321. chkMood_Usefull.Checked = select;
  322. chkMood_Useless.Checked = select;
  323. }
  324. private void cmdMood_All_Click(object sender, EventArgs e) {
  325. this.MoodSelect(true);
  326. }
  327. private void cmdNextItem_Click(object sender, EventArgs e) {
  328. if (this.currentItem + 1 < frmMain.Content.Count)
  329. this.LoadIndex(this.currentItem + 1);
  330. else
  331. this.LoadIndex(0);
  332. }
  333. private void cmdPrevItem_Click(object sender, EventArgs e) {
  334. if(this.currentItem>0)
  335. this.LoadIndex(this.currentItem - 1);
  336. else
  337. this.LoadIndex(frmMain.Content.Count-1);
  338. }
  339. private bool itemNumChangeEvent = true;
  340. private void txtItemNum_TextChanged(object sender, EventArgs e) {
  341. if (this.itemNumChangeEvent) {
  342. int page = 0;
  343. if (int.TryParse(this.txtItemNum.Text, out page)) {
  344. this.LoadIndex(page);
  345. } else if(this.txtItemNum.Text.ToLower().Trim()=="new") {
  346. this.LoadIndex(-1);
  347. } else {
  348. this.itemNumChangeEvent = false;
  349. txtItemNum.Text = this.currentItem.ToString();
  350. this.itemNumChangeEvent = true;
  351. }
  352. }
  353. }
  354. private void cmdTags_None_Click(object sender, EventArgs e) {
  355. //this.SelectListBox(lstTags, false);
  356. this.CheckOptionalTags(false);
  357. }
  358. private void cmdTags_All_Click(object sender, EventArgs e) {
  359. //this.SelectListBox(lstTags, true);
  360. this.CheckOptionalTags(true);
  361. }
  362. private void cmdOptionalTags_None_Click(object sender, EventArgs e) {
  363. //this.SelectListBox(lstOptionalTags, false);
  364. this.CheckOptionalTags(false);
  365. }
  366. private void cmdOptionalTags_All_Click(object sender, EventArgs e) {
  367. //this.SelectListBox(lstOptionalTags, true);
  368. this.CheckOptionalTags(true);
  369. }
  370. private void cmdNew_Click(object sender, EventArgs e) {
  371. //this.DialogResult = DialogResult.OK;
  372. this.SaveEntry();
  373. this.LoadIndex(-1);
  374. }
  375. private void SaveEntry() {
  376. ContentEntry entry;
  377. if(this.currentItem>=0) {
  378. System.Diagnostics.Debug.WriteLine("Fetch entry: " + this.currentItem);
  379. entry = frmMain.Content.GetEntry(this.currentItem);
  380. } else {
  381. entry = new ContentEntry();
  382. if(frmMain.Content.Add(entry)) {
  383. this.currentItem = frmMain.Content.Count - 1;
  384. System.Diagnostics.Debug.WriteLine("new item added with id: "+this.currentItem.ToString());
  385. } else {
  386. System.Diagnostics.Debug.WriteLine("Failed to add item");
  387. }
  388. }
  389. if(entry!=null) {
  390. frmMain.bNeedAutoSave = true;
  391. entry.Text = this.txtContent.Text.Trim();
  392. entry.Mood = this.GetMood();
  393. entry.Tags = new ushort[] { };
  394. foreach (DataGridViewRow r in this.dgvTags.Rows) {
  395. 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) {
  396. r.Tag = frmMain.Content.Tag_Add((string)r.Cells[1].Value);
  397. }
  398. if (r.Tag != null && r.Cells[0].Value != null) {
  399. ushort tagId;
  400. if (ushort.TryParse(r.Tag.ToString(), out tagId)) {
  401. if ((bool)r.Cells[0].Value == true) {
  402. entry.Tag_Add(tagId);
  403. }
  404. }
  405. }
  406. }
  407. entry.OptionalTags = new ushort[] { };
  408. foreach (DataGridViewRow r in this.dgvOptionalTags.Rows) {
  409. 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) {
  410. r.Tag = frmMain.Content.OptionalTag_Add((string)r.Cells[1].Value);
  411. }
  412. if (r.Tag != null && r.Cells[0].Value != null) {
  413. ushort tagId;
  414. if (ushort.TryParse(r.Tag.ToString(), out tagId)) {
  415. if ((bool)r.Cells[0].Value == true) {
  416. entry.OptionalTag_Add(tagId);
  417. }
  418. }
  419. }
  420. }
  421. frmMain.Content.Entries[this.currentItem] = entry;
  422. } else {
  423. System.Diagnostics.Debug.WriteLine("no entry");
  424. }
  425. }
  426. private void cmdOk_Click(object sender, EventArgs e) {
  427. this.frmResult = DialogResult.OK;
  428. this.SaveEntry();
  429. this.Close();
  430. }
  431. private void cmdAbort_Click(object sender, EventArgs e) {
  432. this.Close();
  433. }
  434. private void cmdAdd_Click(object sender, EventArgs e) {
  435. this.frmResult = DialogResult.OK;
  436. this.SaveEntry();
  437. this.LoadIndex(-1);
  438. }
  439. private void cmdPlay_Click(object sender, EventArgs e) {
  440. this.Speak();
  441. }
  442. private void Speak() {
  443. frmMain.Speak(this.txtContent.Text);
  444. }
  445. private void toolStripMenuItem1_Click(object sender, EventArgs e) {
  446. }
  447. private void mnuText_SelectAll_Click(object sender, EventArgs e) {
  448. this.txtContent.SelectAll();
  449. }
  450. private void mnuText_Paste_Click(object sender, EventArgs e) {
  451. /*string txtBefore = this.txtContent.Text.Substring(0, this.txtContent.SelectionStart);
  452. string txtAfter = this.txtContent.Text.Substring(this.txtContent.SelectionStart + this.txtContent.SelectionLength);
  453. string clipboard = Clipboard.GetText();
  454. this.txtContent.Text = txtBefore + clipboard + txtAfter;*/
  455. this.txtContent.Paste();
  456. }
  457. private void mnuText_Copy_Click(object sender, EventArgs e) {
  458. //Clipboard.SetText(this.txtContent.Text.Substring(this.txtContent.SelectionStart, this.txtContent.SelectionLength));
  459. this.txtContent.Copy();
  460. }
  461. private void mnuText_Cur_Click(object sender, EventArgs e) {
  462. /*Clipboard.SetText(this.txtContent.Text.Substring(this.txtContent.SelectionStart, this.txtContent.SelectionLength));
  463. string txtBefore = this.txtContent.Text.Substring(0, this.txtContent.SelectionStart);
  464. string txtAfter = this.txtContent.Text.Substring(this.txtContent.SelectionStart + this.txtContent.SelectionLength);
  465. this.txtContent.Text = txtBefore + txtAfter;*/
  466. this.txtContent.Cut();
  467. }
  468. private void mnuText_Undo_Click(object sender, EventArgs e) {
  469. this.txtContent.Undo();
  470. }
  471. private void mnuTextBox_Opening(object sender, CancelEventArgs e) {
  472. this.mnuText_Undo.Enabled = txtContent.CanUndo;
  473. this.mnuText_Redo.Enabled = false;
  474. this.mnuText_Cur.Enabled = txtContent.SelectionLength > 0 && txtContent.Enabled;
  475. this.mnuText_Copy.Enabled = txtContent.SelectionLength > 0;
  476. this.mnuText_Paste.Enabled = Clipboard.ContainsText();
  477. this.mnuText_SelectAll.Enabled = txtContent.CanSelect;
  478. this.mnuText_Add.Enabled = txtContent.Enabled;
  479. this.mnuText_Speech.Enabled = txtContent.Enabled;
  480. setupMenuItem(this.mnuText_Add, mnuTextAdd_Click);
  481. setupMenuItem(this.mnuText_Speech, mnuTextAdd_Click);
  482. }
  483. private void mnuTextAdd_Click(object sender, EventArgs e) {
  484. ToolStripMenuItem mnu = (ToolStripMenuItem)sender;
  485. if (mnu != null) {
  486. bool bHasClipboard = Clipboard.ContainsText();
  487. string t="";
  488. if(bHasClipboard)
  489. t = Clipboard.GetText();
  490. //string txtBefore = this.txtContent.Text.Substring(0, this.txtContent.SelectionStart);
  491. //string txtAfter = this.txtContent.Text.Substring(this.txtContent.SelectionStart + this.txtContent.SelectionLength);
  492. string selectedText = this.txtContent.Text.Substring(this.txtContent.SelectionStart, this.txtContent.SelectionLength);
  493. string tag = mnu.Tag.ToString().Replace("%",selectedText);
  494. //this.txtContent.Text = txtBefore + tag + txtAfter;
  495. this.txtContent.Paste(tag);
  496. if(bHasClipboard) {
  497. Clipboard.SetText(t);
  498. } else {
  499. Clipboard.Clear();
  500. }
  501. }
  502. }
  503. private void setupMenuItem(ToolStripMenuItem mnu, EventHandler e) {
  504. foreach(object o in mnu.DropDownItems) {
  505. if (o.GetType() == typeof(ToolStripMenuItem)) {
  506. ToolStripMenuItem m = (ToolStripMenuItem)o;
  507. if (m.Tag == null) {
  508. if (m.DropDownItems.Count == 0) {
  509. m.Enabled = false;
  510. } else {
  511. m.Enabled = true;
  512. setupMenuItem(m, e);
  513. }
  514. } else {
  515. m.Enabled = true;
  516. if (String.IsNullOrEmpty(m.ToolTipText)) {
  517. m.Click += e;
  518. m.ToolTipText = m.Text;
  519. }
  520. m.Text = m.ToolTipText + ": " + System.Speech.SpeechFormat.Format(m.Tag.ToString());
  521. }
  522. }
  523. }
  524. }
  525. }
  526. }