3 # -*- coding: utf-8 -*-
5 # Project: J-Ben, Python front-end
6 # File: widget_hwpad.py
8 # Created on: 25 Nov 2008
10 # I think I originally wrote this to be a generic writing pad object,
11 # but currently it is linked specifically into kanji handwriting
16 from subprocess
import Popen
, PIPE
18 from .infomessage
import show_message
19 from jben
import configure
23 def __init__(self
, init_x
, init_y
):
27 """kpengine expects a very simple format."""
28 return "%s %s" % (self
.x
, self
.y
)
30 class WidgetHWPad(gtk
.DrawingArea
):
32 gtk
.DrawingArea
.__init
__(self
)
35 gtk
.gdk
.EXPOSURE_MASK |
36 gtk
.gdk
.BUTTON1_MOTION_MASK |
37 gtk
.gdk
.BUTTON_PRESS_MASK |
38 gtk
.gdk
.BUTTON_RELEASE_MASK |
39 gtk
.gdk
.POINTER_MOTION_HINT_MASK
41 self
.connect("expose-event", self
.on_expose
)
42 self
.connect("motion-notify-event", self
.on_motion
)
43 self
.connect("button-press-event", self
.on_press
)
44 self
.connect("button-release-event", self
.on_release
)
46 self
.current_line
= None
50 def on_expose(self
, widget
, event
):
51 cairo_context
= self
.window
.cairo_create()
54 cairo_context
.rectangle(
55 event
.area
.x
, event
.area
.y
, event
.area
.width
, event
.area
.height
)
59 cairo_context
.set_line_width(5)
60 cairo_context
.set_line_join(cairo
.LINE_JOIN_ROUND
)
61 cairo_context
.set_line_cap(cairo
.LINE_CAP_ROUND
)
63 # Fill background to white
64 cairo_context
.set_source_rgb(1, 1, 1)
67 # Create path from stored data
68 # Also, if a line is being drawn, append the partial line temporarily.
69 if self
.current_line
: self
.lines
.append(self
.current_line
)
70 for line
in self
.lines
:
71 if len(line
) < 2: continue
76 cairo_context
.move_to(point
.x
, point
.y
)
79 cairo_context
.line_to(point
.x
, point
.y
)
80 if self
.current_line
: self
.lines
.pop()
83 cairo_context
.set_source_rgb(0, 0, 0)
84 cairo_context
.stroke()
87 def on_motion(self
, widget
, event
):
88 if event
.state | gtk
.gdk
.BUTTON1_MASK
:
90 self
.current_line
.append(Point(int(event
.x
), int(event
.y
)))
91 self
.update_drawing_area()
93 # This was marked as non-Win32 in the C++ version. Unsure whether
94 # it's okay as it is here.
95 gtk
.gdk
.event_request_motions(event
)
99 def on_press(self
, widget
, event
):
100 # On left mouse click, create a new line
101 if event
.button
== 1:
102 self
.current_line
= []
103 self
.current_line
.append(Point(int(event
.x
), int(event
.y
)))
107 def on_release(self
, widget
, event
):
108 """Returns False when the control's state has been changed by a button release, True otherwise."""
110 if event
.button
== 1:
111 if self
.current_line
:
112 self
.lines
.append(self
.current_line
)
113 self
.current_line
= None
114 self
.update_drawing_area()
117 # If no line was written, return true (no change in state)
120 elif event
.button
== 3:
121 if self
.current_line
:
122 self
.current_line
= None
123 self
.update_drawing_area()
124 # No change in search results has occurred, so return true
128 self
.update_drawing_area()
130 # The search -has- changed, return False so we can capture it.
137 def get_results(self
):
138 """Returns an array of 5 UTF8-encoded kanji characters."""
140 print "WidgetHWPad.get_results() \n\t= %s" % (self
.results
,)
145 self
.current_line
= None
148 self
.update_drawing_area()
150 def update_drawing_area(self
):
151 self
.window
.invalidate_rect(None, False)
153 def look_up_chars(self
):
154 print "WidgetHWPad.look_up_chars()"
157 data_dir
= configure
.get_system_data_dir()
158 kpdata_dir
= os
.path
.join(data_dir
, "kpdata")
161 exe_name
= "jben_kpengine.exe"
163 exe_name
= "jben_kpengine"
166 # x y x y x y x y x y\n (line 1)
167 # x y x y x y x y\n (line 2)
169 pipe_data
= "\n".join(
170 [" ".join([str(point
) for point
in line
])
171 for line
in self
.lines
]) + "\n\n"
173 p
= Popen([exe_name
, "-d", kpdata_dir
], stdin
=PIPE
, stdout
=PIPE
)
174 stdout
, stderr
= p
.communicate(pipe_data
)
176 klist
= stdout
[1:].strip().split()
180 self
.results
.append(unichr(int(kanji
)))