ComboBox for cocoa

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32109 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Ryan Norton
2005-02-17 04:45:13 +00:00
parent c605b871f0
commit 8f248607e5
2 changed files with 253 additions and 39 deletions

View File

@@ -1,9 +1,9 @@
/////////////////////////////////////////////////////////////////////////////
// Name: wx/cocoa/combobox.h
// Purpose: wxComboBox class
// Author: David Elliott
// Author: Ryan Norton
// Modified by:
// Created: 2003/07/14
// Created: 2005/02/16
// RCS-ID: $Id$
// Copyright: (c) 2003 David Elliott
// Licence: wxWindows licence
@@ -12,16 +12,36 @@
#ifndef __WX_COCOA_COMBOBOX_H__
#define __WX_COCOA_COMBOBOX_H__
//#include "wx/cocoa/NSTableView.h"
//Begin NSComboBox.h
#include "wx/hashmap.h"
#include "wx/cocoa/ObjcAssociate.h"
DECLARE_WXCOCOA_OBJC_CLASS(NSComboBox);
WX_DECLARE_OBJC_HASHMAP(NSComboBox);
class wxCocoaNSComboBox
{
WX_DECLARE_OBJC_INTERFACE_HASHMAP(NSComboBox)
public:
void AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox);
void DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox);
virtual void doWxEvent(int nEvent) = 0;
};
//begin combobox.h
#include "wx/dynarray.h"
// ========================================================================
// wxComboBox
// ========================================================================
class WXDLLEXPORT wxComboBox: public wxTextCtrl, public wxComboBoxBase //, protected wxCocoaNSTableView
class WXDLLEXPORT wxComboBox : public wxTextCtrl, public wxComboBoxBase, protected wxCocoaNSComboBox
{
DECLARE_DYNAMIC_CLASS(wxComboBox)
DECLARE_EVENT_TABLE()
// WX_DECLARE_COCOA_OWNER(NSComboBox,NSTextField,NSView)
WX_DECLARE_COCOA_OWNER(NSComboBox,NSTextField,NSView)
// ------------------------------------------------------------------------
// initialization
// ------------------------------------------------------------------------
@@ -73,6 +93,9 @@ public:
// Cocoa callbacks
// ------------------------------------------------------------------------
protected:
wxArrayPtrVoid m_Datas;
virtual void doWxEvent(int nEvent);
// ------------------------------------------------------------------------
// Implementation
// ------------------------------------------------------------------------

View File

@@ -1,9 +1,9 @@
/////////////////////////////////////////////////////////////////////////////
// Name: cocoa/combobox.mm
// Purpose: wxComboBox
// Author: David Elliott
// Author: Ryan Norton
// Modified by:
// Created: 2003/07/14
// Created: 2005/02/16
// RCS-ID: $Id$
// Copyright: (c) 2003 David Elliott
// Licence: wxWidgets licence
@@ -11,8 +11,164 @@
#include "wx/wxprec.h"
//
// Impl notes:
// There is no custom data source because doing so unneccesaraly sacrifices
// some native autocompletion behavior (we would have to make our own -
// the SimpleComboBox sample does so in the developer folder that
// comes with OSX). One reason you might want this would be to have
// only one array or be able to display numbers by returned an NSNumber
// from the methods.
//
// One problem though is that wxCB_SORT isn't implemented...
//
// Also, not sure if it is correctly getting text field events since
// I'm using SetNSComboBox instead of SetNSTextField
//
// doWxEvent is really hackish... but since there's only one event...
//
// Ideas for future improvement - other notes:
// Combox w/o wxCB_DROPDOWN doesn't seem to be implementable
//wxCB_READONLY Same as wxCB_DROPDOWN but only the strings specified as the combobox choices can be selected, it is impossible to select (even from a program) a string which is not in the choices list.
//wxCB_SORT is possible with data source
//
// setIntercellSpacing:/setItemHeight: to autoadjust to number of inserted items?
//
/*
//example of autocompletion from SimpleComboBox Example
// ==========================================================
// Combo box data source methods
// ==========================================================
- (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
return [genres count];
}
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index {
return [genres objectAtIndex:index];
}
- (unsigned int)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
return [genres indexOfObject: string];
}
- (NSString *) firstGenreMatchingPrefix:(NSString *)prefix {
NSString *string = nil;
NSString *lowercasePrefix = [prefix lowercaseString];
NSEnumerator *stringEnum = [genres objectEnumerator];
while ((string = [stringEnum nextObject])) {
if ([[string lowercaseString] hasPrefix: lowercasePrefix]) return string;
}
return nil;
}
- (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)inputString {
// This method is received after each character typed by the user, because we have checked the "completes" flag for genreComboBox in IB.
// Given the inputString the user has typed, see if we can find a genre with the prefix, and return it as the suggested complete string.
NSString *candidate = [self firstGenreMatchingPrefix: inputString];
return (candidate ? candidate : inputString);
}
*/
#if wxUSE_COMBOBOX
/////////////////////////////////////////////////////////////////////////////
// Name: cocoa/NSComboBox.mm
// Purpose: wxCocoaNSComboBox
// Author: Ryan Norton
// Modified by:
// Created: 2005/02/16
// RCS-ID: $Id:
// Copyright: (c) 2003 David Elliott
// Licence: wxWidgets licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/window.h"
#endif // WX_PRECOMP
#include "wx/cocoa/ObjcPose.h"
#include "wx/combobox.h"
#import <AppKit/NSComboBox.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSString.h>
// ----------------------------------------------------------------------------
// globals
// ----------------------------------------------------------------------------
WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
{
if(cocoaNSComboBox)
{
sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this));
[[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
[[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
[[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
[[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
}
}
void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
{
if(cocoaNSComboBox)
{
sm_cocoaHash.erase(cocoaNSComboBox);
[[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
[[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
[[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
[[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
}
}
// ============================================================================
// @class wxPoserNSComboBox
// ============================================================================
@interface wxPoserNSComboBox : NSComboBox
{
}
- (void)comboBoxSelectionDidChange:(NSNotification *)notification;
- (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
- (void)comboBoxWillDismiss:(NSNotification *)notification;
- (void)comboBoxWillPopUp:(NSNotification *)notification;
@end // wxPoserNSComboBox
//WX_IMPLEMENT_POSER(wxPoserNSComboBox);
@implementation wxPoserNSComboBox : NSComboBox
- (void)comboBoxSelectionDidChange:(NSNotification *)notification
{
wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
}
- (void)comboBoxSelectionIsChanging:(NSNotification *)notification
{
//...
}
- (void)comboBoxWillDismiss:(NSNotification *)notification
{
//...
}
- (void)comboBoxWillPopUp:(NSNotification *)notification
{
//...
}
@end // implementation wxPoserNSComboBox
#include "wx/app.h"
#include "wx/combobox.h"
#include "wx/log.h"
@@ -25,7 +181,7 @@
IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl)
BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl)
END_EVENT_TABLE()
// WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
const wxString& value,
@@ -54,9 +210,9 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
wxAutoNSAutoreleasePool pool;
if(!CreateControl(parent,winid,pos,size,style,validator,name))
return false;
m_cocoaNSView = NULL;
SetNSTextField([[NSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
[m_cocoaNSView release];
[GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
[GetNSControl() sizeToFit];
@@ -64,80 +220,115 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
m_parent->CocoaAddChild(this);
SetInitialFrameRect(pos,size);
for(int i = 0; i < n; ++i)
wxComboBox::DoAppend(choices[i]);
[GetNSComboBox() setCompletes:true]; //autocomplete :)
return true;
}
wxComboBox::~wxComboBox()
{
DisassociateNSComboBox(GetNSComboBox());
}
void wxComboBox::SetSelection(int)
void wxComboBox::doWxEvent(int nEvent)
{
wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
event2.SetInt(GetSelection());
event2.SetEventObject(this);
event2.SetString(GetStringSelection());
GetEventHandler()->ProcessEvent(event2);
// For consistency with MSW and GTK, also send a text updated event
// After all, the text is updated when a selection is made
wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
TextEvent.SetString( GetStringSelection() );
TextEvent.SetEventObject( this );
GetEventHandler()->ProcessEvent( TextEvent );
}
void wxComboBox::SetSelection(int nSelection)
{
[GetNSComboBox() selectItemAtIndex:nSelection];
}
wxString wxComboBox::GetStringSelection()
{
return wxEmptyString;
return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
}
void wxComboBox::Clear()
{
[GetNSComboBox() removeAllItems];
m_Datas.Clear();
}
void wxComboBox::Delete(int)
void wxComboBox::Delete(int nIndex)
{
[GetNSComboBox() removeItemAtIndex:nIndex];
m_Datas.RemoveAt(nIndex);
}
int wxComboBox::GetCount() const
{
return 0;
return [GetNSComboBox() numberOfItems];
}
wxString wxComboBox::GetString(int) const
{
return wxEmptyString;
wxString wxComboBox::GetString(int nIndex) const
{ return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]); }
void wxComboBox::SetString(int nIndex, const wxString& szString)
{
wxAutoNSAutoreleasePool pool;
//FIXME: There appears to be no "set item data" method - maybe
//an assignment would work?
[GetNSComboBox() removeItemAtIndex:nIndex];
[GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
}
void wxComboBox::SetString(int, const wxString&)
{
}
int wxComboBox::FindString(const wxString&) const
{
return wxNOT_FOUND;
}
int wxComboBox::FindString(const wxString& szItem) const
{ return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)]; }
int wxComboBox::GetSelection() const
{ return [GetNSComboBox() indexOfSelectedItem]; }
int wxComboBox::DoAppend(const wxString& szItem)
{
return 0;
m_Datas.Add(NULL);
wxAutoNSAutoreleasePool pool;
[GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
return [GetNSComboBox() numberOfItems];
}
int wxComboBox::DoAppend(const wxString&)
int wxComboBox::DoInsert(const wxString& szItem, int nIndex)
{
return 0;
m_Datas.Insert(NULL, nIndex);
wxAutoNSAutoreleasePool pool;
[GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
return nIndex;
}
int wxComboBox::DoInsert(const wxString&, int)
void wxComboBox::DoSetItemClientData(int nIndex, void* pData)
{
return 0;
m_Datas[nIndex] = pData;
}
void wxComboBox::DoSetItemClientData(int, void*)
void* wxComboBox::DoGetItemClientData(int nIndex) const
{
return m_Datas[nIndex];
}
void* wxComboBox::DoGetItemClientData(int) const
void wxComboBox::DoSetItemClientObject(int nIndex, wxClientData* pClientData)
{
return NULL;
m_Datas[nIndex] = (void*) pClientData;
}
void wxComboBox::DoSetItemClientObject(int, wxClientData*)
wxClientData* wxComboBox::DoGetItemClientObject(int nIndex) const
{
}
wxClientData* wxComboBox::DoGetItemClientObject(int) const
{
return NULL;
return (wxClientData*) m_Datas[nIndex];
}
#endif //wxUSE_COMBOBOX