1/////////////////////////////////////////////////////////////////////////////
2// Name:        src/motif/statbox.cpp
3// Purpose:     wxStaticBox
4// Author:      Julian Smart
5// Modified by:
6// Created:     17/09/98
7// RCS-ID:      $Id: statbox.cpp 50982 2008-01-01 20:38:33Z VZ $
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#include "wx/statbox.h"
16
17#ifndef WX_PRECOMP
18    #include "wx/utils.h"
19#endif
20
21#ifdef __VMS__
22#pragma message disable nosimpint
23#endif
24#include <Xm/Frame.h>
25#include <Xm/Label.h>
26#ifdef __VMS__
27#pragma message enable nosimpint
28#endif
29
30#include "wx/motif/private.h"
31
32IMPLEMENT_DYNAMIC_CLASS(wxStaticBox, wxControl)
33
34BEGIN_EVENT_TABLE(wxStaticBox, wxControl)
35//EVT_ERASE_BACKGROUND(wxStaticBox::OnEraseBackground)
36END_EVENT_TABLE()
37
38// ----------------------------------------------------------------------------
39// wxXmSizeKeeper
40// ----------------------------------------------------------------------------
41
42// helper class to reduce code duplication
43class wxXmSizeKeeper
44{
45    Dimension m_x, m_y;
46    Widget m_widget;
47public:
48    wxXmSizeKeeper( Widget w )
49        : m_widget( w )
50    {
51        XtVaGetValues( m_widget,
52                       XmNwidth, &m_x,
53                       XmNheight, &m_y,
54                       NULL );
55    }
56
57    void Restore()
58    {
59        int x, y;
60
61        XtVaGetValues( m_widget,
62                       XmNwidth, &x,
63                       XmNheight, &y,
64                       NULL );
65        if( x != m_x || y != m_y )
66            XtVaSetValues( m_widget,
67                           XmNwidth, m_x,
68                           XmNheight, m_y,
69                           NULL );
70    }
71};
72
73/*
74 * Static box
75 */
76
77wxStaticBox::wxStaticBox()
78{
79    m_labelWidget = (WXWidget) 0;
80}
81
82bool wxStaticBox::Create(wxWindow *parent, wxWindowID id,
83           const wxString& label,
84           const wxPoint& pos,
85           const wxSize& size,
86           long style,
87           const wxString& name)
88{
89    if( !CreateControl( parent, id, pos, size, style,
90                        wxDefaultValidator, name ) )
91        return false;
92
93    m_labelWidget = (WXWidget) 0;
94    Widget parentWidget = (Widget) parent->GetClientWidget();
95
96    m_mainWidget = XtVaCreateManagedWidget ("staticboxframe",
97            xmFrameWidgetClass, parentWidget,
98            // MBN: why override default?
99            // XmNshadowType, XmSHADOW_IN,
100            NULL);
101
102    if (!label.empty())
103    {
104        wxString label1(GetLabelText(label));
105        wxXmString text(label1);
106        Display* dpy = XtDisplay( parentWidget );
107
108        m_labelWidget = (WXWidget) XtVaCreateManagedWidget ("staticboxlabel",
109                xmLabelWidgetClass, (Widget)m_mainWidget,
110                wxFont::GetFontTag(), m_font.GetFontTypeC(dpy),
111                XmNlabelString, text(),
112#if wxCHECK_MOTIF_VERSION( 2, 0 )
113                XmNframeChildType, XmFRAME_TITLE_CHILD,
114#else
115                XmNchildType, XmFRAME_TITLE_CHILD,
116#endif
117                NULL);
118    }
119
120    AttachWidget (parent, m_mainWidget, NULL, pos.x, pos.y, size.x, size.y);
121    ChangeBackgroundColour();
122
123    return true;
124}
125
126wxStaticBox::~wxStaticBox()
127{
128   DetachWidget(m_mainWidget);
129   XtDestroyWidget((Widget) m_mainWidget);
130
131   m_mainWidget = (WXWidget) 0;
132   m_labelWidget = (WXWidget) 0;
133}
134
135void wxStaticBox::SetLabel( const wxString& label )
136{
137    wxXmSizeKeeper sk( (Widget)GetMainWidget() );
138
139    wxStaticBoxBase::SetLabel( label );
140
141    sk.Restore();
142}
143
144void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
145{
146    Dimension shadow, border;
147
148    XtVaGetValues( (Widget) GetMainWidget(),
149                   XmNshadowThickness, &shadow,
150                   XmNborderWidth, &border,
151                   NULL);
152
153    *borderOther = shadow + border;
154
155    if( GetLabelWidget() )
156    {
157        XtWidgetGeometry preferred;
158        XtQueryGeometry( (Widget) GetLabelWidget(), NULL, &preferred );
159
160        *borderTop = preferred.height;
161    }
162    else
163    {
164        *borderTop = shadow;
165    }
166}
167