| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: stattext.cpp |
| 3 | // Purpose: wxStaticText |
| 4 | // Author: David Webster |
| 5 | // Modified by: |
| 6 | // Created: 10/17/99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) David Webster |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifndef WX_PRECOMP |
| 16 | #include "wx/event.h" |
| 17 | #include "wx/app.h" |
| 18 | #include "wx/brush.h" |
| 19 | #include "wx/scrolwin.h" |
| 20 | #endif |
| 21 | |
| 22 | #include "wx/stattext.h" |
| 23 | #include "wx/os2/private.h" |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
| 27 | |
| 28 | bool wxStaticText::Create( |
| 29 | wxWindow* pParent |
| 30 | , wxWindowID vId |
| 31 | , const wxString& rsLabel |
| 32 | , const wxPoint& rPos |
| 33 | , const wxSize& rSize |
| 34 | , long lStyle |
| 35 | , const wxString& rsName |
| 36 | ) |
| 37 | { |
| 38 | SetName(rsName); |
| 39 | if (pParent) |
| 40 | pParent->AddChild(this); |
| 41 | |
| 42 | SetBackgroundColour(pParent->GetBackgroundColour()) ; |
| 43 | SetForegroundColour(pParent->GetForegroundColour()) ; |
| 44 | |
| 45 | if ( vId == -1 ) |
| 46 | m_windowId = (int)NewControlId(); |
| 47 | else |
| 48 | m_windowId = vId; |
| 49 | |
| 50 | int nX = rPos.x; |
| 51 | int nY = rPos.y; |
| 52 | int nWidth = rSize.x; |
| 53 | int nHeight = rSize.y; |
| 54 | |
| 55 | m_windowStyle = lStyle; |
| 56 | |
| 57 | long lSstyle = 0L; |
| 58 | |
| 59 | // Used to have DT_VCENTER but that doesn't work correctly with |
| 60 | // multiline strings and DT_WORDBREAK. Accept a reasonable |
| 61 | // compromise for now |
| 62 | lSstyle = WS_VISIBLE | SS_TEXT | DT_WORDBREAK | DT_MNEMONIC; |
| 63 | if (m_windowStyle & wxALIGN_CENTRE) |
| 64 | lSstyle |= DT_CENTER; |
| 65 | else if (m_windowStyle & wxALIGN_RIGHT) |
| 66 | lSstyle |= DT_RIGHT; |
| 67 | else |
| 68 | lSstyle |= DT_LEFT; |
| 69 | |
| 70 | wxString sLabel = ::wxPMTextToLabel(rsLabel); |
| 71 | |
| 72 | m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle |
| 73 | ,WC_STATIC // Window class |
| 74 | ,(PSZ)sLabel.c_str() // Initial Text |
| 75 | ,(ULONG)lSstyle // Style flags |
| 76 | ,0L, 0L, 0L, 0L // Origin -- 0 size |
| 77 | ,(HWND)GetHwndOf(pParent) // owner window handle (same as parent |
| 78 | ,HWND_TOP // initial z position |
| 79 | ,(ULONG)m_windowId // Window identifier |
| 80 | ,NULL // no control data |
| 81 | ,NULL // no Presentation parameters |
| 82 | ); |
| 83 | |
| 84 | wxCHECK_MSG(m_hWnd, FALSE, wxT("Failed to create static ctrl")); |
| 85 | |
| 86 | wxColour vColour; |
| 87 | |
| 88 | vColour.Set(wxString(wxT("BLACK"))); |
| 89 | |
| 90 | LONG lColor = (LONG)vColour.GetPixel(); |
| 91 | |
| 92 | ::WinSetPresParam( m_hWnd |
| 93 | ,PP_FOREGROUNDCOLOR |
| 94 | ,sizeof(LONG) |
| 95 | ,(PVOID)&lColor |
| 96 | ); |
| 97 | lColor = (LONG)m_backgroundColour.GetPixel(); |
| 98 | |
| 99 | ::WinSetPresParam( m_hWnd |
| 100 | ,PP_BACKGROUNDCOLOR |
| 101 | ,sizeof(LONG) |
| 102 | ,(PVOID)&lColor |
| 103 | ); |
| 104 | |
| 105 | SubclassWin(m_hWnd); |
| 106 | SetFont(*wxSMALL_FONT); |
| 107 | SetXComp(0); |
| 108 | SetYComp(0); |
| 109 | SetSize( nX |
| 110 | ,nY |
| 111 | ,nWidth |
| 112 | ,nHeight |
| 113 | ); |
| 114 | return TRUE; |
| 115 | } // end of wxStaticText::Create |
| 116 | |
| 117 | wxSize wxStaticText::DoGetBestSize() const |
| 118 | { |
| 119 | wxString sText(wxGetWindowText(GetHWND())); |
| 120 | int nWidthTextMax = 0; |
| 121 | int nWidthLine = 0; |
| 122 | int nHeightTextTotal = 0; |
| 123 | int nHeightLineDefault = 0; |
| 124 | int nHeightLine = 0; |
| 125 | wxString sCurLine; |
| 126 | bool bLastWasTilde = FALSE; |
| 127 | |
| 128 | for (const wxChar *pc = sText; ; pc++) |
| 129 | { |
| 130 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) |
| 131 | { |
| 132 | if (!sCurLine ) |
| 133 | { |
| 134 | // |
| 135 | // We can't use GetTextExtent - it will return 0 for both width |
| 136 | // and height and an empty line should count in height |
| 137 | // calculation |
| 138 | // |
| 139 | if (!nHeightLineDefault) |
| 140 | nHeightLineDefault = nHeightLine; |
| 141 | if (!nHeightLineDefault) |
| 142 | GetTextExtent(_T("W"), NULL, &nHeightLineDefault); |
| 143 | nHeightTextTotal += nHeightLineDefault; |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | GetTextExtent( sCurLine |
| 148 | ,&nWidthLine |
| 149 | ,&nHeightLine |
| 150 | ); |
| 151 | if (nWidthLine > nWidthTextMax) |
| 152 | nWidthTextMax = nWidthLine; |
| 153 | nHeightTextTotal += nHeightLine; |
| 154 | } |
| 155 | |
| 156 | if ( *pc == wxT('\n') ) |
| 157 | { |
| 158 | sCurLine.Empty(); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | // |
| 168 | // We shouldn't take into account the '~' which just introduces the |
| 169 | // mnemonic characters and so are not shown on the screen -- except |
| 170 | // when it is preceded by another '~' in which case it stands for a |
| 171 | // literal tilde |
| 172 | // |
| 173 | if (*pc == _T('~')) |
| 174 | { |
| 175 | if (!bLastWasTilde) |
| 176 | { |
| 177 | bLastWasTilde = TRUE; |
| 178 | |
| 179 | // |
| 180 | // Skip the statement adding pc to curLine below |
| 181 | // |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | // |
| 186 | // It is a literal tilde |
| 187 | // |
| 188 | bLastWasTilde = FALSE; |
| 189 | } |
| 190 | sCurLine += *pc; |
| 191 | } |
| 192 | } |
| 193 | return wxSize( nWidthTextMax |
| 194 | ,nHeightTextTotal |
| 195 | ); |
| 196 | } // end of wxStaticText::DoGetBestSize |
| 197 | |
| 198 | void wxStaticText::DoSetSize( |
| 199 | int nX |
| 200 | , int nY |
| 201 | , int nWidth |
| 202 | , int nHeight |
| 203 | , int nSizeFlags |
| 204 | ) |
| 205 | { |
| 206 | // |
| 207 | // We need to refresh the window after changing its size as the standard |
| 208 | // control doesn't always update itself properly. |
| 209 | // |
| 210 | wxStaticTextBase::DoSetSize( nX |
| 211 | ,nY |
| 212 | ,nWidth |
| 213 | ,nHeight |
| 214 | ,nSizeFlags |
| 215 | ); |
| 216 | Refresh(); |
| 217 | } // end of wxStaticText::DoSetSize |
| 218 | |
| 219 | bool wxStaticText::SetFont( |
| 220 | const wxFont& rFont |
| 221 | ) |
| 222 | { |
| 223 | bool bRet = wxControl::SetFont(rFont); |
| 224 | |
| 225 | // |
| 226 | // Adjust the size of the window to fit to the label unless autoresizing is |
| 227 | // disabled |
| 228 | // |
| 229 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) |
| 230 | { |
| 231 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); |
| 232 | } |
| 233 | return bRet; |
| 234 | } // end of wxStaticText::SetFont |
| 235 | |
| 236 | void wxStaticText::SetLabel( |
| 237 | const wxString& rsLabel |
| 238 | ) |
| 239 | { |
| 240 | wxString sLabel = ::wxPMTextToLabel(rsLabel); |
| 241 | ::WinSetWindowText(GetHwnd(), (PSZ)sLabel.c_str()); |
| 242 | |
| 243 | // |
| 244 | // Adjust the size of the window to fit to the label unless autoresizing is |
| 245 | // disabled |
| 246 | // |
| 247 | if (!(GetWindowStyle() & wxST_NO_AUTORESIZE)) |
| 248 | { |
| 249 | wxCoord vX; |
| 250 | wxCoord vY; |
| 251 | wxCoord vWidth; |
| 252 | wxCoord vHeight; |
| 253 | |
| 254 | GetPosition(&vX, &vY); |
| 255 | GetSize(&vWidth, &vHeight); |
| 256 | if (!(vX == -1 && vY == -1 && vWidth == -1 && vHeight == -1)) |
| 257 | DoSetSize(vX, vY, vWidth, vHeight, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); |
| 258 | else |
| 259 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); |
| 260 | } |
| 261 | } // end of wxStaticText::SetLabel |
| 262 | |
| 263 | MRESULT wxStaticText::OS2WindowProc( |
| 264 | WXUINT uMsg |
| 265 | , WXWPARAM wParam |
| 266 | , WXLPARAM lParam |
| 267 | ) |
| 268 | { |
| 269 | return wxWindow::OS2WindowProc( uMsg |
| 270 | ,wParam |
| 271 | ,lParam |
| 272 | ); |
| 273 | } // end of wxStaticText::OS2WindowProc |