Class: TkNamespace

Inherits:
TkObject show all
Extended by:
Tk
Defined in:
lib/tk/namespace.rb

Defined Under Namespace

Classes: Ensemble, NsCode, ScopeArgs

Constant Summary collapse

TkCommandNames =
[
  'namespace'.freeze,
].freeze
Tk_Namespace_ID_TBL =
TkCore::INTERP.create_table
Tk_NsCode_RetObjID_TBL =
TkCore::INTERP.create_table

Constants included from Tk

Tk::AUTO_PATH, Tk::INTERP, Tk::JAPANIZED_TK, Tk::LIBRARY_PATH, Tk::MAINLOOP, Tk::PACKAGE_PATH, Tk::RELEASE_DATE, Tk::TCL_LIBRARY_PATH, Tk::TCL_MAJOR_VERSION, Tk::TCL_MINOR_VERSION, Tk::TCL_PACKAGE_PATH, Tk::TCL_PATCHLEVEL, Tk::TCL_PRECISION, Tk::TCL_VERSION, Tk::TK_MAJOR_VERSION, Tk::TK_MINOR_VERSION, Tk::TK_PATCHLEVEL, Tk::TK_VERSION, Tk::Tkextlib_RELEASE_DATE, Tk::TreeCtrl_Widget, Tk::X_Scrollable, Tk::Y_Scrollable

Constants included from Tk::Encoding

Tk::Encoding::BINARY, Tk::Encoding::BINARY_NAME, Tk::Encoding::DEFAULT_EXTERNAL_NAME, Tk::Encoding::DEFAULT_INTERNAL_NAME, Tk::Encoding::ENCODING_TABLE, Tk::Encoding::RubyEncoding, Tk::Encoding::UNKNOWN, Tk::Encoding::UTF8_NAME

Constants included from TkCore

TkCore::EventFlag, TkCore::INTERP, TkCore::INTERP_MUTEX, TkCore::INTERP_ROOT_CHECK, TkCore::INTERP_THREAD, TkCore::INTERP_THREAD_STATUS, TkCore::RUN_EVENTLOOP_ON_MAIN_THREAD, TkCore::WIDGET_DESTROY_HOOK, TkCore::WITH_ENCODING, TkCore::WITH_RUBY_VM

Constants included from TkComm

TkComm::GET_CONFIGINFO_AS_ARRAY, TkComm::GET_CONFIGINFOwoRES_AS_ARRAY, TkComm::TkExtlibAutoloadModule, TkComm::Tk_CMDTBL, TkComm::Tk_IDs, TkComm::Tk_WINDOWS, TkComm::USE_TCLs_LIST_FUNCTIONS, TkComm::WidgetClassNames

Constants included from TkUtil

TkUtil::None, TkUtil::RELEASE_DATE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tk

BinaryString, EncodedString, Grid, Pack, Place, UTF8_String, __create_widget_set__, __disable_toplevel_control__, __import_toplevel_aliases__, __regist_toplevel_aliases__, __remove_toplevel_aliases__, __reset_toplevel_owner__, __set_loaded_toplevel_aliases__, __set_toplevel_aliases__, __toplevel_alias_setup_proc__, add_kinsoku, bell, bell_on_display, const_missing, current_grabs, cursor_display, default_widget_set, default_widget_set=, delete_kinsoku, destroy, encoding, encoding=, errorCode, errorInfo, exit, focus, focus_lastfor, focus_next, focus_prev, focus_to, fromUTF8, grid, grid_forget, has_mainwindow?, load_tcllibrary, load_tclscript, load_tclscript_rsrc, load_tclscript_rsrcid, lower_window, pack, pack_forget, pkgconfig_get, pkgconfig_list, place, place_forget, raise_window, root, show_kinsoku, sleep, strictMotif, subst_tk_backslash, subst_utf_backslash, tcl_pkgconfig_get, tcl_pkgconfig_list, thread_update, thread_update_idletasks, tk_pkgconfig_get, tk_pkgconfig_list, toUTF8, to_backslash_sequence, toplevel_aliases_on_widget_set, ungrid, unload_tcllibrary, unpack, unplace, update, update, update_idletasks, utf_to_backslash, utf_to_backslash_sequence, wakeup, widget_set_symbols

