]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/frame.cpp
Fixed event handling for DialogEd
[wxWidgets.git] / src / gtk1 / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: frame.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "frame.h"
13 #endif
14
15 #include "wx/frame.h"
16 #include "wx/dialog.h"
17 #include "wx/control.h"
18 #include "wx/app.h"
19 #include "wx/menu.h"
20 #include "wx/toolbar.h"
21 #include "wx/statusbr.h"
22 #include "wx/mdi.h"
23 #include "wx/dcclient.h"
24 #include "wx/gtk/win_gtk.h"
25
26 //-----------------------------------------------------------------------------
27 // constants
28 //-----------------------------------------------------------------------------
29
30 const int wxMENU_HEIGHT = 28;
31 const int wxSTATUS_HEIGHT = 25;
32
33 //-----------------------------------------------------------------------------
34 // data
35 //-----------------------------------------------------------------------------
36
37 extern wxList wxTopLevelWindows;
38 extern wxList wxPendingDelete;
39
40 //-----------------------------------------------------------------------------
41 // "size_allocate"
42 //-----------------------------------------------------------------------------
43
44 static void gtk_frame_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxFrame *win )
45 {
46 if (!win->HasVMT()) return;
47
48 /*
49 printf( "OnFrameResize from " );
50 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
51 printf( win->GetClassInfo()->GetClassName() );
52 printf( ".\n" );
53 */
54
55 win->GtkOnSize( alloc->x, alloc->y, alloc->width, alloc->height );
56 }
57
58 //-----------------------------------------------------------------------------
59 // "delete_event"
60 //-----------------------------------------------------------------------------
61
62 static gint gtk_frame_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxFrame *win )
63 {
64 /*
65 printf( "OnDelete from " );
66 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
67 printf( win->GetClassInfo()->GetClassName() );
68 printf( ".\n" );
69 */
70
71 win->Close();
72
73 return TRUE;
74 }
75
76 //-----------------------------------------------------------------------------
77 // "configure_event"
78 //-----------------------------------------------------------------------------
79
80 static gint gtk_frame_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxFrame *win )
81 {
82 if (!win->HasVMT()) return FALSE;
83
84 win->m_x = event->x;
85 win->m_y = event->y;
86
87 return FALSE;
88 }
89
90 //-----------------------------------------------------------------------------
91 // wxFrame
92 //-----------------------------------------------------------------------------
93
94 BEGIN_EVENT_TABLE(wxFrame, wxWindow)
95 EVT_SIZE(wxFrame::OnSize)
96 EVT_CLOSE(wxFrame::OnCloseWindow)
97 EVT_IDLE(wxFrame::OnIdle)
98 END_EVENT_TABLE()
99
100 IMPLEMENT_DYNAMIC_CLASS(wxFrame,wxWindow)
101
102 wxFrame::wxFrame()
103 {
104 m_frameMenuBar = (wxMenuBar *) NULL;
105 m_frameStatusBar = (wxStatusBar *) NULL;
106 m_frameToolBar = (wxToolBar *) NULL;
107 m_sizeSet = FALSE;
108 m_addPrivateChild = FALSE;
109 m_wxwindow = (GtkWidget *) NULL;
110 m_mainWindow = (GtkWidget *) NULL;
111 wxTopLevelWindows.Insert( this );
112 }
113
114 wxFrame::wxFrame( wxWindow *parent, wxWindowID id, const wxString &title,
115 const wxPoint &pos, const wxSize &size,
116 long style, const wxString &name )
117 {
118 m_frameMenuBar = (wxMenuBar *) NULL;
119 m_frameStatusBar = (wxStatusBar *) NULL;
120 m_frameToolBar = (wxToolBar *) NULL;
121 m_sizeSet = FALSE;
122 m_addPrivateChild = FALSE;
123 m_wxwindow = (GtkWidget *) NULL;
124 m_mainWindow = (GtkWidget *) NULL;
125 Create( parent, id, title, pos, size, style, name );
126 wxTopLevelWindows.Insert( this );
127 }
128
129 bool wxFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
130 const wxPoint &pos, const wxSize &size,
131 long style, const wxString &name )
132 {
133 m_needParent = FALSE;
134
135 PreCreation( parent, id, pos, size, style, name );
136
137 m_title = title;
138
139 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
140 if ((size.x != -1) && (size.y != -1))
141 gtk_widget_set_usize( m_widget, m_width, m_height );
142 if ((pos.x != -1) && (pos.y != -1))
143 gtk_widget_set_uposition( m_widget, m_x, m_y );
144
145 gtk_window_set_title( GTK_WINDOW(m_widget), title );
146 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
147
148 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
149
150 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
151 GTK_SIGNAL_FUNC(gtk_frame_delete_callback), (gpointer)this );
152
153 m_mainWindow = gtk_myfixed_new();
154 gtk_widget_show( m_mainWindow );
155 GTK_WIDGET_UNSET_FLAGS( m_mainWindow, GTK_CAN_FOCUS );
156
157 gtk_container_add( GTK_CONTAINER(m_widget), m_mainWindow );
158 gtk_widget_set_uposition( m_mainWindow, 0, 0 );
159
160 m_wxwindow = gtk_myfixed_new();
161 gtk_widget_show( m_wxwindow );
162 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
163
164 gtk_container_add( GTK_CONTAINER(m_mainWindow), m_wxwindow );
165
166 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
167 GTK_SIGNAL_FUNC(gtk_frame_size_callback), (gpointer)this );
168
169 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
170 GTK_SIGNAL_FUNC(gtk_frame_configure_callback), (gpointer)this );
171
172 PostCreation();
173
174 gtk_widget_realize( m_mainWindow );
175
176 return TRUE;
177 }
178
179 wxFrame::~wxFrame()
180 {
181 if (m_frameMenuBar) delete m_frameMenuBar;
182 if (m_frameStatusBar) delete m_frameStatusBar;
183 if (m_frameToolBar) delete m_frameToolBar;
184
185 // if (m_mainWindow) gtk_widget_destroy( m_mainWindow );
186
187 wxTopLevelWindows.DeleteObject( this );
188 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
189 }
190
191 bool wxFrame::Show( bool show )
192 {
193 if (show)
194 {
195 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
196 m_sizeSet = FALSE;
197 ProcessEvent( event );
198 }
199 return wxWindow::Show( show );
200 }
201
202 void wxFrame::Enable( bool enable )
203 {
204 wxWindow::Enable( enable );
205 gtk_widget_set_sensitive( m_mainWindow, enable );
206 }
207
208 void wxFrame::OnCloseWindow( wxCloseEvent &event )
209 {
210 if ( GetEventHandler()->OnClose() || event.GetForce())
211 {
212 this->Destroy();
213 }
214 }
215
216 bool wxFrame::Destroy()
217 {
218 if (!wxPendingDelete.Member(this))
219 wxPendingDelete.Append(this);
220
221 return TRUE;
222 }
223
224 void wxFrame::ImplementSetPosition(void)
225 {
226 if ((m_x != -1) || (m_y != -1))
227 gtk_widget_set_uposition( m_widget, m_x, m_y );
228 }
229
230 void wxFrame::Centre( int direction )
231 {
232 if (direction & wxHORIZONTAL == wxHORIZONTAL) m_x = (gdk_screen_width () - m_width) / 2;
233 if (direction & wxVERTICAL == wxVERTICAL) m_y = (gdk_screen_height () - m_height) / 2;
234 ImplementSetPosition();
235 }
236
237 void wxFrame::GetClientSize( int *width, int *height ) const
238 {
239 wxWindow::GetClientSize( width, height );
240 if (height)
241 {
242 if (m_frameMenuBar) (*height) -= wxMENU_HEIGHT;
243 if (m_frameStatusBar) (*height) -= wxSTATUS_HEIGHT;
244 if (m_frameToolBar)
245 {
246 int y = 0;
247 m_frameToolBar->GetSize( (int *) NULL, &y );
248 (*height) -= y;
249 }
250 }
251 }
252
253 void wxFrame::SetClientSize( int const width, int const height )
254 {
255 int h = height;
256 if (m_frameMenuBar) h += wxMENU_HEIGHT;
257 if (m_frameStatusBar) h += wxSTATUS_HEIGHT;
258 if (m_frameToolBar)
259 {
260 int y = 0;
261 m_frameToolBar->GetSize( (int *) NULL, &y );
262 h += y;
263 }
264 wxWindow::SetClientSize( width, h );
265 }
266
267 void wxFrame::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
268 {
269 // due to a bug in gtk, x,y are always 0
270 // m_x = x;
271 // m_y = y;
272
273 if ((m_height == height) && (m_width == width) &&
274 (m_sizeSet)) return;
275 if (!m_mainWindow) return;
276 if (!m_wxwindow) return;
277
278 m_width = width;
279 m_height = height;
280 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
281 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
282 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth;
283 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight;
284
285 gtk_widget_set_usize( m_widget, width, height );
286
287 int main_x = 0;
288 int main_y = 0;
289 int main_height = height;
290 int main_width = width;
291
292 // This emulates Windows behaviour:
293 // The menu bar is part of the main window, but the status bar
294 // is on the implementation side in the client area. The
295 // function GetClientSize returns the size of the client area
296 // minus the status bar height. Under wxGTK, the main window
297 // is represented by m_mainWindow. The menubar is inserted
298 // into m_mainWindow whereas the statusbar is insertes into
299 // m_wxwindow just like any other window.
300
301 // not really needed
302 // gtk_widget_set_usize( m_mainWindow, width, height );
303
304 if (m_frameMenuBar)
305 {
306 main_y = wxMENU_HEIGHT;
307 main_height -= wxMENU_HEIGHT;
308 }
309
310 int toolbar_height = 0;
311 if (m_frameToolBar) m_frameToolBar->GetSize( (int *) NULL, &toolbar_height );
312
313 main_y += toolbar_height;
314 main_height -= toolbar_height;
315
316 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow), m_wxwindow, main_x, main_y );
317 gtk_widget_set_usize( m_wxwindow, main_width, main_height );
318
319 if (m_frameMenuBar)
320 {
321 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow), m_frameMenuBar->m_widget, 1, 1 );
322 gtk_widget_set_usize( m_frameMenuBar->m_widget, width-2, wxMENU_HEIGHT-2 );
323 }
324
325 if (m_frameToolBar)
326 {
327 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow), m_frameToolBar->m_widget, 1, wxMENU_HEIGHT );
328 gtk_widget_set_usize( m_frameToolBar->m_widget, width-2, toolbar_height );
329 }
330
331 if (m_frameStatusBar)
332 {
333 m_frameStatusBar->SetSize( 0, main_height-wxSTATUS_HEIGHT, width, wxSTATUS_HEIGHT );
334 }
335
336 m_sizeSet = TRUE;
337
338 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
339 event.SetEventObject( this );
340 ProcessEvent( event );
341 }
342
343 void wxFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
344 {
345 if ( GetAutoLayout() )
346 Layout();
347 else {
348 // no child: go out !
349 if (!GetChildren()->First())
350 return;
351
352 // do we have exactly one child?
353 wxWindow *child = (wxWindow *) NULL;
354 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
355 {
356 wxWindow *win = (wxWindow *)node->Data();
357 if (!IS_KIND_OF(win,wxFrame) && !IS_KIND_OF(win,wxDialog)
358 #if 0 // not in m_children anyway
359 && (win != m_frameMenuBar) &&
360 (win != m_frameToolBar) &&
361 (win != m_frameStatusBar)
362 #endif
363 )
364 {
365 if ( child ) // it's the second one: do nothing
366 return;
367
368 child = win;
369 }
370 }
371
372 // yes: set it's size to fill all the frame
373 int client_x, client_y;
374 GetClientSize(&client_x, &client_y);
375 child->SetSize( 1, 1, client_x-2, client_y);
376 }
377 }
378
379 void wxFrame::AddChild( wxWindow *child )
380 {
381 // wxFrame and wxDialog as children aren't placed into the parents
382
383 if (IS_KIND_OF(child,wxMDIChildFrame)) wxFAIL_MSG( "wxFrame::AddChild error.\n" );
384
385 if ( IS_KIND_OF(child,wxFrame) || IS_KIND_OF(child,wxDialog))
386 {
387 m_children.Append( child );
388
389 if ((child->m_x != -1) && (child->m_y != -1))
390 gtk_widget_set_uposition( child->m_widget, child->m_x, child->m_y );
391
392 return;
393 }
394
395 if (m_addPrivateChild)
396 {
397 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow), child->m_widget, child->m_x, child->m_y );
398
399 gtk_widget_set_usize( child->m_widget, child->m_width, child->m_height );
400 }
401 else
402 {
403 m_children.Append( child );
404
405 if (m_wxwindow)
406 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow), child->m_widget, child->m_x, child->m_y );
407
408 gtk_widget_set_usize( child->m_widget, child->m_width, child->m_height );
409 }
410 }
411
412 static void SetInvokingWindow( wxMenu *menu, wxWindow *win )
413 {
414 menu->SetInvokingWindow( win );
415 wxNode *node = menu->m_items.First();
416 while (node)
417 {
418 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
419 if (menuitem->IsSubMenu())
420 SetInvokingWindow( menuitem->GetSubMenu(), win );
421 node = node->Next();
422 }
423 }
424
425 void wxFrame::SetMenuBar( wxMenuBar *menuBar )
426 {
427 m_frameMenuBar = menuBar;
428
429 if (m_frameMenuBar)
430 {
431 wxNode *node = m_frameMenuBar->m_menus.First();
432 while (node)
433 {
434 wxMenu *menu = (wxMenu*)node->Data();
435 SetInvokingWindow( menu, this );
436 node = node->Next();
437 }
438
439 if (m_frameMenuBar->m_parent != this)
440 {
441 m_frameMenuBar->m_parent = this;
442 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow),
443 m_frameMenuBar->m_widget, m_frameMenuBar->m_x, m_frameMenuBar->m_y );
444 }
445 }
446 }
447
448 wxMenuBar *wxFrame::GetMenuBar(void) const
449 {
450 return m_frameMenuBar;
451 }
452
453 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
454 {
455 wxCHECK_MSG( m_frameToolBar == NULL, FALSE, "recreating toolbar in wxFrame" );
456
457 m_addPrivateChild = TRUE;
458 m_frameToolBar = OnCreateToolBar( style, id, name );
459 m_addPrivateChild = FALSE;
460
461 return m_frameToolBar;
462 }
463
464 wxToolBar* wxFrame::OnCreateToolBar( long style, wxWindowID id, const wxString& name )
465 {
466 return new wxToolBar( this, id, wxDefaultPosition, wxDefaultSize, style, name );
467 }
468
469 wxToolBar *wxFrame::GetToolBar(void) const
470 {
471 return m_frameToolBar;
472 }
473
474 wxStatusBar* wxFrame::CreateStatusBar( int number, long style, wxWindowID id, const wxString& name )
475 {
476 wxCHECK_MSG( m_frameStatusBar == NULL, FALSE, "recreating status bar in wxFrame" );
477
478 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
479
480 return m_frameStatusBar;
481 }
482
483 wxStatusBar *wxFrame::OnCreateStatusBar( int number, long style, wxWindowID id, const wxString& name )
484 {
485 wxStatusBar *statusBar = (wxStatusBar *) NULL;
486
487 statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20), style, name);
488
489 // Set the height according to the font and the border size
490 wxClientDC dc(statusBar);
491 dc.SetFont( *statusBar->GetFont() );
492
493 long x, y;
494 dc.GetTextExtent( "X", &x, &y );
495
496 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
497
498 statusBar->SetSize( -1, -1, 100, height );
499
500 statusBar->SetFieldsCount( number );
501 return statusBar;
502 }
503
504 void wxFrame::SetStatusText(const wxString& text, int number)
505 {
506 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" );
507
508 m_frameStatusBar->SetStatusText(text, number);
509 }
510
511 void wxFrame::SetStatusWidths(int n, const int widths_field[] )
512 {
513 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" );
514
515 m_frameStatusBar->SetStatusWidths(n, widths_field);
516 }
517
518 wxStatusBar *wxFrame::GetStatusBar(void) const
519 {
520 return m_frameStatusBar;
521 }
522
523 void wxFrame::SetTitle( const wxString &title )
524 {
525 m_title = title;
526 if (m_title.IsNull()) m_title = "";
527 gtk_window_set_title( GTK_WINDOW(m_widget), title );
528 }
529
530 void wxFrame::SetIcon( const wxIcon &icon )
531 {
532 m_icon = icon;
533 if (!icon.Ok()) return;
534
535 wxMask *mask = icon.GetMask();
536 GdkBitmap *bm = (GdkBitmap *) NULL;
537 if (mask) bm = mask->GetBitmap();
538
539 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
540 }
541