-
Notifications
You must be signed in to change notification settings - Fork 8
/
EditorUtils.py
537 lines (426 loc) · 18.3 KB
/
EditorUtils.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
import os, time, threading, socket
import wx
import Preferences, Utils
from Utils import _
#-----Toolbar-------------------------------------------------------------------
class MyToolBar(wx.ToolBar):
def __init__(self, *_args, **_kwargs):
wx.ToolBar.__init__(self, _kwargs['parent'], _kwargs['id'],
style=wx.TB_HORIZONTAL | wx.NO_BORDER|Preferences.flatTools)
self.toolLst = []
self.toolCount = 0
self.SetToolBitmapSize((16, 16))
def AddTool(self, id, bitmap, toggleBitmap=wx.NullBitmap, shortHelpString='', isToggle=False):
wx.ToolBar.AddTool(self, id, bitmap, toggleBitmap, isToggle=isToggle,
shortHelpString=shortHelpString)
self.toolLst.append(id)
self.toolCount = self.toolCount + 1
def AddTool2(self, id, bitmapname, shortHelpString='', toggleBitmap=wx.NullBitmap, isToggle=False):
self.AddTool(id, Preferences.IS.load(bitmapname), toggleBitmap, shortHelpString, isToggle)
def AddSeparator(self):
wx.ToolBar.AddSeparator(self)
self.toolLst.append(-1)
self.toolCount = self.toolCount + 1
def DeleteTool(self, id):
wx.ToolBar.DeleteTool(self, id)
self.toolLst.remove(id)
self.toolCount = self.toolCount - 1
def ClearTools(self):
posLst = range(self.toolCount)
posLst.reverse()
for pos in posLst:
self.DeleteToolByPos(pos)
self.DisconnectToolIds()
self.toolLst = []
self.toolCount = 0
def GetToolPopupPosition(self, id):
margins = self.GetToolMargins()
toolSize = self.GetToolSize()
xPos = margins.x
for tId in self.toolLst:
if tId == id:
return wx.Point(xPos, margins.y + toolSize.y)
if tId == -1:
xPos = xPos + self.GetToolSeparation()
else:
xPos = xPos + toolSize.x
return wx.Point(0, 0)
def PopupToolMenu(self, toolId, menu):
self.PopupMenu(menu, self.GetToolPopupPosition(toolId))
def DisconnectToolIds(self):
for wid in self.toolLst:
if wid != -1:
self.GetParent().Disconnect(wid)
class EditorToolBar(MyToolBar):
pass
# fields
sbfIcon, sbfBrwsBtns, sbfStatus, sbfCrsInfo, sbfProgress = range(5)
class EditorStatusBar(wx.StatusBar):
""" Displays information about the current view. Also global stats/
progress bar etc. """
maxHistorySize = 250
def __init__(self, *_args, **_kwargs):
wx.StatusBar.__init__(self, _kwargs['parent'], _kwargs['id'], style=wx.ST_SIZEGRIP)
self.SetFieldsCount(6)
if wx.Platform == '__WXGTK__':
imgWidth = 21
else:
imgWidth = 16
self.SetStatusWidths([imgWidth, 36, 400, 25, 150, -1])
rect = self.GetFieldRect(sbfIcon)
self.img = wx.StaticBitmap(self, -1,
Preferences.IS.load('Images/Shared/BoaLogo.png'),
(rect.x+1, rect.y+1), (16, 16))
self.img.Bind(wx.EVT_LEFT_DCLICK, self.OnShowHistory)
rect = self.GetFieldRect(sbfBrwsBtns)
#self.historyBtns = wx.SpinButton(self, -1, (rect.x+1, rect.y+1),
# (rect.width-2, rect.height-2))
self.historyBtnBack = wx.BitmapButton(self, -1,
Preferences.IS.load('Images/Shared/PreviousSmall.png'),
(rect.x+1, rect.y+1), (int(round(rect.width/2.0))-1, rect.height-2))
self.historyBtnFwd = wx.BitmapButton(self, -1,
Preferences.IS.load('Images/Shared/NextSmall.png'),
(rect.x+1+int(round(rect.width/2.0)), rect.y+1), (int(round(rect.width/2.0))-1, rect.height-2))
#self.historyBtns.SetToolTipString('Browse the Traceback/Error/Output window history.')
tip = _('Browse the Traceback/Error/Output window history.')
self.historyBtnBack.SetToolTipString(tip)
self.historyBtnFwd.SetToolTipString(tip)
#self.historyBtns.Bind(wx.EVT_SPIN_DOWN, self.OnErrOutHistoryBack, id=self.historyBtns.GetId())
#self.historyBtns.Bind(wx.EVT_SPIN_UP, self.OnErrOutHistoryFwd, id=self.historyBtns.GetId())
self.historyBtnBack.Bind(wx.EVT_BUTTON, self.OnErrOutHistoryBack, id=self.historyBtnBack.GetId())
self.historyBtnFwd.Bind(wx.EVT_BUTTON, self.OnErrOutHistoryFwd, id=self.historyBtnFwd.GetId())
self.erroutFrm = None
self.progress = wx.Gauge(self, -1, 100)
self.linkProgressToStatusBar()
self.images = {'Info': Preferences.IS.load('Images/Shared/Info.png'),
'Warning': Preferences.IS.load('Images/Shared/Warning.png'),
'Error': Preferences.IS.load('Images/Shared/Error.png')}
self.history = []
self._histcnt = 0
def destroy(self):
self.images = None
def setHint(self, hint, msgType='Info', ringBell=False):
""" Show a status message in the statusbar, optionally rings a bell.
msgType can be 'Info', 'Warning' or 'Error'
"""
if not self.images:
return
self._histcnt = self._histcnt - 1
if hint.strip():
self.history.append( (msgType, time.strftime('%H:%M:%S',
time.localtime(time.time())), hint, ringBell) )
if len(self.history) > self.maxHistorySize:
del self.history[0]
self.SetStatusText(hint, sbfStatus)
self.img.SetToolTipString(hint)
self.img.SetBitmap(self.images[msgType])
if ringBell: wx.Bell()
def OnEditorNotification(self, event):
self.setHint(event.message)
logDlgs = {'Info': wx.LogMessage,
'Warning': wx.LogWarning,
'Error': wx.LogError}
def OnShowHistory(self, event):
hist = self.history[:]
hp = HistoryPopup(self.GetParent(), hist, self.images)
def linkProgressToStatusBar(self):
rect = self.GetFieldRect(sbfProgress)
self.progress.SetDimensions(rect.x+1, rect.y+1, rect.width -2, rect.height -2)
def setColumnPos(self, value):
self.SetStatusText(str(value), sbfCrsInfo)
def OnErrOutHistoryBack(self, event):
if self.erroutFrm:
self.erroutFrm.stepBackInHistory()
def OnErrOutHistoryFwd(self, event):
if self.erroutFrm:
self.erroutFrm.stepFwdInHistory()
def HistoryPopup(parent, hist, imgs):
f = wx.MiniFrame(parent, -1, _('Editor status history'), size = (350, 200))
lc = wx.ListCtrl(f, style=wx.LC_REPORT | wx.LC_VRULES | wx.LC_NO_HEADER)
lc.il = wx.ImageList(16, 16)
idxs = {}
for tpe, img in imgs.items():
idxs[tpe] = lc.il.Add(img)
lc.SetImageList(lc.il, wx.IMAGE_LIST_SMALL)
lc.InsertColumn(0, _('Time'))
lc.InsertColumn(1, _('Message'))
lc.SetColumnWidth(0, 75)
lc.SetColumnWidth(1, 750)
for tpe, tme, msg, _bell in hist:
lc.InsertImageStringItem(0, tme, idxs[tpe])
lc.SetStringItem(0, 1, msg)
f.Center()
f.Show()
wx.PostEvent(f, wx.SizeEvent(f.GetSize()))
return f
#-----Model hoster--------------------------------------------------------------
wxID_MODULEPAGEVIEWCHANGE, wxID_MODULEPAGECLOSEVIEW = Utils.wxNewIds(2)
class ModulePage:
""" Represents a notebook on a page of the top level notebook hosting
the model instance. """
def __init__(self, parent, model, defViews, views, idx, editor):
self.editor = editor
self.defViews = [(v, wx.NewId()) for v in defViews]
self.adtViews = [(v, wx.NewId()) for v in views]
self.viewIds = []
self.model = model
self.parent = parent
self.notebook = wx.Notebook(parent, -1, style=wx.WANTS_CHARS | wx.CLIP_CHILDREN)
self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChange, id=self.notebook.GetId())
self.tIdx = idx
self.updatePageName()
self.windowId = wx.NewId()
self.editor.winMenu.Append(self.windowId, self.getMenuLabel(),
_('Switch to highlighted file'))
self.editor.Bind(wx.EVT_MENU, self.editor.OnGotoModulePage, id=self.windowId)
self.notebook.Bind(wx.EVT_MENU, self.OnDirectActionClose, id=wxID_MODULEPAGECLOSEVIEW)
self.notebook.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
Class = model.__class__
## if not editor.defaultAdtViews.has_key(cls):
## cls = model.__class__.__bases__[0]
tot = len(defViews) ##+ len(editor.defaultAdtViews.get(Class, []))
if tot:
stepsDone = 50.0
editor.statusBar.progress.SetValue(int(stepsDone))
step = (100 - stepsDone) / tot
for View in defViews:
self.addView(View)
stepsDone = stepsDone + step
editor.statusBar.progress.SetValue(int(stepsDone))
## for View in editor.defaultAdtViews.get(Class, []):
## self.addView(View)
## stepsDone = stepsDone + step
## editor.statusBar.progress.SetValue(int(stepsDone))
if defViews:
self.default = defViews[0].viewName
else:
self.default = None
self.viewMenu = self.viewSelectionMenu()
editor.statusBar.progress.SetValue(0)
def destroy(self):
""" Destroy all views, notepad pages and the view notebook."""
self.disconnectEvts()
self.editor.Disconnect(self.windowId)
self.editor.winMenu.Delete(self.windowId)
self.viewMenu.Destroy()
for view in self.model.views.values():
if view:
view.close()
self.notebook.DeleteAllPages()
self.model.destroy()
self.model = None
self.notebook.Destroy()
## def __del__(self):
## print '__del__', self.__class__.__name__
def __repr__(self):
return '<%s: %s, %d>' %(self.__class__.__name__, os.path.basename(self.model.filename), self.tIdx)
def updatePageName(self):
""" Return a name that is decorated with () meaning never been saved
and/or * meaning model modified ~ meaning view modified. """
pageName = self.model.getPageName()
if not self.model.savedAs: sa1, sa2 = '(', ')'
else: sa1 = sa2 = ''
if len(self.model.viewsModified): vm = '~'
else: vm = ''
if self.model.modified: m = '*'
else: m = ''
if self.model.transport and self.model.transport.stdAttrs['read-only']:
ro = ' (read only)'
else: ro = ''
self.pageName = '%s%s%s%s%s%s%s%s' % (m, vm, sa1, pageName, ro, sa2, vm, m)
return self.pageName
def getActiveView(self, idx=None):
if idx is None: idx = self.notebook.GetSelection()
if idx == -1: return None
for name, view in self.model.views.items():
if view.pageIdx == idx:
return view
return None
## name = self.notebook.GetPageText(idx)
## if name and name[0] == '~': name = name[1:-1]
## try:
## return self.model.views[name]
## except KeyError:
## return None
def viewSelectionMenu(self):
menu = wx.Menu()
for View, wId in self.defViews:
menu.Append(wId, Utils.getViewTitle(View))
menu.AppendSeparator()
for View, wId in self.adtViews:
menu.Append(wId, Utils.getViewTitle(View), '', View not in self.adtViews)
return menu
def connectEvts(self):
for view, wId in self.defViews:
self.editor.Bind(wx.EVT_MENU, self.editor.OnSwitchedToView, id=wId)
for view, wId in self.adtViews:
self.editor.Bind(wx.EVT_MENU, self.editor.OnToggleView, id=wId)
def disconnectEvts(self):
if self.model:
for view, wId in self.defViews + self.adtViews:
self.editor.Disconnect(wId)
def setActiveViewsMenu(self):
viewClss = [x.__class__ for x in self.model.views.values()]
for view, wId in self.adtViews:
self.viewMenu.Check(wId, view in viewClss)
def addView(self, View, viewName=''):
""" Add a view to the model and display it as a page in the notebook
of view instances."""
if not viewName:
viewName = View.viewName
viewTitle = Utils.getViewTitle(View)
else:
viewTitle = viewName
if wx.Platform == '__WXGTK__':
panel, view = Utils.wxProxyPanel(self.notebook, View, self.model)
self.model.views[viewName] = view
if View.docked:
self.model.views[viewName].addToNotebook(self.notebook, viewTitle,
panel=panel)
else:
view = View(self.notebook, self.model)
self.model.views[viewName] = view
if View.docked:
self.model.views[viewName].addToNotebook(self.notebook, viewTitle)
return self.model.views[viewName]
def refresh(self):
pass
# self.notebook.Refresh()
def focus(self):
""" Make this model page the currently selected page. """
self.parent.SetSelection(self.tIdx)
def removedPage(self, idx):
""" Called on all ModulePages after a sibling ModulePage deletion.
Decrements tIdx if bigger than idx. """
if idx < self.tIdx:
self.tIdx = self.tIdx - 1
def addedPage(self, idx):
if idx <= self.tIdx:
self.tIdx = self.tIdx + 1
def saveAs(self, filename):
newFilename, success = self.editor.saveAsDlg(filename)
if success:
self.model.saveAs(newFilename)
self.editor.updateModulePage(self.model, filename)
self.editor.updateTitle()
return success
def saveOrSaveAs(self, forceSaveAs=False):
model = self.model
editor = self.editor
if forceSaveAs or not model.savedAs:
oldName = model.filename
if self.saveAs(oldName) and (oldName != model.filename):
self.rename(oldName, model.filename)
editor.statusBar.setHint(_('%s saved.')%\
os.path.basename(model.filename))
else:
from Explorers.ExplorerNodes import TransportModifiedSaveError
try:
model.save()
except TransportModifiedSaveError, err:
choice = wx.MessageBox(_('%s\nDo you want to overwrite these '
'changes (Yes), reload your file (No) or cancel this operation '
'(Cancel)?')%str(err), _('Overwrite newer file warning'),
wx.YES_NO | wx.CANCEL | wx.ICON_WARNING)
if choice == wx.YES:
model.save(overwriteNewer=True)
elif choice == wx.NO:
raise TransportModifiedSaveError('Reload')
elif choice == wx.CANCEL:
raise TransportModifiedSaveError('Cancel')
editor.updateModulePage(model)
editor.updateTitle()
editor.statusBar.setHint(_('%s saved.')%\
os.path.basename(model.filename))
def OnPageChange(self, event):
viewIdx = event.GetSelection()
if event.GetOldSelection() != viewIdx or wx.Platform == '__WXGTK__':
self.editor.setupToolBar(viewIdx=viewIdx)
view = self.getActiveView(viewIdx)
if hasattr(view, 'OnPageActivated'):
view.OnPageActivated(event)
event.Skip()
def OnRightDown(self, event):
actView = self.getActiveView()
doDirectMenuPopup = False
for View, wid in self.adtViews:
if isinstance(actView, View):
doDirectMenuPopup = True
break
if not doDirectMenuPopup:
from Views.EditorViews import CloseableViewMix
if isinstance(actView, CloseableViewMix):
doDirectMenuPopup = True
if doDirectMenuPopup:
directMenu = wx.Menu()
directMenu.Append(wxID_MODULEPAGECLOSEVIEW, _('Close active view'))
self.notebook.PopupMenu(directMenu, event.GetPosition())
directMenu.Destroy()
return
def OnDirectActionClose(self, event):
actView = self.getActiveView()
for View, wid in self.adtViews:
if isinstance(actView, View):
actView.deleteFromNotebook(self.default, actView.viewName)
self.editor.mainMenu.Check(wid, False)
return
from Views.EditorViews import CloseableViewMix
if isinstance(actView, CloseableViewMix):
actView.OnClose(None)
def rename(self, oldName, newName):
del self.editor.modules[oldName]
self.editor.modules[newName] = self
item = self.editor.winMenu.FindItemById(self.windowId)
item.SetText(self.getMenuLabel())
self.editor.editorUpdateNotify()
def getMenuLabel(self):
return '%s (%s)'%(os.path.basename(self.model.filename),
self.model.filename)
socketPort = 50007
selectTimeout = 0.25
class Listener(threading.Thread):
def __init__(self, editor, closed):
#self.queue = queue
self.editor = editor
self.closed = closed
threading.Thread.__init__(self)
def run(self, host='127.0.0.1', port=socketPort):
import socket
from select import select
# Open a socket and listen.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host, port))
except socket.error, err:
self.closed.set()
return
s.listen(5)
while 1:
while 1:
# Listen for 0.25 s, then check if closed is set. In that case,
# end thread by returning.
ready, dummy, dummy = select([s],[],[], selectTimeout)
if self.closed.isSet():
return
if ready:
break
# Accept a connection, read the data and put it into the queue.
conn, addr = s.accept()
l = []
while 1:
data = conn.recv(1024)
if not data: break
l.append(data)
name = ''.join(l)
if name.strip():
wx.CallAfter(self.editor.openOrGotoModule, name)
conn.close()
def socketFileOpenServerListen(editor):
closed = threading.Event()
listener = Listener(editor, closed)
listener.start()
return closed, listener
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = HistoryPopup(None, (), {})
app.MainLoop()