demo-text-multiview.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local Gtk = lgi.Gtk
  4. local Gdk = lgi.Gdk
  5. local Pango = lgi.Pango
  6. local GdkPixbuf = lgi.GdkPixbuf
  7. -- Create shared text buffer.
  8. local buffer = Gtk.TextBuffer {
  9. tag_table = Gtk.TextTagTable {
  10. -- Create a bunch of tags.
  11. Gtk.TextTag {
  12. name = 'heading',
  13. weight = Pango.Weight.BOLD,
  14. size = 15 * Pango.SCALE,
  15. },
  16. Gtk.TextTag {
  17. name = 'italic',
  18. style = Pango.Style.ITALIC,
  19. },
  20. Gtk.TextTag {
  21. name = 'bold',
  22. weight = Pango.Weight.BOLD,
  23. },
  24. Gtk.TextTag {
  25. name = 'big',
  26. -- points times the Pango.SCALE factor
  27. size = 20 * Pango.SCALE,
  28. },
  29. Gtk.TextTag {
  30. name = 'xx-small',
  31. scale = Pango.SCALE_XX_SMALL,
  32. },
  33. Gtk.TextTag {
  34. name = 'x-large',
  35. scale = Pango.SCALE_X_LARGE,
  36. },
  37. Gtk.TextTag {
  38. name = 'monospace',
  39. family = 'monospace',
  40. },
  41. Gtk.TextTag {
  42. name = 'blue_foreground',
  43. foreground = 'blue',
  44. },
  45. Gtk.TextTag {
  46. name = 'red_background',
  47. background = 'red',
  48. },
  49. Gtk.TextTag {
  50. name = 'big_gap_before_line',
  51. pixels_above_lines = 30,
  52. },
  53. Gtk.TextTag {
  54. name = 'big_gap_after_line',
  55. pixels_below_lines = 30,
  56. },
  57. Gtk.TextTag {
  58. name = 'double_spaced_line',
  59. pixels_inside_wrap = 10,
  60. },
  61. Gtk.TextTag {
  62. name = 'not_editable',
  63. editable = false,
  64. },
  65. Gtk.TextTag {
  66. name = 'word_wrap',
  67. wrap_mode = 'WORD',
  68. },
  69. Gtk.TextTag {
  70. name = 'char_wrap',
  71. wrap_mode = 'CHAR',
  72. },
  73. Gtk.TextTag {
  74. name = 'no_wrap',
  75. wrap_mode = 'NONE',
  76. },
  77. Gtk.TextTag {
  78. name = 'center',
  79. justification = 'CENTER',
  80. },
  81. Gtk.TextTag {
  82. name = 'right_justify',
  83. justification = 'RIGHT',
  84. },
  85. Gtk.TextTag {
  86. name = 'wide_margins',
  87. left_margin = 50,
  88. right_margin = 50,
  89. },
  90. Gtk.TextTag {
  91. name = 'strikethrough',
  92. strikethrough = true,
  93. },
  94. Gtk.TextTag {
  95. name = 'underline',
  96. underline = 'SINGLE',
  97. },
  98. Gtk.TextTag {
  99. name = 'double_underline',
  100. underline = 'DOUBLE',
  101. },
  102. Gtk.TextTag {
  103. name = 'superscript',
  104. rise = 10 * Pango.SCALE, -- 10 pixels
  105. size = 8 * Pango.SCALE, -- 8 points
  106. },
  107. Gtk.TextTag {
  108. name = 'subscript',
  109. rise = -10 * Pango.SCALE, -- 10 pixels
  110. size = 8 * Pango.SCALE, -- 8 points
  111. },
  112. Gtk.TextTag {
  113. name = 'rtl_quote',
  114. wrap_mode = 'WORD',
  115. direction = 'RTL',
  116. indent = 30,
  117. left_margin = 20,
  118. right_margin = 20,
  119. },
  120. },
  121. }
  122. local pixbuf = GdkPixbuf.Pixbuf.new_from_file(
  123. dir:get_child('gtk-logo-rgb.gif'):get_path())
  124. pixbuf = pixbuf:scale_simple(32, 32, 'BILINEAR')
  125. local anchors = {}
  126. local iter = buffer:get_iter_at_offset(0)
  127. for _, item in ipairs {
  128. { [[
  129. The text widget can display text with all kinds of nifty attributes. It also supports multiple views of the same buffer; this demo is showing the same buffer in two places.
  130. ]] },
  131. { [[Font styles. ]], 'heading' },
  132. { [[For example, you can have ]] },
  133. { [[italic]], 'italic' },
  134. { [[, ]] },
  135. { [[bold]], 'bold' },
  136. { [[, or ]] },
  137. { [[monospace (typewriter)]], 'monospace' },
  138. { [[, or ]] },
  139. { [[big]], 'big' },
  140. { [[ text. ]] },
  141. { [[
  142. It's best not to hardcode specific text sizes; you can use relative sizes as with CSS, such as ]] },
  143. { [[xx-small]], 'xx-small' },
  144. { [[ or ]] },
  145. { [[x-large]], 'x-large' },
  146. { [[ to ensure that your program properly adapts if the user changes the default font size.
  147. ]] },
  148. { [[Colors. ]], 'heading' },
  149. { [[Colors such as ]] },
  150. { [[a blue foreground]], 'blue_foreground' },
  151. { [[ or ]] },
  152. { [[a red background]], 'red_background' },
  153. { [[ or even ]] },
  154. { [[a blue foreground on red background]],
  155. 'blue_foreground', 'red_background' },
  156. { [[ (select that to read it) can be used.
  157. ]] },
  158. { [[Underline, strikethrough and rise. ]], 'heading' },
  159. { [[Strikethrough]], 'strikethrough' },
  160. { [[, ]] },
  161. { [[underline]], 'underline' },
  162. { [[, ]] },
  163. { [[double underline]], 'double_underline' },
  164. { [[, ]] },
  165. { [[superscript]], 'superscript' },
  166. { [[, and ]] },
  167. { [[subscript]], 'subscript' },
  168. { [[ are all supported.
  169. ]] },
  170. { [[Images. ]], 'heading' },
  171. { [[The buffer can have images in it: ]] },
  172. { pixbuf }, { pixbuf }, { pixbuf },
  173. { [[ for example.
  174. ]] },
  175. { [[Spacing. ]], 'heading' },
  176. { [[You can adjust the amount of space before each line.
  177. ]] },
  178. { [[This line has a whole lot of space before it.
  179. ]], 'big_gap_before_line', 'wide_margins' },
  180. { [[You can also adjust the amount of space after each line; this line has a while lot of space after it
  181. ]], 'big_gap_after_line', 'wide_margins' },
  182. { [[You can also adjust the amount of space between wrapped lines; this line has extra space between each wrapped line in the same paragraph. To show off wrapping some filler text: the quick brown fox jumped over the lazy dog. Blah blah blah blah blah blah blah blah blah.
  183. ]], 'double_spaced_line', 'wide_margins' },
  184. { [[Editability. ]], 'heading' },
  185. { [[This line is 'locked down' and can't be edited by the user - just try it! You can't delete this line.
  186. ]], 'not_editable' },
  187. { [[Wrapping. ]], 'heading' },
  188. { [[This line (and most of the others in this buffer) is word-wrapped, using the proper Unicode algorithm. Word wrap should work in all scripts and languages that GTK+ supports. Let's make this a long paragraph to demonstrate: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
  189. ]] },
  190. { [[This line has character-based wrapping, and can wrap between any two character glyphs. Let's make this a long paragraph to demonstrate: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
  191. ]], 'char_wrap' },
  192. { [[This line has all wrapping turned off, so it makes the horizontal scrollbar appear.
  193. ]], 'no_wrap' },
  194. { [[Justification. ]], 'heading' },
  195. { [[
  196. This line has center justification
  197. ]], 'center' },
  198. { [[This line has right jusitification]], 'right_justify' },
  199. { [[
  200. This line has big wide margins. Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
  201. ]], 'wide_margins' },
  202. { [[Internationalization. ]], 'heading' },
  203. { [[
  204. You can put all sorts of Unicode text in the buffer.
  205. German (Deutsch Süd) Grüß Gott
  206. Greek (Ελληνικά) Γειά σας
  207. Hebrew שלום
  208. Japanese (日本語)
  209. The widget properly handles bidirectional text, word wrapping, DOS/UNIX/Unicode paragraph separators, grapheme boundaries, and so on using the Pango internationalization framework.
  210. Here's a word-wrapped quote in a right-to-left language:
  211. ]] },
  212. { [[وقد بدأ ثلاث من أكثر المؤسسات تقدما في شبكة اكسيون برامجها كمنظمات لا تسعى للربح، ثم تحولت في السنوات الخمس الماضية إلى مؤسسات مالية منظمة، وباتت جزءا من النظام المالي في بلدانها، ولكنها تتخصص في خدمة قطاع المشروعات الصغيرة. وأحد أكثر هذه المؤسسات نجاحا هو »بانكوسول« في بوليفيا.
  213. ]], 'rtl_quote' },
  214. { [[You can put widgets in the buffer: Here s a button: ]] },
  215. function()
  216. return Gtk.Button {
  217. label = "Click Me",
  218. }
  219. end,
  220. { [[ and a menu: ]] },
  221. function()
  222. local combo = Gtk.ComboBoxText {}
  223. combo:append_text("Option 1")
  224. combo:append_text("Option 2")
  225. combo:append_text("Option 3")
  226. return combo
  227. end,
  228. { [[ and a scale: ]] },
  229. function()
  230. local scale = Gtk.Scale {
  231. adjustment = Gtk.Adjustment {
  232. lower = 0,
  233. upper = 100,
  234. }
  235. }
  236. scale:set_size_request(70, -1)
  237. return scale
  238. end,
  239. { [[ and an animation: ]] },
  240. function()
  241. return Gtk.Image { file = dir:get_child('floppybuddy.gif'):get_path() }
  242. end,
  243. { [[ finally a text entry: ]] },
  244. function()
  245. return Gtk.Entry()
  246. end,
  247. { [[.
  248. This demo does not demonstrate all the Gtk.TextBuffer features; it leaves out, for example: invisible/hidden text, tab stops, application-drawn areas on the sides of the widget for displaying breakpoints and such...]] }
  249. } do
  250. if type(item) == 'function' then
  251. anchors[buffer:create_child_anchor(iter)] = item
  252. elseif type(item[1]) == 'string' then
  253. local offset = iter:get_offset()
  254. buffer:insert(iter, item[1], -1)
  255. for i = 2, #item do
  256. buffer:apply_tag_by_name(
  257. item[i], buffer:get_iter_at_offset(offset), iter)
  258. end
  259. elseif GdkPixbuf.Pixbuf:is_type_of(item[1]) then
  260. buffer:insert_pixbuf(iter, item[1])
  261. end
  262. end
  263. -- Apply word_wrap tag to the whole buffer.
  264. buffer:apply_tag_by_name('word_wrap', buffer:get_bounds())
  265. local window = Gtk.Window {
  266. title = "TextView",
  267. default_width = 450,
  268. default_height = 450,
  269. Gtk.Paned {
  270. orientation = 'VERTICAL',
  271. border_width = 5,
  272. Gtk.ScrolledWindow {
  273. Gtk.TextView {
  274. id = 'view1',
  275. buffer = buffer,
  276. },
  277. },
  278. Gtk.ScrolledWindow {
  279. Gtk.TextView {
  280. id = 'view2',
  281. buffer = buffer,
  282. }
  283. },
  284. },
  285. }
  286. -- Create and attach widgets to anchors.
  287. for _, view in pairs { window.child.view1, window.child.view2 } do
  288. for anchor, creator in pairs(anchors) do
  289. view:add_child_at_anchor(creator(), anchor)
  290. end
  291. end
  292. window:show_all()
  293. return window
  294. end,
  295. "Text Widget/Multiple Views",
  296. table.concat {
  297. [[The Gtk.TextView widget displays a Gtk.TextBuffer. One ]],
  298. [[Gtk.TextBuffer can be displayed by multiple Gtk.TextViews. ]],
  299. [[This demo has two views displaying a single buffer, and shows off ]],
  300. [[the widget's text formatting features.]],
  301. }