Redfine some events
[wxWidgets.git] / src / os2 / notebook.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: notebook.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/12/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 // wxWindows
16 #ifndef WX_PRECOMP
17 #include <wx/string.h>
18 #endif // WX_PRECOMP
19
20 #include <wx/log.h>
21 #include <wx/imaglist.h>
22 #include <wx/event.h>
23 #include <wx/control.h>
24 #include <wx/notebook.h>
25
26 #include <wx/os2/private.h>
27
28
29 // ----------------------------------------------------------------------------
30 // macros
31 // ----------------------------------------------------------------------------
32 // check that the page index is valid
33 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
34
35 // hide the ugly cast
36 #define m_hwnd (HWND)GetHWND()
37
38 // ----------------------------------------------------------------------------
39 // constants
40 // ----------------------------------------------------------------------------
41
42 // This is a work-around for missing defines in gcc-2.95 headers
43 #ifndef TCS_RIGHT
44 #define TCS_RIGHT 0x0002
45 #endif
46
47 #ifndef TCS_VERTICAL
48 #define TCS_VERTICAL 0x0080
49 #endif
50
51 #ifndef TCS_BOTTOM
52 #define TCS_BOTTOM TCS_RIGHT
53 #endif
54
55 // ----------------------------------------------------------------------------
56 // event table
57 // ----------------------------------------------------------------------------
58
59 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
60 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
61
62 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
63 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
64 EVT_SIZE(wxNotebook::OnSize)
65 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
66 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
67 END_EVENT_TABLE()
68
69 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
70 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
71
72 // ============================================================================
73 // implementation
74 // ============================================================================
75
76 // ----------------------------------------------------------------------------
77 // wxNotebook construction
78 // ----------------------------------------------------------------------------
79
80 // common part of all ctors
81 void wxNotebook::Init()
82 {
83 m_pImageList = NULL;
84 m_nSelection = -1;
85 }
86
87 // default for dynamic class
88 wxNotebook::wxNotebook()
89 {
90 Init();
91 }
92
93 // the same arguments as for wxControl
94 wxNotebook::wxNotebook(wxWindow *parent,
95 wxWindowID id,
96 const wxPoint& pos,
97 const wxSize& size,
98 long style,
99 const wxString& name)
100 {
101 Init();
102
103 Create(parent, id, pos, size, style, name);
104 }
105
106 // Create() function
107 bool wxNotebook::Create(wxWindow *parent,
108 wxWindowID id,
109 const wxPoint& pos,
110 const wxSize& size,
111 long style,
112 const wxString& name)
113 {
114 // base init
115 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
116 return FALSE;
117
118 // colors and font
119 // TODO: m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE));
120 m_foregroundColour = *wxBLACK ;
121
122 // TODO:
123 /*
124 // style
125 m_windowStyle = style | wxTAB_TRAVERSAL;
126
127 long tabStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | TCS_TABS;
128
129 if (m_windowStyle & wxCLIP_CHILDREN)
130 tabStyle |= WS_CLIPCHILDREN;
131 if ( m_windowStyle & wxTC_MULTILINE )
132 tabStyle |= TCS_MULTILINE;
133 if ( m_windowStyle & wxBORDER )
134 tabStyle &= WS_BORDER;
135 if (m_windowStyle & wxNB_FIXEDWIDTH)
136 tabStyle |= TCS_FIXEDWIDTH ;
137 if (m_windowStyle & wxNB_BOTTOM)
138 tabStyle |= TCS_RIGHT;
139 if (m_windowStyle & wxNB_LEFT)
140 tabStyle |= TCS_VERTICAL;
141 if (m_windowStyle & wxNB_RIGHT)
142 tabStyle |= TCS_VERTICAL|TCS_RIGHT;
143
144
145 if ( !MSWCreate(GetId(), GetParent(), WC_TABCONTROL,
146 this, NULL, pos.x, pos.y, size.x, size.y,
147 tabStyle, NULL, 0) )
148 {
149 return FALSE;
150 }
151
152 // Not all compilers recognise SetWindowFont
153 ::SendMessage(GetHwnd(), WM_SETFONT,
154 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), TRUE);
155
156
157 if ( parent != NULL )
158 parent->AddChild(this);
159 */
160 SubclassWin(m_hWnd);
161
162 return FALSE;
163 }
164
165 // dtor
166 wxNotebook::~wxNotebook()
167 {
168 }
169
170 // ----------------------------------------------------------------------------
171 // wxNotebook accessors
172 // ----------------------------------------------------------------------------
173 int wxNotebook::GetPageCount() const
174 {
175 return m_aPages.Count();
176 }
177
178 int wxNotebook::GetRowCount() const
179 {
180 // TODO
181 return 0;
182 }
183
184 int wxNotebook::SetSelection(int nPage)
185 {
186 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
187
188 ChangePage(m_nSelection, nPage);
189
190 // TODO
191 return 0;
192 }
193
194 void wxNotebook::AdvanceSelection(bool bForward)
195 {
196 int nSel = GetSelection();
197 int nMax = GetPageCount() - 1;
198 if ( bForward )
199 SetSelection(nSel == nMax ? 0 : nSel + 1);
200 else
201 SetSelection(nSel == 0 ? nMax : nSel - 1);
202 }
203
204 bool wxNotebook::SetPageText(int nPage, const wxString& strText)
205 {
206 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
207
208 // TODO
209 return FALSE;
210 }
211
212 wxString wxNotebook::GetPageText(int nPage) const
213 {
214 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxT(""), wxT("notebook page out of range") );
215
216 // TODO
217 return wxString("");
218 }
219
220 int wxNotebook::GetPageImage(int nPage) const
221 {
222 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
223
224 // TODO
225 return 0;
226 }
227
228 bool wxNotebook::SetPageImage(int nPage, int nImage)
229 {
230 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
231
232 // TODO
233 return FALSE;
234 }
235
236 void wxNotebook::SetImageList(wxImageList* imageList)
237 {
238 m_pImageList = imageList;
239 // TODO
240 }
241
242 void wxNotebook::SetTabSize(const wxSize& sz)
243 {
244 // TODO
245 }
246
247 // ----------------------------------------------------------------------------
248 // wxNotebook operations
249 // ----------------------------------------------------------------------------
250
251 // remove one page from the notebook
252 bool wxNotebook::DeletePage(int nPage)
253 {
254 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
255
256 // TODO: delete native widget page
257
258 delete m_aPages[nPage];
259 m_aPages.Remove(nPage);
260
261 return TRUE;
262 }
263
264 // remove one page from the notebook, without deleting the window
265 bool wxNotebook::RemovePage(int nPage)
266 {
267 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
268
269 m_aPages.Remove(nPage);
270
271 return TRUE;
272 }
273
274 // remove all pages
275 bool wxNotebook::DeleteAllPages()
276 {
277 // TODO: delete native widget pages
278
279 int nPageCount = GetPageCount();
280 int nPage;
281 for ( nPage = 0; nPage < nPageCount; nPage++ )
282 delete m_aPages[nPage];
283
284 m_aPages.Clear();
285
286 return TRUE;
287 }
288
289 // add a page to the notebook
290 bool wxNotebook::AddPage(wxNotebookPage *pPage,
291 const wxString& strText,
292 bool bSelect,
293 int imageId)
294 {
295 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
296 }
297
298 // same as AddPage() but does it at given position
299 bool wxNotebook::InsertPage(int nPage,
300 wxNotebookPage *pPage,
301 const wxString& strText,
302 bool bSelect,
303 int imageId)
304 {
305 wxASSERT( pPage != NULL );
306 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
307
308 // TODO: insert native widget page
309
310 // save the pointer to the page
311 m_aPages.Insert(pPage, nPage);
312
313 // some page must be selected: either this one or the first one if there is
314 // still no selection
315 if ( bSelect )
316 m_nSelection = nPage;
317 else if ( m_nSelection == -1 )
318 m_nSelection = 0;
319
320 return TRUE;
321 }
322
323 // ----------------------------------------------------------------------------
324 // wxNotebook callbacks
325 // ----------------------------------------------------------------------------
326
327 // @@@ OnSize() is used for setting the font when it's called for the first
328 // time because doing it in ::Create() doesn't work (for unknown reasons)
329 void wxNotebook::OnSize(wxSizeEvent& event)
330 {
331 static bool s_bFirstTime = TRUE;
332 if ( s_bFirstTime ) {
333 // TODO: any first-time-size processing.
334 s_bFirstTime = FALSE;
335 }
336
337 // TODO: all this may or may not be necessary for your platform
338
339 // emulate page change (it's esp. important to do it first time because
340 // otherwise our page would stay invisible)
341 int nSel = m_nSelection;
342 m_nSelection = -1;
343 SetSelection(nSel);
344
345 // fit the notebook page to the tab control's display area
346 int w, h;
347 GetSize(&w, &h);
348
349 unsigned int nCount = m_aPages.Count();
350 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
351 wxNotebookPage *pPage = m_aPages[nPage];
352 pPage->SetSize(0, 0, w, h);
353 #if wxUSE_CONSTRAINTS
354 if ( pPage->GetAutoLayout() )
355 pPage->Layout();
356 #endif //wxUSE_CONSTRAINTS
357
358 }
359
360 // Processing continues to next OnSize
361 event.Skip();
362 }
363
364 void wxNotebook::OnSelChange(wxNotebookEvent& event)
365 {
366 // is it our tab control?
367 if ( event.GetEventObject() == this )
368 {
369 int sel = event.GetOldSelection();
370 if ( sel != -1 )
371 m_aPages[sel]->Show(FALSE);
372
373 sel = event.GetSelection();
374 if ( sel != -1 )
375 {
376 wxNotebookPage *pPage = m_aPages[sel];
377 pPage->Show(TRUE);
378 pPage->SetFocus();
379 }
380
381 m_nSelection = sel;
382 }
383 // we want to give others a chance to process this message as well
384 event.Skip();
385 }
386
387 void wxNotebook::OnSetFocus(wxFocusEvent& event)
388 {
389 // set focus to the currently selected page if any
390 if ( m_nSelection != -1 )
391 m_aPages[m_nSelection]->SetFocus();
392
393 event.Skip();
394 }
395
396 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
397 {
398 if ( event.IsWindowChange() ) {
399 // change pages
400 AdvanceSelection(event.GetDirection());
401 }
402 else {
403 // pass to the parent
404 if ( GetParent() ) {
405 event.SetCurrentFocus(this);
406 GetParent()->ProcessEvent(event);
407 }
408 }
409 }
410
411 // ----------------------------------------------------------------------------
412 // wxNotebook base class virtuals
413 // ----------------------------------------------------------------------------
414
415 // override these 2 functions to do nothing: everything is done in OnSize
416
417 void wxNotebook::SetConstraintSizes(bool /* recurse */)
418 {
419 #if wxUSE_CONSTRAINTS
420 // don't set the sizes of the pages - their correct size is not yet known
421 wxControl::SetConstraintSizes(FALSE);
422 #endif
423 }
424
425 bool wxNotebook::DoPhase(int /* nPhase */)
426 {
427 return TRUE;
428 }
429
430 bool wxNotebook::OS2OnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
431 {
432 wxNotebookEvent event(wxEVT_NULL, m_windowId);
433 //TODO:
434 /*
435 NMHDR* hdr = (NMHDR *)lParam;
436 switch ( hdr->code ) {
437 case TCN_SELCHANGE:
438 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
439 break;
440
441 case TCN_SELCHANGING:
442 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
443 break;
444
445 default:
446 return wxControl::MSWOnNotify(idCtrl, lParam, result);
447 }
448 */
449 // TODO: event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
450 event.SetOldSelection(m_nSelection);
451 event.SetEventObject(this);
452 event.SetInt(idCtrl);
453
454 bool processed = GetEventHandler()->ProcessEvent(event);
455 // TODO: *result = !event.IsAllowed();
456 return processed;
457 }
458
459 // ----------------------------------------------------------------------------
460 // wxNotebook helper functions
461 // ----------------------------------------------------------------------------
462
463 // hide the currently active panel and show the new one
464 void wxNotebook::ChangePage(int nOldSel, int nSel)
465 {
466 // MT-FIXME should use a real semaphore
467 static bool s_bInsideChangePage = FALSE;
468
469 // when we call ProcessEvent(), our own OnSelChange() is called which calls
470 // this function - break the infinite loop
471 if ( s_bInsideChangePage )
472 return;
473
474 // it's not an error (the message may be generated by the tab control itself)
475 // and it may happen - just do nothing
476 if ( nSel == nOldSel )
477 return;
478
479 s_bInsideChangePage = TRUE;
480
481 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
482 event.SetSelection(nSel);
483 event.SetOldSelection(nOldSel);
484 event.SetEventObject(this);
485 if ( ProcessEvent(event) && !event.IsAllowed() )
486 {
487 // program doesn't allow the page change
488 s_bInsideChangePage = FALSE;
489 return;
490 }
491
492 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
493 ProcessEvent(event);
494
495 s_bInsideChangePage = FALSE;
496 }
497