]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/deprecated/tbarsmpl.cpp
Defaults in wxWinCE documented.
[wxWidgets.git] / contrib / src / deprecated / tbarsmpl.cpp
CommitLineData
8a0681f9 1/////////////////////////////////////////////////////////////////////////////
8a82c9ec 2// Name: contrib/src/deprecated//tbarsmpl.cpp
8a0681f9
VZ
3// Purpose: wxToolBarSimple
4// Author: Julian Smart
5// Modified by: VZ on 14.12.99 during wxToolBarSimple reorganization
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
8a0681f9
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
8a0681f9
VZ
21 #pragma implementation "tbarsmpl.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
3080bf59 31#if wxUSE_TOOLBAR && wxUSE_TOOLBAR_SIMPLE
8a0681f9
VZ
32
33#ifndef WX_PRECOMP
34 #include "wx/settings.h"
35 #include "wx/window.h"
36 #include "wx/dcclient.h"
37 #include "wx/dcmemory.h"
38#endif
39
68a14aee 40#include "wx/toolbar.h"
8a82c9ec 41#include "wx/deprecated/tbarsmpl.h"
8a0681f9
VZ
42
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
8a82c9ec 47class WXDLLIMPEXP_DEPRECATED wxToolBarToolSimple : public wxToolBarToolBase
8a0681f9
VZ
48{
49public:
50 wxToolBarToolSimple(wxToolBarSimple *tbar,
51 int id,
e76c0b5f
VZ
52 const wxString& label,
53 const wxBitmap& bmpNormal,
54 const wxBitmap& bmpDisabled,
55 wxItemKind kind,
8a0681f9 56 wxObject *clientData,
e76c0b5f
VZ
57 const wxString& shortHelp,
58 const wxString& longHelp)
59 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
60 clientData, shortHelp, longHelp)
8a0681f9
VZ
61 {
62 }
63
64 wxToolBarToolSimple(wxToolBarSimple *tbar, wxControl *control)
65 : wxToolBarToolBase(tbar, control)
66 {
67 }
68
69 void SetSize(const wxSize& size)
70 {
71 m_width = size.x;
72 m_height = size.y;
73 }
74
6f2a55e3
VZ
75 wxCoord GetWidth() const { return m_width; }
76 wxCoord GetHeight() const { return m_height; }
8a0681f9 77
e76c0b5f
VZ
78 wxCoord m_x;
79 wxCoord m_y;
80 wxCoord m_width;
81 wxCoord m_height;
2eb10e2a
VZ
82
83 DECLARE_NO_COPY_CLASS(wxToolBarToolSimple)
8a0681f9
VZ
84};
85
86// ----------------------------------------------------------------------------
87// wxWin macros
88// ----------------------------------------------------------------------------
89
2eb10e2a 90IMPLEMENT_DYNAMIC_CLASS(wxToolBarSimple, wxControl)
8a0681f9 91
08af0d25 92#if !wxUSE_TOOLBAR_NATIVE && !defined(__WXUNIVERSAL__)
7aa58644
VZ
93 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarSimple)
94#endif
95
8a0681f9 96BEGIN_EVENT_TABLE(wxToolBarSimple, wxToolBarBase)
479cd5de 97 EVT_SIZE(wxToolBarSimple::OnSize)
8a0681f9 98 EVT_SCROLL(wxToolBarSimple::OnScroll)
479cd5de
VZ
99 EVT_PAINT(wxToolBarSimple::OnPaint)
100 EVT_KILL_FOCUS(wxToolBarSimple::OnKillFocus)
101 EVT_MOUSE_EVENTS(wxToolBarSimple::OnMouseEvent)
8a0681f9
VZ
102END_EVENT_TABLE()
103
104// ============================================================================
105// implementation
106// ============================================================================
107
108// ----------------------------------------------------------------------------
109// tool bar tools creation
110// ----------------------------------------------------------------------------
111
112wxToolBarToolBase *wxToolBarSimple::CreateTool(int id,
e76c0b5f
VZ
113 const wxString& label,
114 const wxBitmap& bmpNormal,
115 const wxBitmap& bmpDisabled,
116 wxItemKind kind,
8a0681f9 117 wxObject *clientData,
e76c0b5f
VZ
118 const wxString& shortHelp,
119 const wxString& longHelp)
8a0681f9 120{
e76c0b5f
VZ
121 return new wxToolBarToolSimple(this, id, label, bmpNormal, bmpDisabled,
122 kind, clientData, shortHelp, longHelp);
8a0681f9
VZ
123}
124
125wxToolBarToolBase *wxToolBarSimple::CreateTool(wxControl *control)
126{
127 return new wxToolBarToolSimple(this, control);
128}
129
130// ----------------------------------------------------------------------------
131// wxToolBarSimple creation
132// ----------------------------------------------------------------------------
133
134void wxToolBarSimple::Init()
135{
136 m_currentRowsOrColumns = 0;
137
138 m_lastX =
139 m_lastY = 0;
140
141 m_maxWidth =
142 m_maxHeight = 0;
143
144 m_pressedTool =
145 m_currentTool = -1;
146
147 m_xPos =
318c5e9f 148 m_yPos = wxDefaultCoord;
8a0681f9
VZ
149
150 m_toolPacking = 1;
151 m_toolSeparation = 5;
152
153 m_defaultWidth = 16;
154 m_defaultHeight = 15;
f42c2a7a
JS
155
156 m_xScrollPixelsPerLine = 1;
157 m_yScrollPixelsPerLine = 1;
c7f8ebf8
WS
158 m_xScrollingEnabled = false;
159 m_yScrollingEnabled = false;
f42c2a7a
JS
160 m_xScrollPosition = 0;
161 m_yScrollPosition = 0;
162 m_xScrollLines = 0;
163 m_yScrollLines = 0;
164 m_xScrollLinesPerPage = 0;
165 m_yScrollLinesPerPage = 0;
8a0681f9
VZ
166}
167
e76c0b5f
VZ
168wxToolBarToolBase *wxToolBarSimple::DoAddTool(int id,
169 const wxString& label,
170 const wxBitmap& bitmap,
171 const wxBitmap& bmpDisabled,
172 wxItemKind kind,
173 const wxString& shortHelp,
174 const wxString& longHelp,
175 wxObject *clientData,
176 wxCoord xPos,
177 wxCoord yPos)
8a0681f9
VZ
178{
179 // rememeber the position for DoInsertTool()
180 m_xPos = xPos;
181 m_yPos = yPos;
182
e76c0b5f
VZ
183 return wxToolBarBase::DoAddTool(id, label, bitmap, bmpDisabled, kind,
184 shortHelp, longHelp,
185 clientData, xPos, yPos);
8a0681f9
VZ
186}
187
406005d2
VZ
188bool wxToolBarSimple::DoInsertTool(size_t WXUNUSED(pos),
189 wxToolBarToolBase *toolBase)
8a0681f9
VZ
190{
191 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)toolBase;
192
c7f8ebf8 193 wxCHECK_MSG( !tool->IsControl(), false,
8a0681f9
VZ
194 _T("generic wxToolBarSimple doesn't support controls") );
195
196 tool->m_x = m_xPos;
318c5e9f 197 if ( tool->m_x == wxDefaultCoord )
8a0681f9
VZ
198 tool->m_x = m_xMargin;
199
200 tool->m_y = m_yPos;
318c5e9f 201 if ( tool->m_y == wxDefaultCoord )
8a0681f9
VZ
202 tool->m_y = m_yMargin;
203
204 tool->SetSize(GetToolSize());
205
206 if ( tool->IsButton() )
207 {
208 // Calculate reasonable max size in case Layout() not called
bfe9b3da 209 if ((tool->m_x + tool->GetNormalBitmap().GetWidth() + m_xMargin) > m_maxWidth)
479cd5de 210 m_maxWidth = (wxCoord)((tool->m_x + tool->GetWidth() + m_xMargin));
8a0681f9 211
bfe9b3da 212 if ((tool->m_y + tool->GetNormalBitmap().GetHeight() + m_yMargin) > m_maxHeight)
479cd5de 213 m_maxHeight = (wxCoord)((tool->m_y + tool->GetHeight() + m_yMargin));
8a0681f9
VZ
214 }
215
c7f8ebf8 216 return true;
8a0681f9
VZ
217}
218
219bool wxToolBarSimple::DoDeleteTool(size_t WXUNUSED(pos),
220 wxToolBarToolBase *tool)
221{
222 // VZ: didn't test whether it works, but why not...
223 tool->Detach();
224
225 Refresh();
226
c7f8ebf8 227 return true;
8a0681f9
VZ
228}
229
230bool wxToolBarSimple::Create(wxWindow *parent,
231 wxWindowID id,
232 const wxPoint& pos,
233 const wxSize& size,
234 long style,
235 const wxString& name)
236{
237 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
c7f8ebf8 238 return false;
8a0681f9
VZ
239
240 // Set it to grey (or other 3D face colour)
7516ed26 241 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
8a0681f9
VZ
242
243 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
244 {
245 m_lastX = 7;
246 m_lastY = 3;
247
248 m_maxRows = 32000; // a lot
249 m_maxCols = 1;
250 }
251 else
252 {
253 m_lastX = 3;
254 m_lastY = 7;
255
256 m_maxRows = 1;
257 m_maxCols = 32000; // a lot
258 }
259
260 SetCursor(*wxSTANDARD_CURSOR);
261
c7f8ebf8 262 return true;
8a0681f9
VZ
263}
264
265wxToolBarSimple::~wxToolBarSimple()
266{
267}
268
269bool wxToolBarSimple::Realize()
270{
271 m_currentRowsOrColumns = 0;
272 m_lastX = m_xMargin;
273 m_lastY = m_yMargin;
274 m_maxWidth = 0;
275 m_maxHeight = 0;
276
277 int maxToolWidth = 0;
278 int maxToolHeight = 0;
279
280 // Find the maximum tool width and height
222ed1d6 281 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
8a0681f9
VZ
282 while ( node )
283 {
284 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)node->GetData();
285 if ( tool->GetWidth() > maxToolWidth )
286 maxToolWidth = tool->GetWidth();
287 if (tool->GetHeight() > maxToolHeight)
288 maxToolHeight = tool->GetHeight();
289
290 node = node->GetNext();
291 }
292
293 int separatorSize = m_toolSeparation;
294
295 node = m_tools.GetFirst();
296 while ( node )
297 {
298 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)node->GetData();
299 if ( tool->IsSeparator() )
300 {
301 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
302 {
303 if (m_currentRowsOrColumns >= m_maxCols)
304 m_lastY += separatorSize;
305 else
306 m_lastX += separatorSize;
307 }
308 else
309 {
310 if (m_currentRowsOrColumns >= m_maxRows)
311 m_lastX += separatorSize;
312 else
313 m_lastY += separatorSize;
314 }
315 }
316 else if ( tool->IsButton() )
317 {
318 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
319 {
320 if (m_currentRowsOrColumns >= m_maxCols)
321 {
322 m_currentRowsOrColumns = 0;
323 m_lastX = m_xMargin;
324 m_lastY += maxToolHeight + m_toolPacking;
325 }
674ac8b9
VZ
326 tool->m_x = (wxCoord)(m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
327 tool->m_y = (wxCoord)(m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
8a0681f9
VZ
328
329 m_lastX += maxToolWidth + m_toolPacking;
330 }
331 else
332 {
333 if (m_currentRowsOrColumns >= m_maxRows)
334 {
335 m_currentRowsOrColumns = 0;
336 m_lastX += (maxToolWidth + m_toolPacking);
337 m_lastY = m_yMargin;
338 }
674ac8b9
VZ
339 tool->m_x = (wxCoord)(m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
340 tool->m_y = (wxCoord)(m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
8a0681f9
VZ
341
342 m_lastY += maxToolHeight + m_toolPacking;
343 }
344 m_currentRowsOrColumns ++;
345 }
346 else
347 {
348 // TODO: support the controls
349 }
350
351 if (m_lastX > m_maxWidth)
352 m_maxWidth = m_lastX;
353 if (m_lastY > m_maxHeight)
354 m_maxHeight = m_lastY;
355
356 node = node->GetNext();
357 }
358
359 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
8a0681f9 360 m_maxHeight += maxToolHeight;
2b5f62a0
VZ
361 else
362 m_maxWidth += maxToolWidth;
8a0681f9
VZ
363
364 m_maxWidth += m_xMargin;
365 m_maxHeight += m_yMargin;
366
2b5f62a0
VZ
367 SetSize(m_maxWidth, m_maxHeight);
368
c7f8ebf8 369 return true;
8a0681f9
VZ
370}
371
372// ----------------------------------------------------------------------------
373// event handlers
374// ----------------------------------------------------------------------------
375
376void wxToolBarSimple::OnPaint (wxPaintEvent& WXUNUSED(event))
377{
378 wxPaintDC dc(this);
379 PrepareDC(dc);
380
381 static int count = 0;
382 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
383 if ( count > 0 )
384 return;
385 count++;
386
222ed1d6 387 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
8a0681f9
VZ
388 node;
389 node = node->GetNext() )
390 {
391 wxToolBarToolBase *tool = node->GetData();
392 if ( tool->IsButton() )
393 DrawTool(dc, tool);
394 }
395
396 count--;
397}
398
399void wxToolBarSimple::OnSize (wxSizeEvent& WXUNUSED(event))
400{
8a0681f9
VZ
401 if (GetAutoLayout())
402 Layout();
8a0681f9
VZ
403
404 AdjustScrollbars();
405}
406
407void wxToolBarSimple::OnKillFocus(wxFocusEvent& WXUNUSED(event))
408{
409 OnMouseEnter(m_pressedTool = m_currentTool = -1);
410}
411
412void wxToolBarSimple::OnMouseEvent(wxMouseEvent & event)
413{
414 wxCoord x, y;
415 event.GetPosition(&x, &y);
416 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)FindToolForPosition(x, y);
417
418 if (event.LeftDown())
419 {
420 CaptureMouse();
421 }
422 if (event.LeftUp())
423 {
424 ReleaseMouse();
425 }
426
427 if (!tool)
428 {
429 if (m_currentTool > -1)
430 {
431 if (event.LeftIsDown())
432 SpringUpButton(m_currentTool);
433 m_currentTool = -1;
434 OnMouseEnter(-1);
435 }
436 return;
437 }
438
439 if (!event.IsButton())
440 {
441 if ( tool->GetId() != m_currentTool )
442 {
443 // If the left button is kept down and moved over buttons,
444 // press those buttons.
445 if ( event.LeftIsDown() && tool->IsEnabled() )
446 {
447 SpringUpButton(m_currentTool);
448
449 if ( tool->CanBeToggled() )
450 {
451 tool->Toggle();
452 }
453
454 DrawTool(tool);
455 }
456
457 m_currentTool = tool->GetId();
458 OnMouseEnter(m_currentTool);
459 }
460 return;
461 }
462
463 // Left button pressed.
464 if ( event.LeftDown() && tool->IsEnabled() )
465 {
466 if ( tool->CanBeToggled() )
467 {
468 tool->Toggle();
469 }
470
471 DrawTool(tool);
472 }
473 else if (event.RightDown())
474 {
475 OnRightClick(tool->GetId(), x, y);
476 }
477
478 // Left Button Released. Only this action confirms selection.
479 // If the button is enabled and it is not a toggle tool and it is
480 // in the pressed state, then raise the button and call OnLeftClick.
481 //
482 if ( event.LeftUp() && tool->IsEnabled() )
483 {
484 // Pass the OnLeftClick event to tool
485 if ( !OnLeftClick(tool->GetId(), tool->IsToggled()) &&
486 tool->CanBeToggled() )
487 {
488 // If it was a toggle, and OnLeftClick says No Toggle allowed,
489 // then change it back
490 tool->Toggle();
491 }
492
493 DrawTool(tool);
494 }
495}
496
497// ----------------------------------------------------------------------------
498// drawing
499// ----------------------------------------------------------------------------
500
501void wxToolBarSimple::DrawTool(wxToolBarToolBase *tool)
502{
503 wxClientDC dc(this);
504 DrawTool(dc, tool);
505}
506
507void wxToolBarSimple::DrawTool(wxDC& dc, wxToolBarToolBase *toolBase)
508{
509 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)toolBase;
510
511 wxMemoryDC memDC;
512 PrepareDC(dc);
513
514 wxPen dark_grey_pen(wxColour( 85,85,85 ), 1, wxSOLID);
9e0a12c9
MB
515 wxPen white_pen(wxT("WHITE"), 1, wxSOLID);
516 wxPen black_pen(wxT("BLACK"), 1, wxSOLID);
8a0681f9 517
a977709b
JS
518 wxBitmap bitmap = tool->GetNormalBitmap();
519 if (!bitmap.Ok())
520 return;
8a0681f9 521
a977709b 522 if ( !tool->IsToggled() )
8a0681f9 523 {
80806624 524#if wxUSE_PALETTE
8a0681f9
VZ
525#ifndef __WXGTK__
526 if (bitmap.GetPalette())
527 memDC.SetPalette(*bitmap.GetPalette());
528#endif
80806624 529#endif // wxUSE_PALETTE
8a0681f9
VZ
530
531 int ax = (int)tool->m_x,
532 ay = (int)tool->m_y,
533 bx = (int)(tool->m_x+tool->GetWidth()),
534 by = (int)(tool->m_y+tool->GetHeight());
535
536 memDC.SelectObject(bitmap);
537 if (m_windowStyle & wxTB_3DBUTTONS)
538 {
539 dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
540 dc.Blit((ax+1), (ay+1), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
541 wxPen * old_pen = & dc.GetPen();
542 dc.SetPen( white_pen );
543 dc.DrawLine(ax,(by-1),ax,ay);
544 dc.DrawLine(ax,ay,(bx-1),ay);
545 dc.SetPen( dark_grey_pen );
546 dc.DrawLine((bx-1),(ay+1),(bx-1),(by-1));
547 dc.DrawLine((bx-1),(by-1),(ax+1),(by-1));
548 dc.SetPen( black_pen );
549 dc.DrawLine(bx,ay,bx,by);
550 dc.DrawLine(bx,by,ax,by);
551 dc.SetPen( *old_pen );
552 dc.DestroyClippingRegion();
553 // Select bitmap out of the DC
554 }
555 else
556 {
557 dc.Blit(tool->m_x, tool->m_y,
558 bitmap.GetWidth(), bitmap.GetHeight(),
559 &memDC, 0, 0);
560 }
561 memDC.SelectObject(wxNullBitmap);
80806624
VZ
562
563#if wxUSE_PALETTE
8a0681f9 564#ifndef __WXGTK__
a6b27b78
VS
565 if (bitmap.GetPalette())
566 memDC.SetPalette(wxNullPalette);
8a0681f9 567#endif
80806624 568#endif // wxUSE_PALETTE
8a0681f9
VZ
569 }
570 // No second bitmap, so draw a thick line around bitmap, or invert if mono
571 else if ( tool->IsToggled() )
572 {
c7f8ebf8 573 bool drawBorder = false;
8a0681f9
VZ
574#ifdef __X__ // X doesn't invert properly on colour
575 drawBorder = wxColourDisplay();
576#else // Inversion works fine under Windows
c7f8ebf8 577 drawBorder = false;
8a0681f9
VZ
578#endif
579
580 if (!drawBorder)
581 {
bfe9b3da 582 memDC.SelectObject(tool->GetNormalBitmap());
8a0681f9
VZ
583 dc.Blit(tool->m_x, tool->m_y, tool->GetWidth(), tool->GetHeight(),
584 &memDC, 0, 0, wxSRC_INVERT);
585 memDC.SelectObject(wxNullBitmap);
586 }
587 else
588 {
bfe9b3da 589 bitmap = tool->GetNormalBitmap();
8a0681f9
VZ
590
591 if (m_windowStyle & wxTB_3DBUTTONS)
592 {
593 int ax = (int)tool->m_x,
594 ay = (int)tool->m_y,
595 bx = (int)(tool->m_x+tool->GetWidth()),
596 by = (int)(tool->m_y+tool->GetHeight());
597
598 memDC.SelectObject(bitmap);
599 dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
600 dc.Blit((ax+2), (ay+2), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
601 wxPen * old_pen = & dc.GetPen();
602 dc.SetPen( black_pen );
603 dc.DrawLine(ax,(by-1),ax,ay);
604 dc.DrawLine(ax,ay,(bx-1),ay);
605 dc.SetPen( dark_grey_pen );
606 dc.DrawLine((ax+1),(by-2),(ax+1),(ay+1));
607 dc.DrawLine((ax+1),(ay+1),(bx-2),(ay+1));
608 dc.SetPen( white_pen );
609 dc.DrawLine(bx,ay,bx,by);
610 dc.DrawLine(bx,by,ax,by);
611 dc.SetPen( *old_pen );
612 dc.DestroyClippingRegion();
613 memDC.SelectObject(wxNullBitmap);
614 }
615 else
616 {
6f2a55e3
VZ
617 wxCoord x = tool->m_x;
618 wxCoord y = tool->m_y;
619 wxCoord w = bitmap.GetWidth();
620 wxCoord h = bitmap.GetHeight();
9e0a12c9 621 wxPen thick_black_pen(wxT("BLACK"), 3, wxSOLID);
8a0681f9
VZ
622
623 memDC.SelectObject(bitmap);
624 dc.SetClippingRegion(tool->m_x, tool->m_y, w, h);
625 dc.Blit(tool->m_x, tool->m_y, w, h,
626 &memDC, 0, 0);
627 dc.SetPen(thick_black_pen);
628 dc.SetBrush(*wxTRANSPARENT_BRUSH);
629 dc.DrawRectangle(x, y, w-1, h-1);
630 dc.DestroyClippingRegion();
631 memDC.SelectObject(wxNullBitmap);
632 }
633 }
634 }
635}
636
637// ----------------------------------------------------------------------------
638// toolbar geometry
639// ----------------------------------------------------------------------------
640
641void wxToolBarSimple::SetRows(int nRows)
642{
643 wxCHECK_RET( nRows != 0, _T("max number of rows must be > 0") );
644
645 m_maxCols = (GetToolsCount() + nRows - 1) / nRows;
646
647 AdjustScrollbars();
648 Refresh();
649}
650
651wxToolBarToolBase *wxToolBarSimple::FindToolForPosition(wxCoord x,
652 wxCoord y) const
653{
222ed1d6 654 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
8a0681f9
VZ
655 while (node)
656 {
657 wxToolBarToolSimple *tool = (wxToolBarToolSimple *)node->GetData();
658 if ((x >= tool->m_x) && (y >= tool->m_y) &&
659 (x <= (tool->m_x + tool->GetWidth())) &&
660 (y <= (tool->m_y + tool->GetHeight())))
661 {
662 return tool;
663 }
664
665 node = node->GetNext();
666 }
667
668 return (wxToolBarToolBase *)NULL;
669}
670
671// ----------------------------------------------------------------------------
672// tool state change handlers
673// ----------------------------------------------------------------------------
674
675void wxToolBarSimple::DoEnableTool(wxToolBarToolBase *tool,
676 bool WXUNUSED(enable))
677{
678 DrawTool(tool);
679}
680
681void wxToolBarSimple::DoToggleTool(wxToolBarToolBase *tool,
682 bool WXUNUSED(toggle))
683{
684 DrawTool(tool);
685}
686
687void wxToolBarSimple::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
688 bool WXUNUSED(toggle))
689{
690 // nothing to do
691}
692
693// Okay, so we've left the tool we're in ... we must check if the tool we're
694// leaving was a 'sprung push button' and if so, spring it back to the up
695// state.
696void wxToolBarSimple::SpringUpButton(int id)
697{
698 wxToolBarToolBase *tool = FindById(id);
699
700 if ( tool && tool->CanBeToggled() )
701 {
e1437298
JS
702 if (tool->IsToggled())
703 tool->Toggle();
8a0681f9
VZ
704
705 DrawTool(tool);
706 }
707}
708
709// ----------------------------------------------------------------------------
710// scrolling implementation
711// ----------------------------------------------------------------------------
712
713/*
714 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
715 * noUnitsX/noUnitsY: : no. units per scrollbar
716 */
717void wxToolBarSimple::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
718 int noUnitsX, int noUnitsY,
719 int xPos, int yPos)
720{
721 m_xScrollPixelsPerLine = pixelsPerUnitX;
722 m_yScrollPixelsPerLine = pixelsPerUnitY;
723 m_xScrollLines = noUnitsX;
724 m_yScrollLines = noUnitsY;
725
726 int w, h;
727 GetSize(&w, &h);
728
729 // Recalculate scroll bar range and position
730 if (m_xScrollLines > 0)
731 {
732 m_xScrollPosition = xPos;
c7f8ebf8 733 SetScrollPos (wxHORIZONTAL, m_xScrollPosition, true);
8a0681f9
VZ
734 }
735 else
736 {
c7f8ebf8 737 SetScrollbar(wxHORIZONTAL, 0, 0, 0, false);
8a0681f9
VZ
738 m_xScrollPosition = 0;
739 }
740
741 if (m_yScrollLines > 0)
742 {
743 m_yScrollPosition = yPos;
c7f8ebf8 744 SetScrollPos (wxVERTICAL, m_yScrollPosition, true);
8a0681f9
VZ
745 }
746 else
747 {
c7f8ebf8 748 SetScrollbar(wxVERTICAL, 0, 0, 0, false);
8a0681f9
VZ
749 m_yScrollPosition = 0;
750 }
751 AdjustScrollbars();
752 Refresh();
753
754#if 0 //def __WXMSW__
755 ::UpdateWindow ((HWND) GetHWND());
756#endif
757}
758
759void wxToolBarSimple::OnScroll(wxScrollEvent& event)
760{
761 int orient = event.GetOrientation();
762
763 int nScrollInc = CalcScrollInc(event);
764 if (nScrollInc == 0)
765 return;
766
767 if (orient == wxHORIZONTAL)
768 {
769 int newPos = m_xScrollPosition + nScrollInc;
c7f8ebf8 770 SetScrollPos(wxHORIZONTAL, newPos, true );
8a0681f9
VZ
771 }
772 else
773 {
774 int newPos = m_yScrollPosition + nScrollInc;
c7f8ebf8 775 SetScrollPos(wxVERTICAL, newPos, true );
8a0681f9
VZ
776 }
777
778 if (orient == wxHORIZONTAL)
779 {
780 if (m_xScrollingEnabled)
781 ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, NULL);
782 else
783 Refresh();
784 }
785 else
786 {
787 if (m_yScrollingEnabled)
788 ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, NULL);
789 else
790 Refresh();
791 }
792
793 if (orient == wxHORIZONTAL)
794 {
795 m_xScrollPosition += nScrollInc;
796 }
797 else
798 {
799 m_yScrollPosition += nScrollInc;
800 }
801
802}
803
804int wxToolBarSimple::CalcScrollInc(wxScrollEvent& event)
805{
806 int pos = event.GetPosition();
807 int orient = event.GetOrientation();
808
809 int nScrollInc = 0;
0f442030 810 if (event.GetEventType() == wxEVT_SCROLL_TOP)
8a0681f9 811 {
0f442030
RR
812 if (orient == wxHORIZONTAL)
813 nScrollInc = - m_xScrollPosition;
814 else
815 nScrollInc = - m_yScrollPosition;
816 } else
817 if (event.GetEventType() == wxEVT_SCROLL_BOTTOM)
818 {
819 if (orient == wxHORIZONTAL)
820 nScrollInc = m_xScrollLines - m_xScrollPosition;
821 else
822 nScrollInc = m_yScrollLines - m_yScrollPosition;
823 } else
824 if (event.GetEventType() == wxEVT_SCROLL_LINEUP)
825 {
826 nScrollInc = -1;
827 } else
828 if (event.GetEventType() == wxEVT_SCROLL_LINEDOWN)
829 {
830 nScrollInc = 1;
831 } else
832 if (event.GetEventType() == wxEVT_SCROLL_PAGEUP)
833 {
834 if (orient == wxHORIZONTAL)
835 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
836 else
837 nScrollInc = -GetScrollPageSize(wxVERTICAL);
838 } else
839 if (event.GetEventType() == wxEVT_SCROLL_PAGEDOWN)
840 {
841 if (orient == wxHORIZONTAL)
842 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
843 else
844 nScrollInc = GetScrollPageSize(wxVERTICAL);
845 } else
846 if ((event.GetEventType() == wxEVT_SCROLL_THUMBTRACK) ||
847 (event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE))
848 {
849 if (orient == wxHORIZONTAL)
850 nScrollInc = pos - m_xScrollPosition;
851 else
852 nScrollInc = pos - m_yScrollPosition;
8a0681f9 853 }
318c5e9f 854
8a0681f9
VZ
855 if (orient == wxHORIZONTAL)
856 {
857 int w, h;
858 GetClientSize(&w, &h);
859
860 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
861 int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 );
862 if (noPositions < 0)
863 noPositions = 0;
864
865 if ( (m_xScrollPosition + nScrollInc) < 0 )
866 nScrollInc = -m_xScrollPosition; // As -ve as we can go
867 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
868 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
869
870 return nScrollInc;
871 }
872 else
873 {
874 int w, h;
875 GetClientSize(&w, &h);
876
877 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
878 int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 );
879 if (noPositions < 0)
880 noPositions = 0;
881
882 if ( (m_yScrollPosition + nScrollInc) < 0 )
883 nScrollInc = -m_yScrollPosition; // As -ve as we can go
884 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
885 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
886
887 return nScrollInc;
888 }
889}
890
891// Adjust the scrollbars - new version.
892void wxToolBarSimple::AdjustScrollbars()
893{
894 int w, h;
895 GetClientSize(&w, &h);
896
897 // Recalculate scroll bar range and position
898 if (m_xScrollLines > 0)
899 {
900 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
901 int newRange = (int) ( ((nMaxWidth)/(float)m_xScrollPixelsPerLine) + 0.5 );
902 if (newRange < 0)
903 newRange = 0;
904
905 m_xScrollPosition = wxMin(newRange, m_xScrollPosition);
906
907 // Calculate page size i.e. number of scroll units you get on the
908 // current client window
909 int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
910 if (noPagePositions < 1)
911 noPagePositions = 1;
912
913 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, newRange);
914 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
915 }
916 if (m_yScrollLines > 0)
917 {
918 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
919 int newRange = (int) ( ((nMaxHeight)/(float)m_yScrollPixelsPerLine) + 0.5 );
920 if (newRange < 0)
921 newRange = 0;
922
923 m_yScrollPosition = wxMin(newRange, m_yScrollPosition);
924
925 // Calculate page size i.e. number of scroll units you get on the
926 // current client window
927 int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
928 if (noPagePositions < 1)
929 noPagePositions = 1;
930
931 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, newRange);
932 SetScrollPageSize(wxVERTICAL, noPagePositions);
933 }
934}
935
936// Prepare the DC by translating it according to the current scroll position
937void wxToolBarSimple::PrepareDC(wxDC& dc)
938{
939 dc.SetDeviceOrigin(- m_xScrollPosition * m_xScrollPixelsPerLine, - m_yScrollPosition * m_yScrollPixelsPerLine);
940}
941
942void wxToolBarSimple::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
943{
944 *x_unit = m_xScrollPixelsPerLine;
945 *y_unit = m_yScrollPixelsPerLine;
946}
947
948int wxToolBarSimple::GetScrollPageSize(int orient) const
949{
950 if ( orient == wxHORIZONTAL )
951 return m_xScrollLinesPerPage;
952 else
953 return m_yScrollLinesPerPage;
954}
955
956void wxToolBarSimple::SetScrollPageSize(int orient, int pageSize)
957{
958 if ( orient == wxHORIZONTAL )
959 m_xScrollLinesPerPage = pageSize;
960 else
961 m_yScrollLinesPerPage = pageSize;
962}
963
964/*
965 * Scroll to given position (scroll position, not pixel position)
966 */
967void wxToolBarSimple::Scroll (int x_pos, int y_pos)
968{
969 int old_x, old_y;
970 ViewStart (&old_x, &old_y);
318c5e9f 971 if (((x_pos == wxDefaultCoord) || (x_pos == old_x)) && ((y_pos == wxDefaultCoord) || (y_pos == old_y)))
8a0681f9
VZ
972 return;
973
318c5e9f 974 if (x_pos != wxDefaultCoord)
8a0681f9
VZ
975 {
976 m_xScrollPosition = x_pos;
c7f8ebf8 977 SetScrollPos (wxHORIZONTAL, x_pos, true);
8a0681f9 978 }
318c5e9f 979 if (y_pos != wxDefaultCoord)
8a0681f9
VZ
980 {
981 m_yScrollPosition = y_pos;
c7f8ebf8 982 SetScrollPos (wxVERTICAL, y_pos, true);
8a0681f9
VZ
983 }
984 Refresh();
985
986#if 0 //def __WXMSW__
987 UpdateWindow ((HWND) GetHWND());
988#endif
989}
990
991void wxToolBarSimple::EnableScrolling (bool x_scroll, bool y_scroll)
992{
993 m_xScrollingEnabled = x_scroll;
994 m_yScrollingEnabled = y_scroll;
995}
996
997void wxToolBarSimple::GetVirtualSize (int *x, int *y) const
998{
999 *x = m_xScrollPixelsPerLine * m_xScrollLines;
1000 *y = m_yScrollPixelsPerLine * m_yScrollLines;
1001}
1002
1003// Where the current view starts from
1004void wxToolBarSimple::ViewStart (int *x, int *y) const
1005{
1006 *x = m_xScrollPosition;
1007 *y = m_yScrollPosition;
1008}
1009
1010#endif // wxUSE_TOOLBAR_SIMPLE
8a82c9ec 1011