Class: Dictionaries::GUI::Gtk::Dictionary

Inherits:
Gtk::Box
  • Object
show all
Includes:
Gtk::BaseModule
Defined in:
lib/dictionaries/gui/gtk3/dictionary/dictionary.rb

Overview

Dictionaries::GUI::Gtk::Dictionary

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
TITLE =
#

TITLE

#
'Dictionary'
WIDTH =
#

WIDTH

#
'25% or minimum 200px'
HEIGHT =
#

HEIGHT

#
'25% or minimum 200px'
USE_THIS_FONT =
#

USE_THIS_FONT

#
:dejavu_condensed_22
SMALLER_FONT =
#

SMALLER_FONT

This one should be relative to the font used above.

#
:dejavu_condensed_18

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commandline_arguments = ARGV, run_already = true) ⇒ Dictionary

#

initialize

#

62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 62

def initialize(
    commandline_arguments = ARGV,
    run_already           = true
  )
  super(:vertical)
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Class Method Details

.run(i = ARGV) ⇒ Object

#

Dictionaries::GUI::Gtk::Dictionary.run

Use this method if you wish to start a new gtk-application window.

#

427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 427

def self.run(
    i = ARGV
  )
  require 'gtk_paradise/app/app.rb'
  _ = ::Dictionaries::GUI::Gtk::Dictionary.new(i)
  r = ::Gtk.run
  r << _
  r.automatic_size_then_automatic_title
  r.enable_quick_exit
  r.border_width = 12
  r.top_left_then_run
end

Instance Method Details

#add_ask_the_question_buttonObject

#

add_ask_the_question_button

#

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 132

def add_ask_the_question_button
  button = bold_button('_Translate into german')
  button.clear_background
  button.bblack2
  button.hint =
    'Click this button in order to <b>translate the english '\
    'word into the german equivalent word</b>.'
  button.on_clicked {
    button_pressed_so_try_to_convert_the_input
  }
  @array_style_these_buttons << button
  hbox1 = gtk_hbox
  hbox1.minimal(button, 1)
  hbox1.halign_center
  minimal(hbox1, 5)
end

#add_the_answer_fieldObject

#

add_the_answer_field

#

396
397
398
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 396

def add_the_answer_field
  minimal(@answer_field) # ← Translation appears here.
end

#add_the_header_barObject

#

add_the_header_bar

#

382
383
384
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 382

def add_the_header_bar
  minimal(@header_bar, 1)
end

#add_the_statistics_frameObject

#

add_the_statistics_frame

#

389
390
391
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 389

def add_the_statistics_frame
  maximal(@frame_statistics, 2)
end

#add_the_widget_for_remote_query_of_the_dictionary_at_leoObject

#

add_the_widget_for_remote_query_of_the_dictionary_at_leo (bottom tag)

This is the bottom widget.

#

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
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 184

def add_the_widget_for_remote_query_of_the_dictionary_at_leo
  bottom_vbox = gtk_vbox
  bold_label = gtk_bold_label(
    'Query from a remote website instead (more entries available)'
  ) 
  bottom_vbox.minimal(bold_label, 15)
  # This is the user-input entry at the bottom.
  @entry_for_the_remote_query = gtk_entry
  @entry_for_the_remote_query.center
  @entry_for_the_remote_query.on_key_press_event { |widget, event|
    case Gdk::Keyval.to_name(event.keyval) 
    when 'Return', 'KP_Enter'
      do_perform_a_remote_query_to_the_leo_dictionary
    end
  }
  bottom_vbox.minimal(@entry_for_the_remote_query)
  # ======================================================================= #
  # === button_for_the_remote_query
  # ======================================================================= #
  button_for_the_remote_query = button(
    '_Query from a remote dictionary'
  )
  button_for_the_remote_query.clear_background
  button_for_the_remote_query.bblack2
  button_for_the_remote_query.make_bold
  button_for_the_remote_query.hint = 
    'This will <b>query the remote dictionary</b> at leo instead. '\
    'The <b>advantage</b> here is that the dataset at leo will '\
    'contain many more entries than does the knowledgebase '\
    'distributed with the <b>dictionaries gem</b>.'
  button_for_the_remote_query.on_clicked {
    do_perform_a_remote_query_to_the_leo_dictionary
  }
  bottom_vbox.minimal(
    @entry_at_the_bottom_showing_the_result_from_online_leo, 2
  )
  @array_style_these_buttons << button_for_the_remote_query
  small_hbox = gtk_hbox
  small_hbox.minimal(button_for_the_remote_query, 1)
  small_hbox.halign_center
  bottom_vbox.minimal(
    small_hbox, 2
  )
  maximal(bottom_vbox, 2)
