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