]> git.saurik.com Git - wxWidgets.git/blame - src/stc/stc.cpp.in
Unicode compilation fixes (patch from Dimitri)
[wxWidgets.git] / src / stc / stc.cpp.in
CommitLineData
f97d84a6
RD
1////////////////////////////////////////////////////////////////////////////
2// Name: stc.cpp
3// Purpose: A wxWindows implementation of Scintilla. This class is the
4// one meant to be used directly by wx applications. It does not
5// derive directly from the Scintilla classes, but instead
6// delegates most things to the real Scintilla class.
7// This allows the use of Scintilla without polluting the
8// namespace with all the classes and identifiers from Scintilla.
9//
10// Author: Robin Dunn
11//
12// Created: 13-Jan-2000
13// RCS-ID: $Id$
14// Copyright: (c) 2000 by Total Control Software
15// Licence: wxWindows license
16/////////////////////////////////////////////////////////////////////////////
17
18#include <ctype.h>
19
20#include "wx/stc/stc.h"
21#include "ScintillaWX.h"
22
23#include <wx/tokenzr.h>
24
f97d84a6
RD
25
26//----------------------------------------------------------------------
27
10ef30eb 28const wxChar* wxSTCNameStr = wxT("stcwindow");
f97d84a6 29
d25f5fbb
RD
30DEFINE_EVENT_TYPE( wxEVT_STC_CHANGE )
31DEFINE_EVENT_TYPE( wxEVT_STC_STYLENEEDED )
32DEFINE_EVENT_TYPE( wxEVT_STC_CHARADDED )
d25f5fbb
RD
33DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTREACHED )
34DEFINE_EVENT_TYPE( wxEVT_STC_SAVEPOINTLEFT )
35DEFINE_EVENT_TYPE( wxEVT_STC_ROMODIFYATTEMPT )
65ec6247 36DEFINE_EVENT_TYPE( wxEVT_STC_KEY )
d25f5fbb 37DEFINE_EVENT_TYPE( wxEVT_STC_DOUBLECLICK )
65ec6247 38DEFINE_EVENT_TYPE( wxEVT_STC_UPDATEUI )
d25f5fbb 39DEFINE_EVENT_TYPE( wxEVT_STC_MODIFIED )
d25f5fbb
RD
40DEFINE_EVENT_TYPE( wxEVT_STC_MACRORECORD )
41DEFINE_EVENT_TYPE( wxEVT_STC_MARGINCLICK )
42DEFINE_EVENT_TYPE( wxEVT_STC_NEEDSHOWN )
43DEFINE_EVENT_TYPE( wxEVT_STC_POSCHANGED )
65ec6247
RD
44DEFINE_EVENT_TYPE( wxEVT_STC_PAINTED )
45DEFINE_EVENT_TYPE( wxEVT_STC_USERLISTSELECTION )
46DEFINE_EVENT_TYPE( wxEVT_STC_URIDROPPED )
47DEFINE_EVENT_TYPE( wxEVT_STC_DWELLSTART )
48DEFINE_EVENT_TYPE( wxEVT_STC_DWELLEND )
a29a241f
RD
49DEFINE_EVENT_TYPE( wxEVT_STC_START_DRAG )
50DEFINE_EVENT_TYPE( wxEVT_STC_DRAG_OVER )
51DEFINE_EVENT_TYPE( wxEVT_STC_DO_DROP )
d25f5fbb
RD
52
53
f97d84a6
RD
54BEGIN_EVENT_TABLE(wxStyledTextCtrl, wxControl)
55 EVT_PAINT (wxStyledTextCtrl::OnPaint)
56 EVT_SCROLLWIN (wxStyledTextCtrl::OnScrollWin)
5fa4613c 57 EVT_SCROLL (wxStyledTextCtrl::OnScroll)
f97d84a6
RD
58 EVT_SIZE (wxStyledTextCtrl::OnSize)
59 EVT_LEFT_DOWN (wxStyledTextCtrl::OnMouseLeftDown)
4ceb1196
RD
60#ifdef __WXMSW__
61 // Let Scintilla see the double click as a second click
62 EVT_LEFT_DCLICK (wxStyledTextCtrl::OnMouseLeftDown)
63#endif
f97d84a6
RD
64 EVT_MOTION (wxStyledTextCtrl::OnMouseMove)
65 EVT_LEFT_UP (wxStyledTextCtrl::OnMouseLeftUp)
ddf2da08
RD
66#ifdef __WXGTK__
67 EVT_RIGHT_UP (wxStyledTextCtrl::OnMouseRightUp)
68#else
65ec6247 69 EVT_CONTEXT_MENU (wxStyledTextCtrl::OnContextMenu)
ddf2da08 70#endif
37d62433 71 EVT_MOUSEWHEEL (wxStyledTextCtrl::OnMouseWheel)
f97d84a6
RD
72 EVT_CHAR (wxStyledTextCtrl::OnChar)
73 EVT_KEY_DOWN (wxStyledTextCtrl::OnKeyDown)
74 EVT_KILL_FOCUS (wxStyledTextCtrl::OnLoseFocus)
75 EVT_SET_FOCUS (wxStyledTextCtrl::OnGainFocus)
76 EVT_SYS_COLOUR_CHANGED (wxStyledTextCtrl::OnSysColourChanged)
77 EVT_ERASE_BACKGROUND (wxStyledTextCtrl::OnEraseBackground)
78 EVT_MENU_RANGE (-1, -1, wxStyledTextCtrl::OnMenu)
79 EVT_LISTBOX_DCLICK (-1, wxStyledTextCtrl::OnListBox)
80END_EVENT_TABLE()
81
82
83IMPLEMENT_CLASS(wxStyledTextCtrl, wxControl)
84IMPLEMENT_DYNAMIC_CLASS(wxStyledTextEvent, wxCommandEvent)
85
1a2fb4cd
RD
86// forces the linking of the lexer modules
87int Scintilla_LinkLexers();
88
f97d84a6
RD
89//----------------------------------------------------------------------
90// Constructor and Destructor
91
92wxStyledTextCtrl::wxStyledTextCtrl(wxWindow *parent,
93 wxWindowID id,
94 const wxPoint& pos,
95 const wxSize& size,
96 long style,
97 const wxString& name) :
98 wxControl(parent, id, pos, size,
99 style | wxVSCROLL | wxHSCROLL | wxWANTS_CHARS | wxCLIP_CHILDREN,
100 wxDefaultValidator, name)
101{
1a2fb4cd 102 Scintilla_LinkLexers();
f97d84a6
RD
103 m_swx = new ScintillaWX(this);
104 m_stopWatch.Start();
d6582821 105 m_lastKeyDownConsumed = FALSE;
5fa4613c
RD
106 m_vScrollBar = NULL;
107 m_hScrollBar = NULL;
10ef30eb
RD
108#if wxUSE_UNICODE
109 // Put Scintilla into unicode (UTF-8) mode
110 SetCodePage(wxSTC_CP_UTF8);
111#endif
f97d84a6
RD
112}
113
114
115wxStyledTextCtrl::~wxStyledTextCtrl() {
116 delete m_swx;
117}
118
119
120//----------------------------------------------------------------------
121
122long wxStyledTextCtrl::SendMsg(int msg, long wp, long lp) {
123
124 return m_swx->WndProc(msg, wp, lp);
125}
126
127
128#ifdef MAKELONG
129#undef MAKELONG
130#endif
131
132#define MAKELONG(a, b) ((a) | ((b) << 16))
133
134
135static long wxColourAsLong(const wxColour& co) {
136 return (((long)co.Blue() << 16) |
137 ((long)co.Green() << 8) |
138 ((long)co.Red()));
139}
140
141static wxColour wxColourFromLong(long c) {
142 wxColour clr;
143 clr.Set(c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff);
144 return clr;
145}
146
147
148static wxColour wxColourFromSpec(const wxString& spec) {
10ef30eb
RD
149 // spec should be "#RRGGBB"
150 long red, green, blue;
151 red = green = blue = 0;
152 spec.Mid(1,2).ToLong(&red, 16);
153 spec.Mid(3,2).ToLong(&green, 16);
154 spec.Mid(5,2).ToLong(&blue, 16);
f97d84a6
RD
155 return wxColour(red, green, blue);
156}
157
158
159//----------------------------------------------------------------------
160// BEGIN generated section. The following code is automatically generated
161// by gen_iface.py from the contents of Scintilla.iface. Do not edit
162// this file. Edit stc.cpp.in or gen_iface.py instead and regenerate.
163
164%(METHOD_IMPS)s
165
166// END of generated section
167//----------------------------------------------------------------------
168
169
170// Returns the line number of the line with the caret.
171int wxStyledTextCtrl::GetCurrentLine() {
172 int line = LineFromPosition(GetCurrentPos());
173 return line;
174}
175
176
177// Extract style settings from a spec-string which is composed of one or
178// more of the following comma separated elements:
179//
180// bold turns on bold
181// italic turns on italics
182// fore:#RRGGBB sets the foreground colour
183// back:#RRGGBB sets the background colour
184// face:[facename] sets the font face name to use
185// size:[num] sets the font size in points
186// eol turns on eol filling
187// underline turns on underlining
188//
189void wxStyledTextCtrl::StyleSetSpec(int styleNum, const wxString& spec) {
190
191 wxStringTokenizer tkz(spec, ",");
192 while (tkz.HasMoreTokens()) {
193 wxString token = tkz.GetNextToken();
194
195 wxString option = token.BeforeFirst(':');
196 wxString val = token.AfterFirst(':');
197
198 if (option == "bold")
199 StyleSetBold(styleNum, true);
200
201 else if (option == "italic")
202 StyleSetItalic(styleNum, true);
203
204 else if (option == "underline")
205 StyleSetUnderline(styleNum, true);
206
207 else if (option == "eol")
208 StyleSetEOLFilled(styleNum, true);
209
210 else if (option == "size") {
211 long points;
212 if (val.ToLong(&points))
213 StyleSetSize(styleNum, points);
214 }
215
216 else if (option == "face")
217 StyleSetFaceName(styleNum, val);
218
219 else if (option == "fore")
220 StyleSetForeground(styleNum, wxColourFromSpec(val));
221
222 else if (option == "back")
223 StyleSetBackground(styleNum, wxColourFromSpec(val));
224 }
225}
226
227
228// Set style size, face, bold, italic, and underline attributes from
229// a wxFont's attributes.
230void wxStyledTextCtrl::StyleSetFont(int styleNum, wxFont& font) {
231 int size = font.GetPointSize();
232 wxString faceName = font.GetFaceName();
233 bool bold = font.GetWeight() == wxBOLD;
234 bool italic = font.GetStyle() != wxNORMAL;
235 bool under = font.GetUnderlined();
236
237 // TODO: add encoding/charset mapping
238 StyleSetFontAttr(styleNum, size, faceName, bold, italic, under);
239}
240
241// Set all font style attributes at once.
242void wxStyledTextCtrl::StyleSetFontAttr(int styleNum, int size,
243 const wxString& faceName,
244 bool bold, bool italic,
245 bool underline) {
246 StyleSetSize(styleNum, size);
247 StyleSetFaceName(styleNum, faceName);
248 StyleSetBold(styleNum, bold);
249 StyleSetItalic(styleNum, italic);
250 StyleSetUnderline(styleNum, underline);
251
252 // TODO: add encoding/charset mapping
253}
254
255
256// Perform one of the operations defined by the wxSTC_CMD_* constants.
257void wxStyledTextCtrl::CmdKeyExecute(int cmd) {
258 SendMsg(cmd);
259}
260
261
262// Set the left and right margin in the edit area, measured in pixels.
263void wxStyledTextCtrl::SetMargins(int left, int right) {
264 SetMarginLeft(left);
265 SetMarginRight(right);
266}
267
268
269// Retrieve the start and end positions of the current selection.
270void wxStyledTextCtrl::GetSelection(int* startPos, int* endPos) {
271 if (startPos != NULL)
272 *startPos = SendMsg(SCI_GETSELECTIONSTART);
273 if (endPos != NULL)
274 *endPos = SendMsg(SCI_GETSELECTIONEND);
275}
276
277
278// Retrieve the point in the window where a position is displayed.
279wxPoint wxStyledTextCtrl::PointFromPosition(int pos) {
280 int x = SendMsg(SCI_POINTXFROMPOSITION, 0, pos);
281 int y = SendMsg(SCI_POINTYFROMPOSITION, 0, pos);
282 return wxPoint(x, y);
283}
284
285// Scroll enough to make the given line visible
286void wxStyledTextCtrl::ScrollToLine(int line) {
287 m_swx->DoScrollToLine(line);
288}
289
290
291// Scroll enough to make the given column visible
292void wxStyledTextCtrl::ScrollToColumn(int column) {
293 m_swx->DoScrollToColumn(column);
294}
295
296
297
298//----------------------------------------------------------------------
299// Event handlers
300
301void wxStyledTextCtrl::OnPaint(wxPaintEvent& evt) {
302 wxPaintDC dc(this);
303 wxRegion region = GetUpdateRegion();
304
305 m_swx->DoPaint(&dc, region.GetBox());
306}
307
308void wxStyledTextCtrl::OnScrollWin(wxScrollWinEvent& evt) {
309 if (evt.GetOrientation() == wxHORIZONTAL)
310 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
311 else
312 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
313}
314
5fa4613c
RD
315void wxStyledTextCtrl::OnScroll(wxScrollEvent& evt) {
316 wxScrollBar* sb = wxDynamicCast(evt.GetEventObject(), wxScrollBar);
317 if (sb) {
318 if (sb->IsVertical())
319 m_swx->DoVScroll(evt.GetEventType(), evt.GetPosition());
320 else
321 m_swx->DoHScroll(evt.GetEventType(), evt.GetPosition());
322 }
323}
324
f97d84a6
RD
325void wxStyledTextCtrl::OnSize(wxSizeEvent& evt) {
326 wxSize sz = GetClientSize();
327 m_swx->DoSize(sz.x, sz.y);
10ef30eb 328 Refresh(FALSE);
f97d84a6
RD
329}
330
331void wxStyledTextCtrl::OnMouseLeftDown(wxMouseEvent& evt) {
332 wxPoint pt = evt.GetPosition();
333 m_swx->DoButtonDown(Point(pt.x, pt.y), m_stopWatch.Time(),
334 evt.ShiftDown(), evt.ControlDown(), evt.AltDown());
335}
336
337void wxStyledTextCtrl::OnMouseMove(wxMouseEvent& evt) {
338 wxPoint pt = evt.GetPosition();
339 m_swx->DoButtonMove(Point(pt.x, pt.y));
340}
341
342void wxStyledTextCtrl::OnMouseLeftUp(wxMouseEvent& evt) {
343 wxPoint pt = evt.GetPosition();
344 m_swx->DoButtonUp(Point(pt.x, pt.y), m_stopWatch.Time(),
345 evt.ControlDown());
346}
347
348
ddf2da08
RD
349void wxStyledTextCtrl::OnMouseRightUp(wxMouseEvent& evt) {
350 wxPoint pt = evt.GetPosition();
351 m_swx->DoContextMenu(Point(pt.x, pt.y));
352}
353
354
65ec6247 355void wxStyledTextCtrl::OnContextMenu(wxContextMenuEvent& evt) {
f97d84a6 356 wxPoint pt = evt.GetPosition();
65ec6247 357 ScreenToClient(&pt.x, &pt.y);
f97d84a6
RD
358 m_swx->DoContextMenu(Point(pt.x, pt.y));
359}
360
37d62433
RD
361
362void wxStyledTextCtrl::OnMouseWheel(wxMouseEvent& evt) {
363 m_swx->DoMouseWheel(evt.GetWheelRotation(),
364 evt.GetWheelDelta(),
65ec6247
RD
365 evt.GetLinesPerAction(),
366 evt.ControlDown());
37d62433
RD
367}
368
369
f97d84a6 370void wxStyledTextCtrl::OnChar(wxKeyEvent& evt) {
10ef30eb 371 int key = evt.GetKeyCode();
f3c2c221
RD
372
373 // AltGr keys???
374