canis 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +45 -0
  3. data/CHANGES +52 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +24 -0
  7. data/Rakefile +2 -0
  8. data/canis.gemspec +25 -0
  9. data/examples/alpmenu.rb +46 -0
  10. data/examples/app.sample +19 -0
  11. data/examples/appemail.rb +191 -0
  12. data/examples/atree.rb +105 -0
  13. data/examples/bline.rb +181 -0
  14. data/examples/common/devel.rb +319 -0
  15. data/examples/common/file.rb +93 -0
  16. data/examples/data/README.markdown +9 -0
  17. data/examples/data/brew.txt +38 -0
  18. data/examples/data/color.2 +37 -0
  19. data/examples/data/gemlist.txt +59 -0
  20. data/examples/data/lotr.txt +12 -0
  21. data/examples/data/ports.txt +136 -0
  22. data/examples/data/table.txt +37 -0
  23. data/examples/data/tasks.csv +88 -0
  24. data/examples/data/tasks.txt +27 -0
  25. data/examples/data/todo.txt +16 -0
  26. data/examples/data/todocsv.csv +28 -0
  27. data/examples/data/unix1.txt +21 -0
  28. data/examples/data/unix2.txt +11 -0
  29. data/examples/dbdemo.rb +506 -0
  30. data/examples/dirtree.rb +177 -0
  31. data/examples/newtabbedwindow.rb +100 -0
  32. data/examples/newtesttabp.rb +92 -0
  33. data/examples/tabular.rb +212 -0
  34. data/examples/tasks.rb +179 -0
  35. data/examples/term2.rb +88 -0
  36. data/examples/testbuttons.rb +307 -0
  37. data/examples/testcombo.rb +102 -0
  38. data/examples/testdb.rb +182 -0
  39. data/examples/testfields.rb +208 -0
  40. data/examples/testflowlayout.rb +43 -0
  41. data/examples/testkeypress.rb +98 -0
  42. data/examples/testlistbox.rb +187 -0
  43. data/examples/testlistbox1.rb +199 -0
  44. data/examples/testmessagebox.rb +144 -0
  45. data/examples/testprogress.rb +116 -0
  46. data/examples/testree.rb +107 -0
  47. data/examples/testsplitlayout.rb +53 -0
  48. data/examples/testsplitlayout1.rb +49 -0
  49. data/examples/teststacklayout.rb +48 -0
  50. data/examples/testwsshortcuts.rb +68 -0
  51. data/examples/testwsshortcuts2.rb +129 -0
  52. data/lib/canis.rb +16 -0
  53. data/lib/canis/core/docs/index.txt +104 -0
  54. data/lib/canis/core/docs/list.txt +16 -0
  55. data/lib/canis/core/docs/style_help.yml +34 -0
  56. data/lib/canis/core/docs/tabbedpane.txt +15 -0
  57. data/lib/canis/core/docs/table.txt +31 -0
  58. data/lib/canis/core/docs/textpad.txt +48 -0
  59. data/lib/canis/core/docs/tree.txt +23 -0
  60. data/lib/canis/core/include/.DS_Store +0 -0
  61. data/lib/canis/core/include/action.rb +83 -0
  62. data/lib/canis/core/include/actionmanager.rb +49 -0
  63. data/lib/canis/core/include/appmethods.rb +179 -0
  64. data/lib/canis/core/include/bordertitle.rb +49 -0
  65. data/lib/canis/core/include/canisparser.rb +100 -0
  66. data/lib/canis/core/include/colorparser.rb +437 -0
  67. data/lib/canis/core/include/defaultfilerenderer.rb +64 -0
  68. data/lib/canis/core/include/io.rb +320 -0
  69. data/lib/canis/core/include/layouts/SplitLayout.rb +161 -0
  70. data/lib/canis/core/include/layouts/abstractlayout.rb +213 -0
  71. data/lib/canis/core/include/layouts/flowlayout.rb +104 -0
  72. data/lib/canis/core/include/layouts/stacklayout.rb +109 -0
  73. data/lib/canis/core/include/listbindings.rb +89 -0
  74. data/lib/canis/core/include/listeditable.rb +319 -0
  75. data/lib/canis/core/include/listoperations.rb +61 -0
  76. data/lib/canis/core/include/listselectionmodel.rb +388 -0
  77. data/lib/canis/core/include/multibuffer.rb +173 -0
  78. data/lib/canis/core/include/ractionevent.rb +73 -0
  79. data/lib/canis/core/include/rchangeevent.rb +27 -0
  80. data/lib/canis/core/include/rhistory.rb +95 -0
  81. data/lib/canis/core/include/rinputdataevent.rb +47 -0
  82. data/lib/canis/core/include/textdocument.rb +111 -0
  83. data/lib/canis/core/include/vieditable.rb +175 -0
  84. data/lib/canis/core/include/widgetmenu.rb +66 -0
  85. data/lib/canis/core/system/colormap.rb +165 -0
  86. data/lib/canis/core/system/keydefs.rb +32 -0
  87. data/lib/canis/core/system/ncurses.rb +237 -0
  88. data/lib/canis/core/system/panel.rb +129 -0
  89. data/lib/canis/core/system/window.rb +1081 -0
  90. data/lib/canis/core/util/ansiparser.rb +119 -0
  91. data/lib/canis/core/util/app.rb +696 -0
  92. data/lib/canis/core/util/basestack.rb +412 -0
  93. data/lib/canis/core/util/defaultcolorparser.rb +84 -0
  94. data/lib/canis/core/util/extras/README +5 -0
  95. data/lib/canis/core/util/extras/bottomline.rb +1815 -0
  96. data/lib/canis/core/util/extras/padreader.rb +192 -0
  97. data/lib/canis/core/util/focusmanager.rb +31 -0
  98. data/lib/canis/core/util/helpmanager.rb +160 -0
  99. data/lib/canis/core/util/oldwidgetshortcuts.rb +304 -0
  100. data/lib/canis/core/util/promptmenu.rb +235 -0
  101. data/lib/canis/core/util/rcommandwindow.rb +933 -0
  102. data/lib/canis/core/util/rdialogs.rb +520 -0
  103. data/lib/canis/core/util/textutils.rb +74 -0
  104. data/lib/canis/core/util/viewer.rb +238 -0
  105. data/lib/canis/core/util/widgetshortcuts.rb +508 -0
  106. data/lib/canis/core/widgets/applicationheader.rb +103 -0
  107. data/lib/canis/core/widgets/box.rb +58 -0
  108. data/lib/canis/core/widgets/divider.rb +310 -0
  109. data/lib/canis/core/widgets/extras/README.md +12 -0
  110. data/lib/canis/core/widgets/extras/rtextarea.rb +960 -0
  111. data/lib/canis/core/widgets/extras/stackflow.rb +474 -0
  112. data/lib/canis/core/widgets/keylabelprinter.rb +194 -0
  113. data/lib/canis/core/widgets/listbox.rb +326 -0
  114. data/lib/canis/core/widgets/listfooter.rb +86 -0
  115. data/lib/canis/core/widgets/rcombo.rb +210 -0
  116. data/lib/canis/core/widgets/rcontainer.rb +415 -0
  117. data/lib/canis/core/widgets/rlink.rb +30 -0
  118. data/lib/canis/core/widgets/rmenu.rb +970 -0
  119. data/lib/canis/core/widgets/rmenulink.rb +30 -0
  120. data/lib/canis/core/widgets/rmessagebox.rb +400 -0
  121. data/lib/canis/core/widgets/rprogress.rb +118 -0
  122. data/lib/canis/core/widgets/rtabbedpane.rb +631 -0
  123. data/lib/canis/core/widgets/rtabbedwindow.rb +70 -0
  124. data/lib/canis/core/widgets/rwidget.rb +3634 -0
  125. data/lib/canis/core/widgets/scrollbar.rb +147 -0
  126. data/lib/canis/core/widgets/statusline.rb +113 -0
  127. data/lib/canis/core/widgets/table.rb +1072 -0
  128. data/lib/canis/core/widgets/tabular.rb +264 -0
  129. data/lib/canis/core/widgets/textpad.rb +1674 -0
  130. data/lib/canis/core/widgets/tree.rb +690 -0
  131. data/lib/canis/core/widgets/tree/treecellrenderer.rb +150 -0
  132. data/lib/canis/core/widgets/tree/treemodel.rb +432 -0
  133. data/lib/canis/version.rb +3 -0
  134. metadata +229 -0
