]> git.saurik.com Git - wxWidgets.git/blob - src/motif/listbox.cpp
floating pane size now restores after perspective restore
[wxWidgets.git] / src / motif / listbox.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/listbox.cpp
3 // Purpose: wxListBox
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_LISTBOX
16
17 #include "wx/listbox.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/dynarray.h"
21 #include "wx/log.h"
22 #include "wx/utils.h"
23 #include "wx/settings.h"
24 #include "wx/arrstr.h"
25 #endif
26
27 #ifdef __VMS
28 #define XtParent XTPARENT
29 #define XtDisplay XTDISPLAY
30 #endif
31
32 #ifdef __VMS__
33 #pragma message disable nosimpint
34 #endif
35 #include <Xm/List.h>
36 #ifdef __VMS__
37 #pragma message enable nosimpint
38 #endif
39 #include "wx/motif/private.h"
40
41 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
42
43 static void wxListBoxCallback(Widget w,
44 XtPointer clientData,
45 XmListCallbackStruct * cbs);
46
47 // ----------------------------------------------------------------------------
48 // wxSizeKeeper
49 // ----------------------------------------------------------------------------
50
51 // helper class to reduce code duplication
52 class wxSizeKeeper
53 {
54 int m_x, m_y;
55 wxWindow* m_w;
56 public:
57 wxSizeKeeper( wxWindow* w )
58 : m_w( w )
59 {
60 m_w->GetSize( &m_x, &m_y );
61 }
62
63 void Restore()
64 {
65 int x, y;
66
67 m_w->GetSize( &x, &y );
68 if( x != m_x || y != m_y )
69 m_w->SetSize( -1, -1, m_x, m_y );
70 }
71 };
72
73 // ============================================================================
74 // list box control implementation
75 // ============================================================================
76
77 // Listbox item
78 wxListBox::wxListBox()
79 {
80 m_noItems = 0;
81 }
82
83 bool wxListBox::Create(wxWindow *parent, wxWindowID id,
84 const wxPoint& pos,
85 const wxSize& size,
86 int n, const wxString choices[],
87 long style,
88 const wxValidator& validator,
89 const wxString& name)
90 {
91 if( !wxControl::CreateControl( parent, id, pos, size, style,
92 validator, name ) )
93 return false;
94
95 m_noItems = (unsigned int)n;
96 m_backgroundColour = * wxWHITE;
97
98 Widget parentWidget = (Widget) parent->GetClientWidget();
99 Display* dpy = XtDisplay(parentWidget);
100
101 Arg args[4];
102 int count = 0;
103 XtSetArg( args[count], XmNlistSizePolicy, XmCONSTANT ); ++count;
104 XtSetArg( args[count], XmNselectionPolicy,
105 ( m_windowStyle & wxLB_MULTIPLE ) ? XmMULTIPLE_SELECT :
106 ( m_windowStyle & wxLB_EXTENDED ) ? XmEXTENDED_SELECT :
107 XmBROWSE_SELECT );
108 ++count;
109 if( m_font.Ok() )
110 {
111 XtSetArg( args[count],
112 (String)wxFont::GetFontTag(), m_font.GetFontTypeC(dpy) );
113 ++count;
114 }
115 if( m_windowStyle & wxLB_ALWAYS_SB )
116 {
117 XtSetArg( args[count], XmNscrollBarDisplayPolicy, XmSTATIC );
118 ++count;
119 }
120
121 Widget listWidget =
122 XmCreateScrolledList(parentWidget,
123 wxConstCast(name.c_str(), char), args, count);
124
125 m_mainWidget = (WXWidget) listWidget;
126
127 Set(n, choices);
128
129 XtManageChild (listWidget);
130
131 wxSize best = GetBestSize();
132 if( size.x != -1 ) best.x = size.x;
133 if( size.y != -1 ) best.y = size.y;
134
135 XtAddCallback (listWidget,
136 XmNbrowseSelectionCallback,
137 (XtCallbackProc) wxListBoxCallback,
138 (XtPointer) this);
139 XtAddCallback (listWidget,
140 XmNextendedSelectionCallback,
141 (XtCallbackProc) wxListBoxCallback,
142 (XtPointer) this);
143 XtAddCallback (listWidget,
144 XmNmultipleSelectionCallback,
145 (XtCallbackProc) wxListBoxCallback,
146 (XtPointer) this);
147 XtAddCallback (listWidget,
148 XmNdefaultActionCallback,
149 (XtCallbackProc) wxListBoxCallback,
150 (XtPointer) this);
151
152 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
153 pos.x, pos.y, best.x, best.y);
154
155 ChangeBackgroundColour();
156
157 return true;
158 }
159
160 bool wxListBox::Create(wxWindow *parent, wxWindowID id,
161 const wxPoint& pos,
162 const wxSize& size,
163 const wxArrayString& choices,
164 long style,
165 const wxValidator& validator,
166 const wxString& name)
167 {
168 wxCArrayString chs(choices);
169 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
170 style, validator, name);
171 }
172
173 wxListBox::~wxListBox()
174 {
175 if( HasClientObjectData() )
176 m_clientDataDict.DestroyData();
177 }
178
179 void wxListBox::SetSelectionPolicy()
180 {
181 Widget listBox = (Widget)m_mainWidget;
182 Arg args[3];
183
184 XtSetArg( args[0], XmNlistSizePolicy, XmCONSTANT );
185
186 XtSetArg( args[1], XmNselectionPolicy,
187 ( m_windowStyle & wxLB_MULTIPLE ) ? XmMULTIPLE_SELECT :
188 ( m_windowStyle & wxLB_EXTENDED ) ? XmEXTENDED_SELECT :
189 XmBROWSE_SELECT );
190
191 XtSetValues( listBox, args, 2 );
192 }
193
194 void wxListBox::DoSetFirstItem( int N )
195 {
196 int count, length;
197
198 if (!IsValid(N))
199 return;
200
201 XtVaGetValues ((Widget) m_mainWidget,
202 XmNvisibleItemCount, &count,
203 XmNitemCount, &length,
204 NULL);
205 if ((N + count) >= length)
206 N = length - count;
207 XmListSetPos ((Widget) m_mainWidget, N + 1);
208 }
209
210 void wxListBox::Delete(unsigned int n)
211 {
212 Widget listBox = (Widget) m_mainWidget;
213
214 XmListDeletePos (listBox, n + 1);
215
216 m_clientDataDict.Delete(n, HasClientObjectData());
217 m_noItems --;
218 }
219
220 int wxListBox::DoAppend(const wxString& item)
221 {
222 Widget listBox = (Widget) m_mainWidget;
223
224 int n;
225 XtVaGetValues (listBox, XmNitemCount, &n, NULL);
226 wxXmString text( item );
227 // XmListAddItem(listBox, text, n + 1);
228 XmListAddItemUnselected (listBox, text(), 0);
229
230 // It seems that if the list is cleared, we must re-ask for
231 // selection policy!!
232 SetSelectionPolicy();
233
234 m_noItems ++;
235
236 return GetCount() - 1;
237 }
238
239 void wxListBox::DoSetItems(const wxArrayString& items, void** clientData)
240 {
241 Widget listBox = (Widget) m_mainWidget;
242
243 if( HasClientObjectData() )
244 m_clientDataDict.DestroyData();
245
246 XmString *text = new XmString[items.GetCount()];
247 unsigned int i;
248 for (i = 0; i < items.GetCount(); ++i)
249 text[i] = wxStringToXmString (items[i]);
250
251 if ( clientData )
252 for (i = 0; i < items.GetCount(); ++i)
253 m_clientDataDict.Set(i, (wxClientData*)clientData[i], false);
254
255 XmListAddItems (listBox, text, items.GetCount(), 0);
256 for (i = 0; i < items.GetCount(); i++)
257 XmStringFree (text[i]);
258 delete[] text;
259
260 // It seems that if the list is cleared, we must re-ask for
261 // selection policy!!
262 SetSelectionPolicy();
263
264 m_noItems = items.GetCount();
265 }
266
267 int wxDoFindStringInList(Widget w, const wxString& s)
268 {
269 wxXmString str( s );
270 int *positions = NULL;
271 int no_positions = 0;
272 bool success = XmListGetMatchPos (w, str(),
273 &positions, &no_positions);
274
275 if (success)
276 {
277 int pos = positions[0];
278 if (positions)
279 XtFree ((char *) positions);
280 return pos - 1;
281 }
282 else
283 return -1;
284 }
285
286 int wxListBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const
287 {
288 // FIXME: back to base class for not supported value of bCase
289
290 return wxDoFindStringInList( (Widget)m_mainWidget, s );
291 }
292
293 void wxListBox::Clear()
294 {
295 if (m_noItems <= 0)
296 return;
297
298 wxSizeKeeper sk( this );
299 Widget listBox = (Widget) m_mainWidget;
300
301 XmListDeleteAllItems (listBox);
302 if( HasClientObjectData() )
303 m_clientDataDict.DestroyData();
304
305 sk.Restore();
306
307 m_noItems = 0;
308 }
309
310 void wxListBox::DoSetSelection(int N, bool select)
311 {
312 m_inSetValue = true;
313 if (select)
314 {
315 #if 0
316 if (m_windowStyle & wxLB_MULTIPLE)
317 {
318 int *selections = NULL;
319 int n = GetSelections (&selections);
320
321 // This hack is supposed to work, to make it possible
322 // to select more than one item, but it DOESN'T under Motif 1.1.
323
324 XtVaSetValues ((Widget) m_mainWidget,
325 XmNselectionPolicy, XmMULTIPLE_SELECT,
326 NULL);
327
328 int i;
329 for (i = 0; i < n; i++)
330 XmListSelectPos ((Widget) m_mainWidget,
331 selections[i] + 1, False);
332
333 XmListSelectPos ((Widget) m_mainWidget, N + 1, False);
334
335 XtVaSetValues ((Widget) m_mainWidget,
336 XmNselectionPolicy, XmEXTENDED_SELECT,
337 NULL);
338 }
339 else
340 #endif // 0
341 XmListSelectPos ((Widget) m_mainWidget, N + 1, False);
342
343 }
344 else
345 XmListDeselectPos ((Widget) m_mainWidget, N + 1);
346
347 m_inSetValue = false;
348 }
349
350 bool wxListBox::IsSelected(int N) const
351 {
352 // In Motif, no simple way to determine if the item is selected.
353 wxArrayInt theSelections;
354 int count = GetSelections (theSelections);
355 if (count == 0)
356 return false;
357 else
358 {
359 int j;
360 for (j = 0; j < count; j++)
361 if (theSelections[j] == N)
362 return true;
363 }
364 return false;
365 }
366
367 void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
368 {
369 m_clientDataDict.Set(n, clientData, false);
370 }
371
372 wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
373 {
374 return m_clientDataDict.Get(n);
375 }
376
377 void *wxListBox::DoGetItemClientData(unsigned int n) const
378 {
379 return (void*)m_clientDataDict.Get(n);
380 }
381
382 void wxListBox::DoSetItemClientData(unsigned int n, void *Client_data)
383 {
384 m_clientDataDict.Set(n, (wxClientData*)Client_data, false);
385 }
386
387 // Return number of selections and an array of selected integers
388 int wxListBox::GetSelections(wxArrayInt& aSelections) const
389 {
390 aSelections.Empty();
391
392 Widget listBox = (Widget) m_mainWidget;
393 int *posList = NULL;
394 int posCnt = 0;
395 bool flag = XmListGetSelectedPos (listBox, &posList, &posCnt);
396 if (flag)
397 {
398 if (posCnt > 0)
399 {
400 aSelections.Alloc(posCnt);
401
402 int i;
403 for (i = 0; i < posCnt; i++)
404 aSelections.Add(posList[i] - 1);
405
406 XtFree ((char *) posList);
407 return posCnt;
408 }
409 else
410 return 0;
411 }
412 else
413 return 0;
414 }
415
416 // Get single selection, for single choice list items
417 int wxDoGetSelectionInList(Widget listBox)
418 {
419 int *posList = NULL;
420 int posCnt = 0;
421 bool flag = XmListGetSelectedPos (listBox, &posList, &posCnt);
422 if (flag)
423 {
424 int id = -1;
425 if (posCnt > 0)
426 id = posList[0] - 1;
427 XtFree ((char *) posList);
428 return id;
429 }
430 else
431 return -1;
432 }
433
434 int wxListBox::GetSelection() const
435 {
436 return wxDoGetSelectionInList((Widget) m_mainWidget);
437 }
438
439 // Find string for position
440 wxString wxDoGetStringInList( Widget listBox, int n )
441 {
442 XmString *strlist;
443 int count;
444 XtVaGetValues( listBox,
445 XmNitemCount, &count,
446 XmNitems, &strlist,
447 NULL );
448 if( n < count && n >= 0 )
449 return wxXmStringToString( strlist[n] );
450 else
451 return wxEmptyString;
452 }
453
454 wxString wxListBox::GetString(unsigned int n) const
455 {
456 return wxDoGetStringInList( (Widget)m_mainWidget, n );
457 }
458
459 void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
460 {
461 Widget listBox = (Widget) m_mainWidget;
462
463 XmString *text = new XmString[items.GetCount()];
464 unsigned int i;
465 // Steve Hammes: Motif 1.1 compatibility
466 // #if XmVersion > 1100
467 // Corrected by Sergey Krasnov from Steve Hammes' code
468 #if XmVersion > 1001
469 for (i = 0; i < items.GetCount(); i++)
470 text[i] = wxStringToXmString(items[i]);
471 XmListAddItemsUnselected(listBox, text, items.GetCount(), pos+1);
472 #else
473 for (i = 0; i < items.GetCount(); i++)
474 {
475 text[i] = wxStringToXmString(items[i]);
476 // Another Sergey correction
477 XmListAddItemUnselected(listBox, text[i], pos+i+1);
478 }
479 #endif
480 for (i = 0; i < items.GetCount(); i++)
481 XmStringFree(text[i]);
482 delete[] text;
483
484 // It seems that if the list is cleared, we must re-ask for
485 // selection policy!!
486 SetSelectionPolicy();
487
488 m_noItems += items.GetCount();
489 }
490
491 void wxListBox::SetString(unsigned int n, const wxString& s)
492 {
493 wxSizeKeeper sk( this );
494 Widget listBox = (Widget) m_mainWidget;
495
496 wxXmString text( s );
497
498 // delete the item and add it again.
499 // FIXME isn't there a way to change it in place?
500 XmListDeletePos (listBox, n+1);
501 XmListAddItem (listBox, text(), n+1);
502
503 sk.Restore();
504 }
505
506 void wxListBox::Command (wxCommandEvent & event)
507 {
508 if (event.GetExtraLong())
509 SetSelection (event.GetInt());
510 else
511 {
512 Deselect (event.GetInt());
513 return;
514 }
515 ProcessCommand (event);
516 }
517
518 void wxListBoxCallback (Widget WXUNUSED(w), XtPointer clientData,
519 XmListCallbackStruct * cbs)
520 {
521 wxListBox *item = (wxListBox *) clientData;
522
523 if (item->InSetValue())
524 return;
525
526 wxEventType evtType;
527
528 if( cbs->reason == XmCR_DEFAULT_ACTION )
529 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
530 else
531 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
532
533 int n = cbs->item_position - 1;
534 wxCommandEvent event (evtType, item->GetId());
535 if ( item->HasClientObjectData() )
536 event.SetClientObject( item->GetClientObject(n) );
537 else if ( item->HasClientUntypedData() )
538 event.SetClientData( item->GetClientData(n) );
539 event.SetInt(n);
540 event.SetExtraLong(true);
541 event.SetEventObject(item);
542 event.SetString( item->GetString( n ) );
543
544 int x = -1;
545 if( NULL != cbs->event && cbs->event->type == ButtonRelease )
546 {
547 XButtonEvent* evt = (XButtonEvent*)cbs->event;
548
549 x = evt->x;
550 }
551
552 switch (cbs->reason)
553 {
554 case XmCR_MULTIPLE_SELECT:
555 case XmCR_BROWSE_SELECT:
556 #if wxUSE_CHECKLISTBOX
557 item->DoToggleItem( n, x );
558 #endif
559 case XmCR_DEFAULT_ACTION:
560 item->GetEventHandler()->ProcessEvent(event);
561 break;
562 case XmCR_EXTENDED_SELECT:
563 switch (cbs->selection_type)
564 {
565 case XmINITIAL:
566 case XmADDITION:
567 case XmMODIFICATION:
568 item->DoToggleItem( n, x );
569 item->GetEventHandler()->ProcessEvent(event);
570 break;
571 }
572 break;
573 }
574 }
575
576 WXWidget wxListBox::GetTopWidget() const
577 {
578 return (WXWidget) XtParent( (Widget) m_mainWidget );
579 }
580
581 void wxListBox::ChangeBackgroundColour()
582 {
583 wxWindow::ChangeBackgroundColour();
584
585 Widget parent = XtParent ((Widget) m_mainWidget);
586 Widget hsb, vsb;
587
588 XtVaGetValues (parent,
589 XmNhorizontalScrollBar, &hsb,
590 XmNverticalScrollBar, &vsb,
591 NULL);
592
593 /* TODO: should scrollbars be affected? Should probably have separate
594 * function to change them (by default, taken from wxSystemSettings)
595 */
596 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
597 wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, true);
598 wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, true);
599
600 XtVaSetValues (hsb,
601 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)),
602 NULL);
603 XtVaSetValues (vsb,
604 XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)),
605 NULL);
606
607 // MBN: why change parent's background? It looks really ugly.
608 // wxDoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
609 }
610
611 void wxListBox::ChangeForegroundColour()
612 {
613 wxWindow::ChangeForegroundColour();
614
615 Widget parent = XtParent ((Widget) m_mainWidget);
616 Widget hsb, vsb;
617
618 XtVaGetValues(parent,
619 XmNhorizontalScrollBar, &hsb,
620 XmNverticalScrollBar, &vsb,
621 NULL);
622
623 /* TODO: should scrollbars be affected? Should probably have separate
624 function to change them (by default, taken from wxSystemSettings)
625
626 wxDoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
627 wxDoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
628 wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
629 */
630 }
631
632 unsigned int wxListBox::GetCount() const
633 {
634 return m_noItems;
635 }
636
637 #define LIST_SCROLL_SPACING 6
638
639 wxSize wxDoGetListBoxBestSize( Widget listWidget, const wxWindow* window )
640 {
641 int max;
642 Dimension spacing, highlight, xmargin, ymargin, shadow;
643 int width = 0;
644 int x, y;
645
646 XtVaGetValues( listWidget,
647 XmNitemCount, &max,
648 XmNlistSpacing, &spacing,
649 XmNhighlightThickness, &highlight,
650 XmNlistMarginWidth, &xmargin,
651 XmNlistMarginHeight, &ymargin,
652 XmNshadowThickness, &shadow,
653 NULL );
654
655 for( size_t i = 0; i < (size_t)max; ++i )
656 {
657 window->GetTextExtent( wxDoGetStringInList( listWidget, i ), &x, &y );
658 width = wxMax( width, x );
659 }
660
661 // use some arbitrary value if there are no strings
662 if( width == 0 )
663 width = 100;
664
665 // get my
666 window->GetTextExtent( "v", &x, &y );
667
668 // make it a little larger than widest string, plus the scrollbar
669 width += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X )
670 + 2 * highlight + LIST_SCROLL_SPACING + 2 * xmargin + 2 * shadow;
671
672 // at least 3 items, at most 10
673 int height = wxMax( 3, wxMin( 10, max ) ) *
674 ( y + spacing + 2 * highlight ) + 2 * ymargin + 2 * shadow;
675
676 return wxSize( width, height );
677 }
678
679 wxSize wxListBox::DoGetBestSize() const
680 {
681 return wxDoGetListBoxBestSize( (Widget)m_mainWidget, this );
682 }
683
684 #endif // wxUSE_LISTBOX