]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/frame.cpp
made the interface more compatible with the MSW version (although
[wxWidgets.git] / src / gtk / 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/gtk/win_gtk.h"
20
21 const wxMENU_HEIGHT = 28;
22 const wxSTATUS_HEIGHT = 25;
23
24 extern wxList wxTopLevelWindows;
25 extern wxList wxPendingDelete;
26
27 //-----------------------------------------------------------------------------
28 // wxFrame
29 //-----------------------------------------------------------------------------
30
31 //-----------------------------------------------------------------------------
32 // size
33
34 void gtk_frame_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxFrame *win )
35 {
36 if (!win->HasVMT()) return;
37
38 /*
39 printf( "OnFrameResize from " );
40 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
41 printf( win->GetClassInfo()->GetClassName() );
42 printf( ".\n" );
43 */
44
45 win->GtkOnSize( alloc->width, alloc->height );
46 };
47
48 //-----------------------------------------------------------------------------
49 // delete
50
51 bool gtk_frame_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxFrame *win )
52 {
53 /*
54 printf( "OnDelete from " );
55 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
56 printf( win->GetClassInfo()->GetClassName() );
57 printf( ".\n" );
58 */
59
60 win->Close();
61
62 return TRUE;
63 };
64
65 //-----------------------------------------------------------------------------
66
67 BEGIN_EVENT_TABLE(wxFrame, wxWindow)
68 EVT_CLOSE(wxFrame::OnCloseWindow)
69 EVT_SIZE(wxFrame::OnSize)
70 END_EVENT_TABLE()
71
72 IMPLEMENT_DYNAMIC_CLASS(wxFrame,wxWindow)
73
74 wxFrame::wxFrame(void)
75 {
76 m_doingOnSize = FALSE;
77 m_frameMenuBar = NULL;
78 m_frameStatusBar = NULL;
79 m_sizeSet = FALSE;
80 wxTopLevelWindows.Insert( this );
81 };
82
83 wxFrame::wxFrame( wxWindow *parent, const wxWindowID id, const wxString &title,
84 const wxPoint &pos, const wxSize &size,
85 const long style, const wxString &name )
86 {
87 m_sizeSet = FALSE;
88 Create( parent, id, title, pos, size, style, name );
89 wxTopLevelWindows.Insert( this );
90 };
91
92 bool wxFrame::Create( wxWindow *parent, const wxWindowID id, const wxString &title,
93 const wxPoint &pos, const wxSize &size,
94 const long style, const wxString &name )
95 {
96 m_needParent = FALSE;
97 m_mainWindow = NULL;
98 m_wxwindow = NULL;
99
100 PreCreation( parent, id, pos, size, style, name );
101
102 m_doingOnSize = FALSE;
103
104 m_title = title;
105
106 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
107 if ((size.x != -1) && (size.y != -1))
108 gtk_widget_set_usize( m_widget, m_width, m_height );
109 if ((pos.x != -1) && (pos.y != -1))
110 gtk_widget_set_uposition( m_widget, m_x, m_y );
111
112 gtk_window_set_title( GTK_WINDOW(m_widget), title );
113 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
114
115 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
116
117 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
118 GTK_SIGNAL_FUNC(gtk_frame_delete_callback), (gpointer)this );
119
120 m_mainWindow = gtk_myfixed_new();
121 gtk_widget_show( m_mainWindow );
122 GTK_WIDGET_UNSET_FLAGS( m_mainWindow, GTK_CAN_FOCUS );
123
124 gtk_container_add( GTK_CONTAINER(m_widget), m_mainWindow );
125 gtk_widget_set_uposition( m_mainWindow, 0, 0 );
126
127 m_wxwindow = gtk_myfixed_new();
128 gtk_widget_show( m_wxwindow );
129 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
130
131 gtk_container_add( GTK_CONTAINER(m_mainWindow), m_wxwindow );
132
133 m_frameMenuBar = NULL;
134 m_frameStatusBar = NULL;
135
136 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
137 GTK_SIGNAL_FUNC(gtk_frame_size_callback), (gpointer)this );
138
139 PostCreation();
140
141 gtk_widget_realize( m_mainWindow );
142
143 return TRUE;
144 };
145
146 wxFrame::~wxFrame(void)
147 {
148 if (m_frameMenuBar) delete m_frameMenuBar;
149 if (m_frameStatusBar) delete m_frameStatusBar;
150
151 // if (m_mainWindow) gtk_widget_destroy( m_mainWindow );
152
153 wxTopLevelWindows.DeleteObject( this );
154 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
155 };
156
157 bool wxFrame::Show( const bool show )
158 {
159 if (show)
160 {
161 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
162 m_sizeSet = FALSE;
163 ProcessEvent( event );
164 };
165 return wxWindow::Show( show );
166 };
167
168 void wxFrame::Enable( const bool enable )
169 {
170 wxWindow::Enable( enable );
171 gtk_widget_set_sensitive( m_mainWindow, enable );
172 };
173
174 void wxFrame::OnCloseWindow( wxCloseEvent& WXUNUSED(event) )
175 {
176 this->Destroy();
177 };
178
179 bool wxFrame::Destroy(void)
180 {
181 if (!wxPendingDelete.Member(this))
182 wxPendingDelete.Append(this);
183
184 return TRUE;
185 }
186
187 void wxFrame::GetClientSize( int *width, int *height ) const
188 {
189 wxWindow::GetClientSize( width, height );
190 if (height)
191 {
192 if (m_frameMenuBar) (*height) -= wxMENU_HEIGHT;
193 if (m_frameStatusBar) (*height) -= wxSTATUS_HEIGHT;
194 };
195 };
196
197 void wxFrame::GtkOnSize( int width, int height )
198 {
199 if ((m_height == height) && (m_width == width) &&
200 (m_sizeSet)) return;
201 if (!m_mainWindow) return;
202 if (!m_wxwindow) return;
203
204 m_width = width;
205 m_height = height;
206
207 gtk_widget_set_usize( m_widget, width, height );
208
209 int main_x = 0;
210 int main_y = 0;
211 int main_height = height;
212 int main_width = width;
213
214 // This emulates Windows behaviour:
215 // The menu bar is part of the main window, but the status bar
216 // is on the implementation side in the client area. The
217 // function GetClientSize returns the size of the client area
218 // minus the status bar height. Under wxGTK, the main window
219 // is represented by m_mainWindow. The menubar is inserted
220 // into m_mainWindow whereas the statusbar is insertes into
221 // m_wxwindow just like any other window.
222
223 // not really needed
224 gtk_widget_set_usize( m_mainWindow, width, height );
225
226 if (m_frameMenuBar)
227 {
228 main_y = wxMENU_HEIGHT;
229 main_height -= wxMENU_HEIGHT;
230 };
231
232 gtk_widget_set_uposition( GTK_WIDGET(m_wxwindow), main_x, main_y );
233 gtk_widget_set_usize( GTK_WIDGET(m_wxwindow), main_width, main_height );
234
235 if (m_frameMenuBar)
236 {
237 gtk_widget_set_uposition( m_frameMenuBar->m_widget, 1, 1 );
238 gtk_widget_set_usize( m_frameMenuBar->m_widget, width-2, wxMENU_HEIGHT-2 );
239 };
240
241 if (m_frameStatusBar)
242 {
243 m_frameStatusBar->SetSize( 0, main_height-wxSTATUS_HEIGHT, width, wxSTATUS_HEIGHT );
244 };
245
246 m_sizeSet = TRUE;
247
248 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
249 event.SetEventObject( this );
250 ProcessEvent( event );
251 };
252
253 void wxFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
254 {
255 wxWindow *child = NULL;
256 int noChildren = 0;
257 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
258 {
259 wxWindow *win = (wxWindow *)node->Data();
260 if (!win->IsKindOf(CLASSINFO(wxFrame)) &&
261 !win->IsKindOf(CLASSINFO(wxDialog))
262 /* && (win != m_frameMenuBar) &&
263 (win != m_frameStatusBar) not in m_children anyway */
264 )
265 {
266 child = win;
267 noChildren ++;
268 };
269 }
270 ;
271
272 if ((child) && (noChildren == 1))
273 {
274 int client_x, client_y;
275
276 GetClientSize(&client_x, &client_y);
277
278 child->SetSize( 1, 1, client_x-2, client_y);
279 }
280 ;
281 };
282
283 void SetInvokingWindow( wxMenu *menu, wxWindow *win )
284 {
285 menu->SetInvokingWindow( win );
286 wxNode *node = menu->m_items.First();
287 while (node)
288 {
289 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
290 if (menuitem->m_isSubMenu) SetInvokingWindow( menuitem->m_subMenu, win );
291 node = node->Next();
292 };
293 };
294
295 void wxFrame::SetMenuBar( wxMenuBar *menuBar )
296 {
297 m_frameMenuBar = menuBar;
298
299 wxNode *node = m_frameMenuBar->m_menus.First();
300 while (node)
301 {
302 wxMenu *menu = (wxMenu*)node->Data();
303 SetInvokingWindow( menu, this );
304 node = node->Next();
305 };
306
307 m_frameMenuBar->m_parent = this;
308 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow),
309 m_frameMenuBar->m_widget, m_frameMenuBar->m_x, m_frameMenuBar->m_y );
310 };
311
312 bool wxFrame::CreateStatusBar( const int number )
313 {
314 if (m_frameStatusBar)
315 delete m_frameStatusBar;
316
317 m_frameStatusBar = new wxStatusBar( this, -1, wxPoint(0,0), wxSize(100,20) );
318
319 m_frameStatusBar->SetFieldsCount( number );
320 return TRUE;
321 };
322
323 void wxFrame::SetStatusText( const wxString &text, const int number )
324 {
325 if (m_frameStatusBar) m_frameStatusBar->SetStatusText( text, number );
326 };
327
328 void wxFrame::SetStatusWidths( const int n, const int *width )
329 {
330 if (m_frameStatusBar) m_frameStatusBar->SetStatusWidths( n, width );
331 };
332
333 wxStatusBar *wxFrame::GetStatusBar(void)
334 {
335 return m_frameStatusBar;
336 };
337
338 wxMenuBar *wxFrame::GetMenuBar(void)
339 {
340 return m_frameMenuBar;
341 };
342
343 void wxFrame::SetTitle( const wxString &title )
344 {
345 m_title = title;
346 gtk_window_set_title( GTK_WINDOW(m_widget), title );
347 };
348
349 wxString wxFrame::GetTitle(void) const
350 {
351 return (wxString&)m_title;
352 };
353