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