]>
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" 
  37 #include "wx/notebook.h" 
  38 #include "wx/sysopt.h" 
  40 #include "wx/sysopt.h" 
  42 #include "wx/msw/uxtheme.h" 
  43 #include "wx/msw/private.h" 
  44 #include "wx/msw/missing.h" 
  46 // ---------------------------------------------------------------------------- 
  48 // ---------------------------------------------------------------------------- 
  50 #if wxUSE_EXTENDED_RTTI 
  51 WX_DEFINE_FLAGS( wxStaticBoxStyle 
) 
  53 wxBEGIN_FLAGS( wxStaticBoxStyle 
) 
  54     // new style border flags, we put them first to 
  55     // use them for streaming out 
  56     wxFLAGS_MEMBER(wxBORDER_SIMPLE
) 
  57     wxFLAGS_MEMBER(wxBORDER_SUNKEN
) 
  58     wxFLAGS_MEMBER(wxBORDER_DOUBLE
) 
  59     wxFLAGS_MEMBER(wxBORDER_RAISED
) 
  60     wxFLAGS_MEMBER(wxBORDER_STATIC
) 
  61     wxFLAGS_MEMBER(wxBORDER_NONE
) 
  63     // old style border flags 
  64     wxFLAGS_MEMBER(wxSIMPLE_BORDER
) 
  65     wxFLAGS_MEMBER(wxSUNKEN_BORDER
) 
  66     wxFLAGS_MEMBER(wxDOUBLE_BORDER
) 
  67     wxFLAGS_MEMBER(wxRAISED_BORDER
) 
  68     wxFLAGS_MEMBER(wxSTATIC_BORDER
) 
  69     wxFLAGS_MEMBER(wxBORDER
) 
  71     // standard window styles 
  72     wxFLAGS_MEMBER(wxTAB_TRAVERSAL
) 
  73     wxFLAGS_MEMBER(wxCLIP_CHILDREN
) 
  74     wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
) 
  75     wxFLAGS_MEMBER(wxWANTS_CHARS
) 
  76     wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
) 
  77     wxFLAGS_MEMBER(wxALWAYS_SHOW_SB 
) 
  78     wxFLAGS_MEMBER(wxVSCROLL
) 
  79     wxFLAGS_MEMBER(wxHSCROLL
) 
  81 wxEND_FLAGS( wxStaticBoxStyle 
) 
  83 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox
, wxControl
,"wx/statbox.h") 
  85 wxBEGIN_PROPERTIES_TABLE(wxStaticBox
) 
  86     wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) 
  87     wxPROPERTY_FLAGS( WindowStyle 
, wxStaticBoxStyle 
, long , SetWindowStyleFlag 
, GetWindowStyleFlag 
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style 
  92 wxEND_PROPERTIES_TABLE() 
  94 wxBEGIN_HANDLERS_TABLE(wxStaticBox
) 
  95 wxEND_HANDLERS_TABLE() 
  97 wxCONSTRUCTOR_6( wxStaticBox 
, wxWindow
* , Parent 
, wxWindowID 
, Id 
, wxString 
, Label 
, wxPoint 
, Position 
, wxSize 
, Size 
, long , WindowStyle 
) 
  99 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox
, wxControl
) 
 102 // ============================================================================ 
 104 // ============================================================================ 
 106 // ---------------------------------------------------------------------------- 
 108 // ---------------------------------------------------------------------------- 
 110 bool wxStaticBox::Create(wxWindow 
*parent
, 
 112                          const wxString
& label
, 
 116                          const wxString
& name
) 
 118     if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) ) 
 121     if ( !MSWCreateControl(wxT("BUTTON"), label
, pos
, size
) ) 
 125     if (!wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint"))) 
 126         Connect(wxEVT_PAINT
, wxPaintEventHandler(wxStaticBox::OnPaint
)); 
 127 #endif // !__WXWINCE__ 
 132 wxBorder 
wxStaticBox::GetDefaultBorder() const 
 134     return wxBORDER_NONE
; 
 137 WXDWORD 
wxStaticBox::MSWGetStyle(long style
, WXDWORD 
*exstyle
) const 
 139     long styleWin 
= wxStaticBoxBase::MSWGetStyle(style
, exstyle
); 
 141     // no need for it anymore, must be removed for wxRadioBox child 
 142     // buttons to be able to repaint themselves 
 143     styleWin 
&= ~WS_CLIPCHILDREN
; 
 148         if (wxSystemOptions::IsFalse(wxT("msw.staticbox.optimized-paint"))) 
 149             *exstyle 
= WS_EX_TRANSPARENT
; 
 155     return styleWin 
| BS_GROUPBOX
; 
 158 wxSize 
wxStaticBox::DoGetBestSize() const 
 161     wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont()); 
 164     GetTextExtent(wxStripMenuCodes(wxGetWindowText(m_hWnd
)), &wBox
, &cy
); 
 167     int hBox 
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
); 
 169     wxSize 
