/examples/GUI/GTK+/Tutorials/rangewidgets.bas

https://github.com/freebasic/fbc · Basic · 215 lines · 135 code · 50 blank · 30 comment · 0 complexity · 3c518168c791af4defd04afeeb5a71f9 MD5 · raw file

  1. ' Rangewidgets.bas
  2. ' Translated from C to FB by TinyCla, 2006
  3. '
  4. ' Reviewed by TJF (2011)
  5. ' Details: http://developer.gnome.org/gtk/
  6. '#DEFINE __FB_GTK3__
  7. #include once "gtk/gtk.bi"
  8. Dim Shared As GtkScale Ptr hscale
  9. Dim Shared As GtkScale Ptr vscale
  10. Dim Shared As GtkWidget Ptr digits
  11. Sub cb_pos_menu_select Cdecl( Byval item As GtkWidget Ptr, Byval dat As gpointer)
  12. Var pos_ = GTK_POS_TOP, active = TRUE
  13. SELECT CASE AS CONST gtk_combo_box_get_active(GTK_COMBO_BOX(item))
  14. CASE 1
  15. CASE 2 : pos_ = GTK_POS_BOTTOM
  16. CASE 3 : pos_ = GTK_POS_RIGHT
  17. CASE 4 : pos_ = GTK_POS_LEFT
  18. CASE ELSE : active = FALSE
  19. END SELECT
  20. ' Set the value position on both scale widgets
  21. gtk_scale_set_value_pos (hscale, pos_)
  22. gtk_scale_set_value_pos (vscale, pos_)
  23. gtk_scale_set_draw_value (hscale, active)
  24. gtk_scale_set_draw_value (vscale, active)
  25. gtk_widget_set_sensitive(digits, active)
  26. End Sub
  27. Sub cb_digits_scale Cdecl( Byval adj As GtkAdjustment Ptr )
  28. ' Set the number of decimal places to which adj->value is rounded
  29. Var value = gtk_adjustment_get_value(adj)
  30. gtk_scale_set_digits (hscale, value)
  31. gtk_scale_set_digits (vscale, value)
  32. End Sub
  33. Sub cb_page_size Cdecl( Byval get_adj As GtkAdjustment Ptr, Byval set_adj As GtkAdjustment Ptr )
  34. ' Set the page size and page increment size of the sample
  35. ' adjustment to the value specified by the "Page Size" scale
  36. Var value = gtk_adjustment_get_value(get_adj) + 1
  37. gtk_adjustment_set_page_size(set_adj, value)
  38. gtk_adjustment_set_step_increment(set_adj, value)
  39. ' This sets the adjustment and makes it emit the "changed" signal to
  40. ' reconfigure all the widgets that are attached to this signal.
  41. Var lower = gtk_adjustment_get_lower(set_adj)
  42. Var upper = gtk_adjustment_get_upper(set_adj)
  43. gtk_adjustment_clamp_page (set_adj, lower, upper)
  44. End Sub
  45. ' makes the sample window
  46. Sub create_range_controls( )
  47. ' Standard window-creating stuff
  48. Var win = gtk_window_new (GTK_WINDOW_TOPLEVEL)
  49. g_signal_connect (G_OBJECT (win), "destroy", _
  50. G_CALLBACK (@gtk_main_quit), NULL)
  51. gtk_window_set_title (GTK_WINDOW (win), "range controls")
  52. Var box1 = gtk_vbox_new (FALSE, 0)
  53. gtk_container_add (GTK_CONTAINER (win), box1)
  54. gtk_widget_show (box1)
  55. Var box2 = gtk_hbox_new (FALSE, 10)
  56. gtk_container_set_border_width (GTK_CONTAINER (box2), 10)
  57. gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0)
  58. gtk_widget_show (box2)
  59. ' value, lower, upper, step_increment, page_increment, page_size
  60. ' Note that the page_size value only makes a difference for
  61. ' scrollbar widgets, and the highest value you'll get is actually
  62. ' (upper - page_size).
  63. Var adj1 = gtk_adjustment_new (0.0, 0.0, 101.0, 0.1, 1.0, 1.0)
  64. Var item = gtk_vscale_new (GTK_ADJUSTMENT (adj1))
  65. gtk_scale_set_digits (GTK_SCALE (item), 1)
  66. gtk_box_pack_start (GTK_BOX (box2), item, TRUE, TRUE, 0)
  67. gtk_widget_show (item)
  68. vscale = GTK_SCALE (item)
  69. Var box3 = gtk_vbox_new (FALSE, 10)
  70. gtk_box_pack_start (GTK_BOX (box2), box3, TRUE, TRUE, 0)
  71. gtk_widget_show (box3)
  72. ' Reuse the same adjustment
  73. item = gtk_hscale_new (GTK_ADJUSTMENT (adj1))
  74. gtk_widget_set_size_request (GTK_WIDGET (item), 200, -1)
  75. gtk_scale_set_digits (GTK_SCALE (item), 1)
  76. gtk_box_pack_start (GTK_BOX (box3), item, TRUE, TRUE, 0)
  77. gtk_widget_show (item)
  78. hscale = GTK_SCALE (item)
  79. ' Reuse the same adjustment again
  80. Var scrollbar = gtk_hscrollbar_new (GTK_ADJUSTMENT (adj1))
  81. ' Notice how this causes the scales to always be updated
  82. ' continuously when the scrollbar is moved
  83. gtk_box_pack_start (GTK_BOX (box3), scrollbar, TRUE, TRUE, 0)
  84. gtk_widget_show (scrollbar)
  85. box2 = gtk_hbox_new (FALSE, 10)
  86. gtk_container_set_border_width (GTK_CONTAINER (box2), 10)
  87. ' An option menu to change the position of the value
  88. Var label = gtk_label_new ("Scale Value Position:")
  89. gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0)
  90. gtk_widget_show (label)
  91. DIM As GtkTreeIter iter
  92. Var store = gtk_list_store_new(1, G_TYPE_STRING)
  93. gtk_list_store_insert_with_values(store, @iter, 0, 0, "None", -1)
  94. gtk_list_store_insert_with_values(store, @iter, 1, 0, "Top", -1)
  95. gtk_list_store_insert_with_values(store, @iter, 2, 0, "Bottom", -1)
  96. gtk_list_store_insert_with_values(store, @iter, 3, 0, "Left", -1)
  97. gtk_list_store_insert_with_values(store, @iter, 4, 0, "Right", -1)
  98. Var combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL(store))
  99. Var renderer = gtk_cell_renderer_text_new ()
  100. gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE)
  101. gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, _
  102. "text", 0, _
  103. NULL)
  104. gtk_combo_box_set_active (GTK_COMBO_BOX(combo), 1)
  105. g_signal_connect (G_OBJECT (combo), "changed", _
  106. G_CALLBACK(@cb_pos_menu_select), NULL)
  107. gtk_box_pack_start (GTK_BOX (box2), combo, TRUE, TRUE, 0)
  108. gtk_widget_show (combo)
  109. gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0)
  110. gtk_widget_show (box2)
  111. box2 = gtk_hbox_new (FALSE, 10)
  112. gtk_container_set_border_width (GTK_CONTAINER (box2), 10)
  113. ' An HScale widget for adjusting the number of digits on the
  114. ' sample scales.
  115. label = gtk_label_new ("Scale Digits:")
  116. gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0)
  117. gtk_widget_show (label)
  118. Var adj2 = gtk_adjustment_new (1.0, 0.0, 5.0, 1.0, 1.0, 0.0)
  119. g_signal_connect (G_OBJECT (adj2), "value_changed", _
  120. G_CALLBACK (@cb_digits_scale), NULL)
  121. Var scale = gtk_hscale_new (GTK_ADJUSTMENT (adj2))
  122. gtk_scale_set_digits (GTK_SCALE (scale), 0)
  123. gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0)
  124. gtk_widget_show (scale)
  125. gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0)
  126. gtk_widget_show (box2)
  127. digits = box2
  128. box2 = gtk_hbox_new (FALSE, 10)
  129. gtk_container_set_border_width (GTK_CONTAINER (box2), 10)
  130. ' And, one last HScale widget for adjusting the page size of the
  131. ' scrollbar.
  132. label = gtk_label_new ("Scrollbar Page Size:")
  133. gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0)
  134. gtk_widget_show (label)
  135. adj2 = gtk_adjustment_new (0.0, 0.0, 100.0, 1.0, 1.0, 0.0)
  136. g_signal_connect (G_OBJECT (adj2), "value_changed", _
  137. G_CALLBACK (@cb_page_size), adj1)
  138. scale = gtk_hscale_new (GTK_ADJUSTMENT (adj2))
  139. gtk_scale_set_digits (GTK_SCALE (scale), 0)
  140. gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0)
  141. gtk_widget_show (scale)
  142. gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0)
  143. gtk_widget_show (box2)
  144. Var separator = gtk_hseparator_new ()
  145. gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0)
  146. gtk_widget_show (separator)
  147. box2 = gtk_vbox_new (FALSE, 10)
  148. gtk_container_set_border_width (GTK_CONTAINER (box2), 10)
  149. gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0)
  150. gtk_widget_show (box2)
  151. Var button = gtk_button_new_with_label ("Quit")
  152. g_signal_connect_swapped (G_OBJECT (button), "clicked", _
  153. G_CALLBACK (@gtk_main_quit), NULL)
  154. gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0)
  155. gtk_widget_set_can_default (button, TRUE)
  156. gtk_widget_grab_default (button)
  157. gtk_widget_show (button)
  158. gtk_widget_show (win)
  159. End Sub
  160. ' ==============================================
  161. ' Main
  162. ' ==============================================
  163. gtk_init (NULL, NULL)
  164. create_range_controls ()
  165. gtk_main ()