Fixes for OS/2 wxUniv merge.
[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 void wxNotebook::SetPageSize(const wxSize& size)
252 {
253 // transform the page size into the notebook size
254 RECT rc;
255 rc.xLeft = rc.yTop = 0;
256 rc.xRight = size.x;
257 rc.yBottom = size.y;
258
259 // TabCtrl_AdjustRect(GetHwnd(), TRUE, &rc);
260
261 // and now set it
262 SetSize(rc.xRight - rc.xLeft, rc.yBottom - rc.yTop);
263 }
264
265 void wxNotebook::SetPadding(const wxSize& padding)
266 {
267 // TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
268 }
269
270 // remove one page from the notebook
271 bool wxNotebook::DeletePage(int nPage)
272 {
273 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
274
275 // TODO: delete native widget page
276
277 delete m_aPages[nPage];
278 m_aPages.Remove(nPage);
279
280 return TRUE;
281 }
282
283 // remove one page from the notebook, without deleting the window
284 bool wxNotebook::RemovePage(int nPage)
285 {
286 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
287
288 m_aPages.Remove(nPage);
289
290 return TRUE;
291 }
292
293 // remove all pages
294 bool wxNotebook::DeleteAllPages()
295 {
296 // TODO: delete native widget pages
297
298 int nPageCount = GetPageCount();
299 int nPage;
300 for ( nPage = 0; nPage < nPageCount; nPage++ )
301 delete m_aPages[nPage];
302
303 m_aPages.Clear();
304
305 return TRUE;
306 }
307
308 // add a page to the notebook
309 bool wxNotebook::AddPage(wxNotebookPage *pPage,
310 const wxString& strText,
311 bool bSelect,
312 int imageId)
313 {
314 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
315 }
316
317 // same as AddPage() but does it at given position
318 bool wxNotebook::InsertPage(int nPage,
319 wxNotebookPage *pPage,
320 const wxString& strText,
321 bool bSelect,
322 int imageId)
323 {
324 wxASSERT( pPage != NULL );
325 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
326
327 // TODO: insert native widget page
328
329 // save the pointer to the page
330 m_aPages.Insert(pPage, nPage);
331
332 // some page must be selected: either this one or the first one if there is
333 // still no selection
334 if ( bSelect )
335 m_nSelection = nPage;
336 else if ( m_nSelection == -1 )
337 m_nSelection = 0;
338
339 return TRUE;
340 }
341
342 // ----------------------------------------------------------------------------
343 // wxNotebook callbacks
344 // ----------------------------------------------------------------------------
345
346 // @@@ OnSize() is used for setting the font when it's called for the first
347 // time because doing it in ::Create() doesn't work (for unknown reasons)
348 void wxNotebook::OnSize(wxSizeEvent& event)
349 {
350 static bool s_bFirstTime = TRUE;
351 if ( s_bFirstTime ) {
352 // TODO: any first-time-size processing.
353 s_bFirstTime = FALSE;
354 }
355
356 // TODO: all this may or may not be necessary for your platform
357
358 // emulate page change (it's esp. important to do it first time because
359 // otherwise our page would stay invisible)
360 int nSel = m_nSelection;
361 m_nSelection = -1;
362 SetSelection(nSel);
363
364 // fit the notebook page to the tab control's display area
365 int w, h;
366 GetSize(&w, &h);
367
368 unsigned int nCount = m_aPages.Count();
369 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
370 wxNotebookPage *pPage = m_aPages[nPage];
371 pPage->SetSize(0, 0, w, h);
372 #if wxUSE_CONSTRAINTS
373 if ( pPage->GetAutoLayout() )
374 pPage->Layout();
375 #endif //wxUSE_CONSTRAINTS
376
377 }
378
379 // Processing continues to next OnSize
380 event.Skip();
381 }
382
383 void wxNotebook::OnSelChange(wxNotebookEvent& event)
384 {
385 // is it our tab control?
386 if ( event.GetEventObject() == this )
387 {
388 int sel = event.GetOldSelection();
389 if ( sel != -1 )
390 m_aPages[sel]->Show(FALSE);
391
392 sel = event.GetSelection();
393 if ( sel != -1 )
394 {
395 wxNotebookPage *pPage = m_aPages[sel];
396 pPage->Show(TRUE);
397 pPage->SetFocus();
398 }
399
400 m_nSelection = sel;
401 }
402 // we want to give others a chance to process this message as well
403 event.Skip();
404 }
405
406 void wxNotebook::OnSetFocus(wxFocusEvent& event)
407 {
408 // set focus to the currently selected page if any
409 if ( m_nSelection != -1 )
410 m_aPages[m_nSelection]->SetFocus();
411
412 event.Skip();
413 }
414
415 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
416 {
417 if ( event.IsWindowChange() ) {
418 // change pages
419 AdvanceSelection(event.GetDirection());
420 }
421 else {
422 // pass to the parent
423 if ( GetParent() ) {
424 event.SetCurrentFocus(this);
425 GetParent()->ProcessEvent(event);
426 }
427 }
428 }
429
430 // ----------------------------------------------------------------------------
431 // wxNotebook base class virtuals
432 // ----------------------------------------------------------------------------
433
434 // override these 2 functions to do nothing: everything is done in OnSize
435
436 void wxNotebook::SetConstraintSizes(bool /* recurse */)
437 {
438 #if wxUSE_CONSTRAINTS
439 // don't set the sizes of the pages - their correct size is not yet known
440 wxControl::SetConstraintSizes(FALSE);
441 #endif
442 }
443
444 bool wxNotebook::DoPhase(int /* nPhase */)
445 {
446 return TRUE;
447 }
448
449 bool wxNotebook::OS2OnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
450 {
451 wxNotebookEvent event(wxEVT_NULL, m_windowId);
452 //TODO:
453 /*
454 NMHDR* hdr = (NMHDR *)lParam;
455 switch ( hdr->code ) {
456 case TCN_SELCHANGE:
457 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
458 break;
459
460 case TCN_SELCHANGING:
461 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING);
462 break;
463
464 default:
465 return wxControl::MSWOnNotify(idCtrl, lParam, result);
466 }
467 */
468 // TODO: event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
469 event.SetOldSelection(m_nSelection);
470 event.SetEventObject(this);
471 event.SetInt(idCtrl);
472
473 bool processed = GetEventHandler()->ProcessEvent(event);
474 // TODO: *result = !event.IsAllowed();
475 return processed;
476 }
477
478 // ----------------------------------------------------------------------------
479 // wxNotebook helper functions
480 // ----------------------------------------------------------------------------
481
482 // hide the currently active panel and show the new one
483 void wxNotebook::ChangePage(int nOldSel, int nSel)
484 {
485 // MT-FIXME should use a real semaphore
486 static bool s_bInsideChangePage = FALSE;
487
488 // when we call ProcessEvent(), our own OnSelChange() is called which calls
489 // this function - break the infinite loop
490 if ( s_bInsideChangePage )
491 return;
492
493 // it's not an error (the message may be generated by the tab control itself)
494 // and it may happen - just do nothing
495 if ( nSel == nOldSel )
496 return;
497
498 s_bInsideChangePage = TRUE;
499
500 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId);
501 event.SetSelection(nSel);
502 event.SetOldSelection(nOldSel);
503 event.SetEventObject(this);
504 if ( ProcessEvent(event) && !event.IsAllowed() )
505 {
506 // program doesn't allow the page change
507 s_bInsideChangePage = FALSE;
508 return;
509 }
510
511 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
512 ProcessEvent(event);
513
514 s_bInsideChangePage = FALSE;
515 }
516