Methods included from Tk::Encoding

#default_encoding=, #encoding=, #encoding_convertfrom, #encoding_convertto, #encoding_dirs, #encoding_dirs=, #encoding_name, #encoding_names, #encoding_obj, #encoding_objs, #encoding_system=, #encoding_system_name, #encoding_system_obj, #force_default_encoding, #force_default_encoding=, #force_default_encoding?, #tk_encoding_names

Methods included from TkCore

#_tk_call_to_list_core, #after, #after_cancel, #after_idle, #appname, #appsend, #appsend_deny, #appsend_displayof, callback, #callback_break, #callback_continue, #callback_return, #chooseColor, #chooseDirectory, #do_one_event, #event_generate, #getMultipleOpenFile, #getMultipleSaveFile, #getOpenFile, #getSaveFile, #get_eventloop_tick, #get_eventloop_weight, #get_no_event_wait, #inactive, #inactive_displayof, #info, #ip_eval, #ip_eval_with_enc, #ip_eval_without_enc, #ip_invoke, #ip_invoke_with_enc, #ip_invoke_without_enc, #is_mainloop?, #load_cmd_on_ip, #mainloop, #mainloop_exist?, #mainloop_thread?, #mainloop_watchdog, #messageBox, #rb_appsend, #rb_appsend_displayof, #reset_inactive, #reset_inactive_displayof, #restart, #scaling, #scaling_displayof, #set_eventloop_tick, #set_eventloop_weight, #set_no_event_wait, #tk_call_to_list, #tk_call_to_list_with_enc, #tk_call_to_list_without_enc, #tk_call_to_simplelist, #tk_call_to_simplelist_with_enc, #tk_call_to_simplelist_without_enc, #windowingsystem

Methods included from TkComm

_at, _callback_entry?, _callback_entry_class?, _curr_cmd_id, _fromUTF8, _genobj_for_tkwidget, _next_cmd_id, _toUTF8, array2tk_list, #bind, #bind_all, #bind_append, #bind_append_all, #bind_remove, #bind_remove_all, #bindinfo, #bindinfo_all, bool, image_obj, install_cmd, list, num_or_nil, num_or_str, number, procedure, simplelist, slice_ary, string, #subst, tk_tcl2ruby, uninstall_cmd, #uninstall_cmd, window

Methods included from TkEvent

#install_bind, #install_bind_for_event_class

Methods included from TkUtil

#_conv_args, _conv_args, #_fromUTF8, #_get_eval_enc_str, _get_eval_enc_str, #_get_eval_string, _get_eval_string, _symbolkey2str, #_symbolkey2str, #_toUTF8, #bool, bool, callback, eval_cmd, #hash_kv, hash_kv, install_cmd, #num_or_nil, num_or_nil, num_or_str, #num_or_str, number, #number, string, #string, uninstall_cmd, untrust

Methods inherited from TkObject

#destroy, #epath, #event_generate, #method_missing, #path, #tk_send, #tk_send_to_list, #tk_send_to_list_with_enc, #tk_send_to_list_without_enc, #tk_send_to_simplelist, #tk_send_to_simplelist_with_enc, #tk_send_to_simplelist_without_enc, #tk_send_with_enc, #tk_send_without_enc, #to_eval

Methods included from TkBindCore

#bind, #bind_append, #bind_remove, #bindinfo

Methods included from TkConfigMethod

#[], #[]=, __IGNORE_UNKNOWN_CONFIGURE_OPTION__, #__check_available_configure_options, __set_IGNORE_UNKNOWN_CONFIGURE_OPTION__!, #cget, #cget_strict, #cget_tkstring, #config_hash_kv, #configinfo, #configure, #configure_cmd, #current_configinfo

Methods included from TkTreatFont

#font_configinfo, #font_configure, #font_copy, #kanjifont_configure, #kanjifont_copy, #latinfont_configure, #latinfont_copy

Methods inherited from TkKernel

new

Constructor Details

#initialize(name = nil, parent = nil) ⇒ TkNamespace

