2
0

tut4_3.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. {
  2. This file extracted from the GTK 1.2 tutorial.
  3. Section 4.3
  4. Converted from C to Pascal by Thomas E. Payne
  5. }
  6. program Tut4_3;
  7. {$mode objfpc}
  8. uses
  9. glib,gdk,gtk,sysutils;
  10. function delete_event (widget : pGtkWidget ; event: pGdkEvent; data: pgpointer ): integer; cdecl;
  11. begin
  12. gtk_main_quit();
  13. delete_event:=0;
  14. end;
  15. { * Make a new hbox filled with button-labels. Arguments for the
  16. * variables we're interested are passed in to this function.
  17. * We do not show the box, but do show everything inside. }
  18. function make_box(homogeneous: boolean;
  19. spacing : LONGINT;
  20. expand : boolean;
  21. fill : boolean;
  22. padding : LONGINT ):pGtkWidget; cdecl;
  23. var
  24. box, button : pGtkWidget; //GtkWidget is the storage type for widgets
  25. padstr : string;
  26. ppadstr : Pchar;
  27. begin
  28. //* Create a new hbox with the appropriate homogeneous
  29. //* and spacing settings */}
  30. box := gtk_hbox_new (homogeneous, spacing);
  31. //* Create a series of buttons with the appropriate settings */
  32. button := gtk_button_new_with_label ('gtk_box_pack');
  33. gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
  34. gtk_widget_show (button);
  35. button := gtk_button_new_with_label ('(box,');
  36. gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
  37. gtk_widget_show (button);
  38. button := gtk_button_new_with_label ('button,');
  39. gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
  40. gtk_widget_show (button);
  41. //* Create a button with the label depending on the value of
  42. //* expand. */}
  43. if (expand = TRUE) then
  44. button := gtk_button_new_with_label ('TRUE,')
  45. else
  46. button := gtk_button_new_with_label ('FALSE,');
  47. gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
  48. gtk_widget_show (button);
  49. //* This is the same as the button creation for "expand"
  50. //* above, but uses the shorthand form. */}
  51. if fill then
  52. button := gtk_button_new_with_label ('TRUE,')
  53. else
  54. button := gtk_button_new_with_label ('FALSE,');
  55. gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
  56. gtk_widget_show (button);
  57. padstr := InttoStr(padding)+');';
  58. ppadstr := stralloc(length(padstr)+1);
  59. strpcopy(ppadstr,padstr);
  60. button := gtk_button_new_with_label (ppadstr);
  61. gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
  62. gtk_widget_show (button);
  63. make_box := box;
  64. end; //make_box
  65. var
  66. window,button,box1,box2,separator,label_,quitbox : pGtkWidget;
  67. which : integer;
  68. begin
  69. //* Our init, don't forget this! :) */
  70. gtk_init (@argc, @argv);
  71. if (argc <> 2) then
  72. begin
  73. writeln (stderr, 'usage: packbox num, where num is 1, 2, or 3.');
  74. //* This just does cleanup in GTK and exits with an exit status of 1. */
  75. gtk_exit (1);
  76. end;
  77. which := StrtoInt(argv[1]); //use params
  78. //* Create our window */
  79. window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
  80. //* You should always remember to connect the destroy signal to the
  81. //* main window. This is very important for proper intuitive
  82. //* behavior */
  83. gtk_signal_connect (pGTKOBJECT (window), 'delete_event',
  84. GTK_SIGNAL_FUNC (@delete_event), NULL);
  85. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  86. //* We create a vertical box (vbox) to pack the horizontal boxes into.
  87. //* This allows us to stack the horizontal boxes filled with buttons one
  88. //* on top of the other in this vbox. */
  89. box1 := gtk_vbox_new (FALSE, 0);
  90. //* which example to show. These correspond to the pictures above. */
  91. case which of
  92. 1: begin
  93. //* create a new label_. */
  94. label_ := gtk_label_new ('gtk_hbox_new (FALSE, 0);');
  95. //* Align the label_ to the left side. We'll discuss this function and
  96. //* others in the section on Widget Attributes. */
  97. gtk_misc_set_alignment (GTK_MISC (label_), 0, 0);
  98. //* Pack the label_ into the vertical box (vbox box1). Remember that
  99. //* widgets added to a vbox will be packed one on top of the other in
  100. //* order. */
  101. gtk_box_pack_start (GTK_BOX (box1), label_, FALSE, FALSE, 0);
  102. //* Show the label_ */
  103. gtk_widget_show (label_);
  104. //* Call our make box function - homogeneous = FALSE, spacing = 0,
  105. //* expand = FALSE, fill = FALSE, padding = 0 */
  106. box2 := make_box (FALSE, 0, FALSE, FALSE, 0);
  107. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  108. gtk_widget_show (box2);
  109. //* Call our make box function - homogeneous = FALSE, spacing = 0,
  110. //* expand = FALSE, fill = FALSE, padding = 0 */
  111. box2 := make_box (FALSE, 0, TRUE, FALSE, 0);
  112. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  113. gtk_widget_show (box2);
  114. //* Args are: homogeneous, spacing, expand, fill, padding */
  115. box2 := make_box (FALSE, 0, TRUE, TRUE, 0);
  116. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  117. gtk_widget_show (box2);
  118. //* Creates a separator, we'll learn more about these later,
  119. //* but they are quite simple. */
  120. separator := gtk_hseparator_new ();
  121. //* Pack the separator into the vbox. Remember each of these
  122. //* widgets are being packed into a vbox, so they'll be stacked
  123. //* vertically. */
  124. gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
  125. gtk_widget_show (separator);
  126. //* Create another new label, and show it. */
  127. label_ := gtk_label_new ('gtk_hbox_new (TRUE, 0);');
  128. gtk_misc_set_alignment (GTK_MISC (label_), 0, 0);
  129. gtk_box_pack_start (GTK_BOX (box1), label_, FALSE, FALSE, 0);
  130. gtk_widget_show (label_);
  131. //* Args are: homogeneous, spacing, expand, fill, padding */
  132. box2 := make_box (TRUE, 0, TRUE, FALSE, 0);
  133. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  134. gtk_widget_show (box2);
  135. //* Args are: homogeneous, spacing, expand, fill, padding */
  136. box2 := make_box (TRUE, 0, TRUE, TRUE, 0);
  137. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  138. gtk_widget_show (box2);
  139. //* Another new separator. */
  140. separator := gtk_hseparator_new ();
  141. //* The last 3 arguments to gtk_box_pack_start are:
  142. //* expand, fill, padding. */
  143. gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
  144. gtk_widget_show (separator);
  145. end;
  146. 2: begin
  147. //* Create a new label, remember box1 is a vbox as created
  148. //* near the beginning of main() */
  149. label_ := gtk_label_new ('gtk_hbox_new (FALSE, 10);');
  150. gtk_misc_set_alignment (GTK_MISC (label_), 0, 0);
  151. gtk_box_pack_start (GTK_BOX (box1), label_, FALSE, FALSE, 0);
  152. gtk_widget_show (label_);
  153. //* Args are: homogeneous, spacing, expand, fill, padding */
  154. box2 := make_box (FALSE, 10, TRUE, FALSE, 0);
  155. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  156. gtk_widget_show (box2);
  157. //* Args are: homogeneous, spacing, expand, fill, padding */
  158. box2 := make_box (FALSE, 10, TRUE, TRUE, 0);
  159. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  160. gtk_widget_show (box2);
  161. separator := gtk_hseparator_new ();
  162. //* The last 3 arguments to gtk_box_pack_start are:
  163. //* expand, fill, padding. */
  164. gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
  165. gtk_widget_show (separator);
  166. label_ := gtk_label_new ('gtk_hbox_new (FALSE, 0);');
  167. gtk_misc_set_alignment (GTK_MISC (label_), 0, 0);
  168. gtk_box_pack_start (GTK_BOX (box1), label_, FALSE, FALSE, 0);
  169. gtk_widget_show (label_);
  170. //* Args are: homogeneous, spacing, expand, fill, padding */
  171. box2 := make_box (FALSE, 0, TRUE, FALSE, 10);
  172. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  173. gtk_widget_show (box2);
  174. //* Args are: homogeneous, spacing, expand, fill, padding */
  175. box2 := make_box (FALSE, 0, TRUE, TRUE, 10);
  176. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  177. gtk_widget_show (box2);
  178. separator := gtk_hseparator_new ();
  179. //* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
  180. gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
  181. gtk_widget_show (separator);
  182. end;
  183. 3: begin
  184. //* This demonstrates the ability to use gtk_box_pack_end() to
  185. //* right justify widgets. First, we create a new box as before. */
  186. box2 := make_box (FALSE, 0, FALSE, FALSE, 0);
  187. //* Create the label_ that will be put at the end. */
  188. label_ := gtk_label_new ('end');
  189. //* Pack it using gtk_box_pack_end(), so it is put on the right
  190. //* side of the hbox created in the make_box() call. */
  191. gtk_box_pack_end (GTK_BOX (box2), label_, FALSE, FALSE, 0);
  192. //* Show the label_. */
  193. gtk_widget_show (label_);
  194. //* Pack box2 into box1 (the vbox remember ? :) */
  195. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
  196. gtk_widget_show (box2);
  197. //* A separator for the bottom. */
  198. separator := gtk_hseparator_new ();
  199. //* This explicitly sets the separator to 400 pixels wide by 5 pixels
  200. //* high. This is so the hbox we created will also be 400 pixels wide,
  201. //* and the "end" label_ will be separated from the other labels in the
  202. //* hbox. Otherwise, all the widgets in the hbox would be packed as
  203. //* close together as possible. */
  204. gtk_widget_set_usize (separator, 400, 5);
  205. //* pack the separator into the vbox (box1) created near the start
  206. //* of main() */
  207. gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
  208. gtk_widget_show (separator);
  209. end;
  210. end; //case
  211. //* Create another new hbox.. remember we can use as many as we need! */
  212. quitbox := gtk_hbox_new (FALSE, 0);
  213. //* Our quit button. */
  214. button := gtk_button_new_with_label ('Quit');
  215. //* Setup the signal to destroy the window. Remember that this will send
  216. //* the "destroy" signal to the window which will be caught by our signal
  217. //* handler as defined above. */
  218. gtk_signal_connect_object (pGTKOBJECT (button), 'clicked',
  219. GTK_SIGNAL_FUNC (@gtk_main_quit),
  220. GTK_OBJECT (window));
  221. //* Pack the button into the quitbox.
  222. //* The last 3 arguments to gtk_box_pack_start are:
  223. //* expand, fill, padding. */
  224. gtk_box_pack_start (GTK_BOX (quitbox), button, TRUE, FALSE, 0);
  225. //* pack the quitbox into the vbox (box1) */
  226. gtk_box_pack_start (GTK_BOX (box1), quitbox, FALSE, FALSE, 0);
  227. //* Pack the vbox (box1) which now contains all our widgets, into the
  228. //* main window. */
  229. gtk_container_add (GTK_CONTAINER (window), box1);
  230. //* And show everything left */
  231. gtk_widget_show (button);
  232. gtk_widget_show (quitbox);
  233. gtk_widget_show (box1);
  234. //* Showing the window last so everything pops up at once. */
  235. gtk_widget_show (window);
  236. //* And of course, our main function. */
  237. gtk_main ();
  238. //* Control returns here when gtk_main_quit() is called, but not when
  239. //* gtk_exit is used. */
  240. //return(0);
  241. end.