]> git.saurik.com Git - wxWidgets.git/blobdiff - src/motif/textctrl.cpp
1. added native wxMessageDialog implementation for GTK+2
[wxWidgets.git] / src / motif / textctrl.cpp
index ce98355f14467e143f7180b3a490fe9bca16cd5c..6512988d93bd2114513d2efe905a9522be082fb7 100644 (file)
 #define XtParent XTPARENT
 #endif
 
+#include "wx/defs.h"
+
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <fstream.h>
 #include <ctype.h>
 
 #include "wx/textctrl.h"
@@ -106,24 +107,14 @@ bool wxTextCtrl::Create(wxWindow *parent,
                         const wxValidator& validator,
                         const wxString& name)
 {
+    if( !CreateControl( parent, id, pos, size, style, validator, name ) )
+        return false;
+
     m_tempCallbackStruct = (void*) NULL;
     m_modified = FALSE;
     m_processedDefault = FALSE;
-    //    m_backgroundColour = parent->GetBackgroundColour();
-    m_backgroundColour = * wxWHITE;
-    m_foregroundColour = parent->GetForegroundColour();
-
-    SetName(name);
-    SetValidator(validator);
-    if (parent)
-        parent->AddChild(this);
 
-    m_windowStyle = style;
-
-    if ( id == -1 )
-        m_windowId = (int)NewControlId();
-    else
-        m_windowId = id;
+    m_backgroundColour = *wxWHITE;
 
     Widget parentWidget = (Widget) parent->GetClientWidget();
 
@@ -179,20 +170,13 @@ bool wxTextCtrl::Create(wxWindow *parent,
                       NULL);
     }
 
-    if ( !!value )
+    if ( !value.empty() )
     {
-#if 0
-        // don't do this because it is just linking the text to a source
-        // string which is unsafe. MB
-        //
-        XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str());
-#else
         // do this instead... MB
         //
         XtVaSetValues( (Widget) m_mainWidget,
                        XmNvalue, (char *)value.c_str(),
                        NULL);
-#endif
     }
     
     // install callbacks
@@ -207,11 +191,15 @@ bool wxTextCtrl::Create(wxWindow *parent,
     XtAddCallback((Widget) m_mainWidget, XmNlosingFocusCallback, (XtCallbackProc)wxTextWindowLoseFocusProc, (XtPointer)this);
 
     // font
-    m_font = parent->GetFont();
     ChangeFont(FALSE);
 
+    wxSize best = GetBestSize();
+    if( size.x != -1 ) best.x = size.x;
+    if( size.y != -1 ) best.y = size.y;
+
     SetCanAddEventHandler(TRUE);
-    AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
+    AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
+                  pos.x, pos.y, best.x, best.y);
 
     ChangeBackgroundColour();
 
@@ -259,18 +247,11 @@ void wxTextCtrl::SetValue(const wxString& value)
 {
     m_inSetValue = TRUE;
 
-#if 0
-    // don't do this because it is just linking the text to a source
-    // string which is unsafe. MB
-    //
-    XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str());
-#else
     // do this instead... MB
     //
     XtVaSetValues( (Widget) m_mainWidget,
                    XmNvalue, (char *)value.c_str(),
                    NULL);
-#endif
 
     m_inSetValue = FALSE;
 }
@@ -304,7 +285,7 @@ bool wxTextCtrl::CanCut() const
     // Can cut if there's a selection
     long from, to;
     GetSelection(& from, & to);
-    return (from != to) ;
+    return (from != to) && (IsEditable());
 }
 
 bool wxTextCtrl::CanPaste() const
@@ -393,6 +374,9 @@ void wxTextCtrl::Remove(long from, long to)
 
 void wxTextCtrl::SetSelection(long from, long to)
 {
+    if( to == -1 )
+        to = GetLastPosition();
+
     XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
                       (Time) 0);
 }
@@ -665,7 +649,7 @@ void wxTextCtrl::ChangeBackgroundColour()
             XmNhorizontalScrollBar, &hsb,
             XmNverticalScrollBar, &vsb,
             NULL);
-        wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
+        wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
         if (hsb)
             DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
         if (vsb)
@@ -731,6 +715,38 @@ void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode)
     m_tempCallbackStruct = NULL;
 }
 
+wxSize wxDoGetSingleTextCtrlBestSize( Widget textWidget,
+                                      const wxWindow* window )
+{
+    Dimension xmargin, ymargin, highlight, shadow;
+    char* value;
+
+    XtVaGetValues( textWidget,
+                   XmNmarginWidth, &xmargin,
+                   XmNmarginHeight, &ymargin,
+                   XmNvalue, &value,
+                   XmNhighlightThickness, &highlight,
+                   XmNshadowThickness, &shadow,
+                   NULL );
+    if( !value )
+        value = "|";
+
+    int x, y;
+    window->GetTextExtent( value, &x, &y );
+
+    return wxSize( x + 2 * xmargin + 2 * highlight + 2 * shadow,
+                   // MBN: +2 necessary: Lesstif bug or mine?
+                   y + 2 * ymargin + 2 * highlight + 2 * shadow + 2 ); 
+}
+
+wxSize wxTextCtrl::DoGetBestSize() const
+{
+    if( IsSingleLine() )
+        return wxDoGetSingleTextCtrlBestSize( (Widget)m_mainWidget, this );
+    else
+        return wxWindow::DoGetBestSize();
+}
+
 // ----------------------------------------------------------------------------
 // helpers and Motif callbacks
 // ----------------------------------------------------------------------------
@@ -760,7 +776,9 @@ static void MergeChangesIntoString(wxString& value,
         const char * const passwd = value;
         int len = value.length();
 
-        len += strlen(cbs->text->ptr) + 1;     // + new text (if any) + NUL
+        len += ( cbs->text->ptr ?
+                 strlen(cbs->text->ptr) :
+                 0 ) + 1;                      // + new text (if any) + NUL
         len -= cbs->endPos - cbs->startPos;    // - text from affected region.
 
         char * newS = new char [len];
@@ -774,8 +792,9 @@ static void MergeChangesIntoString(wxString& value,
             *dest++ = *p++;
 
         // Copy the text to be inserted).
-        while (*insert)
-            *dest++ = *insert++;
+        if (insert)
+            while (*insert)
+                *dest++ = *insert++;
 
         // Finally, copy into newS any remaining text from passwd[endPos] on.
         for (p = passwd + cbs->endPos; *p; )