Misc. Motif; removed duplicate wxICON; variant compile fix; added wxString form
[wxWidgets.git] / src / motif / statbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: statbox.cpp
3 // Purpose: wxStaticBox
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 #ifdef __GNUG__
13 #pragma implementation "statbox.h"
14 #endif
15
16 #include "wx/statbox.h"
17 #include "wx/utils.h"
18
19 #include <Xm/Frame.h>
20 #include <Xm/Form.h>
21 #include <Xm/Label.h>
22 #include <Xm/LabelG.h>
23
24 #include <wx/motif/private.h>
25
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
28
29 BEGIN_EVENT_TABLE(wxStaticBox, wxControl)
30 EVT_ERASE_BACKGROUND(wxStaticBox::OnEraseBackground)
31 END_EVENT_TABLE()
32
33 #endif
34
35 /*
36 * Static box
37 */
38
39 wxStaticBox::wxStaticBox()
40 {
41 m_formWidget = (WXWidget) 0;
42 m_labelWidget = (WXWidget) 0;
43 }
44
45 bool wxStaticBox::Create(wxWindow *parent, wxWindowID id,
46 const wxString& label,
47 const wxPoint& pos,
48 const wxSize& size,
49 long style,
50 const wxString& name)
51 {
52 m_formWidget = (WXWidget) 0;
53 m_labelWidget = (WXWidget) 0;
54
55 SetName(name);
56
57 if (parent) parent->AddChild(this);
58
59 if ( id == -1 )
60 m_windowId = (int)NewControlId();
61 else
62 m_windowId = id;
63
64 m_windowStyle = style;
65
66 bool hasLabel = (!label.IsNull() && !label.IsEmpty()) ;
67
68 Widget parentWidget = (Widget) parent->GetClientWidget();
69
70 Widget formWidget = XtVaCreateManagedWidget ((char*) (const char*) name,
71 xmFormWidgetClass, parentWidget,
72 XmNmarginHeight, 0,
73 XmNmarginWidth, 0,
74 NULL);
75
76
77 if (hasLabel)
78 {
79 wxString label1(wxStripMenuCodes(label));
80 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
81 m_labelWidget = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) label1,
82 xmLabelWidgetClass, formWidget,
83 XmNlabelString, text,
84 NULL);
85 XmStringFree (text);
86 }
87
88 Widget frameWidget = XtVaCreateManagedWidget ("frame",
89 xmFrameWidgetClass, formWidget,
90 XmNshadowType, XmSHADOW_IN,
91 // XmNmarginHeight, 0,
92 // XmNmarginWidth, 0,
93 NULL);
94
95 if (hasLabel)
96 XtVaSetValues ((Widget) m_labelWidget,
97 XmNtopAttachment, XmATTACH_FORM,
98 XmNleftAttachment, XmATTACH_FORM,
99 XmNrightAttachment, XmATTACH_FORM,
100 XmNalignment, XmALIGNMENT_BEGINNING,
101 NULL);
102
103 XtVaSetValues (frameWidget,
104 XmNtopAttachment, hasLabel ? XmATTACH_WIDGET : XmATTACH_FORM,
105 XmNtopWidget, hasLabel ? (Widget) m_labelWidget : formWidget,
106 XmNbottomAttachment, XmATTACH_FORM,
107 XmNleftAttachment, XmATTACH_FORM,
108 XmNrightAttachment, XmATTACH_FORM,
109 NULL);
110
111 m_mainWidget = (Widget) formWidget;
112
113 SetCanAddEventHandler(TRUE);
114 AttachWidget (parent, m_mainWidget, (WXWidget) frameWidget, pos.x, pos.y, size.x, size.y);
115 ChangeColour(m_mainWidget);
116
117 return TRUE;
118 }
119
120 void wxStaticBox::SetLabel(const wxString& label)
121 {
122 if (!m_labelWidget)
123 return;
124
125 if (!label.IsNull())
126 {
127 wxString label1(wxStripMenuCodes(label));
128
129 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
130 XtVaSetValues ((Widget) m_labelWidget,
131 XmNlabelString, text,
132 XmNlabelType, XmSTRING,
133 NULL);
134 XmStringFree (text);
135 }
136 }
137
138 wxString wxStaticBox::GetLabel() const
139 {
140 if (!m_labelWidget)
141 return wxEmptyString;
142
143 XmString text = 0;
144 char *s;
145 XtVaGetValues ((Widget) m_labelWidget,
146 XmNlabelString, &text,
147 NULL);
148
149 if (!text)
150 return wxEmptyString;
151
152 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
153 {
154 wxString str(s);
155 XtFree (s);
156 return str;
157 }
158 else
159 {
160 return wxEmptyString;
161 }
162 }
163
164 void wxStaticBox::SetSize(int x, int y, int width, int height, int sizeFlags)
165 {
166 wxControl::SetSize (x, y, width, height, sizeFlags);
167
168 if (m_labelWidget)
169 {
170 Dimension xx, yy;
171 XtVaGetValues ((Widget) m_labelWidget, XmNwidth, &xx, XmNheight, &yy, NULL);
172
173 if (width > -1)
174 XtVaSetValues ((Widget) m_mainWidget, XmNwidth, width,
175 NULL);
176 if (height > -1)
177 XtVaSetValues ((Widget) m_mainWidget, XmNheight, height - yy,
178 NULL);
179 }
180 }
181