tut4_3.pp 12 KB

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