best(wBox
, hBox
); 
 174 void wxStaticBox::GetBordersForSizer(int *borderTop
, int *borderOther
) const 
 176     wxStaticBoxBase::GetBordersForSizer(borderTop
, borderOther
); 
 178     // need extra space, don't know how much but this seems to be enough 
 179     *borderTop 
+= GetCharHeight()/3; 
 182 // all the hacks below are not necessary for WinCE 
 185 WXLRESULT 
wxStaticBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
) 
 187     if ( nMsg 
== WM_NCHITTEST 
) 
 189         // This code breaks some other processing such as enter/leave tracking 
 190         // so it's off by default. 
 192         static int s_useHTClient 
= -1; 
 193         if (s_useHTClient 
== -1) 
 194             s_useHTClient 
= wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient")); 
 195         if (s_useHTClient 
== 1) 
 197             int xPos 
= GET_X_LPARAM(lParam
); 
 198             int yPos 
= GET_Y_LPARAM(lParam
); 
 200             ScreenToClient(&xPos
, &yPos
); 
 202             // Make sure you can drag by the top of the groupbox, but let 
 203             // other (enclosed) controls get mouse events also 
 205                 return (long)HTCLIENT
; 
 209     if ( nMsg 
== WM_PRINTCLIENT 
) 
 211         // we have to process WM_PRINTCLIENT ourselves as otherwise child 
 212         // windows' background (eg buttons in radio box) would never be drawn 
 213         // unless we have a parent with non default background 
 215         // so check first if we have one 
 216         if ( !HandlePrintClient((WXHDC
)wParam
) ) 
 218             // no, we don't, erase the background ourselves 
 219             // (don't use our own) - see PaintBackground for explanation 
 220             wxBrush 
brush(GetParent()->GetBackgroundColour()); 
 221             wxFillRect(GetHwnd(), (HDC
)wParam
, GetHbrushOf(brush
)); 
 227     return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
); 
 230 // ---------------------------------------------------------------------------- 
 231 // static box drawing 
 232 // ---------------------------------------------------------------------------- 
 235    We draw the static box ourselves because it's the only way to prevent it 
 236    from flickering horribly on resize (because everything inside the box is 
 237    erased twice: once when the box itself is repainted and second time when 
 238    the control inside it is repainted) without using WS_EX_TRANSPARENT style as 
 239    we used to do and which resulted in other problems. 
 242 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region 
 244 SubtractRectFromRgn(HRGN hrgn
, int left
, int top
, int right
, int bottom
) 
 246     AutoHRGN 
hrgnRect(::CreateRectRgn(left
, top
, right
, bottom
)); 
 249         wxLogLastError(_T("CreateRectRgn()")); 
 253     ::CombineRgn(hrgn
, hrgn
, hrgnRect
, RGN_DIFF
); 
 256 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn
, int w
, int h
) 
 258     HRGN hrgn 
