]> git.saurik.com Git - wxWidgets.git/blob - src/mac/frame.cpp
a961e3616f8e4a180f5f3a5546586744fb8c0c11
[wxWidgets.git] / src / mac / frame.cpp
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"
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"
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
42 #if wxUSE_NATIVE_STATUSBAR
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 // Is the frame maximized?
167 bool wxFrame::IsMaximized(void) const
168 {
169 // TODO
170 return FALSE;
171 }
172
173 void wxFrame::SetTitle(const wxString& title)
174 {
175 // TODO
176 }
177
178 wxString wxFrame::GetTitle() const
179 {
180 // TODO
181 return wxString("");
182 }
183
184 void wxFrame::SetIcon(const wxIcon& icon)
185 {
186 m_icon = icon;
187 // TODO
188 }
189
190 wxStatusBar *wxFrame::OnCreateStatusBar(int number, long style, wxWindowID id,
191 const wxString& name)
192 {
193 wxStatusBar *statusBar = NULL;
194
195 statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20),
196 style, name);
197
198 // Set the height according to the font and the border size
199 wxClientDC dc(statusBar);
200 dc.SetFont(statusBar->GetFont());
201
202 long x, y;
203 dc.GetTextExtent("X", &x, &y);
204
205 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
206
207 statusBar->SetSize(-1, -1, 100, height);
208
209 statusBar->SetFieldsCount(number);
210 return statusBar;
211 }
212
213 wxStatusBar* wxFrame::CreateStatusBar(int number, long style, wxWindowID id,
214 const wxString& name)
215 {
216 // Calling CreateStatusBar twice is an error.
217 wxCHECK_MSG( m_frameStatusBar == NULL, FALSE,
218 "recreating status bar in wxFrame" );
219
220 m_frameStatusBar = OnCreateStatusBar(number, style, id,
221 name);
222 if ( m_frameStatusBar )
223 {
224 PositionStatusBar();
225 return m_frameStatusBar;
226 }
227 else
228 return NULL;
229 }
230
231 void wxFrame::SetStatusText(const wxString& text, int number)
232 {
233 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" );
234
235 m_frameStatusBar->SetStatusText(text, number);
236 }
237
238 void wxFrame::SetStatusWidths(int n, const int widths_field[])
239 {
240 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" );
241
242 m_frameStatusBar->SetStatusWidths(n, widths_field);
243 PositionStatusBar();
244 }
245
246 void wxFrame::PositionStatusBar()
247 {
248 int w, h;
249 GetClientSize(&w, &h);
250 int sw, sh;
251 m_frameStatusBar->GetSize(&sw, &sh);
252
253 // Since we wish the status bar to be directly under the client area,
254 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
255 m_frameStatusBar->SetSize(0, h, w, sh);
256 }
257
258 void wxFrame::SetMenuBar(wxMenuBar *menuBar)
259 {
260 if (!menuBar)
261 {
262 m_frameMenuBar = NULL;
263 return;
264 }
265
266 m_frameMenuBar = menuBar;
267
268 // TODO
269 }
270
271 void wxFrame::Fit()
272 {
273 // Work out max. size
274 wxNode *node = GetChildren().First();
275 int max_width = 0;
276 int max_height = 0;
277 while (node)
278 {
279 // Find a child that's a subwindow, but not a dialog box.
280 wxWindow *win = (wxWindow *)node->Data();
281
282 if (!win->IsKindOf(CLASSINFO(wxFrame)) &&
283 !win->IsKindOf(CLASSINFO(wxDialog)))
284 {
285 int width, height;
286 int x, y;
287 win->GetSize(&width, &height);
288 win->GetPosition(&x, &y);
289
290 if ((x + width) > max_width)
291 max_width = x + width;
292 if ((y + height) > max_height)
293 max_height = y + height;
294 }
295 node = node->Next();
296 }
297 SetClientSize(max_width, max_height);
298 }
299
300 // Responds to colour changes, and passes event on to children.
301 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
302 {
303 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
304 Refresh();
305
306 if ( m_frameStatusBar )
307 {
308 wxSysColourChangedEvent event2;
309 event2.SetEventObject( m_frameStatusBar );
310 m_frameStatusBar->ProcessEvent(event2);
311 }
312
313 // Propagate the event to the non-top-level children
314 wxWindow::OnSysColourChanged(event);
315 }
316
317 // Default resizing behaviour - if only ONE subwindow,
318 // resize to client rectangle size
319 void wxFrame::OnSize(wxSizeEvent& event)
320 {
321 // if we're using constraints - do use them
322 #if wxUSE_CONSTRAINTS
323 if ( GetAutoLayout() ) {
324 Layout();
325 return;
326 }
327 #endif
328
329 // do we have _exactly_ one child?
330 wxWindow *child = NULL;
331 for ( wxNode *node = GetChildren().First(); node; node = node->Next() )
332 {
333 wxWindow *win = (wxWindow *)node->Data();
334 if ( !win->IsKindOf(CLASSINFO(wxFrame)) &&
335 !win->IsKindOf(CLASSINFO(wxDialog)) &&
336 (win != GetStatusBar()) &&
337 (win != GetToolBar()) )
338 {
339 if ( child )
340 return; // it's our second subwindow - nothing to do
341 child = win;
342 }
343 }
344
345 if ( child ) {
346 // we have exactly one child - set it's size to fill the whole frame
347 int clientW, clientH;
348 GetClientSize(&clientW, &clientH);
349
350 int x = 0;
351 int y = 0;
352
353 child->SetSize(x, y, clientW, clientH);
354 }
355 }
356
357 // Default activation behaviour - set the focus for the first child
358 // subwindow found.
359 void wxFrame::OnActivate(wxActivateEvent& event)
360 {
361 for(wxNode *node = GetChildren().First(); node; node = node->Next())
362 {
363 // Find a child that's a subwindow, but not a dialog box.
364 wxWindow *child = (wxWindow *)node->Data();
365 if (!child->IsKindOf(CLASSINFO(wxFrame)) &&
366 !child->IsKindOf(CLASSINFO(wxDialog)))
367 {
368 child->SetFocus();
369 return;
370 }
371 }
372 }
373
374 // The default implementation for the close window event - calls
375 // OnClose for backward compatibility.
376
377 void wxFrame::OnCloseWindow(wxCloseEvent& event)
378 {
379 // Compatibility
380 if ( GetEventHandler()->OnClose() || !event.CanVeto())
381 {
382 this->Destroy();
383 }
384 else
385 event.Veto(TRUE);
386 }
387
388 bool wxFrame::OnClose()
389 {
390 return TRUE;
391 }
392
393 // Destroy the window (delayed, if a managed window)
394 bool wxFrame::Destroy()
395 {
396 if (!wxPendingDelete.Member(this))
397 wxPendingDelete.Append(this);
398 return TRUE;
399 }
400
401 // Default menu selection behaviour - display a help string
402 void wxFrame::OnMenuHighlight(wxMenuEvent& event)
403 {
404 if (GetStatusBar())
405 {
406 if (event.GetMenuId() == -1)
407 SetStatusText("");
408 else
409 {
410 wxMenuBar *menuBar = GetMenuBar();
411 if (menuBar)
412 {
413 wxString helpString(menuBar->GetHelpString(event.GetMenuId()));
414 if (helpString != "")
415 SetStatusText(helpString);
416 }
417 }
418 }
419 }
420
421 wxMenuBar *wxFrame::GetMenuBar() const
422 {
423 return m_frameMenuBar;
424 }
425
426 void wxFrame::Centre(int direction)
427 {
428 int display_width, display_height, width, height, x, y;
429 wxDisplaySize(&display_width, &display_height);
430
431 GetSize(&width, &height);
432 GetPosition(&x, &y);
433
434 if (direction & wxHORIZONTAL)
435 x = (int)((display_width - width)/2);
436 if (direction & wxVERTICAL)
437 y = (int)((display_height - height)/2);
438
439 SetSize(x, y, width, height);
440 }
441
442 // Call this to simulate a menu command
443 void wxFrame::Command(int id)
444 {
445 ProcessCommand(id);
446 }
447
448 void wxFrame::ProcessCommand(int id)
449 {
450 wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, id);
451 commandEvent.SetInt( id );
452 commandEvent.SetEventObject( this );
453
454 wxMenuBar *bar = GetMenuBar() ;
455 if (!bar)
456 return;
457
458 /* TODO: check the menu item if required
459 wxMenuItem *item = bar->FindItemForId(id) ;
460 if (item && item->IsCheckable())
461 {
462 bar->Check(id,!bar->Checked(id)) ;
463 }
464 */
465
466 GetEventHandler()->ProcessEvent(commandEvent);
467 }
468
469 // Checks if there is a toolbar, and returns the first free client position
470 wxPoint wxFrame::GetClientAreaOrigin() const
471 {
472 wxPoint pt(0, 0);
473 if (GetToolBar())
474 {
475 int w, h;
476 GetToolBar()->GetSize(& w, & h);
477
478 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
479 {
480 pt.x += w;
481 }
482 else
483 {
484 pt.y += h;
485 }
486 }
487 return pt;
488 }
489
490 void wxFrame::ScreenToClient(int *x, int *y) const
491 {
492 wxWindow::ScreenToClient(x, y);
493
494 // We may be faking the client origin.
495 // So a window that's really at (0, 30) may appear
496 // (to wxWin apps) to be at (0, 0).
497 wxPoint pt(GetClientAreaOrigin());
498 *x -= pt.x;
499 *y -= pt.y;
500 }
501
502 void wxFrame::ClientToScreen(int *x, int *y) const
503 {
504 // We may be faking the client origin.
505 // So a window that's really at (0, 30) may appear
506 // (to wxWin apps) to be at (0, 0).
507 wxPoint pt1(GetClientAreaOrigin());
508 *x += pt1.x;
509 *y += pt1.y;
510
511 wxWindow::ClientToScreen(x, y);
512 }
513
514 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
515 {
516 wxCHECK_MSG( m_frameToolBar == NULL, FALSE,
517 "recreating toolbar in wxFrame" );
518
519 wxToolBar* toolBar = OnCreateToolBar(style, id, name);
520 if (toolBar)
521 {
522 SetToolBar(toolBar);
523 PositionToolBar();
524 return toolBar;
525 }
526 else
527 {
528 return NULL;
529 }
530 }
531
532 wxToolBar* wxFrame::OnCreateToolBar(long style, wxWindowID id, const wxString& name)
533 {
534 return new wxToolBar(this, id, wxDefaultPosition, wxDefaultSize, style, name);
535 }
536
537 void wxFrame::PositionToolBar()
538 {
539 int cw, ch;
540
541 // TODO: we actually need to use the low-level client size, before
542 // the toolbar/status bar were added.
543 // So DEFINITELY replace the line below with something appropriate.
544
545 GetClientSize(& cw, &ch);
546
547 if ( GetStatusBar() )
548 {
549 int statusX, statusY;
550 GetStatusBar()->GetClientSize(&statusX, &statusY);
551 ch -= statusY;
552 }
553
554 if (GetToolBar())
555 {
556 int tw, th;
557 GetToolBar()->GetSize(& tw, & th);
558
559 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL)
560 {
561 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
562 // means, pretend we don't have toolbar/status bar, so we
563 // have the original client size.
564 GetToolBar()->SetSize(0, 0, tw, ch, wxSIZE_NO_ADJUSTMENTS);
565 }
566 else
567 {
568 // Use the 'real' position
569 GetToolBar()->SetSize(0, 0, cw, th, wxSIZE_NO_ADJUSTMENTS);
570 }
571 }
572 }
573