]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
   1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/statbox.cpp 
   3 // Purpose:     wxStaticBox 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  29 #include "wx/statbox.h" 
  33     #include "wx/dcclient.h" 
  34     #include "wx/dcmemory.h" 
  38 #include "wx/notebook.h" 
  39 #include "wx/sysopt.h" 
  41 #include "wx/msw/uxtheme.h" 
  42 #include "wx/msw/private.h" 
  43 #include "wx/msw/missing.h" 
  45 // ---------------------------------------------------------------------------- 
  47 // ---------------------------------------------------------------------------- 
  49 #if wxUSE_EXTENDED_RTTI 
  50 WX_DEFINE_FLAGS( wxStaticBoxStyle 
) 
  52 wxBEGIN_FLAGS( wxStaticBoxStyle 
) 
  53     // new style border flags, we put them first to 
  54     // use them for streaming out 
  55     wxFLAGS_MEMBER(wxBORDER_SIMPLE
) 
  56     wxFLAGS_MEMBER(wxBORDER_SUNKEN
) 
  57     wxFLAGS_MEMBER(wxBORDER_DOUBLE
) 
  58     wxFLAGS_MEMBER(wxBORDER_RAISED
) 
  59     wxFLAGS_MEMBER(wxBORDER_STATIC
) 
  60     wxFLAGS_MEMBER(wxBORDER_NONE
) 
  62     // old style border flags 
  63     wxFLAGS_MEMBER(wxSIMPLE_BORDER
) 
  64     wxFLAGS_MEMBER(wxSUNKEN_BORDER
) 
  65     wxFLAGS_MEMBER(wxDOUBLE_BORDER
) 
  66     wxFLAGS_MEMBER(wxRAISED_BORDER
) 
  67     wxFLAGS_MEMBER(wxSTATIC_BORDER
) 
  68     wxFLAGS_MEMBER(wxBORDER
) 
  70     // standard window styles 
  71     wxFLAGS_MEMBER(wxTAB_TRAVERSAL
) 
  72     wxFLAGS_MEMBER(wxCLIP_CHILDREN
) 
  73     wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
) 
  74     wxFLAGS_MEMBER(wxWANTS_CHARS
) 
  75     wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
) 
  76     wxFLAGS_MEMBER(wxALWAYS_SHOW_SB 
) 
  77     wxFLAGS_MEMBER(wxVSCROLL
) 
  78     wxFLAGS_MEMBER(wxHSCROLL
) 
  80 wxEND_FLAGS( wxStaticBoxStyle 
) 
  82 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox
, wxControl
,"wx/statbox.h") 
  84 wxBEGIN_PROPERTIES_TABLE(wxStaticBox
) 
  85     wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) 
  86     wxPROPERTY_FLAGS( WindowStyle 
, wxStaticBoxStyle 
, long , SetWindowStyleFlag 
, GetWindowStyleFlag 
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style 
  91 wxEND_PROPERTIES_TABLE() 
  93 wxBEGIN_HANDLERS_TABLE(wxStaticBox
) 
  94 wxEND_HANDLERS_TABLE() 
  96 wxCONSTRUCTOR_6( wxStaticBox 
, wxWindow
* , Parent 
, wxWindowID 
, Id 
, wxString 
, Label 
, wxPoint 
, Position 
, wxSize 
, Size 
, long , WindowStyle 
) 
  98 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox
, wxControl
) 
 101 // ============================================================================ 
 103 // ============================================================================ 
 105 // ---------------------------------------------------------------------------- 
 107 // ---------------------------------------------------------------------------- 
 109 bool wxStaticBox::Create(wxWindow 
*parent
, 
 111                          const wxString
& label
, 
 115                          const wxString
& name
) 
 117     if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) ) 
 120     if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) ) 
 124     if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint"))) 
 125         Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
)); 
 126 #endif // !__WXWINCE__ 
 131 wxBorder 
wxStaticBox::GetDefaultBorder() const 
 133     return wxBORDER_NONE
; 
 136 WXDWORD 
wxStaticBox::MSWGetStyle(long style
, WXDWORD 
*exstyle
) const 
 138     long styleWin 
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
); 
 140     // no need for it anymore, must be removed for wxRadioBox child 
 141     // buttons to be able to repaint themselves 
 142     styleWin 
&= ~WS_CLIPCHILDREN
; 
 147         if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint"))) 
 148             *exstyle 
= WS_EX_TRANSPARENT
; 
 154     return styleWin 
| BS_GROUPBOX
; 
 157 wxSize 
wxStaticBox::DoGetBestSize() const 
 160     wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont()); 
 163     GetTextExtent(wxStripMenuCodes(wxGetWindowText(m_hWnd
)), &wBox
, &cy
); 
 166     int hBox 
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
); 
 168     wxSize 