data/examples/tasks.rb ADDED
@@ -0,0 +1,179 @@
1
+ require 'canis/core/util/app'
2
+ require 'canis/core/include/defaultfilerenderer'
3
+ #require 'canis/core/widgets/rlist'
4
+
5
+ App.new do
6
+ def resize
7
+ tab = @form.by_name["tasklist"]
8
+ cols = Ncurses.COLS
9
+ rows = Ncurses.LINES
10
+ tab.width_pc ||= (1.0*tab.width / $orig_cols)
11
+ tab.height_pc ||= (1.0*tab.height / $orig_rows)
12
+ tab.height = (tab.height_pc * rows).floor
13
+ tab.width = (tab.width_pc * cols).floor
14
+ #$log.debug "XXX: RESIZE h w #{tab.height} , #{tab.width} "
15
+ end
16
+ @default_prefix = " "
17
+ header = app_header "canis #{Canis::VERSION}", :text_center => "Task List", :text_right =>"New Improved!"
18
+
19
+ message "Press F10 or qq to quit "
20
+
21
+ file = "data/todo.txt"
22
+ alist = File.open(file,'r').read.split("\n") if File.exists? file
23
+ #flow :margin_top => 1, :item_width => 50 , :height => FFI::NCurses.LINES-2 do
24
+ #stack :margin_top => 1, :width => :expand, :height => FFI::NCurses.LINES-4 do
25
+
26
+ #task = field :label => " Task:", :width => 50, :maxlen => 80, :bgcolor => :cyan, :color => :black
27
+ #pri = field :label => "Priority:", :width => 1, :maxlen => 1, :type => :integer,
28
+ #:valid_range => 1..9, :bgcolor => :cyan, :color => :black , :default => "5"
29
+ #pri.overwrite_mode = true
30
+ # u,se voerwrite mode for this TODO and catch exception
31
+
32
+ # modify the default file renderer
33
+ dr = DefaultFileRenderer.new
34
+ dr.insert_mapping /^x/, [:blue, :black]
35
+ dr.insert_mapping /^.1/, [:white, :blue]
36
+ dr.insert_mapping /^.2/, [:red, :blue]
37
+ dr.insert_mapping /^.3/, [:white, :black]
38
+ dr.insert_mapping /^.4/, [:green, :black]
39
+ dr.insert_mapping /^.5/, [:red, :black]
40
+ dr.insert_mapping /^.6/, [:cyan, :black]
41
+ dr.insert_mapping /^.[7-9]/, [:magenta, :black]
42
+
43
+ $data_modified = false
44
+ lb = listbox :list => alist.sort, :title => "[ todos ]", :name => "tasklist", :row => 1, :height => Ncurses.LINES-4, :width => Ncurses.COLS-1
45
+ lb.should_show_focus = false
46
+ lb.renderer dr
47
+ lb.bind_key(?d, "Delete Row"){
48
+ if confirm("Delete #{lb.current_value} ?")
49
+ lb.delete_at lb.current_index
50
+ $data_modified = true
51
+ # TODO reposition cursor at 0. use list_data_changed ?
52
+ end
53
+ }
54
+ lb.bind_key(?e, "Edit Row"){
55
+ if ((value = get_string("Edit Task:", :width => 80, :default => lb.current_value, :maxlen => 80, :width => 70)) != nil)
56
+
57
+ lb[lb.current_index]=value
58
+ $data_modified = true
59
+ end
60
+ }
61
+ lb.bind_key(?a, "Add Record"){
62
+
63
+ # ADD
64
+ task = LabeledField.new :label => " Task:", :width => 60, :maxlen => 80, :bgcolor => :cyan, :color => :black,
65
+ :name => 'task'
66
+ pri = LabeledField.new :label => "Priority:", :width => 1, :maxlen => 1, :type => :integer,
67
+ :valid_range => 1..9, :bgcolor => :cyan, :color => :black , :default => "5", :name => 'pri'
68
+ pri.overwrite_mode = true
69
+ config = {}
70
+ config[:width] = 80
71
+ config[:title] = "New Task"
72
+ tp = MessageBox.new config do
73
+ item task
74
+ item pri
75
+ button_type :ok_cancel
76
+ default_button 0
77
+ end
78
+ index = tp.run
79
+ if index == 0 # OK
80
+ # when does this memory get released ??? XXX
81
+ _t = tp.form.by_name['pri'].text
82
+ if _t != ""
83
+ val = @default_prefix + tp.form.by_name['pri'].text + ". " + tp.form.by_name['task'].text
84
+ w = @form.by_name["tasklist"]
85
+ _l = w.list
86
+ _l << val
87
+ w.list(_l.sort)
88
+ $data_modified = true
89
+ end
90
+ else # CANCEL
91
+ #return nil
92
+ end
93
+ }
94
+ # decrease priority
95
+ lb.bind_key(?-, 'decrease priority'){
96
+ line = lb.current_value
97
+ p = line[1,1].to_i
98
+ if p < 9
99
+ p += 1
100
+ line[1,1] = p.to_s
101
+ lb[lb.current_index]=line
102
+ lb.list(lb.list.sort)
103
+ $data_modified = true
104
+ end
105
+ }
106
+ # increase priority
107
+ lb.bind_key(?+, 'increase priority'){
108
+ line = lb.current_value
109
+ p = line[1,1].to_i
110
+ if p > 1
111
+ p -= 1
112
+ line[1,1] = p.to_s
113
+ lb[lb.current_index]=line
114
+ lb.list(lb.list.sort)
115
+ $data_modified = true
116
+ # how to get the new row of that item and position it there. so one
117
+ # can do consecutive increases or decreases
118
+ # cursor on old row, but current has become zero. FIXME
119
+ # Maybe setform_row needs to be called
120
+ end
121
+ }
122
+ # mark as done
123
+ lb.bind_key(?x, 'mark done'){
124
+ line = lb.current_value
125
+ line[0,1] = "x"
126
+ lb[lb.current_index]=line
127
+ lb.list(lb.list.sort)
128
+ $data_modified = true
129
+ }
130
+ # flag task with a single character
131
+ lb.bind_key(?!, 'flag'){
132
+ line = lb.current_value.chomp
133
+ value = get_string("Flag for #{line}. Enter one character.", :maxlen => 1, :width => 1)
134
+ #if ((value = get_string("Edit Task:", :width => 80, :default => lb.current_value)) != nil)
135
+ #lb[lb.current_index]=value
136
+ #end
137
+ if value ##&& value[0,1] != " "
138
+ line[0,1] = value[0,1]
139
+ lb[lb.current_index]=line
140
+ lb.list(lb.list.sort)
141
+ $data_modified = true
142
+ end
143
+ }
144
+ #end # stack
145
+ s = status_line
146
+ @form.bind(:RESIZE) { resize }
147
+
148
+ keyarray = [
149
+ ["F1" , "Help"], ["F10" , "Exit"],
150
+ ["F2", "Menu"], ["F4", "View"],
151
+ ["d", "delete item"], ["e", "edit item"],
152
+ ["a", "add item"], ["x", "close item"],
153
+ ["+", "inc priority"], ["-", "dec priority"],
154
+
155
+ ["M-x", "Command"], nil
156
+ ]
157
+
158
+ gw = get_color($reversecolor, 'green', 'black')
159
+ @adock = dock keyarray, { :row => Ncurses.LINES-2, :footer_color_pair => $datacolor,
160
+ :footer_mnemonic_color_pair => gw }
161
+
162
+ @window.confirm_close_command do
163
+ confirm "Sure you wanna quit?", :default_button => 1
164
+ end
165
+ @window.close_command do
166
+ if $data_modified
167
+ w = @form.by_name["tasklist"]
168
+ if confirm("Save tasks?", :default_button => 0)
169
+ system("cp #{file} #{file}.bak")
170
+ File.open(file, 'w') {|f|
171
+ w.list.each { |e|
172
+ f.puts(e)
173
+ }
174
+ }
175
+ end
176
+ end # if modif
177
+ end
178
+
179
+ end # app
data/examples/term2.rb ADDED
@@ -0,0 +1,88 @@
1
+ require 'canis/core/util/app'
2
+ require 'canis/core/widgets/tabular'
3
+ require 'canis/core/widgets/scrollbar'
4
+
5
+ def my_help_text
6
+ <<-eos
7
+ term2.rb
8
+ =========================================================================
9
+ Basic Usage
10
+
11
+ This example shows different ways of putting data in tabular format.
12
+
13
+ The 2 tables on the right differ in behaviour. The first puts tabular data
14
+ into a listbox so you get single/multiple selection. The second puts tabular
15
+ data into a textview, so there's no selection. <space> scrolls instead of
16
+ selects <ENTER> allows us to use the word under cursor for further actions.
17
+
18
+ To see an example of placing tabular data in a tabular widget, see tabular.rb.
19
+ The advantage of tabular_widget is column resizing, hiding, aligning and sorting.
20
+
21
+ =========================================================================
22
+ :n or Alt-n for next buffer. 'q' to quit.
23
+
24
+ eos
25
+ end
26
+ App.new do
27
+ header = app_header "canis #{Canis::VERSION}", :text_center => "Tabular Demo", :text_right =>"New Improved!", :color => :black, :bgcolor => :white, :attr => :bold
28
+ message "F10 quit, F1 Help, ? Bindings"
29
+ #install_help_text my_help_text
30
+ @form.help_manager.help_text = my_help_text
31
+
32
+ flow :width => FFI::NCurses.COLS , :height => FFI::NCurses.LINES-2 do
33
+ stack :margin_top => 1, :width_pc => 20 do
34
+ t = Tabular.new(['a', 'b'], [1, 2], [3, 4], [5,6])
35
+ listbox :list => t.render
36
+
37
+ t = Tabular.new ['a', 'b']
38
+ t << [1, 2]
39
+ t << [3, 4]
40
+ t << [4, 6]
41
+ t << [8, 6]
42
+ t << [2, 6]
43
+ #list_box :list => t.to_s.split("\n")
44
+ listbox :list => t.render
45
+ end # stack
46
+
47
+
48
+ file = "data/tasks.csv"
49
+ lines = File.open(file,'r').readlines
50
+ heads = %w[ id sta type prio title ]
51
+ t = Tabular.new do |t|
52
+ t.headings = heads
53
+ lines.each { |e| t.add_row e.chomp.split '|' }
54
+ end
55
+
56
+ t = t.render
57
+ wid = t[0].length + 2
58
+ wid = 30
59
+ stack :margin_top => 1, :width_pc => 80 , :height_pc => 100 do
60
+ listbox :list => t, :title => '[ tasks ]', :height_pc => 60
61
+
62
+ r = `ls -l`
63
+ res = r.split("\n")
64
+
65
+ t = Tabular.new do
66
+ # self.headings = 'Perm', 'Gr', 'User', 'U', 'Size', 'Mon', 'Date', 'Time', 'File' # changed 2011 dts
67
+ self.headings = 'User', 'Size', 'Mon', 'Date', 'Time', 'File'
68
+ res.each { |e|
69
+ cols = e.split
70
+ next if cols.count < 6
71
+ cols = cols[3..-1]
72
+ cols = cols[0..5] if cols.count > 6
73
+ add_row cols
74
+ }
75
+ column_width 1, 6
76
+ align_column 1, :right
77
+ end
78
+ #lb = list_box :list => t.render2
79
+ lb = textview :set_content => t.render, :title => '[ls -l]', :height_pc => 40
80
+ lb.bind(:PRESS){|tae|
81
+ alert "Pressed list on line #{tae.current_index} #{tae.word_under_cursor(nil, nil, "|")} "
82
+ }
83
+ Scrollbar.new @form, :parent => lb
84
+ #make a textview that is vienabled by default.
85
+ end
86
+ end # end
87
+
88
+ end # app
@@ -0,0 +1,307 @@
1
+ # This program tests out various buttons.
2
+ ## I have used both DSL and conventional constructors for each button to test out.
3
+ ## Had to fix checkbox code (text and mnemonic to return self).
4
+ # Had to fix radio button code, remove raise from constructor
5
+ # 2014-04-30 - 23:46 removed variable in radiobutton to try out newly introduced
6
+ # ButtonGroup.
7
+ ## -----
8
+ ## -- THIS WILL NOT WORK WITH < 0.0.15. WILL REQUIRE DOWNLOADING FIXES FROM GITHUB
9
+ ## OR HAVING AT LEAST 0.0.15
10
+ #
11
+ require 'logger'
12
+ require 'canis'
13
+ require 'canis/core/include/appmethods.rb'
14
+ def help_text
15
+ <<-eos
16
+ BUTTONS HELP
17
+
18
+ This is some help text for testbuttons.
19
+ To select any button press the SPACEBAR, although ENTER will also work.
20
+ You may also press the mnemonic or hotkey on the label..
21
+
22
+ The toggle button toggles the kind of dialog for the Cancel button. Modern look
23
+ and feel refers to a popup with buttons. This is like the links editor.
24
+
25
+ Classic look and feel refers to a line at the bottom of the screen, with a y/n prompt.
26
+ THis is like a lot of older apps, i think Pine and maybe vim.
27
+
28
+
29
+ Alt-c/F10 - Exit application
30
+
31
+
32
+
33
+ -----------------------------------------------------------------------
34
+ eos
35
+ end
36
+ if $0 == __FILE__
37
+
38
+ include Canis
39
+ include Canis::Utils
40
+
41
+ begin
42
+ # Initialize curses
43
+ Canis::start_ncurses # this is initializing colors via ColorMap.setup
44
+ path = File.join(ENV["LOGDIR"] || "./" ,"canis14.log")
45
+ file = File.open(path, File::WRONLY|File::TRUNC|File::CREAT)
46
+ $log = Logger.new(path)
47
+ $log.level = Logger::DEBUG
48
+
49
+ @lookfeel = :classic # :dialog # or :classic
50
+ @lookfeel = :dialog # or :classic
51
+
52
+ @window = Canis::Window.root_window
53
+ # Initialize few color pairs
54
+ # Create the window to be associated with the form
55
+ # Un post form and free the memory
56
+
57
+ catch(:close) do
58
+ colors = Ncurses.COLORS
59
+ $log.debug "START #{colors} colors #{$0} --------- #{@window} "
60
+ @form = Form.new @window
61
+ title = (" "*30) + "Demo of Buttons" + Canis::VERSION
62
+ Label.new @form, {'text' => title, :row => 0, :col => 0, :color => 'green', :bgcolor => 'black'}
63
+ r = 1; fc = 12;
64
+ r += 1
65
+ row = 5
66
+ col = 3
67
+
68
+ _mess = "Message Comes Here"
69
+ message_label = Canis::Label.new @form, {text: _mess,
70
+ :name=>"message_label",:row => Ncurses.LINES-1, :col => 1, :width => 60,
71
+ :height => 2, :color => :cyan}
72
+
73
+ $results = Variable.new
74
+ $results.value = "A variable"
75
+
76
+ row += 1
77
+ checkbutton = CheckBox.new(@form).
78
+ variable($results).
79
+ onvalue("Selected bold ").
80
+ offvalue("UNselected bold").
81
+ width(18).
82
+ text("Bold attribute ").
83
+ mnemonic('B').
84
+ row(row).
85
+ col(col)
86
+ row += 1
87
+ # trying to do without thse stupid variables.
88
+ #@cb_rev = Variable.new false # related to checkbox reverse
89
+ #cbb = @cb_rev
90
+ checkbutton1 = CheckBox.new @form do
91
+ #variable cbb # $cb_rev
92
+ #value = true
93
+ onvalue "Selected reverse "
94
+ offvalue "UNselected reverse"
95
+ text "Reverse attribute "
96
+ width 18
97
+ row row
98
+ col col
99
+ mnemonic 'R'
100
+ end
101
+ row += 1
102
+ togglebutton = ToggleButton.new(@form).
103
+ value( true).
104
+ onvalue( " Toggle Down ").
105
+ offvalue(" Untoggle ").
106
+ row(row).
107
+ col(col).
108
+ mnemonic('T')
109
+ #underline 0
110
+ togglebutton.command do
111
+ if togglebutton.value
112
+ message_label.text "Modern look and feel for dialogs"
113
+ else
114
+ message_label.text "Classic look and feel for dialogs"
115
+ end
116
+ end
117
+
118
+ @form.bind(:ENTER) { |f| f.label && f.label.bgcolor = 'red' if f.respond_to? :label}
119
+ @form.bind(:LEAVE) { |f| f.label && f.label.bgcolor = 'black' if f.respond_to? :label}
120
+
121
+ row += 1
122
+ colorlabel = Label.new @form, {'text' => "Select a color:", "row" => row, "col" => col,
123
+ "color"=>"cyan", "mnemonic" => 'S'}
124
+ #$radio = Variable.new
125
+ #$radio.update_command(colorlabel) {|tv, label| label.color tv.value; }
126
+ #
127
+ #$radio.update_command() {|tv| @form.widgets.each { |e| next unless e.is_a? Widget;
128
+ #e.bgcolor tv.value }; }
129
+
130
+ # whenever updated set colorlabel and messagelabel to bold
131
+ $results.update_command(colorlabel,checkbutton) {|tv, label, cb|
132
+ attrs = cb.value ? 'bold' : 'normal'; label.attr(attrs); message_label.attr(attrs)}
133
+
134
+
135
+ checkbutton1.command do
136
+ attrs = checkbutton1.value ? 'reverse' : 'normal'; colorlabel.attr(attrs); message_label.attr(attrs)
137
+ end
138
+ row += 1
139
+ dlen = 10
140
+ # if we try conventional style then constructor throws exception since @variable must be set
141
+ radio1 = RadioButton.new(@form).
142
+ #variable($radio).
143
+ text("red").
144
+ value("red").
145
+ color("red").
146
+ width(dlen). # helps when right aligning
147
+ row(row).
148
+ col(col)
149
+
150
+ radio11 = RadioButton.new @form do
151
+ #variable $radio
152
+ text "c&yan"
153
+ value "cyan"
154
+ color "cyan"
155
+ width dlen # helps when right aligning
156
+ row row
157
+ col col+24
158
+ end
159
+
160
+ row += 1
161
+ radio2 = RadioButton.new @form do
162
+ #variable $radio
163
+ text "&green"
164
+ value "green"
165
+ color "green"
166
+ width dlen # helps when right aligning
167
+ row row
168
+ col col
169
+ end
170
+ radio22 = RadioButton.new @form do
171
+ #variable $radio
172
+ text "magenta"
173
+ value "magenta"
174
+ color "magenta"
175
+ width dlen # helps when right aligning
176
+ row row
177
+ col col+24
178
+ end
179
+ #$radio.update_command(colorlabel) {|tv, label| label.color tv.value; }
180
+ #
181
+ #$radio.update_command() {|tv| @form.widgets.each { |e| next unless e.is_a? Widget;
182
+ #e.bgcolor tv.value }; }
183
+ colorlabel.label_for radio1
184
+
185
+ group = ButtonGroup.new
186
+ [radio1, radio2, radio11, radio22].each { |r|
187
+ group.add r
188
+ }
189
+ group.command(colorlabel) {|tv, label| label.color tv.value; }
190
+ #
191
+ group.command() {|tv| @form.widgets.each { |e| next unless e.is_a? Widget;
192
+ e.bgcolor tv.value }; }
193
+
194
+ # instead of using frozen, I will use a PropertyVeto
195
+ # to disallow changes to color itself
196
+ veto = lambda { |e, name|
197
+ if e.property_name == 'color'
198
+ if e.newvalue != name
199
+ raise PropertyVetoException.new("Cannot change this at all!", e)
200
+ end
201
+ elsif e.property_name == 'bgcolor'
202
+ raise PropertyVetoException.new("Cannot change this!", e)
203
+ end
204
+ }
205
+ [radio1, radio2, radio11, radio22].each { |r|
206
+ r.bind(:PROPERTY_CHANGE) do |e| veto.call(e, r.text) end
207
+ }
208
+
209
+ require 'canis/core/widgets/statusline'
210
+ @status_line = Canis::StatusLine.new @form, :row => Ncurses.LINES-2
211
+ @status_line.command {
212
+ "F1 Help | F2 Menu | F3 View | F4 Shell | F5 Sh | %20s" % [message_label.text]
213
+ }
214
+ row += 1 #2
215
+ ## DSL style of construction
216
+ ok_button = Button.new @form do
217
+ text "OK"
218
+ name "OK"
219
+ row row
220
+ col col
221
+ #attr 'reverse'
222
+ #highlight_background "white"
223
+ #highlight_foreground "blue"
224
+ mnemonic 'O'
225
+ end
226
+ ok_button.command() { |eve|
227
+ alert("Hope you enjoyed this demo - Press the Cancel button to quit", {'title' => "Hello", :bgcolor => :blue , :color => :white})
228
+ }
229
+
230
+ ## conventional style of construction
231
+ # using ampersand to set mnemonic
232
+ cancel_button = Button.new(@form).
233
+ text("&Cancel").
234
+ name("Cancel").
235
+ row(row).
236
+ col(col + 10)
237
+ #attr 'reverse'
238
+ #highlight_background "white"
239
+ #highlight_foreground "blue"
240
+ #surround_chars ['{ ',' }'] ## change the surround chars
241
+ cancel_button.command { |aeve|
242
+ #if @lookfeel == :dialog
243
+ if togglebutton.value == true
244
+ ret = confirm("Do your really want to quit?")
245
+ else
246
+ ret = rb_confirm("Do your really want to quit?")
247
+ end
248
+ if ret
249
+ throw(:close);
250
+ else
251
+ message_label.text = "Quit aborted"
252
+ end
253
+ }
254
+ #col += 22
255
+ @form.repaint
256
+ @window.wrefresh
257
+ Ncurses::Panel.update_panels
258
+
259
+ # the main loop
260
+
261
+ while((ch = @window.getchar()) != FFI::NCurses::KEY_F10 )
262
+ break if ch == ?\C-q.getbyte(0)
263
+ begin
264
+ @form.handle_key(ch)
265
+
266
+ rescue FieldValidationException => fve
267
+ alert fve.to_s
268
+
269
+ f = @form.get_current_field
270
+ # lets restore the value
271
+ if f.respond_to? :restore_original_value
272
+ f.restore_original_value
273
+ @form.repaint
274
+ end
275
+ $error_message.value = ""
276
+ rescue => err
277
+ $log.error( err) if err
278
+ $log.error(err.backtrace.join("\n")) if err
279
+ textdialog err
280
+ $error_message.value = ""
281
+ end
282
+
283
+ # this should be avoided, we should not muffle the exception and set a variable
284
+ # However, we have been doing that
285
+ if $error_message.get_value != ""
286
+ if @lookfeel == :dialog
287
+ alert($error_message, {:bgcolor => :red, 'color' => 'yellow'}) if $error_message.get_value != ""
288
+ else
289
+ print_error_message $error_message, {:bgcolor => :red, :color => :yellow}
290
+ end
291
+ $error_message.value = ""
292
+ end
293
+
294
+ @window.wrefresh
295
+ end # while loop
296
+ end # catch
297
+ rescue => ex
298
+ ensure
299
+ $log.debug " -==== EXCEPTION =====-"
300
+ $log.debug( ex) if ex
301
+ $log.debug(ex.backtrace.join("\n")) if ex
302
+ @window.destroy if !@window.nil?
303
+ Canis::stop_ncurses
304
+ puts ex if ex
305
+ puts(ex.backtrace.join("\n")) if ex
306
+ end
307
+ end