progressbar.pp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. {
  2. This is the pascal translation of the progressbar sample from the
  3. GTK-1.2 library.
  4. Converted from C to Pascal by Stefan Hille
  5. <[email protected]>
  6. }
  7. (* example-start progressbar progressbar.c *)
  8. program progressbar;
  9. uses glib, gtk, strings;
  10. type
  11. ProgressData = record
  12. window : PGtkWidget;
  13. pbar : PGtkWidget;
  14. timer : integer;
  15. end;
  16. PProgressData = ^ProgressData;
  17. (* Update the value of the progress bar so that we get
  18. * some movement *)
  19. function progress_timeout( data : gpointer ) : gint; cdecl;
  20. var
  21. new_val : gfloat;
  22. adj : PGtkAdjustment;
  23. begin
  24. (* Calculate the value of the progress bar using the
  25. * value range set in the adjustment object *)
  26. new_val := gtk_progress_get_value( GTK_PROGRESS(data) ) + 1;
  27. adj := GTK_PROGRESS (data)^.adjustment;
  28. if (new_val > adj^.upper) then
  29. new_val := adj^.lower;
  30. (* Set the new value *)
  31. gtk_progress_set_value (GTK_PROGRESS (data), new_val);
  32. (* As this is a timeout function, return TRUE so that it
  33. * continues to get called *)
  34. progress_timeout := 1;
  35. end;
  36. (* Callback that toggles the text display within the progress
  37. * bar trough *)
  38. procedure toggle_show_text( widget : PGtkWidget;
  39. pdata : PProgressData);cdecl;
  40. begin
  41. gtk_progress_set_show_text (GTK_PROGRESS (pdata^.pbar),
  42. active(PGtkToggleButton(widget)^));
  43. end;
  44. (* Callback that toggles the activity mode of the progress
  45. * bar *)
  46. procedure toggle_activity_mode( widget : PGtkWidget;
  47. pdata : PProgressData); cdecl;
  48. begin
  49. gtk_progress_set_activity_mode (GTK_PROGRESS (pdata^.pbar),
  50. active(PGtkToggleButton(widget)^));
  51. end;
  52. (* Callback that toggles the continuous mode of the progress
  53. * bar *)
  54. procedure set_continuous_mode( widget : PGtkWidget;
  55. pdata : PProgressData); cdecl;
  56. begin
  57. gtk_progress_bar_set_bar_style (GTK_PROGRESS_BAR (pdata^.pbar),
  58. GTK_PROGRESS_CONTINUOUS);
  59. end;
  60. (* Callback that toggles the discrete mode of the progress
  61. * bar *)
  62. procedure set_discrete_mode( widget : PGtkWidget;
  63. pdata : PProgressData); cdecl;
  64. begin
  65. gtk_progress_bar_set_bar_style (GTK_PROGRESS_BAR (pdata^.pbar),
  66. GTK_PROGRESS_DISCRETE);
  67. end;
  68. (* Clean up allocated memory and remove the timer *)
  69. procedure destroy_progress( widget : PGtkWidget;
  70. pdata : PProgressData); cdecl;
  71. begin
  72. gtk_timeout_remove (pdata^.timer);
  73. pdata^.timer := 0;
  74. pdata^.window := NULL;
  75. g_free(pdata);
  76. gtk_main_quit();
  77. end;
  78. var
  79. pdata : PProgressData;
  80. align : PGtkWidget;
  81. separator : PGtkWidget;
  82. table : PGtkWidget;
  83. adj : PGtkAdjustment;
  84. button: PGtkWidget;
  85. check : PGtkWidget;
  86. vbox : PGtkWidget;
  87. begin
  88. gtk_init (@argc, @argv);
  89. (* Allocate memory for the data that is passwd to the callbacks *)
  90. pdata := g_malloc( sizeof(ProgressData) );
  91. pdata^.window := gtk_window_new (GTK_WINDOW_TOPLEVEL);
  92. gtk_window_set_policy (GTK_WINDOW (pdata^.window), 0{FALSE}, 1{TRUE}, 1{TRUE});
  93. gtk_signal_connect (GTK_OBJECT (pdata^.window), 'destroy',
  94. GTK_SIGNAL_FUNC (@destroy_progress),
  95. pdata);
  96. gtk_window_set_title (GTK_WINDOW (pdata^.window),'GtkProgressBar' );
  97. gtk_container_set_border_width (GTK_CONTAINER (pdata^.window), 0);
  98. vbox := gtk_vbox_new (FALSE, 5);
  99. gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
  100. gtk_container_add (GTK_CONTAINER (pdata^.window), vbox);
  101. (* Create a centering alignment object *)
  102. align := gtk_alignment_new (0.5, 0.5, 0, 0);
  103. gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 5);
  104. (* Create a GtkAdjusment object to hold the range of the
  105. * progress bar *)
  106. adj := PGtkAdjustment( gtk_adjustment_new (0, 1, 150, 0, 0, 0));
  107. (* Create the GtkProgressBar using the adjustment *)
  108. pdata^.pbar := gtk_progress_bar_new_with_adjustment (adj);
  109. (* Set the format of the string that can be displayed in the
  110. * trough of the progress bar:
  111. * %p - percentage
  112. * %v - value
  113. * %l - lower range value
  114. * %u - upper range value *)
  115. gtk_progress_set_format_string (GTK_PROGRESS (pdata^.pbar),
  116. '%v from [%l-%u] (=%p%%)');
  117. gtk_container_add (GTK_CONTAINER (align), pdata^.pbar);
  118. (* Add a timer callback to update the value of the progress bar *)
  119. pdata^.timer := gtk_timeout_add (100, TGtkFunction(@progress_timeout), pdata^.pbar);
  120. separator := gtk_hseparator_new ();
  121. gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
  122. (* rows, columns, homogeneous *)
  123. table := gtk_table_new (2, 3, FALSE);
  124. gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, TRUE, 0);
  125. (* Add a check button to select displaying of the trough text *)
  126. check := gtk_check_button_new_with_label ('Show text');
  127. gtk_table_attach (GTK_TABLE (table), check, 0, 1, 0, 1,
  128. GTK_EXPAND or GTK_FILL, GTK_EXPAND or GTK_FILL,
  129. 5, 5);
  130. gtk_signal_connect (GTK_OBJECT (check), 'clicked',
  131. GTK_SIGNAL_FUNC (@toggle_show_text),
  132. pdata);
  133. (* Add a check button to toggle activity mode *)
  134. check := gtk_check_button_new_with_label ( 'Activity mode');
  135. gtk_table_attach (GTK_TABLE (table), check, 0, 1, 1, 2,
  136. GTK_EXPAND or GTK_FILL, GTK_EXPAND or GTK_FILL,
  137. 5, 5);
  138. gtk_signal_connect (GTK_OBJECT (check), 'clicked',
  139. GTK_SIGNAL_FUNC (@toggle_activity_mode),
  140. pdata);
  141. separator := gtk_vseparator_new ();
  142. gtk_table_attach (GTK_TABLE (table), separator, 1, 2, 0, 2,
  143. GTK_EXPAND or GTK_FILL, GTK_EXPAND or GTK_FILL,
  144. 5, 5);
  145. (* Add a radio button to select continuous display mode *)
  146. button := gtk_radio_button_new_with_label (NULL, 'Continuous');
  147. gtk_table_attach (GTK_TABLE (table), button, 2, 3, 0, 1,
  148. GTK_EXPAND or GTK_FILL, GTK_EXPAND or GTK_FILL,
  149. 5, 5);
  150. gtk_signal_connect (GTK_OBJECT (button), 'clicked',
  151. GTK_SIGNAL_FUNC (@set_continuous_mode),
  152. pdata);
  153. (* Add a radio button to select discrete display mode *)
  154. button := gtk_radio_button_new_with_label(
  155. gtk_radio_button_group (GTK_RADIO_BUTTON (button)),
  156. 'Discrete');
  157. gtk_table_attach (GTK_TABLE (table), button, 2, 3, 1, 2,
  158. GTK_EXPAND or GTK_FILL, GTK_EXPAND or GTK_FILL,
  159. 5, 5);
  160. gtk_signal_connect (GTK_OBJECT (button), 'clicked',
  161. GTK_SIGNAL_FUNC (@set_discrete_mode),
  162. pdata);
  163. separator := gtk_hseparator_new ();
  164. gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
  165. (* Add a button to exit the program *)
  166. button := gtk_button_new_with_label ('close');
  167. gtk_signal_connect_object (GTK_OBJECT (button), 'clicked',
  168. Gtk_Signal_Func (@gtk_widget_destroy),
  169. GTK_OBJECT (pdata^.window));
  170. gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
  171. (* This makes it so the button is the default. *)
  172. GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  173. (* This grabs this button to be the default button. Simply hitting
  174. * the 'Enter' key will cause this button to activate. *)
  175. gtk_widget_grab_default (button);
  176. gtk_widget_show_all (pdata^.window);
  177. gtk_main ();
  178. end.