X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/dfc5454127ac2195e10deebb216d82c674e757df..184b5d99a5382cd7a19888c85aff11f8a21af2f6:/src/motif/textctrl.cpp diff --git a/src/motif/textctrl.cpp b/src/motif/textctrl.cpp index d62cac41f6..b28bdf9ef5 100644 --- a/src/motif/textctrl.cpp +++ b/src/motif/textctrl.cpp @@ -67,7 +67,6 @@ wxTextCtrl::wxTextCtrl() m_tempCallbackStruct = (void*) NULL; m_modified = FALSE; m_processedDefault = FALSE; - m_inSetValue = FALSE; } bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, @@ -81,7 +80,8 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, m_modified = FALSE; m_processedDefault = FALSE; m_fileName = ""; - m_inSetValue = FALSE; + m_backgroundColour = parent->GetBackgroundColour(); + m_foregroundColour = parent->GetForegroundColour(); SetName(name); SetValidator(validator); @@ -143,11 +143,13 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, XtAddCallback((Widget) m_mainWidget, XmNlosingFocusCallback, (XtCallbackProc)wxTextWindowLoseFocusProc, (XtPointer)this); + m_windowFont = parent->GetFont(); + ChangeFont(FALSE); + SetCanAddEventHandler(TRUE); AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); - SetFont(* parent->GetFont()); - ChangeColour(m_mainWidget); + ChangeBackgroundColour(); return TRUE; } @@ -179,7 +181,9 @@ wxString wxTextCtrl::GetValue() const void wxTextCtrl::SetValue(const wxString& value) { - wxASSERT_MSG( (!value.IsNull()), "Must not pass a null string to wxTextCtrl::SetValue." ) ; + // This assert is wrong -- means that you can't set an empty + // string (IsNull == IsEmpty). + // wxASSERT_MSG( (!value.IsNull()), "Must not pass a null string to wxTextCtrl::SetValue." ) ; m_inSetValue = TRUE; XmTextSetString ((Widget) m_mainWidget, (char*) (const char*) value); @@ -257,37 +261,36 @@ bool wxTextCtrl::LoadFile(const wxString& file) Clear(); - ifstream input((char*) (const char*) file, ios::nocreate | ios::in); + Widget textWidget = (Widget) m_mainWidget; + FILE *fp; - if (!input.bad()) + struct stat statb; + if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG || + !(fp = fopen ((char*) (const char*) file, "r"))) { - struct stat stat_buf; - if (stat(file, &stat_buf) < 0) - return FALSE; - // This may need to be a bigger buffer than the file size suggests, - // if it's a UNIX file. Give it an extra 1000 just in case. - char *tmp_buffer = (char*)malloc((size_t)(stat_buf.st_size+1+1000)); - long no_lines = 0; - long pos = 0; - while (!input.eof() && input.peek() != EOF) - { - input.getline(wxBuffer, 500); - int len = strlen(wxBuffer); - wxBuffer[len] = 13; - wxBuffer[len+1] = 10; - wxBuffer[len+2] = 0; - strcpy(tmp_buffer+pos, wxBuffer); - pos += strlen(wxBuffer); - no_lines++; - } - - // TODO add line - - free(tmp_buffer); - - return TRUE; + return FALSE; + } + else + { + long len = statb.st_size; + char *text; + if (!(text = XtMalloc ((unsigned) (len + 1)))) + { + fclose (fp); + return FALSE; + } + if (fread (text, sizeof (char), len, fp) != (size_t) len) + { + } + fclose (fp); + + text[len] = 0; + XmTextSetString (textWidget, text); + // m_textPosition = len; + XtFree (text); + m_modified = FALSE; + return TRUE; } - return FALSE; } // If file is null, try saved file name first @@ -301,13 +304,31 @@ bool wxTextCtrl::SaveFile(const wxString& file) return FALSE; m_fileName = theFile; - ofstream output((char*) (const char*) theFile); - if (output.bad()) - return FALSE; + Widget textWidget = (Widget) m_mainWidget; + FILE *fp; - // TODO get and save text + if (!(fp = fopen ((char*) (const char*) theFile, "w"))) + { + return FALSE; + } + else + { + char *text = XmTextGetString (textWidget); + long len = XmTextGetLastPosition (textWidget); - return FALSE; + if (fwrite (text, sizeof (char), len, fp) != (size_t) len) + { + // Did not write whole file + } + // Make sure newline terminates the file + if (text[len - 1] != '\n') + fputc ('\n', fp); + + fclose (fp); + XtFree (text); + m_modified = FALSE; + return TRUE; + } } void wxTextCtrl::WriteText(const wxString& text) @@ -619,6 +640,57 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) } } +void wxTextCtrl::ChangeFont(bool keepOriginalSize) +{ + wxWindow::ChangeFont(keepOriginalSize); +} + +void wxTextCtrl::ChangeBackgroundColour() +{ + wxWindow::ChangeBackgroundColour(); + + Widget parent = XtParent ((Widget) m_mainWidget); + Widget hsb, vsb; + + XtVaGetValues (parent, + XmNhorizontalScrollBar, &hsb, + XmNverticalScrollBar, &vsb, + NULL); + + /* TODO: should scrollbars be affected? Should probably have separate + * function to change them (by default, taken from wxSystemSettings) + if (hsb) + DoChangeBackgroundColour((WXWidget) hsb, m_backgroundColour, TRUE); + if (vsb) + DoChangeBackgroundColour((WXWidget) vsb, m_backgroundColour, TRUE); + */ + + DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE); +} + +void wxTextCtrl::ChangeForegroundColour() +{ + wxWindow::ChangeForegroundColour(); + + + Widget parent = XtParent ((Widget) m_mainWidget); + Widget hsb, vsb; + + XtVaGetValues (parent, + XmNhorizontalScrollBar, &hsb, + XmNverticalScrollBar, &vsb, + NULL); + + /* TODO: should scrollbars be affected? Should probably have separate + * function to change them (by default, taken from wxSystemSettings) + if (hsb) + DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour); + if (vsb) + DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour); + */ + DoChangeForegroundColour((WXWidget) parent, m_foregroundColour); +} + static void wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer ptr) { if (!wxGetWindowFromTable(w)) @@ -718,7 +790,7 @@ wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStru tw->m_tempCallbackStruct = NULL; - if (tw->m_inSetValue) + if (tw->InSetValue()) return; if (tw->m_processedDefault) @@ -757,7 +829,7 @@ wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStru tw->m_tempCallbackStruct = NULL; - if (tw->m_inSetValue) + if (tw->InSetValue()) return; if (tw->m_processedDefault) @@ -810,7 +882,7 @@ static void wxTextWindowActivateProc(Widget w, XtPointer clientData, } */ - if (tw->m_inSetValue) + if (tw->InSetValue()) return; wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER);