]>
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 | 60 | m_hover = |
6327cdd5 VZ |
61 | m_colBeingResized = |
62 | m_colBeingReordered = COL_NONE; | |
63 | m_dragOffset = 0; | |
b63f9a33 VZ |
64 | m_scrollOffset = 0; |
65 | } | |
66 | ||
67 | bool wxHeaderCtrl::Create(wxWindow *parent, | |
68 | wxWindowID id, | |
69 | const wxPoint& pos, | |
70 | const wxSize& size, | |
71 | long style, | |
72 | const wxString& name) | |
73 | { | |
74 | if ( !wxHeaderCtrlBase::Create(parent, id, pos, size, | |
75 | style, wxDefaultValidator, name) ) | |
76 | return false; | |
77 | ||
78 | // tell the system to not paint the background at all to avoid flicker as | |
79 | // we paint the entire window area in our OnPaint() | |
80 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
81 | ||
82 | return true; | |
83 | } | |
84 | ||
85 | wxHeaderCtrl::~wxHeaderCtrl() | |
86 | { | |
87 | } | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // wxHeaderCtrl columns manipulation | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
40dc2ae6 VZ |
93 | void wxHeaderCtrl::DoSetCount(unsigned int count) |
94 | { | |
f6655391 VZ |
95 | // update the column indices order array before changing m_numColumns |
96 | DoResizeColumnIndices(m_colIndices, count); | |
74d283aa | 97 | |
40dc2ae6 VZ |
98 | m_numColumns = count; |
99 | ||
7bf5be6b | 100 | InvalidateBestSize(); |
40dc2ae6 VZ |
101 | Refresh(); |
102 | } | |
103 | ||
b63f9a33 VZ |
104 | unsigned int wxHeaderCtrl::DoGetCount() const |
105 | { | |
40dc2ae6 VZ |
106 | return m_numColumns; |
107 | } | |
108 | ||
109 | void wxHeaderCtrl::DoUpdate(unsigned int idx) | |
110 | { | |
7bf5be6b VZ |
111 | InvalidateBestSize(); |
112 | ||
40dc2ae6 VZ |
113 | // we need to refresh not only this column but also the ones after it in |
114 | // case it was shown or hidden or its width changed -- it would be nice to | |
115 | // avoid doing this unnecessary by storing the old column width (TODO) | |
116 | RefreshColsAfter(idx); | |
b63f9a33 VZ |
117 | } |
118 | ||
b63f9a33 VZ |
119 | // ---------------------------------------------------------------------------- |
120 | // wxHeaderCtrl scrolling | |
121 | // ---------------------------------------------------------------------------- | |
122 | ||
123 | void wxHeaderCtrl::DoScrollHorz(int dx) | |
124 | { | |
125 | m_scrollOffset += dx; | |
126 | ||
127 | // don't call our own version which calls this function! | |
128 | wxControl::ScrollWindow(dx, 0); | |
129 | } | |
130 | ||
131 | // ---------------------------------------------------------------------------- | |
132 | // wxHeaderCtrl geometry | |
133 | // ---------------------------------------------------------------------------- | |
134 | ||
135 | wxSize wxHeaderCtrl::DoGetBestSize() const | |
136 | { | |
137 | // the vertical size is rather arbitrary but it looks better if we leave | |
138 | // some space around the text | |
7bf5be6b VZ |
139 | const wxSize size(IsEmpty() ? wxHeaderCtrlBase::DoGetBestSize().x |
140 | : GetColEnd(GetColumnCount() - 1), | |
141 | (7*GetCharHeight())/4); | |
142 | CacheBestSize(size); | |
143 | return size; | |
b63f9a33 VZ |
144 | } |
145 | ||
146 | int wxHeaderCtrl::GetColStart(unsigned int idx) const | |
147 | { | |
40dc2ae6 VZ |
148 | wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this); |
149 | ||
04a33b50 | 150 | int pos = m_scrollOffset; |
6327cdd5 | 151 | for ( unsigned n = 0; ; n++ ) |
b63f9a33 | 152 | { |
6327cdd5 VZ |
153 | const unsigned i = m_colIndices[n]; |
154 | if ( i == idx ) | |
155 | break; | |
156 | ||
dcb6cbec | 157 | const wxHeaderColumn& col = self->GetColumn(i); |
b63f9a33 VZ |
158 | if ( col.IsShown() ) |
159 | pos += col.GetWidth(); | |
160 | } | |
161 | ||
162 | return pos; | |
163 | } | |
164 | ||
6327cdd5 VZ |
165 | int wxHeaderCtrl::GetColEnd(unsigned int idx) const |
166 | { | |
167 | int x = GetColStart(idx); | |
168 | ||
169 | return x + const_cast<wxHeaderCtrl *>(this)->GetColumn(idx).GetWidth(); | |
170 | } | |
171 | ||
172 | unsigned int wxHeaderCtrl::FindColumnAtPoint(int x, bool *onSeparator) const | |
fa3d4aaf VZ |
173 | { |
174 | wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this); | |
175 | ||
176 | int pos = 0; | |
177 | const unsigned count = GetColumnCount(); | |
178 | for ( unsigned n = 0; n < count; n++ ) | |
179 | { | |
6327cdd5 | 180 | const unsigned idx = m_colIndices[n]; |
dcb6cbec | 181 | const wxHeaderColumn& col = self->GetColumn(idx); |
fa3d4aaf VZ |
182 | if ( col.IsHidden() ) |
183 | continue; | |
184 | ||
185 | pos += col.GetWidth(); | |
186 | ||
187 | // if the column is resizeable, check if we're approximatively over the | |
188 | // line separating it from the next column | |
189 | // | |
190 | // TODO: don't hardcode sensitivity | |
191 | if ( col.IsResizeable() && abs(x - pos) < 8 ) | |
192 | { | |
6327cdd5 VZ |
193 | if ( onSeparator ) |
194 | *onSeparator = true; | |
195 | return idx; | |
fa3d4aaf VZ |
196 | } |
197 | ||
198 | // inside this column? | |
199 | if ( x < pos ) | |
200 | { | |
6327cdd5 VZ |
201 | if ( onSeparator ) |
202 | *onSeparator = false; | |
203 | return idx; | |
fa3d4aaf VZ |
204 | } |
205 | } | |
206 | ||
22f4180e VZ |
207 | if ( onSeparator ) |
208 | *onSeparator = false; | |
fa3d4aaf VZ |
209 | return COL_NONE; |
210 | } | |
211 | ||
b63f9a33 VZ |
212 | // ---------------------------------------------------------------------------- |
213 | // wxHeaderCtrl repainting | |
214 | // ---------------------------------------------------------------------------- | |
215 | ||
216 | void wxHeaderCtrl::RefreshCol(unsigned int idx) | |
217 | { | |
218 | wxRect rect = GetClientRect(); | |
219 | rect.x += GetColStart(idx); | |
40dc2ae6 | 220 | rect.width = GetColumn(idx).GetWidth(); |
b63f9a33 VZ |
221 | |
222 | RefreshRect(rect); | |
223 | } | |
224 | ||
6090efab VZ |
225 | void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx) |
226 | { | |
227 | if ( idx != COL_NONE ) | |
228 | RefreshCol(idx); | |
229 | } | |
230 | ||
b63f9a33 VZ |
231 | void wxHeaderCtrl::RefreshColsAfter(unsigned int idx) |
232 | { | |
233 | wxRect rect = GetClientRect(); | |
234 | const int ofs = GetColStart(idx); | |
235 | rect.x += ofs; | |
236 | rect.width -= ofs; | |
237 | ||
238 | RefreshRect(rect); | |
239 | } | |
240 | ||
aef252d9 | 241 | // ---------------------------------------------------------------------------- |
6327cdd5 | 242 | // wxHeaderCtrl dragging/resizing/reordering |
aef252d9 VZ |
243 | // ---------------------------------------------------------------------------- |
244 | ||
e36dcd10 VZ |
245 | bool wxHeaderCtrl::IsResizing() const |
246 | { | |
247 | return m_colBeingResized != COL_NONE; | |
248 | } | |
249 | ||
6327cdd5 VZ |
250 | bool wxHeaderCtrl::IsReordering() const |
251 | { | |
252 | return m_colBeingReordered != COL_NONE; | |
253 | } | |
254 | ||
255 | void wxHeaderCtrl::ClearMarkers() | |
256 | { | |
257 | wxClientDC dc(this); | |
258 | ||
259 | wxDCOverlay dcover(m_overlay, &dc); | |
260 | dcover.Clear(); | |
261 | } | |
262 | ||
aef252d9 VZ |
263 | void wxHeaderCtrl::UpdateResizingMarker(int xPhysical) |
264 | { | |
aef252d9 VZ |
265 | wxClientDC dc(this); |
266 | ||
267 | wxDCOverlay dcover(m_overlay, &dc); | |
268 | dcover.Clear(); | |
269 | ||
6327cdd5 VZ |
270 | // unfortunately drawing the marker over the parent window doesn't work as |
271 | // it's usually covered by another window (the main control view) so just | |
272 | // draw the marker over the header itself, even if it makes it not very | |
273 | // useful | |
274 | dc.SetPen(*wxLIGHT_GREY_PEN); | |
275 | dc.DrawLine(xPhysical, 0, xPhysical, GetClientSize().y); | |
aef252d9 VZ |
276 | } |
277 | ||
278 | void wxHeaderCtrl::EndDragging() | |
279 | { | |
6327cdd5 | 280 | ClearMarkers(); |
aef252d9 VZ |
281 | |
282 | m_overlay.Reset(); | |
10118a24 VZ |
283 | |
284 | // don't use the special dragging cursor any more | |
285 | SetCursor(wxNullCursor); | |
aef252d9 VZ |
286 | } |
287 | ||
6327cdd5 VZ |
288 | void wxHeaderCtrl::CancelDragging() |
289 | { | |
290 | wxASSERT_MSG( IsDragging(), | |
291 | "shouldn't be called if we're not dragging anything" ); | |
292 | ||
293 | EndDragging(); | |
294 | ||
295 | unsigned int& col = IsResizing() ? m_colBeingResized : m_colBeingReordered; | |
296 | ||
297 | wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED, GetId()); | |
298 | event.SetEventObject(this); | |
299 | event.SetColumn(col); | |
300 | ||
301 | GetEventHandler()->ProcessEvent(event); | |
302 | ||
303 | col = COL_NONE; | |
304 | } | |
305 | ||
a45caa71 VZ |
306 | int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col, int& xPhysical) |
307 | { | |
308 | const int xStart = GetColStart(col); | |
309 | ||
310 | // notice that GetMinWidth() returns 0 if there is no minimal width so it | |
311 | // still makes sense to use it even in this case | |
312 | const int xMinEnd = xStart + GetColumn(col).GetMinWidth(); | |
313 | ||
314 | if ( xPhysical < xMinEnd ) | |
315 | xPhysical = xMinEnd; | |
316 | ||
317 | return xPhysical - xStart; | |
318 | } | |
319 | ||
320 | void wxHeaderCtrl::StartOrContinueResizing(unsigned int col, int xPhysical) | |
321 | { | |
322 | wxHeaderCtrlEvent event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING | |
323 | : wxEVT_COMMAND_HEADER_BEGIN_RESIZE, | |
324 | GetId()); | |
325 | event.SetEventObject(this); | |
326 | event.SetColumn(col); | |
327 | ||
328 | event.SetWidth(ConstrainByMinWidth(col, xPhysical)); | |
329 | ||
330 | if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() ) | |
331 | { | |
332 | if ( IsResizing() ) | |
333 | { | |
334 | ReleaseMouse(); | |
6327cdd5 | 335 | CancelDragging(); |
a45caa71 VZ |
336 | } |
337 | //else: nothing to do -- we just don't start to resize | |
338 | } | |
339 | else // go ahead with resizing | |
340 | { | |
341 | if ( !IsResizing() ) | |
342 | { | |
343 | m_colBeingResized = col; | |
344 | SetCursor(wxCursor(wxCURSOR_SIZEWE)); | |
345 | CaptureMouse(); | |
346 | } | |
347 | //else: we had already done the above when we started | |
348 | ||
349 | UpdateResizingMarker(xPhysical); | |
350 | } | |
351 | } | |
352 | ||
353 | void wxHeaderCtrl::EndResizing(int xPhysical) | |
aef252d9 | 354 | { |
e36dcd10 | 355 | wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" ); |
aef252d9 VZ |
356 | |
357 | EndDragging(); | |
358 | ||
6327cdd5 | 359 | ReleaseMouse(); |
565804f2 | 360 | |
6327cdd5 | 361 | wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_RESIZE, GetId()); |
aef252d9 VZ |
362 | event.SetEventObject(this); |
363 | event.SetColumn(m_colBeingResized); | |
6327cdd5 | 364 | event.SetWidth(ConstrainByMinWidth(m_colBeingResized, xPhysical)); |
aef252d9 VZ |
365 | |
366 | GetEventHandler()->ProcessEvent(event); | |
367 | ||
368 | m_colBeingResized = COL_NONE; | |
369 | } | |
370 | ||
6327cdd5 VZ |
371 | void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical) |
372 | { | |
373 | wxClientDC dc(this); | |
374 | ||
375 | wxDCOverlay dcover(m_overlay, &dc); | |
376 | dcover.Clear(); | |
377 | ||
378 | dc.SetPen(*wxBLUE); | |
379 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
380 | ||
381 | // draw the phantom position of the column being dragged | |
382 | int x = xPhysical - m_dragOffset; | |
383 | int y = GetClientSize().y; | |
384 | dc.DrawRectangle(x, 0, | |
385 | GetColumn(m_colBeingReordered).GetWidth(), y); | |
386 | ||
387 | // and also a hint indicating where it is going to be inserted if it's | |
388 | // dropped now | |
389 | unsigned int col = FindColumnAtPoint(xPhysical); | |
390 | if ( col != COL_NONE ) | |
391 | { | |
392 | static const int DROP_MARKER_WIDTH = 4; | |
393 | ||
394 | dc.SetBrush(*wxBLUE); | |
395 | dc.DrawRectangle(GetColEnd(col) - DROP_MARKER_WIDTH/2, 0, | |
396 | DROP_MARKER_WIDTH, y); | |
397 | } | |
398 | } | |
399 | ||
400 | void wxHeaderCtrl::StartReordering(unsigned int col, int xPhysical) | |
401 | { | |
402 | wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_BEGIN_REORDER, GetId()); | |
403 | event.SetEventObject(this); | |
404 | event.SetColumn(col); | |
405 | ||
406 | if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() ) | |
407 | { | |
408 | // don't start dragging it, nothing to do otherwise | |
409 | return; | |
410 | } | |
411 | ||
412 | m_dragOffset = xPhysical - GetColStart(col); | |
413 | ||
414 | m_colBeingReordered = col; | |
415 | SetCursor(wxCursor(wxCURSOR_HAND)); | |
416 | CaptureMouse(); | |
417 | ||
59704b49 VZ |
418 | // do not call UpdateReorderingMarker() here: we don't want to give |
419 | // feedback for reordering until the user starts to really move the mouse | |
420 | // as he might want to just click on the column and not move it at all | |
6327cdd5 VZ |
421 | } |
422 | ||
59704b49 | 423 | bool wxHeaderCtrl::EndReordering(int xPhysical) |
6327cdd5 VZ |
424 | { |
425 | wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" ); | |
426 | ||
427 | EndDragging(); | |
428 | ||
429 | ReleaseMouse(); | |
430 | ||
59704b49 VZ |
431 | const int colOld = m_colBeingReordered, |
432 | colNew = FindColumnAtPoint(xPhysical); | |
6327cdd5 | 433 | |
59704b49 | 434 | m_colBeingReordered = COL_NONE; |
6327cdd5 | 435 | |
59704b49 VZ |
436 | if ( xPhysical - GetColStart(colOld) == m_dragOffset ) |
437 | return false; | |
438 | ||
439 | if ( colNew != colOld ) | |
6327cdd5 | 440 | { |
59704b49 VZ |
441 | wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_REORDER, GetId()); |
442 | event.SetEventObject(this); | |
443 | event.SetColumn(colOld); | |
444 | ||
445 | const unsigned pos = GetColumnPos(FindColumnAtPoint(xPhysical)); | |
446 | event.SetNewOrder(pos); | |
447 | ||
448 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
449 | { | |
450 | // do reorder the columns | |
451 | DoMoveCol(colOld, pos); | |
452 | } | |
6327cdd5 VZ |
453 | } |
454 | ||
59704b49 VZ |
455 | // whether we moved the column or not, the user did move the mouse and so |
456 | // did try to do it so return true | |
457 | return true; | |
6327cdd5 VZ |
458 | } |
459 | ||
20db5469 VZ |
460 | // ---------------------------------------------------------------------------- |
461 | // wxHeaderCtrl column reordering | |
462 | // ---------------------------------------------------------------------------- | |
463 | ||
464 | void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt& order) | |
465 | { | |
466 | m_colIndices = order; | |
467 | Refresh(); | |
468 | } | |
469 | ||
470 | wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const | |
471 | { | |
472 | return m_colIndices; | |
473 | } | |
474 | ||
6327cdd5 VZ |
475 | void wxHeaderCtrl::DoMoveCol(unsigned int idx, unsigned int pos) |
476 | { | |
1bb74626 | 477 | MoveColumnInOrderArray(m_colIndices, idx, pos); |
6327cdd5 VZ |
478 | |
479 | Refresh(); | |
480 | } | |
481 | ||
b63f9a33 VZ |
482 | // ---------------------------------------------------------------------------- |
483 | // wxHeaderCtrl event handlers | |
484 | // ---------------------------------------------------------------------------- | |
485 | ||
3bfaa5a7 | 486 | BEGIN_EVENT_TABLE(wxHeaderCtrl, wxHeaderCtrlBase) |
b63f9a33 VZ |
487 | EVT_PAINT(wxHeaderCtrl::OnPaint) |
488 | ||
489 | EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse) | |
aef252d9 VZ |
490 | |
491 | EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost) | |
e36dcd10 VZ |
492 | |
493 | EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown) | |
b63f9a33 VZ |
494 | END_EVENT_TABLE() |
495 | ||
496 | void wxHeaderCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
497 | { | |
498 | int w, h; | |
499 | GetClientSize(&w, &h); | |
500 | ||
501 | wxAutoBufferedPaintDC dc(this); | |
502 | ||
503 | dc.SetBackground(GetBackgroundColour()); | |
504 | dc.Clear(); | |
505 | ||
506 | // account for the horizontal scrollbar offset in the parent window | |
507 | dc.SetDeviceOrigin(m_scrollOffset, 0); | |
508 | ||
40dc2ae6 | 509 | const unsigned int count = m_numColumns; |
b63f9a33 VZ |
510 | int xpos = 0; |
511 | for ( unsigned int i = 0; i < count; i++ ) | |
512 | { | |
6327cdd5 | 513 | const unsigned idx = m_colIndices[i]; |
dcb6cbec | 514 | const wxHeaderColumn& col = GetColumn(idx); |
b63f9a33 VZ |
515 | if ( col.IsHidden() ) |
516 | continue; | |
517 | ||
518 | const int colWidth = col.GetWidth(); | |
519 | ||
520 | wxHeaderSortIconType sortArrow; | |
40dc2ae6 | 521 | if ( col.IsSortKey() ) |
b63f9a33 | 522 | { |
40dc2ae6 VZ |
523 | sortArrow = col.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP |
524 | : wxHDR_SORT_ICON_DOWN; | |
525 | } | |
526 | else // not sorting by this column | |
527 | { | |
528 | sortArrow = wxHDR_SORT_ICON_NONE; | |
b63f9a33 VZ |
529 | } |
530 | ||
531 | int state = 0; | |
532 | if ( IsEnabled() ) | |
533 | { | |
6327cdd5 | 534 | if ( idx == m_hover ) |
b63f9a33 VZ |
535 | state = wxCONTROL_CURRENT; |
536 | } | |
537 | else // disabled | |
538 | { | |
539 | state = wxCONTROL_DISABLED; | |
540 | } | |
541 | ||
542 | wxHeaderButtonParams params; | |
543 | params.m_labelText = col.GetTitle(); | |
544 | params.m_labelBitmap = col.GetBitmap(); | |
545 | params.m_labelAlignment = col.GetAlignment(); | |
546 | ||
547 | wxRendererNative::Get().DrawHeaderButton | |
548 | ( | |
549 | this, | |
550 | dc, | |
551 | wxRect(xpos, 0, colWidth, h), | |
552 | state, | |
553 | sortArrow, | |
554 | ¶ms | |
555 | ); | |
556 | ||
557 | xpos += colWidth; | |
558 | } | |
559 | } | |
560 | ||
aef252d9 VZ |
561 | void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) |
562 | { | |
6327cdd5 VZ |
563 | if ( IsDragging() ) |
564 | CancelDragging(); | |
e36dcd10 VZ |
565 | } |
566 | ||
567 | void wxHeaderCtrl::OnKeyDown(wxKeyEvent& event) | |
568 | { | |
6327cdd5 | 569 | if ( event.GetKeyCode() == WXK_ESCAPE ) |
e36dcd10 | 570 | { |
6327cdd5 VZ |
571 | if ( IsDragging() ) |
572 | { | |
573 | ReleaseMouse(); | |
574 | CancelDragging(); | |
575 | ||
576 | return; | |
577 | } | |
e36dcd10 | 578 | } |
6327cdd5 VZ |
579 | |
580 | event.Skip(); | |
aef252d9 VZ |
581 | } |
582 | ||
fa3d4aaf | 583 | void wxHeaderCtrl::OnMouse(wxMouseEvent& mevent) |
b63f9a33 | 584 | { |
aef252d9 VZ |
585 | // do this in advance to allow simply returning if we're not interested, |
586 | // we'll undo it if we do handle the event below | |
fa3d4aaf VZ |
587 | mevent.Skip(); |
588 | ||
aef252d9 | 589 | |
04a33b50 | 590 | // account for the control displacement |
aef252d9 VZ |
591 | const int xPhysical = mevent.GetX(); |
592 | const int xLogical = xPhysical - m_scrollOffset; | |
593 | ||
594 | // first deal with the [continuation of any] dragging operations in | |
595 | // progress | |
e36dcd10 | 596 | if ( IsResizing() ) |
aef252d9 VZ |
597 | { |
598 | if ( mevent.LeftUp() ) | |
a45caa71 | 599 | EndResizing(xPhysical); |
aef252d9 | 600 | else // update the live separator position |
a45caa71 | 601 | StartOrContinueResizing(m_colBeingResized, xPhysical); |
aef252d9 VZ |
602 | |
603 | return; | |
604 | } | |
605 | ||
6327cdd5 VZ |
606 | if ( IsReordering() ) |
607 | { | |
59704b49 VZ |
608 | if ( !mevent.LeftUp() ) |
609 | { | |
610 | // update the column position | |
6327cdd5 VZ |
611 | UpdateReorderingMarker(xPhysical); |
612 | ||
59704b49 VZ |
613 | return; |
614 | } | |
615 | ||
616 | // finish reordering and continue to generate a click event below if we | |
617 | // didn't really reorder anything | |
618 | if ( EndReordering(xPhysical) ) | |
619 | return; | |
6327cdd5 VZ |
620 | } |
621 | ||
04a33b50 | 622 | |
fa3d4aaf VZ |
623 | // find if the event is over a column at all |
624 | bool onSeparator; | |
6090efab VZ |
625 | const unsigned col = mevent.Leaving() |
626 | ? (onSeparator = false, COL_NONE) | |
6327cdd5 | 627 | : FindColumnAtPoint(xLogical, &onSeparator); |
aef252d9 | 628 | |
6090efab VZ |
629 | |
630 | // update the highlighted column if it changed | |
631 | if ( col != m_hover ) | |
632 | { | |
633 | const unsigned hoverOld = m_hover; | |
634 | m_hover = col; | |
635 | ||
636 | RefreshColIfNotNone(hoverOld); | |
637 | RefreshColIfNotNone(m_hover); | |
638 | } | |
639 | ||
fa3d4aaf VZ |
640 | // update mouse cursor as it moves around |
641 | if ( mevent.Moving() ) | |
642 | { | |
643 | SetCursor(onSeparator ? wxCursor(wxCURSOR_SIZEWE) : wxNullCursor); | |
644 | return; | |
645 | } | |
646 | ||
aef252d9 VZ |
647 | // all the other events only make sense when they happen over a column |
648 | if ( col == COL_NONE ) | |
649 | return; | |
650 | ||
651 | ||
652 | // enter various dragging modes on left mouse press | |
fa3d4aaf VZ |
653 | if ( mevent.LeftDown() ) |
654 | { | |
fa3d4aaf | 655 | if ( onSeparator ) |
aef252d9 VZ |
656 | { |
657 | // start resizing the column | |
6327cdd5 | 658 | wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" ); |
a45caa71 | 659 | StartOrContinueResizing(col, xPhysical); |
aef252d9 VZ |
660 | } |
661 | else // on column itself | |
662 | { | |
6327cdd5 VZ |
663 | // start dragging the column |
664 | wxASSERT_MSG( !IsReordering(), "reentering column move mode?" ); | |
665 | ||
666 | StartReordering(col, xPhysical); | |
aef252d9 | 667 | } |
fa3d4aaf VZ |
668 | |
669 | return; | |
670 | } | |
671 | ||
aef252d9 | 672 | // determine the type of header event corresponding to click events |
3bfaa5a7 VZ |
673 | wxEventType evtType = wxEVT_NULL; |
674 | const bool click = mevent.ButtonUp(), | |
675 | dblclk = mevent.ButtonDClick(); | |
676 | if ( click || dblclk ) | |
fa3d4aaf VZ |
677 | { |
678 | switch ( mevent.GetButton() ) | |
679 | { | |
680 | case wxMOUSE_BTN_LEFT: | |
3bfaa5a7 VZ |
681 | // treat left double clicks on separator specially |
682 | if ( onSeparator && dblclk ) | |
683 | { | |
684 | evtType = wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK; | |
685 | } | |
686 | else // not double click on separator | |
687 | { | |
688 | evtType = click ? wxEVT_COMMAND_HEADER_CLICK | |
689 | : wxEVT_COMMAND_HEADER_DCLICK; | |
690 | } | |
fa3d4aaf VZ |
691 | break; |
692 | ||
693 | case wxMOUSE_BTN_RIGHT: | |
694 | evtType = click ? wxEVT_COMMAND_HEADER_RIGHT_CLICK | |
695 | : wxEVT_COMMAND_HEADER_RIGHT_DCLICK; | |
696 | break; | |
697 | ||
698 | case wxMOUSE_BTN_MIDDLE: | |
699 | evtType = click ? wxEVT_COMMAND_HEADER_MIDDLE_CLICK | |
700 | : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK; | |
701 | break; | |
702 | ||
703 | default: | |
704 | // ignore clicks from other mouse buttons | |
3bfaa5a7 | 705 | ; |
fa3d4aaf | 706 | } |
3bfaa5a7 VZ |
707 | } |
708 | ||
709 | if ( evtType == wxEVT_NULL ) | |
710 | return; | |
fa3d4aaf | 711 | |
3bfaa5a7 VZ |
712 | wxHeaderCtrlEvent event(evtType, GetId()); |
713 | event.SetEventObject(this); | |
714 | event.SetColumn(col); | |
fa3d4aaf | 715 | |
3bfaa5a7 VZ |
716 | if ( GetEventHandler()->ProcessEvent(event) ) |
717 | mevent.Skip(false); | |
b63f9a33 VZ |
718 | } |
719 | ||
56873923 | 720 | #endif // wxHAS_GENERIC_HEADERCTRL |