]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/textctrl.mm
Fix horizontal mouse wheel scrolling in wxGTK.
[wxWidgets.git] / src / cocoa / textctrl.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/textctrl.mm
3 // Purpose: wxTextCtrl
4 // Author: David Elliott
5 // Modified by: Mark Oxenham
6 // Created: 2003/03/16
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #include "wx/textctrl.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/app.h"
17 #include "wx/log.h"
18 #endif //WX_PRECOMP
19
20 #include "wx/cocoa/string.h"
21
22 #include "wx/cocoa/autorelease.h"
23
24 #import <Foundation/NSString.h>
25 #import <AppKit/NSTextField.h>
26 #import <AppKit/NSSecureTextField.h>
27 #import <AppKit/NSCell.h>
28
29 #include <math.h>
30
31 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
32 END_EVENT_TABLE()
33 WX_IMPLEMENT_COCOA_OWNER(wxTextCtrl,NSTextField,NSControl,NSView)
34
35 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID winid,
36 const wxString& value,
37 const wxPoint& pos,
38 const wxSize& size,
39 long style,
40 const wxValidator& validator,
41 const wxString& name)
42 {
43 wxAutoNSAutoreleasePool pool;
44 if(!CreateControl(parent,winid,pos,size,style,validator,name))
45 return false;
46 m_cocoaNSView = NULL;
47 SetNSTextField([(style & wxTE_PASSWORD)?[NSSecureTextField alloc]:[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
48 [m_cocoaNSView release];
49 [GetNSTextField() setStringValue:wxNSStringWithWxString(value)];
50
51 [GetNSControl() sizeToFit];
52 NSRect currentFrame = [m_cocoaNSView frame];
53 if(currentFrame.size.width < 70)
54 {
55 currentFrame.size.width = 70;
56 [m_cocoaNSView setFrame:currentFrame];
57 }
58 if(m_parent)
59 m_parent->CocoaAddChild(this);
60 SetInitialFrameRect(pos,size);
61
62 [(NSTextField*)m_cocoaNSView setTarget: sm_cocoaTarget];
63 [(NSTextField*)m_cocoaNSView setAction:@selector(wxNSControlAction:)];
64
65 // set the text alignment option
66 NSTextAlignment alignStyle;
67 if (style & wxTE_RIGHT)
68 alignStyle = NSRightTextAlignment;
69 else if (style & wxTE_CENTRE)
70 alignStyle = NSCenterTextAlignment;
71 else // default to wxTE_LEFT because it is 0 and can't be tested
72 alignStyle = NSLeftTextAlignment;
73 [GetNSControl() setAlignment:alignStyle];
74
75 // if Read-only then set as such, this flag is overwritable by wxTextCtrl::SetEditable(TRUE)
76 if (style & wxTE_READONLY)
77 {
78 SetEditable(FALSE);
79 }
80
81 return true;
82 }
83
84 wxTextCtrl::~wxTextCtrl()
85 {
86 DisassociateNSTextField(GetNSTextField());
87 }
88
89 void wxTextCtrl::Cocoa_didChangeText(void)
90 {
91 }
92
93 void wxTextCtrl::CocoaTarget_action(void)
94 {
95 SendTextUpdatedEvent();
96 }
97
98 void wxTextCtrl::AppendText(wxString const&)
99 {
100 }
101
102 void wxTextCtrl::SetEditable(bool editable)
103 {
104 // first ensure that the current value is stored (in case the user had not finished editing
105 // before SetEditable(FALSE) was called)
106 DoSetValue(GetValue(),1);
107
108 [GetNSTextField() setEditable: editable];
109
110 // forces the focus on the textctrl to be lost - while focus is still maintained
111 // after SetEditable(FALSE) the user may still edit the control
112 // (might not the best way to do this..)
113 [GetNSTextField() abortEditing];
114 }
115
116 void wxTextCtrl::MarkDirty()
117 {
118 }
119
120 void wxTextCtrl::DiscardEdits()
121 {
122 }
123
124 void wxTextCtrl::SetSelection(long, long)
125 {
126 }
127
128 void wxTextCtrl::ShowPosition(long)
129 {
130 }
131
132 void wxTextCtrl::SetInsertionPoint(long)
133 {
134 }
135
136 void wxTextCtrl::SetInsertionPointEnd()
137 {
138 }
139
140 void wxTextCtrl::Cut()
141 {
142 }
143
144 void wxTextCtrl::Copy()
145 {
146 }
147
148 void wxTextCtrl::Redo()
149 {
150 }
151
152 void wxTextCtrl::Undo()
153 {
154 }
155
156 void wxTextCtrl::Clear()
157 {
158 }
159
160 void wxTextCtrl::Paste()
161 {
162 }
163
164 void wxTextCtrl::Remove(long, long)
165 {
166 }
167
168 void wxTextCtrl::Replace(long, long, wxString const&)
169 {
170 }
171
172 void wxTextCtrl::DoSetValue(wxString const& value, int flags)
173 {
174 wxAutoNSAutoreleasePool pool;
175 [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
176
177 if ( flags & SetValue_SendEvent )
178 SendTextUpdatedEvent();
179 }
180
181 void wxTextCtrl::WriteText(wxString const&)
182 {
183 }
184
185 bool wxTextCtrl::IsEditable() const
186 {
187 return [GetNSTextField() isEditable];
188 }
189
190 bool wxTextCtrl::IsModified() const
191 {
192 return false;
193 }
194
195 wxString wxTextCtrl::GetLineText(long) const
196 {
197 return wxEmptyString;
198 }
199
200 void wxTextCtrl::GetSelection(long*, long*) const
201 {
202 }
203
204 bool wxTextCtrl::PositionToXY(long, long*, long*) const
205 {
206 return false;
207 }
208
209 long wxTextCtrl::XYToPosition(long, long) const
210 {
211 return 0;
212 }
213
214 int wxTextCtrl::GetLineLength(long) const
215 {
216 return 0;
217 }
218
219 wxTextPos wxTextCtrl::GetLastPosition() const
220 {
221 // working - returns the size of the wxString
222 return (long)(GetValue().Len());
223 }
224
225 int wxTextCtrl::GetNumberOfLines() const
226 {
227 return 0;
228 }
229
230 long wxTextCtrl::GetInsertionPoint() const
231 {
232 return 0;
233 }
234
235 bool wxTextCtrl::CanRedo() const
236 {
237 return false;
238 }
239
240 bool wxTextCtrl::CanUndo() const
241 {
242 return false;
243 }
244
245 wxString wxTextCtrl::GetValue() const
246 {
247 wxAutoNSAutoreleasePool pool;
248 return wxStringWithNSString([GetNSTextField() stringValue]);
249 }
250
251 wxSize wxTextCtrl::DoGetBestSize() const
252 {
253 wxAutoNSAutoreleasePool pool;
254 wxASSERT(GetNSControl());
255 NSCell *cell = [GetNSControl() cell];
256 wxASSERT(cell);
257 NSSize cellSize = [cell cellSize];
258 wxSize size(100,(int)ceil(cellSize.height));
259
260 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);
261 return size;
262 }