]> git.saurik.com Git - wxWidgets.git/blame - src/generic/toolbkg.cpp
Check for m_treeCtrl!=NULL, [ wxwindows-Patches-1953459 ] wxGenericDirCtrl::SetFocus...
[wxWidgets.git] / src / generic / toolbkg.cpp
CommitLineData
3c55b365
JS
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
3c55b365
JS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
34d6780e 19#if wxUSE_TOOLBOOK
3c55b365 20
28bfd0a1
DS
21#ifndef WX_PRECOMP
22 #include "wx/icon.h"
6037dd26
DS
23 #include "wx/settings.h"
24 #include "wx/toolbar.h"
28bfd0a1
DS
25#endif
26
6037dd26
DS
27#include "wx/imaglist.h"
28#include "wx/sysopt.h"
29#include "wx/toolbook.h"
b887dc7b
JS
30
31#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
64d3ed17 32#include "wx/generic/buttonbar.h"
b887dc7b 33#endif
6037dd26 34
3c55b365
JS
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();
3c55b365
JS
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
c2794afd 72bool wxToolbook::Create(wxWindow *parent,
3c55b365
JS
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 )
3c55b365 80 style |= wxBK_TOP;
3c55b365
JS
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
050d159c
VZ
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;
c2794afd 98
917cd965 99 // TODO: make more configurable
9804d540 100
b887dc7b 101#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
e3507dd8 102 if (style & wxTBK_BUTTONBAR)
64d3ed17
JS
103 {
104 m_bookctrl = new wxButtonToolBar
105 (
106 this,
447325a4 107 wxID_ANY,
64d3ed17
JS
108 wxDefaultPosition,
109 wxDefaultSize,
050d159c 110 tbFlags
64d3ed17
JS
111 );
112 }
113 else
b887dc7b 114#endif
64d3ed17
JS
115 {
116 m_bookctrl = new wxToolBar
3c55b365
JS
117 (
118 this,
447325a4 119 wxID_ANY,
3c55b365
JS
120 wxDefaultPosition,
121 wxDefaultSize,
050d159c 122 tbFlags | wxTB_NODIVIDER
3c55b365 123 );
64d3ed17 124 }
3c55b365
JS
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();
c2794afd 159
3c55b365
JS
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
3c55b365
JS
181// ----------------------------------------------------------------------------
182// accessing the pages
183// ----------------------------------------------------------------------------
184
185bool wxToolbook::SetPageText(size_t n, const wxString& strText)
186{
187 // Assume tool ids start from 1
c2794afd 188 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365
JS
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{
c2794afd 200 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365 201 if (tool)
3c55b365 202 return tool->GetLabel();
3c55b365
JS
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;
c2794afd
DS
219
220 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365
JS
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
deb325e3 250wxBookCtrlBaseEvent* wxToolbook::CreatePageChangingEvent() const
3c55b365 251{
deb325e3
VZ
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);
3c55b365
JS
258}
259
8de043bb
RR
260void wxToolbook::UpdateSelectedPage(size_t newsel)
261{
262 m_selection = newsel;
263 GetToolBar()->ToggleTool(newsel + 1, true);
264}
265
3c55b365
JS
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);
a16c6fdc
RD
273
274 int remap = wxSystemOptions::GetOptionInt(wxT("msw.remap"));
3c55b365
JS
275 wxSystemOptions::SetOption(wxT("msw.remap"), 0);
276 GetToolBar()->Realize();
a16c6fdc 277 wxSystemOptions::SetOption(wxT("msw.remap"), remap);
3c55b365 278 }
c2794afd 279
3c55b365 280 m_needsRealizing = false;
c2794afd 281
3c55b365
JS
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 }
c2794afd 291
3c55b365
JS
292 DoSize();
293}
294
d0a84b63
VZ
295int wxToolbook::HitTest(const wxPoint& pt, long *flags) const
296{
297 int pagePos = wxNOT_FOUND;
298
299 if ( flags )
9804d540 300 *flags = wxBK_HITTEST_NOWHERE;
d0a84b63
VZ
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?
22a35096 307 if ( wxRect(tbar->GetSize()).Contains(tbarPt) )
d0a84b63
VZ
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 )
9804d540 316 *flags = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL;
d0a84b63
VZ
317 }
318 }
319 else // not over the toolbar
320 {
22a35096 321 if ( flags && GetPageRect().Contains(pt) )
9804d540 322 *flags |= wxBK_HITTEST_ONPAGE;
d0a84b63
VZ
323 }
324
325 return pagePos;
326}
327
3c55b365
JS
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
c2794afd 339bool wxToolbook::InsertPage(size_t n,
3c55b365
JS
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;
c2794afd 349
3c55b365 350 wxASSERT(GetImageList() != NULL);
c2794afd 351
3c55b365
JS
352 if (!GetImageList())
353 return false;
354
556bf2c3
JS
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
3c55b365
JS
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);
556bf2c3 364#endif
c2794afd 365
3c55b365
JS
366 m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x);
367 m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y);
c2794afd 368
3c55b365 369 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
c2794afd 370 GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text);
3c55b365
JS
371
372 if (bSelect)
373 {
77631b1d 374 GetToolBar()->ToggleTool(n, true);
3c55b365
JS
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 {
c2794afd 391 GetToolBar()->DeleteTool(page + 1);
3c55b365
JS
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{
c2794afd 426 const int selNew = event.GetId() - 1;
3c55b365
JS
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)
28bfd0a1 440 {
3c55b365 441 GetToolBar()->ToggleTool(m_selection, false);
28bfd0a1 442 }
3c55b365
JS
443}
444
445#endif // wxUSE_TOOLBOOK