forked from JeffHoogland/ePad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathePad.py
434 lines (370 loc) · 16 KB
/
ePad.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/python
# encoding: utf-8
# ePad - a simple text editor written in Elementary and Python
#
# 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/>.
from __future__ import print_function # May as well bite the bullet
__author__ = "Jeff Hoogland"
__contirbutors__ = ["Jeff Hoogland", "Robert Wiley", "Kai Huuhko", "Scimmia22"]
__copyright__ = "Copyright (C) 2014 Bodhi Linux"
__version__ = "0.5.6"
PY_EFL = "https://git.enlightenment.org/bindings/python/python-efl.git/"
def printErr(*objs):
print(*objs, file=sys.stderr)
import sys
import os
import time
try:
from efl.evas import EVAS_HINT_EXPAND, EVAS_HINT_FILL
from efl import elementary
from efl.elementary.window import StandardWindow
from efl.elementary.box import Box
from efl.elementary.button import Button
from efl.elementary.label import Label
from efl.elementary.icon import Icon
from efl.elementary.entry import Entry, ELM_TEXT_FORMAT_PLAIN_UTF8
from efl.elementary.popup import Popup
from efl.elementary.toolbar import Toolbar, ELM_OBJECT_SELECT_MODE_NONE
from efl.elementary.flip import Flip, ELM_FLIP_ROTATE_XZ_CENTER_AXIS, \
ELM_FLIP_ROTATE_YZ_CENTER_AXIS, ELM_FLIP_INTERACTION_ROTATE
from efl.elementary.fileselector import Fileselector
from efl.elementary.transit import Transit, \
ELM_TRANSIT_EFFECT_WIPE_TYPE_HIDE, ELM_TRANSIT_EFFECT_WIPE_DIR_RIGHT
from efl.elementary.check import Check
# Imported here to stop class resolver complaining when an input event
# applies to an internal layout object
from efl.elementary.layout import Layout
# Imported here to stop ValueError exception msgs in Fileselector dialog
from efl.elementary.genlist import Genlist
except ImportError:
printErr("ImportError: Please install Python-EFL:\n ", PY_EFL)
exit(1)
EXPAND_BOTH = EVAS_HINT_EXPAND, EVAS_HINT_EXPAND
EXPAND_HORIZ = EVAS_HINT_EXPAND, 0.0
FILL_BOTH = EVAS_HINT_FILL, EVAS_HINT_FILL
FILL_HORIZ = EVAS_HINT_FILL, 0.5
ALIGN_CENTER = 0.5, 0.5
ALIGN_RIGHT = 1.0, 0.5
WORD_WRAP = False
SHOW_POS = True
class Interface(object):
def __init__(self):
self.mainWindow = StandardWindow("epad", "Untitled - ePad",
size=(600, 400))
self.mainWindow.callback_delete_request_add(self.closeChecks)
self.mainWindow.elm_event_callback_add(self.eventsCb)
icon = Icon(self.mainWindow,
size_hint_weight=EXPAND_BOTH,
size_hint_align=FILL_BOTH)
icon.standard_set('accessories-text-editor')
icon.show()
self.mainWindow.icon_object_set(icon.object_get())
self.mainBox = Box(self.mainWindow,
size_hint_weight=EXPAND_BOTH,
size_hint_align=FILL_BOTH)
self.mainBox.show()
self.mainTb = ePadToolbar(self, self.mainWindow)
self.mainTb.show()
self.mainBox.pack_end(self.mainTb)
# Initialize Text entry box
print("Word wrap Initialized: {0}".format(self.wordwrap))
self.entryInit()
# Add label to show current cursor position
if SHOW_POS:
self.line_label = Label(self.mainWindow,
size_hint_weight=EXPAND_HORIZ,
size_hint_align=ALIGN_RIGHT)
self.curChanged(self.mainEn, self.line_label)
self.line_label.show()
self.mainBox.pack_end(self.line_label)
self.mainEn.callback_cursor_changed_add(self.curChanged,
self.line_label)
# Build our file selector for saving/loading files
self.fileBox = Box(self.mainWindow,
size_hint_weight=EXPAND_BOTH,
size_hint_align=FILL_BOTH)
self.fileBox.show()
self.fileLabel = Label(self.mainWindow,
size_hint_weight=EXPAND_HORIZ,
size_hint_align=FILL_BOTH, text="")
self.fileLabel.show()
self.fileSelector = Fileselector(self.mainWindow, is_save=False,
expandable=False, folder_only=False,
path=os.getenv("HOME"),
size_hint_weight=EXPAND_BOTH,
size_hint_align=FILL_BOTH)
self.fileSelector.callback_done_add(self.fileSelected)
self.fileSelector.show()
self.fileBox.pack_end(self.fileLabel)
self.fileBox.pack_end(self.fileSelector)
# Flip object has the file selector on one side
# and the GUI on the other
self.flip = Flip(self.mainWindow, size_hint_weight=EXPAND_BOTH,
size_hint_align=FILL_BOTH)
self.flip.part_content_set("front", self.mainBox)
self.flip.part_content_set("back", self.fileBox)
self.mainWindow.resize_object_add(self.flip)
self.flip.show()
self.isSaved = True
self.isNewFile = False
self.confirmPopup = None
def entryInit(self):
self.mainEn = Entry(self.mainWindow, scrollable=True,
line_wrap=self.wordwrap, autosave=False,
size_hint_weight=EXPAND_BOTH,
size_hint_align=FILL_BOTH)
self.mainEn.callback_changed_user_add(self.textEdited)
self.mainEn.elm_event_callback_add(self.eventsCb)
# self.mainEn.markup_filter_append(self.textFilter)
self.mainEn.show()
self.mainBox.pack_end(self.mainEn)
def curChanged(self, entry, label):
# get linear index into current text
index = entry.cursor_pos_get()
# Replace <br /> tag with single char
# to simplify (line, col) calculation
tmp_text = entry.entry_get().replace("<br/>", "\n")
line = tmp_text[:index].count("\n") + 1
col = len(tmp_text[:index].split("\n")[-1]) + 1
# Update label text with line, col
label.text = "Ln {0} Col {1} ".format(line, col)
def textEdited(self, obj):
current_file = self.mainEn.file[0]
current_file = \
os.path.basename(current_file) if \
current_file and not self.isNewFile else \
"Untitled"
self.mainWindow.title = "*%s - ePad" % (current_file)
self.isSaved = False
def fileSelected(self, fs, file_selected, onStartup=False):
if not onStartup:
self.flip.go(ELM_FLIP_INTERACTION_ROTATE)
print(file_selected)
IsSave = fs.is_save_get()
if file_selected:
if IsSave:
newfile = open(file_selected, 'w')
tmp_text = self.mainEn.entry_get()
newfile.write(tmp_text)
newfile.close()
self.mainEn.file_set(file_selected, ELM_TEXT_FORMAT_PLAIN_UTF8)
self.mainEn.entry_set(tmp_text)
self.mainEn.file_save()
self.mainWindow.title_set("%s - ePad"
% os.path.basename(file_selected))
self.isSaved = True
self.isNewFile = False
else:
try:
self.mainEn.file_set(file_selected,
ELM_TEXT_FORMAT_PLAIN_UTF8)
except RuntimeError:
print("Empty file: {0}".format(file_selected))
self.mainWindow.title_set("%s - ePad"
% os.path.basename(file_selected))
def newFile(self, obj=None, ignoreSave=False):
if self.isSaved is True or ignoreSave is True:
trans = Transit()
trans.object_add(self.mainEn)
trans.auto_reverse = True
trans.effect_wipe_add(
ELM_TRANSIT_EFFECT_WIPE_TYPE_HIDE,
ELM_TRANSIT_EFFECT_WIPE_DIR_RIGHT)
trans.duration = 0.5
trans.go()
time.sleep(0.5)
self.mainWindow.title_set("Untitled - ePad")
self.mainEn.delete()
self.entryInit()
self.isNewFile = True
elif self.confirmPopup is None:
self.confirmSave(self.newFile)
def openFile(self, obj=None, ignoreSave=False):
if self.isSaved is True or ignoreSave is True:
self.fileSelector.is_save_set(False)
self.fileLabel.text = "<b>Select a text file to open:</b>"
self.flip.go(ELM_FLIP_ROTATE_YZ_CENTER_AXIS)
elif self.confirmPopup is None:
self.confirmSave(self.openFile)
def confirmSave(self, ourCallback=None):
self.confirmPopup = Popup(self.mainWindow,
size_hint_weight=EXPAND_BOTH)
self.confirmPopup.part_text_set("title,text", "File Unsaved")
current_file = self.mainEn.file[0]
current_file = \
os.path.basename(current_file) if current_file else "Untitled"
self.confirmPopup.text = "Save changes to '%s'?" % (current_file)
# Close without saving button
no_btt = Button(self.mainWindow)
no_btt.text = "No"
no_btt.callback_clicked_add(self.closePopup, self.confirmPopup)
if ourCallback is not None:
no_btt.callback_clicked_add(ourCallback, True)
no_btt.show()
# cancel close request
cancel_btt = Button(self.mainWindow)
cancel_btt.text = "Cancel"
cancel_btt.callback_clicked_add(self.closePopup, self.confirmPopup)
cancel_btt.show()
# Save the file and then close button
sav_btt = Button(self.mainWindow)
sav_btt.text = "Yes"
sav_btt.callback_clicked_add(self.saveFile)
sav_btt.callback_clicked_add(self.closePopup, self.confirmPopup)
sav_btt.show()
# add buttons to popup
self.confirmPopup.part_content_set("button1", no_btt)
self.confirmPopup.part_content_set("button2", cancel_btt)
self.confirmPopup.part_content_set("button3", sav_btt)
self.confirmPopup.show()
def saveAs(self):
self.fileSelector.is_save_set(True)
self.fileLabel.text = "<b>Save new file to where:</b>"
self.flip.go(ELM_FLIP_ROTATE_XZ_CENTER_AXIS)
def saveFile(self, obj=False):
if self.mainEn.file_get()[0] is None or self.isNewFile:
self.saveAs()
else:
self.mainEn.file_save()
self.mainWindow.title_set("%s - ePad"
% os.path.basename(self.mainEn.file[0]))
self.isSaved = True
def closeChecks(self, obj):
print(self.isSaved)
if self.isSaved is False and self.confirmPopup is None:
self.confirmSave(self.closeApp)
else:
self.closeApp()
def closePopup(self, bt, confirmPopup):
self.confirmPopup.delete()
self.confirmPopup = None
def closeApp(self, obj=False, trash=False):
elementary.exit()
def eventsCb(self, obj, src, event_type, event):
if event.modifier_is_set("Control"):
if event.key.lower() == "n":
self.newFile()
elif event.key.lower() == "s" and event.modifier_is_set("Shift"):
self.saveAs()
elif event.key.lower() == "s":
self.saveFile()
elif event.key.lower() == "o":
self.openFile()
# Legacy hack no longer needed
# there was an issue in elementary entry where it would
# accept those character controls
# def textFilter( self, obj, theText, data ):
# # Block ctrl+hot keys used in eventsCb
# #
# # Ctrl O Ctrl N Ctrl S
# ctrl_block = [chr(14), chr(15), chr(19)]
# if theText in ctrl_block:
# return None
# else:
# return theText
def launch(self, startingFile=False):
if startingFile:
self.fileSelected(self.fileSelector, startingFile, True)
self.mainWindow.show()
class ePadToolbar(Toolbar):
def __init__(self, parent, canvas):
Toolbar.__init__(self, canvas)
self._parent = parent
self._canvas = canvas
self.homogeneous = False
self.size_hint_weight = (0.0, 0.0)
self.size_hint_align = (EVAS_HINT_FILL, 0.0)
self.select_mode = ELM_OBJECT_SELECT_MODE_NONE
self.menu_parent = canvas
self.item_append("document-new", "New", self.newPress)
self.item_append("document-open", "Open", self.openPress)
self.item_append("document-save", "Save", self.savePress)
self.item_append("document-save-as", "Save As", self.saveAsPress)
# -- Edit Dropdown Menu --
tb_it = self.item_append("edit", "Edit")
tb_it.menu = True
menu = tb_it.menu
menu.item_add(None, "Copy", "edit-copy", self.copyPress)
menu.item_add(None, "Paste", "edit-paste", self.pastePress)
menu.item_add(None, "Cut", "edit-cut", self.cutPress)
menu.item_separator_add()
menu.item_add(None, "Select All", "edit-select-all",
self.selectAllPress)
# -----------------------
#
# -- Options Dropdown Menu --
#
# self.item_append("settings", "Options", self.optionsPress)
tb_it = self.item_append("preferences-desktop", "Options")
tb_it.menu = True
menu = tb_it.menu
self._parent.wordwrap = WORD_WRAP
it = menu.item_add(None, "Wordwrap", None, self.optionsWWPress)
chk = Check(canvas, disabled=True)
it.content = chk
# ---------------------------
self.item_append("dialog-information", "About", self.aboutPress)
def newPress(self, obj, it):
self._parent.newFile()
def openPress(self, obj, it):
self._parent.openFile()
def savePress(self, obj, it):
self._parent.saveFile()
def saveAsPress(self, obj, it):
self._parent.saveAs()
def optionsWWPress(self, obj, it):
wordwrap = self._parent.mainEn.line_wrap
wordwrap = not wordwrap
self._parent.mainEn.line_wrap = wordwrap
it.content.state = wordwrap
# FIXME: is this variable needed for anything?
self._parent.wordwrap = wordwrap
def copyPress(self, obj, it):
self._parent.mainEn.selection_copy()
def pastePress(self, obj, it):
self._parent.mainEn.selection_paste()
def cutPress(self, obj, it):
self._parent.mainEn.selection_cut()
def selectAllPress(self, obj, it):
self._parent.mainEn.select_all()
def aboutPress(self, obj, it):
# About popup
self.popupAbout = Popup(self._canvas, size_hint_weight=EXPAND_BOTH)
self.popupAbout.part_text_set("title,text",
"ePad version {0}".format(__version__))
self.popupAbout.text = (
"A simple text editor written in "
"python and elementary<br><br> "
"By: Jeff Hoogland"
)
bt = Button(self._canvas, text="Done")
bt.callback_clicked_add(self.aboutClose)
self.popupAbout.part_content_set("button1", bt)
self.popupAbout.show()
def aboutClose(self, bt):
self.popupAbout.delete()
if __name__ == "__main__":
elementary.init()
GUI = Interface()
if len(sys.argv) > 1:
ourFile = str(sys.argv[1])
if ourFile[0:7] == "file://":
print(ourFile)
ourFile = ourFile[7:len(ourFile)]
print(ourFile)
GUI.launch(ourFile)
else:
GUI.launch()
elementary.run()
elementary.shutdown()