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 =
"Usage:\n  gtk2checkboxes [:options+]\nOptions:\n  -h --help\n  -v --version\n  --minime      \\t Real minime\n  --notoggle    \\t Minime wont toggle decorated and keep above\n  --notdecorated\\t Dont decorate window\n"
VERSION =
'3.0.210824'
CACHE =
File.join UserSpace::XDG['cache'], 'gtk3app', 'gtk2checkboxes'
DATA_DIR =
File.join UserSpace::XDG['data'], 'gtk3app', 'gtk2checkboxes'
CONFIG =
{
  DefaultTab: 'TODO',
  Editor: 'gedit $cachefile',
  HelpFile: 'https://github.com/carlosjhr64/gtk2checkboxes',
  : "#{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],

  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],
    show: a0,
  },
  vbox!: [:VBOX, :vbox],

  CHECKBUTTON: a0,
  checkbutton: {show: a0},
  checkbutton!: [:CHECKBUTTON, :checkbutton],

  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.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
# 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

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



155
156
157
158
159
160
161
# 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) ⇒ Object



151
152
153
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 151

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



89
90
91
92
93
94
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 89

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

#deleteObject



101
102
103
104
105
106
107
108
109
# 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) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# 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) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# 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) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
# 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

#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



75
76
77
78
79
80
81
82
83
84
85
86
87
# 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

#reloadObject



96
97
98
99
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 96

def reload
  clear
  populate_page
end

#set_tab_text(text) ⇒ Object



111
112
113
# File 'lib/gtk2checkboxes/gtk2checkboxes.rb', line 111

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)


115
116
117
118
119
120
# 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