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