= (HRGN
)hRgn
; 
 260     // remove the area occupied by the static box borders from the region 
 261     int borderTop
, border
; 
 262     GetBordersForSizer(&borderTop
, &border
); 
 265     SubtractRectFromRgn(hrgn
, 0, 0, w
, borderTop
); 
 268     SubtractRectFromRgn(hrgn
, 0, h 
- border
, w
, h
); 
 271     SubtractRectFromRgn(hrgn
, 0, 0, border
, h
); 
 274     SubtractRectFromRgn(hrgn
, w 
- border
, 0, w
, h
); 
 277 WXHRGN 
wxStaticBox::MSWGetRegionWithoutChildren() 
 280     ::GetWindowRect(GetHwnd(), &rc
); 
 281     HRGN hrgn 
= ::CreateRectRgn(rc
.left
, rc
.top
, rc
.right 
+ 1, rc
.bottom 
+ 1); 
 282     bool foundThis 
= false; 
 284     // iterate over all child windows (not just wxWindows but all windows) 
 285     for ( HWND child 
= ::GetWindow(GetHwndOf(GetParent()), GW_CHILD
); 
 287           child 
= ::GetWindow(child
, GW_HWNDNEXT
) ) 
 289         if ( ! ::IsWindowVisible(child
) ) 
 291             // if the window isn't visible then it doesn't need clipped 
 295         LONG style 
= ::GetWindowLong(child
, GWL_STYLE
); 
 296         wxString 
str(wxGetWindowClass(child
)); 
 298         if ( str 
== wxT("BUTTON") && (style 
& BS_GROUPBOX
) == BS_GROUPBOX 
) 
 300             if ( child 
== GetHwnd() ) 
 303             // Any static boxes below this one in the Z-order can't be clipped 
 304             // since if we have the case where a static box with a low Z-order 
 305             // is nested inside another static box with a high Z-order then the 
 306             // nested static box would be painted over. Doing it this way 
 307             // unfortunately results in flicker if the Z-order of nested static 
 308             // boxes is not inside (lowest) to outside (highest) but at least 
 309             // they are still shown. 
 314         ::GetWindowRect(child
, &rc
); 
 315         if ( ::RectInRegion(hrgn
, &rc
) ) 
 317             // need to remove WS_CLIPSIBLINGS from all sibling windows 
 318             // that are within this staticbox if set 
 319             if ( style 
& WS_CLIPSIBLINGS 
) 
 321                 style 
