| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/msw/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 |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 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 | #if wxUSE_STATTEXT |
| 24 | |
| 25 | #ifndef WX_PRECOMP |
| 26 | #include "wx/event.h" |
| 27 | #include "wx/app.h" |
| 28 | #include "wx/brush.h" |
| 29 | #endif |
| 30 | |
| 31 | #include "wx/stattext.h" |
| 32 | #include "wx/msw/private.h" |
| 33 | #include <stdio.h> |
| 34 | |
| 35 | #if wxUSE_EXTENDED_RTTI |
| 36 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl,"wx/stattext.h") |
| 37 | |
| 38 | WX_BEGIN_PROPERTIES_TABLE(wxStaticText) |
| 39 | WX_PROPERTY( Label,wxString, SetLabel, GetLabel, wxEmptyString ) |
| 40 | WX_END_PROPERTIES_TABLE() |
| 41 | |
| 42 | WX_BEGIN_HANDLERS_TABLE(wxStaticText) |
| 43 | WX_END_HANDLERS_TABLE() |
| 44 | |
| 45 | WX_CONSTRUCTOR_6( wxStaticText , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
| 46 | #else |
| 47 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
| 48 | #endif |
| 49 | |
| 50 | bool wxStaticText::Create(wxWindow *parent, |
| 51 | wxWindowID id, |
| 52 | const wxString& label, |
| 53 | const wxPoint& pos, |
| 54 | const wxSize& size, |
| 55 | long style, |
| 56 | const wxString& name) |
| 57 | { |
| 58 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
| 59 | return FALSE; |
| 60 | |
| 61 | if ( !MSWCreateControl(wxT("STATIC"), label, pos, size) ) |
| 62 | return FALSE; |
| 63 | |
| 64 | return TRUE; |
| 65 | } |
| 66 | |
| 67 | wxBorder wxStaticText::GetDefaultBorder() const |
| 68 | { |
| 69 | return wxBORDER_NONE; |
| 70 | } |
| 71 | |
| 72 | WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const |
| 73 | { |
| 74 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); |
| 75 | |
| 76 | // translate the alignment flags to the Windows ones |
| 77 | // |
| 78 | // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't |
| 79 | // test for them using & operator |
| 80 | if ( style & wxALIGN_CENTRE ) |
| 81 | msStyle |= SS_CENTER; |
| 82 | else if ( style & wxALIGN_RIGHT ) |
| 83 | msStyle |= SS_RIGHT; |
| 84 | else |
| 85 | msStyle |= SS_LEFT; |
| 86 | |
| 87 | return msStyle; |
| 88 | } |
| 89 | |
| 90 | wxSize wxStaticText::DoGetBestSize() const |
| 91 | { |
| 92 | wxString text(wxGetWindowText(GetHWND())); |
| 93 | |
| 94 | int widthTextMax = 0, widthLine, |
| 95 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; |
| 96 | |
| 97 | bool lastWasAmpersand = FALSE; |
| 98 | |
| 99 | wxString curLine; |
| 100 | for ( const wxChar *pc = text; ; pc++ ) |
| 101 | { |
| 102 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) |
| 103 | { |
| 104 | if ( !curLine ) |
| 105 | { |
| 106 | // we can't use GetTextExtent - it will return 0 for both width |
| 107 | // and height and an empty line should count in height |
| 108 | // calculation |
| 109 | if ( !heightLineDefault ) |
| 110 | heightLineDefault = heightLine; |
| 111 | if ( !heightLineDefault ) |
| 112 | GetTextExtent(_T("W"), NULL, &heightLineDefault); |
| 113 | |
| 114 | heightTextTotal += heightLineDefault; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | GetTextExtent(curLine, &widthLine, &heightLine); |
| 119 | if ( widthLine > widthTextMax ) |
| 120 | widthTextMax = widthLine; |
| 121 | heightTextTotal += heightLine; |
| 122 | } |
| 123 | |
| 124 | if ( *pc == wxT('\n') ) |
| 125 | { |
| 126 | curLine.Empty(); |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | // the end of string |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | // we shouldn't take into account the '&' which just introduces the |
| 137 | // mnemonic characters and so are not shown on the screen -- except |
| 138 | // when it is preceded by another '&' in which case it stands for a |
| 139 | // literal ampersand |
| 140 | if ( *pc == _T('&') ) |
| 141 | { |
| 142 | if ( !lastWasAmpersand ) |
| 143 | { |
| 144 | lastWasAmpersand = TRUE; |
| 145 | |
| 146 | // skip the statement adding pc to curLine below |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | // it is a literal ampersand |
| 151 | lastWasAmpersand = FALSE; |
| 152 | } |
| 153 | |
| 154 | curLine += *pc; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return wxSize(widthTextMax, heightTextTotal); |
| 159 | } |
| 160 | |
| 161 | void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags) |
| 162 | { |
| 163 | // we need to refresh the window after changing its size as the standard |
| 164 | // control doesn't always update itself properly |
| 165 | wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags); |
| 166 | |
| 167 | Refresh(); |
| 168 | } |
| 169 | |
| 170 | void wxStaticText::SetLabel(const wxString& label) |
| 171 | { |
| 172 | wxStaticTextBase::SetLabel(label); |
| 173 | |
| 174 | // adjust the size of the window to fit to the label unless autoresizing is |
| 175 | // disabled |
| 176 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) |
| 177 | { |
| 178 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | bool wxStaticText::SetFont(const wxFont& font) |
| 184 | { |
| 185 | bool ret = wxControl::SetFont(font); |
| 186 | |
| 187 | // adjust the size of the window to fit to the label unless autoresizing is |
| 188 | // disabled |
| 189 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) |
| 190 | { |
| 191 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); |
| 192 | } |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | #endif // wxUSE_STATTEXT |