Class: Gtk2CheckBoxes

Inherits:
Object
  • Object
show all
Extended by:
Rafini::Empty
Defined in:
lib/gtk2checkboxes.rb,
lib/gtk2checkboxes/config.rb,
lib/gtk2checkboxes/gtk2checkboxes.rb

Defined Under Namespace

Classes: EntryDialog, YesNo

Constant Summary collapse

HELP =
<<~HELP
  Usage:
    gtk2checkboxes [:options+]
  Options:
    -h --help
    -v --version
    --minime      \t Real minime
    --notoggle    \t Minime wont toggle decorated and keep above
    --notdecorated\t Dont decorate window
HELP
VERSION =
'3.1.230114'
CACHE =
File.join UserSpace::XDG['cache'], 'gtk3app', 'gtk2checkboxes'
DATA_DIR =
File.join UserSpace::XDG['data'], 'gtk3app', 'gtk2checkboxes'
EDITOR =
`which gedit mousepad pluma kwrite`.split.first || 'xterm -e vi'
CONFIG =
{
  Editor: "#{EDITOR} $cachefile",
  DefaultTab: 'TODO',
  HelpFile: 'https://github.com/carlosjhr64/gtk2checkboxes',
  Logo: "#{DATA_DIR}/logo.png",

  about_dialog: {
    set_program_name: 'Gtk2CheckBoxes',
    set_version: VERSION.semantic(0..1),
    set_copyright: '(c) 2021 CarlosJHR64',
    set_comments: 'A Ruby Gtk3App With Check-Boxes',
    set_website: 'https://github.com/carlosjhr64/gtk2checkboxes',
    set_website_label: 'See it at GitHub!',
  },

  DIALOG_ENTRY: a0,
  dialog_entry: h0,
  dialog_entry!: [:DIALOG_ENTRY, :dialog_entry, 'activate'],

  ITEM_DIALOG: [title: 'Append Item'],
  item_dialog: h0,
  item_dialog!: [:ITEM_DIALOG, :item_dialog],

  RENAME_DIALOG: [title: 'Raname Page'],
  rename_dialog: h0,
  rename_dialog!: [:RENAME_DIALOG, :rename_dialog],

  ADD_DIALOG: [title: 'Add Page'],
  add_dialog: h0,
  add_dialog!: [:ADD_DIALOG, :add_dialog],

  UNIQ_NAME: [title: 'Need one unique word:'],
  uniq_name: h0,
  uniq_name!: [:UNIQ_NAME, :uniq_name],

  UNIQ_ITEM: [title: 'Need unique item:'],
  uniq_item: h0,
  uniq_item!: [:UNIQ_ITEM, :uniq_item],

  DELETE_DIALOG: [title: 'Detete Page'],
  delete_dialog: h0,
  delete_dialog!: [:DELETE_DIALOG, :delete_dialog],

  DELETE_LABEL: ['Are you sure?'],
  delete_label: h0,
  delete_label!: [:DELETE_LABEL, :delete_label],

  window: {
    set_title: 'Gtk2CheckBoxes',
    set_window_position: :center,
  },

  NOTEBOOK: a0,
  notebook: h0,
  notebook!: [:NOTEBOOK, :notebook],

  tab_label: h0,

  VBOX: [:vertical],
  vbox: {into: [:append_page]},
  vbox!: [:VBOX, :vbox],

  CHECKBUTTON: a0,
  checkbutton: h0,
  checkbutton!: [:CHECKBUTTON, :checkbutton],

  LABEL: a0,
  label: {set_xalign: 0.01},
  label!: [:LABEL, :label],

  BUTTON: a0,
  button: {set_xalign: 0.01},
  button!: [:BUTTON, :button],

  HBOX: [:horizontal],
  hbox: h0,
  hbox!: [:HBOX, :hbox],

  APPEND_ITEM: [label: 'Append item'],
  append_item: h0,
  append_item!: [:APPEND_ITEM, :append_item],

  EDIT_PAGE: [label: 'Edit'],
  edit_page: h0,
  edit_page!: [:EDIT_PAGE, :edit_page],

  ADD_PAGE: [label: 'Add'],
  add_page: h0,
  add_page!: [:ADD_PAGE, :add_page],

  RENAME_PAGE: [label: 'Rename'],
  rename_page: h0,
  rename_page!: [:RENAME_PAGE, :rename_page],

  DELETE_PAGE: [label: 'Delete'],
  delete_page: h0,
  delete_page!: [:DELETE_PAGE, :delete_page],

  app_menu: {
    add_menu_item: [:minime!, :help!, :about!, :quit!],
  },
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage, toolbar, options) ⇒ Gtk2CheckBoxes

Returns a new instance of Gtk2CheckBoxes.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 187

def initialize(stage, toolbar, options)
  @notebook = Such::Notebook.new stage, :notebook!
  Find.find(CACHE) do |fn|
    Find.prune if !(fn==CACHE) && File.directory?(fn)
    case fn
    when %r{/\w+\.md$}
      add_page fn, populate:true
    when %r{/\w+\.md\.bak$}
      File.unlink fn
    end
  end
  add_page CONFIG[:DefaultTab], touch:true if @notebook.children.empty?
  @tools = Such::Box.new toolbar, :hbox!
  Such::Button.new @tools, :append_item! do
    if item = get_new_item(:item_dialog!)
      append item
      add_check_button(page, item, false).show_all
    end
  end
  Such::Button.new @tools, :edit_page! do
    start = Time.now
    system CONFIG[:Editor].sub('$cachefile', cachefile)
    reload.show_all if File.mtime(cachefile) > start
  end
  Such::Button.new @tools, :rename_page! do
    if text = get_new_page_name(:rename_dialog!)
      File.rename cachefile, File.join(CACHE, text+'.md')
      set_tab_text text
    end
  end
  Such::Button.new @tools, :add_page! do
    if text = get_new_page_name(:add_dialog!)
      add_page(text, touch:true).show_all
    end
  end
  Such::Button.new @tools, :delete_page! do
    dialog = YesNo.new :delete_dialog!
    dialog.label :delete_label!
    Gtk3App.transient dialog
    if dialog.yes?
      File.rename cachefile, cachefile+'.bak'
      delete
    end
  end
  Gtk3App.logo_press_event do |button|
    case button
    when 1
      Gtk3App.minime!
    # When 2 Nothing
    # When 3 gets captured by Gtk3App's main menu
    end
  end
