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