&= ~WS_CLIPSIBLINGS
; 
 322                 ::SetWindowLong(child
, GWL_STYLE
, style
); 
 324                 // MSDN: "If you have changed certain window data using 
 325                 // SetWindowLong, you must call SetWindowPos to have the 
 326                 // changes take effect." 
 327                 ::SetWindowPos(child
, NULL
, 0, 0, 0, 0, 
 328                                SWP_NOMOVE 
| SWP_NOSIZE 
| SWP_NOZORDER 
| 
 332             AutoHRGN 
hrgnChild(::CreateRectRgnIndirect(&rc
)); 
 333             ::CombineRgn(hrgn
, hrgn
, hrgnChild
, RGN_DIFF
); 
 340 // helper for OnPaint(): really erase the background, i.e. do it even if we 
 341 // don't have any non default brush for doing it (DoEraseBackground() doesn't 
 342 // do anything in such case) 
 343 void wxStaticBox::PaintBackground(wxDC
& dc
, const RECT
& rc
) 
 345     // note that we do not use the box background colour here, it shouldn't 
 346     // apply to its interior for several reasons: 
 347     //  1. wxGTK doesn't do it 
 348     //  2. controls inside the box don't get correct bg colour because they 
 349     //     are not our children so we'd have some really ugly colour mix if 
 351     //  3. this is backwards compatible behaviour and some people rely on it, 
 352     //     see http://groups.google.com/groups?selm=4252E932.3080801%40able.es 
 353     wxWindow 
*parent 
= GetParent(); 
 354     HBRUSH hbr 
= (HBRUSH
)parent
->MSWGetBgBrush(dc
.GetHDC(), GetHWND()); 
 356     // if there is no special brush for painting this control, just use the 
 357     // solid background colour 
 361         brush 
= wxBrush(parent
->GetBackgroundColour()); 
 362         hbr 
= GetHbrushOf(brush
); 
 365     ::FillRect(GetHdcOf(dc
), &rc
, hbr
); 
 368 void wxStaticBox::PaintForeground(wxDC
& dc
, const RECT
& WXUNUSED(rc
)) 
 370     MSWDefWindowProc(WM_PAINT
, (WPARAM
)GetHdcOf(dc
), 0); 
 372     // when using XP themes, neither setting the text colour nor transparent 
 373     // background mode doesn't change anything: the static box def window proc 
 374     // still draws the label in its own colours, so we need to redraw the text 
 375     // ourselves if we have a non default fg colour 
 376     if ( m_hasFgCol 
&& wxUxThemeEngine::GetIfActive() ) 
 378         // draw over the text in default colour in our colour 
 379         dc
.SetFont(GetFont()); 
 381         HDC hdc 
= GetHdcOf(dc
); 
 382         ::SetTextColor(hdc
, GetForegroundColour().GetPixel()); 
 384         // FIXME: value of x is hardcoded as this is what it is on my system, 
 385         //        no idea if it's true everywhere 
 386         const int y 
= dc
.GetCharHeight(); 
 390         RECT rc 
= { x
, 0, GetSize().x
, y 
}; 
 392         const wxString label 
= GetLabel(); 
 393         ::DrawText(hdc
, label
, label
.length(), &rc
, DT_SINGLELINE 
| DT_VCENTER
); 
 397 void wxStaticBox::OnPaint(wxPaintEvent
& WXUNUSED(event
)) 
 400     ::GetClientRect(GetHwnd(), &rc
); 
 402     // draw the entire box in a memory DC 
 404     wxBitmap 
bitmap(rc
.right
, rc
.bottom
); 
 405     memdc
.SelectObject(bitmap
); 
 407     PaintBackground(memdc
, rc
); 
 408     PaintForeground(memdc
, rc
); 
 410     // now only blit the static box border itself, not the interior, to avoid 
 411     // flicker when background is drawn below 
 413     // note that it seems to be faster to do 4 small blits here and then paint 
 414     // directly into wxPaintDC than painting background in wxMemoryDC and then 
 415     // blitting everything at once to wxPaintDC, this is why we do it like this 
 417     int borderTop
, border
; 
 418     GetBordersForSizer(&borderTop
, &border
); 
 421     dc
.Blit(border
, 0, rc
.right 
- border
, borderTop
, 
 424     dc
.Blit(border
, rc
.bottom 
- border
, rc
.right 
- border
, rc
.bottom
, 
 425             &memdc
, border
, rc
.bottom 
- border
); 
 427     dc
.Blit(0, 0, border
, rc
.bottom
, 
 430     dc
.Blit(rc
.right 
- border
, 0, rc
.right
, rc
.bottom
, 
 431             &memdc
, rc
.right 
- border
, 0); 
 434     // create the region excluding box children 
 435     AutoHRGN 
hrgn((HRGN
)MSWGetRegionWithoutChildren()); 
 437     ::GetWindowRect(GetHwnd(), &rcWin
); 
 438     ::OffsetRgn(hrgn
, -rcWin
.left
, -rcWin
.top
); 
 440     // and also the box itself 
 441     MSWGetRegionWithoutSelf((WXHRGN
) hrgn
, rc
.right
, rc
.bottom
); 
 442     HDCClipper 
clipToBg(GetHdcOf(dc
), hrgn
); 
 444     // paint the inside of the box (excluding box itself and child controls) 
 445     PaintBackground(dc
, rc
); 
 448 #endif // !__WXWINCE__ 
 450 #endif // wxUSE_STATBOX