best(wBox
, hBox
); 
 173 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const 
 175     wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
); 
 177     // need extra space, don't know how much but this seems to be enough 
 178     *borderTop 
+= GetCharHeight()/3; 
 181 // all the hacks below are not necessary for WinCE 
 184 WXLRESULT 
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
) 
 186     if ( nMsg 
== WM_NCHITTEST 
) 
 188         // This code breaks some other processing such as enter/leave tracking 
 189         // so it's off by default. 
 191         static int s_useHTClient 
= -1; 
 192         if (s_useHTClient 
== -1) 
 193             s_useHTClient 
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient")); 
 194         if (s_useHTClient 
== 1) 
 196             int xPos 
= GET_X_LPARAM(lParam
); 
 197             int yPos 
= GET_Y_LPARAM(lParam
); 
 199             ScreenToClient(&xPos
, &yPos
); 
 201             // Make sure you can drag by the top of the groupbox, but let 
 202             // other (enclosed) controls get mouse events also 
 204                 return (long)HTCLIENT
; 
 208     if ( nMsg 
== WM_PRINTCLIENT 
) 
 210         // we have to process WM_PRINTCLIENT ourselves as otherwise child 
 211         // windows' background (eg buttons in radio box) would never be drawn 
 212         // unless we have a parent with non default background 
 214         // so check first if we have one 
 215         if ( !HandlePrintClient((WXHDC
)wParam
) ) 
 217             // no, we don't, erase the background ourselves 
 218             // (don't use our own) - see PaintBackground for explanation 
 219             wxBrush 
brush(GetParent()->GetBackgroundColour()); 
 220             wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
)); 
 226     return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
); 
 229 // ---------------------------------------------------------------------------- 
 230 // static box drawing 
 231 // ---------------------------------------------------------------------------- 
 234    We draw the static box ourselves because it's the only way to prevent it 
 235    from flickering horribly on resize (because everything inside the box is 
 236    erased twice: once when the box itself is repainted and second time when 
 237    the control inside it is repainted) without using WS_EX_TRANSPARENT style as 
 238    we used to do and which resulted in other problems. 
 241 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region 
 243 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
) 
 245     AutoHRGN 
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
)); 
 248         wxLogLastError(_T("CreateRectRgn()")); 
 252     ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
); 
 255 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
) 
 257     HRGN hrgn 
= (HRGN
)hRgn
; 
 259     // remove the area occupied by the static box borders from the region 
 260     int borderTop
, border
; 
 261     GetBordersForSizer(&borderTop
, &border
); 
 264     SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
); 
 267     SubtractRectFromRgn(hrgn
, 0, h 
- border
, w
, h
); 
 270     SubtractRectFromRgn(hrgn
, 0, 0, border
, h
); 
 273     SubtractRectFromRgn(hrgn
, w 
- border
, 0, w
, h
); 
 276 WXHRGN 
wxStaticBox::MSWGetRegionWithoutChildren() 
 279     ::GetWindowRect(GetHwnd(), &rc
); 
 280     HRGN hrgn 
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right 
+ 1, rc
.bottom 
+ 1); 
 281     bool foundThis 
= false; 
 283     // iterate over all child windows (not just wxWindows but all windows) 
 284     for ( HWND child 
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
); 
 286           child 
= ::GetWindow(child
, GW_HWNDNEXT
) ) 
 288         if ( ! ::IsWindowVisible(child
) ) 
 290             // if the window isn't visible then it doesn't need clipped 
 294         LONG style 
= ::GetWindowLong(child
, GWL_STYLE
); 
 295         wxString 
str(wxGetWindowClass(child
)); 
 297         if ( str 
== wxT("BUTTON") && (style 
& BS_GROUPBOX
) == BS_GROUPBOX 
) 
 299             if ( child 
== GetHwnd() ) 
 302             // Any static boxes below this one in the Z-order can't be clipped 
 303             // since if we have the case where a static box with a low Z-order 
 304             // is nested inside another static box with a high Z-order then the 
 305             // nested static box would be painted over. Doing it this way 
 306             // unfortunately results in flicker if the Z-order of nested static 
 307             // boxes is not inside (lowest) to outside (highest) but at least 
 308             // they are still shown. 
 313         ::GetWindowRect(child
, &rc
); 
 314         if ( ::RectInRegion(hrgn
, &rc
) ) 
 316             // need to remove WS_CLIPSIBLINGS from all sibling windows 
 317             // that are within this staticbox if set 
 318             if ( style 
& WS_CLIPSIBLINGS 
) 
 320                 style 
