]>
Commit | Line | Data |
---|---|---|
93cf77c0 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: frame.cpp | |
3 | // Purpose: wxFrame | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "frame.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/frame.h" | |
17 | #include "wx/statusbr.h" | |
18 | #include "wx/toolbar.h" | |
19 | #include "wx/menuitem.h" | |
34138703 JS |
20 | #include "wx/menu.h" |
21 | #include "wx/dcclient.h" | |
22 | #include "wx/dialog.h" | |
23 | #include "wx/settings.h" | |
24 | #include "wx/app.h" | |
93cf77c0 JS |
25 | |
26 | extern wxList wxModelessWindows; | |
27 | extern wxList wxPendingDelete; | |
28 | ||
29 | #if !USE_SHARED_LIBRARY | |
30 | BEGIN_EVENT_TABLE(wxFrame, wxWindow) | |
31 | EVT_SIZE(wxFrame::OnSize) | |
32 | EVT_ACTIVATE(wxFrame::OnActivate) | |
33 | EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight) | |
34 | EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged) | |
35 | EVT_IDLE(wxFrame::OnIdle) | |
36 | EVT_CLOSE(wxFrame::OnCloseWindow) | |
37 | END_EVENT_TABLE() | |
38 | ||
39 | IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow) | |
40 | #endif | |
41 | ||
47d67540 | 42 | #if wxUSE_NATIVE_STATUSBAR |
93cf77c0 JS |
43 | bool wxFrame::m_useNativeStatusBar = TRUE; |
44 | #else | |
45 | bool wxFrame::m_useNativeStatusBar = FALSE; | |
46 | #endif | |
47 | ||
48 | wxFrame::wxFrame() | |
49 | { | |
50 | m_frameToolBar = NULL ; | |
51 | m_frameMenuBar = NULL; | |
52 | m_frameStatusBar = NULL; | |
53 | ||
54 | m_windowParent = NULL; | |
55 | m_iconized = FALSE; | |
56 | } | |
57 | ||
58 | bool wxFrame::Create(wxWindow *parent, | |
59 | wxWindowID id, | |
60 | const wxString& title, | |
61 | const wxPoint& pos, | |
62 | const wxSize& size, | |
63 | long style, | |
64 | const wxString& name) | |
65 | { | |
66 | if (!parent) | |
67 | wxTopLevelWindows.Append(this); | |
68 | ||
69 | SetName(name); | |
70 | m_windowStyle = style; | |
71 | m_frameMenuBar = NULL; | |
72 | m_frameToolBar = NULL ; | |
73 | m_frameStatusBar = NULL; | |
74 | ||
75 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); | |
76 | ||
77 | if ( id > -1 ) | |
78 | m_windowId = id; | |
79 | else | |
80 | m_windowId = (int)NewControlId(); | |
81 | ||
82 | if (parent) parent->AddChild(this); | |
83 | ||
84 | wxModelessWindows.Append(this); | |
85 | ||
86 | // TODO: create frame. | |
87 | ||
88 | return FALSE; | |
89 | } | |
90 | ||
91 | wxFrame::~wxFrame() | |
92 | { | |
93 | wxTopLevelWindows.DeleteObject(this); | |
94 | ||
95 | if (m_frameStatusBar) | |
96 | delete m_frameStatusBar; | |
97 | if (m_frameMenuBar) | |
98 | delete m_frameMenuBar; | |
99 | ||
100 | /* Check if it's the last top-level window */ | |
101 | ||
102 | if (wxTheApp && (wxTopLevelWindows.Number() == 0)) | |
103 | { | |
104 | wxTheApp->SetTopWindow(NULL); | |
105 | ||
106 | if (wxTheApp->GetExitOnFrameDelete()) | |
107 | { | |
108 | // TODO signal to the app that we're going to close | |
109 | } | |
110 | } | |
111 | ||
112 | wxModelessWindows.DeleteObject(this); | |
113 | } | |
114 | ||
115 | // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc. | |
116 | void wxFrame::GetClientSize(int *x, int *y) const | |
117 | { | |
118 | // TODO | |
119 | } | |
120 | ||
121 | // Set the client size (i.e. leave the calculation of borders etc. | |
122 | // to wxWindows) | |
123 | void wxFrame::SetClientSize(int width, int height) | |
124 | { | |
125 | // TODO | |
126 | } | |
127 | ||
128 | void wxFrame::GetSize(int *width, int *height) const | |
129 | { | |
130 | // TODO | |
131 | } | |
132 | ||
133 | void wxFrame::GetPosition(int *x, int *y) const | |
134 | { | |
135 | // TODO | |
136 | } | |
137 | ||
138 | void wxFrame::SetSize(int x, int y, int width, int height, int sizeFlags) | |
139 | { | |
140 | // TODO | |
141 | } | |
142 | ||
143 | bool wxFrame::Show(bool show) | |
144 | { | |
145 | // TODO | |
146 | return FALSE; | |
147 | } | |
148 | ||
149 | void wxFrame::Iconize(bool iconize) | |
150 | { | |
151 | // TODO | |
152 | } | |
153 | ||
154 | // Equivalent to maximize/restore in Windows | |
155 | void wxFrame::Maximize(bool maximize) | |
156 | { | |
157 | // TODO | |
158 | } | |
159 | ||
160 | bool wxFrame::IsIconized() const | |
161 | { | |
162 | // TODO | |
163 | return FALSE; | |
164 | } | |
165 | ||
166 | void wxFrame::SetTitle(const wxString& title) | |
167 | { | |
168 | // TODO | |
169 | } | |
170 | ||
171 | wxString wxFrame::GetTitle() const | |
172 | { | |
173 | // TODO | |
174 | return wxString(""); | |
175 | } | |
176 | ||
177 | void wxFrame::SetIcon(const wxIcon& icon) | |
178 | { | |
179 | m_icon = icon; | |
180 | // TODO | |
181 | } | |
182 | ||
93cf77c0 JS |
183 | wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id, |
184 | const wxString& name) | |
185 | { | |
186 | wxStatusBar *statusBar = NULL; | |
187 | ||
188 | statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20), | |
189 | style, name); | |
190 | ||
191 | // Set the height according to the font and the border size | |
192 | wxClientDC dc(statusBar); | |
c0ed460c | 193 | dc.SetFont(statusBar->GetFont()); |
93cf77c0 JS |
194 | |
195 | long x, y; | |
196 | dc.GetTextExtent("X", &x, &y); | |
197 | ||
198 | int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY()); | |
199 | ||
200 | statusBar->SetSize(-1, -1, 100, height); | |
201 | ||
202 | statusBar->SetFieldsCount(number); | |
203 | return statusBar; | |
204 | } | |
205 | ||
206 | wxStatusBar* wxFrame::CreateStatusBar(int number, long style, wxWindowID id, | |
207 | const wxString& name) | |
208 | { | |
209 | // Calling CreateStatusBar twice is an error. | |
210 | wxCHECK_MSG( m_frameStatusBar == NULL, FALSE, | |
211 | "recreating status bar in wxFrame" ); | |
212 | ||
213 | m_frameStatusBar = OnCreateStatusBar(number, style, id, | |
214 | name); | |
215 | if ( m_frameStatusBar ) | |
216 | { | |
217 | PositionStatusBar(); | |
218 | return m_frameStatusBar; | |
219 | } | |
220 | else | |
221 | return NULL; | |
222 | } | |
223 | ||
224 | void wxFrame::SetStatusText(const wxString& text, int number) | |
225 | { | |
226 | wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" ); | |
227 | ||
228 | m_frameStatusBar->SetStatusText(text, number); | |
229 | } | |
230 | ||
231 | void wxFrame::SetStatusWidths(int n, const int widths_field[]) | |
232 | { | |
233 | wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" ); | |
234 | ||
235 | m_frameStatusBar->SetStatusWidths(n, widths_field); | |
236 | PositionStatusBar(); | |
237 | } | |
238 | ||
239 | void wxFrame::PositionStatusBar() | |
240 | { | |
241 | int w, h; | |
242 | GetClientSize(&w, &h); | |
243 | int sw, sh; | |
244 | m_frameStatusBar->GetSize(&sw, &sh); | |
245 | ||
246 | // Since we wish the status bar to be directly under the client area, | |
247 | // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS. | |
248 | m_frameStatusBar->SetSize(0, h, w, sh); | |
249 | } | |
250 | ||
251 | void wxFrame::SetMenuBar(wxMenuBar *menuBar) | |
252 | { | |
253 | if (!menuBar) | |
254 | { | |
255 | m_frameMenuBar = NULL; | |
256 | return; | |
257 | } | |
258 | ||
259 | m_frameMenuBar = menuBar; | |
260 | ||
261 | // TODO | |
262 | } | |
263 | ||
264 | void wxFrame::Fit() | |
265 | { | |
266 | // Work out max. size | |
c0ed460c | 267 | wxNode *node = GetChildren().First(); |
93cf77c0 JS |
268 | int max_width = 0; |
269 | int max_height = 0; | |
270 | while (node) | |
271 | { | |
272 | // Find a child that's a subwindow, but not a dialog box. | |
273 | wxWindow *win = (wxWindow *)node->Data(); | |
274 | ||
275 | if (!win->IsKindOf(CLASSINFO(wxFrame)) && | |
276 | !win->IsKindOf(CLASSINFO(wxDialog))) | |
277 | { | |
278 | int width, height; | |
279 | int x, y; | |
280 | win->GetSize(&width, &height); | |
281 | win->GetPosition(&x, &y); | |
282 | ||
283 | if ((x + width) > max_width) | |
284 | max_width = x + width; | |
285 | if ((y + height) > max_height) | |
286 | max_height = y + height; | |
287 | } | |
288 | node = node->Next(); | |
289 | } | |
290 | SetClientSize(max_width, max_height); | |
291 | } | |
292 | ||
293 | // Responds to colour changes, and passes event on to children. | |
294 | void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event) | |
295 | { | |
296 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE)); | |
297 | Refresh(); | |
298 | ||
299 | if ( m_frameStatusBar ) | |
300 | { | |
301 | wxSysColourChangedEvent event2; | |
302 | event2.SetEventObject( m_frameStatusBar ); | |
303 | m_frameStatusBar->ProcessEvent(event2); | |
304 | } | |
305 | ||
306 | // Propagate the event to the non-top-level children | |
307 | wxWindow::OnSysColourChanged(event); | |
308 | } | |
309 | ||
310 | // Default resizing behaviour - if only ONE subwindow, | |
311 | // resize to client rectangle size | |
312 | void wxFrame::OnSize(wxSizeEvent& event) | |
313 | { | |
314 | // if we're using constraints - do use them | |
47d67540 | 315 | #if wxUSE_CONSTRAINTS |
93cf77c0 JS |
316 | if ( GetAutoLayout() ) { |
317 | Layout(); | |
318 | return; | |
319 | } | |
320 | #endif | |
321 | ||
322 | // do we have _exactly_ one child? | |
323 | wxWindow *child = NULL; | |
c0ed460c | 324 | for ( wxNode *node = GetChildren().First(); node; node = node->Next() ) |
93cf77c0 JS |
325 | { |
326 | wxWindow *win = (wxWindow *)node->Data(); | |
327 | if ( !win->IsKindOf(CLASSINFO(wxFrame)) && | |
328 | !win->IsKindOf(CLASSINFO(wxDialog)) && | |
329 | (win != GetStatusBar()) && | |
330 | (win != GetToolBar()) ) | |
331 | { | |
332 | if ( child ) | |
333 | return; // it's our second subwindow - nothing to do | |
334 | child = win; | |
335 | } | |
336 | } | |
337 | ||
338 | if ( child ) { | |
339 | // we have exactly one child - set it's size to fill the whole frame | |
340 | int clientW, clientH; | |
341 | GetClientSize(&clientW, &clientH); | |
342 | ||
343 | int x = 0; | |
344 | int y = 0; | |
345 | ||
346 | child->SetSize(x, y, clientW, clientH); | |
347 | } | |
348 | } | |
349 | ||
350 | // Default activation behaviour - set the focus for the first child | |
351 | // subwindow found. | |
352 | void wxFrame::OnActivate(wxActivateEvent& event) | |
353 | { | |
c0ed460c | 354 | for(wxNode *node = GetChildren().First(); node; node = node->Next()) |
93cf77c0 JS |
355 | { |
356 | // Find a child that's a subwindow, but not a dialog box. | |
357 | wxWindow *child = (wxWindow *)node->Data(); | |
358 | if (!child->IsKindOf(CLASSINFO(wxFrame)) && | |
359 | !child->IsKindOf(CLASSINFO(wxDialog))) | |
360 | { | |
93cf77c0 JS |
361 | child->SetFocus(); |
362 | return; | |
363 | } | |
364 | } | |
365 | } | |
366 | ||
367 | // The default implementation for the close window event - calls | |
368 | // OnClose for backward compatibility. | |
369 | ||
370 | void wxFrame::OnCloseWindow(wxCloseEvent& event) | |
371 | { | |
372 | // Compatibility | |
373 | if ( GetEventHandler()->OnClose() || event.GetForce()) | |
374 | { | |
375 | this->Destroy(); | |
376 | } | |
377 | } | |
378 | ||
379 | bool wxFrame::OnClose() | |
380 | { | |
381 | return TRUE; | |
382 | } | |
383 | ||
384 | // Destroy the window (delayed, if a managed window) | |
385 | bool wxFrame::Destroy() | |
386 | { | |
387 | if (!wxPendingDelete.Member(this)) | |
388 | wxPendingDelete.Append(this); | |
389 | return TRUE; | |
390 | } | |
391 | ||
392 | // Default menu selection behaviour - display a help string | |
393 | void wxFrame::OnMenuHighlight(wxMenuEvent& event) | |
394 | { | |
395 | if (GetStatusBar()) | |
396 | { | |
397 | if (event.GetMenuId() == -1) | |
398 | SetStatusText(""); | |
399 | else | |
400 | { | |
401 | wxMenuBar *menuBar = GetMenuBar(); | |
402 | if (menuBar) | |
403 | { | |
404 | wxString helpString(menuBar->GetHelpString(event.GetMenuId())); | |
405 | if (helpString != "") | |
406 | SetStatusText(helpString); | |
407 | } | |
408 | } | |
409 | } | |
410 | } | |
411 | ||
412 | wxMenuBar *wxFrame::GetMenuBar() const | |
413 | { | |
414 | return m_frameMenuBar; | |
415 | } | |
416 | ||
417 | void wxFrame::Centre(int direction) | |
418 | { | |
419 | int display_width, display_height, width, height, x, y; | |
420 | wxDisplaySize(&display_width, &display_height); | |
421 | ||
422 | GetSize(&width, &height); | |
423 | GetPosition(&x, &y); | |
424 | ||
425 | if (direction & wxHORIZONTAL) | |
426 | x = (int)((display_width - width)/2); | |
427 | if (direction & wxVERTICAL) | |
428 | y = (int)((display_height - height)/2); | |
429 | ||
430 | SetSize(x, y, width, height); | |
431 | } | |
432 | ||
433 | // Call this to simulate a menu command | |
434 | void wxFrame::Command(int id) | |
435 | { | |
436 | ProcessCommand(id); | |
437 | } | |
438 | ||
439 | void wxFrame::ProcessCommand(int id) | |
440 | { | |
441 | wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, id); | |
442 | commandEvent.SetInt( id ); | |
443 | commandEvent.SetEventObject( this ); | |
444 | ||
445 | wxMenuBar *bar = GetMenuBar() ; | |
446 | if (!bar) | |
447 | return; | |
448 | ||
449 | /* TODO: check the menu item if required | |
450 | wxMenuItem *item = bar->FindItemForId(id) ; | |
451 | if (item && item->IsCheckable()) | |
452 | { | |
453 | bar->Check(id,!bar->Checked(id)) ; | |
454 | } | |
455 | */ | |
456 | ||
457 | GetEventHandler()->ProcessEvent(commandEvent); | |
458 | } | |
459 | ||
460 | // Checks if there is a toolbar, and returns the first free client position | |
461 | wxPoint wxFrame::GetClientAreaOrigin() const | |
462 | { | |
463 | wxPoint pt(0, 0); | |
464 | if (GetToolBar()) | |
465 | { | |
466 | int w, h; | |
467 | GetToolBar()->GetSize(& w, & h); | |
468 | ||
469 | if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL) | |
470 | { | |
471 | pt.x += w; | |
472 | } | |
473 | else | |
474 | { | |
475 | pt.y += h; | |
476 | } | |
477 | } | |
478 | return pt; | |
479 | } | |
480 | ||
87d1e11f JS |
481 | void wxFrame::ScreenToClient(int *x, int *y) const |
482 | { | |
483 | wxWindow::ScreenToClient(x, y); | |
484 | ||
485 | // We may be faking the client origin. | |
486 | // So a window that's really at (0, 30) may appear | |
487 | // (to wxWin apps) to be at (0, 0). | |
488 | wxPoint pt(GetClientAreaOrigin()); | |
489 | *x -= pt.x; | |
490 | *y -= pt.y; | |
491 | } | |
492 | ||
493 | void wxFrame::ClientToScreen(int *x, int *y) const | |
494 | { | |
495 | // We may be faking the client origin. | |
496 | // So a window that's really at (0, 30) may appear | |
497 | // (to wxWin apps) to be at (0, 0). | |
498 | wxPoint pt1(GetClientAreaOrigin()); | |
499 | *x += pt1.x; | |
500 | *y += pt1.y; | |
501 | ||
502 | wxWindow::ClientToScreen(x, y); | |
503 | } | |
504 | ||
93cf77c0 JS |
505 | wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name) |
506 | { | |
507 | wxCHECK_MSG( m_frameToolBar == NULL, FALSE, | |
508 | "recreating toolbar in wxFrame" ); | |
509 | ||
510 | wxToolBar* toolBar = OnCreateToolBar(style, id, name); | |
511 | if (toolBar) | |
512 | { | |
513 | SetToolBar(toolBar); | |
514 | PositionToolBar(); | |
515 | return toolBar; | |
516 | } | |
517 | else | |
518 | { | |
519 | return NULL; | |
520 | } | |
521 | } | |
522 | ||
523 | wxToolBar* wxFrame::OnCreateToolBar(long style, wxWindowID id, const wxString& name) | |
524 | { | |
525 | return new wxToolBar(this, id, wxDefaultPosition, wxDefaultSize, style, name); | |
526 | } | |
527 | ||
528 | void wxFrame::PositionToolBar() | |
529 | { | |
530 | int cw, ch; | |
531 | ||
532 | // TODO: we actually need to use the low-level client size, before | |
533 | // the toolbar/status bar were added. | |
534 | // So DEFINITELY replace the line below with something appropriate. | |
535 | ||
93cf77c0 JS |
536 | GetClientSize(& cw, &ch); |
537 | ||
538 | if ( GetStatusBar() ) | |
539 | { | |
540 | int statusX, statusY; | |
541 | GetStatusBar()->GetClientSize(&statusX, &statusY); | |
542 | ch -= statusY; | |
543 | } | |
544 | ||
545 | if (GetToolBar()) | |
546 | { | |
547 | int tw, th; | |
548 | GetToolBar()->GetSize(& tw, & th); | |
549 | ||
550 | if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL) | |
551 | { | |
552 | // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS | |
553 | // means, pretend we don't have toolbar/status bar, so we | |
554 | // have the original client size. | |
555 | GetToolBar()->SetSize(0, 0, tw, ch, wxSIZE_NO_ADJUSTMENTS); | |
556 | } | |
557 | else | |
558 | { | |
559 | // Use the 'real' position | |
560 | GetToolBar()->SetSize(0, 0, cw, th, wxSIZE_NO_ADJUSTMENTS); | |
561 | } | |
562 | } | |
563 | } | |
564 |