class Gtk2CheckBoxes

Constants

CACHE
CONFIG
DATA_DIR
HELP
VERSION

Public Class Methods

new(stage, toolbar, options) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 163
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
    end
  end
  Such::Button.new @tools, :edit_page! do
    start = Time.now
    system CONFIG[:Editor].sub('$cachefile', cachefile)
    reload 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
    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
run() click to toggle source
# 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

Public Instance Methods

add_check_button(vbox, text, status) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 52
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_page(fn, populate:false, touch:false) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 155
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
end
append(text) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 151
def append(text)
  File.open(cachefile, 'a'){_1.puts '- [ ] '+text}
end
cachefile() click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 48
def cachefile
  File.join CACHE, tab+'.md'
end
clear() click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 89
def clear
  page.each do |item|
    page.remove item
    item.destroy
  end
end
delete() click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 101
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) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 140
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) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 122
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) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 133
def item_exist?(text)
  page.each do |checkbutton|
    return true if checkbutton.label == text
  end
  return false
end
page() click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 40
def page
  @notebook.children[@notebook.page]
end
populate_page(fn=cachefile, vbox=page) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 75
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
      end
    end
  end
end
reload() click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 96
def reload
  clear
  populate_page
end
set_tab_text(text) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 111
def set_tab_text(text)
  @notebook.get_tab_label(page).set_text text
end
tab() click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 44
def tab
  @notebook.get_tab_label(page).text
end
tab_exist?(text) click to toggle source
# File lib/gtk2checkboxes/gtk2checkboxes.rb, line 115
def tab_exist?(text)
  @notebook.children.each do |page|
    return true if text == @notebook.get_tab_label(page).text
  end
  return false
end