]>
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 | { | |
44d130a3 MB |
396 | if( to == -1 ) |
397 | to = GetLastPosition(); | |
398 | ||
dfc54541 | 399 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
66a007fb | 400 | (Time) 0); |
4bb6408c JS |
401 | } |
402 | ||
403 | bool wxTextCtrl::LoadFile(const wxString& file) | |
404 | { | |
405 | if (!wxFileExists(file)) | |
406 | return FALSE; | |
66a007fb | 407 | |
4bb6408c | 408 | m_fileName = file; |
66a007fb | 409 | |
4bb6408c | 410 | Clear(); |
66a007fb | 411 | |
1a3ac83f | 412 | Widget textWidget = (Widget) m_mainWidget; |
af111fc3 | 413 | FILE *fp = 0; |
66a007fb | 414 | |
1a3ac83f JS |
415 | struct stat statb; |
416 | if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG || | |
2d120f83 | 417 | !(fp = fopen ((char*) (const char*) file, "r"))) |
4bb6408c | 418 | { |
2d120f83 | 419 | return FALSE; |
1a3ac83f JS |
420 | } |
421 | else | |
422 | { | |
2d120f83 JS |
423 | long len = statb.st_size; |
424 | char *text; | |
425 | if (!(text = XtMalloc ((unsigned) (len + 1)))) | |
426 | { | |
427 | fclose (fp); | |
428 | return FALSE; | |
429 | } | |
430 | if (fread (text, sizeof (char), len, fp) != (size_t) len) | |
431 | { | |
432 | } | |
433 | fclose (fp); | |
66a007fb | 434 | |
2d120f83 JS |
435 | text[len] = 0; |
436 | XmTextSetString (textWidget, text); | |
437 | // m_textPosition = len; | |
438 | XtFree (text); | |
439 | m_modified = FALSE; | |
440 | return TRUE; | |
4bb6408c | 441 | } |
4bb6408c JS |
442 | } |
443 | ||
444 | // If file is null, try saved file name first | |
445 | // Returns TRUE if succeeds. | |
446 | bool wxTextCtrl::SaveFile(const wxString& file) | |
447 | { | |
448 | wxString theFile(file); | |
449 | if (theFile == "") | |
450 | theFile = m_fileName; | |
451 | if (theFile == "") | |
452 | return FALSE; | |
453 | m_fileName = theFile; | |
66a007fb | 454 | |
2d120f83 JS |
455 | Widget textWidget = (Widget) m_mainWidget; |
456 | FILE *fp; | |
66a007fb | 457 | |
2d120f83 | 458 | if (!(fp = fopen ((char*) (const char*) theFile, "w"))) |
1a3ac83f | 459 | { |
2d120f83 | 460 | return FALSE; |
1a3ac83f | 461 | } |
2d120f83 | 462 | else |
1a3ac83f | 463 | { |
2d120f83 JS |
464 | char *text = XmTextGetString (textWidget); |
465 | long len = XmTextGetLastPosition (textWidget); | |
66a007fb | 466 | |
2d120f83 JS |
467 | if (fwrite (text, sizeof (char), len, fp) != (size_t) len) |
468 | { | |
469 | // Did not write whole file | |
470 | } | |
471 | // Make sure newline terminates the file | |
472 | if (text[len - 1] != '\n') | |
473 | fputc ('\n', fp); | |
66a007fb | 474 | |
2d120f83 JS |
475 | fclose (fp); |
476 | XtFree (text); | |
477 | m_modified = FALSE; | |
478 | return TRUE; | |
1a3ac83f | 479 | } |
4bb6408c JS |
480 | } |
481 | ||
482 | void wxTextCtrl::WriteText(const wxString& text) | |
483 | { | |
dfc54541 JS |
484 | long textPosition = GetInsertionPoint() + strlen (text); |
485 | XmTextInsert ((Widget) m_mainWidget, GetInsertionPoint(), (char*) (const char*) text); | |
486 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); | |
487 | SetInsertionPoint(textPosition); | |
488 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
489 | m_modified = TRUE; | |
4bb6408c JS |
490 | } |
491 | ||
4dba84be JS |
492 | void wxTextCtrl::AppendText(const wxString& text) |
493 | { | |
494 | long textPosition = GetLastPosition() + strlen(text); | |
495 | XmTextInsert ((Widget) m_mainWidget, GetLastPosition(), (char*) (const char*) text); | |
496 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); | |
497 | SetInsertionPoint(textPosition); | |
498 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
499 | m_modified = TRUE; | |
500 | } | |
501 | ||
4bb6408c JS |
502 | void wxTextCtrl::Clear() |
503 | { | |
02e8b2f9 | 504 | XmTextSetString ((Widget) m_mainWidget, ""); |
02e8b2f9 | 505 | m_modified = FALSE; |
4bb6408c JS |
506 | } |
507 | ||
508 | bool wxTextCtrl::IsModified() const | |
509 | { | |
02e8b2f9 | 510 | return m_modified; |
4bb6408c JS |
511 | } |
512 | ||
513 | // Makes 'unmodified' | |
514 | void wxTextCtrl::DiscardEdits() | |
515 | { | |
dfc54541 | 516 | m_modified = FALSE; |
4bb6408c JS |
517 | } |
518 | ||
519 | int wxTextCtrl::GetNumberOfLines() const | |
520 | { | |
dfc54541 JS |
521 | // HIDEOUSLY inefficient, but we have no choice. |
522 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
523 | if (s) | |
524 | { | |
2d120f83 JS |
525 | long i = 0; |
526 | int currentLine = 0; | |
527 | bool finished = FALSE; | |
528 | while (!finished) | |
529 | { | |
530 | int ch = s[i]; | |
531 | if (ch == '\n') | |
532 | { | |
533 | currentLine++; | |
534 | i++; | |
535 | } | |
536 | else if (ch == 0) | |
537 | { | |
538 | finished = TRUE; | |
539 | } | |
540 | else | |
541 | i++; | |
542 | } | |
66a007fb | 543 | |
2d120f83 JS |
544 | XtFree (s); |
545 | return currentLine; | |
dfc54541 | 546 | } |
4bb6408c JS |
547 | return 0; |
548 | } | |
549 | ||
550 | long wxTextCtrl::XYToPosition(long x, long y) const | |
551 | { | |
dfc54541 | 552 | /* It seems, that there is a bug in some versions of the Motif library, |
2d120f83 JS |
553 | so the original wxWin-Code doesn't work. */ |
554 | /* | |
555 | Widget textWidget = (Widget) handle; | |
556 | return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y); | |
557 | */ | |
dfc54541 JS |
558 | /* Now a little workaround: */ |
559 | long r=0; | |
560 | for (int i=0; i<y; i++) r+=(GetLineLength(i)+1); | |
66a007fb | 561 | return r+x; |
4bb6408c JS |
562 | } |
563 | ||
813c20a6 | 564 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
4bb6408c | 565 | { |
dfc54541 JS |
566 | Position xx, yy; |
567 | XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy); | |
813c20a6 VZ |
568 | if ( x ) |
569 | *x = xx; | |
570 | if ( y ) | |
571 | *y = yy; | |
572 | ||
573 | return TRUE; | |
4bb6408c JS |
574 | } |
575 | ||
576 | void wxTextCtrl::ShowPosition(long pos) | |
577 | { | |
dfc54541 | 578 | XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
579 | } |
580 | ||
581 | int wxTextCtrl::GetLineLength(long lineNo) const | |
582 | { | |
dfc54541 JS |
583 | wxString str = GetLineText (lineNo); |
584 | return (int) str.Length(); | |
4bb6408c JS |
585 | } |
586 | ||
587 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
588 | { | |
dfc54541 JS |
589 | // HIDEOUSLY inefficient, but we have no choice. |
590 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
66a007fb | 591 | |
dfc54541 JS |
592 | if (s) |
593 | { | |
594 | wxString buf(""); | |
595 | long i; | |
596 | int currentLine = 0; | |
597 | for (i = 0; currentLine != lineNo && s[i]; i++ ) | |
2d120f83 JS |
598 | if (s[i] == '\n') |
599 | currentLine++; | |
600 | // Now get the text | |
601 | int j; | |
602 | for (j = 0; s[i] && s[i] != '\n'; i++, j++ ) | |
603 | buf += s[i]; | |
66a007fb | 604 | |
2d120f83 JS |
605 | XtFree(s); |
606 | return buf; | |
607 | } | |
608 | else | |
609 | return wxEmptyString; | |
4bb6408c JS |
610 | } |
611 | ||
612 | /* | |
2d120f83 JS |
613 | * Text item |
614 | */ | |
615 | ||
4bb6408c JS |
616 | void wxTextCtrl::Command(wxCommandEvent & event) |
617 | { | |
618 | SetValue (event.GetString()); | |
619 | ProcessCommand (event); | |
620 | } | |
621 | ||
622 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
623 | { | |
624 | // By default, load the first file into the text window. | |
625 | if (event.GetNumberOfFiles() > 0) | |
626 | { | |
627 | LoadFile(event.GetFiles()[0]); | |
628 | } | |
629 | } | |
630 | ||
02e8b2f9 JS |
631 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
632 | { | |
2d120f83 JS |
633 | // Indicates that we should generate a normal command, because |
634 | // we're letting default behaviour happen (otherwise it's vetoed | |
635 | // by virtue of overriding OnChar) | |
636 | m_processedDefault = TRUE; | |
66a007fb | 637 | |
2d120f83 | 638 | if (m_tempCallbackStruct) |
02e8b2f9 | 639 | { |
2d120f83 JS |
640 | XmTextVerifyCallbackStruct *textStruct = |
641 | (XmTextVerifyCallbackStruct *) m_tempCallbackStruct; | |
642 | textStruct->doit = True; | |
643 | if (isascii(event.m_keyCode) && (textStruct->text->length == 1)) | |
644 | { | |
645 | textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode); | |
646 | } | |
02e8b2f9 | 647 | } |
02e8b2f9 JS |
648 | } |
649 | ||
4b5f3fe6 | 650 | void wxTextCtrl::ChangeFont(bool keepOriginalSize) |
0d57be45 | 651 | { |
4b5f3fe6 | 652 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
653 | } |
654 | ||
655 | void wxTextCtrl::ChangeBackgroundColour() | |
656 | { | |
321db4b6 | 657 | wxWindow::ChangeBackgroundColour(); |
66a007fb | 658 | |
94b49b93 | 659 | /* TODO: should scrollbars be affected? Should probably have separate |
2d120f83 JS |
660 | * function to change them (by default, taken from wxSystemSettings) |
661 | */ | |
94b49b93 JS |
662 | if (m_windowStyle & wxTE_MULTILINE) |
663 | { | |
664 | Widget parent = XtParent ((Widget) m_mainWidget); | |
665 | Widget hsb, vsb; | |
66a007fb | 666 | |
94b49b93 | 667 | XtVaGetValues (parent, |
2d120f83 JS |
668 | XmNhorizontalScrollBar, &hsb, |
669 | XmNverticalScrollBar, &vsb, | |
670 | NULL); | |
a756f210 | 671 | wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
94b49b93 JS |
672 | if (hsb) |
673 | DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE); | |
674 | if (vsb) | |
675 | DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE); | |
66a007fb | 676 | |
94b49b93 JS |
677 | DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE); |
678 | } | |
0d57be45 JS |
679 | } |
680 | ||
681 | void wxTextCtrl::ChangeForegroundColour() | |
682 | { | |
321db4b6 | 683 | wxWindow::ChangeForegroundColour(); |
66a007fb | 684 | |
94b49b93 JS |
685 | if (m_windowStyle & wxTE_MULTILINE) |
686 | { | |
687 | Widget parent = XtParent ((Widget) m_mainWidget); | |
688 | Widget hsb, vsb; | |
66a007fb | 689 | |
94b49b93 | 690 | XtVaGetValues (parent, |
2d120f83 JS |
691 | XmNhorizontalScrollBar, &hsb, |
692 | XmNverticalScrollBar, &vsb, | |
693 | NULL); | |
66a007fb | 694 | |
2d120f83 JS |
695 | /* TODO: should scrollbars be affected? Should probably have separate |
696 | * function to change them (by default, taken from wxSystemSettings) | |
697 | if (hsb) | |
94b49b93 | 698 | DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour); |
2d120f83 | 699 | if (vsb) |
94b49b93 | 700 | DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour); |
2d120f83 | 701 | */ |
94b49b93 JS |
702 | DoChangeForegroundColour((WXWidget) parent, m_foregroundColour); |
703 | } | |
0d57be45 JS |
704 | } |
705 | ||
c27eab7e VZ |
706 | void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode) |
707 | { | |
708 | // we're in process of updating the text control | |
709 | m_tempCallbackStruct = wxcbs; | |
710 | ||
711 | XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs; | |
712 | ||
713 | wxKeyEvent event (wxEVT_CHAR); | |
714 | event.SetId(GetId()); | |
715 | event.m_keyCode = keycode; | |
716 | event.SetEventObject(this); | |
717 | ||
718 | // Only if wxTextCtrl::OnChar is called will this be set to True (and | |
719 | // the character passed through) | |
720 | cbs->doit = False; | |
721 | ||
722 | GetEventHandler()->ProcessEvent(event); | |
723 | ||
724 | if ( !InSetValue() && m_processedDefault ) | |
725 | { | |
726 | // Can generate a command | |
727 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
728 | commandEvent.SetEventObject(this); | |
729 | ProcessCommand(commandEvent); | |
730 | } | |
731 | ||
732 | // do it after the (user) event handlers processed the events because | |
733 | // otherwise GetValue() would return incorrect (not yet updated value) | |
734 | m_tempCallbackStruct = NULL; | |
735 | } | |
736 | ||
737 | // ---------------------------------------------------------------------------- | |
738 | // helpers and Motif callbacks | |
739 | // ---------------------------------------------------------------------------- | |
740 | ||
741 | static void MergeChangesIntoString(wxString& value, | |
742 | XmTextVerifyCallbackStruct *cbs) | |
743 | { | |
744 | /* _sm_ | |
745 | * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of | |
746 | * every event as a replace event. cbs->text->ptr gives the replacement | |
747 | * text, cbs->startPos gives the index of the first char affected by the | |
748 | * replace, and cbs->endPos gives the index one more than the last char | |
749 | * affected by the replace (startPos == endPos implies an empty range). | |
750 | * Hence, a deletion is represented by replacing all input text with a | |
751 | * blank string ("", *not* NULL!). A simple insertion that does not | |
752 | * overwrite any text has startPos == endPos. | |
753 | */ | |
754 | ||
755 | if ( !value ) | |
756 | { | |
757 | // easy case: the ol value was empty | |
758 | value = cbs->text->ptr; | |
759 | } | |
760 | else | |
761 | { | |
762 | // merge the changes into the value | |
763 | const char * const passwd = value; | |
764 | int len = value.length(); | |
765 | ||
44d130a3 MB |
766 | len += ( cbs->text->ptr ? |
767 | strlen(cbs->text->ptr) : | |
768 | 0 ) + 1; // + new text (if any) + NUL | |
c27eab7e VZ |
769 | len -= cbs->endPos - cbs->startPos; // - text from affected region. |
770 | ||
771 | char * newS = new char [len]; | |
772 | char * dest = newS, | |
773 | * insert = cbs->text->ptr; | |
774 | ||
775 | // Copy (old) text from passwd, up to the start posn of the change. | |
776 | int i; | |
777 | const char * p = passwd; | |
778 | for (i = 0; i < cbs->startPos; ++i) | |
779 | *dest++ = *p++; | |
780 | ||
781 | // Copy the text to be inserted). | |
44d130a3 MB |
782 | if (insert) |
783 | while (*insert) | |
784 | *dest++ = *insert++; | |
c27eab7e VZ |
785 | |
786 | // Finally, copy into newS any remaining text from passwd[endPos] on. | |
787 | for (p = passwd + cbs->endPos; *p; ) | |
788 | *dest++ = *p++; | |
789 | *dest = 0; | |
790 | ||
791 | value = newS; | |
792 | ||
793 | delete[] newS; | |
794 | } | |
795 | } | |
796 | ||
797 | static void | |
af111fc3 | 798 | wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr)) |
02e8b2f9 | 799 | { |
2d120f83 JS |
800 | if (!wxGetWindowFromTable(w)) |
801 | // Widget has been deleted! | |
802 | return; | |
66a007fb | 803 | |
2d120f83 JS |
804 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
805 | tw->SetModified(TRUE); | |
02e8b2f9 JS |
806 | } |
807 | ||
66a007fb | 808 | static void |
af111fc3 | 809 | wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs) |
02e8b2f9 | 810 | { |
dfc54541 JS |
811 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
812 | tw->m_processedDefault = FALSE; | |
66a007fb | 813 | |
c27eab7e VZ |
814 | // First, do some stuff if it's a password control: in this case, we need |
815 | // to store the string inside the class because GetValue() can't retrieve | |
816 | // it from the text ctrl. We do *not* do it in other circumstances because | |
817 | // it would double the amount of memory needed. | |
66a007fb | 818 | |
c27eab7e | 819 | if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD ) |
dfc54541 | 820 | { |
c27eab7e | 821 | MergeChangesIntoString(tw->m_value, cbs); |
66a007fb | 822 | |
c27eab7e | 823 | if ( cbs->text->length > 0 ) |
dfc54541 JS |
824 | { |
825 | int i; | |
826 | for (i = 0; i < cbs->text->length; ++i) | |
827 | cbs->text->ptr[i] = '*'; | |
c27eab7e | 828 | cbs->text->ptr[i] = '\0'; |
dfc54541 JS |
829 | } |
830 | } | |
66a007fb | 831 | |
c27eab7e VZ |
832 | // If we're already within an OnChar, return: probably a programmatic |
833 | // insertion. | |
dfc54541 JS |
834 | if (tw->m_tempCallbackStruct) |
835 | return; | |
66a007fb | 836 | |
dfc54541 JS |
837 | // Check for a backspace |
838 | if (cbs->startPos == (cbs->currInsert - 1)) | |
839 | { | |
c27eab7e | 840 | tw->DoSendEvents((void *)cbs, WXK_DELETE); |
66a007fb | 841 | |
dfc54541 JS |
842 | return; |
843 | } | |
66a007fb | 844 | |
c27eab7e | 845 | // Pasting operation: let it through without calling OnChar |
dfc54541 JS |
846 | if (cbs->text->length > 1) |
847 | return; | |
66a007fb | 848 | |
dfc54541 JS |
849 | // Something other than text |
850 | if (cbs->text->ptr == NULL) | |
851 | return; | |
66a007fb | 852 | |
c27eab7e VZ |
853 | // normal key press |
854 | char ch = cbs->text->ptr[0]; | |
855 | tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch); | |
02e8b2f9 JS |
856 | } |
857 | ||
66a007fb | 858 | static void |
af111fc3 | 859 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 860 | { |
2d120f83 JS |
861 | if (!wxGetWindowFromTable(w)) |
862 | return; | |
66a007fb | 863 | |
2d120f83 JS |
864 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
865 | wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId()); | |
866 | event.SetEventObject(tw); | |
867 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 JS |
868 | } |
869 | ||
66a007fb | 870 | static void |
af111fc3 | 871 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 872 | { |
2d120f83 JS |
873 | if (!wxGetWindowFromTable(w)) |
874 | return; | |
66a007fb | 875 | |
2d120f83 JS |
876 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
877 | wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId()); | |
878 | event.SetEventObject(tw); | |
879 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 | 880 | } |
dfc54541 JS |
881 | |
882 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, | |
af111fc3 | 883 | XmAnyCallbackStruct *WXUNUSED(ptr)) |
dfc54541 | 884 | { |
2d120f83 JS |
885 | if (!wxGetWindowFromTable(w)) |
886 | return; | |
66a007fb | 887 | |
2d120f83 | 888 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
66a007fb | 889 | |
2d120f83 JS |
890 | if (tw->InSetValue()) |
891 | return; | |
66a007fb | 892 | |
2d120f83 JS |
893 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER); |
894 | event.SetId(tw->GetId()); | |
895 | event.SetEventObject(tw); | |
896 | tw->ProcessCommand(event); | |
dfc54541 | 897 | } |
c27eab7e | 898 | |
e702ff0f JS |
899 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
900 | { | |
901 | Cut(); | |
902 | } | |
903 | ||
904 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
905 | { | |
906 | Copy(); | |
907 | } | |
908 | ||
909 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
910 | { | |
911 | Paste(); | |
912 | } | |
913 | ||
914 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
915 | { | |
916 | Undo(); | |
917 | } | |
918 | ||
919 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
920 | { | |
921 | Redo(); | |
922 | } | |
923 | ||
924 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
925 | { | |
926 | event.Enable( CanCut() ); | |
927 | } | |
928 | ||
929 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
930 | { | |
931 | event.Enable( CanCopy() ); | |
932 | } | |
933 | ||
934 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
935 | { | |
936 | event.Enable( CanPaste() ); | |
937 | } | |
938 | ||
939 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
940 | { | |
941 | event.Enable( CanUndo() ); | |
942 | } | |
943 | ||
944 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
945 | { | |
946 | event.Enable( CanRedo() ); | |
947 | } |