ComboBoxProperty.xaml.cs 12 KB

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