]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/toolbkg.cpp
use wxWANTS_CHARS to allow arrows to work inside the control
[wxWidgets.git] / src / generic / toolbkg.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/toolbkg.cpp
3// Purpose: generic implementation of wxToolbook
4// Author: Julian Smart
5// Modified by:
6// Created: 2006-01-29
7// RCS-ID: $Id$
8// Copyright: (c) 2006 Julian Smart
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#if wxUSE_TOOLBOOK
20
21#ifndef WX_PRECOMP
22 #include "wx/icon.h"
23 #include "wx/settings.h"
24 #include "wx/toolbar.h"
25#endif
26
27#include "wx/imaglist.h"
28#include "wx/sysopt.h"
29#include "wx/toolbook.h"
30
31#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
32#include "wx/generic/buttonbar.h"
33#endif
34
35// ----------------------------------------------------------------------------
36// various wxWidgets macros
37// ----------------------------------------------------------------------------
38
39// check that the page index is valid
40#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
41
42// ----------------------------------------------------------------------------
43// event table
44// ----------------------------------------------------------------------------
45
46IMPLEMENT_DYNAMIC_CLASS(wxToolbook, wxBookCtrlBase)
47IMPLEMENT_DYNAMIC_CLASS(wxToolbookEvent, wxNotifyEvent)
48
49const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING = wxNewEventType();
50const wxEventType wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED = wxNewEventType();
51
52BEGIN_EVENT_TABLE(wxToolbook, wxBookCtrlBase)
53 EVT_SIZE(wxToolbook::OnSize)
54 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected)
55 EVT_IDLE(wxToolbook::OnIdle)
56END_EVENT_TABLE()
57
58// ============================================================================
59// wxToolbook implementation
60// ============================================================================
61
62// ----------------------------------------------------------------------------
63// wxToolbook creation
64// ----------------------------------------------------------------------------
65
66void wxToolbook::Init()
67{
68 m_selection = wxNOT_FOUND;
69 m_needsRealizing = false;
70}
71
72bool wxToolbook::Create(wxWindow *parent,
73 wxWindowID id,
74 const wxPoint& pos,
75 const wxSize& size,
76 long style,
77 const wxString& name)
78{
79 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
80 style |= wxBK_TOP;
81
82 // no border for this control
83 style &= ~wxBORDER_MASK;
84 style |= wxBORDER_NONE;
85
86 if ( !wxControl::Create(parent, id, pos, size, style,
87 wxDefaultValidator, name) )
88 return false;
89
90 int tbFlags = wxTB_TEXT | wxTB_FLAT | wxBORDER_NONE;
91 if ( (style & (wxBK_LEFT | wxBK_RIGHT)) != 0 )
92 tbFlags |= wxTB_VERTICAL;
93 else
94 tbFlags |= wxTB_HORIZONTAL;
95
96 if ( style & wxTBK_HORZ_LAYOUT )
97 tbFlags |= wxTB_HORZ_LAYOUT;
98
99 // TODO: make more configurable
100
101#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
102 if (style & wxTBK_BUTTONBAR)
103 {
104 m_bookctrl = new wxButtonToolBar
105 (
106 this,
107 wxID_ANY,
108 wxDefaultPosition,
109 wxDefaultSize,
110 tbFlags
111 );
112 }
113 else
114#endif
115 {
116 m_bookctrl = new wxToolBar
117 (
118 this,
119 wxID_ANY,
120 wxDefaultPosition,
121 wxDefaultSize,
122 tbFlags | wxTB_NODIVIDER
123 );
124 }
125
126 return true;
127}
128
129// ----------------------------------------------------------------------------
130// wxToolbook geometry management
131// ----------------------------------------------------------------------------
132
133wxSize wxToolbook::GetControllerSize() const
134{
135 const wxSize sizeClient = GetClientSize(),
136 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
137 sizeToolBar = GetToolBar()->GetSize() + sizeBorder;
138
139 wxSize size;
140
141 if ( IsVertical() )
142 {
143 size.x = sizeClient.x;
144 size.y = sizeToolBar.y;
145 }
146 else // left/right aligned
147 {
148 size.x = sizeToolBar.x;
149 size.y = sizeClient.y;
150 }
151
152 return size;
153}
154
155void wxToolbook::OnSize(wxSizeEvent& event)
156{
157 if (m_needsRealizing)
158 Realize();
159
160 wxBookCtrlBase::OnSize(event);
161}
162
163wxSize wxToolbook::CalcSizeFromPage(const wxSize& sizePage) const
164{
165 // we need to add the size of the list control and the border between
166 const wxSize sizeToolBar = GetControllerSize();
167
168 wxSize size = sizePage;
169 if ( IsVertical() )
170 {
171 size.y += sizeToolBar.y + GetInternalBorder();
172 }
173 else // left/right aligned
174 {
175 size.x += sizeToolBar.x + GetInternalBorder();
176 }
177
178 return size;
179}
180
181// ----------------------------------------------------------------------------
182// accessing the pages
183// ----------------------------------------------------------------------------
184
185bool wxToolbook::SetPageText(size_t n, const wxString& strText)
186{
187 // Assume tool ids start from 1
188 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
189 if (tool)
190 {
191 tool->SetLabel(strText);
192 return true;
193 }
194 else
195 return false;
196}
197
198wxString wxToolbook::GetPageText(size_t n) const
199{
200 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
201 if (tool)
202 return tool->GetLabel();
203 else
204 return wxEmptyString;
205}
206
207int wxToolbook::GetPageImage(size_t WXUNUSED(n)) const
208{
209 wxFAIL_MSG( _T("wxToolbook::GetPageImage() not implemented") );
210
211 return wxNOT_FOUND;
212}
213
214bool wxToolbook::SetPageImage(size_t n, int imageId)
215{
216 wxASSERT( GetImageList() != NULL );
217 if (!GetImageList())
218 return false;
219
220 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
221 if (tool)
222 {
223 // Find the image list index for this tool
224 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
225 tool->SetNormalBitmap(bitmap);
226 return true;
227 }
228 else
229 return false;
230}
231
232// ----------------------------------------------------------------------------
233// image list stuff
234// ----------------------------------------------------------------------------
235
236void wxToolbook::SetImageList(wxImageList *imageList)
237{
238 wxBookCtrlBase::SetImageList(imageList);
239}
240
241// ----------------------------------------------------------------------------
242// selection
243// ----------------------------------------------------------------------------
244
245int wxToolbook::GetSelection() const
246{
247 return m_selection;
248}
249
250wxBookCtrlBaseEvent* wxToolbook::CreatePageChangingEvent() const
251{
252 return new wxToolbookEvent(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGING, m_windowId);
253}
254
255void wxToolbook::MakeChangedEvent(wxBookCtrlBaseEvent &event)
256{
257 event.SetEventType(wxEVT_COMMAND_TOOLBOOK_PAGE_CHANGED);
258}
259
260void wxToolbook::UpdateSelectedPage(size_t newsel)
261{
262 m_selection = newsel;
263 GetToolBar()->ToggleTool(newsel + 1, true);
264}
265
266// Not part of the wxBookctrl API, but must be called in OnIdle or
267// by application to realize the toolbar and select the initial page.
268void wxToolbook::Realize()
269{
270 if (m_needsRealizing)
271 {
272 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
273
274 int remap = wxSystemOptions::GetOptionInt(wxT("msw.remap"));
275 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
276 GetToolBar()->Realize();
277 wxSystemOptions::SetOption(wxT("msw.remap"), remap);
278 }
279
280 m_needsRealizing = false;
281
282 if (m_selection == -1)
283 m_selection = 0;
284
285 if (GetPageCount() > 0)
286 {
287 int sel = m_selection;
288 m_selection = -1;
289 SetSelection(sel);
290 }
291
292 DoSize();
293}
294
295int wxToolbook::HitTest(const wxPoint& pt, long *flags) const
296{
297 int pagePos = wxNOT_FOUND;
298
299 if ( flags )
300 *flags = wxBK_HITTEST_NOWHERE;
301
302 // convert from wxToolbook coordinates to wxToolBar ones
303 const wxToolBarBase * const tbar = GetToolBar();
304 const wxPoint tbarPt = tbar->ScreenToClient(ClientToScreen(pt));
305
306 // is the point over the toolbar?
307 if ( wxRect(tbar->GetSize()).Contains(tbarPt) )
308 {
309 const wxToolBarToolBase * const
310 tool = tbar->FindToolForPosition(tbarPt.x, tbarPt.y);
311
312 if ( tool )
313 {
314 pagePos = tbar->GetToolPos(tool->GetId());
315 if ( flags )
316 *flags = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL;
317 }
318 }
319 else // not over the toolbar
320 {
321 if ( flags && GetPageRect().Contains(pt) )
322 *flags |= wxBK_HITTEST_ONPAGE;
323 }
324
325 return pagePos;
326}
327
328void wxToolbook::OnIdle(wxIdleEvent& event)
329{
330 if (m_needsRealizing)
331 Realize();
332 event.Skip();
333}
334
335// ----------------------------------------------------------------------------
336// adding/removing the pages
337// ----------------------------------------------------------------------------
338
339bool wxToolbook::InsertPage(size_t n,
340 wxWindow *page,
341 const wxString& text,
342 bool bSelect,
343 int imageId)
344{
345 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
346 return false;
347
348 m_needsRealizing = true;
349
350 wxASSERT(GetImageList() != NULL);
351
352 if (!GetImageList())
353 return false;
354
355 // TODO: make sure all platforms can convert between icon and bitmap,
356 // and/or test whether the image is a bitmap or an icon.
357#ifdef __WXMAC__
358 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
359#else
360 // On Windows, we can lose information by using GetBitmap, so extract icon instead
361 wxIcon icon = GetImageList()->GetIcon(imageId);
362 wxBitmap bitmap;
363 bitmap.CopyFromIcon(icon);
364#endif
365
366 m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x);
367 m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y);
368
369 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
370 GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text);
371
372 if (bSelect)
373 {
374 GetToolBar()->ToggleTool(n, true);
375 m_selection = n;
376 }
377 else
378 page->Hide();
379
380 InvalidateBestSize();
381 return true;
382}
383
384wxWindow *wxToolbook::DoRemovePage(size_t page)
385{
386 const size_t page_count = GetPageCount();
387 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
388
389 if ( win )
390 {
391 GetToolBar()->DeleteTool(page + 1);
392
393 if (m_selection >= (int)page)
394 {
395 // force new sel valid if possible
396 int sel = m_selection - 1;
397 if (page_count == 1)
398 sel = wxNOT_FOUND;
399 else if ((page_count == 2) || (sel == -1))
400 sel = 0;
401
402 // force sel invalid if deleting current page - don't try to hide it
403 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
404
405 if ((sel != wxNOT_FOUND) && (sel != m_selection))
406 SetSelection(sel);
407 }
408 }
409
410 return win;
411}
412
413
414bool wxToolbook::DeleteAllPages()
415{
416 GetToolBar()->ClearTools();
417 return wxBookCtrlBase::DeleteAllPages();
418}
419
420// ----------------------------------------------------------------------------
421// wxToolbook events
422// ----------------------------------------------------------------------------
423
424void wxToolbook::OnToolSelected(wxCommandEvent& event)
425{
426 const int selNew = event.GetId() - 1;
427
428 if ( selNew == m_selection )
429 {
430 // this event can only come from our own Select(m_selection) below
431 // which we call when the page change is vetoed, so we should simply
432 // ignore it
433 return;
434 }
435
436 SetSelection(selNew);
437
438 // change wasn't allowed, return to previous state
439 if (m_selection != selNew)
440 {
441 GetToolBar()->ToggleTool(m_selection, false);
442 }
443}
444
445#endif // wxUSE_TOOLBOOK