forked from crftwr/ckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckit_theme.py
271 lines (194 loc) · 9.73 KB
/
ckit_theme.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
import os
import sys
import re
import struct
import configparser
from PIL import Image
from ckit import ckitcore
from ckit import ckit_misc
## @addtogroup theme テーマ機能
## @{
default_theme_name = "black"
theme_name = default_theme_name
ini = None
def _themePath(filename):
return os.path.join( ckit_misc.getAppExePath(), 'theme', theme_name, filename )
## テーマを設定する
#
def setTheme( name, default_color ):
global theme_name
global ini
theme_name = name
filename = _themePath("theme.ini")
ini = configparser.RawConfigParser()
ini.add_section("COLOR")
for k in default_color.keys():
ini.set( "COLOR", k, str(default_color[k]) )
ini.read(filename)
if not ini.has_option("COLOR","bg") : ini.set( "COLOR", "bg", "(0,0,0)" )
if not ini.has_option("COLOR","fg") : ini.set( "COLOR", "fg", "(255,255,255)" )
if not ini.has_option("COLOR","caret0") : ini.set( "COLOR", "caret0", "(255,255,255)" )
if not ini.has_option("COLOR","caret1") : ini.set( "COLOR", "caret1", "(255,0,0)" )
if not ini.has_option("COLOR","bar_fg") : ini.set( "COLOR", "bar_fg", "(255,255,255)" )
if not ini.has_option("COLOR","bar_error_fg") : ini.set( "COLOR", "bar_error_fg", "(255,130,130)" )
if not ini.has_option("COLOR","select_bg") : ini.set( "COLOR", "select_bg", "(130,130,130)" )
if not ini.has_option("COLOR","select_fg") : ini.set( "COLOR", "select_fg", "(255,255,255)" )
## テーマをデフォルトの設定にする
#
def setThemeDefault():
setTheme( default_theme_name, {} )
## テーマカラーを取得する
#
def getColor( name, section="COLOR" ):
try:
s = ini.get( section, name )
except:
print( "color not found :", name )
return (0,0,0)
match_result = re.match( r"[ ]*\([ ]*([0-9]+)[ ]*,[ ]*([0-9]+)[ ]*,[ ]*([0-9]+)[ ]*\)[ ]*", s )
if match_result:
r = int(match_result.group(1))
g = int(match_result.group(2))
b = int(match_result.group(3))
return (r,g,b)
## テーマ画像を取得する
#
def createThemeImage(filename):
try:
path = _themePath(filename)
pil_img = Image.open(path)
pil_img = pil_img.convert( "RGBA" )
return ckitcore.Image.fromBytes( pil_img.size, pil_img.tobytes(), (0,0,0) )
except:
print( "ERROR : image file cannot be loaded.", filename )
return ckitcore.Image.fromBytes( (1,1), struct.pack( "BBBB", 0x10, 0x10, 0x10, 0xff ) )
## テーマ画像を 3x3 に分割したものを取得する
#
def createThemeImage3x3(filename):
try:
path = _themePath(filename)
pil_img = Image.open(path)
pil_img = pil_img.convert( "RGBA" )
except IOError:
print( "ERROR : image file cannot be loaded.", filename )
img = ckitcore.Image.fromBytes( (1,1), struct.pack( "BBBB", 0x10, 0x10, 0x10, 0xff ) )
img = [ [img,img,img], [img,img,img], [img,img,img] ]
return img, 1, 1, 1, 1
img = [ [None,None,None], [None,None,None], [None,None,None] ]
frame_width = pil_img.size[0] // 3
frame_height = pil_img.size[1] // 3
center_width = pil_img.size[0]-frame_width*2
center_height = pil_img.size[1]-frame_height*2
def crop( y, x, rect ):
crop_img = pil_img.crop( rect )
img[y][x] = ckitcore.Image.fromBytes( crop_img.size, crop_img.tobytes(), (0,0,0) )
crop( 0, 0, ( 0, 0, frame_width, frame_height ) )
crop( 0, 1, ( frame_width, 0, frame_width+center_width, frame_height ) )
crop( 0, 2, ( frame_width+center_width, 0, frame_width*2+center_width, frame_height ) )
crop( 1, 0, ( 0, frame_height, frame_width, frame_height+center_height ) )
crop( 1, 1, ( frame_width, frame_height, frame_width+center_width, frame_height+center_height ) )
crop( 1, 2, ( frame_width+center_width, frame_height, frame_width*2+center_width, frame_height+center_height ) )
crop( 2, 0, ( 0, frame_height+center_height, frame_width, frame_height*2+center_height ) )
crop( 2, 1, ( frame_width, frame_height+center_height, frame_width+center_width, frame_height*2+center_height ) )
crop( 2, 2, ( frame_width+center_width, frame_height+center_height, frame_width*2+center_width, frame_height*2+center_height ) )
return img, frame_width, frame_height, center_width, center_height
## テーマ画像表示クラス
#
class ThemePlane:
def __init__( self, main_window, imgfile, priority=2 ):
img = createThemeImage(imgfile)
self.plane = ckitcore.ImagePlane( main_window, (0,0), (1,1), priority )
self.plane.setImage(img)
def destroy(self):
self.plane.destroy()
def setPosSize( self, x, y, width, height ):
self.plane.setPosition( (x,y) )
self.plane.setSize( (width,height) )
def setPosSizeByChar( self, main_window, x, y, width, height ):
client_rect = main_window.getClientRect()
offset_x, offset_y = main_window.charToClient( 0, 0 )
char_w, char_h = main_window.getCharSize()
px1 = x * char_w + offset_x
px2 = (x+width) * char_w + offset_x
py1 = y * char_h + offset_y
py2 = (y+height) * char_h + offset_y
if x==0 : px1=0
if y==0 : py1=0
if x+width==main_window.width() : px2=client_rect[2]
if y+height==main_window.height() : py2=client_rect[3]
self.setPosSize( px1, py1, px2-px1, py2-py1 )
def show( self, _show ):
self.plane.show( _show )
## 3x3に分割したテーマ画像表示クラス
#
class ThemePlane3x3:
def __init__( self, main_window, imgfile, priority=2 ):
self.img, self.frame_width, self.frame_height, self.center_width, self.center_height = createThemeImage3x3(imgfile)
self.plane = [ [None,None,None], [None,None,None], [None,None,None] ]
for y in (0,1,2):
for x in (0,1,2):
self.plane[y][x] = ckitcore.ImagePlane( main_window, (0,0), (1,1), priority )
self.plane[y][x].setImage(self.img[y][x])
def destroy(self):
self.plane[0][0].destroy()
self.plane[0][1].destroy()
self.plane[0][2].destroy()
self.plane[1][0].destroy()
self.plane[1][1].destroy()
self.plane[1][2].destroy()
self.plane[2][0].destroy()
self.plane[2][1].destroy()
self.plane[2][2].destroy()
def setPosSize( self, x, y, width, height ):
if width > self.frame_width*2+self.center_width:
plane_frame_width = self.frame_width
else:
plane_frame_width = width//3
plane_center_width = width-plane_frame_width*2
if height > self.frame_height*2+self.center_height:
plane_frame_height = self.frame_height
else:
plane_frame_height = height//3
plane_center_height = height-plane_frame_height*2
self.plane[0][0].setPosition( (x,y) )
self.plane[0][0].setSize( (plane_frame_width,plane_frame_height) )
self.plane[0][1].setPosition( (x+plane_frame_width,y) )
self.plane[0][1].setSize( (plane_center_width,plane_frame_height) )
self.plane[0][2].setPosition( (x+plane_frame_width+plane_center_width,y) )
self.plane[0][2].setSize( (plane_frame_width,plane_frame_height) )
self.plane[1][0].setPosition( (x,y+plane_frame_height) )
self.plane[1][0].setSize( (plane_frame_width,plane_center_height) )
self.plane[1][1].setPosition( (x+plane_frame_width,y+plane_frame_height) )
self.plane[1][1].setSize( (plane_center_width,plane_center_height) )
self.plane[1][2].setPosition( (x+plane_frame_width+plane_center_width,y+plane_frame_height) )
self.plane[1][2].setSize( (plane_frame_width,plane_center_height) )
self.plane[2][0].setPosition( (x,y+plane_frame_height+plane_center_height) )
self.plane[2][0].setSize( (plane_frame_width,plane_frame_height) )
self.plane[2][1].setPosition( (x+plane_frame_width,y+plane_frame_height+plane_center_height) )
self.plane[2][1].setSize( (plane_center_width,plane_frame_height) )
self.plane[2][2].setPosition( (x+plane_frame_width+plane_center_width,y+plane_frame_height+plane_center_height) )
self.plane[2][2].setSize( (plane_frame_width,plane_frame_height) )
def setPosSizeByChar( self, main_window, x, y, width, height ):
client_rect = main_window.getClientRect()
offset_x, offset_y = main_window.charToClient( 0, 0 )
char_w, char_h = main_window.getCharSize()
px1 = x * char_w + offset_x
px2 = (x+width) * char_w + offset_x
py1 = y * char_h + offset_y
py2 = (y+height) * char_h + offset_y
if x==0 : px1=0
if y==0 : py1=0
if x+width==main_window.width() : px2=client_rect[2]
if y+height==main_window.height() : py2=client_rect[3]
self.setPosSize( px1, py1, px2-px1, py2-py1 )
def show( self, _show ):
self.plane[0][0].show( _show )
self.plane[0][1].show( _show )
self.plane[0][2].show( _show )
self.plane[1][0].show( _show )
self.plane[1][1].show( _show )
self.plane[1][2].show( _show )
self.plane[2][0].show( _show )
self.plane[2][1].show( _show )
self.plane[2][2].show( _show )
## @} theme