This repository has been archived by the owner on Aug 8, 2019. It is now read-only.
/
zen_dialog.py
120 lines (99 loc) · 4 KB
/
zen_dialog.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
# Zen Coding for Gedit
#
# Copyright (C) 2010 Franck Marcia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pygtk
pygtk.require('2.0')
import gtk
class ZenDialog():
def __init__(self, editor, x, y, callback, text, last):
self.editor = editor
self.exit = False
self.done = False
self.abbreviation = text
self.callback = callback
self.last = last
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_decorated(False)
self.window.connect("destroy", self.quit)
self.window.connect("focus-out-event", self.focus_lost)
self.window.connect("key-press-event", self.key_pressed)
self.window.set_resizable(False)
self.window.move(x - 15, y - 35)
self.frame = gtk.Frame()
self.window.add(self.frame)
self.frame.show()
self.box = gtk.HBox()
self.frame.add(self.box)
self.box.show()
self.entry = gtk.Entry()
self.entry.connect("changed", self.update)
self.entry.set_text(text)
self.entry.set_icon_from_icon_name(gtk.ENTRY_ICON_PRIMARY, 'zencoding')
self.entry.set_width_chars(48)
self.box.pack_start(self.entry, True, True, 4)
self.entry.show()
self.window.show()
def key_pressed(widget, what, event):
if event.keyval == 65293: # Return
widget.exit = True
if widget.callback and widget.last:
widget.done = widget.callback(widget.done, widget.abbreviation, True)
widget.quit()
elif event.keyval == 65289: # Tab
widget.exit = True
if widget.callback and widget.last:
widget.done = widget.callback(widget.done, widget.abbreviation, True)
widget.quit()
elif event.keyval == 65307: # Escape
widget.exit = False
if widget.callback:
widget.done = widget.callback(widget.done, '', True)
widget.quit()
else:
return False
def focus_lost(self, widget=None, event=None):
self.exit = True
self.quit()
def update(self, entry):
self.abbreviation = self.entry.get_text()
if self.callback:
self.done = self.callback(self.done, self.abbreviation)
def quit(self, widget=None, event=None):
self.window.hide()
self.window.destroy()
gtk.main_quit()
def main(self):
gtk.main()
def main(editor, window, callback, text = "", last = False):
# ensure the caret is hidden
editor.view.set_cursor_visible(False)
# get coordinates of the cursor
offset_start, offset_end = editor.get_selection_range()
insert = editor.buffer.get_iter_at_offset(offset_start)
location = editor.view.get_iter_location(insert)
window = editor.view.get_window(gtk.TEXT_WINDOW_TEXT)
xo, yo = window.get_origin()
xb, yb = editor.view.buffer_to_window_coords(gtk.TEXT_WINDOW_TEXT, location.x + location.width, location.y)
# open dialog at coordinates with eventual text
my_zen_dialog = ZenDialog(editor, xo + xb, yo + yb, callback, text, last)
my_zen_dialog.main()
# show the caret again
editor.view.set_cursor_visible(True)
# return exit status and abbreviation
if callback:
return my_zen_dialog.done and my_zen_dialog.exit, my_zen_dialog.abbreviation
else:
return my_zen_dialog.exit, my_zen_dialog.abbreviation