-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtkcommon.rb
More file actions
139 lines (118 loc) · 3.36 KB
/
tkcommon.rb
File metadata and controls
139 lines (118 loc) · 3.36 KB
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
=begin
Common, reusable methods for Ruby Tk / vTcl development
Author: Gunbard
=end
# Finds and returns a widget
# @param window The window to look in
# @param str The path string for the widget
# @returns A widget
def wpath(window, str)
depth = str.count('.')
if depth == 2
window.winfo_children.each do |some_widget|
if some_widget.path == str
return some_widget
end
end
elsif depth == 3
window.winfo_children.each do |some_widget|
some_widget.winfo_children.each do |some_inner_widget|
if some_inner_widget.path == str
return some_inner_widget
end
end
end
end
end
# Shows a standard, blocking, modal info box with ok button
# @param title The title of the dialog
# @param msg The display message
# @param window The window this is attached to
def show_msg(title, msg, window)
if window
Tk.messageBox ({
:type => 'ok',
:icon => 'info',
:title => title,
:message => msg,
:parent => window
})
else
# Make box free-floating!
Tk.messageBox ({
:type => 'ok',
:icon => 'info',
:title => title,
:message => msg
})
end
end
# Creates and shows a non-modal, non-blocking message window with OK button
# @param title The window title
# @param msg The message to show
# @param window The window to center in or nil to ceter on screen
# @param root Tk root, used for screen centering
# @param command Command block for the OK button
# @returns The dialog window
def show_dialog(title, msg, window, root, command)
window_width = 360
window_height = 180
dialog = TkToplevel.new ({
:width => window_width,
:height => window_height,
:title => title
})
dialog['minsize'] = window_width, window_height
dialog['resizable'] = false, false
msg_label = TkLabel.new(dialog, {
:text => msg
})
unless command
dialog.destroy
end
ok_button = TkButton.new(dialog, {
:text => 'OK',
:width => 10,
:height => 2,
:command => command
})
msg_label.pack(:padx => 20, :pady => 20, :side => 'top')
ok_button.pack(:padx => 20, :pady => 20, :side => 'bottom')
dialog.update
dialog.focus
center_window(dialog, window, root)
dialog
end
# Centers a window
# @param window The window to center
# @param parent The window to center in, or the screen if nil
# @param root Tk root, used for screen centering
def center_window(window, parent, root)
window_width = window.winfo_width
window_height = window.winfo_height
screen_width = root.winfo_screenwidth
screen_height = root.winfo_screenheight
screen_Xorigin = 0
screen_Yorigin = 0
if parent
screen_width = parent.winfo_width
screen_height = parent.winfo_height
screen_Xorigin = parent.winfo_x
screen_Yorigin = parent.winfo_y
end
center_x = (screen_width / 2) - (window_width / 2)
center_y = (screen_height / 2) - (window_height / 2)
window.geometry("+#{screen_Xorigin + center_x}+#{screen_Yorigin + center_y}")
end
# Determines current operating system
# @returns OS string or 'unknown'
def current_os
if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
return 'windows'
elsif (/darwin/ =~ RUBY_PLATFORM) != nil
return 'osx'
elsif (/linux|bsd/ =~ RUBY_PLATFORM) != nil
return 'linux'
end
'unknown'
end