]> git.saurik.com Git - wxWidgets.git/blob - src/generic/toolbkg.cpp
Use CFSocket instead of CFFileDescriptor in wxCFEventLoopSource.
[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 // 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
46 IMPLEMENT_DYNAMIC_CLASS(wxToolbook, wxBookCtrlBase)
47
48 wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGING, wxBookCtrlEvent );
49 wxDEFINE_EVENT( wxEVT_TOOLBOOK_PAGE_CHANGED, wxBookCtrlEvent );
50
51 BEGIN_EVENT_TABLE(wxToolbook, wxBookCtrlBase)
52 EVT_SIZE(wxToolbook::OnSize)
53 EVT_TOOL_RANGE(1, 50, wxToolbook::OnToolSelected)
54 EVT_IDLE(wxToolbook::OnIdle)
55 END_EVENT_TABLE()
56
57 // ============================================================================
58 // wxToolbook implementation
59 // ============================================================================
60
61 // ----------------------------------------------------------------------------
62 // wxToolbook creation
63 // ----------------------------------------------------------------------------
64
65 void wxToolbook::Init()
66 {
67 m_needsRealizing = false;
68 }
69
70 bool wxToolbook::Create(wxWindow *parent,
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 )
78 style |= wxBK_TOP;
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
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;
96
97 // TODO: make more configurable
98
99 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
100 if (style & wxTBK_BUTTONBAR)
101 {
102 m_bookctrl = new wxButtonToolBar
103 (
104 this,
105 wxID_ANY,
106 wxDefaultPosition,
107 wxDefaultSize,
108 tbFlags
109 );
110 }
111 else
112 #endif
113 {
114 m_bookctrl = new wxToolBar
115 (
116 this,
117 wxID_ANY,
118 wxDefaultPosition,
119 wxDefaultSize,
120 tbFlags | wxTB_NODIVIDER
121 );
122 }
123
124 return true;
125 }
126
127 // ----------------------------------------------------------------------------
128 // wxToolbook geometry management
129 // ----------------------------------------------------------------------------
130
131 void wxToolbook::OnSize(wxSizeEvent& event)
132 {
133 if (m_needsRealizing)
134 Realize();
135
136 wxBookCtrlBase::OnSize(event);
137 }
138
139 // ----------------------------------------------------------------------------
140 // accessing the pages
141 // ----------------------------------------------------------------------------
142
143 bool wxToolbook::SetPageText(size_t n, const wxString& strText)
144 {
145 // Assume tool ids start from 1
146 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
147 if (tool)
148 {
149 tool->SetLabel(strText);
150 return true;
151 }
152 else
153 return false;
154 }
155
156 wxString wxToolbook::GetPageText(size_t n) const
157 {
158 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
159 if (tool)
160 return tool->GetLabel();
161 else
162 return wxEmptyString;
163 }
164
165 int wxToolbook::GetPageImage(size_t WXUNUSED(n)) const
166 {
167 wxFAIL_MSG( wxT("wxToolbook::GetPageImage() not implemented") );
168
169 return wxNOT_FOUND;
170 }
171
172 bool wxToolbook::SetPageImage(size_t n, int imageId)
173 {
174 wxASSERT( GetImageList() != NULL );
175 if (!GetImageList())
176 return false;
177
178 wxToolBarToolBase* tool = GetToolBar()->FindById(n + 1);
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
194 void wxToolbook::SetImageList(wxImageList *imageList)
195 {
196 wxBookCtrlBase::SetImageList(imageList);
197 }
198
199 // ----------------------------------------------------------------------------
200 // selection
201 // ----------------------------------------------------------------------------
202
203 wxBookCtrlEvent* wxToolbook::CreatePageChangingEvent() const
204 {
205 return new wxBookCtrlEvent(wxEVT_TOOLBOOK_PAGE_CHANGING, m_windowId);
206 }
207
208 void wxToolbook::MakeChangedEvent(wxBookCtrlEvent &event)
209 {
210 event.SetEventType(wxEVT_TOOLBOOK_PAGE_CHANGED);
211 }
212
213 void wxToolbook::UpdateSelectedPage(size_t newsel)
214 {
215 m_selection = newsel;
216 GetToolBar()->ToggleTool(newsel + 1, true);
217 }
218
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.
221 void wxToolbook::Realize()
222 {
223 if (m_needsRealizing)
224 {
225 m_needsRealizing = false;
226
227 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
228
229 GetToolBar()->Realize();
230 }
231
232 if (m_selection == wxNOT_FOUND)
233 m_selection = 0;
234
235 if (GetPageCount() > 0)
236 {
237 int sel = m_selection;
238 m_selection = wxNOT_FOUND;
239 SetSelection(sel);
240 }
241
242 DoSize();
243 }
244
245 int wxToolbook::HitTest(const wxPoint& pt, long *flags) const
246 {
247 int pagePos = wxNOT_FOUND;
248
249 if ( flags )
250 *flags = wxBK_HITTEST_NOWHERE;
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?
257 if ( wxRect(tbar->GetSize()).Contains(tbarPt) )
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 )
266 *flags = wxBK_HITTEST_ONICON | wxBK_HITTEST_ONLABEL;
267 }
268 }
269 else // not over the toolbar
270 {
271 if ( flags && GetPageRect().Contains(pt) )
272 *flags |= wxBK_HITTEST_ONPAGE;
273 }
274
275 return pagePos;
276 }
277
278 void wxToolbook::OnIdle(wxIdleEvent& event)
279 {
280 if (m_needsRealizing)
281 Realize();
282 event.Skip();
283 }
284
285 // ----------------------------------------------------------------------------
286 // adding/removing the pages
287 // ----------------------------------------------------------------------------
288
289 bool wxToolbook::InsertPage(size_t n,
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;
299
300 wxASSERT(GetImageList() != NULL);
301
302 if (!GetImageList())
303 return false;
304
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
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);
314 #endif
315
316 m_maxBitmapSize.x = wxMax(bitmap.GetWidth(), m_maxBitmapSize.x);
317 m_maxBitmapSize.y = wxMax(bitmap.GetHeight(), m_maxBitmapSize.y);
318
319 GetToolBar()->SetToolBitmapSize(m_maxBitmapSize);
320 GetToolBar()->AddRadioTool(n + 1, text, bitmap, wxNullBitmap, text);
321
322 if (bSelect)
323 {
324 GetToolBar()->ToggleTool(n, true);
325 m_selection = n;
326 }
327 else
328 page->Hide();
329
330 InvalidateBestSize();
331 return true;
332 }
333
334 wxWindow *wxToolbook::DoRemovePage(size_t page)
335 {
336 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
337
338 if ( win )
339 {
340 GetToolBar()->DeleteTool(page + 1);
341
342 DoSetSelectionAfterRemoval(page);
343 }
344
345 return win;
346 }
347
348
349 bool wxToolbook::DeleteAllPages()
350 {
351 GetToolBar()->ClearTools();
352 return wxBookCtrlBase::DeleteAllPages();
353 }
354
355 // ----------------------------------------------------------------------------
356 // wxToolbook events
357 // ----------------------------------------------------------------------------
358
359 void wxToolbook::OnToolSelected(wxCommandEvent& event)
360 {
361 const int selNew = event.GetId() - 1;
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)
375 {
376 GetToolBar()->ToggleTool(m_selection, false);
377 }
378 }
379
380 #endif // wxUSE_TOOLBOOK