| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: stattext.cpp |
| 3 | // Purpose: wxStaticText |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "stattext.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __BORLANDC__ |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #ifndef WX_PRECOMP |
| 24 | #include "wx/event.h" |
| 25 | #include "wx/app.h" |
| 26 | #include "wx/brush.h" |
| 27 | #endif |
| 28 | |
| 29 | #include "wx/stattext.h" |
| 30 | #include "wx/msw/private.h" |
| 31 | #include <stdio.h> |
| 32 | |
| 33 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
| 34 | |
| 35 | bool wxStaticText::Create(wxWindow *parent, wxWindowID id, |
| 36 | const wxString& label, |
| 37 | const wxPoint& pos, |
| 38 | const wxSize& size, |
| 39 | long style, |
| 40 | const wxString& name) |
| 41 | { |
| 42 | SetName(name); |
| 43 | if (parent) parent->AddChild(this); |
| 44 | |
| 45 | SetBackgroundColour(parent->GetBackgroundColour()) ; |
| 46 | SetForegroundColour(parent->GetForegroundColour()) ; |
| 47 | |
| 48 | if ( id == -1 ) |
| 49 | m_windowId = (int)NewControlId(); |
| 50 | else |
| 51 | m_windowId = id; |
| 52 | |
| 53 | int x = pos.x; |
| 54 | int y = pos.y; |
| 55 | int width = size.x; |
| 56 | int height = size.y; |
| 57 | |
| 58 | m_windowStyle = style; |
| 59 | |
| 60 | long msStyle = WS_CHILD | WS_VISIBLE; |
| 61 | |
| 62 | if ( m_windowStyle & wxCLIP_SIBLINGS ) |
| 63 | msStyle |= WS_CLIPSIBLINGS; |
| 64 | if (m_windowStyle & wxALIGN_CENTRE) |
| 65 | msStyle |= SS_CENTER; |
| 66 | else if (m_windowStyle & wxALIGN_RIGHT) |
| 67 | msStyle |= SS_RIGHT; |
| 68 | else |
| 69 | msStyle |= SS_LEFT; |
| 70 | |
| 71 | // Even with extended styles, need to combine with WS_BORDER |
| 72 | // for them to look right. |
| 73 | if ( wxStyleHasBorder(m_windowStyle) ) |
| 74 | msStyle |= WS_BORDER; |
| 75 | |
| 76 | m_hWnd = (WXHWND)::CreateWindowEx(MakeExtendedStyle(m_windowStyle), wxT("STATIC"), (const wxChar *)label, |
| 77 | msStyle, |
| 78 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, |
| 79 | wxGetInstance(), NULL); |
| 80 | |
| 81 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static ctrl") ); |
| 82 | |
| 83 | #if wxUSE_CTL3D |
| 84 | /* |
| 85 | if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS)) |
| 86 | Ctl3dSubclassCtl(static_item); |
| 87 | */ |
| 88 | #endif |
| 89 | |
| 90 | SubclassWin(m_hWnd); |
| 91 | |
| 92 | wxControl::SetFont(parent->GetFont()); |
| 93 | SetSize(x, y, width, height); |
| 94 | |
| 95 | return TRUE; |
| 96 | } |
| 97 | |
| 98 | wxSize wxStaticText::DoGetBestSize() const |
| 99 | { |
| 100 | wxString text(wxGetWindowText(GetHWND())); |
| 101 | |
| 102 | int widthTextMax = 0, widthLine, |
| 103 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; |
| 104 | |
| 105 | wxString curLine; |
| 106 | for ( const wxChar *pc = text; ; pc++ ) { |
| 107 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) { |
| 108 | if ( !curLine ) { |
| 109 | // we can't use GetTextExtent - it will return 0 for both width |
| 110 | // and height and an empty line should count in height |
| 111 | // calculation |
| 112 | if ( !heightLineDefault ) |
| 113 | heightLineDefault = heightLine; |
| 114 | if ( !heightLineDefault ) |
| 115 | GetTextExtent(_T("W"), NULL, &heightLineDefault); |
| 116 | |
| 117 | heightTextTotal += heightLineDefault; |
| 118 | } |
| 119 | else { |
| 120 | GetTextExtent(curLine, &widthLine, &heightLine); |
| 121 | if ( widthLine > widthTextMax ) |
| 122 | widthTextMax = widthLine; |
| 123 | heightTextTotal += heightLine; |
| 124 | } |
| 125 | |
| 126 | if ( *pc == wxT('\n') ) { |
| 127 | curLine.Empty(); |
| 128 | } |
| 129 | else { |
| 130 | // the end of string |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | else { |
| 135 | curLine += *pc; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return wxSize(widthTextMax, heightTextTotal); |
| 140 | } |
| 141 | |
| 142 | void wxStaticText::SetLabel(const wxString& label) |
| 143 | { |
| 144 | SetWindowText(GetHwnd(), label); |
| 145 | |
| 146 | // adjust the size of the window to fit to the label unless autoresizing is |
| 147 | // disabled |
| 148 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) |
| 149 | { |
| 150 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | bool wxStaticText::SetFont(const wxFont& font) |
| 156 | { |
| 157 | bool ret = wxControl::SetFont(font); |
| 158 | |
| 159 | // adjust the size of the window to fit to the label unless autoresizing is |
| 160 | // disabled |
| 161 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) |
| 162 | { |
| 163 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); |
| 164 | } |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | long wxStaticText::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
| 171 | { |
| 172 | // Ensure that static items get messages. Some controls don't like this |
| 173 | // message to be intercepted (e.g. RichEdit), hence the tests. |
| 174 | if (nMsg == WM_NCHITTEST) |
| 175 | return (long)HTCLIENT; |
| 176 | |
| 177 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); |
| 178 | } |
| 179 | |
| 180 | |