forked from crftwr/cfiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfiler_resultwindow.py
186 lines (148 loc) · 6.02 KB
/
cfiler_resultwindow.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import sys
import unicodedata
import ckit
from ckit.ckit_const import *
## @addtogroup resultwindow コンソールウインドウ機能
## @{
#--------------------------------------------------------------------
class ResultWindow( ckit.TextWindow ):
def __init__( self, x, y, width, height, parent_window, ini, title, keydown_hook=None ):
ckit.TextWindow.__init__(
self,
x=x,
y=y,
width=width,
height=height,
origin= ORIGIN_X_CENTER | ORIGIN_Y_CENTER,
parent_window=parent_window,
bg_color = ckit.getColor("bg"),
cursor0_color = ckit.getColor("cursor0"),
cursor1_color = ckit.getColor("cursor1"),
resizable = True,
title = title,
minimizebox = True,
maximizebox = True,
close_handler = self.onClose,
size_handler = self._onSize,
keydown_handler = self.onKeyDown,
)
self.lines = [""]
self.last_line_terminated = False
self.keydown_hook = keydown_hook
self.scroll_info = ckit.ScrollInfo()
self.scroll_info.makeVisible( 0, self.height() )
try:
self.wallpaper = ckit.Wallpaper(self)
self.wallpaper.copy( parent_window )
self.wallpaper.adjust()
except AttributeError:
self.wallpaper = None
self.paint()
def onClose(self):
self.quit()
def _onSize( self, width, height ):
if self.wallpaper:
self.wallpaper.adjust()
self.paint()
def numLines(self):
return len(self.lines)
def getLine(self,lineno):
return self.lines[lineno]
def write( self, s, paint=True ):
s = unicodedata.normalize( "NFC", s )
for line in s.splitlines(True):
if self.last_line_terminated:
if line.endswith("\n"):
self.lines.append(line.rstrip("\n"))
self.last_line_terminated = True
else:
self.lines.append(line)
self.last_line_terminated = False
else:
if line.endswith("\n"):
self.lines[-1] += line.rstrip("\n")
self.last_line_terminated = True
else:
self.lines[-1] += line
self.last_line_terminated = False
if len(self.lines)>1000:
self.lines = self.lines[-1000:]
if paint:
self.scroll_info.makeVisible( self.numLines()-1, self.height() )
self.paint()
def onKeyDown( self, vk, mod ):
if self.keydown_hook:
if self.keydown_hook( vk, mod ):
return True
if vk==VK_UP:
self.scroll_info.pos -= 1
if self.scroll_info.pos<0 : self.scroll_info.pos=0
self.paint()
elif vk==VK_DOWN:
self.scroll_info.pos += 1
if self.scroll_info.pos>self.numLines()-self.height() : self.scroll_info.pos=self.numLines()-self.height()
if self.scroll_info.pos<0 : self.scroll_info.pos=0
self.paint()
elif vk==VK_PRIOR or vk==VK_LEFT:
self.scroll_info.pos -= self.height()
if self.scroll_info.pos<0 : self.scroll_info.pos=0
self.paint()
elif vk==VK_NEXT or vk==VK_RIGHT:
self.scroll_info.pos += self.height()
if self.scroll_info.pos>self.numLines()-self.height() : self.scroll_info.pos=self.numLines()-self.height()
if self.scroll_info.pos<0 : self.scroll_info.pos=0
self.paint()
elif vk==VK_RETURN:
self.quit()
elif vk==VK_ESCAPE:
self.quit()
def paint(self):
y=0
width=self.width()
height=self.height()
client_rect = self.getClientRect()
char_w, char_h = self.getCharSize()
attribute_normal = ckit.Attribute( fg=ckit.getColor("fg"))
for i in range(height):
index = self.scroll_info.pos+i
self.putString( 0, y+i, width, 1, attribute_normal, " " * width )
if index < len(self.lines):
self.putString( 0, y+i, width, 1, attribute_normal, self.lines[index] )
## コンソールウインドウを表示する
#
# @param main_window MainWindowオブジェクト
# @param title コンソールウインドウのタイトルバーに表示する文字列
# @param message メッセージ文字列
# @param return_modkey 閉じたときのモディファイアキーの状態を取得するかどうか
# @return 引数 return_modkey が False の場合は結果値、引数 return_modkey が True の場合は ( 結果値, モディファイアキーの状態 ) を返す
#
# 返値の結果値としては、Enter が押されたときは True、ESC が押されたときは False が返ります。
#
def popResultWindow( main_window, title, message, return_modkey=False ):
result = [True]
result_mod = [0]
def onKeyDown( vk, mod ):
if vk==VK_RETURN and mod==0:
result[0] = True
result_mod[0] = mod
console_window.quit()
return True
elif vk==VK_ESCAPE and mod==0:
result[0] = False
result_mod[0] = mod
console_window.quit()
return True
pos = main_window.centerOfWindowInPixel()
console_window = ResultWindow( pos[0], pos[1], 60, 24, main_window, main_window.ini, title, onKeyDown )
console_window.write(message)
main_window.enable(False)
console_window.messageLoop()
main_window.enable(True)
main_window.activate()
console_window.destroy()
if return_modkey:
return result[0], result_mod[0]
else:
return result[0]
## @} resultwindow