]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/tool/MessageService.py
implement {Create,Clone}RefData for gtk wxBitmap
[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().SetCurrentPos(self.GetControl().GetTextLength())
128 self.GetControl().SetReadOnly(False)
129 self.GetControl().AddText(text)
130 self.GetControl().SetReadOnly(True)
131
132
133 def GetText(self):
134 return self.GetControl().GetText()
135
136
137 def GetCurrentPos(self):
138 return self.GetControl().GetCurrentPos()
139
140
141 def GetCurrLine(self):
142 return self.GetControl().GetCurLine()
143
144
145 #----------------------------------------------------------------------------
146 # Callback Methods
147 #----------------------------------------------------------------------------
148
149 def SetCallback(self, callback):
150 """ Sets in the event table for a doubleclick to invoke the given callback.
151 Additional calls to this method overwrites the previous entry and only the last set callback will be invoked.
152 """
153 wx.stc.EVT_STC_DOUBLECLICK(self.GetControl(), self.GetControl().GetId(), callback)
154
155
156
157 class MessageService(Service.Service):
158
159
160 #----------------------------------------------------------------------------
161 # Constants
162 #----------------------------------------------------------------------------
163 SHOW_WINDOW = wx.NewId() # keep this line for each subclass, need unique ID for each Service
164
165
166 #----------------------------------------------------------------------------
167 # Overridden methods
168 #----------------------------------------------------------------------------
169
170 def _CreateView(self):
171 return MessageView(self)