&= ~WS_CLIPSIBLINGS
; 
 321                 ::SetWindowLong(child
, GWL_STYLE
, style
); 
 323                 // MSDN: "If you have changed certain window data using 
 324                 // SetWindowLong, you must call SetWindowPos to have the 
 325                 // changes take effect." 
 326                 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0, 
 327                                SWP_NOMOVE 
| SWP_NOSIZE 
| SWP_NOZORDER 
| 
 331             AutoHRGN 
hrgnChild(::CreateRectRgnIndirect(&rc
)); 
 332             ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
); 
 339 // helper for OnPaint(): really erase the background, i.e. do it even if we 
 340 // don't have any non default brush for doing it (DoEraseBackground() doesn't 
 341 // do anything in such case) 
 342 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
) 
 344     // note that we do not use the box background colour here, it shouldn't 
 345     // apply to its interior for several reasons: 
 346     //  1. wxGTK doesn't do it 
 347     //  2. controls inside the box don't get correct bg colour because they 
 348     //     are not our children so we'd have some really ugly colour mix if 
 350     //  3. this is backwards compatible behaviour and some people rely on it, 
 351     //     see http://groups.google.com/groups?selm=4252E932.3080801%40able.es 
 352     wxWindow 
*parent 
= GetParent(); 
 353     HBRUSH hbr 
= (HBRUSH
)parent
->MSWGetBgBrush(dc
.GetHDC(), GetHWND()); 
 355     // if there is no special brush for painting this control, just use the 
 356     // solid background colour 
 360         brush 
= wxBrush(parent
->GetBackgroundColour()); 
 361         hbr 
= GetHbrushOf(brush
); 
 364     ::FillRect(GetHdcOf(dc
), &rc
, hbr
); 
 367 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& WXUNUSED(rc
)) 
 369     MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(dc
), 0); 
 371     // when using XP themes, neither setting the text colour nor transparent 
 372     // background mode doesn't change anything: the static box def window proc 
 373     // still draws the label in its own colours, so we need to redraw the text 
 374     // ourselves if we have a non default fg colour 
 375     if ( m_hasFgCol 
&& wxUxThemeEngine::GetIfActive() ) 
 377         // draw over the text in default colour in our colour 
 378         dc
.SetFont(GetFont()); 
 380         HDC hdc 
= GetHdcOf(dc
); 
 381         ::SetTextColor(hdc
, GetForegroundColour().GetPixel()); 
 383         // FIXME: value of x is hardcoded as this is what it is on my system, 
 384         //        no idea if it's true everywhere 
 385         const int y 
= dc
.GetCharHeight(); 
 389         RECT rc 
= { x
, 0, GetSize().x
, y 
}; 
 391         const wxString label 
= GetLabel(); 
 392         ::DrawText(hdc
, label
, label
.length(), &rc
, DT_SINGLELINE 
| DT_VCENTER
); 
 396 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
)) 
 399     ::GetClientRect(GetHwnd(), &rc
); 
 401     // draw the entire box in a memory DC 
 403     wxBitmap 
bitmap(rc
.right
, rc
.bottom
); 
 404     memdc
.SelectObject(bitmap
); 
 406     PaintBackground(memdc
, rc
); 
 407     PaintForeground(memdc
, rc
); 
 409     // now only blit the static box border itself, not the interior, to avoid 
 410     // flicker when background is drawn below 
 412     // note that it seems to be faster to do 4 small blits here and then paint 
 413     // directly into wxPaintDC than painting background in wxMemoryDC and then 
 414     // blitting everything at once to wxPaintDC, this is why we do it like this 
 416     int borderTop
, border
; 
 417     GetBordersForSizer(&borderTop
, &border
); 
 420     dc
.Blit(border
, 0, rc
.right 
- border
, borderTop
, 
 423     dc
.Blit(border
, rc
.bottom 
- border
, rc
.right 
- border
, rc
.bottom
, 
 424             &memdc
, border
, rc
.bottom 
- border
); 
 426     dc
.Blit(0, 0, border
, rc
.bottom
, 
 429     dc
.Blit(rc
.right 
- border
, 0, rc
.right
, rc
.bottom
, 
 430             &memdc
, rc
.right 
- border
, 0); 
 433     // create the region excluding box children 
 434     AutoHRGN 
hrgn((HRGN
)MSWGetRegionWithoutChildren()); 
 436     ::GetWindowRect(GetHwnd(), &rcWin
); 
 437     ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
); 
 439     // and also the box itself 
 440     MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
); 
 441     HDCClipper 
clipToBg(GetHdcOf(dc
), hrgn
); 
 443     // paint the inside of the box (excluding box itself and child controls) 
 444     PaintBackground(dc
, rc
); 
 447 #endif // !__WXWINCE__ 
 449 #endif // wxUSE_STATBOX