ComboBoxHistory.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.Help;
  5. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  6. namespace ComPDFKit.Tool.UndoManger
  7. {
  8. public class ComboBoxHistory:AnnotHistory
  9. {
  10. public override int GetAnnotIndex()
  11. {
  12. if (CurrentParam != null)
  13. {
  14. return CurrentParam.AnnotIndex;
  15. }
  16. return base.GetAnnotIndex();
  17. }
  18. public override int GetPageIndex()
  19. {
  20. if (CurrentParam != null)
  21. {
  22. return CurrentParam.PageIndex;
  23. }
  24. return base.GetPageIndex();
  25. }
  26. public override void SetAnnotIndex(int newIndex)
  27. {
  28. if (CurrentParam != null)
  29. {
  30. CurrentParam.AnnotIndex = newIndex;
  31. }
  32. if (PreviousParam != null)
  33. {
  34. PreviousParam.AnnotIndex = newIndex;
  35. }
  36. }
  37. internal override bool Add()
  38. {
  39. ComboBoxParam currentParam = CurrentParam as ComboBoxParam;
  40. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  41. {
  42. return false;
  43. }
  44. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  45. CPDFComboBoxWidget comboboxWidget = pdfPage.CreateWidget(C_WIDGET_TYPE.WIDGET_COMBOBOX) as CPDFComboBoxWidget;
  46. if (comboboxWidget != null)
  47. {
  48. int annotIndex = pdfPage.GetAnnotCount() - 1;
  49. if (!string.IsNullOrEmpty(currentParam.FieldName))
  50. {
  51. comboboxWidget.SetFieldName(currentParam.FieldName);
  52. }
  53. if(currentParam.HasLineColor)
  54. {
  55. if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
  56. {
  57. comboboxWidget.SetWidgetBorderRGBColor(currentParam.LineColor);
  58. }
  59. }
  60. if(currentParam.HasBgColor)
  61. {
  62. if (currentParam.BgColor != null && currentParam.BgColor.Length == 3)
  63. {
  64. comboboxWidget.SetWidgetBgRGBColor(currentParam.BgColor);
  65. }
  66. }
  67. comboboxWidget.SetBorderWidth((float)currentParam.LineWidth);
  68. comboboxWidget.SetWidgetBorderStyle(currentParam.BorderStyle);
  69. CTextAttribute textAttr = new CTextAttribute();
  70. byte[] fontColor = new byte[3];
  71. if (currentParam.FontColor != null && currentParam.FontColor.Length == 3)
  72. {
  73. fontColor = currentParam.FontColor;
  74. }
  75. textAttr.FontColor = fontColor;
  76. textAttr.FontSize = (float)currentParam.FontSize;
  77. textAttr.FontName = ObtainFontName(
  78. GetFontType(currentParam.FontName),
  79. currentParam.IsBold,
  80. currentParam.IsItalic);
  81. comboboxWidget.SetTextAttribute(textAttr);
  82. if(currentParam.OptionItems!=null && currentParam.OptionItems.Count > 0)
  83. {
  84. int addIndex = 0;
  85. foreach (string key in currentParam.OptionItems.Keys)
  86. {
  87. comboboxWidget.AddOptionItem(addIndex, currentParam.OptionItems[key], key);
  88. addIndex++;
  89. }
  90. }
  91. if(currentParam.SelectItemsIndex!=null && currentParam.SelectItemsIndex.Count > 0)
  92. {
  93. comboboxWidget.SelectItem(currentParam.SelectItemsIndex[0]);
  94. }
  95. comboboxWidget.SetRect(currentParam.ClientRect);
  96. comboboxWidget.SetFlags(currentParam.Flags);
  97. comboboxWidget.SetIsLocked(currentParam.Locked);
  98. comboboxWidget.SetIsReadOnly(currentParam.IsReadOnly);
  99. comboboxWidget.SetIsHidden(currentParam.IsHidden);
  100. comboboxWidget.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  101. comboboxWidget.UpdateFormAp();
  102. comboboxWidget.ReleaseAnnot();
  103. if (CurrentParam != null)
  104. {
  105. currentParam.AnnotIndex = annotIndex;
  106. }
  107. if (PreviousParam != null)
  108. {
  109. PreviousParam.AnnotIndex = annotIndex;
  110. }
  111. return true;
  112. }
  113. return false;
  114. }
  115. internal override bool Update(bool isUndo)
  116. {
  117. if (CurrentParam as ComboBoxParam == null || PreviousParam as ComboBoxParam == null)
  118. {
  119. return false;
  120. }
  121. if (MakeAnnotValid(CurrentParam))
  122. {
  123. CPDFComboBoxWidget comboboxWidget = Annot as CPDFComboBoxWidget;
  124. if (comboboxWidget == null || !comboboxWidget.IsValid())
  125. {
  126. return false;
  127. }
  128. ComboBoxParam updateParam = (isUndo ? PreviousParam : CurrentParam) as ComboBoxParam;
  129. ComboBoxParam checkParam = (isUndo ? CurrentParam : PreviousParam) as ComboBoxParam;
  130. if (updateParam.FieldName != checkParam.FieldName)
  131. {
  132. comboboxWidget.SetFieldName(updateParam.FieldName);
  133. }
  134. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  135. {
  136. if (updateParam.HasLineColor)
  137. {
  138. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  139. {
  140. comboboxWidget.SetWidgetBorderRGBColor(updateParam.LineColor);
  141. }
  142. }
  143. else
  144. {
  145. comboboxWidget.ClearWidgetBorderRGBColor();
  146. }
  147. }
  148. if (!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  149. {
  150. if (updateParam.HasBgColor)
  151. {
  152. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  153. {
  154. comboboxWidget.SetWidgetBgRGBColor(updateParam.BgColor);
  155. }
  156. }
  157. else
  158. {
  159. comboboxWidget.ClearWidgetBgRGBColor();
  160. }
  161. }
  162. if (updateParam.LineWidth!=checkParam.LineWidth)
  163. {
  164. comboboxWidget.SetBorderWidth((float)updateParam.LineWidth);
  165. }
  166. if(updateParam.BorderStyle!=checkParam.BorderStyle)
  167. {
  168. comboboxWidget.SetWidgetBorderStyle(updateParam.BorderStyle);
  169. }
  170. if (updateParam.FontName != checkParam.FontName)
  171. {
  172. CTextAttribute textAttr = comboboxWidget.GetTextAttribute();
  173. bool isBold = IsBold(textAttr.FontName);
  174. bool isItalic = IsItalic(textAttr.FontName);
  175. FontType fontType = GetFontType(updateParam.FontName);
  176. textAttr.FontName = ObtainFontName(fontType, isBold, isItalic);
  177. comboboxWidget.SetTextAttribute(textAttr);
  178. }
  179. if (updateParam.FontSize != checkParam.FontSize)
  180. {
  181. CTextAttribute textAttr = comboboxWidget.GetTextAttribute();
  182. textAttr.FontSize = (float)updateParam.FontSize;
  183. comboboxWidget.SetTextAttribute(textAttr);
  184. }
  185. if (!CheckArrayEqual(updateParam.FontColor,checkParam.FontColor))
  186. {
  187. if (updateParam.FontColor != null && updateParam.FontColor.Length == 3)
  188. {
  189. CTextAttribute textAttr = comboboxWidget.GetTextAttribute();
  190. textAttr.FontColor = updateParam.FontColor;
  191. comboboxWidget.SetTextAttribute(textAttr);
  192. }
  193. }
  194. if (updateParam.IsBold != checkParam.IsBold)
  195. {
  196. CTextAttribute textAttr = comboboxWidget.GetTextAttribute();
  197. bool isItalic = IsItalic(textAttr.FontName);
  198. FontType fontType = GetFontType(textAttr.FontName);
  199. textAttr.FontName = ObtainFontName(fontType, updateParam.IsBold, isItalic);
  200. comboboxWidget.SetTextAttribute(textAttr);
  201. }
  202. if (updateParam.IsItalic != checkParam.IsItalic)
  203. {
  204. CTextAttribute textAttr = comboboxWidget.GetTextAttribute();
  205. bool isBold = IsBold(textAttr.FontName);
  206. FontType fontType = GetFontType(textAttr.FontName);
  207. textAttr.FontName = ObtainFontName(fontType, isBold, updateParam.IsItalic);
  208. comboboxWidget.SetTextAttribute(textAttr);
  209. }
  210. if (updateParam.OptionItems != null)
  211. {
  212. int optionsCount = comboboxWidget.GetItemsCount();
  213. for (int i = 0; i < optionsCount; i++)
  214. {
  215. comboboxWidget.RemoveOptionItem(0);
  216. }
  217. int addIndex = 0;
  218. foreach (string key in updateParam.OptionItems.Keys)
  219. {
  220. comboboxWidget.AddOptionItem(addIndex, updateParam.OptionItems[key], key);
  221. addIndex++;
  222. }
  223. }
  224. if (updateParam.SelectItemsIndex != null)
  225. {
  226. if (updateParam.SelectItemsIndex.Count > 0)
  227. {
  228. comboboxWidget.SelectItem(updateParam.SelectItemsIndex[0]);
  229. }
  230. }
  231. if (updateParam.SelectItemsIndex != null && updateParam.SelectItemsIndex.Count > 0)
  232. {
  233. comboboxWidget.SelectItem(updateParam.SelectItemsIndex[0]);
  234. }
  235. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  236. {
  237. comboboxWidget.SetRect(updateParam.ClientRect);
  238. }
  239. if (updateParam.Flags != checkParam.Flags)
  240. {
  241. comboboxWidget.SetFlags(updateParam.Flags);
  242. }
  243. if (updateParam.Locked != checkParam.Locked)
  244. {
  245. comboboxWidget.SetIsLocked(updateParam.Locked);
  246. }
  247. if (updateParam.IsReadOnly != checkParam.IsReadOnly)
  248. {
  249. comboboxWidget.SetIsReadOnly(updateParam.IsReadOnly);
  250. }
  251. if (updateParam.IsHidden != checkParam.IsHidden)
  252. {
  253. comboboxWidget.SetIsHidden(updateParam.IsHidden);
  254. }
  255. comboboxWidget.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  256. comboboxWidget.UpdateFormAp();
  257. return true;
  258. }
  259. return false;
  260. }
  261. internal override bool Remove()
  262. {
  263. if (MakeAnnotValid(CurrentParam))
  264. {
  265. Annot.RemoveAnnot();
  266. return true;
  267. }
  268. return false;
  269. }
  270. }
  271. }