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