summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tk/tk_mac.rb
blob: ed90fd1c677698deade5f6d5e7da355b04552d81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: false
#
# tk/tk_mac.rb : Access Mac-Specific functionality on OS X from Tk
#                (supported by Tk8.6 or later)
#
#     ATTENTION !!
#         This is NOT TESTED. Because I have no test-environment.
#
require 'tk'

module Tk
  module Mac
  end
end

module Tk::Mac
  extend TkCore

  # event handler callbacks
  def self.def_ShowPreferences(cmd=Proc.new)
    ip_eval("proc ::tk::mac::ShowPreferences {} { #{install_cmd(cmd)} }")
    nil
  end

  def self.def_OpenApplication(cmd=Proc.new)
    ip_eval("proc ::tk::mac::OpenApplication {} { #{install_cmd(cmd)} }")
    nil
  end

  def self.def_ReopenApplication(cmd=Proc.new)
    ip_eval("proc ::tk::mac::ReopenApplication {} { #{install_cmd(cmd)} }")
    nil
  end

  def self.def_OpenDocument(cmd=Proc.new)
    ip_eval("proc ::tk::mac::OpenDocument {args} { eval #{install_cmd(cmd)} $args }")
    nil
  end

  def self.def_PrintDocument(cmd=Proc.new)
    ip_eval("proc ::tk::mac::PrintDocument {args} { eval #{install_cmd(cmd)} $args }")
    nil
  end

  def self.def_Quit(cmd=Proc.new)
    ip_eval("proc ::tk::mac::Quit {} { #{install_cmd(cmd)} }")
    nil
  end

  def self.def_OnHide(cmd=Proc.new)
    ip_eval("proc ::tk::mac::OnHide {} { #{install_cmd(cmd)} }")
    nil
  end

  def self.def_OnShow(cmd=Proc.new)
    ip_eval("proc ::tk::mac::OnShow {} { #{install_cmd(cmd)} }")
    nil
  end

  def self.def_ShowHelp(cmd=Proc.new)
    ip_eval("proc ::tk::mac::ShowHelp {} { #{install_cmd(cmd)} }")
    nil
  end


  # additional dialogs
  def self.standardAboutPanel
    tk_call('::tk::mac::standardAboutPanel')
    nil
  end


  # system configuration
  def self.useCompatibilityMetrics(mode)
    tk_call('::tk::mac::useCompatibilityMetrics', mode)
    nil
  end

  def self.CGAntialiasLimit(limit)
    tk_call('::tk::mac::CGAntialiasLimit', limit)
    nil
  end

  def self.antialiasedtext(num)
    tk_call('::tk::mac::antialiasedtext', num)
    nil
  end

  def self.useThemedToplevel(mode)
    tk_call('::tk::mac::useThemedToplevel', mode)
    nil
  end

end

class Tk::Mac::IconBitmap < TkImage
  TkCommandNames = ['::tk::mac::iconBitmap'].freeze

  def self.new(width, height, keys)
    if keys.kind_of?(Hash)
      name = nil
      if keys.key?(:imagename)
        name = keys[:imagename]
      elsif keys.key?('imagename')
        name = keys['imagename']
      end
      if name
        if name.kind_of?(TkImage)
          obj = name
        else
          name = _get_eval_string(name)
          obj = nil
          Tk_IMGTBL.mutex.synchronize{
            obj = Tk_IMGTBL[name]
          }
        end
        if obj
          if !(keys[:without_creating] || keys['without_creating'])
            keys = _symbolkey2str(keys)
            keys.delete('imagename')
            keys.delete('without_creating')
            obj.instance_eval{
              tk_call_without_enc('::tk::mac::iconBitmap',
                                  @path, width, height, *hash_kv(keys, true))
            }
          end
          return obj
        end
      end
    end
    (obj = self.allocate).instance_eval{
      Tk_IMGTBL.mutex.synchronize{
        initialize(width, height, keys)
        Tk_IMGTBL[@path] = self
      }
    }
    obj
  end

  def initialize(width, height, keys)
    @path = nil
    without_creating = false
    if keys.kind_of?(Hash)
      keys = _symbolkey2str(keys)
      @path = keys.delete('imagename')
      without_creating = keys.delete('without_creating')
    end
    unless @path
      Tk_Image_ID.mutex.synchronize{
        @path = Tk_Image_ID.join(TkCore::INTERP._ip_id_)
        Tk_Image_ID[1].succ!
      }
    end
    unless without_creating
      tk_call_without_enc('::tk::mac::iconBitmap',
                          @path, width, height, *hash_kv(keys, true))
    end
  end
end