]> git.saurik.com Git - wxWidgets.git/blame - src/generic/toolbkg.cpp
Make wxEventLoop::AddSourceForFD() static.
[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)
3c55b365 47
ce7fe42e
VZ
48wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGING, wxBookCtrlEvent );
49wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGED, wxBookCtrlEvent );
3c55b365
JS
50
51BEGIN_EVENT_TABLE(wxToolbook, wxBookCtrlBase)
52 EVT_SIZE(wxToolbook::OnSize)
53 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected)
54 EVT_IDLE(wxToolbook::OnIdle)
55END_EVENT_TABLE()
56
57// ============================================================================
58// wxToolbook implementation
59// ============================================================================
60
61// ----------------------------------------------------------------------------
62// wxToolbook creation
63// ----------------------------------------------------------------------------
64
65void wxToolbook::Init()
66{
3c55b365
JS
67 m_needsRealizing = false;
68}
69
c2794afd 70bool wxToolbook::Create(wxWindow *parent,
3c55b365
JS
71 wxWindowID id,
72 const wxPoint& pos,
73 const wxSize& size,
74 long style,
75 const wxString& name)
76{
77 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
3c55b365 78 style |= wxBK_TOP;
3c55b365
JS
79
80 // no border for this control
81 style &= ~wxBORDER_MASK;
82 style |= wxBORDER_NONE;
83
84 if ( !wxControl::Create(parent, id, pos, size, style,
85 wxDefaultValidator, name) )
86 return false;
87
050d159c
VZ
88 int tbFlags = wxTB_TEXT | wxTB_FLAT | wxBORDER_NONE;
89 if ( (style & (wxBK_LEFT | wxBK_RIGHT)) != 0 )
90 tbFlags |= wxTB_VERTICAL;
91 else
92 tbFlags |= wxTB_HORIZONTAL;
93
94 if ( style & wxTBK_HORZ_LAYOUT )
95 tbFlags |= wxTB_HORZ_LAYOUT;
c2794afd 96
917cd965 97 // TODO: make more configurable
9804d540 98
b887dc7b 99#if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
e3507dd8 100 if (style & wxTBK_BUTTONBAR)
64d3ed17
JS
101 {
102 m_bookctrl = new wxButtonToolBar
103 (
104 this,
447325a4 105 wxID_ANY,
64d3ed17
JS
106 wxDefaultPosition,
107 wxDefaultSize,
050d159c 108 tbFlags
64d3ed17
JS
109 );
110 }
111 else
b887dc7b 112#endif
64d3ed17
JS
113 {
114 m_bookctrl = new wxToolBar
3c55b365
JS
115 (
116 this,
447325a4 117 wxID_ANY,
3c55b365
JS
118 wxDefaultPosition,
119 wxDefaultSize,
050d159c 120 tbFlags | wxTB_NODIVIDER
3c55b365 121 );
64d3ed17 122 }
3c55b365
JS
123
124 return true;
125}
126
127// ----------------------------------------------------------------------------
128// wxToolbook geometry management
129// ----------------------------------------------------------------------------
130
3c55b365
JS
131void wxToolbook::OnSize(wxSizeEvent& event)
132{
133 if (m_needsRealizing)
134 Realize();
c2794afd 135
3c55b365
JS
136 wxBookCtrlBase::OnSize(event);
137}
138
3c55b365
JS
139// ----------------------------------------------------------------------------
140// accessing the pages
141// ----------------------------------------------------------------------------
142
143bool wxToolbook::SetPageText(size_t n, const wxString& strText)
144{
145 // Assume tool ids start from 1
c2794afd 146 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365
JS
147 if (tool)
148 {
149 tool->SetLabel(strText);
150 return true;
151 }
152 else
153 return false;
154}
155
156wxString wxToolbook::GetPageText(size_t n) const
157{
c2794afd 158 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365 159 if (tool)
3c55b365 160 return tool->GetLabel();
3c55b365
JS
161 else
162 return wxEmptyString;
163}
164
165int wxToolbook::GetPageImage(size_t WXUNUSED(n)) const
166{
9a83f860 167 wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") );
3c55b365
JS
168
169 return wxNOT_FOUND;
170}
171
172bool wxToolbook::SetPageImage(size_t n, int imageId)
173{
174 wxASSERT( GetImageList() != NULL );
175 if (!GetImageList())
176 return false;
c2794afd
DS
177
178 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
3c55b365
JS
179 if (tool)
180 {
181 // Find the image list index for this tool
182 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
183 tool->SetNormalBitmap(bitmap);
184 return true;
185 }
186 else
187 return false;
188}
189
190// ----------------------------------------------------------------------------
191// image list stuff
192// ----------------------------------------------------------------------------
193
194void wxToolbook::SetImageList(wxImageList *imageList)
195{
196 wxBookCtrlBase::SetImageList(imageList);
197}
198
199// ----------------------------------------------------------------------------
200// selection
201// ----------------------------------------------------------------------------
202
3e97a905 203wxBookCtrlEvent* wxToolbook::CreatePageChangingEvent() const
3c55b365 204{
ce7fe42e 205 return new wxBookCtrlEvent(wxEVT_TOOLBOOK_PAGE_CHANGING, m_windowId);
deb325e3
VZ
206}
207
3e97a905 208void wxToolbook::MakeChangedEvent(wxBookCtrlEvent &event)
deb325e3 209{
ce7fe42e 210 event.SetEventType(wxEVT_TOOLBOOK_PAGE_CHANGED);
3c55b365
JS
211}
212
8de043bb
RR
213void wxToolbook::UpdateSelectedPage(size_t newsel)
214{
215 m_selection = newsel;
216 GetToolBar()->ToggleTool(newsel + 1, true);
217}
218
3c55b365
JS
219// Not part of the wxBookctrl API, but must be called in OnIdle or
220// by application to realize the toolbar and select the initial page.
221void wxToolbook::Realize()
222{
223 if (m_needsRealizing)
224 {
1c54c4ed
VZ
225 m_needsRealizing = false;
226
3c55b365 227 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
a16c6fdc 228
3c55b365 229 GetToolBar()->Realize();
3c55b365 230 }
c2794afd 231
7e837615 232 if (m_selection == wxNOT_FOUND)
3c55b365
JS
233 m_selection = 0;
234
235 if (GetPageCount() > 0)
236 {
237 int sel = m_selection;
7e837615 238 m_selection = wxNOT_FOUND;
3c55b365
JS
239 SetSelection(sel);
240 }
c2794afd 241
3c55b365
JS
242 DoSize();
243}
244
d0a84b63
VZ
245int wxToolbook::HitTest(const wxPoint& pt, long *flags) const
246{
247 int pagePos = wxNOT_FOUND;
248
249 if ( flags )
9804d540 250 *flags = wxBK_HITTEST_NOWHERE;
d0a84b63
VZ
251
252 // convert from wxToolbook coordinates to wxToolBar ones
253 const wxToolBarBase * const tbar = GetToolBar();
254 const wxPoint tbarPt = tbar->ScreenToClient(ClientToScreen(pt));
255
256 // is the point over the toolbar?
22a35096 257 if ( wxRect(tbar->GetSize()).Contains(tbarPt) )
d0a84b63
VZ
258 {
259 const wxToolBarToolBase * const
260 tool = tbar->FindToolForPosition(tbarPt.x, tbarPt.y);
261
262 if ( tool )
263 {
264 pagePos = tbar->GetToolPos(tool->GetId());
265 if ( flags )
9804d540 266 *flags = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL;
d0a84b63
VZ
267 }
268 }
269 else // not over the toolbar
270 {
22a35096 271 if ( flags && GetPageRect().Contains(pt) )
9804d540 272 *flags |= wxBK_HITTEST_ONPAGE;
d0a84b63
VZ
273 }
274
275 return pagePos;
276}
277
3c55b365
JS
278void wxToolbook::OnIdle(wxIdleEvent& event)
279{
280 if (m_needsRealizing)
281 Realize();
282 event.Skip();
283}
284
285// ----------------------------------------------------------------------------
286// adding/removing the pages
287// ----------------------------------------------------------------------------
288
c2794afd 289bool wxToolbook::InsertPage(size_t n,
3c55b365
JS
290 wxWindow *page,
291 const wxString& text,
292 bool bSelect,
293 int imageId)
294{
295 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
296 return false;
297
298 m_needsRealizing = true;
c2794afd 299
3c55b365 300 wxASSERT(GetImageList() != NULL);
c2794afd 301
3c55b365
JS
302 if (!GetImageList())
303 return false;
304
556bf2c3
JS
305 // TODO: make sure all platforms can convert between icon and bitmap,
306 // and/or test whether the image is a bitmap or an icon.
307#ifdef __WXMAC__
308 wxBitmap bitmap = GetImageList()->GetBitmap(imageId);
309#else
3c55b365
JS
310 // On Windows, we can lose information by using GetBitmap, so extract icon instead
311 wxIcon icon = GetImageList()->GetIcon(imageId);
312 wxBitmap bitmap;
313 bitmap.CopyFromIcon(icon);
556bf2c3 314#endif
c2794afd 315
3c55b365
JS
316 m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x);
317 m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y);
c2794afd 318
3c55b365 319 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
c2794afd 320 GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text);
3c55b365
JS
321
322 if (bSelect)
323 {
77631b1d 324 GetToolBar()->ToggleTool(n, true);
3c55b365
JS
325 m_selection = n;
326 }
327 else
328 page->Hide();
329
330 InvalidateBestSize();
331 return true;
332}
333
334wxWindow *wxToolbook::DoRemovePage(size_t page)
335{
3c55b365
JS
336 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
337
338 if ( win )
339 {
c2794afd 340 GetToolBar()->DeleteTool(page + 1);
3c55b365 341
4f9ccec5 342 DoSetSelectionAfterRemoval(page);
3c55b365
JS
343 }
344
345 return win;
346}
347
348
349bool wxToolbook::DeleteAllPages()
350{
351 GetToolBar()->ClearTools();
352 return wxBookCtrlBase::DeleteAllPages();
353}
354
355// ----------------------------------------------------------------------------
356// wxToolbook events
357// ----------------------------------------------------------------------------
358
359void wxToolbook::OnToolSelected(wxCommandEvent& event)
360{
c2794afd 361 const int selNew = event.GetId() - 1;
3c55b365
JS
362
363 if ( selNew == m_selection )
364 {
365 // this event can only come from our own Select(m_selection) below
366 // which we call when the page change is vetoed, so we should simply
367 // ignore it
368 return;
369 }
370
371 SetSelection(selNew);
372
373 // change wasn't allowed, return to previous state
374 if (m_selection != selNew)
28bfd0a1 375 {
3c55b365 376 GetToolBar()->ToggleTool(m_selection, false);
28bfd0a1 377 }
3c55b365
JS
378}
379
380#endif // wxUSE_TOOLBOOK