end

Class Method Details

.runObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gtk2checkboxes.rb', line 14

def self.run
  # StdLib
  require 'find'
  require 'fileutils'
  # Gems
  require 'gtk3app'
  # This Gem
  require_relative 'gtk2checkboxes/config.rb'
  require_relative 'gtk2checkboxes/gtk2checkboxes.rb'
  # Run
  Gtk3App.run(klass:Gtk2CheckBoxes)
end

Instance Method Details

#add_check_button(vbox, text, status) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 68

def add_check_button(vbox, text, status)
  checkbutton = Such::CheckButton.new(
    vbox,
    {set_label: text, set_active: status},
    :checkbutton!,
    'toggled'
  ) do
    x = checkbutton.active? ? 'x' : ' '
    # Note that x was toggled, so the xbox is inverted for the matcher.
    xbox = checkbutton.active? ? '- [ ]' : '- [x]'
    matcher = "#{xbox} #{checkbutton.label}"
    File.open(cachefile, 'r+') do |fh|
      fh.each_line do |line|
        if matcher == line.chomp
          fh.seek(3-line.length, IO::SEEK_CUR)
          fh.write x
          break
        end
      end
    end
  end
end

#add_command(vbox, command, exe) ⇒ Object

Spawn command defined



53
54
55
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 53

def add_command(vbox, command, exe)
  Such::Button.new(vbox, {set_label: command}, :button!){spawn exe}
end

#add_label(vbox, text) ⇒ Object



64
65
66
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 64

def add_label(vbox, text)
  Such::Label.new(vbox, {set_label: text}, :label!)
end

Open link target with default application:



58
59
60
61
62
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 58

def add_link(vbox, link, url)
  Such::Button.new(vbox, {set_label: link}, :button!) do
    system(CONFIG[:Open], url)
  end
end

#add_page(fn, populate: false, touch: false) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 178

def add_page(fn, populate:false, touch:false)
  label = File.basename fn, '.*'
  vbox = Such::Box.new @notebook, :vbox!
  @notebook.set_tab_label vbox, Such::Label.new([label], :tab_label)
  populate_page(fn, vbox) if populate and File.exist? fn
  FileUtils.touch File.join(CACHE, label+'.md') if touch
  vbox
end

#append(text) ⇒ Object



174
175
176
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 174

def append(text)
  File.open(cachefile, 'a'){_1.puts '- [ ] '+text}
end

#cachefileObject



48
49
50
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 48

def cachefile
  File.join CACHE, tab+'.md'
end

#clearObject



112
113
114
115
116
117
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 112

def clear
  page.each do |item|
    page.remove item
    item.destroy
  end
end

#deleteObject



124
125
126
127
128
129
130
131
132
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 124

def delete
 clear
 # Notebook requires at least one page
 if @notebook.children.length > 1
   @notebook.remove_page @notebook.page
 else
   FileUtils.touch cachefile
 end
end

#get_new_item(dialog_key) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 163

def get_new_item(dialog_key)
  loop do
    dialog = EntryDialog.new dialog_key
    dialog.entry :dialog_entry!
    Gtk3App.transient dialog
    text = dialog.text
    return text unless item_exist? text
    dialog_key = :uniq_item!
  end
end

#get_new_page_name(dialog_key) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 145

def get_new_page_name(dialog_key)
  loop do
    dialog = EntryDialog.new dialog_key
    dialog.entry :dialog_entry!
    Gtk3App.transient dialog
    text = dialog.text
    return text if text.nil? or (/^\w+$/.match? text and not tab_exist? text)
    dialog_key = :uniq_name!
  end
end

#item_exist?(text) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 156

def item_exist?(text)
  page.each do |checkbutton|
    return true if checkbutton.label == text
  end
  return false
end

#pageObject



40
41
42
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 40

def page
  @notebook.children[@notebook.page]
end

#populate_page(fn = cachefile, vbox = page) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 91

def populate_page(fn=cachefile, vbox=page)
  File.open(fn, 'r') do |fh|
    fh.each do |line|
      line.chomp!
      case line
      when %r{^\- \[ \] (.*)$}
        add_check_button vbox, $1, false
      when %r{^\- \[x\] (.*)$}
        add_check_button vbox, $1, true
      when %r{^\* \[([^\[\]]+)\]\(([^\(\)]+)\)}
        add_link vbox, $1, $2
      when %r{^\* (.*)$}
        add_label vbox, $1
      when %r{^\+ ([^:]+): (.*)$}
        add_command vbox, $1, $2
      end
    end
  end
  vbox
end

#reloadObject



119
120
121
122
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 119

def reload
  clear
  populate_page
end

#set_tab_text(text) ⇒ Object



134
135
136
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 134

def set_tab_text(text)
  @notebook.get_tab_label(page).set_text text
end

#tabObject



44
45
46
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 44

def tab
  @notebook.get_tab_label(page).text
end

#tab_exist?(text) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 138

def tab_exist?(text)
  @notebook.children.each do |page|
    return true if text == @notebook.get_tab_label(page).text
  end
  return false
end