Returns a new instance of TkNamespace.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/tk/namespace.rb', line 245

def initialize(name = nil, parent = nil)
  unless name
    Tk_Namespace_ID.mutex.synchronize{
      # name = Tk_Namespace_ID.join('')
      name = Tk_Namespace_ID.join(TkCore::INTERP._ip_id_)
      Tk_Namespace_ID[1].succ!
    }
  end
  name = __tk_call('namespace', 'current') if name == ''
  if parent
    if parent =~ /^::/
      if name =~ /^::/
        @fullname = parent + name
      else
        @fullname = parent +'::'+ name
      end
    else
      ancestor = __tk_call('namespace', 'current')
      ancestor = '' if ancestor == '::'
      if name =~ /^::/
        @fullname = ancestor + '::' + parent + name
      else
        @fullname = ancestor + '::'+ parent +'::'+ name
      end
    end
  else # parent == nil
    ancestor = __tk_call('namespace', 'current')
    ancestor = '' if ancestor == '::'
    if name =~ /^::/
      @fullname = name
    else
      @fullname = ancestor + '::' + name
    end
  end
  @path = @fullname
  @parent = __tk_call('namespace', 'qualifiers', @fullname)
  @name = __tk_call('namespace', 'tail', @fullname)

  # create namespace
  __tk_call('namespace', 'eval', @fullname, '')

  Tk_Namespace_ID_TBL.mutex.synchronize{
    Tk_Namespace_ID_TBL[@fullname] = self
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class TkObject

Class Method Details

.children(*args) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/tk/namespace.rb', line 291

def self.children(*args)
  # args ::= [<namespace>] [<pattern>]
  # <pattern> must be glob-style pattern
  tk_split_simplelist(tk_call('namespace', 'children', *args)).collect{|ns|
    # ns is fullname
    Tk_Namespace_ID_TBL.mutex.synchronize{
      if Tk_Namespace_ID_TBL.key?(ns)
        Tk_Namespace_ID_TBL[ns]
      else
        ns
      end
    }
  }
end

.code(script = Proc.new) ⇒ Object



309
310
311
# File 'lib/tk/namespace.rb', line 309

def self.code(script = Proc.new)
  TkNamespace.new('').code(script)
end

.currentObject



360
361
362
363
364
365
366
367
368
369
# File 'lib/tk/namespace.rb', line 360

def self.current
  ns = self.current_path
  Tk_Namespace_ID_TBL.mutex.synchronize{
    if Tk_Namespace_ID_TBL.key?(ns)
      Tk_Namespace_ID_TBL[ns]
    else
      ns
    end
  }
end

.current_pathObject



353
354
355
# File 'lib/tk/namespace.rb', line 353

def self.current_path
  tk_call('namespace', 'current')
end

.delete(*ns_list) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/tk/namespace.rb', line 377

def self.delete(*ns_list)
  tk_call('namespace', 'delete', *ns_list)
  ns_list.each{|ns|
    Tk_Namespace_ID_TBL.mutex.synchronize{
      if ns.kind_of?(TkNamespace)
        Tk_Namespace_ID_TBL.delete(ns.path)
      else
        Tk_Namespace_ID_TBL.delete(ns.to_s)
      end
    }
  }
end

.ensemble_configinfo(cmd, slot = nil) ⇒ Object



403
404
405
406
407
408
409
410
411
# File 'lib/tk/namespace.rb', line 403

def self.ensemble_configinfo(cmd, slot = nil)
  if slot
    tk_call('namespace', 'ensemble', 'configure', cmd, '-' + slot.to_s)
  else
    inf = {}
    Hash(*tk_split_simplelist(tk_call('namespace', 'ensemble', 'configure', cmd))).each{|k, v| inf[k[1..-1]] = v}
    inf
  end
end

.ensemble_configure(cmd, slot, value = None) ⇒ Object



396
397
398
399
400
401
402
# File 'lib/tk/namespace.rb', line 396

def self.ensemble_configure(cmd, slot, value=None)
  if slot.kind_of?(Hash)
    tk_call('namespace', 'ensemble', 'configure', cmd, *hash_kv(slot))
  else
    tk_call('namespace', 'ensemble', 'configure', cmd, '-'+slot.to_s, value)
  end
end

.ensemble_create(*keys) ⇒ Object



393
394
395
# File 'lib/tk/namespace.rb', line 393

def self.ensemble_create(*keys)
  tk_call('namespace', 'ensemble', 'create', *hash_kv(keys))
end

.ensemble_exist?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


412
413
414
# File 'lib/tk/namespace.rb', line 412

def self.ensemble_exist?(cmd)
  bool(tk_call('namespace', 'ensemble', 'exists', cmd))
end

.eval(namespace, cmd = Proc.new, *args) ⇒ Object



416
417
418
419
# File 'lib/tk/namespace.rb', line 416

def self.eval(namespace, cmd = Proc.new, *args)
  #tk_call('namespace', 'eval', namespace, cmd, *args)
  TkNamespace.new(namespace).eval(cmd, *args)
end

.exist?(ns) ⇒ Boolean

Returns:

  • (Boolean)


438
439
440
# File 'lib/tk/namespace.rb', line 438

def self.exist?(ns)
  bool(tk_call('namespace', 'exists', ns))
end

.export(*patterns) ⇒ Object



445
446
447
# File 'lib/tk/namespace.rb', line 445

def self.export(*patterns)
  tk_call('namespace', 'export', *patterns)
end

.export_with_clear(*patterns) ⇒ Object



448
449
450
# File 'lib/tk/namespace.rb', line 448

def self.export_with_clear(*patterns)
  tk_call('namespace', 'export', '-clear', *patterns)
end

.force_import(*patterns) ⇒ Object



468
469
470
# File 'lib/tk/namespace.rb', line 468

def self.force_import(*patterns)
  tk_call('namespace', 'import', '-force', *patterns)
end

.forget(*patterns) ⇒ Object



458
459
460
# File 'lib/tk/namespace.rb', line 458

def self.forget(*patterns)
  tk_call('namespace', 'forget', *patterns)
end

.get_pathObject



503
504
505
# File 'lib/tk/namespace.rb', line 503

def self.get_path
  tk_call('namespace', 'path')
end

.get_unknown_handlerObject



528
529
530
# File 'lib/tk/namespace.rb', line 528

def self.get_unknown_handler
  tk_tcl2ruby(tk_call('namespace', 'unknown'))
end

.id2obj(id) ⇒ Object



29
30
31
32
33
# File 'lib/tk/namespace.rb', line 29

def TkNamespace.id2obj(id)
  Tk_Namespace_ID_TBL.mutex.synchronize{
    Tk_Namespace_ID_TBL[id]? Tk_Namespace_ID_TBL[id]: id
  }
end

.import(*patterns) ⇒ Object



465
466
467
# File 'lib/tk/namespace.rb', line 465

def self.import(*patterns)
  tk_call('namespace', 'import', *patterns)
end

.inscope(namespace, script, *args) ⇒ Object



478
479
480
# File 'lib/tk/namespace.rb', line 478

def self.inscope(namespace, script, *args)
  tk_call('namespace', 'inscope', namespace, script, *args)
end

.origin(cmd) ⇒ Object



485
486
487
# File 'lib/tk/namespace.rb', line 485

def self.origin(cmd)
  tk_call('namespace', 'origin', cmd)
end

.parent(namespace = None) ⇒ Object



489
490
491
492
493
494
495
496
497
498
# File 'lib/tk/namespace.rb', line 489

def self.parent(namespace=None)
  ns = tk_call('namespace', 'parent', namespace)
  Tk_Namespace_ID_TBL.mutex.synchronize{
    if Tk_Namespace_ID_TBL.key?(ns)
      Tk_Namespace_ID_TBL[ns]
    else
      ns
    end
  }
end

.qualifiers(str) ⇒ Object



513
514
515
# File 'lib/tk/namespace.rb', line 513

def self.qualifiers(str)
  tk_call('namespace', 'qualifiers', str)
end

.set_path(*namespace_list) ⇒ Object



506
507
508
# File 'lib/tk/namespace.rb', line 506

def self.set_path(*namespace_list)
  tk_call('namespace', 'path', array2tk_list(namespace_list))
end

.set_unknown_handler(cmd = Proc.new) ⇒ Object



531
532
533
# File 'lib/tk/namespace.rb', line 531

def self.set_unknown_handler(cmd = Proc.new)
  tk_call('namespace', 'unknown', cmd)
end

.tail(str) ⇒ Object



517
518
519
# File 'lib/tk/namespace.rb', line 517

def self.tail(str)
  tk_call('namespace', 'tail', str)
end

.upvar(namespace, *var_pairs) ⇒ Object



521
522
523
# File 'lib/tk/namespace.rb', line 521

def self.upvar(namespace, *var_pairs)
  tk_call('namespace', 'upvar', namespace, *(var_pairs.flatten))
end

.which(name) ⇒ Object



535
536
537
# File 'lib/tk/namespace.rb', line 535

def self.which(name)
  tk_call('namespace', 'which', name)
end

.which_command(name) ⇒ Object



538
539
540
# File 'lib/tk/namespace.rb', line 538

def self.which_command(name)
  tk_call('namespace', 'which', '-command', name)
end

.which_variable(name) ⇒ Object



541
542
543
# File 'lib/tk/namespace.rb', line 541

def self.which_variable(name)
  tk_call('namespace', 'which', '-variable', name)
end

Instance Method Details

#__tk_callObject



220
# File 'lib/tk/namespace.rb', line 220

alias __tk_call             tk_call

#__tk_call_with_encObject



222
# File 'lib/tk/namespace.rb', line 222

alias __tk_call_with_enc    tk_call_with_enc

#__tk_call_without_encObject



221
# File 'lib/tk/namespace.rb', line 221

alias __tk_call_without_enc tk_call_without_enc

#children(pattern = None) ⇒ Object



305
306
307
# File 'lib/tk/namespace.rb', line 305

def children(pattern=None)
  TkNamespace.children(@fullname, pattern)
end

#code(script = Proc.new) ⇒ Object

def code(script = Proc.new)

  if script.kind_of?(String)
    cmd = proc{|*args| ScopeArgs.new(@fullname,*args).instance_eval(script)}
  elsif script.kind_of?(Proc)
    cmd = proc{|*args| ScopeArgs.new(@fullname,*args).instance_eval(&script)}
  else
    fail ArgumentError, "String or Proc is expected"
  end
  TkNamespace::NsCode.new(tk_call_without_enc('namespace', 'code',
                                              _get_eval_string(cmd, false)))
end


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/tk/namespace.rb', line 325

def code(script = Proc.new)
  if script.kind_of?(String)
    cmd = proc{|*args|
      ret = ScopeArgs.new(@fullname,*args).instance_eval(script)
      id = ret.object_id
      TkNamespace::Tk_NsCode_RetObjID_TBL[id] = ret
      id
    }
  elsif script.kind_of?(Proc)
    cmd = proc{|*args|
      if TkCore::WITH_RUBY_VM  ### Ruby 1.9 !!!!
        obj = ScopeArgs.new(@fullname,*args)
        ret = obj.instance_exec(obj, &script)
      else
        ret = ScopeArgs.new(@fullname,*args).instance_eval(&script)
      end
      id = ret.object_id
      TkNamespace::Tk_NsCode_RetObjID_TBL[id] = ret
      id
    }
  else
    fail ArgumentError, "String or Proc is expected"
  end
  TkNamespace::NsCode.new(tk_call_without_enc('namespace', 'code',
                                              _get_eval_string(cmd, false)),
                          true)
end

#current_namespaceObject Also known as: current



370
371
372
373
374
# File 'lib/tk/namespace.rb', line 370

def current_namespace
  # ns_tk_call('namespace', 'current')
  # @fullname
  self
end

#current_pathObject



356
357
358
# File 'lib/tk/namespace.rb', line 356

def current_path
  @fullname
end

#deleteObject



389
390
391
# File 'lib/tk/namespace.rb', line 389

def delete
  TkNamespece.delete(@fullname)
end

#eval(cmd = Proc.new, *args) ⇒ Object

def eval(cmd = Proc.new, *args)

  #TkNamespace.eval(@fullname, cmd, *args)
  #ns_tk_call(cmd, *args)
  code_obj = code(cmd)
  ret = code_obj.call(*args)
  # uninstall_cmd(TkCore::INTERP._split_tklist(code_obj.path)[-1])
  uninstall_cmd(_fromUTF8(TkCore::INTERP._split_tklist(_toUTF8(code_obj.path))[-1]))
  tk_tcl2ruby(ret)
end


431
432
433
434
435
436
# File 'lib/tk/namespace.rb', line 431

def eval(cmd = Proc.new, *args)
  code_obj = code(cmd)
  ret = code_obj.call(*args)
  uninstall_cmd(_fromUTF8(TkCore::INTERP._split_tklist(_toUTF8(code_obj.path))[-1]))
  ret
end

#exist?Boolean

Returns:

  • (Boolean)


441
442
443
# File 'lib/tk/namespace.rb', line 441

def exist?
  TkNamespece.exist?(@fullname)
end

#exportObject



451
452
453
# File 'lib/tk/namespace.rb', line 451

def export
  TkNamespace.export(@fullname)
end

#export_with_clearObject



454
455
456
# File 'lib/tk/namespace.rb', line 454

def export_with_clear
  TkNamespace.export_with_clear(@fullname)
end

#force_importObject



474
475
476
# File 'lib/tk/namespace.rb', line 474

def force_import
  TkNamespace.force_import(@fullname)
end

#forgetObject



461
462
463
# File 'lib/tk/namespace.rb', line 461

def forget
  TkNamespace.forget(@fullname)
end

#importObject



471
472
473
# File 'lib/tk/namespace.rb', line 471

def import
  TkNamespace.import(@fullname)
end

#inscope(script, *args) ⇒ Object



481
482
483
# File 'lib/tk/namespace.rb', line 481

def inscope(script, *args)
  TkNamespace.inscope(@fullname, script, *args)
end

#install_cmd(cmd) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/tk/namespace.rb', line 210

def install_cmd(cmd)
  lst = tk_split_simplelist(super(cmd), false, false)
  if lst[1] =~ /^::/
    lst[1] = @fullname
  else
    lst.insert(1, @fullname)
  end
  TkCore::INTERP._merge_tklist(*lst)
end

#parentObject



499
500
501
# File 'lib/tk/namespace.rb', line 499

def parent
  tk_call('namespace', 'parent', @fullname)
end

#set_pathObject



509
510
511
# File 'lib/tk/namespace.rb', line 509

def set_path
  tk_call('namespace', 'path', @fullname)
end

#tk_call(*args) ⇒ Object Also known as: ns_tk_call



223
224
225
226
227
228
# File 'lib/tk/namespace.rb', line 223

def tk_call(*args)
  #super('namespace', 'eval', @fullname, *args)
  args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
  super('namespace', 'eval', @fullname,
        TkCore::INTERP._merge_tklist(*args))
end

#tk_call_with_enc(*args) ⇒ Object Also known as: ns_tk_call_with_enc



235
236
237
238
239
240
# File 'lib/tk/namespace.rb', line 235

def tk_call_with_enc(*args)
  #super('namespace', 'eval', @fullname, *args)
  args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
  super('namespace', 'eval', @fullname,
        TkCore::INTERP._merge_tklist(*args))
end

#tk_call_without_enc(*args) ⇒ Object Also known as: ns_tk_call_without_enc



229
230
231
232
233
234
# File 'lib/tk/namespace.rb', line 229

def tk_call_without_enc(*args)
  #super('namespace', 'eval', @fullname, *args)
  args = args.collect{|arg| (s = _get_eval_string(arg, true))? s: ''}
  super('namespace', 'eval', @fullname,
        TkCore::INTERP._merge_tklist(*args))
end

#upvar(*var_pairs) ⇒ Object



524
525
526
# File 'lib/tk/namespace.rb', line 524

def upvar(*var_pairs)
  TkNamespace.inscope(@fullname, *(var_pairs.flatten))
end