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