]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/tool/MessageService.py
Tweaks for demos on MacOSX
[wxWidgets.git] / wxPython / samples / ide / activegrid / tool / MessageService.py
1 #----------------------------------------------------------------------------
2 # Name: MessageService.py
3 # Purpose: Message View Service for pydocview
4 #
5 # Author: Morgan Hua
6 #
7 # Created: 9/2/04
8 # CVS-ID: $Id$
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
12
13 import wx
14 import Service
15 import STCTextEditor
16
17 #----------------------------------------------------------------------------
18 # Utility
19 #----------------------------------------------------------------------------
20
21 def ClearMessages():
22 messageService = wx.GetApp().GetService(MessageService)
23 view = messageService.GetView()
24 if view:
25 view.ClearLines()
26
27
28 def ShowMessages(messages, clear=False):
29 if ((messages != None) and (len(messages) > 0)):
30 messageService = wx.GetApp().GetService(MessageService)
31 messageService.ShowWindow(True)
32 view = messageService.GetView()
33 if view:
34 if (clear):
35 view.ClearLines()
36 for message in messages:
37 view.AddLines(message)
38 view.AddLines("\n")
39
40
41 #----------------------------------------------------------------------------
42 # Classes
43 #----------------------------------------------------------------------------
44
45
46 class MessageView(Service.ServiceView):
47 """ Reusable Message View for any document.
48 When an item is selected, the document view is called back (with DoSelectCallback) to highlight and display the corresponding item in the document view.
49 """
50
51 #----------------------------------------------------------------------------
52 # Overridden methods
53 #----------------------------------------------------------------------------
54
55 def _CreateControl(self, parent, id):
56 txtCtrl = STCTextEditor.TextCtrl(parent, id)
57 txtCtrl.SetMarginWidth(1, 0) # hide line numbers
58 txtCtrl.SetReadOnly(True)
59
60 if wx.Platform == '__WXMSW__':
61 font = "Courier New"
62 else:
63 font = "Courier"
64 txtCtrl.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, faceName = font))
65 txtCtrl.SetFontColor(wx.BLACK)
66 txtCtrl.StyleClearAll()
67 txtCtrl.UpdateStyles()
68 wx.EVT_SET_FOCUS(txtCtrl, self.OnFocus)
69
70 return txtCtrl
71
72 def GetDocument(self):
73 return None
74
75 def OnFocus(self, event):
76 wx.GetApp().GetDocumentManager().ActivateView(self)
77 event.Skip()
78
79 def ProcessEvent(self, event):
80 stcControl = self.GetControl()
81 if not isinstance(stcControl, wx.stc.StyledTextCtrl):
82 return wx.lib.docview.View.ProcessEvent(self, event)
83 id = event.GetId()
84 if id == wx.ID_COPY:
85 stcControl.Copy()
86 return True
87 elif id == wx.ID_CLEAR:
88 stcControl.Clear()
89 return True
90 elif id == wx.ID_SELECTALL:
91 stcControl.SetSelection(0, -1)
92 return True
93
94
95 def ProcessUpdateUIEvent(self, event):
96 stcControl = self.GetControl()
97 if not isinstance(stcControl, wx.stc.StyledTextCtrl):
98 return wx.lib.docview.View.ProcessUpdateUIEvent(self, event)
99 id = event.GetId()
100 if id == wx.ID_CUT or id == wx.ID_PASTE:
101 # I don't think cut or paste makes sense from a message/log window.
102 event.Enable(False)
103 return True
104 elif id == wx.ID_COPY:
105 hasSelection = (stcControl.GetSelectionStart() != stcControl.GetSelectionEnd())
106 event.Enable(hasSelection)
107 return True
108 elif id == wx.ID_CLEAR:
109 event.Enable(True) # wxBug: should be stcControl.CanCut()) but disabling clear item means del key doesn't work in control as expected
110 return True
111 elif id == wx.ID_SELECTALL:
112 event.Enable(stcControl.GetTextLength() > 0)
113 return True
114
115
116 #----------------------------------------------------------------------------
117 # Service specific methods
118 #----------------------------------------------------------------------------
119
120 def ClearLines(self):
121 self.GetControl().SetReadOnly(False)
122 self.GetControl().ClearAll()
123 self.GetControl().SetReadOnly(True)
124
125
126 def AddLines(self, text):
127 self.GetControl().SetReadOnly(False)
128 self.GetControl().AddText(text)
129 self.GetControl().SetReadOnly(True)
130
131
132 def GetText(self):
133 return self.GetControl().GetText()
134
135
136 def GetCurrentPos(self):
137 return self.GetControl().GetCurrentPos()
138
139
140 def GetCurrLine(self):
141 return self.GetControl().GetCurLine()
142
143
144 #----------------------------------------------------------------------------
145 # Callback Methods
146 #----------------------------------------------------------------------------
147
148 def SetCallback(self, callback):
149 """ Sets in the event table for a doubleclick to invoke the given callback.
150 Additional calls to this method overwrites the previous entry and only the last set callback will be invoked.
151 """
152 wx.stc.EVT_STC_DOUBLECLICK(self.GetControl(), self.GetControl().GetId(), callback)
153
154
155
156 class MessageService(Service.Service):
157
158
159 #----------------------------------------------------------------------------
160 # Constants
161 #----------------------------------------------------------------------------
162 SHOW_WINDOW = wx.NewId() # keep this line for each subclass, need unique ID for each Service
163
164
165 #----------------------------------------------------------------------------
166 # Overridden methods
167 #----------------------------------------------------------------------------
168
169 def _CreateView(self):
170 return MessageView(self)