]> git.saurik.com Git - wxWidgets.git/blame - src/msw/nativdlg.cpp
fixed crash on startup under GTK
[wxWidgets.git] / src / msw / nativdlg.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: nativdlg.cpp
3// Purpose: Native dialog loading code (part of wxWindow)
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
cc2b7472
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
cc2b7472 21 #pragma implementation
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
cc2b7472 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
cc2b7472
VZ
32 #include <stdio.h>
33
34 #include "wx/wx.h"
2bda0e17
KB
35#endif
36
b39dbf34 37#if defined(__WIN95__)
2bda0e17 38#include "wx/spinbutt.h"
3372145d 39#endif
2bda0e17
KB
40#include "wx/msw/private.h"
41
cc2b7472
VZ
42// ---------------------------------------------------------------------------
43// global functions
44// ---------------------------------------------------------------------------
45
cc2b7472
VZ
46extern LONG APIENTRY _EXPORT wxDlgProc(HWND hWnd, UINT message,
47 WPARAM wParam, LPARAM lParam);
48
49// ===========================================================================
50// implementation
51// ===========================================================================
2bda0e17 52
debe6624 53bool wxWindow::LoadNativeDialog(wxWindow* parent, wxWindowID& id)
2bda0e17 54{
cc2b7472 55 m_windowId = id;
b225f659
VZ
56
57 wxWindowCreationHook hook(this);
cc2b7472
VZ
58 m_hWnd = (WXHWND)::CreateDialog((HINSTANCE)wxGetInstance(),
59 MAKEINTRESOURCE(id),
60 parent ? (HWND)parent->GetHWND() : 0,
61 (DLGPROC) wxDlgProc);
2bda0e17 62
cc2b7472
VZ
63 if ( !m_hWnd )
64 return FALSE;
2bda0e17 65
cc2b7472 66 SubclassWin(GetHWND());
2bda0e17 67
cc2b7472
VZ
68 if ( parent )
69 parent->AddChild(this);
70 else
71 wxTopLevelWindows.Append(this);
2bda0e17 72
cc2b7472 73 // Enumerate all children
2bda0e17
KB
74 HWND hWndNext;
75 hWndNext = ::GetWindow((HWND) m_hWnd, GW_CHILD);
76
cc2b7472 77 if (hWndNext)
196be0f1 78 CreateWindowFromHWND(this, (WXHWND) hWndNext);
2bda0e17 79
57c208c5 80 while (hWndNext != (HWND) NULL)
2bda0e17 81 {
cc2b7472
VZ
82 hWndNext = ::GetWindow(hWndNext, GW_HWNDNEXT);
83 if (hWndNext)
196be0f1 84 CreateWindowFromHWND(this, (WXHWND) hWndNext);
2bda0e17
KB
85 }
86
cc2b7472 87 return TRUE;
2bda0e17
KB
88}
89
90bool wxWindow::LoadNativeDialog(wxWindow* parent, const wxString& name)
91{
cc2b7472 92 SetName(name);
2bda0e17 93
b225f659 94 wxWindowCreationHook hook(this);
cc2b7472
VZ
95 m_hWnd = (WXHWND)::CreateDialog((HINSTANCE) wxGetInstance(),
96 name.c_str(),
97 parent ? (HWND)parent->GetHWND() : 0,
98 (DLGPROC)wxDlgProc);
2bda0e17 99
cc2b7472
VZ
100 if ( !m_hWnd )
101 return FALSE;
2bda0e17 102
cc2b7472 103 SubclassWin(GetHWND());
2bda0e17 104
cc2b7472
VZ
105 if ( parent )
106 parent->AddChild(this);
107 else
108 wxTopLevelWindows.Append(this);
2bda0e17 109
f032bf3d
JS
110 // Enumerate all children
111 HWND hWndNext;
112 hWndNext = ::GetWindow((HWND) m_hWnd, GW_CHILD);
113
f032bf3d 114 if (hWndNext)
196be0f1 115 CreateWindowFromHWND(this, (WXHWND) hWndNext);
f032bf3d
JS
116
117 while (hWndNext != (HWND) NULL)
118 {
119 hWndNext = ::GetWindow(hWndNext, GW_HWNDNEXT);
120 if (hWndNext)
196be0f1 121 CreateWindowFromHWND(this, (WXHWND) hWndNext);
f032bf3d 122 }
2bda0e17 123
cc2b7472 124 return TRUE;
2bda0e17
KB
125}
126
cc2b7472
VZ
127// ---------------------------------------------------------------------------
128// look for child by id
129// ---------------------------------------------------------------------------
130
131wxWindow* wxWindow::GetWindowChild1(wxWindowID id)
2bda0e17 132{
cc2b7472
VZ
133 if ( m_windowId == id )
134 return this;
135
222ed1d6 136 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
cc2b7472
VZ
137 while ( node )
138 {
139 wxWindow* child = node->GetData();
140 wxWindow* win = child->GetWindowChild1(id);
141 if ( win )
142 return win;
143
144 node = node->GetNext();
145 }
146
147 return NULL;
2bda0e17
KB
148}
149
cc2b7472 150wxWindow* wxWindow::GetWindowChild(wxWindowID id)
2bda0e17 151{
cc2b7472
VZ
152 wxWindow* win = GetWindowChild1(id);
153 if ( !win )
154 {
155 HWND hWnd = ::GetDlgItem((HWND) GetHWND(), id);
156
157 if (hWnd)
158 {
159 wxWindow* child = CreateWindowFromHWND(this, (WXHWND) hWnd);
160 if (child)
161 {
162 child->AddChild(this);
163 return child;
164 }
165 }
166 }
167
168 return NULL;
2bda0e17
KB
169}
170
cc2b7472
VZ
171// ---------------------------------------------------------------------------
172// create wxWin window from a native HWND
173// ---------------------------------------------------------------------------
2bda0e17
KB
174
175wxWindow* wxWindow::CreateWindowFromHWND(wxWindow* parent, WXHWND hWnd)
176{
cc2b7472
VZ
177 wxString str(wxGetWindowClass(hWnd));
178 str.UpperCase();
2bda0e17 179
cc2b7472
VZ
180 long id = wxGetWindowId(hWnd);
181 long style = GetWindowLong((HWND) hWnd, GWL_STYLE);
2bda0e17 182
cc2b7472 183 wxWindow* win = NULL;
2bda0e17 184
223d09f6 185 if (str == wxT("BUTTON"))
cc2b7472
VZ
186 {
187 int style1 = (style & 0xFF);
188 if ((style1 == BS_3STATE) || (style1 == BS_AUTO3STATE) || (style1 == BS_AUTOCHECKBOX) ||
189 (style1 == BS_CHECKBOX))
190 {
191 win = new wxCheckBox;
192 }
193 else if ((style1 == BS_AUTORADIOBUTTON) || (style1 == BS_RADIOBUTTON))
194 {
195 win = new wxRadioButton;
196 }
8c18da2e 197#if wxUSE_BMPBUTTON
2432b92d 198#if defined(__WIN32__) && defined(BS_BITMAP)
cc2b7472
VZ
199 else if (style & BS_BITMAP)
200 {
201 // TODO: how to find the bitmap?
202 win = new wxBitmapButton;
223d09f6 203 wxLogError(wxT("Have not yet implemented bitmap button as BS_BITMAP button."));
cc2b7472 204 }
2bda0e17 205#endif
cc2b7472
VZ
206 else if (style1 == BS_OWNERDRAW)
207 {
208 // TODO: how to find the bitmap?
209 // TODO: can't distinguish between bitmap button and bitmap static.
210 // Change implementation of wxStaticBitmap to SS_BITMAP.
211 // PROBLEM: this assumes that we're using resource-based bitmaps.
212 // So maybe need 2 implementations of bitmap buttons/static controls,
213 // with a switch in the drawing code. Call default proc if BS_BITMAP.
214 win = new wxBitmapButton;
215 }
8c18da2e 216#endif
cc2b7472
VZ
217 else if ((style1 == BS_PUSHBUTTON) || (style1 == BS_DEFPUSHBUTTON))
218 {
219 win = new wxButton;
220 }
221 else if (style1 == BS_GROUPBOX)
222 {
223 win = new wxStaticBox;
224 }
225 else
226 {
92aff599 227 wxLogError(wxT("Don't know what kind of button this is: id = %ld"),
cc2b7472
VZ
228 id);
229 }
230 }
223d09f6 231 else if (str == wxT("COMBOBOX"))
cc2b7472
VZ
232 {
233 win = new wxComboBox;
234 }
235 // TODO: Problem if the user creates a multiline - but not rich text - text control,
236 // since wxWin assumes RichEdit control for this. Should have m_isRichText in
237 // wxTextCtrl. Also, convert as much of the window style as is necessary
238 // for correct functioning.
239 // Could have wxWindow::AdoptAttributesFromHWND(WXHWND)
240 // to be overridden by each control class.
223d09f6 241 else if (str == wxT("EDIT"))
cc2b7472
VZ
242 {
243 win = new wxTextCtrl;
244 }
223d09f6 245 else if (str == wxT("LISTBOX"))
cc2b7472
VZ
246 {
247 win = new wxListBox;
248 }
223d09f6 249 else if (str == wxT("SCROLLBAR"))
cc2b7472
VZ
250 {
251 win = new wxScrollBar;
252 }
b39dbf34 253#if defined(__WIN95__) && wxUSE_SPINBTN
223d09f6 254 else if (str == wxT("MSCTLS_UPDOWN32"))
cc2b7472
VZ
255 {
256 win = new wxSpinButton;
257 }
2bda0e17 258#endif
1e6feb95 259#if wxUSE_SLIDER
223d09f6 260 else if (str == wxT("MSCTLS_TRACKBAR32"))
cc2b7472
VZ
261 {
262 // Need to ascertain if it's horiz or vert
263 win = new wxSlider;
264 }
1e6feb95 265#endif // wxUSE_SLIDER
223d09f6 266 else if (str == wxT("STATIC"))
cc2b7472
VZ
267 {
268 int style1 = (style & 0xFF);
269
4676948b
JS
270 if ((style1 == SS_LEFT) || (style1 == SS_RIGHT)
271#ifndef __WXWINCE__
272 || (style1 == SS_SIMPLE)
273#endif
274 )
cc2b7472 275 win = new wxStaticText;
8c18da2e 276#if wxUSE_STATBMP
2432b92d 277#if defined(__WIN32__) && defined(BS_BITMAP)
cc2b7472
VZ
278 else if (style1 == SS_BITMAP)
279 {
280 win = new wxStaticBitmap;
2bda0e17 281
cc2b7472 282 // Help! this doesn't correspond with the wxWin implementation.
223d09f6 283 wxLogError(wxT("Please make SS_BITMAP statics into owner-draw buttons."));
cc2b7472 284 }
2bda0e17 285#endif
8c18da2e 286#endif /* wxUSE_STATBMP */
cc2b7472
VZ
287 }
288 else
289 {
223d09f6 290 wxString msg(wxT("Don't know how to convert from Windows class "));
cc2b7472
VZ
291 msg += str;
292 wxLogError(msg);
293 }
294
295 if (win)
296 {
297 parent->AddChild(win);
298 win->SetEventHandler(win);
299 win->SetHWND(hWnd);
300 win->SetId(id);
301 win->SubclassWin(hWnd);
302 win->AdoptAttributesFromHWND();
303 win->SetupColours();
cc2b7472 304 }
3ca6a5f0
BP
305
306 return win;
2bda0e17
KB
307}
308
309// Make sure the window style (etc.) reflects the HWND style (roughly)
310void wxWindow::AdoptAttributesFromHWND(void)
311{
cc2b7472
VZ
312 HWND hWnd = (HWND) GetHWND();
313 long style = GetWindowLong((HWND) hWnd, GWL_STYLE);
2bda0e17 314
cc2b7472
VZ
315 if (style & WS_VSCROLL)
316 m_windowStyle |= wxVSCROLL;
317 if (style & WS_HSCROLL)
318 m_windowStyle |= wxHSCROLL;
2bda0e17
KB
319}
320