]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
3 | // Purpose: wxTextCtrl | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
66a007fb | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
66a007fb VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
4bb6408c | 20 | #ifdef __GNUG__ |
66a007fb | 21 | #pragma implementation "textctrl.h" |
4bb6408c JS |
22 | #endif |
23 | ||
bcd055ae JJ |
24 | #ifdef __VMS |
25 | #define XtParent XTPARENT | |
26 | #endif | |
27 | ||
4bb6408c JS |
28 | #include <sys/types.h> |
29 | #include <sys/stat.h> | |
30 | #include <fstream.h> | |
66a007fb | 31 | #include <ctype.h> |
4bb6408c JS |
32 | |
33 | #include "wx/textctrl.h" | |
34 | #include "wx/settings.h" | |
35 | #include "wx/filefn.h" | |
36 | #include "wx/utils.h" | |
37 | ||
338dd992 JJ |
38 | #ifdef __VMS__ |
39 | #pragma message disable nosimpint | |
40 | #endif | |
02e8b2f9 | 41 | #include <Xm/Text.h> |
338dd992 JJ |
42 | #ifdef __VMS__ |
43 | #pragma message enable nosimpint | |
44 | #endif | |
02e8b2f9 JS |
45 | |
46 | #include "wx/motif/private.h" | |
47 | ||
66a007fb VZ |
48 | // ---------------------------------------------------------------------------- |
49 | // private functions | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
c27eab7e VZ |
52 | // helper: inserts the new text in the value of the text ctrl and returns the |
53 | // result in place | |
54 | static void MergeChangesIntoString(wxString& value, | |
55 | XmTextVerifyCallbackStruct *textStruct); | |
56 | ||
66a007fb VZ |
57 | // callbacks |
58 | static void wxTextWindowChangedProc(Widget w, XtPointer clientData, XtPointer ptr); | |
59 | static void wxTextWindowModifyProc(Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs); | |
60 | static void wxTextWindowGainFocusProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs); | |
61 | static void wxTextWindowLoseFocusProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs); | |
62 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *ptr); | |
02e8b2f9 | 63 | |
66a007fb | 64 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) |
4bb6408c | 65 | |
66a007fb VZ |
66 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
67 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
68 | EVT_CHAR(wxTextCtrl::OnChar) | |
e702ff0f JS |
69 | |
70 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
71 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
72 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
73 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
74 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
75 | ||
76 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
77 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
78 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
79 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
80 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
81 | ||
66a007fb | 82 | END_EVENT_TABLE() |
4bb6408c | 83 | |
66a007fb VZ |
84 | // ============================================================================ |
85 | // implementation | |
86 | // ============================================================================ | |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // wxTextCtrl | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
4bb6408c JS |
92 | // Text item |
93 | wxTextCtrl::wxTextCtrl() | |
4bb6408c | 94 | { |
02e8b2f9 JS |
95 | m_tempCallbackStruct = (void*) NULL; |
96 | m_modified = FALSE; | |
dfc54541 | 97 | m_processedDefault = FALSE; |
4bb6408c JS |
98 | } |
99 | ||
66a007fb VZ |
100 | bool wxTextCtrl::Create(wxWindow *parent, |
101 | wxWindowID id, | |
2d120f83 JS |
102 | const wxString& value, |
103 | const wxPoint& pos, | |
66a007fb VZ |
104 | const wxSize& size, |
105 | long style, | |
2d120f83 JS |
106 | const wxValidator& validator, |
107 | const wxString& name) | |
4bb6408c | 108 | { |
02e8b2f9 JS |
109 | m_tempCallbackStruct = (void*) NULL; |
110 | m_modified = FALSE; | |
dfc54541 | 111 | m_processedDefault = FALSE; |
94b49b93 JS |
112 | // m_backgroundColour = parent->GetBackgroundColour(); |
113 | m_backgroundColour = * wxWHITE; | |
0d57be45 | 114 | m_foregroundColour = parent->GetForegroundColour(); |
66a007fb | 115 | |
4bb6408c JS |
116 | SetName(name); |
117 | SetValidator(validator); | |
66a007fb VZ |
118 | if (parent) |
119 | parent->AddChild(this); | |
120 | ||
4bb6408c | 121 | m_windowStyle = style; |
66a007fb | 122 | |
4bb6408c | 123 | if ( id == -1 ) |
2d120f83 | 124 | m_windowId = (int)NewControlId(); |
4bb6408c | 125 | else |
2d120f83 | 126 | m_windowId = id; |
66a007fb | 127 | |
02e8b2f9 | 128 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
66a007fb | 129 | |
02e8b2f9 | 130 | bool wantHorizScrolling = ((m_windowStyle & wxHSCROLL) != 0); |
66a007fb | 131 | |
02e8b2f9 JS |
132 | // If we don't have horizontal scrollbars, we want word wrap. |
133 | bool wantWordWrap = !wantHorizScrolling; | |
66a007fb | 134 | |
02e8b2f9 JS |
135 | if (m_windowStyle & wxTE_MULTILINE) |
136 | { | |
137 | Arg args[2]; | |
138 | XtSetArg (args[0], XmNscrollHorizontal, wantHorizScrolling ? True : False); | |
139 | XtSetArg (args[1], XmNwordWrap, wantWordWrap ? True : False); | |
66a007fb VZ |
140 | |
141 | m_mainWidget = (WXWidget) XmCreateScrolledText(parentWidget, | |
142 | (char*)name.c_str(), | |
143 | args, 2); | |
144 | ||
02e8b2f9 | 145 | XtVaSetValues ((Widget) m_mainWidget, |
66a007fb VZ |
146 | XmNeditable, ((style & wxTE_READONLY) ? False : True), |
147 | XmNeditMode, XmMULTI_LINE_EDIT, | |
148 | NULL); | |
02e8b2f9 JS |
149 | XtManageChild ((Widget) m_mainWidget); |
150 | } | |
151 | else | |
152 | { | |
66a007fb VZ |
153 | m_mainWidget = (WXWidget)XtVaCreateManagedWidget |
154 | ( | |
155 | (char*)name.c_str(), | |
156 | xmTextWidgetClass, | |
157 | parentWidget, | |
158 | NULL | |
159 | ); | |
160 | ||
17023320 MB |
161 | XtVaSetValues ((Widget) m_mainWidget, |
162 | XmNeditable, ((style & wxTE_READONLY) ? False : True), | |
163 | NULL); | |
164 | ||
02e8b2f9 JS |
165 | // TODO: Is this relevant? What does it do? |
166 | int noCols = 2; | |
167 | if (!value.IsNull() && (value.Length() > (unsigned int) noCols)) | |
168 | noCols = value.Length(); | |
66a007fb VZ |
169 | XtVaSetValues((Widget) m_mainWidget, |
170 | XmNcolumns, noCols, | |
171 | NULL); | |
02e8b2f9 | 172 | } |
66a007fb VZ |
173 | |
174 | // remove border if asked for | |
175 | if ( style & wxNO_BORDER ) | |
176 | { | |
177 | XtVaSetValues((Widget)m_mainWidget, | |
178 | XmNshadowThickness, 0, | |
179 | NULL); | |
180 | } | |
181 | ||
182 | if ( !!value ) | |
b86de524 MB |
183 | { |
184 | #if 0 | |
185 | // don't do this because it is just linking the text to a source | |
186 | // string which is unsafe. MB | |
187 | // | |
66a007fb | 188 | XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str()); |
b86de524 MB |
189 | #else |
190 | // do this instead... MB | |
191 | // | |
192 | XtVaSetValues( (Widget) m_mainWidget, | |
193 | XmNvalue, (char *)value.c_str(), | |
194 | NULL); | |
195 | #endif | |
196 | } | |
197 | ||
66a007fb | 198 | // install callbacks |
02e8b2f9 | 199 | XtAddCallback((Widget) m_mainWidget, XmNvalueChangedCallback, (XtCallbackProc)wxTextWindowChangedProc, (XtPointer)this); |
66a007fb | 200 | |
02e8b2f9 | 201 | XtAddCallback((Widget) m_mainWidget, XmNmodifyVerifyCallback, (XtCallbackProc)wxTextWindowModifyProc, (XtPointer)this); |
66a007fb | 202 | |
dfc54541 | 203 | XtAddCallback((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc)wxTextWindowActivateProc, (XtPointer)this); |
66a007fb | 204 | |
02e8b2f9 | 205 | XtAddCallback((Widget) m_mainWidget, XmNfocusCallback, (XtCallbackProc)wxTextWindowGainFocusProc, (XtPointer)this); |
66a007fb | 206 | |
02e8b2f9 | 207 | XtAddCallback((Widget) m_mainWidget, XmNlosingFocusCallback, (XtCallbackProc)wxTextWindowLoseFocusProc, (XtPointer)this); |
66a007fb VZ |
208 | |
209 | // font | |
da175b2c | 210 | m_font = parent->GetFont(); |
4b5f3fe6 | 211 | ChangeFont(FALSE); |
66a007fb | 212 | |
02e8b2f9 JS |
213 | SetCanAddEventHandler(TRUE); |
214 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); | |
66a007fb | 215 | |
0d57be45 | 216 | ChangeBackgroundColour(); |
66a007fb | 217 | |
4bb6408c JS |
218 | return TRUE; |
219 | } | |
220 | ||
02e8b2f9 | 221 | WXWidget wxTextCtrl::GetTopWidget() const |
4bb6408c | 222 | { |
dfc54541 | 223 | return ((m_windowStyle & wxTE_MULTILINE) ? (WXWidget) XtParent((Widget) m_mainWidget) : m_mainWidget); |
4bb6408c JS |
224 | } |
225 | ||
02e8b2f9 | 226 | wxString wxTextCtrl::GetValue() const |
4bb6408c | 227 | { |
c27eab7e VZ |
228 | wxString str; // result |
229 | ||
dfc54541 | 230 | if (m_windowStyle & wxTE_PASSWORD) |
c27eab7e VZ |
231 | { |
232 | // the value is stored always in m_value because it can't be retrieved | |
233 | // from the text control | |
234 | str = m_value; | |
235 | } | |
dfc54541 JS |
236 | else |
237 | { | |
c27eab7e | 238 | // just get the string from Motif |
dfc54541 | 239 | char *s = XmTextGetString ((Widget) m_mainWidget); |
c27eab7e | 240 | if ( s ) |
dfc54541 | 241 | { |
c27eab7e | 242 | str = s; |
dfc54541 | 243 | XtFree (s); |
2d120f83 | 244 | } |
c27eab7e VZ |
245 | //else: return empty string |
246 | ||
247 | if ( m_tempCallbackStruct ) | |
dfc54541 | 248 | { |
c27eab7e VZ |
249 | // the string in the control isn't yet updated, can't use it as is |
250 | MergeChangesIntoString(str, (XmTextVerifyCallbackStruct *) | |
251 | m_tempCallbackStruct); | |
dfc54541 JS |
252 | } |
253 | } | |
c27eab7e VZ |
254 | |
255 | return str; | |
4bb6408c JS |
256 | } |
257 | ||
02e8b2f9 | 258 | void wxTextCtrl::SetValue(const wxString& value) |
4bb6408c | 259 | { |
dfc54541 | 260 | m_inSetValue = TRUE; |
66a007fb | 261 | |
b86de524 MB |
262 | #if 0 |
263 | // don't do this because it is just linking the text to a source | |
264 | // string which is unsafe. MB | |
265 | // | |
66a007fb | 266 | XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str()); |
b86de524 MB |
267 | #else |
268 | // do this instead... MB | |
269 | // | |
270 | XtVaSetValues( (Widget) m_mainWidget, | |
271 | XmNvalue, (char *)value.c_str(), | |
272 | NULL); | |
273 | #endif | |
66a007fb | 274 | |
dfc54541 | 275 | m_inSetValue = FALSE; |
4bb6408c JS |
276 | } |
277 | ||
278 | // Clipboard operations | |
279 | void wxTextCtrl::Copy() | |
280 | { | |
dfc54541 | 281 | XmTextCopy((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
282 | } |
283 | ||
284 | void wxTextCtrl::Cut() | |
285 | { | |
dfc54541 | 286 | XmTextCut((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
287 | } |
288 | ||
289 | void wxTextCtrl::Paste() | |
290 | { | |
dfc54541 | 291 | XmTextPaste((Widget) m_mainWidget); |
4bb6408c JS |
292 | } |
293 | ||
ca8b28f2 JS |
294 | bool wxTextCtrl::CanCopy() const |
295 | { | |
296 | // Can copy if there's a selection | |
297 | long from, to; | |
298 | GetSelection(& from, & to); | |
299 | return (from != to) ; | |
300 | } | |
301 | ||
302 | bool wxTextCtrl::CanCut() const | |
303 | { | |
304 | // Can cut if there's a selection | |
305 | long from, to; | |
306 | GetSelection(& from, & to); | |
dbf28859 | 307 | return (from != to) && (IsEditable()); |
ca8b28f2 JS |
308 | } |
309 | ||
310 | bool wxTextCtrl::CanPaste() const | |
311 | { | |
312 | return IsEditable() ; | |
313 | } | |
314 | ||
315 | // Undo/redo | |
316 | void wxTextCtrl::Undo() | |
317 | { | |
318 | // Not possible in Motif | |
319 | } | |
320 | ||
321 | void wxTextCtrl::Redo() | |
322 | { | |
323 | // Not possible in Motif | |
324 | } | |
325 | ||
326 | bool wxTextCtrl::CanUndo() const | |
327 | { | |
328 | // No Undo in Motif | |
329 | return FALSE; | |
330 | } | |
331 | ||
332 | bool wxTextCtrl::CanRedo() const | |
333 | { | |
334 | // No Redo in Motif | |
335 | return FALSE; | |
336 | } | |
337 | ||
338 | // If the return values from and to are the same, there is no | |
339 | // selection. | |
340 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
341 | { | |
342 | XmTextPosition left, right; | |
343 | ||
344 | XmTextGetSelectionPosition((Widget) m_mainWidget, & left, & right); | |
345 | ||
346 | *from = (long) left; | |
347 | *to = (long) right; | |
348 | } | |
349 | ||
350 | bool wxTextCtrl::IsEditable() const | |
351 | { | |
352 | return (XmTextGetEditable((Widget) m_mainWidget) != 0); | |
353 | } | |
354 | ||
4bb6408c JS |
355 | void wxTextCtrl::SetEditable(bool editable) |
356 | { | |
dfc54541 | 357 | XmTextSetEditable((Widget) m_mainWidget, (Boolean) editable); |
4bb6408c JS |
358 | } |
359 | ||
360 | void wxTextCtrl::SetInsertionPoint(long pos) | |
361 | { | |
dfc54541 | 362 | XmTextSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
363 | } |
364 | ||
365 | void wxTextCtrl::SetInsertionPointEnd() | |
366 | { | |
367 | long pos = GetLastPosition(); | |
368 | SetInsertionPoint(pos); | |
369 | } | |
370 | ||
371 | long wxTextCtrl::GetInsertionPoint() const | |
372 | { | |
dfc54541 | 373 | return (long) XmTextGetInsertionPosition ((Widget) m_mainWidget); |
4bb6408c JS |
374 | } |
375 | ||
376 | long wxTextCtrl::GetLastPosition() const | |
377 | { | |
dfc54541 | 378 | return (long) XmTextGetLastPosition ((Widget) m_mainWidget); |
4bb6408c JS |
379 | } |
380 | ||
381 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
382 | { | |
dfc54541 | 383 | XmTextReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
2d120f83 | 384 | (char*) (const char*) value); |
4bb6408c JS |
385 | } |
386 | ||
387 | void wxTextCtrl::Remove(long from, long to) | |
388 | { | |
dfc54541 | 389 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
66a007fb | 390 | (Time) 0); |
dfc54541 | 391 | XmTextRemove ((Widget) m_mainWidget); |
4bb6408c JS |
392 | } |
393 | ||
394 | void wxTextCtrl::SetSelection(long from, long to) | |
395 | { | |
dfc54541 | 396 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
66a007fb | 397 | (Time) 0); |
4bb6408c JS |
398 | } |
399 | ||
400 | bool wxTextCtrl::LoadFile(const wxString& file) | |
401 | { | |
402 | if (!wxFileExists(file)) | |
403 | return FALSE; | |
66a007fb | 404 | |
4bb6408c | 405 | m_fileName = file; |
66a007fb | 406 | |
4bb6408c | 407 | Clear(); |
66a007fb | 408 | |
1a3ac83f | 409 | Widget textWidget = (Widget) m_mainWidget; |
af111fc3 | 410 | FILE *fp = 0; |
66a007fb | 411 | |
1a3ac83f JS |
412 | struct stat statb; |
413 | if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG || | |
2d120f83 | 414 | !(fp = fopen ((char*) (const char*) file, "r"))) |
4bb6408c | 415 | { |
2d120f83 | 416 | return FALSE; |
1a3ac83f JS |
417 | } |
418 | else | |
419 | { | |
2d120f83 JS |
420 | long len = statb.st_size; |
421 | char *text; | |
422 | if (!(text = XtMalloc ((unsigned) (len + 1)))) | |
423 | { | |
424 | fclose (fp); | |
425 | return FALSE; | |
426 | } | |
427 | if (fread (text, sizeof (char), len, fp) != (size_t) len) | |
428 | { | |
429 | } | |
430 | fclose (fp); | |
66a007fb | 431 | |
2d120f83 JS |
432 | text[len] = 0; |
433 | XmTextSetString (textWidget, text); | |
434 | // m_textPosition = len; | |
435 | XtFree (text); | |
436 | m_modified = FALSE; | |
437 | return TRUE; | |
4bb6408c | 438 | } |
4bb6408c JS |
439 | } |
440 | ||
441 | // If file is null, try saved file name first | |
442 | // Returns TRUE if succeeds. | |
443 | bool wxTextCtrl::SaveFile(const wxString& file) | |
444 | { | |
445 | wxString theFile(file); | |
446 | if (theFile == "") | |
447 | theFile = m_fileName; | |
448 | if (theFile == "") | |
449 | return FALSE; | |
450 | m_fileName = theFile; | |
66a007fb | 451 | |
2d120f83 JS |
452 | Widget textWidget = (Widget) m_mainWidget; |
453 | FILE *fp; | |
66a007fb | 454 | |
2d120f83 | 455 | if (!(fp = fopen ((char*) (const char*) theFile, "w"))) |
1a3ac83f | 456 | { |
2d120f83 | 457 | return FALSE; |
1a3ac83f | 458 | } |
2d120f83 | 459 | else |
1a3ac83f | 460 | { |
2d120f83 JS |
461 | char *text = XmTextGetString (textWidget); |
462 | long len = XmTextGetLastPosition (textWidget); | |
66a007fb | 463 | |
2d120f83 JS |
464 | if (fwrite (text, sizeof (char), len, fp) != (size_t) len) |
465 | { | |
466 | // Did not write whole file | |
467 | } | |
468 | // Make sure newline terminates the file | |
469 | if (text[len - 1] != '\n') | |
470 | fputc ('\n', fp); | |
66a007fb | 471 | |
2d120f83 JS |
472 | fclose (fp); |
473 | XtFree (text); | |
474 | m_modified = FALSE; | |
475 | return TRUE; | |
1a3ac83f | 476 | } |
4bb6408c JS |
477 | } |
478 | ||
479 | void wxTextCtrl::WriteText(const wxString& text) | |
480 | { | |
dfc54541 JS |
481 | long textPosition = GetInsertionPoint() + strlen (text); |
482 | XmTextInsert ((Widget) m_mainWidget, GetInsertionPoint(), (char*) (const char*) text); | |
483 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); | |
484 | SetInsertionPoint(textPosition); | |
485 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
486 | m_modified = TRUE; | |
4bb6408c JS |
487 | } |
488 | ||
4dba84be JS |
489 | void wxTextCtrl::AppendText(const wxString& text) |
490 | { | |
491 | long textPosition = GetLastPosition() + strlen(text); | |
492 | XmTextInsert ((Widget) m_mainWidget, GetLastPosition(), (char*) (const char*) text); | |
493 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); | |
494 | SetInsertionPoint(textPosition); | |
495 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
496 | m_modified = TRUE; | |
497 | } | |
498 | ||
4bb6408c JS |
499 | void wxTextCtrl::Clear() |
500 | { | |
02e8b2f9 | 501 | XmTextSetString ((Widget) m_mainWidget, ""); |
02e8b2f9 | 502 | m_modified = FALSE; |
4bb6408c JS |
503 | } |
504 | ||
505 | bool wxTextCtrl::IsModified() const | |
506 | { | |
02e8b2f9 | 507 | return m_modified; |
4bb6408c JS |
508 | } |
509 | ||
510 | // Makes 'unmodified' | |
511 | void wxTextCtrl::DiscardEdits() | |
512 | { | |
dfc54541 | 513 | m_modified = FALSE; |
4bb6408c JS |
514 | } |
515 | ||
516 | int wxTextCtrl::GetNumberOfLines() const | |
517 | { | |
dfc54541 JS |
518 | // HIDEOUSLY inefficient, but we have no choice. |
519 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
520 | if (s) | |
521 | { | |
2d120f83 JS |
522 | long i = 0; |
523 | int currentLine = 0; | |
524 | bool finished = FALSE; | |
525 | while (!finished) | |
526 | { | |
527 | int ch = s[i]; | |
528 | if (ch == '\n') | |
529 | { | |
530 | currentLine++; | |
531 | i++; | |
532 | } | |
533 | else if (ch == 0) | |
534 | { | |
535 | finished = TRUE; | |
536 | } | |
537 | else | |
538 | i++; | |
539 | } | |
66a007fb | 540 | |
2d120f83 JS |
541 | XtFree (s); |
542 | return currentLine; | |
dfc54541 | 543 | } |
4bb6408c JS |
544 | return 0; |
545 | } | |
546 | ||
547 | long wxTextCtrl::XYToPosition(long x, long y) const | |
548 | { | |
dfc54541 | 549 | /* It seems, that there is a bug in some versions of the Motif library, |
2d120f83 JS |
550 | so the original wxWin-Code doesn't work. */ |
551 | /* | |
552 | Widget textWidget = (Widget) handle; | |
553 | return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y); | |
554 | */ | |
dfc54541 JS |
555 | /* Now a little workaround: */ |
556 | long r=0; | |
557 | for (int i=0; i<y; i++) r+=(GetLineLength(i)+1); | |
66a007fb | 558 | return r+x; |
4bb6408c JS |
559 | } |
560 | ||
813c20a6 | 561 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
4bb6408c | 562 | { |
dfc54541 JS |
563 | Position xx, yy; |
564 | XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy); | |
813c20a6 VZ |
565 | if ( x ) |
566 | *x = xx; | |
567 | if ( y ) | |
568 | *y = yy; | |
569 | ||
570 | return TRUE; | |
4bb6408c JS |
571 | } |
572 | ||
573 | void wxTextCtrl::ShowPosition(long pos) | |
574 | { | |
dfc54541 | 575 | XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
576 | } |
577 | ||
578 | int wxTextCtrl::GetLineLength(long lineNo) const | |
579 | { | |
dfc54541 JS |
580 | wxString str = GetLineText (lineNo); |
581 | return (int) str.Length(); | |
4bb6408c JS |
582 | } |
583 | ||
584 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
585 | { | |
dfc54541 JS |
586 | // HIDEOUSLY inefficient, but we have no choice. |
587 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
66a007fb | 588 | |
dfc54541 JS |
589 | if (s) |
590 | { | |
591 | wxString buf(""); | |
592 | long i; | |
593 | int currentLine = 0; | |
594 | for (i = 0; currentLine != lineNo && s[i]; i++ ) | |
2d120f83 JS |
595 | if (s[i] == '\n') |
596 | currentLine++; | |
597 | // Now get the text | |
598 | int j; | |
599 | for (j = 0; s[i] && s[i] != '\n'; i++, j++ ) | |
600 | buf += s[i]; | |
66a007fb | 601 | |
2d120f83 JS |
602 | XtFree(s); |
603 | return buf; | |
604 | } | |
605 | else | |
606 | return wxEmptyString; | |
4bb6408c JS |
607 | } |
608 | ||
609 | /* | |
2d120f83 JS |
610 | * Text item |
611 | */ | |
612 | ||
4bb6408c JS |
613 | void wxTextCtrl::Command(wxCommandEvent & event) |
614 | { | |
615 | SetValue (event.GetString()); | |
616 | ProcessCommand (event); | |
617 | } | |
618 | ||
619 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
620 | { | |
621 | // By default, load the first file into the text window. | |
622 | if (event.GetNumberOfFiles() > 0) | |
623 | { | |
624 | LoadFile(event.GetFiles()[0]); | |
625 | } | |
626 | } | |
627 | ||
02e8b2f9 JS |
628 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
629 | { | |
2d120f83 JS |
630 | // Indicates that we should generate a normal command, because |
631 | // we're letting default behaviour happen (otherwise it's vetoed | |
632 | // by virtue of overriding OnChar) | |
633 | m_processedDefault = TRUE; | |
66a007fb | 634 | |
2d120f83 | 635 | if (m_tempCallbackStruct) |
02e8b2f9 | 636 | { |
2d120f83 JS |
637 | XmTextVerifyCallbackStruct *textStruct = |
638 | (XmTextVerifyCallbackStruct *) m_tempCallbackStruct; | |
639 | textStruct->doit = True; | |
640 | if (isascii(event.m_keyCode) && (textStruct->text->length == 1)) | |
641 | { | |
642 | textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode); | |
643 | } | |
02e8b2f9 | 644 | } |
02e8b2f9 JS |
645 | } |
646 | ||
4b5f3fe6 | 647 | void wxTextCtrl::ChangeFont(bool keepOriginalSize) |
0d57be45 | 648 | { |
4b5f3fe6 | 649 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
650 | } |
651 | ||
652 | void wxTextCtrl::ChangeBackgroundColour() | |
653 | { | |
321db4b6 | 654 | wxWindow::ChangeBackgroundColour(); |
66a007fb | 655 | |
94b49b93 | 656 | /* TODO: should scrollbars be affected? Should probably have separate |
2d120f83 JS |
657 | * function to change them (by default, taken from wxSystemSettings) |
658 | */ | |
94b49b93 JS |
659 | if (m_windowStyle & wxTE_MULTILINE) |
660 | { | |
661 | Widget parent = XtParent ((Widget) m_mainWidget); | |
662 | Widget hsb, vsb; | |
66a007fb | 663 | |
94b49b93 | 664 | XtVaGetValues (parent, |
2d120f83 JS |
665 | XmNhorizontalScrollBar, &hsb, |
666 | XmNverticalScrollBar, &vsb, | |
667 | NULL); | |
a756f210 | 668 | wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
94b49b93 JS |
669 | if (hsb) |
670 | DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE); | |
671 | if (vsb) | |
672 | DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE); | |
66a007fb | 673 | |
94b49b93 JS |
674 | DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE); |
675 | } | |
0d57be45 JS |
676 | } |
677 | ||
678 | void wxTextCtrl::ChangeForegroundColour() | |
679 | { | |
321db4b6 | 680 | wxWindow::ChangeForegroundColour(); |
66a007fb | 681 | |
94b49b93 JS |
682 | if (m_windowStyle & wxTE_MULTILINE) |
683 | { | |
684 | Widget parent = XtParent ((Widget) m_mainWidget); | |
685 | Widget hsb, vsb; | |
66a007fb | 686 | |
94b49b93 | 687 | XtVaGetValues (parent, |
2d120f83 JS |
688 | XmNhorizontalScrollBar, &hsb, |
689 | XmNverticalScrollBar, &vsb, | |
690 | NULL); | |
66a007fb | 691 | |
2d120f83 JS |
692 | /* TODO: should scrollbars be affected? Should probably have separate |
693 | * function to change them (by default, taken from wxSystemSettings) | |
694 | if (hsb) | |
94b49b93 | 695 | DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour); |
2d120f83 | 696 | if (vsb) |
94b49b93 | 697 | DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour); |
2d120f83 | 698 | */ |
94b49b93 JS |
699 | DoChangeForegroundColour((WXWidget) parent, m_foregroundColour); |
700 | } | |
0d57be45 JS |
701 | } |
702 | ||
c27eab7e VZ |
703 | void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode) |
704 | { | |
705 | // we're in process of updating the text control | |
706 | m_tempCallbackStruct = wxcbs; | |
707 | ||
708 | XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs; | |
709 | ||
710 | wxKeyEvent event (wxEVT_CHAR); | |
711 | event.SetId(GetId()); | |
712 | event.m_keyCode = keycode; | |
713 | event.SetEventObject(this); | |
714 | ||
715 | // Only if wxTextCtrl::OnChar is called will this be set to True (and | |
716 | // the character passed through) | |
717 | cbs->doit = False; | |
718 | ||
719 | GetEventHandler()->ProcessEvent(event); | |
720 | ||
721 | if ( !InSetValue() && m_processedDefault ) | |
722 | { | |
723 | // Can generate a command | |
724 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
725 | commandEvent.SetEventObject(this); | |
726 | ProcessCommand(commandEvent); | |
727 | } | |
728 | ||
729 | // do it after the (user) event handlers processed the events because | |
730 | // otherwise GetValue() would return incorrect (not yet updated value) | |
731 | m_tempCallbackStruct = NULL; | |
732 | } | |
733 | ||
734 | // ---------------------------------------------------------------------------- | |
735 | // helpers and Motif callbacks | |
736 | // ---------------------------------------------------------------------------- | |
737 | ||
738 | static void MergeChangesIntoString(wxString& value, | |
739 | XmTextVerifyCallbackStruct *cbs) | |
740 | { | |
741 | /* _sm_ | |
742 | * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of | |
743 | * every event as a replace event. cbs->text->ptr gives the replacement | |
744 | * text, cbs->startPos gives the index of the first char affected by the | |
745 | * replace, and cbs->endPos gives the index one more than the last char | |
746 | * affected by the replace (startPos == endPos implies an empty range). | |
747 | * Hence, a deletion is represented by replacing all input text with a | |
748 | * blank string ("", *not* NULL!). A simple insertion that does not | |
749 | * overwrite any text has startPos == endPos. | |
750 | */ | |
751 | ||
752 | if ( !value ) | |
753 | { | |
754 | // easy case: the ol value was empty | |
755 | value = cbs->text->ptr; | |
756 | } | |
757 | else | |
758 | { | |
759 | // merge the changes into the value | |
760 | const char * const passwd = value; | |
761 | int len = value.length(); | |
762 | ||
763 | len += strlen(cbs->text->ptr) + 1; // + new text (if any) + NUL | |
764 | len -= cbs->endPos - cbs->startPos; // - text from affected region. | |
765 | ||
766 | char * newS = new char [len]; | |
767 | char * dest = newS, | |
768 | * insert = cbs->text->ptr; | |
769 | ||
770 | // Copy (old) text from passwd, up to the start posn of the change. | |
771 | int i; | |
772 | const char * p = passwd; | |
773 | for (i = 0; i < cbs->startPos; ++i) | |
774 | *dest++ = *p++; | |
775 | ||
776 | // Copy the text to be inserted). | |
777 | while (*insert) | |
778 | *dest++ = *insert++; | |
779 | ||
780 | // Finally, copy into newS any remaining text from passwd[endPos] on. | |
781 | for (p = passwd + cbs->endPos; *p; ) | |
782 | *dest++ = *p++; | |
783 | *dest = 0; | |
784 | ||
785 | value = newS; | |
786 | ||
787 | delete[] newS; | |
788 | } | |
789 | } | |
790 | ||
791 | static void | |
af111fc3 | 792 | wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr)) |
02e8b2f9 | 793 | { |
2d120f83 JS |
794 | if (!wxGetWindowFromTable(w)) |
795 | // Widget has been deleted! | |
796 | return; | |
66a007fb | 797 | |
2d120f83 JS |
798 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
799 | tw->SetModified(TRUE); | |
02e8b2f9 JS |
800 | } |
801 | ||
66a007fb | 802 | static void |
af111fc3 | 803 | wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs) |
02e8b2f9 | 804 | { |
dfc54541 JS |
805 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
806 | tw->m_processedDefault = FALSE; | |
66a007fb | 807 | |
c27eab7e VZ |
808 | // First, do some stuff if it's a password control: in this case, we need |
809 | // to store the string inside the class because GetValue() can't retrieve | |
810 | // it from the text ctrl. We do *not* do it in other circumstances because | |
811 | // it would double the amount of memory needed. | |
66a007fb | 812 | |
c27eab7e | 813 | if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD ) |
dfc54541 | 814 | { |
c27eab7e | 815 | MergeChangesIntoString(tw->m_value, cbs); |
66a007fb | 816 | |
c27eab7e | 817 | if ( cbs->text->length > 0 ) |
dfc54541 JS |
818 | { |
819 | int i; | |
820 | for (i = 0; i < cbs->text->length; ++i) | |
821 | cbs->text->ptr[i] = '*'; | |
c27eab7e | 822 | cbs->text->ptr[i] = '\0'; |
dfc54541 JS |
823 | } |
824 | } | |
66a007fb | 825 | |
c27eab7e VZ |
826 | // If we're already within an OnChar, return: probably a programmatic |
827 | // insertion. | |
dfc54541 JS |
828 | if (tw->m_tempCallbackStruct) |
829 | return; | |
66a007fb | 830 | |
dfc54541 JS |
831 | // Check for a backspace |
832 | if (cbs->startPos == (cbs->currInsert - 1)) | |
833 | { | |
c27eab7e | 834 | tw->DoSendEvents((void *)cbs, WXK_DELETE); |
66a007fb | 835 | |
dfc54541 JS |
836 | return; |
837 | } | |
66a007fb | 838 | |
c27eab7e | 839 | // Pasting operation: let it through without calling OnChar |
dfc54541 JS |
840 | if (cbs->text->length > 1) |
841 | return; | |
66a007fb | 842 | |
dfc54541 JS |
843 | // Something other than text |
844 | if (cbs->text->ptr == NULL) | |
845 | return; | |
66a007fb | 846 | |
c27eab7e VZ |
847 | // normal key press |
848 | char ch = cbs->text->ptr[0]; | |
849 | tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch); | |
02e8b2f9 JS |
850 | } |
851 | ||
66a007fb | 852 | static void |
af111fc3 | 853 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 854 | { |
2d120f83 JS |
855 | if (!wxGetWindowFromTable(w)) |
856 | return; | |
66a007fb | 857 | |
2d120f83 JS |
858 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
859 | wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId()); | |
860 | event.SetEventObject(tw); | |
861 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 JS |
862 | } |
863 | ||
66a007fb | 864 | static void |
af111fc3 | 865 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 866 | { |
2d120f83 JS |
867 | if (!wxGetWindowFromTable(w)) |
868 | return; | |
66a007fb | 869 | |
2d120f83 JS |
870 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
871 | wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId()); | |
872 | event.SetEventObject(tw); | |
873 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 | 874 | } |
dfc54541 JS |
875 | |
876 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, | |
af111fc3 | 877 | XmAnyCallbackStruct *WXUNUSED(ptr)) |
dfc54541 | 878 | { |
2d120f83 JS |
879 | if (!wxGetWindowFromTable(w)) |
880 | return; | |
66a007fb | 881 | |
2d120f83 | 882 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
66a007fb | 883 | |
2d120f83 JS |
884 | if (tw->InSetValue()) |
885 | return; | |
66a007fb | 886 | |
2d120f83 JS |
887 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER); |
888 | event.SetId(tw->GetId()); | |
889 | event.SetEventObject(tw); | |
890 | tw->ProcessCommand(event); | |
dfc54541 | 891 | } |
c27eab7e | 892 | |
e702ff0f JS |
893 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
894 | { | |
895 | Cut(); | |
896 | } | |
897 | ||
898 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
899 | { | |
900 | Copy(); | |
901 | } | |
902 | ||
903 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
904 | { | |
905 | Paste(); | |
906 | } | |
907 | ||
908 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
909 | { | |
910 | Undo(); | |
911 | } | |
912 | ||
913 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
914 | { | |
915 | Redo(); | |
916 | } | |
917 | ||
918 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
919 | { | |
920 | event.Enable( CanCut() ); | |
921 | } | |
922 | ||
923 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
924 | { | |
925 | event.Enable( CanCopy() ); | |
926 | } | |
927 | ||
928 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
929 | { | |
930 | event.Enable( CanPaste() ); | |
931 | } | |
932 | ||
933 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
934 | { | |
935 | event.Enable( CanUndo() ); | |
936 | } | |
937 | ||
938 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
939 | { | |
940 | event.Enable( CanRedo() ); | |
941 | } |