]> git.saurik.com Git - wxWidgets.git/blame - src/os2/notebook.cpp
add automatic rescaling to wxArtProvider
[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
0367c1c0
DW
251void 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
265void wxNotebook::SetPadding(const wxSize& padding)
266{
267// TabCtrl_SetPadding(GetHwnd(), padding.x, padding.y);
268}
269
0e320a79
DW
270// remove one page from the notebook
271bool wxNotebook::DeletePage(int nPage)
272{
cdf1e714 273 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
0e320a79
DW
274
275 // TODO: delete native widget page
276
277 delete m_aPages[nPage];
893758d5 278 m_aPages.RemoveAt(nPage);
0e320a79
DW
279
280 return TRUE;
281}
282
283// remove one page from the notebook, without deleting the window
284bool wxNotebook::RemovePage(int nPage)
285{
cdf1e714 286 wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, wxT("notebook page out of range") );
0e320a79 287
893758d5 288 m_aPages.RemoveAt(nPage);
0e320a79
DW
289
290 return TRUE;
291}
292
293// remove all pages
294bool 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
309bool 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
318bool 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
fb46a9a6 332 // some page must be selected: either this one or the first one if there is
0e320a79
DW
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)
348void 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);
7e99520b 372#if wxUSE_CONSTRAINTS
0e320a79
DW
373 if ( pPage->GetAutoLayout() )
374 pPage->Layout();
7e99520b
DW
375#endif //wxUSE_CONSTRAINTS
376
0e320a79
DW
377 }
378
379 // Processing continues to next OnSize
380 event.Skip();
381}
382
383void wxNotebook::OnSelChange(wxNotebookEvent& event)
384{
385 // is it our tab control?
386 if ( event.GetEventObject() == this )
cdf1e714
DW
387 {
388 int sel = event.GetOldSelection();
389 if ( sel != -1 )
390 m_aPages[sel]->Show(FALSE);
fb46a9a6 391
cdf1e714
DW
392 sel = event.GetSelection();
393 if ( sel != -1 )
394 {
395 wxNotebookPage *pPage = m_aPages[sel];
396 pPage->Show(TRUE);
397 pPage->SetFocus();
398 }
fb46a9a6 399
cdf1e714
DW
400 m_nSelection = sel;
401 }
0e320a79
DW
402 // we want to give others a chance to process this message as well
403 event.Skip();
404}
405
406void 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
415void 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
436void wxNotebook::SetConstraintSizes(bool /* recurse */)
437{
7e99520b 438#if wxUSE_CONSTRAINTS
0e320a79
DW
439 // don't set the sizes of the pages - their correct size is not yet known
440 wxControl::SetConstraintSizes(FALSE);
7e99520b 441#endif
0e320a79
DW
442}
443
444bool wxNotebook::DoPhase(int /* nPhase */)
445{
446 return TRUE;
447}
448
cdf1e714 449bool wxNotebook::OS2OnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM* result)
0e320a79 450{
cdf1e714
DW
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*/
fb46a9a6 468// TODO: event.SetSelection(TabCtrl_GetCurSel(m_hwnd));
cdf1e714
DW
469 event.SetOldSelection(m_nSelection);
470 event.SetEventObject(this);
471 event.SetInt(idCtrl);
472
473 bool processed = GetEventHandler()->ProcessEvent(event);
fb46a9a6 474// TODO: *result = !event.IsAllowed();
cdf1e714 475 return processed;
0e320a79
DW
476}
477
478// ----------------------------------------------------------------------------
479// wxNotebook helper functions
480// ----------------------------------------------------------------------------
481
482// hide the currently active panel and show the new one
483void wxNotebook::ChangePage(int nOldSel, int nSel)
484{
cdf1e714
DW
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;
0e320a79
DW
509 }
510
cdf1e714
DW
511 event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED);
512 ProcessEvent(event);
0e320a79 513
cdf1e714 514 s_bInsideChangePage = FALSE;
0e320a79
DW
515}
516