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