]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
3 | // Purpose: wxListBox | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "listbox.h" | |
14 | #endif | |
15 | ||
03e11df5 | 16 | #include "wx/app.h" |
e9576ca5 SC |
17 | #include "wx/listbox.h" |
18 | #include "wx/settings.h" | |
422644a3 | 19 | #include "wx/toplevel.h" |
e9576ca5 SC |
20 | #include "wx/dynarray.h" |
21 | #include "wx/log.h" | |
22 | ||
519cb848 | 23 | #include "wx/utils.h" |
519cb848 | 24 | |
2f1ae414 | 25 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 26 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
519cb848 SC |
27 | |
28 | BEGIN_EVENT_TABLE(wxListBox, wxControl) | |
a8e6bf8a | 29 | EVT_SIZE( wxListBox::OnSize ) |
519cb848 | 30 | END_EVENT_TABLE() |
2f1ae414 | 31 | #endif |
e9576ca5 | 32 | |
d497dca4 | 33 | #include "wx/mac/uma.h" |
519cb848 | 34 | |
e42e45a9 SC |
35 | |
36 | typedef struct { | |
37 | unsigned short instruction; | |
38 | void (*function)(); | |
39 | } ldefRec, *ldefPtr, **ldefHandle; | |
40 | ||
41 | extern "C" | |
42 | { | |
43 | static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect, | |
a8e6bf8a RR |
44 | Cell cell, short dataOffset, short dataLength, |
45 | ListHandle listHandle ) ; | |
e42e45a9 SC |
46 | } |
47 | ||
48 | static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect, | |
a8e6bf8a RR |
49 | Cell cell, short dataOffset, short dataLength, |
50 | ListHandle listHandle ) | |
51 | { | |
52 | FontInfo fontInfo; | |
53 | GrafPtr savePort; | |
54 | GrafPtr grafPtr; | |
55 | RgnHandle savedClipRegion; | |
56 | SInt32 savedPenMode; | |
57 | wxListBox* list; | |
e42e45a9 SC |
58 | GetPort(&savePort); |
59 | SetPort((**listHandle).port); | |
60 | grafPtr = (**listHandle).port ; | |
a8e6bf8a RR |
61 | // typecast our refCon |
62 | list = (wxListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) ); | |
63 | ||
64 | // Calculate the cell rect. | |
65 | ||
66 | switch( message ) { | |
67 | case lInitMsg: | |
68 | break; | |
69 | ||
70 | case lCloseMsg: | |
71 | break; | |
72 | ||
73 | case lDrawMsg: | |
74 | { | |
75 | const wxString text = list->m_stringArray[cell.v] ; | |
76 | ||
77 | // Save the current clip region, and set the clip region to the area we are about | |
78 | // to draw. | |
79 | ||
80 | savedClipRegion = NewRgn(); | |
81 | GetClip( savedClipRegion ); | |
82 | ||
83 | ClipRect( drawRect ); | |
84 | EraseRect( drawRect ); | |
85 | ||
86 | MoveTo(drawRect->left + 4 , drawRect->top + 10 ); | |
87 | ::TextFont( kFontIDMonaco ) ; | |
88 | ::TextSize( 9 ); | |
89 | ::TextFace( 0 ) ; | |
e42e45a9 | 90 | |
a8e6bf8a RR |
91 | DrawText(text, 0 , text.Length()); |
92 | // If the cell is hilited, do the hilite now. Paint the cell contents with the | |
93 | // appropriate QuickDraw transform mode. | |
94 | ||
95 | if( isSelected ) { | |
96 | savedPenMode = GetPortPenMode( grafPtr ); | |
97 | SetPortPenMode( grafPtr, hilitetransfermode ); | |
98 | PaintRect( drawRect ); | |
99 | SetPortPenMode( grafPtr, savedPenMode ); | |
100 | } | |
101 | ||
102 | // Restore the saved clip region. | |
103 | ||
104 | SetClip( savedClipRegion ); | |
105 | DisposeRgn( savedClipRegion ); | |
106 | } | |
107 | break; | |
108 | case lHiliteMsg: | |
109 | ||
110 | // Hilite or unhilite the cell. Paint the cell contents with the | |
111 | // appropriate QuickDraw transform mode. | |
112 | ||
113 | GetPort( &grafPtr ); | |
114 | savedPenMode = GetPortPenMode( grafPtr ); | |
115 | SetPortPenMode( grafPtr, hilitetransfermode ); | |
116 | PaintRect( drawRect ); | |
117 | SetPortPenMode( grafPtr, savedPenMode ); | |
118 | break; | |
119 | default : | |
120 | break ; | |
121 | } | |
122 | SetPort(savePort); | |
e42e45a9 SC |
123 | } |
124 | ||
519cb848 SC |
125 | extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ; |
126 | const short kwxMacListWithVerticalScrollbar = 128 ; | |
dbfc5b97 | 127 | const short kwxMacListItemHeight = 14 ; |
519cb848 | 128 | |
e9576ca5 SC |
129 | // ============================================================================ |
130 | // list box control implementation | |
131 | // ============================================================================ | |
132 | ||
133 | // Listbox item | |
134 | wxListBox::wxListBox() | |
135 | { | |
136 | m_noItems = 0; | |
137 | m_selected = 0; | |
2f1ae414 | 138 | m_macList = NULL ; |
e9576ca5 SC |
139 | } |
140 | ||
e42e45a9 SC |
141 | static ListDefUPP macListDefUPP = NULL ; |
142 | ||
e9576ca5 SC |
143 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, |
144 | const wxPoint& pos, | |
145 | const wxSize& size, | |
146 | int n, const wxString choices[], | |
147 | long style, | |
148 | const wxValidator& validator, | |
149 | const wxString& name) | |
150 | { | |
60149370 GD |
151 | m_noItems = 0 ; // this will be increased by our append command |
152 | m_selected = 0; | |
153 | ||
154 | Rect bounds ; | |
155 | Str255 title ; | |
156 | ||
157 | MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ; | |
e9576ca5 | 158 | |
60149370 | 159 | ListDefSpec listDef; |
e42e45a9 SC |
160 | listDef.defType = kListDefUserProcType; |
161 | if ( macListDefUPP == NULL ) | |
162 | { | |
163 | macListDefUPP = NewListDefUPP( wxMacListDefinition ); | |
164 | } | |
a8e6bf8a | 165 | listDef.u.userProc = macListDefUPP ; |
de043984 | 166 | |
e42e45a9 | 167 | #if TARGET_CARBON |
60149370 | 168 | Size asize; |
519cb848 | 169 | |
519cb848 | 170 | |
41c9a21f | 171 | CreateListBoxControl( parent->MacGetRootWindow(), &bounds, false, 0, 1, false, true, |
dbfc5b97 | 172 | kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, &m_macControl ); |
519cb848 | 173 | |
60149370 GD |
174 | GetControlData(m_macControl, kControlNoPart, kControlListBoxListHandleTag, |
175 | sizeof(ListHandle), (Ptr) &m_macList, &asize); | |
519cb848 | 176 | |
60149370 GD |
177 | SetControlReference(m_macControl, (long) this); |
178 | SetControlVisibility(m_macControl, false, false); | |
519cb848 | 179 | |
60149370 | 180 | #else |
de043984 | 181 | |
a8e6bf8a | 182 | long result ; |
60149370 | 183 | |
41c9a21f | 184 | m_macControl = ::NewControl( parent->MacGetRootWindow() , &bounds , title , false , |
a8e6bf8a RR |
185 | kwxMacListWithVerticalScrollbar , 0 , 0, |
186 | kControlListBoxProc , (long) this ) ; | |
72055702 | 187 | ::GetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag , |
a8e6bf8a | 188 | sizeof( ListHandle ) , (char*) &m_macList , &result ) ; |
60149370 GD |
189 | |
190 | HLock( (Handle) m_macList ) ; | |
e42e45a9 SC |
191 | ldefHandle ldef ; |
192 | ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ; | |
193 | if ( (**m_macList).listDefProc != NULL ) | |
194 | { | |
195 | (**ldef).instruction = 0x4EF9; /* JMP instruction */ | |
196 | (**ldef).function = (void(*)()) listDef.u.userProc; | |
197 | (**m_macList).listDefProc = (Handle) ldef ; | |
198 | } | |
199 | ||
200 | Point pt = (**m_macList).cellSize ; | |
dbfc5b97 | 201 | pt.v = kwxMacListItemHeight ; |
e42e45a9 | 202 | LCellSize( pt , m_macList ) ; |
e42e45a9 SC |
203 | LAddColumn( 1 , 0 , m_macList ) ; |
204 | #endif | |
205 | OptionBits options = 0; | |
60149370 GD |
206 | if ( style & wxLB_MULTIPLE ) |
207 | { | |
e42e45a9 | 208 | options += lNoExtend ; |
60149370 GD |
209 | } |
210 | else if ( style & wxLB_EXTENDED ) | |
211 | { | |
e42e45a9 | 212 | options += lExtendDrag ; |
60149370 GD |
213 | } |
214 | else | |
215 | { | |
e42e45a9 | 216 | options = lOnlyOne ; |
60149370 | 217 | } |
e42e45a9 | 218 | SetListSelectionFlags(m_macList, options); |
60149370 GD |
219 | |
220 | MacPostControlCreate() ; | |
221 | ||
222 | for ( int i = 0 ; i < n ; i++ ) | |
223 | { | |
a8e6bf8a | 224 | Append( choices[i] ) ; |
60149370 GD |
225 | } |
226 | ||
227 | LSetDrawingMode( true , m_macList ) ; | |
519cb848 | 228 | |
60149370 | 229 | return TRUE; |
e9576ca5 SC |
230 | } |
231 | ||
232 | wxListBox::~wxListBox() | |
233 | { | |
a8e6bf8a RR |
234 | Free() ; |
235 | if ( m_macList ) | |
236 | { | |
60149370 | 237 | #if !TARGET_CARBON |
e42e45a9 SC |
238 | DisposeHandle( (**m_macList).listDefProc ) ; |
239 | (**m_macList).listDefProc = NULL ; | |
60149370 | 240 | #endif |
a8e6bf8a RR |
241 | m_macList = NULL ; |
242 | } | |
e9576ca5 SC |
243 | } |
244 | ||
e7549107 | 245 | void wxListBox::Free() |
e9576ca5 | 246 | { |
e7549107 SC |
247 | #if wxUSE_OWNER_DRAWN |
248 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
249 | { | |
250 | size_t uiCount = m_aItems.Count(); | |
251 | while ( uiCount-- != 0 ) { | |
252 | delete m_aItems[uiCount]; | |
253 | } | |
254 | ||
255 | m_aItems.Clear(); | |
256 | } | |
257 | else | |
258 | #endif // wxUSE_OWNER_DRAWN | |
259 | if ( HasClientObjectData() ) | |
260 | { | |
261 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
262 | { | |
263 | delete GetClientObject(n); | |
264 | } | |
265 | } | |
e9576ca5 SC |
266 | } |
267 | ||
8614041b SC |
268 | void wxListBox::DoSetSize(int x, int y, |
269 | int width, int height, | |
270 | int sizeFlags ) | |
271 | { | |
a8e6bf8a | 272 | wxControl::DoSetSize( x , y , width , height , sizeFlags ) ; |
8614041b | 273 | #if TARGET_CARBON |
a8e6bf8a RR |
274 | Rect bounds ; |
275 | GetControlBounds( m_macControl , &bounds ) ; | |
276 | ControlRef control = GetListVerticalScrollBar( m_macList ) ; | |
277 | if ( control ) | |
278 | { | |
279 | Rect scrollbounds ; | |
280 | GetControlBounds( control , &scrollbounds ) ; | |
281 | if( scrollbounds.right != bounds.right + 1 ) | |
282 | { | |
283 | UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 , | |
284 | scrollbounds.top ) ; | |
285 | } | |
286 | } | |
8614041b SC |
287 | #endif |
288 | } | |
e7549107 | 289 | void wxListBox::DoSetFirstItem(int N) |
e9576ca5 | 290 | { |
a8e6bf8a | 291 | MacScrollTo( N ) ; |
e9576ca5 SC |
292 | } |
293 | ||
294 | void wxListBox::Delete(int N) | |
295 | { | |
e7549107 SC |
296 | wxCHECK_RET( N >= 0 && N < m_noItems, |
297 | wxT("invalid index in wxListBox::Delete") ); | |
298 | ||
299 | #if wxUSE_OWNER_DRAWN | |
300 | delete m_aItems[N]; | |
0baac61e | 301 | m_aItems.RemoveAt(N); |
e7549107 SC |
302 | #else // !wxUSE_OWNER_DRAWN |
303 | if ( HasClientObjectData() ) | |
304 | { | |
305 | delete GetClientObject(N); | |
306 | } | |
307 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
a8e6bf8a RR |
308 | m_stringArray.Remove( N ) ; |
309 | m_dataArray.RemoveAt( N ) ; | |
310 | m_noItems --; | |
311 | ||
312 | MacDelete( N ) ; | |
e9576ca5 SC |
313 | } |
314 | ||
e7549107 | 315 | int wxListBox::DoAppend(const wxString& item) |
e9576ca5 | 316 | { |
a8e6bf8a RR |
317 | int index = m_noItems ; |
318 | if( wxApp::s_macDefaultEncodingIsPC ) | |
319 | { | |
320 | m_stringArray.Add( wxMacMakeMacStringFromPC( item ) ) ; | |
321 | m_dataArray.Add( NULL ); | |
322 | } | |
323 | else { | |
324 | m_stringArray.Add( item ) ; | |
325 | m_dataArray.Add( NULL ); | |
326 | } | |
327 | m_noItems ++; | |
328 | DoSetItemClientData( index , NULL ) ; | |
329 | MacAppend( item ) ; | |
e7549107 | 330 | |
a8e6bf8a | 331 | return index ; |
e9576ca5 SC |
332 | } |
333 | ||
e7549107 SC |
334 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) |
335 | { | |
336 | MacSetRedraw( false ) ; | |
519cb848 | 337 | Clear() ; |
e7549107 SC |
338 | int n = choices.GetCount(); |
339 | ||
519cb848 SC |
340 | for( int i = 0 ; i < n ; ++i ) |
341 | { | |
a8e6bf8a RR |
342 | if ( clientData ) |
343 | { | |
e7549107 SC |
344 | #if wxUSE_OWNER_DRAWN |
345 | wxASSERT_MSG(clientData[i] == NULL, | |
346 | wxT("Can't use client data with owner-drawn listboxes")); | |
347 | #else // !wxUSE_OWNER_DRAWN | |
a8e6bf8a | 348 | Append( choices[i] , clientData[i] ) ; |
e7549107 | 349 | #endif |
a8e6bf8a RR |
350 | } |
351 | else | |
352 | Append( choices[i] ) ; | |
519cb848 | 353 | } |
e7549107 SC |
354 | |
355 | #if wxUSE_OWNER_DRAWN | |
356 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
357 | // first delete old items | |
358 | size_t ui = m_aItems.Count(); | |
359 | while ( ui-- != 0 ) { | |
360 | delete m_aItems[ui]; | |
361 | } | |
362 | m_aItems.Empty(); | |
363 | ||
364 | // then create new ones | |
365 | for ( ui = 0; ui < (size_t)m_noItems; ui++ ) { | |
366 | wxOwnerDrawn *pNewItem = CreateItem(ui); | |
367 | pNewItem->SetName(choices[ui]); | |
368 | m_aItems.Add(pNewItem); | |
369 | } | |
370 | } | |
371 | #endif // wxUSE_OWNER_DRAWN | |
372 | MacSetRedraw( true ) ; | |
373 | } | |
374 | ||
375 | bool wxListBox::HasMultipleSelection() const | |
376 | { | |
377 | return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED); | |
e9576ca5 SC |
378 | } |
379 | ||
519cb848 | 380 | int wxListBox::FindString(const wxString& st) const |
e9576ca5 | 381 | { |
a8e6bf8a RR |
382 | wxString s ; |
383 | if( wxApp::s_macDefaultEncodingIsPC ) | |
384 | { | |
385 | s = wxMacMakeMacStringFromPC( st ) ; | |
386 | } | |
387 | else | |
388 | s = st ; | |
389 | ||
390 | if ( s.Right(1) == "*" ) | |
391 | { | |
392 | wxString search = s.Left( s.Length() - 1 ) ; | |
393 | int len = search.Length() ; | |
394 | Str255 s1 , s2 ; | |
03e11df5 GD |
395 | |
396 | #if TARGET_CARBON | |
a8e6bf8a | 397 | c2pstrcpy( (StringPtr) s2 , search.c_str() ) ; |
03e11df5 | 398 | #else |
a8e6bf8a RR |
399 | strcpy( (char *) s2 , search.c_str() ) ; |
400 | c2pstr( (char *) s2 ) ; | |
03e11df5 GD |
401 | #endif |
402 | ||
a8e6bf8a RR |
403 | for ( int i = 0 ; i < m_noItems ; ++ i ) |
404 | { | |
03e11df5 | 405 | #if TARGET_CARBON |
a8e6bf8a | 406 | c2pstrcpy( (StringPtr) s1 , m_stringArray[i].Left( len ).c_str() ) ; |
03e11df5 | 407 | #else |
a8e6bf8a RR |
408 | strcpy( (char *) s1 , m_stringArray[i].Left( len ).c_str() ) ; |
409 | c2pstr( (char *) s1 ) ; | |
03e11df5 | 410 | #endif |
a8e6bf8a RR |
411 | if ( EqualString( s1 , s2 , false , false ) ) |
412 | return i ; | |
413 | } | |
414 | if ( s.Left(1) == "*" && s.Length() > 1 ) | |
415 | { | |
416 | s.MakeLower() ; | |
417 | for ( int i = 0 ; i < m_noItems ; ++i ) | |
418 | { | |
419 | if ( GetString(i).Lower().Matches(s) ) | |
420 | return i ; | |
421 | } | |
422 | } | |
423 | ||
424 | } | |
425 | else | |
426 | { | |
427 | Str255 s1 , s2 ; | |
03e11df5 GD |
428 | |
429 | #if TARGET_CARBON | |
a8e6bf8a | 430 | c2pstrcpy( (StringPtr) s2 , s.c_str() ) ; |
03e11df5 | 431 | #else |
a8e6bf8a RR |
432 | strcpy( (char *) s2 , s.c_str() ) ; |
433 | c2pstr( (char *) s2 ) ; | |
03e11df5 GD |
434 | #endif |
435 | ||
a8e6bf8a RR |
436 | for ( int i = 0 ; i < m_noItems ; ++ i ) |
437 | { | |
03e11df5 | 438 | #if TARGET_CARBON |
a8e6bf8a | 439 | c2pstrcpy( (StringPtr) s1 , m_stringArray[i].c_str() ) ; |
03e11df5 | 440 | #else |
a8e6bf8a RR |
441 | strcpy( (char *) s1 , m_stringArray[i].c_str() ) ; |
442 | c2pstr( (char *) s1 ) ; | |
03e11df5 | 443 | #endif |
a8e6bf8a RR |
444 | if ( EqualString( s1 , s2 , false , false ) ) |
445 | return i ; | |
446 | } | |
519cb848 SC |
447 | } |
448 | return -1; | |
e9576ca5 SC |
449 | } |
450 | ||
451 | void wxListBox::Clear() | |
452 | { | |
e7549107 | 453 | Free(); |
e9576ca5 | 454 | m_noItems = 0; |
519cb848 SC |
455 | m_stringArray.Empty() ; |
456 | m_dataArray.Empty() ; | |
457 | MacClear() ; | |
e9576ca5 SC |
458 | } |
459 | ||
460 | void wxListBox::SetSelection(int N, bool select) | |
461 | { | |
519cb848 SC |
462 | wxCHECK_RET( N >= 0 && N < m_noItems, |
463 | "invalid index in wxListBox::SetSelection" ); | |
a8e6bf8a RR |
464 | MacSetSelection( N , select ) ; |
465 | GetSelections( m_selectionPreImage ) ; | |
e9576ca5 SC |
466 | } |
467 | ||
e7549107 | 468 | bool wxListBox::IsSelected(int N) const |
e9576ca5 | 469 | { |
519cb848 SC |
470 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, |
471 | "invalid index in wxListBox::Selected" ); | |
472 | ||
a8e6bf8a | 473 | return MacIsSelected( N ) ; |
e9576ca5 SC |
474 | } |
475 | ||
e7549107 | 476 | void *wxListBox::DoGetItemClientData(int N) const |
e9576ca5 | 477 | { |
519cb848 | 478 | wxCHECK_MSG( N >= 0 && N < m_noItems, NULL, |
60149370 | 479 | wxT("invalid index in wxListBox::GetClientData")); |
519cb848 | 480 | |
e7549107 | 481 | return (void *)m_dataArray[N]; |
e9576ca5 SC |
482 | } |
483 | ||
51abe921 SC |
484 | wxClientData *wxListBox::DoGetItemClientObject(int N) const |
485 | { | |
a8e6bf8a | 486 | return (wxClientData *) DoGetItemClientData( N ) ; |
51abe921 SC |
487 | } |
488 | ||
e7549107 | 489 | void wxListBox::DoSetItemClientData(int N, void *Client_data) |
e9576ca5 | 490 | { |
519cb848 SC |
491 | wxCHECK_RET( N >= 0 && N < m_noItems, |
492 | "invalid index in wxListBox::SetClientData" ); | |
493 | ||
e7549107 SC |
494 | #if wxUSE_OWNER_DRAWN |
495 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
496 | { | |
497 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
498 | // in OnMeasure/OnDraw. | |
499 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
500 | } | |
501 | #endif // wxUSE_OWNER_DRAWN | |
a8e6bf8a RR |
502 | wxASSERT_MSG( m_dataArray.GetCount() >= N , "invalid client_data array" ) ; |
503 | ||
504 | if ( m_dataArray.GetCount() > N ) | |
505 | { | |
506 | m_dataArray[N] = (char*) Client_data ; | |
2f1ae414 | 507 | } |
8208e181 SC |
508 | else |
509 | { | |
a8e6bf8a | 510 | m_dataArray.Add( (char*) Client_data ) ; |
8208e181 | 511 | } |
e7549107 SC |
512 | } |
513 | ||
514 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) | |
515 | { | |
516 | DoSetItemClientData(n, clientData); | |
e9576ca5 SC |
517 | } |
518 | ||
519 | // Return number of selections and an array of selected integers | |
520 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
521 | { | |
a8e6bf8a | 522 | return MacGetSelections( aSelections ) ; |
e9576ca5 SC |
523 | } |
524 | ||
525 | // Get single selection, for single choice list items | |
526 | int wxListBox::GetSelection() const | |
527 | { | |
a8e6bf8a | 528 | return MacGetSelection() ; |
e9576ca5 SC |
529 | } |
530 | ||
531 | // Find string for position | |
532 | wxString wxListBox::GetString(int N) const | |
533 | { | |
a8e6bf8a RR |
534 | if( wxApp::s_macDefaultEncodingIsPC ) |
535 | { | |
536 | return wxMacMakePCStringFromMac( m_stringArray[N] ) ; | |
537 | } | |
538 | else | |
539 | return m_stringArray[N] ; | |
e9576ca5 SC |
540 | } |
541 | ||
e7549107 | 542 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) |
e9576ca5 | 543 | { |
e7549107 SC |
544 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, |
545 | wxT("invalid index in wxListBox::InsertItems") ); | |
546 | ||
547 | int nItems = items.GetCount(); | |
548 | ||
a8e6bf8a RR |
549 | for ( int i = 0 ; i < nItems ; i++ ) |
550 | { | |
551 | m_stringArray.Insert( items[i] , pos + i ) ; | |
552 | m_dataArray.Insert( NULL , pos + i ) ; | |
553 | MacInsert( pos + i , items[i] ) ; | |
554 | } | |
e9576ca5 | 555 | |
519cb848 | 556 | m_noItems += nItems; |
e9576ca5 SC |
557 | } |
558 | ||
559 | void wxListBox::SetString(int N, const wxString& s) | |
560 | { | |
a8e6bf8a RR |
561 | wxString str ; |
562 | if( wxApp::s_macDefaultEncodingIsPC ) | |
563 | { | |
564 | str = wxMacMakeMacStringFromPC( s ) ; | |
565 | } | |
566 | else | |
567 | str = s ; | |
568 | m_stringArray[N] = str ; | |
569 | MacSet( N , s ) ; | |
e9576ca5 SC |
570 | } |
571 | ||
37e2cb08 | 572 | wxSize wxListBox::DoGetBestSize() const |
e9576ca5 | 573 | { |
e7549107 | 574 | return wxSize(100, 100); |
e9576ca5 SC |
575 | } |
576 | ||
51abe921 SC |
577 | int wxListBox::GetCount() const |
578 | { | |
579 | return m_noItems; | |
580 | } | |
581 | ||
582 | void wxListBox::SetupColours() | |
583 | { | |
584 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
585 | SetForegroundColour(GetParent()->GetForegroundColour()); | |
586 | } | |
587 | ||
60149370 GD |
588 | void wxListBox::Refresh(bool eraseBack, const wxRect *rect) |
589 | { | |
de043984 SC |
590 | wxControl::Refresh( eraseBack , rect ) ; |
591 | // MacRedrawControl() ; | |
60149370 GD |
592 | } |
593 | ||
51abe921 SC |
594 | #if wxUSE_OWNER_DRAWN |
595 | ||
596 | class wxListBoxItem : public wxOwnerDrawn | |
597 | { | |
598 | public: | |
599 | wxListBoxItem(const wxString& str = ""); | |
600 | }; | |
601 | ||
602 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) | |
603 | { | |
604 | // no bitmaps/checkmarks | |
605 | SetMarginWidth(0); | |
606 | } | |
607 | ||
608 | wxOwnerDrawn *wxListBox::CreateItem(size_t n) | |
609 | { | |
610 | return new wxListBoxItem(); | |
611 | } | |
612 | ||
613 | #endif //USE_OWNER_DRAWN | |
e9576ca5 | 614 | |
519cb848 SC |
615 | // ============================================================================ |
616 | // list box control implementation | |
617 | // ============================================================================ | |
618 | ||
619 | void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) | |
620 | { | |
a8e6bf8a RR |
621 | wxListBox* list; |
622 | // typecast our refCon | |
623 | list = (wxListBox*)refCon; | |
624 | ||
625 | MoveTo(cellRect->left + 4 , cellRect->top + 10 ); | |
626 | const wxString text = list->m_stringArray[lCell.v] ; | |
627 | ::TextFont( kFontIDMonaco ) ; | |
628 | ::TextSize( 9 ); | |
629 | ::TextFace( 0 ) ; | |
630 | DrawText(text, 0 , text.Length()); | |
631 | ||
519cb848 SC |
632 | } |
633 | ||
634 | void wxListBox::MacDelete( int N ) | |
635 | { | |
60149370 GD |
636 | LDelRow( 1 , N , m_macList) ; |
637 | Refresh(); | |
519cb848 SC |
638 | } |
639 | ||
640 | void wxListBox::MacInsert( int n , const char * text) | |
641 | { | |
60149370 GD |
642 | Cell cell = { 0 , 0 } ; |
643 | cell.v = n ; | |
644 | LAddRow( 1 , cell.v , m_macList ) ; | |
e42e45a9 | 645 | // LSetCell(text, strlen(text), cell, m_macList); |
60149370 | 646 | Refresh(); |
519cb848 SC |
647 | } |
648 | ||
649 | void wxListBox::MacAppend( const char * text) | |
650 | { | |
60149370 GD |
651 | Cell cell = { 0 , 0 } ; |
652 | cell.v = (**m_macList).dataBounds.bottom ; | |
653 | LAddRow( 1 , cell.v , m_macList ) ; | |
e42e45a9 | 654 | // LSetCell(text, strlen(text), cell, m_macList); |
60149370 | 655 | Refresh(); |
519cb848 SC |
656 | } |
657 | ||
658 | void wxListBox::MacClear() | |
659 | { | |
60149370 GD |
660 | LDelRow( (**m_macList).dataBounds.bottom , 0 , m_macList ) ; |
661 | Refresh(); | |
519cb848 SC |
662 | } |
663 | ||
664 | void wxListBox::MacSetSelection( int n , bool select ) | |
665 | { | |
a8e6bf8a RR |
666 | Cell cell = { 0 , 0 } ; |
667 | if ( ! (m_windowStyle & wxLB_MULTIPLE) ) | |
668 | { | |
669 | if ( LGetSelect( true , &cell , m_macList ) ) | |
670 | { | |
671 | LSetSelect( false , cell , m_macList ) ; | |
672 | } | |
673 | } | |
674 | ||
675 | cell.v = n ; | |
676 | LSetSelect( select , cell , m_macList ) ; | |
677 | LAutoScroll( m_macList ) ; | |
678 | Refresh(); | |
519cb848 SC |
679 | } |
680 | ||
681 | bool wxListBox::MacIsSelected( int n ) const | |
682 | { | |
a8e6bf8a RR |
683 | Cell cell = { 0 , 0 } ; |
684 | cell.v = n ; | |
685 | return LGetSelect( false , &cell , m_macList ) ; | |
519cb848 SC |
686 | } |
687 | ||
688 | void wxListBox::MacDestroy() | |
689 | { | |
60149370 | 690 | // DisposeExtLDEFInfo( m_macList ) ; |
519cb848 SC |
691 | } |
692 | ||
693 | int wxListBox::MacGetSelection() const | |
694 | { | |
a8e6bf8a RR |
695 | Cell cell = { 0 , 0 } ; |
696 | if ( LGetSelect( true , &cell , m_macList ) ) | |
697 | return cell.v ; | |
698 | else | |
699 | return -1 ; | |
519cb848 SC |
700 | } |
701 | ||
702 | int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const | |
703 | { | |
a8e6bf8a RR |
704 | int no_sel = 0 ; |
705 | ||
519cb848 SC |
706 | aSelections.Empty(); |
707 | ||
a8e6bf8a RR |
708 | Cell cell = { 0 , 0 } ; |
709 | cell.v = 0 ; | |
710 | ||
711 | while ( LGetSelect( true , &cell , m_macList ) ) | |
712 | { | |
713 | aSelections.Add( cell.v ) ; | |
714 | no_sel++ ; | |
715 | cell.v++ ; | |
716 | } | |
717 | return no_sel ; | |
519cb848 SC |
718 | } |
719 | ||
720 | void wxListBox::MacSet( int n , const char * text ) | |
721 | { | |
a8e6bf8a RR |
722 | // our implementation does not store anything in the list |
723 | // so we just have to redraw | |
724 | Cell cell = { 0 , 0 } ; | |
725 | cell.v = n ; | |
726 | // LSetCell(text, strlen(text), cell, m_macList); | |
727 | Refresh(); | |
519cb848 SC |
728 | } |
729 | ||
730 | void wxListBox::MacScrollTo( int n ) | |
731 | { | |
a8e6bf8a | 732 | // TODO implement scrolling |
519cb848 SC |
733 | } |
734 | ||
735 | void wxListBox::OnSize( const wxSizeEvent &event) | |
736 | { | |
60149370 GD |
737 | Point pt; |
738 | ||
739 | #if TARGET_CARBON | |
740 | GetListCellSize(m_macList, &pt); | |
741 | #else | |
742 | pt = (**m_macList).cellSize ; | |
743 | #endif | |
744 | pt.h = m_width - 15 ; | |
745 | LCellSize( pt , m_macList ) ; | |
519cb848 SC |
746 | } |
747 | ||
748 | void wxListBox::MacHandleControlClick( ControlHandle control , SInt16 controlpart ) | |
749 | { | |
a8e6bf8a RR |
750 | Boolean wasDoubleClick = false ; |
751 | long result ; | |
519cb848 | 752 | |
a8e6bf8a RR |
753 | ::GetControlData( m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ; |
754 | if ( !wasDoubleClick ) | |
755 | { | |
756 | MacDoClick() ; | |
757 | } | |
758 | else | |
759 | { | |
760 | MacDoDoubleClick() ; | |
761 | } | |
519cb848 SC |
762 | } |
763 | ||
764 | void wxListBox::MacSetRedraw( bool doDraw ) | |
765 | { | |
a8e6bf8a RR |
766 | LSetDrawingMode( doDraw , m_macList ) ; |
767 | ||
519cb848 SC |
768 | } |
769 | ||
770 | void wxListBox::MacDoClick() | |
771 | { | |
a8e6bf8a RR |
772 | wxArrayInt aSelections; |
773 | int n, count = GetSelections(aSelections); | |
774 | ||
775 | if ( count == m_selectionPreImage.GetCount() ) | |
776 | { | |
777 | bool hasChanged = false ; | |
778 | for ( int i = 0 ; i < count ; ++i ) | |
779 | { | |
780 | if ( aSelections[i] != m_selectionPreImage[i] ) | |
781 | { | |
782 | hasChanged = true ; | |
783 | break ; | |
784 | } | |
785 | } | |
786 | if ( !hasChanged ) | |
787 | { | |
788 | return ; | |
789 | } | |
790 | } | |
791 | ||
792 | m_selectionPreImage = aSelections; | |
793 | ||
794 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
795 | event.SetEventObject( this ); | |
796 | ||
797 | if ( count > 0 ) | |
798 | { | |
799 | n = aSelections[0]; | |
800 | if ( HasClientObjectData() ) | |
801 | event.SetClientObject( GetClientObject(n) ); | |
802 | else if ( HasClientUntypedData() ) | |
803 | event.SetClientData( GetClientData(n) ); | |
804 | event.SetString( GetString(n) ); | |
805 | } | |
806 | else | |
807 | { | |
e7549107 | 808 | n = -1; |
a8e6bf8a RR |
809 | } |
810 | ||
e7549107 SC |
811 | event.m_commandInt = n; |
812 | ||
813 | GetEventHandler()->ProcessEvent(event); | |
519cb848 SC |
814 | } |
815 | ||
816 | void wxListBox::MacDoDoubleClick() | |
817 | { | |
818 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId); | |
819 | event.SetEventObject( this ); | |
a8e6bf8a | 820 | GetEventHandler()->ProcessEvent(event) ; |
519cb848 | 821 | } |