ComboBoxHistory.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 = currentParam.FontName;
  78. comboboxWidget.SetTextAttribute(textAttr);
  79. if(currentParam.OptionItems!=null && currentParam.OptionItems.Count > 0)
  80. {
  81. int addIndex = 0;
  82. foreach (string key in currentParam.OptionItems.Keys)
  83. {
  84. comboboxWidget.AddOptionItem(addIndex, currentParam.OptionItems[key], key);
  85. addIndex++;
  86. }
  87. }
  88. if(currentParam.SelectItemsIndex!=null && currentParam.SelectItemsIndex.Count > 0)
  89. {
  90. comboboxWidget.SelectItem(currentParam.SelectItemsIndex[0]);
  91. }
  92. comboboxWidget.SetRect(currentParam.ClientRect);
  93. comboboxWidget.SetFlags(currentParam.Flags);
  94. comboboxWidget.SetIsLocked(currentParam.Locked);
  95. comboboxWidget.SetIsReadOnly(currentParam.IsReadOnly);
  96. comboboxWidget.SetIsHidden(currentParam.IsHidden);
  97. comboboxWidget.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  98. comboboxWidget.UpdateFormAp();
  99. comboboxWidget.ReleaseAnnot();
  100. if (CurrentParam != null)
  101. {
  102. currentParam.AnnotIndex = annotIndex;
  103. }
  104. if (PreviousParam != null)
  105. {
  106. PreviousParam.AnnotIndex = annotIndex;
  107. }
  108. return true;
  109. }
  110. return false;
  111. }
  112. internal override bool Update(bool isUndo)
  113. {
  114. if (CurrentParam as ComboBoxParam == null || PreviousParam as ComboBoxParam == null)
  115. {
  116. return false;
  117. }
  118. if (MakeAnnotValid(CurrentParam))
  119. {
  120. CPDFComboBoxWidget comboboxWidget = Annot as CPDFComboBoxWidget;
  121. if (comboboxWidget == null || !comboboxWidget.IsValid())
  122. {
  123. return false;
  124. }
  125. ComboBoxParam updateParam = (isUndo ? PreviousParam : CurrentParam) as ComboBoxParam;
  126. ComboBoxParam checkParam = (isUndo ? CurrentParam : PreviousParam) as ComboBoxParam;
  127. if (updateParam.FieldName != checkParam.FieldName)
  128. {
  129. comboboxWidget.SetFieldName(updateParam.FieldName);
  130. }
  131. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  132. {
  133. if (updateParam.HasLineColor)
  134. {
  135. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  136. {
  137. comboboxWidget.SetWidgetBorderRGBColor(updateParam.LineColor);
  138. }
  139. }
  140. else
  141. {
  142. comboboxWidget.ClearWidgetBorderRGBColor();
  143. }
  144. }
  145. if (!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  146. {
  147. if (updateParam.HasBgColor)
  148. {
  149. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  150. {
  151. comboboxWidget.SetWidgetBgRGBColor(updateParam.BgColor);
  152. }
  153. }
  154. else
  155. {
  156. comboboxWidget.ClearWidgetBgRGBColor();
  157. }
  158. }
  159. if (updateParam.LineWidth!=checkParam.LineWidth)
  160. {
  161. comboboxWidget.SetBorderWidth((float)updateParam.LineWidth);
  162. }
  163. if(updateParam.BorderStyle!=checkParam.BorderStyle)
  164. {
  165. comboboxWidget.SetWidgetBorderStyle(updateParam.BorderStyle);
  166. }
  167. if (updateParam.FontName != checkParam.FontName)
  168. {
  169. CTextAttribute textAttr = comboboxWidget.GetTextAttribute();
  170. textAttr.FontName = updateParam.FontName;
  171. comboboxWidget.SetTextAttribute(textAttr);
  172. }
  173. if (updateParam.FontSize != checkParam.FontSize)
  174. {
  175. CTextAttribute textAttr = comboboxWidget.GetTextAttribute();
  176. textAttr.FontSize = (float)updateParam.FontSize;
  177. comboboxWidget.SetTextAttribute(textAttr);
  178. }
  179. if (!CheckArrayEqual(updateParam.FontColor,checkParam.FontColor))
  180. {
  181. if (updateParam.FontColor != null && updateParam.FontColor.Length == 3)
  182. {
  183. CTextAttribute textAttr = comboboxWidget.GetTextAttribute();
  184. textAttr.FontColor = updateParam.FontColor;
  185. comboboxWidget.SetTextAttribute(textAttr);
  186. }
  187. }
  188. if (updateParam.OptionItems != null)
  189. {
  190. int optionsCount = comboboxWidget.GetItemsCount();
  191. for (int i = 0; i < optionsCount; i++)
  192. {
  193. comboboxWidget.RemoveOptionItem(0);
  194. }
  195. int addIndex = 0;
  196. foreach (string key in updateParam.OptionItems.Keys)
  197. {
  198. comboboxWidget.AddOptionItem(addIndex, updateParam.OptionItems[key], key);
  199. addIndex++;
  200. }
  201. }
  202. if (updateParam.SelectItemsIndex != null)
  203. {
  204. if (updateParam.SelectItemsIndex.Count > 0)
  205. {
  206. comboboxWidget.SelectItem(updateParam.SelectItemsIndex[0]);
  207. }
  208. }
  209. if (updateParam.SelectItemsIndex != null && updateParam.SelectItemsIndex.Count > 0)
  210. {
  211. comboboxWidget.SelectItem(updateParam.SelectItemsIndex[0]);
  212. }
  213. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  214. {
  215. comboboxWidget.SetRect(updateParam.ClientRect);
  216. }
  217. if (updateParam.Flags != checkParam.Flags)
  218. {
  219. comboboxWidget.SetFlags(updateParam.Flags);
  220. }
  221. if (updateParam.Locked != checkParam.Locked)
  222. {
  223. comboboxWidget.SetIsLocked(updateParam.Locked);
  224. }
  225. if (updateParam.IsReadOnly != checkParam.IsReadOnly)
  226. {
  227. comboboxWidget.SetIsReadOnly(updateParam.IsReadOnly);
  228. }
  229. if (updateParam.IsHidden != checkParam.IsHidden)
  230. {
  231. comboboxWidget.SetIsHidden(updateParam.IsHidden);
  232. }
  233. comboboxWidget.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  234. comboboxWidget.UpdateFormAp();
  235. return true;
  236. }
  237. return false;
  238. }
  239. internal override bool Remove()
  240. {
  241. if (MakeAnnotValid(CurrentParam))
  242. {
  243. Annot.RemoveAnnot();
  244. return true;
  245. }
  246. return false;
  247. }
  248. }
  249. }