ComboBoxProperty.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Media;
  10. namespace Compdfkit_Tools.PDFControl
  11. {
  12. public partial class ComboBoxProperty : UserControl
  13. {
  14. private WidgetComboBoxArgs widgetArgs = null;
  15. private AnnotAttribEvent annotAttribEvent = null;
  16. private Dictionary<string, string> itemlists = null;
  17. public ObservableCollection<int> SizeList { get; set; } = new ObservableCollection<int>
  18. {
  19. 6,8,9,10,12,14,18,20,24,26,28,32,30,32,48,72
  20. };
  21. bool IsLoadedData = false;
  22. public ComboBoxProperty()
  23. {
  24. InitializeComponent();
  25. }
  26. #region Loaded
  27. public void SetProperty(WidgetArgs Args, AnnotAttribEvent e)
  28. {
  29. widgetArgs = (WidgetComboBoxArgs)Args;
  30. annotAttribEvent = e;
  31. }
  32. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  33. {
  34. Binding SizeListbinding = new Binding();
  35. SizeListbinding.Source = this;
  36. SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
  37. FontSizeCmb.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
  38. FieldNameText.Text = widgetArgs.FieldName;
  39. FormFieldCmb.SelectedIndex = (int)widgetArgs.FormField;
  40. BorderColorPickerControl.SetCheckedForColor(widgetArgs.LineColor);
  41. BackgroundColorPickerControl.SetCheckedForColor(widgetArgs.BgColor);
  42. TextColorPickerControl.SetCheckedForColor(widgetArgs.FontColor);
  43. SetFontName(widgetArgs.FontName);
  44. SetFontStyle(widgetArgs.IsItalic, widgetArgs.IsBold);
  45. SetFontSize(widgetArgs.FontSize);
  46. itemlists = widgetArgs.ListOptions;
  47. if (itemlists != null)
  48. {
  49. foreach (string key in itemlists.Keys)
  50. {
  51. ListBoxItem item = new ListBoxItem();
  52. item.Content = key;
  53. item.Tag = itemlists[key];
  54. itemsListBox.Items.Add(item);
  55. }
  56. CheckListCount();
  57. }
  58. TopTabControl.SelectedIndex = 2;
  59. IsLoadedData = true;
  60. }
  61. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  62. {
  63. IsLoadedData = false;
  64. }
  65. private void SetFontSize(double size)
  66. {
  67. int index = SizeList.IndexOf((int)size);
  68. FontSizeCmb.SelectedIndex = index;
  69. }
  70. private void SetFontStyle(bool IsItalic, bool IsBold)
  71. {
  72. int index = 0;
  73. if (IsItalic && IsBold)
  74. {
  75. index = 3;
  76. }
  77. else if (IsItalic)
  78. {
  79. index = 2;
  80. }
  81. else if (IsBold)
  82. {
  83. index = 1;
  84. }
  85. FontStyleCmb.SelectedIndex = index;
  86. }
  87. private void SetFontName(string fontName)
  88. {
  89. int index = -1;
  90. List<string> fontFamilyList = new List<string>() { "Helvetica", "Courier", "Times" };
  91. for (int i = 0; i < fontFamilyList.Count; i++)
  92. {
  93. if (fontFamilyList[i].ToLower().Contains(fontName.ToLower())
  94. || fontName.ToLower().Contains(fontFamilyList[i].ToLower()))
  95. {
  96. index = i;
  97. }
  98. }
  99. FontCmb.SelectedIndex = index;
  100. }
  101. #endregion
  102. private void FieldNameText_TextChanged(object sender, TextChangedEventArgs e)
  103. {
  104. if (IsLoadedData)
  105. {
  106. annotAttribEvent.UpdateAttrib(AnnotAttrib.FieldName, (sender as TextBox).Text);
  107. annotAttribEvent.UpdateAnnot();
  108. }
  109. }
  110. private void FormFieldCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  111. {
  112. if (IsLoadedData)
  113. {
  114. annotAttribEvent.UpdateAttrib(AnnotAttrib.FormField, (sender as ComboBox).SelectedIndex);
  115. annotAttribEvent.UpdateAnnot();
  116. }
  117. }
  118. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  119. {
  120. if (IsLoadedData)
  121. {
  122. annotAttribEvent.UpdateAttrib(AnnotAttrib.Color, ((SolidColorBrush)BorderColorPickerControl.Brush).Color);
  123. annotAttribEvent.UpdateAnnot();
  124. }
  125. }
  126. private void BackgroundColorPickerControl_ColorChanged(object sender, EventArgs e)
  127. {
  128. if (IsLoadedData)
  129. {
  130. annotAttribEvent.UpdateAttrib(AnnotAttrib.FillColor, ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color);
  131. annotAttribEvent.UpdateAnnot();
  132. }
  133. }
  134. private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
  135. {
  136. if (IsLoadedData)
  137. {
  138. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontColor, ((SolidColorBrush)TextColorPickerControl.Brush).Color);
  139. annotAttribEvent.UpdateAnnot();
  140. }
  141. }
  142. private void FontCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  143. {
  144. if (IsLoadedData)
  145. {
  146. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontName, ((sender as ComboBox).SelectedItem as ComboBoxItem).Content.ToString());
  147. annotAttribEvent.UpdateAnnot();
  148. }
  149. }
  150. private void FontStyleCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  151. {
  152. if (IsLoadedData)
  153. {
  154. bool IsItalic = false;
  155. bool IsBold = false;
  156. switch ((sender as ComboBox).SelectedIndex)
  157. {
  158. case 0:
  159. break;
  160. case 1:
  161. IsBold = true;
  162. break;
  163. case 2:
  164. IsItalic = true;
  165. break;
  166. case 3:
  167. IsItalic = true;
  168. IsBold = true;
  169. break;
  170. default:
  171. break;
  172. }
  173. annotAttribEvent.UpdateAttrib(AnnotAttrib.IsBold, IsBold);
  174. annotAttribEvent.UpdateAttrib(AnnotAttrib.IsItalic, IsItalic);
  175. annotAttribEvent.UpdateAnnot();
  176. }
  177. }
  178. private void FontSizeCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  179. {
  180. if (IsLoadedData)
  181. {
  182. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontSize, (sender as ComboBox).SelectedItem);
  183. annotAttribEvent.UpdateAnnot();
  184. }
  185. }
  186. private void txtItemInput_TextChanged(object sender, TextChangedEventArgs e)
  187. {
  188. if (itemlists.ContainsKey(txtItemInput.Text.Trim()))
  189. {
  190. btnAddItem.IsEnabled = false;
  191. }
  192. else
  193. {
  194. if (!string.IsNullOrEmpty(txtItemInput.Text))
  195. btnAddItem.IsEnabled = true;
  196. }
  197. }
  198. private void btnAddItem_Click(object sender, RoutedEventArgs e)
  199. {
  200. itemlists.Add(txtItemInput.Text, txtItemInput.Text);
  201. ListBoxItem item = new ListBoxItem();
  202. item.Content = txtItemInput.Text;
  203. item.Tag = txtItemInput.Text;
  204. itemsListBox.Items.Add(item);
  205. UpdateListItems();
  206. txtItemInput.Text = "";
  207. txtItemInput.Focus();
  208. btnAddItem.IsEnabled = false;
  209. }
  210. private void UpdateListItems()
  211. {
  212. Dictionary<string, string> pairs = new Dictionary<string, string>();
  213. foreach (ListBoxItem item in itemsListBox.Items)
  214. {
  215. if (item.Content != null && item.Tag != null)
  216. {
  217. pairs.Add(item.Content.ToString(), item.Tag.ToString());
  218. }
  219. }
  220. annotAttribEvent?.UpdateAttrib(AnnotAttrib.ListOptions, pairs);
  221. if (itemsListBox.SelectedIndex > -1)
  222. annotAttribEvent?.UpdateAttrib(AnnotAttrib.DefaultChoice, new List<int>() { itemsListBox.SelectedIndex });
  223. annotAttribEvent?.UpdateAnnot();
  224. CheckListCount();
  225. }
  226. private void CheckListCount()
  227. {
  228. if (itemsListBox.Items.Count > 0)
  229. TipPanel.Visibility = Visibility.Visible;
  230. else
  231. TipPanel.Visibility = Visibility.Collapsed;
  232. }
  233. private void itemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  234. {
  235. if (e.AddedItems.Count > 0)
  236. {
  237. btnDelete.IsEnabled = true;
  238. if (itemsListBox.SelectedIndex <= 0)
  239. {
  240. btnMoveUp.IsEnabled = false;
  241. }
  242. else
  243. {
  244. btnMoveUp.IsEnabled = true;
  245. }
  246. if (itemsListBox.SelectedIndex >= itemsListBox.Items.Count - 1)
  247. {
  248. btnMoveDown.IsEnabled = false;
  249. }
  250. else
  251. {
  252. btnMoveDown.IsEnabled = true;
  253. }
  254. txtItemInput.Text = (itemsListBox.SelectedItem as ListBoxItem).Content.ToString();
  255. txtItemInput.SelectAll();
  256. }
  257. if (itemsListBox.SelectedItems.Count <= 0)
  258. {
  259. btnDelete.IsEnabled = false;
  260. btnMoveDown.IsEnabled = false;
  261. btnMoveUp.IsEnabled = false;
  262. }
  263. if (itemsListBox.SelectedIndex >= 0)
  264. {
  265. annotAttribEvent?.UpdateAttrib(AnnotAttrib.DefaultChoice, new List<int>() { itemsListBox.SelectedIndex });
  266. annotAttribEvent?.UpdateAnnot();
  267. }
  268. }
  269. private void btnDelete_Click(object sender, RoutedEventArgs e)
  270. {
  271. if (itemsListBox.SelectedItems.Count > 0)
  272. {
  273. if ((itemsListBox.SelectedItem as ListBoxItem).Content != null)
  274. itemlists.Remove((itemsListBox.SelectedItem as ListBoxItem).Content.ToString());
  275. itemsListBox.Items.Remove(itemsListBox.SelectedItem as ListBoxItem);
  276. btnDelete.IsEnabled = false;
  277. UpdateListItems();
  278. txtItemInput.Text = "";
  279. }
  280. }
  281. private void btnMoveUp_Click(object sender, RoutedEventArgs e)
  282. {
  283. ListBoxItem newitem = new ListBoxItem();
  284. newitem.Content = (itemsListBox.SelectedItem as ListBoxItem).Content;
  285. newitem.Tag = (itemsListBox.SelectedItem as ListBoxItem).Tag;
  286. int index = itemsListBox.SelectedIndex;
  287. if (index - 1 >= 0)
  288. {
  289. itemsListBox.Items.Insert(index - 1, newitem);
  290. itemsListBox.Items.Remove(itemsListBox.SelectedItem);
  291. itemsListBox.SelectedIndex = index - 1;
  292. itemsListBox.Focus();
  293. UpdateListItems();
  294. }
  295. }
  296. private void btnMoveDown_Click(object sender, RoutedEventArgs e)
  297. {
  298. ListBoxItem newitem = new ListBoxItem();
  299. newitem.Content = (itemsListBox.SelectedItem as ListBoxItem).Content;
  300. newitem.Tag = (itemsListBox.SelectedItem as ListBoxItem).Tag;
  301. int index = itemsListBox.SelectedIndex;
  302. if (index + 1 <= itemsListBox.Items.Count)
  303. {
  304. itemsListBox.Items.Remove(itemsListBox.SelectedItem);
  305. itemsListBox.Items.Insert(index + 1, newitem);
  306. itemsListBox.SelectedIndex = index + 1;
  307. itemsListBox.Focus();
  308. UpdateListItems();
  309. }
  310. }
  311. }
  312. }