]> git.saurik.com Git - wxWidgets.git/blame - src/os2/notebook.cpp
fix for discrepancies between wxNotebookEvent and wxNotebook GetSelection() results
[wxWidgets.git] / src / os2 / notebook.cpp
CommitLineData
0e320a79
DW
1///////////////////////////////////////////////////////////////////////////////
2// Name: notebook.cpp
3// Purpose: implementation of wxNotebook
cdf1e714 4// Author: David Webster
fb46a9a6 5// Modified by:
cdf1e714 6// Created: 10/12/99
0e320a79 7// RCS-ID: $Id$
cdf1e714 8// Copyright: (c) David Webster
0e320a79
DW
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
cdf1e714
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
0e320a79 14
cdf1e714
DW
15// wxWindows
16#ifndef WX_PRECOMP
17 #include <wx/string.h>
18#endif // WX_PRECOMP
0e320a79 19
0e320a79
DW
20#include <wx/log.h>
21#include <wx/imaglist.h>
cdf1e714
DW
22#include <wx/event.h>
23#include <wx/control.h>
0e320a79
DW
24#include <wx/notebook.h>
25
cdf1e714
DW
26#include <wx/os2/private.h>
27
28
0e320a79
DW
29// ----------------------------------------------------------------------------
30// macros
31// ----------------------------------------------------------------------------
0e320a79
DW
32// check that the page index is valid
33#define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
34
cdf1e714
DW
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
0e320a79
DW
55// ----------------------------------------------------------------------------
56// event table
57// ----------------------------------------------------------------------------
58
8546f11e
DW
59DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
60DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
61
62BEGIN_EVENT_TABLE(wxNotebook, wxControl)
0e320a79 63 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
0e320a79
DW
64 EVT_SIZE(wxNotebook::OnSize)
65 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
66 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
8546f11e 67END_EVENT_TABLE()
0e320a79 68
8546f11e
DW
69IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
70IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
0e320a79
DW
71
72// ============================================================================
73// implementation
74// ============================================================================
75
76// ----------------------------------------------------------------------------
77// wxNotebook construction
78// ----------------------------------------------------------------------------
79
80// common part of all ctors
81void wxNotebook::Init()
82{
83 m_pImageList = NULL;
84 m_nSelection = -1;
85}
86
87// default for dynamic class
88wxNotebook::wxNotebook()
89{
90 Init();
91}
92
93// the same arguments as for wxControl
94wxNotebook::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
107bool wxNotebook::Create(wxWindow *parent,
108 wxWindowID id,
109 const wxPoint& pos,
110 const wxSize& size,
111 long style,
112 const wxString& name)
113{
cdf1e714
DW
114 // base init
115 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
116 return FALSE;
117
118 // colors and font
fb46a9a6 119// TODO: m_backgroundColour = wxColour(GetSysColor(COLOR_BTNFACE));
cdf1e714
DW
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;
fb46a9a6 143
cdf1e714
DW
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 }
0e320a79 151
cdf1e714
DW
152 // Not all compilers recognise SetWindowFont
153 ::SendMessage(GetHwnd(), WM_SETFONT,
154 (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), TRUE);
0e320a79 155
0e320a79 156
cdf1e714
DW
157 if ( parent != NULL )
158 parent->AddChild(this);
159*/
160 SubclassWin(m_hWnd);
0e320a79
DW
161
162 return FALSE;
163}
164
165// dtor
166wxNotebook::~wxNotebook()
167{
168}
169
170// ----------------------------------------------------------------------------
171// wxNotebook accessors
172// ----------------------------------------------------------------------------
173int wxNotebook::GetPageCount() const
174{
175 return m_aPages.Count();
176}
177
178int wxNotebook::GetRowCount() const
179{
180 // TODO
181 return 0;
182}
183
184int wxNotebook::SetSelection(int nPage)
185{
cdf1e714 186 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
0e320a79
DW
187
188 ChangePage(m_nSelection, nPage);
189
190 // TODO
191 return 0;
192}
193
194void 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
204bool wxNotebook::SetPageText(int nPage, const wxString& strText)
205{
cdf1e714 206 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
0e320a79
DW
207
208 // TODO
209 return FALSE;
210}
211
212wxString wxNotebook::GetPageText(int nPage) const
213{
cdf1e714 214 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxT(""), wxT("notebook page out of range") );
0e320a79
DW
215
216 // TODO
217 return wxString("");
218}
219
220int wxNotebook::GetPageImage(int nPage) const
221{
cdf1e714 222 wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") );
0e320a79
DW
223
224 // TODO
225 return 0;
226}
227
228bool wxNotebook::SetPageImage(int nPage, int nImage)
229{
cdf1e714 230 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
0e320a79
DW
231
232 // TODO
233 return FALSE;
234}
235
236void wxNotebook::SetImageList(wxImageList* imageList)
fb46a9a6 237{
0e320a79
DW
238 m_pImageList = imageList;
239 // TODO
240}
241
cdf1e714
DW
242void wxNotebook::SetTabSize(const wxSize& sz)
243{
244 // TODO
245}
246
0e320a79
DW
247// ----------------------------------------------------------------------------
248// wxNotebook operations
249// ----------------------------------------------------------------------------
250
251// remove one page from the notebook
252bool wxNotebook::DeletePage(int nPage)
253{
cdf1e714 254 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
0e320a79
DW
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
265bool wxNotebook::RemovePage(int nPage)
266{
cdf1e714 267 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
0e320a79
DW
268
269 m_aPages.Remove(nPage);
270
271 return TRUE;
272}
273
274// remove all pages
275bool 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
290bool 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
299bool 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
fb46a9a6 313 // some page must be selected: either this one or the first one if there is
0e320a79
DW
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)
329void 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);
7e99520b 353#if wxUSE_CONSTRAINTS
0e320a79
DW
354 if ( pPage->GetAutoLayout() )
355 pPage->Layout();
7e99520b
DW
356#endif //wxUSE_CONSTRAINTS
357
0e320a79
DW
358 }
359
360 // Processing continues to next OnSize
361 event.Skip();
362}
363
364void wxNotebook::OnSelChange(wxNotebookEvent& event)
365{
366 // is it our tab control?
367 if ( event.GetEventObject() == this )
cdf1e714
DW
368 {
369 int sel = event.GetOldSelection();
370 if ( sel != -1 )
371 m_aPages[sel]->Show(FALSE);
fb46a9a6 372
cdf1e714
DW
373 sel = event.GetSelection();
374 if ( sel != -1 )
375 {
376 wxNotebookPage *pPage = m_aPages[sel];
377 pPage->Show(TRUE);
378 pPage->SetFocus();
379 }
fb46a9a6 380
cdf1e714
DW
381 m_nSelection = sel;
382 }
0e320a79
DW
383 // we want to give others a chance to process this message as well
384 event.Skip();
385}
386
387void 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
396void 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
417void wxNotebook::SetConstraintSizes(bool /* recurse */)
418{
7e99520b 419#if wxUSE_CONSTRAINTS
0e320a79
DW
420 // don't set the sizes of the pages - their correct size is not yet known
421 wxControl::SetConstraintSizes(FALSE);
7e99520b 422#endif
0e320a79
DW
423}
424
425bool wxNotebook::DoPhase(int /* nPhase */)
426{
427 return TRUE;
428}
429
cdf1e714 430bool wxNotebook::OS2OnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
0e320a79 431{
cdf1e714
DW
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*/
fb46a9a6 449// TODO: event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
cdf1e714
DW
450 event.SetOldSelection(m_nSelection);
451 event.SetEventObject(this);
452 event.SetInt(idCtrl);
453
454 bool processed = GetEventHandler()->ProcessEvent(event);
fb46a9a6 455// TODO: *result = !event.IsAllowed();
cdf1e714 456 return processed;
0e320a79
DW
457}
458
459// ----------------------------------------------------------------------------
460// wxNotebook helper functions
461// ----------------------------------------------------------------------------
462
463// hide the currently active panel and show the new one
464void wxNotebook::ChangePage(int nOldSel, int nSel)
465{
cdf1e714
DW
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;
0e320a79
DW
490 }
491
cdf1e714
DW
492 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
493 ProcessEvent(event);
0e320a79 494
cdf1e714 495 s_bInsideChangePage = FALSE;
0e320a79
DW
496}
497