/poppler/ext/poppler/rbpoppler-form-field.c

https://github.com/geoffyoungs/ruby-gnome2 · C · 291 lines · 230 code · 48 blank · 13 comment · 2 complexity · 78ee2091aea6da6892bfe12ba035dd5a MD5 · raw file

  1. /* -*- c-file-style: "ruby" -*- */
  2. /**********************************************************************
  3. rbpoppler-form-field.c -
  4. Copyright (C) 2008 Ruby-GNOME2 Project Team
  5. **********************************************************************/
  6. #include "rbpoppler-private.h"
  7. #define RVAL2FF(obj) RVAL2POPPLER_FORM_FIELD(obj)
  8. #define RVAL2TF(obj) RVAL2FF(obj)
  9. #define RVAL2BF(obj) RVAL2FF(obj)
  10. #define RVAL2CF(obj) RVAL2FF(obj)
  11. #define FFT2RVAL(obj) (GENUM2RVAL(obj, POPPLER_TYPE_FORM_FIELD_TYPE))
  12. #define RVAL2FFT(obj) (RVAL2GENUM(obj, POPPLER_TYPE_FORM_FIELD_TYPE))
  13. #define FBT2RVAL(obj) (GENUM2RVAL(obj, POPPLER_TYPE_FORM_BUTTON_TYPE))
  14. #define FTT2RVAL(obj) (GENUM2RVAL(obj, POPPLER_TYPE_FORM_TEXT_TYPE))
  15. #define FCT2RVAL(obj) (GENUM2RVAL(obj, POPPLER_TYPE_FORM_CHOICE_TYPE))
  16. static VALUE cUnknownField, cTextField, cButtonField;
  17. static VALUE cChoiceField, cSignatureField;
  18. VALUE
  19. rb_poppler_ruby_object_from_form_field(PopplerFormField *field)
  20. {
  21. VALUE obj;
  22. obj = rbgobj_ruby_object_from_instance2(field, FALSE);
  23. if (NIL_P(obj)) {
  24. switch (poppler_form_field_get_field_type(field)) {
  25. case POPPLER_FORM_FIELD_UNKNOWN:
  26. obj = rbgobj_create_object(cUnknownField);
  27. break;
  28. case POPPLER_FORM_FIELD_BUTTON:
  29. obj = rbgobj_create_object(cButtonField);
  30. break;
  31. case POPPLER_FORM_FIELD_TEXT:
  32. obj = rbgobj_create_object(cTextField);
  33. break;
  34. case POPPLER_FORM_FIELD_CHOICE:
  35. obj = rbgobj_create_object(cChoiceField);
  36. break;
  37. case POPPLER_FORM_FIELD_SIGNATURE:
  38. obj = rbgobj_create_object(cSignatureField);
  39. break;
  40. }
  41. g_object_ref(field);
  42. G_INITIALIZE(obj, (gpointer)field);
  43. }
  44. return obj;
  45. }
  46. /* FormField */
  47. static VALUE
  48. form_field_get_id(VALUE self)
  49. {
  50. return INT2NUM(poppler_form_field_get_id(RVAL2FF(self)));
  51. }
  52. static VALUE
  53. form_field_get_font_size(VALUE self)
  54. {
  55. return rb_float_new(poppler_form_field_get_font_size(RVAL2FF(self)));
  56. }
  57. static VALUE
  58. form_field_is_read_only(VALUE self)
  59. {
  60. return CBOOL2RVAL(poppler_form_field_is_read_only(RVAL2FF(self)));
  61. }
  62. /* Button Field */
  63. static VALUE
  64. button_field_get_button_type(VALUE self)
  65. {
  66. return FBT2RVAL(poppler_form_field_button_get_button_type(RVAL2FF(self)));
  67. }
  68. static VALUE
  69. button_field_get_state(VALUE self)
  70. {
  71. return CBOOL2RVAL(poppler_form_field_button_get_state(RVAL2BF(self)));
  72. }
  73. static VALUE
  74. button_field_set_state(VALUE self, VALUE state)
  75. {
  76. poppler_form_field_button_set_state(RVAL2BF(self), RVAL2CBOOL(state));
  77. return Qnil;
  78. }
  79. /* Text Field */
  80. static VALUE
  81. text_field_get_text_type(VALUE self)
  82. {
  83. return FTT2RVAL(poppler_form_field_text_get_text_type(RVAL2TF(self)));
  84. }
  85. static VALUE
  86. text_field_get_text(VALUE self)
  87. {
  88. return CSTR2RVAL(poppler_form_field_text_get_text(RVAL2TF(self)));
  89. }
  90. static VALUE
  91. text_field_set_text(VALUE self, VALUE text)
  92. {
  93. poppler_form_field_text_set_text(RVAL2TF(self), RVAL2CSTR_ACCEPT_NIL(text));
  94. return Qnil;
  95. }
  96. static VALUE
  97. text_field_get_max_length(VALUE self)
  98. {
  99. return INT2NUM(poppler_form_field_text_get_max_len(RVAL2TF(self)));
  100. }
  101. static VALUE
  102. text_field_do_spell_check(VALUE self)
  103. {
  104. return CBOOL2RVAL(poppler_form_field_text_do_spell_check(RVAL2TF(self)));
  105. }
  106. static VALUE
  107. text_field_do_scroll(VALUE self)
  108. {
  109. return CBOOL2RVAL(poppler_form_field_text_do_scroll(RVAL2TF(self)));
  110. }
  111. static VALUE
  112. text_field_is_rich_text(VALUE self)
  113. {
  114. return CBOOL2RVAL(poppler_form_field_text_is_rich_text(RVAL2TF(self)));
  115. }
  116. static VALUE
  117. text_field_is_password(VALUE self)
  118. {
  119. return CBOOL2RVAL(poppler_form_field_text_is_password(RVAL2TF(self)));
  120. }
  121. /* Choice Field */
  122. static VALUE
  123. choice_field_get_choice_type(VALUE self)
  124. {
  125. return FCT2RVAL(poppler_form_field_choice_get_choice_type(RVAL2CF(self)));
  126. }
  127. static VALUE
  128. choice_field_is_editable(VALUE self)
  129. {
  130. return CBOOL2RVAL(poppler_form_field_choice_is_editable(RVAL2CF(self)));
  131. }
  132. static VALUE
  133. choice_field_can_select_multiple(VALUE self)
  134. {
  135. return CBOOL2RVAL(poppler_form_field_choice_can_select_multiple(RVAL2CF(self)));
  136. }
  137. static VALUE
  138. choice_field_do_spell_check(VALUE self)
  139. {
  140. return CBOOL2RVAL(poppler_form_field_choice_do_spell_check(RVAL2CF(self)));
  141. }
  142. static VALUE
  143. choice_field_commit_on_change(VALUE self)
  144. {
  145. return CBOOL2RVAL(poppler_form_field_choice_commit_on_change(RVAL2CF(self)));
  146. }
  147. static VALUE
  148. choice_field_get_n_items(VALUE self)
  149. {
  150. return INT2NUM(poppler_form_field_choice_get_n_items(RVAL2CF(self)));
  151. }
  152. static VALUE
  153. choice_field_get_item(VALUE self, VALUE index)
  154. {
  155. return CSTR2RVAL(poppler_form_field_choice_get_item(RVAL2CF(self),
  156. NUM2INT(index)));
  157. }
  158. static VALUE
  159. choice_field_is_item_selected(VALUE self, VALUE index)
  160. {
  161. return CBOOL2RVAL(poppler_form_field_choice_is_item_selected(RVAL2CF(self),
  162. NUM2INT(index)));
  163. }
  164. static VALUE
  165. choice_field_select_item(VALUE self, VALUE index)
  166. {
  167. poppler_form_field_choice_select_item(RVAL2CF(self), NUM2INT(index));
  168. return Qnil;
  169. }
  170. static VALUE
  171. choice_field_unselect_all(VALUE self)
  172. {
  173. poppler_form_field_choice_unselect_all(RVAL2CF(self));
  174. return Qnil;
  175. }
  176. static VALUE
  177. choice_field_toggle_item(VALUE self, VALUE index)
  178. {
  179. poppler_form_field_choice_toggle_item(RVAL2CF(self), NUM2INT(index));
  180. return Qnil;
  181. }
  182. static VALUE
  183. choice_field_set_text(VALUE self, VALUE text)
  184. {
  185. poppler_form_field_choice_set_text(RVAL2CF(self), RVAL2CSTR_ACCEPT_NIL(text));
  186. return Qnil;
  187. }
  188. static VALUE
  189. choice_field_get_text(VALUE self)
  190. {
  191. return CSTR2RVAL(poppler_form_field_choice_get_text(RVAL2CF(self)));
  192. }
  193. void
  194. Init_poppler_form_field(VALUE mPoppler)
  195. {
  196. VALUE cFormField;
  197. cFormField = G_DEF_CLASS(POPPLER_TYPE_FORM_FIELD, "FormField", mPoppler);
  198. cUnknownField = rb_define_class_under(mPoppler, "UnknownField", cFormField);
  199. cTextField = rb_define_class_under(mPoppler, "TextField", cFormField);
  200. cButtonField = rb_define_class_under(mPoppler, "ButtonField", cFormField);
  201. cChoiceField = rb_define_class_under(mPoppler, "ChoiceField", cFormField);
  202. cSignatureField = rb_define_class_under(mPoppler, "SignatureField",
  203. cFormField);
  204. /* FormField */
  205. rb_define_method(cFormField, "id", form_field_get_id, 0);
  206. rb_define_method(cFormField, "font_size", form_field_get_font_size, 0);
  207. rb_define_method(cFormField, "read_only?", form_field_is_read_only, 0);
  208. G_DEF_SETTERS(cFormField);
  209. rb_define_method(cButtonField, "type", button_field_get_button_type, 0);
  210. rb_define_method(cButtonField, "active?", button_field_get_state, 0);
  211. rb_define_method(cButtonField, "set_active", button_field_set_state, 1);
  212. G_DEF_SETTERS(cButtonField);
  213. rb_define_method(cTextField, "type", text_field_get_text_type, 0);
  214. rb_define_method(cTextField, "text", text_field_get_text, 0);
  215. rb_define_method(cTextField, "set_text", text_field_set_text, 1);
  216. rb_define_method(cTextField, "max_length", text_field_get_max_length, 0);
  217. rb_define_method(cTextField, "spell_check?", text_field_do_spell_check, 0);
  218. rb_define_method(cTextField, "scroll?", text_field_do_scroll, 0);
  219. rb_define_method(cTextField, "rich_text?", text_field_is_rich_text, 0);
  220. rb_define_method(cTextField, "password?", text_field_is_password, 0);
  221. G_DEF_SETTERS(cTextField);
  222. rb_define_method(cChoiceField, "type", choice_field_get_choice_type, 0);
  223. rb_define_method(cChoiceField, "editable?", choice_field_is_editable, 0);
  224. rb_define_method(cChoiceField, "select_multiple?",
  225. choice_field_can_select_multiple, 0);
  226. rb_define_method(cChoiceField, "spell_check?",
  227. choice_field_do_spell_check, 0);
  228. rb_define_method(cChoiceField, "commit_on_change?",
  229. choice_field_commit_on_change, 0);
  230. rb_define_method(cChoiceField, "n_items", choice_field_get_n_items, 0);
  231. rb_define_method(cChoiceField, "[]", choice_field_get_item, 1);
  232. rb_define_method(cChoiceField, "selected?",
  233. choice_field_is_item_selected, 1);
  234. rb_define_method(cChoiceField, "select", choice_field_select_item, 1);
  235. rb_define_method(cChoiceField, "unselect_all", choice_field_unselect_all, 0);
  236. rb_define_method(cChoiceField, "toggle", choice_field_toggle_item, 1);
  237. rb_define_method(cChoiceField, "text", choice_field_get_text, 0);
  238. rb_define_method(cChoiceField, "set_text", choice_field_set_text, 1);
  239. G_DEF_SETTERS(cChoiceField);
  240. }