]>
Commit | Line | Data |
---|---|---|
56873923 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/generic/headerctrlg.cpp | |
3 | // Purpose: generic wxHeaderCtrl implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2008-12-03 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #endif // WX_PRECOMP | |
28 | ||
29 | #include "wx/headerctrl.h" | |
30 | ||
31 | #ifdef wxHAS_GENERIC_HEADERCTRL | |
32 | ||
b63f9a33 VZ |
33 | #include "wx/dcbuffer.h" |
34 | #include "wx/renderer.h" | |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // constants | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | namespace | |
41 | { | |
42 | ||
43 | const unsigned NO_SORT = (unsigned)-1; | |
44 | ||
45 | const unsigned COL_NONE = (unsigned)-1; | |
46 | ||
47 | } // anonymous namespace | |
48 | ||
56873923 | 49 | // ============================================================================ |
b63f9a33 | 50 | // wxHeaderCtrl implementation |
56873923 VZ |
51 | // ============================================================================ |
52 | ||
b63f9a33 VZ |
53 | // ---------------------------------------------------------------------------- |
54 | // wxHeaderCtrl creation | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
57 | void wxHeaderCtrl::Init() | |
58 | { | |
40dc2ae6 | 59 | m_numColumns = 0; |
aef252d9 VZ |
60 | m_hover = |
61 | m_colBeingResized = COL_NONE; | |
b63f9a33 VZ |
62 | m_scrollOffset = 0; |
63 | } | |
64 | ||
65 | bool wxHeaderCtrl::Create(wxWindow *parent, | |
66 | wxWindowID id, | |
67 | const wxPoint& pos, | |
68 | const wxSize& size, | |
69 | long style, | |
70 | const wxString& name) | |
71 | { | |
72 | if ( !wxHeaderCtrlBase::Create(parent, id, pos, size, | |
73 | style, wxDefaultValidator, name) ) | |
74 | return false; | |
75 | ||
76 | // tell the system to not paint the background at all to avoid flicker as | |
77 | // we paint the entire window area in our OnPaint() | |
78 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
79 | ||
80 | return true; | |
81 | } | |
82 | ||
83 | wxHeaderCtrl::~wxHeaderCtrl() | |
84 | { | |
85 | } | |
86 | ||
87 | // ---------------------------------------------------------------------------- | |
88 | // wxHeaderCtrl columns manipulation | |
89 | // ---------------------------------------------------------------------------- | |
90 | ||
40dc2ae6 VZ |
91 | void wxHeaderCtrl::DoSetCount(unsigned int count) |
92 | { | |
93 | m_numColumns = count; | |
94 | ||
95 | Refresh(); | |
96 | } | |
97 | ||
b63f9a33 VZ |
98 | unsigned int wxHeaderCtrl::DoGetCount() const |
99 | { | |
40dc2ae6 VZ |
100 | return m_numColumns; |
101 | } | |
102 | ||
103 | void wxHeaderCtrl::DoUpdate(unsigned int idx) | |
104 | { | |
105 | // we need to refresh not only this column but also the ones after it in | |
106 | // case it was shown or hidden or its width changed -- it would be nice to | |
107 | // avoid doing this unnecessary by storing the old column width (TODO) | |
108 | RefreshColsAfter(idx); | |
b63f9a33 VZ |
109 | } |
110 | ||
b63f9a33 VZ |
111 | // ---------------------------------------------------------------------------- |
112 | // wxHeaderCtrl scrolling | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
115 | void wxHeaderCtrl::DoScrollHorz(int dx) | |
116 | { | |
117 | m_scrollOffset += dx; | |
118 | ||
119 | // don't call our own version which calls this function! | |
120 | wxControl::ScrollWindow(dx, 0); | |
121 | } | |
122 | ||
123 | // ---------------------------------------------------------------------------- | |
124 | // wxHeaderCtrl geometry | |
125 | // ---------------------------------------------------------------------------- | |
126 | ||
127 | wxSize wxHeaderCtrl::DoGetBestSize() const | |
128 | { | |
129 | // the vertical size is rather arbitrary but it looks better if we leave | |
130 | // some space around the text | |
131 | return wxSize(GetColStart(GetColumnCount()), (7*GetCharHeight())/4); | |
132 | } | |
133 | ||
134 | int wxHeaderCtrl::GetColStart(unsigned int idx) const | |
135 | { | |
40dc2ae6 VZ |
136 | wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this); |
137 | ||
04a33b50 | 138 | int pos = m_scrollOffset; |
b63f9a33 VZ |
139 | for ( unsigned n = 0; n < idx; n++ ) |
140 | { | |
40dc2ae6 | 141 | const wxHeaderColumnBase& col = self->GetColumn(n); |
b63f9a33 VZ |
142 | if ( col.IsShown() ) |
143 | pos += col.GetWidth(); | |
144 | } | |
145 | ||
146 | return pos; | |
147 | } | |
148 | ||
fa3d4aaf VZ |
149 | int wxHeaderCtrl::FindColumnAtPos(int x, bool& onSeparator) const |
150 | { | |
151 | wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this); | |
152 | ||
153 | int pos = 0; | |
154 | const unsigned count = GetColumnCount(); | |
155 | for ( unsigned n = 0; n < count; n++ ) | |
156 | { | |
157 | const wxHeaderColumnBase& col = self->GetColumn(n); | |
158 | if ( col.IsHidden() ) | |
159 | continue; | |
160 | ||
161 | pos += col.GetWidth(); | |
162 | ||
163 | // if the column is resizeable, check if we're approximatively over the | |
164 | // line separating it from the next column | |
165 | // | |
166 | // TODO: don't hardcode sensitivity | |
167 | if ( col.IsResizeable() && abs(x - pos) < 8 ) | |
168 | { | |
169 | onSeparator = true; | |
170 | return n; | |
171 | } | |
172 | ||
173 | // inside this column? | |
174 | if ( x < pos ) | |
175 | { | |
176 | onSeparator = false; | |
177 | return n; | |
178 | } | |
179 | } | |
180 | ||
181 | return COL_NONE; | |
182 | } | |
183 | ||
b63f9a33 VZ |
184 | // ---------------------------------------------------------------------------- |
185 | // wxHeaderCtrl repainting | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | void wxHeaderCtrl::RefreshCol(unsigned int idx) | |
189 | { | |
190 | wxRect rect = GetClientRect(); | |
191 | rect.x += GetColStart(idx); | |
40dc2ae6 | 192 | rect.width = GetColumn(idx).GetWidth(); |
b63f9a33 VZ |
193 | |
194 | RefreshRect(rect); | |
195 | } | |
196 | ||
6090efab VZ |
197 | void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx) |
198 | { | |
199 | if ( idx != COL_NONE ) | |
200 | RefreshCol(idx); | |
201 | } | |
202 | ||
b63f9a33 VZ |
203 | void wxHeaderCtrl::RefreshColsAfter(unsigned int idx) |
204 | { | |
205 | wxRect rect = GetClientRect(); | |
206 | const int ofs = GetColStart(idx); | |
207 | rect.x += ofs; | |
208 | rect.width -= ofs; | |
209 | ||
210 | RefreshRect(rect); | |
211 | } | |
212 | ||
aef252d9 VZ |
213 | // ---------------------------------------------------------------------------- |
214 | // wxHeaderCtrl dragging | |
215 | // ---------------------------------------------------------------------------- | |
216 | ||
e36dcd10 VZ |
217 | bool wxHeaderCtrl::IsResizing() const |
218 | { | |
219 | return m_colBeingResized != COL_NONE; | |
220 | } | |
221 | ||
aef252d9 VZ |
222 | void wxHeaderCtrl::UpdateResizingMarker(int xPhysical) |
223 | { | |
224 | // unfortunately drawing the marker over the parent window doesn't work as | |
225 | // it's usually covered by another window (the main control view) so just | |
226 | // draw the marker over the header itself, even if it makes it not very | |
227 | // useful | |
228 | wxClientDC dc(this); | |
229 | ||
230 | wxDCOverlay dcover(m_overlay, &dc); | |
231 | dcover.Clear(); | |
232 | ||
233 | if ( xPhysical != -1 ) | |
234 | { | |
235 | dc.SetPen(*wxLIGHT_GREY_PEN); | |
236 | dc.DrawLine(xPhysical, 0, xPhysical, GetClientSize().y); | |
237 | } | |
238 | } | |
239 | ||
240 | void wxHeaderCtrl::EndDragging() | |
241 | { | |
242 | UpdateResizingMarker(-1); | |
243 | ||
244 | m_overlay.Reset(); | |
10118a24 VZ |
245 | |
246 | // don't use the special dragging cursor any more | |
247 | SetCursor(wxNullCursor); | |
aef252d9 VZ |
248 | } |
249 | ||
250 | void wxHeaderCtrl::EndResizing(int width) | |
251 | { | |
e36dcd10 | 252 | wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" ); |
aef252d9 VZ |
253 | |
254 | EndDragging(); | |
255 | ||
10118a24 VZ |
256 | // if dragging was cancelled we must have already lost the mouse capture so |
257 | // don't try to release it | |
258 | if ( width != -1 ) | |
259 | ReleaseMouse(); | |
260 | ||
396825dc | 261 | wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_RESIZE, GetId()); |
aef252d9 VZ |
262 | event.SetEventObject(this); |
263 | event.SetColumn(m_colBeingResized); | |
264 | if ( width == -1 ) | |
265 | event.SetCancelled(); | |
266 | else | |
267 | event.SetWidth(width); | |
268 | ||
269 | GetEventHandler()->ProcessEvent(event); | |
270 | ||
271 | m_colBeingResized = COL_NONE; | |
272 | } | |
273 | ||
b63f9a33 VZ |
274 | // ---------------------------------------------------------------------------- |
275 | // wxHeaderCtrl event handlers | |
276 | // ---------------------------------------------------------------------------- | |
277 | ||
3bfaa5a7 | 278 | BEGIN_EVENT_TABLE(wxHeaderCtrl, wxHeaderCtrlBase) |
b63f9a33 VZ |
279 | EVT_PAINT(wxHeaderCtrl::OnPaint) |
280 | ||
281 | EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse) | |
aef252d9 VZ |
282 | |
283 | EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost) | |
e36dcd10 VZ |
284 | |
285 | EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown) | |
b63f9a33 VZ |
286 | END_EVENT_TABLE() |
287 | ||
288 | void wxHeaderCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
289 | { | |
290 | int w, h; | |
291 | GetClientSize(&w, &h); | |
292 | ||
293 | wxAutoBufferedPaintDC dc(this); | |
294 | ||
295 | dc.SetBackground(GetBackgroundColour()); | |
296 | dc.Clear(); | |
297 | ||
298 | // account for the horizontal scrollbar offset in the parent window | |
299 | dc.SetDeviceOrigin(m_scrollOffset, 0); | |
300 | ||
40dc2ae6 | 301 | const unsigned int count = m_numColumns; |
b63f9a33 VZ |
302 | int xpos = 0; |
303 | for ( unsigned int i = 0; i < count; i++ ) | |
304 | { | |
40dc2ae6 | 305 | const wxHeaderColumnBase& col = GetColumn(i); |
b63f9a33 VZ |
306 | if ( col.IsHidden() ) |
307 | continue; | |
308 | ||
309 | const int colWidth = col.GetWidth(); | |
310 | ||
311 | wxHeaderSortIconType sortArrow; | |
40dc2ae6 | 312 | if ( col.IsSortKey() ) |
b63f9a33 | 313 | { |
40dc2ae6 VZ |
314 | sortArrow = col.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP |
315 | : wxHDR_SORT_ICON_DOWN; | |
316 | } | |
317 | else // not sorting by this column | |
318 | { | |
319 | sortArrow = wxHDR_SORT_ICON_NONE; | |
b63f9a33 VZ |
320 | } |
321 | ||
322 | int state = 0; | |
323 | if ( IsEnabled() ) | |
324 | { | |
325 | if ( i == m_hover ) | |
326 | state = wxCONTROL_CURRENT; | |
327 | } | |
328 | else // disabled | |
329 | { | |
330 | state = wxCONTROL_DISABLED; | |
331 | } | |
332 | ||
333 | wxHeaderButtonParams params; | |
334 | params.m_labelText = col.GetTitle(); | |
335 | params.m_labelBitmap = col.GetBitmap(); | |
336 | params.m_labelAlignment = col.GetAlignment(); | |
337 | ||
338 | wxRendererNative::Get().DrawHeaderButton | |
339 | ( | |
340 | this, | |
341 | dc, | |
342 | wxRect(xpos, 0, colWidth, h), | |
343 | state, | |
344 | sortArrow, | |
345 | ¶ms | |
346 | ); | |
347 | ||
348 | xpos += colWidth; | |
349 | } | |
350 | } | |
351 | ||
aef252d9 VZ |
352 | void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) |
353 | { | |
e36dcd10 VZ |
354 | if ( IsResizing() ) |
355 | EndResizing(-1); | |
356 | } | |
357 | ||
358 | void wxHeaderCtrl::OnKeyDown(wxKeyEvent& event) | |
359 | { | |
360 | if ( IsResizing() && event.GetKeyCode() == WXK_ESCAPE ) | |
361 | { | |
362 | ReleaseMouse(); | |
aef252d9 | 363 | EndResizing(-1); |
e36dcd10 VZ |
364 | } |
365 | else | |
366 | { | |
367 | event.Skip(); | |
368 | } | |
aef252d9 VZ |
369 | } |
370 | ||
fa3d4aaf | 371 | void wxHeaderCtrl::OnMouse(wxMouseEvent& mevent) |
b63f9a33 | 372 | { |
aef252d9 VZ |
373 | // do this in advance to allow simply returning if we're not interested, |
374 | // we'll undo it if we do handle the event below | |
fa3d4aaf VZ |
375 | mevent.Skip(); |
376 | ||
aef252d9 | 377 | |
04a33b50 | 378 | // account for the control displacement |
aef252d9 VZ |
379 | const int xPhysical = mevent.GetX(); |
380 | const int xLogical = xPhysical - m_scrollOffset; | |
381 | ||
382 | // first deal with the [continuation of any] dragging operations in | |
383 | // progress | |
e36dcd10 | 384 | if ( IsResizing() ) |
aef252d9 VZ |
385 | { |
386 | if ( mevent.LeftUp() ) | |
8fad69b0 | 387 | EndResizing(xPhysical - GetColStart(m_colBeingResized)); |
aef252d9 VZ |
388 | else // update the live separator position |
389 | UpdateResizingMarker(xPhysical); | |
390 | ||
391 | return; | |
392 | } | |
393 | ||
04a33b50 | 394 | |
fa3d4aaf VZ |
395 | // find if the event is over a column at all |
396 | bool onSeparator; | |
6090efab VZ |
397 | const unsigned col = mevent.Leaving() |
398 | ? (onSeparator = false, COL_NONE) | |
aef252d9 VZ |
399 | : FindColumnAtPos(xLogical, onSeparator); |
400 | ||
6090efab VZ |
401 | |
402 | // update the highlighted column if it changed | |
403 | if ( col != m_hover ) | |
404 | { | |
405 | const unsigned hoverOld = m_hover; | |
406 | m_hover = col; | |
407 | ||
408 | RefreshColIfNotNone(hoverOld); | |
409 | RefreshColIfNotNone(m_hover); | |
410 | } | |
411 | ||
fa3d4aaf VZ |
412 | // update mouse cursor as it moves around |
413 | if ( mevent.Moving() ) | |
414 | { | |
415 | SetCursor(onSeparator ? wxCursor(wxCURSOR_SIZEWE) : wxNullCursor); | |
416 | return; | |
417 | } | |
418 | ||
aef252d9 VZ |
419 | // all the other events only make sense when they happen over a column |
420 | if ( col == COL_NONE ) | |
421 | return; | |
422 | ||
423 | ||
424 | // enter various dragging modes on left mouse press | |
fa3d4aaf VZ |
425 | if ( mevent.LeftDown() ) |
426 | { | |
fa3d4aaf | 427 | if ( onSeparator ) |
aef252d9 VZ |
428 | { |
429 | // start resizing the column | |
430 | m_colBeingResized = col; | |
10118a24 VZ |
431 | SetCursor(wxCursor(wxCURSOR_SIZEWE)); |
432 | CaptureMouse(); | |
aef252d9 VZ |
433 | UpdateResizingMarker(xPhysical); |
434 | } | |
435 | else // on column itself | |
436 | { | |
437 | // TODO: drag column | |
fa3d4aaf | 438 | ; |
aef252d9 | 439 | } |
fa3d4aaf VZ |
440 | |
441 | return; | |
442 | } | |
443 | ||
aef252d9 | 444 | // determine the type of header event corresponding to click events |
3bfaa5a7 VZ |
445 | wxEventType evtType = wxEVT_NULL; |
446 | const bool click = mevent.ButtonUp(), | |
447 | dblclk = mevent.ButtonDClick(); | |
448 | if ( click || dblclk ) | |
fa3d4aaf VZ |
449 | { |
450 | switch ( mevent.GetButton() ) | |
451 | { | |
452 | case wxMOUSE_BTN_LEFT: | |
3bfaa5a7 VZ |
453 | // treat left double clicks on separator specially |
454 | if ( onSeparator && dblclk ) | |
455 | { | |
456 | evtType = wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK; | |
457 | } | |
458 | else // not double click on separator | |
459 | { | |
460 | evtType = click ? wxEVT_COMMAND_HEADER_CLICK | |
461 | : wxEVT_COMMAND_HEADER_DCLICK; | |
462 | } | |
fa3d4aaf VZ |
463 | break; |
464 | ||
465 | case wxMOUSE_BTN_RIGHT: | |
466 | evtType = click ? wxEVT_COMMAND_HEADER_RIGHT_CLICK | |
467 | : wxEVT_COMMAND_HEADER_RIGHT_DCLICK; | |
468 | break; | |
469 | ||
470 | case wxMOUSE_BTN_MIDDLE: | |
471 | evtType = click ? wxEVT_COMMAND_HEADER_MIDDLE_CLICK | |
472 | : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK; | |
473 | break; | |
474 | ||
475 | default: | |
476 | // ignore clicks from other mouse buttons | |
3bfaa5a7 | 477 | ; |
fa3d4aaf | 478 | } |
3bfaa5a7 VZ |
479 | } |
480 | ||
481 | if ( evtType == wxEVT_NULL ) | |
482 | return; | |
fa3d4aaf | 483 | |
3bfaa5a7 VZ |
484 | wxHeaderCtrlEvent event(evtType, GetId()); |
485 | event.SetEventObject(this); | |
486 | event.SetColumn(col); | |
fa3d4aaf | 487 | |
3bfaa5a7 VZ |
488 | if ( GetEventHandler()->ProcessEvent(event) ) |
489 | mevent.Skip(false); | |
b63f9a33 VZ |
490 | } |
491 | ||
56873923 | 492 | #endif // wxHAS_GENERIC_HEADERCTRL |