]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbox.cpp
CW fix, added tif_color
[wxWidgets.git] / src / msw / statbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/statbox.cpp
3 // Purpose: wxStaticBox
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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "statbox.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_STATBOX
32
33 #ifndef WX_PRECOMP
34 #include "wx/app.h"
35 #include "wx/dcclient.h"
36 #endif
37
38 #include "wx/statbox.h"
39 #include "wx/notebook.h"
40 #include "wx/sysopt.h"
41 #include "wx/image.h"
42 #include "wx/dcmemory.h"
43
44 #include "wx/msw/private.h"
45 #include "wx/msw/missing.h"
46
47 // ----------------------------------------------------------------------------
48 // wxWin macros
49 // ----------------------------------------------------------------------------
50
51 #if wxUSE_EXTENDED_RTTI
52 WX_DEFINE_FLAGS( wxStaticBoxStyle )
53
54 wxBEGIN_FLAGS( wxStaticBoxStyle )
55 // new style border flags, we put them first to
56 // use them for streaming out
57 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
58 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
59 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
60 wxFLAGS_MEMBER(wxBORDER_RAISED)
61 wxFLAGS_MEMBER(wxBORDER_STATIC)
62 wxFLAGS_MEMBER(wxBORDER_NONE)
63
64 // old style border flags
65 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
66 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
67 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
68 wxFLAGS_MEMBER(wxRAISED_BORDER)
69 wxFLAGS_MEMBER(wxSTATIC_BORDER)
70 wxFLAGS_MEMBER(wxBORDER)
71
72 // standard window styles
73 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
74 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
75 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
76 wxFLAGS_MEMBER(wxWANTS_CHARS)
77 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
78 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
79 wxFLAGS_MEMBER(wxVSCROLL)
80 wxFLAGS_MEMBER(wxHSCROLL)
81
82 wxEND_FLAGS( wxStaticBoxStyle )
83
84 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticBox, wxControl,"wx/statbox.h")
85
86 wxBEGIN_PROPERTIES_TABLE(wxStaticBox)
87 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
88 wxPROPERTY_FLAGS( WindowStyle , wxStaticBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
89 /*
90 TODO PROPERTIES :
91 label
92 */
93 wxEND_PROPERTIES_TABLE()
94
95 wxBEGIN_HANDLERS_TABLE(wxStaticBox)
96 wxEND_HANDLERS_TABLE()
97
98 wxCONSTRUCTOR_6( wxStaticBox , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
99 #else
100 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
101 #endif
102
103 // ============================================================================
104 // implementation
105 // ============================================================================
106
107 // ----------------------------------------------------------------------------
108 // wxStaticBox
109 // ----------------------------------------------------------------------------
110
111 bool wxStaticBox::Create(wxWindow *parent,
112 wxWindowID id,
113 const wxString& label,
114 const wxPoint& pos,
115 const wxSize& size,
116 long style,
117 const wxString& name)
118 {
119 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
120 return false;
121
122 if ( !MSWCreateControl(wxT("BUTTON"), label, pos, size) )
123 return false;
124
125 #ifndef __WXWINCE__
126 Connect(wxEVT_PAINT, wxPaintEventHandler(wxStaticBox::OnPaint));
127 #endif // !__WXWINCE__
128
129 return true;
130 }
131
132 wxBorder wxStaticBox::GetDefaultBorder() const
133 {
134 return wxBORDER_NONE;
135 }
136
137 WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
138 {
139 long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
140
141 // no need for it anymore, must be removed for wxRadioBox child
142 // buttons to be able to repaint themselves
143 styleWin &= ~WS_CLIPCHILDREN;
144
145 if ( exstyle )
146 *exstyle = 0;
147
148 return styleWin | BS_GROUPBOX;
149 }
150
151 wxSize wxStaticBox::DoGetBestSize() const
152 {
153 int cx, cy;
154 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
155
156 int wBox;
157 GetTextExtent(wxGetWindowText(m_hWnd), &wBox, &cy);
158
159 wBox += 3*cx;
160 int hBox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
161
162 return wxSize(wBox, hBox);
163 }
164
165 void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
166 {
167 wxStaticBoxBase::GetBordersForSizer(borderTop, borderOther);
168
169 // need extra space, don't know how much but this seems to be enough
170 *borderTop += GetCharHeight()/3;
171 }
172
173 // all the hacks below are not necessary for WinCE
174 #ifndef __WXWINCE__
175
176 WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
177 {
178 if ( nMsg == WM_NCHITTEST )
179 {
180 // This code breaks some other processing such as enter/leave tracking
181 // so it's off by default.
182
183 static int s_useHTClient = -1;
184 if (s_useHTClient == -1)
185 s_useHTClient = wxSystemOptions::GetOptionInt(wxT("msw.staticbox.htclient"));
186 if (s_useHTClient == 1)
187 {
188 int xPos = GET_X_LPARAM(lParam);
189 int yPos = GET_Y_LPARAM(lParam);
190
191 ScreenToClient(&xPos, &yPos);
192
193 // Make sure you can drag by the top of the groupbox, but let
194 // other (enclosed) controls get mouse events also
195 if ( yPos < 10 )
196 return (long)HTCLIENT;
197 }
198 }
199
200 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
201 }
202
203 // ----------------------------------------------------------------------------
204 // static box drawing
205 // ----------------------------------------------------------------------------
206
207 /*
208 We draw the static box ourselves because it's the only way to prevent it
209 from flickering horribly on resize (because everything inside the box is
210 erased twice: once when the box itself is repainted and second time when
211 the control inside it is repainted) without using WS_EX_TRANSPARENT style as
212 we used to do and which resulted in other problems.
213 */
214
215 // MSWGetRegionWithoutSelf helper: removes the given rectangle from region
216 static inline void
217 SubtractRectFromRgn(HRGN hrgn, int left, int top, int right, int bottom)
218 {
219 AutoHRGN hrgnRect(::CreateRectRgn(left, top, right, bottom));
220 if ( !hrgnRect )
221 {
222 wxLogLastError(_T("CreateRectRgn()"));
223 return;
224 }
225
226 ::CombineRgn(hrgn, hrgn, hrgnRect, RGN_DIFF);
227 }
228
229 void wxStaticBox::MSWGetRegionWithoutSelf(WXHRGN hRgn, int w, int h)
230 {
231 HRGN hrgn = (HRGN)hRgn;
232
233 // remove the area occupied by the static box borders from the region
234 int borderTop, border;
235 GetBordersForSizer(&borderTop, &border);
236
237 // top
238 SubtractRectFromRgn(hrgn, 0, 0, w, borderTop);
239
240 // bottom
241 SubtractRectFromRgn(hrgn, 0, h - border, w, h);
242
243 // left
244 SubtractRectFromRgn(hrgn, 0, 0, border, h);
245
246 // right
247 SubtractRectFromRgn(hrgn, w - border, 0, w, h);
248 }
249
250 WXHRGN wxStaticBox::MSWGetRegionWithoutChildren()
251 {
252 RECT rc;
253 ::GetWindowRect(GetHwnd(), &rc);
254 HRGN hrgn = ::CreateRectRgn(rc.left, rc.top, rc.right + 1, rc.bottom + 1);
255
256 // iterate over all child windows (not just wxWindows but all windows)
257 for ( HWND child = ::GetWindow(GetHwndOf(GetParent()), GW_CHILD);
258 child;
259 child = ::GetWindow(child, GW_HWNDNEXT) )
260 {
261 if ( ! ::IsWindowVisible(child) )
262 {
263 // if the window isn't visible then it doesn't need clipped
264 continue;
265 }
266
267 LONG style = ::GetWindowLong(child, GWL_STYLE);
268 wxString str(wxGetWindowClass(child));
269 str.UpperCase();
270 if ( str == wxT("BUTTON") && (style & BS_GROUPBOX) != 0 )
271 {
272 // Don't clip any static boxes, not just this one. This will
273 // result in flicker in overlapping static boxes, but at least
274 // they will all be drawn correctly and we shouldn't have
275 // overlapping windows anyway.
276 continue;
277 }
278
279 ::GetWindowRect(child, &rc);
280 if ( ::RectInRegion(hrgn, &rc) )
281 {
282 // need to remove WS_CLIPSIBLINGS from all sibling windows
283 // that are within this staticbox if set
284 if ( style & WS_CLIPSIBLINGS )
285 {
286 style &= ~WS_CLIPSIBLINGS;
287 ::SetWindowLong(child, GWL_STYLE, style);
288
289 // MSDN: "If you have changed certain window data using
290 // SetWindowLong, you must call SetWindowPos to have the
291 // changes take effect."
292 ::SetWindowPos(child, NULL, 0, 0, 0, 0,
293 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
294 SWP_FRAMECHANGED);
295 }
296
297 AutoHRGN hrgnChild(::CreateRectRgnIndirect(&rc));
298 ::CombineRgn(hrgn, hrgn, hrgnChild, RGN_DIFF);
299 }
300 }
301
302 return (WXHRGN)hrgn;
303 }
304
305 // helper for OnPaint(): really erase the background, i.e. do it even if we
306 // don't have any non default brush for doing it (DoEraseBackground() doesn't
307 // do anything in such case)
308 void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
309 {
310 // note that we do not use the box background colour here, it shouldn't
311 // apply to its interior for several reasons:
312 // 1. wxGTK doesn't do it
313 // 2. controls inside the box don't get correct bg colour because they
314 // are not our children so we'd have some really ugly colour mix if
315 // we did it
316 // 3. this is backwards compatible behaviour and some people rely on it,
317 // see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
318 wxWindow *parent = GetParent();
319 HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(dc.GetHDC(), GetHWND());
320
321 // if there is no special brush for painting this control, just use the
322 // solid background colour
323 wxBrush brush;
324 if ( !hbr )
325 {
326 brush = wxBrush(parent->GetBackgroundColour());
327 hbr = GetHbrushOf(brush);
328 }
329
330 ::FillRect(GetHdcOf(dc), &rc, hbr);
331 }
332
333 void wxStaticBox::PaintForeground(wxDC& dc, const RECT& WXUNUSED(rc))
334 {
335 // NB: neither setting the text colour nor transparent background mode
336 // doesn't change anything: the static box def window proc still
337 // draws the label in its own colours, so if we want to have control
338 // over this we really have to draw everything ourselves
339 MSWDefWindowProc(WM_PAINT, (WPARAM)GetHdcOf(dc), 0);
340 }
341
342 void wxStaticBox::OnPaint(wxPaintEvent& WXUNUSED(event))
343 {
344 RECT rc;
345 ::GetClientRect(GetHwnd(), &rc);
346
347 // draw the entire box in a memory DC
348 wxMemoryDC memdc;
349 wxBitmap bitmap(rc.right, rc.bottom);
350 memdc.SelectObject(bitmap);
351
352 PaintBackground(memdc, rc);
353 PaintForeground(memdc, rc);
354
355 // now only blit the static box border itself, not the interior, to avoid
356 // flicker when background is drawn below
357 //
358 // note that it seems to be faster to do 4 small blits here and then paint
359 // directly into wxPaintDC than painting background in wxMemoryDC and then
360 // blitting everything at once to wxPaintDC, this is why we do it like this
361 wxPaintDC dc(this);
362 int borderTop, border;
363 GetBordersForSizer(&borderTop, &border);
364
365 // top
366 dc.Blit(border, 0, rc.right - border, borderTop,
367 &memdc, border, 0);
368 // bottom
369 dc.Blit(border, rc.bottom - border, rc.right - border, rc.bottom,
370 &memdc, border, rc.bottom - border);
371 // left
372 dc.Blit(0, 0, border, rc.bottom,
373 &memdc, 0, 0);
374 // right
375 dc.Blit(rc.right - border, 0, rc.right, rc.bottom,
376 &memdc, rc.right - border, 0);
377
378
379 // create the region excluding box children
380 AutoHRGN hrgn((HRGN)MSWGetRegionWithoutChildren());
381 RECT rcWin;
382 ::GetWindowRect(GetHwnd(), &rcWin);
383 ::OffsetRgn(hrgn, -rcWin.left, -rcWin.top);
384
385 // and also the box itself
386 MSWGetRegionWithoutSelf((WXHRGN) hrgn, rc.right, rc.bottom);
387 HDCClipper clipToBg(GetHdcOf(dc), hrgn);
388
389 // paint the inside of the box (excluding box itself and child controls)
390 PaintBackground(dc, rc);
391 }
392
393 #endif // !__WXWINCE__
394
395 #endif // wxUSE_STATBOX
396