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