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