end

#border_size?Boolean

#

border_size?

#

Returns:

  • (Boolean)

118
119
120
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 118

def border_size?
  2
end

#button_pressed_so_try_to_convert_the_inputObject

#

button_pressed_so_try_to_convert_the_input (click tag)

This action is called when the user clicks on the button.

#

169
170
171
172
173
174
175
176
177
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 169

def button_pressed_so_try_to_convert_the_input
  user_input = @input_field.return_input
  unless user_input.empty? # We check whether the user did input something.
    translated_word = @dictionaries.translate(user_input)
    e "#{::Colours.sfancy(user_input)}"\
      "#{::Colours.simp(translated_word)}"
    @answer_field.set_text(translated_word)
  end
end

#connect_skeletonObject

#

connect_skeleton (connect tag)

#

403
404
405
406
407
408
409
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 403

def connect_skeleton
  abort_on_exception
  add_the_answer_field
  add_ask_the_question_button
  add_the_widget_for_remote_query_of_the_dictionary_at_leo
  add_the_statistics_frame
end

#create_answer_fieldObject

#

create_answer_field

This method builds up the “answer-widget”.

#

154
155
156
157
158
159
160
161
162
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 154

def create_answer_field
  # ======================================================================= #
  # === @answer_field
  # ======================================================================= #
  @answer_field = gtk_input_field
  @answer_field.center
  @answer_field.hint = 'The translated string may appear here.'
  @answer_field.very_light_yellow_background
end

#create_input_field_on_topObject

#

create_input_field_on_top

This is the main widget for user input. The user types in some words, and then hits the main button on the bottom, in order to translate the (english) word to german.

#

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 286

def create_input_field_on_top
  text_for_the_label = 'Input an english word in the field below'
  text = gtk_label(text_for_the_label)
  text.slateblue
  text.set_markup(
    '<span size="x-large" weight="bold">'+text_for_the_label+'</span>',
    use_underline: true
  )
  # ======================================================================= #
  # Add the "input field" next for the english word that is to
  # be translated.
  # ======================================================================= #
  @input_field = gtk_input_field
  @input_field.hint = 'Input the word that should be translated '\
    'here. Then, press the "enter" key or the button '\
    '"Translate into german" below.'
  @input_field.very_light_yellow_background
  completion = gtk_entry_completion
  @input_field.completion = completion # Assign the completion.
  completion_model = gtk_list_store(String)
  @dictionaries.array.each { |word|
    iter = completion_model.append
    iter[0] = word
  }
  completion.model = completion_model
  # ======================================================================= #
  # Use model column 0 as the text column
  # ======================================================================= #
  completion.text_column = 0
  @input_field.center
  @input_field.on_key_press_event { |widget, event|
    case Gdk::Keyval.to_name(event.keyval) 
    when 'Return', 'KP_Enter'
      button_pressed_so_try_to_convert_the_input
    end
  }
  hbox_holding_label_and_input_field = gtk_vbox(
    text, @input_field
  )
  minimal(hbox_holding_label_and_input_field, 2)
end

#create_skeletonObject

#

create_skeleton (create tag, skeleton tag)

#

359
360
361
362
363
364
365
366
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 359

def create_skeleton
  create_the_entries
  create_the_header_bar
  add_the_header_bar
  create_input_field_on_top
  create_answer_field
  create_the_statistics_frame
end

#create_the_entriesObject

#

create_the_entries (entries tag, entry tag)

#

371
372
373
374
375
376
377
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 371

def create_the_entries
  # ======================================================================= #
  # === @entry_at_the_bottom_showing_the_result_from_online_leo
  # ======================================================================= #
  @entry_at_the_bottom_showing_the_result_from_online_leo = gtk_entry
  @entry_at_the_bottom_showing_the_result_from_online_leo.center
end

