From a0b48f2651beaccb480cc0a6e5ba8e8b3cfa0346 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Fri, 13 Nov 2009 18:39:38 +0000 Subject: [PATCH] adding toolbar implementation for iphone git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62633 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/toolbar.h | 13 +- src/osx/iphone/toolbar.mm | 411 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 422 insertions(+), 2 deletions(-) create mode 100644 src/osx/iphone/toolbar.mm diff --git a/include/wx/osx/toolbar.h b/include/wx/osx/toolbar.h index 6dd02028cb..46d94b5edc 100644 --- a/include/wx/osx/toolbar.h +++ b/include/wx/osx/toolbar.h @@ -48,8 +48,10 @@ class WXDLLIMPEXP_CORE wxToolBar: public wxToolBarBase // override/implement base class virtuals virtual wxToolBarToolBase *FindToolForPosition(wxCoord x, wxCoord y) const; +#ifndef __WXOSX_IPHONE__ virtual bool Show(bool show = true); virtual bool IsShown() const; +#endif virtual bool Realize(); virtual void SetToolBitmapSize(const wxSize& size); @@ -60,13 +62,15 @@ class WXDLLIMPEXP_CORE wxToolBar: public wxToolBarBase virtual void SetToolNormalBitmap(int id, const wxBitmap& bitmap); virtual void SetToolDisabledBitmap(int id, const wxBitmap& bitmap); +#ifndef __WXOSX_IPHONE__ // Add all the buttons virtual wxString MacGetToolTipString( wxPoint &where ) ; void OnPaint(wxPaintEvent& event) ; void OnMouse(wxMouseEvent& event) ; virtual void MacSuperChangedPosition() ; - +#endif + #if wxOSX_USE_NATIVE_TOOLBAR bool MacInstallNativeToolbar(bool usesNative); bool MacWantsNativeToolbar(); @@ -76,8 +80,10 @@ protected: // common part of all ctors void Init(); - virtual void DoGetSize(int *width, int *height) const; +#ifndef __WXOSX_IPHONE__ + virtual void DoGetSize(int *width, int *height) const; virtual wxSize DoGetBestSize() const; +#endif virtual bool DoInsertTool(size_t pos, wxToolBarToolBase *tool); virtual bool DoDeleteTool(size_t pos, wxToolBarToolBase *tool); @@ -101,6 +107,9 @@ protected: bool m_macUsesNativeToolbar ; void* m_macToolbar ; #endif +#ifdef __WXOSX_IPHONE__ + WX_UIView m_macToolbar; +#endif }; #endif // wxUSE_TOOLBAR diff --git a/src/osx/iphone/toolbar.mm b/src/osx/iphone/toolbar.mm new file mode 100644 index 0000000000..7ec0bac59c --- /dev/null +++ b/src/osx/iphone/toolbar.mm @@ -0,0 +1,411 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: src/osx/carbon/toolbar.cpp +// Purpose: wxToolBar +// Author: Stefan Csomor +// Modified by: +// Created: 04/01/98 +// RCS-ID: $Id: toolbar.cpp 54954 2008-08-03 11:27:03Z VZ $ +// Copyright: (c) Stefan Csomor +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#include "wx/wxprec.h" + +#if wxUSE_TOOLBAR + +#include "wx/toolbar.h" + +#ifndef WX_PRECOMP +#include "wx/wx.h" +#endif + +#include "wx/app.h" +#include "wx/osx/private.h" +#include "wx/geometry.h" +#include "wx/sysopt.h" + +#pragma mark - +#pragma mark Tool Implementation + +BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) +END_EVENT_TABLE() + +// ---------------------------------------------------------------------------- +// private classes +// ---------------------------------------------------------------------------- + +class wxToolBarTool; + +@interface wxUIToolbar : UIToolbar +{ + NSMutableArray* mutableBarItems; +} + +- (void)clickedAction:(id)sender; + +- (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos; + +- (void)removeTool:(size_t) pos; + +- (id)init; + +@end + + +class wxToolBarTool : public wxToolBarToolBase +{ +public: + wxToolBarTool( + wxToolBar *tbar, + int id, + const wxString& label, + const wxBitmap& bmpNormal, + const wxBitmap& bmpDisabled, + wxItemKind kind, + wxObject *clientData, + const wxString& shortHelp, + const wxString& longHelp ); + + wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label); + + virtual ~wxToolBarTool(); + + void Action() + { + wxToolBar *tbar = (wxToolBar*) GetToolBar(); + if (CanBeToggled()) + { + bool shouldToggle; + + shouldToggle = !IsToggled(); + tbar->ToggleTool( GetId(), shouldToggle ); + } + + tbar->OnLeftClick( GetId(), IsToggled() ); + } + + UIBarButtonItem* GetUIBarButtonItem() const {return m_toolbarItem;} +private: + + void Init() + { + m_toolbarItem = NULL; + m_index = -1; + } + + UIBarButtonItem* m_toolbarItem; + // position in its toolbar, -1 means not inserted + CFIndex m_index; +}; + +WX_DECLARE_HASH_MAP(WX_NSObject, wxToolBarTool*, wxPointerHash, wxPointerEqual, ToolBarToolMap); +static ToolBarToolMap wxToolBarToolList; + +wxToolBarTool::wxToolBarTool( + wxToolBar *tbar, + int id, + const wxString& label, + const wxBitmap& bmpNormal, + const wxBitmap& bmpDisabled, + wxItemKind kind, + wxObject *clientData, + const wxString& shortHelp, + const wxString& longHelp ) +: +wxToolBarToolBase( + tbar, id, label, bmpNormal, bmpDisabled, kind, + clientData, shortHelp, longHelp ) +{ + Init(); + UIBarButtonItem* bui = [UIBarButtonItem alloc]; + UIBarButtonItemStyle style = UIBarButtonItemStylePlain; + wxUIToolbar* toolbar = (wxUIToolbar*) tbar->GetHandle(); + + if ( bmpNormal.Ok() ) + { + [bui initWithImage:bmpNormal.GetUIImage() style:UIBarButtonItemStylePlain target:toolbar + action:@selector(clickedAction:)]; + } + else + { + if ( id == wxID_OK ) + style = UIBarButtonItemStyleDone; + else + style = UIBarButtonItemStyleBordered; + + [bui initWithTitle:wxCFStringRef(label).AsNSString() style:style target:toolbar + action:@selector(clickedAction:)]; + } + + m_toolbarItem = bui; + wxToolBarToolList[bui] = this; +} + +wxToolBarTool::wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) +: wxToolBarToolBase(tbar, control, label) +{ + Init(); + UIBarButtonItem* bui = [UIBarButtonItem alloc]; + + [bui initWithCustomView:control->GetHandle() ]; + + m_toolbarItem = bui; + wxToolBarToolList[bui] = this; +} + +wxToolBarTool::~wxToolBarTool() +{ + bool found = true ; + while ( found ) + { + found = false ; + ToolBarToolMap::iterator it; + for ( it = wxToolBarToolList.begin(); it != wxToolBarToolList.end(); ++it ) + { + if ( it->second == this ) + { + wxToolBarToolList.erase(it); + found = true ; + break; + } + } + } +} + + +wxToolBarToolBase *wxToolBar::CreateTool( + int id, + const wxString& label, + const wxBitmap& bmpNormal, + const wxBitmap& bmpDisabled, + wxItemKind kind, + wxObject *clientData, + const wxString& shortHelp, + const wxString& longHelp ) +{ + return new wxToolBarTool( + this, id, label, bmpNormal, bmpDisabled, kind, + clientData, shortHelp, longHelp ); +} + +wxToolBarToolBase * +wxToolBar::CreateTool(wxControl *control, const wxString& label) +{ + return new wxToolBarTool(this, control, label); +} + +void wxToolBar::Init() +{ + m_maxWidth = -1; + m_maxHeight = -1; + + m_macToolbar = NULL; +} + +// also for the toolbar we have the dual implementation: +// only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar + +bool wxToolBar::Create( + wxWindow *parent, + wxWindowID id, + const wxPoint& pos, + const wxSize& size, + long style, + const wxString& name ) +{ + m_macIsUserPane = false ; + + if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) ) + return false; + + FixupStyle(); + + CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ; + + wxUIToolbar* toolbar = [[wxUIToolbar alloc] init]; + [toolbar sizeToFit]; + + switch ( [[UIApplication sharedApplication] statusBarStyle] ) + { + case UIStatusBarStyleBlackOpaque: + toolbar.barStyle = UIBarStyleBlack; + break; + case UIStatusBarStyleBlackTranslucent: + toolbar.barStyle = UIBarStyleBlack; + toolbar.translucent = YES; + break; + default: + toolbar.barStyle = UIBarStyleDefault; + break; + } + m_macToolbar = toolbar; + + m_peer = new wxWidgetIPhoneImpl( this, toolbar ); + MacPostControlCreate(pos, size) ; + NSLog(@"toolbar was created %@",toolbar); +} + +wxToolBar::~wxToolBar() +{ + m_macToolbar = NULL; +} + +bool wxToolBar::Realize() +{ + if ( !wxToolBarBase::Realize() ) + return false; + + + return true; +} + +void wxToolBar::SetToolBitmapSize(const wxSize& size) +{ + m_defaultWidth = size.x; + m_defaultHeight = size.y; +} + +// The button size is bigger than the bitmap size +wxSize wxToolBar::GetToolSize() const +{ + return wxSize(m_defaultWidth, m_defaultHeight); +} + +void wxToolBar::SetRows(int nRows) +{ + // avoid resizing the frame uselessly + if ( nRows != m_maxRows ) + m_maxRows = nRows; +} + +void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) +{ + wxToolBarTool* tool = static_cast(FindById(id)); + if ( tool ) + { + wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); + + tool->SetNormalBitmap(bitmap); + } +} + +void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) +{ + wxToolBarTool* tool = static_cast(FindById(id)); + if ( tool ) + { + wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); + + tool->SetDisabledBitmap(bitmap); + + // TODO: what to do for this one? + } +} + +wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const +{ + return NULL; +} + +void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable) +{ + /* + if ( t != NULL ) + ((wxToolBarTool*)t)->DoEnable( enable ); + */ +} + +void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) +{ + /* + wxToolBarTool *tool = (wxToolBarTool *)t; + if ( ( tool != NULL ) && tool->IsButton() ) + tool->UpdateToggleImage( toggle ); + */ +} + +bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase) +{ + wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase ); + if (tool == NULL) + return false; + + wxSize toolSize = GetToolSize(); + + switch (tool->GetStyle()) + { + case wxTOOL_STYLE_SEPARATOR: + break; + + case wxTOOL_STYLE_BUTTON: + break; + + case wxTOOL_STYLE_CONTROL: + // right now there's nothing to do here + break; + + default: + break; + } + + [(wxUIToolbar*)m_macToolbar insertTool:tool->GetUIBarButtonItem() atIndex:pos]; + InvalidateBestSize(); + + return true; + +} + +void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) +{ + wxFAIL_MSG( wxT("not implemented") ); +} + +bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolbase) +{ + wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase ); + + [(wxUIToolbar*)m_macToolbar removeTool:pos]; + + return true; +} + +void wxToolBar::SetWindowStyleFlag( long style ) +{ + wxToolBarBase::SetWindowStyleFlag( style ); + +} + +@implementation wxUIToolbar + +- (id)init +{ + if (!(self = [super init])) + return nil; + + mutableBarItems = [NSMutableArray arrayWithCapacity:5]; + return self; +} + +- (void)clickedAction:(id)sender +{ + ToolBarToolMap::iterator node = wxToolBarToolList.find(sender); + + if ( node != wxToolBarToolList.end() ) + node->second->Action(); +} + +- (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos +{ + [mutableBarItems insertObject:item atIndex:pos]; + [super setItems:mutableBarItems]; +} + +- (void)removeTool:(size_t) pos +{ + [mutableBarItems removeObjectAtIndex:pos]; + [super setItems:mutableBarItems]; +} + +@end + +#endif // wxUSE_TOOLBAR -- 2.45.2