From: David Elliott Date: Fri, 9 Apr 2004 20:19:06 +0000 (+0000) Subject: Added files for wxNotebook and wxCocoaNSTabView X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/7848088476db284a94f19eeb24411b45d676ac46 Added files for wxNotebook and wxCocoaNSTabView git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26679 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/cocoa/NSTabView.h b/include/wx/cocoa/NSTabView.h new file mode 100644 index 0000000000..52bad3da13 --- /dev/null +++ b/include/wx/cocoa/NSTabView.h @@ -0,0 +1,34 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: wx/cocoa/NSTabView.h +// Purpose: wxCocoaNSTabView class +// Author: David Elliott +// Modified by: +// Created: 2004/04/08 +// RCS-ID: $Id$ +// Copyright: (c) 2004 David Elliott +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_COCOA_NSTABVIEW_H__ +#define _WX_COCOA_NSTABVIEW_H__ + +#include "wx/hashmap.h" +#include "wx/cocoa/ObjcAssociate.h" +#include "wx/cocoa/ObjcRef.h" + +DECLARE_WXCOCOA_OBJC_CLASS(NSTabView); +DECLARE_WXCOCOA_OBJC_CLASS(NSTabViewItem); +WX_DECLARE_OBJC_HASHMAP(NSTabView); +class wxCocoaNSTabView +{ + WX_DECLARE_OBJC_INTERFACE_HASHMAP(NSTabView) +public: + void AssociateNSTabView(WX_NSTabView cocoaNSTabView); + void DisassociateNSTabView(WX_NSTabView ocoaNSTabView); + virtual void CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabviewItem) = 0; + virtual bool CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabviewItem) = 0; +protected: + static wxObjcAutoRefFromAlloc sm_cocoaDelegate; +}; + +#endif // _WX_COCOA_NSTABVIEW_H__ diff --git a/include/wx/cocoa/notebook.h b/include/wx/cocoa/notebook.h new file mode 100644 index 0000000000..fb2a8153e1 --- /dev/null +++ b/include/wx/cocoa/notebook.h @@ -0,0 +1,103 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/cocoa/notebook.h +// Purpose: wxNotebook class +// Author: David Elliott +// Modified by: +// Created: 2004/04/08 +// RCS-ID: $Id$ +// Copyright: (c) 2004 David Elliott +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_COCOA_NOTEBOOK_H__ +#define _WX_COCOA_NOTEBOOK_H__ + +#include "wx/cocoa/NSTabView.h" + +// ======================================================================== +// wxNotebook +// ======================================================================== +class WXDLLEXPORT wxNotebook: public wxNotebookBase, protected wxCocoaNSTabView +{ + DECLARE_DYNAMIC_CLASS(wxNotebook) + DECLARE_EVENT_TABLE() + WX_DECLARE_COCOA_OWNER(NSTabView,NSView,NSView) +// ------------------------------------------------------------------------ +// initialization +// ------------------------------------------------------------------------ +public: + wxNotebook() { } + wxNotebook(wxWindow *parent, wxWindowID winid, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize, + long style = 0, + const wxString& name = wxNOTEBOOK_NAME) + { + Create(parent, winid, pos, size, style, name); + } + + bool Create(wxWindow *parent, wxWindowID winid, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize, + long style = 0, + const wxString& name = wxNOTEBOOK_NAME); + virtual ~wxNotebook(); + +// ------------------------------------------------------------------------ +// Cocoa callbacks +// ------------------------------------------------------------------------ +protected: + // Notebooks cannot be enabled/disabled + virtual void CocoaSetEnabled(bool enable) { } + virtual void CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabviewItem); + virtual bool CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabviewItem); +// ------------------------------------------------------------------------ +// Implementation +// ------------------------------------------------------------------------ +public: + // set the currently selected page, return the index of the previously + // selected one (or -1 on error) + // NB: this function will _not_ generate wxEVT_NOTEBOOK_PAGE_xxx events + int SetSelection(size_t nPage); + // get the currently selected page + int GetSelection() const; + + // set/get the title of a page + bool SetPageText(size_t nPage, const wxString& strText); + wxString GetPageText(size_t nPage) const; + + // sets/returns item's image index in the current image list + int GetPageImage(size_t nPage) const; + bool SetPageImage(size_t nPage, int nImage); + + // set the size (the same for all pages) + void SetPageSize(const wxSize& size); + + // SetPadding and SetTabSize aren't possible to implement + void SetPadding(const wxSize& padding); + void SetTabSize(const wxSize& sz); + + //----------------------- + // adding/removing pages + + // remove one page from the notebook, without deleting + virtual wxNotebookPage *DoRemovePage(size_t nPage); + + // remove one page from the notebook + bool DeletePage(size_t nPage); + // remove all pages + bool DeleteAllPages(); + + // adds a new page to the notebook (it will be deleted ny the notebook, + // don't delete it yourself). If bSelect, this page becomes active. + // the same as AddPage(), but adds it at the specified position + bool InsertPage( size_t position, + wxNotebookPage *win, + const wxString& strText, + bool bSelect = FALSE, + int imageId = -1 ); + +protected: +}; + +#endif //ndef _WX_COCOA_NOTEBOOK_H__ diff --git a/src/cocoa/NSTabView.mm b/src/cocoa/NSTabView.mm new file mode 100644 index 0000000000..b3255cd51b --- /dev/null +++ b/src/cocoa/NSTabView.mm @@ -0,0 +1,73 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: cocoa/NSTabView.mm +// Purpose: wxCocoaNSTabView +// Author: David Elliott +// Modified by: +// Created: 2004/04/08 +// RCS-ID: $Id$ +// Copyright: (c) 2004 David Elliott +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#include "wx/wxprec.h" +#ifndef WX_PRECOMP +#endif // WX_PRECOMP + +#include "wx/cocoa/NSTabView.h" + +#include + +// ============================================================================ +// @class wxNSTabViewDelegate +// ============================================================================ +@interface wxNSTabViewDelegate : NSObject +{ +} + +- (void)tabView:(NSTabView*)tabView didSelectTabViewItem:(NSTabViewItem*)tabViewItem; +- (BOOL)tabView:(NSTabView*)tabView shouldSelectTabViewItem:(NSTabViewItem*)tabViewItem; +@end // interface wxNSTabViewDelegate : NSObject + +@implementation wxNSTabViewDelegate : NSObject +- (void)tabView:(NSTabView*)tabView didSelectTabViewItem:(NSTabViewItem*)tabViewItem +{ + wxCocoaNSTabView *notebook = wxCocoaNSTabView::GetFromCocoa(tabView); + wxCHECK_RET(notebook, wxT("This delegate is for use only with wxCocoa NSTabViews")); + notebook->CocoaDelegate_tabView_didSelectTabViewItem(tabViewItem); + +} + +- (BOOL)tabView:(NSTabView*)tabView shouldSelectTabViewItem:(NSTabViewItem*)tabViewItem +{ + wxCocoaNSTabView *notebook = wxCocoaNSTabView::GetFromCocoa(tabView); + wxCHECK_MSG(notebook, true, wxT("This delegate is for use only with wxCocoa NSTabViews")); + return notebook->CocoaDelegate_tabView_shouldSelectTabViewItem(tabViewItem); +} + +@end // implementation wxNSTabViewDelegate : NSObject + +// ============================================================================ +// class wxCocoaNSTabView +// ============================================================================ +WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSTabView) + +wxObjcAutoRefFromAlloc wxCocoaNSTabView::sm_cocoaDelegate = [[wxNSTabViewDelegate alloc] init]; + +void wxCocoaNSTabView::AssociateNSTabView(WX_NSTabView cocoaNSTabView) +{ + if(cocoaNSTabView) + { + sm_cocoaHash.insert(wxCocoaNSTabViewHash::value_type(cocoaNSTabView,this)); + [cocoaNSTabView setDelegate: sm_cocoaDelegate]; + } +} + +void wxCocoaNSTabView::DisassociateNSTabView(WX_NSTabView cocoaNSTabView) +{ + if(cocoaNSTabView) + { + [cocoaNSTabView setDelegate: nil]; + sm_cocoaHash.erase(cocoaNSTabView); + } +} + diff --git a/src/cocoa/notebook.mm b/src/cocoa/notebook.mm new file mode 100644 index 0000000000..3518e49a50 --- /dev/null +++ b/src/cocoa/notebook.mm @@ -0,0 +1,290 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: cocoa/notebook.mm +// Purpose: wxNotebook +// Author: David Elliott +// Modified by: +// Created: 2004/04/08 +// RCS-ID: $Id$ +// Copyright: (c) 2004 David Elliott +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#include "wx/wxprec.h" +#ifndef WX_PRECOMP + #include "wx/app.h" + #include "wx/notebook.h" +#endif //WX_PRECOMP +#include "wx/imaglist.h" + +#include "wx/cocoa/autorelease.h" +#include "wx/cocoa/string.h" + +#import +#import +#import + +// testing: +#import +#import + +// ======================================================================== +// WXCTabViewImageItem +// ======================================================================== +@interface WXCTabViewImageItem : NSTabViewItem +{ + NSImage *m_image; +} + +- (id)init; +- (id)initWithIdentifier: (id)identifier; +- (void)dealloc; + +- (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel; +- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect; + +- (NSImage*)image; +- (void)setImage:(NSImage*)image; +@end // interface WXCTabViewImageItem : NSTabViewItem + +@implementation WXCTabViewImageItem : NSTabViewItem +- (id)init +{ + return [self initWithIdentifier:nil]; +} + +- (id)initWithIdentifier: (id)identifier; +{ + m_image = nil; + return [super initWithIdentifier:identifier]; +} + +- (void)dealloc +{ + [m_image release]; +} + +- (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel +{ + NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel]; + if(!m_image) + return labelSize; + NSSize imageSize = [m_image size]; + // scale image size + if(imageSize.height > labelSize.height) + { + imageSize.width *= labelSize.height/imageSize.height; + imageSize.height *= labelSize.height/imageSize.height; + [m_image setScalesWhenResized:YES]; + [m_image setSize: imageSize]; + } + labelSize.width += imageSize.width; + return labelSize; +} + +- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect +{ + if(m_image) + { + NSSize imageSize = [m_image size]; + [m_image compositeToPoint:NSMakePoint(tabRect.origin.x, + tabRect.origin.y+imageSize.height) + operation:NSCompositeSourceOver]; + tabRect.size.width -= imageSize.width; + tabRect.origin.x += imageSize.width; + } + [super drawLabel:shouldTruncateLabel inRect:tabRect]; +} + +- (NSImage*)image +{ + return m_image; +} + +- (void)setImage:(NSImage*)image +{ + [image retain]; + [m_image release]; + m_image = image; + if(!m_image) + return; + [[NSPasteboard generalPasteboard] + declareTypes:[NSArray arrayWithObject:NSTIFFPboardType] + owner:nil]; + [[NSPasteboard generalPasteboard] + setData:[m_image TIFFRepresentation] + forType:NSTIFFPboardType]; +} + +@end // implementation WXCTabViewImageItem : NSTabViewItem + +// ======================================================================== +// wxNotebookEvent +// ======================================================================== +DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) +DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) +IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent) + +// ======================================================================== +// wxNotebook +// ======================================================================== +IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl) +BEGIN_EVENT_TABLE(wxNotebook, wxNotebookBase) +END_EVENT_TABLE() +WX_IMPLEMENT_COCOA_OWNER(wxNotebook,NSTabView,NSView,NSView) + +bool wxNotebook::Create(wxWindow *parent, wxWindowID winid, + const wxPoint& pos, + const wxSize& size, + long style, + const wxString& name) +{ + wxAutoNSAutoreleasePool pool; + if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name)) + return false; + m_cocoaNSView = NULL; + SetNSTabView([[NSTabView alloc] initWithFrame:MakeDefaultNSRect(size)]); + if(m_parent) + m_parent->CocoaAddChild(this); + SetInitialFrameRect(pos,size); + + return true; +} + +wxNotebook::~wxNotebook() +{ +} + +void wxNotebook::SetPadding(const wxSize& padding) +{ // Can't do +} + +void wxNotebook::SetTabSize(const wxSize& sz) +{ // Can't do +} + +void wxNotebook::SetPageSize(const wxSize& size) +{ +} + + +wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage) +{ + wxNotebookPage *page = wxNotebookBase::DoRemovePage(nPage); + if(!page) + return NULL; + NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage]; + wxASSERT(tvitem); + [tvitem retain]; + [GetNSTabView() removeTabViewItem:tvitem]; + // Remove the child window as a notebook page + wxASSERT([tvitem view] == page->GetNSViewForSuperview()); + [tvitem setView:nil]; + [tvitem release]; + // Make it back into a normal child window + [m_cocoaNSView addSubview: page->GetNSViewForSuperview()]; + + return page; +} + +bool wxNotebook::DeletePage(size_t nPage) +{ + return wxNotebookBase::DeletePage(nPage); +} + +bool wxNotebook::InsertPage( size_t pos, + wxNotebookPage *page, const wxString& title, + bool bSelect, int imageId) +{ + wxAutoNSAutoreleasePool pool; + m_pages.Insert(page,pos); + NSTabViewItem *tvitem = [[WXCTabViewImageItem alloc] initWithIdentifier:nil]; + [tvitem setLabel: wxNSStringWithWxString(title)]; + const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmap(imageId):NULL; + if(bmp) + [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)]; + + NSView *pageNSView = page->GetNSViewForSuperview(); + // Remove it as a normal child + wxASSERT(m_cocoaNSView == [pageNSView superview]); + [pageNSView removeFromSuperview]; + // And make it a notebook page + [tvitem setView: pageNSView]; + + [GetNSTabView() insertTabViewItem:tvitem atIndex:pos]; + [tvitem release]; + + return true; +} + +bool wxNotebook::DeleteAllPages() +{ + while(!m_pages.IsEmpty()) + DeletePage(0); + return true; +} + + +bool wxNotebook::SetPageText(size_t nPage, const wxString& title) +{ + NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage]; + if(!tvitem) + return false; + [tvitem setLabel: wxNSStringWithWxString(title)]; + return true; +} + +wxString wxNotebook::GetPageText(size_t nPage) const +{ + return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]); +} + + +int wxNotebook::GetPageImage(size_t nPage) const +{ + // To do this we'd need to keep track of this, which we don't! + return -1; +} + +bool wxNotebook::SetPageImage(size_t nPage, int nImage) +{ + const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmap(nImage):NULL; + if(!bmp) + return false; + NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage]; + if(!tvitem) + return false; + [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)]; + return true; +} + + +int wxNotebook::SetSelection(size_t nPage) +{ + [GetNSTabView() selectTabViewItemAtIndex:nPage]; + return GetSelection(); +} + +int wxNotebook::GetSelection() const +{ + NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem]; + if(!selectedItem) + return -1; + return [GetNSTabView() indexOfTabViewItem:selectedItem]; +} + +void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem) +{ + // FIXME: oldSel probably == newSel + wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(), + [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection()); + event.SetEventObject(this); +} + +bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem) +{ + wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(), + [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection()); + event.SetEventObject(this); + return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed(); +} +