#create_the_header_barObject

#

create_the_header_bar

#

348
349
350
351
352
353
354
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 348

def create_the_header_bar
  # ======================================================================= #
  # === @header_bar
  # ======================================================================= #
  @header_bar = default_header_bar
  @header_bar.title = 'Dictionary'
end

#create_the_statistics_frameObject

#

create_the_statistics_frame

#

254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 254

def create_the_statistics_frame
  # ======================================================================= #
  # === @frame_statistics
  # ======================================================================= #
  @frame_statistics = gtk_frame(' Statistics ')
  @frame_statistics.make_bold
  @frame_statistics.use_this_font = smaller_font?
  vbox = gtk_vbox
  @label_currently_selected_file = gtk_label(return_currently_selected_file)
  vbox.minimal(
    @label_currently_selected_file, 0
  )
  @frame_statistics.add(vbox)
end

#do_perform_a_remote_query_to_the_leo_dictionary(i = @entry_for_the_remote_query.text.to_s) ⇒ Object

#

do_perform_a_remote_query_to_the_leo_dictionary

#

241
242
243
244
245
246
247
248
249
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 241

def do_perform_a_remote_query_to_the_leo_dictionary(
    i = @entry_for_the_remote_query.text.to_s
  )
  if i and !i.empty?
    array = ::Dictionaries.return_array_of_translated_words_from_online_leo(i)
    result = array.first
    @entry_at_the_bottom_showing_the_result_from_online_leo.set_text(result)
  end
end

#do_style_the_important_buttonsObject

#

do_style_the_important_buttons

#

414
415
416
417
418
419
420
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 414

def do_style_the_important_buttons
  @array_style_these_buttons.each {|this_button|
    this_button.modify_background(:normal,   :khaki)         # ← default colour
    this_button.modify_background(:prelight, :paleturquoise) # ← mouse-on-over events
    this_button.modify_background(:active,   :slategray)     # ← colour on mouse-press-event
  }
end

#e(i = '') ⇒ Object

#

e

#

125
126
127
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 125

def e(i = '')
  ::Dictionaries.e(i)
end

#handle_CSSObject

#

handle_CSS

#

104
105
106
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 104

def handle_CSS
  use_gtk_paradise_project_css_file
end

#padding?Boolean

#

padding?

#

Returns:

  • (Boolean)

111
112
113
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 111

def padding?
  2
end

#resetObject

#

reset (reset tag)

#

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 77

def reset
  reset_the_internal_variables
  # ======================================================================= #
  # === @configuration
  # ======================================================================= #
  @configuration = [true, __dir__, NAMESPACE]
  # ======================================================================= #
  # === Set the title, width, height and the font in use.
  # ======================================================================= #
  title_width_height_font(TITLE, WIDTH, HEIGHT, USE_THIS_FONT)
  infer_the_size_automatically
  # ======================================================================= #
  # === @dictionaries
  # ======================================================================= #
  @dictionaries = Dictionaries.new(:do_not_run_yet)
  Dictionaries.set_main_file(:default)
  @dictionaries.load_the_english_file
  # ======================================================================= #
  # === @array_style_these_buttons
  # ======================================================================= #
  @array_style_these_buttons = []
  handle_CSS
end

#return_currently_selected_fileObject

#

return_currently_selected_file

#

272
273
274
275
276
277
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 272

def return_currently_selected_file
  'Currently selected file: <b>'+
  File.basename(
    @dictionaries.currently_selected_file?.to_s
  )+'</b>'
end

#runObject

#

run (run tag)

#

331
332
333
334
335
336
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 331

def run
  super()
  update_the_statistics_frame
  do_style_the_important_buttons
  show_all
end

#smaller_font?Boolean

#

smaller_font?

#

Returns:

  • (Boolean)

341
342
343
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 341

def smaller_font?
  SMALLER_FONT
end

#update_the_statistics_frame(i = return_currently_selected_file) ⇒ Object

#

update_the_statistics_frame (update tag)

#

233
234
235
236
# File 'lib/dictionaries/gui/gtk3/dictionary/dictionary.rb', line 233

def update_the_statistics_frame(i = return_currently_selected_file)
  @label_currently_selected_file.set_text(i)
  @label_currently_selected_file.do_markify
end