]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/mdi.cpp
fixed warnings, reformatted braces
[wxWidgets.git] / src / gtk1 / mdi.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
3cbab641 2// Name: src/gtk1/mdi.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
a81258be
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
88a7a4e1
WS
13#if wxUSE_MDI
14
c801d85f 15#include "wx/mdi.h"
dcf924a3 16
88a7a4e1
WS
17#ifndef WX_PRECOMP
18 #include "wx/intl.h"
3b3dc801 19 #include "wx/menu.h"
fdf565fe 20 #include "wx/dialog.h"
88a7a4e1 21#endif
dcf924a3 22
88a7a4e1 23#include "wx/notebook.h"
3cbab641 24#include "wx/gtk1/private.h"
716b7364 25
16c1f79c
RR
26#include <glib.h>
27#include <gdk/gdk.h>
28#include <gtk/gtk.h>
3cbab641 29#include "wx/gtk1/win_gtk.h"
83624f79 30
6ca41e57
RR
31//-----------------------------------------------------------------------------
32// constants
33//-----------------------------------------------------------------------------
34
a0fdacee 35const int wxMENU_HEIGHT = 27;
6ca41e57 36
acfd422a
RR
37//-----------------------------------------------------------------------------
38// idle system
39//-----------------------------------------------------------------------------
40
41extern void wxapp_install_idle_handler();
42extern bool g_isIdle;
43
6ca41e57
RR
44//-----------------------------------------------------------------------------
45// globals
716b7364
RR
46//-----------------------------------------------------------------------------
47
5e014a0c
RR
48//-----------------------------------------------------------------------------
49// "switch_page"
50//-----------------------------------------------------------------------------
51
865bb325 52extern "C" {
f6bcfd97 53static void
7941ba11 54gtk_mdi_page_change_callback( GtkNotebook *WXUNUSED(widget),
e90196a5 55 GtkNotebookPage *page,
f6bcfd97
BP
56 gint WXUNUSED(page_num),
57 wxMDIParentFrame *parent )
5e014a0c 58{
f6bcfd97 59 if (g_isIdle)
5e014a0c
RR
60 wxapp_install_idle_handler();
61
e90196a5
RR
62 // send deactivate event to old child
63
5e014a0c 64 wxMDIChildFrame *child = parent->GetActiveChild();
e90196a5
RR
65 if (child)
66 {
b1d4dd7a 67 wxActivateEvent event1( wxEVT_ACTIVATE, false, child->GetId() );
e90196a5
RR
68 event1.SetEventObject( child);
69 child->GetEventHandler()->ProcessEvent( event1 );
70 }
f6bcfd97 71
e90196a5 72 // send activate event to new child
f6bcfd97 73
e90196a5
RR
74 wxMDIClientWindow *client_window = parent->GetClientWindow();
75 if (!client_window)
76 return;
77
78 child = (wxMDIChildFrame*) NULL;
79
222ed1d6 80 wxWindowList::compatibility_iterator node = client_window->GetChildren().GetFirst();
e90196a5
RR
81 while (node)
82 {
b1d4dd7a 83 wxMDIChildFrame *child_frame = wxDynamicCast( node->GetData(), wxMDIChildFrame );
1631916e
CE
84 // CE: we come here in the destructor with a null child_frame - I think because
85 // gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page", (see below)
86 // isn't deleted early enough
87 if (!child_frame)
88 return ;
b1d4dd7a 89
e90196a5 90 if (child_frame->m_page == page)
f6bcfd97 91 {
e90196a5 92 child = child_frame;
f6bcfd97
BP
93 break;
94 }
b1d4dd7a 95 node = node->GetNext();
e90196a5 96 }
f6bcfd97 97
e90196a5
RR
98 if (!child)
99 return;
f6bcfd97 100
b1d4dd7a 101 wxActivateEvent event2( wxEVT_ACTIVATE, true, child->GetId() );
e90196a5
RR
102 event2.SetEventObject( child);
103 child->GetEventHandler()->ProcessEvent( event2 );
5e014a0c 104}
865bb325 105}
5e014a0c 106
6ca41e57
RR
107//-----------------------------------------------------------------------------
108// wxMDIParentFrame
33d0b396
RR
109//-----------------------------------------------------------------------------
110
c801d85f
KB
111IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame,wxFrame)
112
f6bcfd97 113void wxMDIParentFrame::Init()
c801d85f 114{
b1d4dd7a 115 m_justInserted = false;
83624f79 116 m_clientWindow = (wxMDIClientWindow *) NULL;
ff7b1510 117}
c801d85f 118
ab2b3dd4 119wxMDIParentFrame::~wxMDIParentFrame()
c801d85f 120{
ff7b1510 121}
c801d85f 122
f6bcfd97
BP
123bool wxMDIParentFrame::Create(wxWindow *parent,
124 wxWindowID id,
125 const wxString& title,
126 const wxPoint& pos,
127 const wxSize& size,
128 long style,
129 const wxString& name )
c801d85f 130{
83624f79 131 wxFrame::Create( parent, id, title, pos, size, style, name );
a3622daa 132
83624f79 133 OnCreateClient();
a3622daa 134
b1d4dd7a 135 return true;
ff7b1510 136}
c801d85f 137
716b7364 138void wxMDIParentFrame::GtkOnSize( int x, int y, int width, int height )
c801d85f 139{
83624f79 140 wxFrame::GtkOnSize( x, y, width, height );
a0fdacee 141
ab2b3dd4
RR
142 wxMDIChildFrame *child_frame = GetActiveChild();
143 if (!child_frame) return;
a0fdacee 144
ab2b3dd4
RR
145 wxMenuBar *menu_bar = child_frame->m_menuBar;
146 if (!menu_bar) return;
a2053b27 147 if (!menu_bar->m_widget) return;
a0fdacee 148
121a3581
RR
149 menu_bar->m_x = 0;
150 menu_bar->m_y = 0;
151 menu_bar->m_width = m_width;
152 menu_bar->m_height = wxMENU_HEIGHT;
f6bcfd97
BP
153 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
154 menu_bar->m_widget,
f03fc89f 155 0, 0, m_width, wxMENU_HEIGHT );
ab2b3dd4
RR
156}
157
158void wxMDIParentFrame::OnInternalIdle()
159{
160 /* if a an MDI child window has just been inserted
161 it has to be brought to the top in idle time. we
162 simply set the last notebook page active as new
163 pages can only be appended at the end */
164
165 if (m_justInserted)
83624f79 166 {
a2053b27 167 GtkNotebook *notebook = GTK_NOTEBOOK(m_clientWindow->m_widget);
a0fdacee
VZ
168 gtk_notebook_set_page( notebook, g_list_length( notebook->children ) - 1 );
169
147d6669 170 /* need to set the menubar of the child */
1d608029 171 wxMDIChildFrame *active_child_frame = GetActiveChild();
1b10056f 172 if (active_child_frame != NULL)
147d6669 173 {
1b10056f
RD
174 wxMenuBar *menu_bar = active_child_frame->m_menuBar;
175 if (menu_bar)
176 {
177 menu_bar->m_width = m_width;
178 menu_bar->m_height = wxMENU_HEIGHT;
179 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
180 menu_bar->m_widget,
181 0, 0, m_width, wxMENU_HEIGHT );
182 menu_bar->SetInvokingWindow(active_child_frame);
183 }
147d6669 184 }
b1d4dd7a 185 m_justInserted = false;
a0fdacee 186 return;
83624f79 187 }
a0fdacee 188
ab2b3dd4 189 wxFrame::OnInternalIdle();
c626a8b7 190
ab2b3dd4 191 wxMDIChildFrame *active_child_frame = GetActiveChild();
b1d4dd7a 192 bool visible_child_menu = false;
a0fdacee 193
222ed1d6 194 wxWindowList::compatibility_iterator node = m_clientWindow->GetChildren().GetFirst();
ab2b3dd4 195 while (node)
83624f79 196 {
b1d4dd7a
RL
197 wxMDIChildFrame *child_frame = wxDynamicCast( node->GetData(), wxMDIChildFrame );
198
f6bcfd97 199 if ( child_frame )
a0fdacee 200 {
f6bcfd97
BP
201 wxMenuBar *menu_bar = child_frame->m_menuBar;
202 if ( menu_bar )
f03fc89f 203 {
f6bcfd97
BP
204 if (child_frame == active_child_frame)
205 {
b1d4dd7a 206 if (menu_bar->Show(true))
f6bcfd97
BP
207 {
208 menu_bar->m_width = m_width;
209 menu_bar->m_height = wxMENU_HEIGHT;
210 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
211 menu_bar->m_widget,
212 0, 0, m_width, wxMENU_HEIGHT );
213 menu_bar->SetInvokingWindow( child_frame );
214 }
b1d4dd7a 215 visible_child_menu = true;
f6bcfd97
BP
216 }
217 else
218 {
b1d4dd7a 219 if (menu_bar->Show(false))
f6bcfd97
BP
220 {
221 menu_bar->UnsetInvokingWindow( child_frame );
222 }
223 }
f03fc89f 224 }
a0fdacee 225 }
f6bcfd97 226
b1d4dd7a 227 node = node->GetNext();
83624f79 228 }
a0fdacee 229
e27ce4e9 230 /* show/hide parent menu bar as required */
5bd9e519
RR
231 if ((m_frameMenuBar) &&
232 (m_frameMenuBar->IsShown() == visible_child_menu))
233 {
234 if (visible_child_menu)
f6bcfd97 235 {
b1d4dd7a 236 m_frameMenuBar->Show( false );
f6bcfd97
BP
237 m_frameMenuBar->UnsetInvokingWindow( this );
238 }
239 else
240 {
b1d4dd7a 241 m_frameMenuBar->Show( true );
f6bcfd97
BP
242 m_frameMenuBar->SetInvokingWindow( this );
243
244 m_frameMenuBar->m_width = m_width;
245 m_frameMenuBar->m_height = wxMENU_HEIGHT;
246 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
247 m_frameMenuBar->m_widget,
248 0, 0, m_width, wxMENU_HEIGHT );
249 }
5bd9e519 250 }
ff7b1510 251}
716b7364 252
40d432c4 253void wxMDIParentFrame::DoGetClientSize(int *width, int *height ) const
c801d85f 254{
40d432c4 255 wxFrame::DoGetClientSize( width, height );
ff7b1510 256}
c801d85f 257
ab2b3dd4
RR
258wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
259{
260 if (!m_clientWindow) return (wxMDIChildFrame*) NULL;
a0fdacee 261
a2053b27 262 GtkNotebook *notebook = GTK_NOTEBOOK(m_clientWindow->m_widget);
ab2b3dd4 263 if (!notebook) return (wxMDIChildFrame*) NULL;
a0fdacee 264
ab2b3dd4
RR
265 gint i = gtk_notebook_get_current_page( notebook );
266 if (i < 0) return (wxMDIChildFrame*) NULL;
a0fdacee 267
ab2b3dd4
RR
268 GtkNotebookPage* page = (GtkNotebookPage*) (g_list_nth(notebook->children,i)->data);
269 if (!page) return (wxMDIChildFrame*) NULL;
a0fdacee 270
222ed1d6 271 wxWindowList::compatibility_iterator node = m_clientWindow->GetChildren().GetFirst();
ab2b3dd4
RR
272 while (node)
273 {
b1d4dd7a
RL
274 wxMDIChildFrame *child_frame = wxDynamicCast( node->GetData(), wxMDIChildFrame );
275
276 wxASSERT_MSG( child_frame, _T("child is not a wxMDIChildFrame") );
277
ab2b3dd4
RR
278 if (child_frame->m_page == page)
279 return child_frame;
b1d4dd7a 280 node = node->GetNext();
ab2b3dd4 281 }
a0fdacee 282
ab2b3dd4 283 return (wxMDIChildFrame*) NULL;
ff7b1510 284}
c801d85f 285
ab2b3dd4 286wxMDIClientWindow *wxMDIParentFrame::GetClientWindow() const
c801d85f 287{
83624f79 288 return m_clientWindow;
ff7b1510 289}
c801d85f 290
ab2b3dd4 291wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
c801d85f 292{
83624f79
RR
293 m_clientWindow = new wxMDIClientWindow( this );
294 return m_clientWindow;
ff7b1510 295}
c801d85f 296
ab2b3dd4 297void wxMDIParentFrame::ActivateNext()
c801d85f 298{
83624f79 299 if (m_clientWindow)
a2053b27 300 gtk_notebook_next_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
ff7b1510 301}
c801d85f 302
ab2b3dd4 303void wxMDIParentFrame::ActivatePrevious()
716b7364 304{
83624f79 305 if (m_clientWindow)
a2053b27 306 gtk_notebook_prev_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
ff7b1510 307}
716b7364 308
c801d85f
KB
309//-----------------------------------------------------------------------------
310// wxMDIChildFrame
311//-----------------------------------------------------------------------------
312
cf4219e7 313IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame,wxFrame)
c626a8b7 314
cf4219e7 315BEGIN_EVENT_TABLE(wxMDIChildFrame, wxFrame)
83624f79 316 EVT_ACTIVATE(wxMDIChildFrame::OnActivate)
f6bcfd97 317 EVT_MENU_HIGHLIGHT_ALL(wxMDIChildFrame::OnMenuHighlight)
716b7364
RR
318END_EVENT_TABLE()
319
ab2b3dd4 320wxMDIChildFrame::wxMDIChildFrame()
c801d85f 321{
83624f79
RR
322 m_menuBar = (wxMenuBar *) NULL;
323 m_page = (GtkNotebookPage *) NULL;
ff7b1510 324}
c801d85f
KB
325
326wxMDIChildFrame::wxMDIChildFrame( wxMDIParentFrame *parent,
debe6624 327 wxWindowID id, const wxString& title,
33d0b396 328 const wxPoint& WXUNUSED(pos), const wxSize& size,
debe6624 329 long style, const wxString& name )
c801d85f 330{
83624f79
RR
331 m_menuBar = (wxMenuBar *) NULL;
332 m_page = (GtkNotebookPage *) NULL;
333 Create( parent, id, title, wxDefaultPosition, size, style, name );
ff7b1510 334}
c801d85f 335
ab2b3dd4 336wxMDIChildFrame::~wxMDIChildFrame()
c801d85f 337{
83624f79 338 if (m_menuBar)
83624f79 339 delete m_menuBar;
72c23f8e 340}
c801d85f
KB
341
342bool wxMDIChildFrame::Create( wxMDIParentFrame *parent,
debe6624 343 wxWindowID id, const wxString& title,
33d0b396 344 const wxPoint& WXUNUSED(pos), const wxSize& size,
debe6624 345 long style, const wxString& name )
c801d85f 346{
83624f79 347 m_title = title;
c626a8b7 348
83624f79 349 return wxWindow::Create( parent->GetClientWindow(), id, wxDefaultPosition, size, style, name );
ff7b1510 350}
c801d85f 351
ac0c857a
RR
352void wxMDIChildFrame::DoSetSize( int x, int y, int width, int height, int sizeFlags )
353{
354 wxWindow::DoSetSize( x, y, width, height, sizeFlags );
355}
356
357void wxMDIChildFrame::DoSetClientSize(int width, int height)
358{
359 wxWindow::DoSetClientSize( width, height );
360}
361
40d432c4 362void wxMDIChildFrame::DoGetClientSize( int *width, int *height ) const
c801d85f 363{
40d432c4 364 wxWindow::DoGetClientSize( width, height );
cf4219e7 365}
9746a2ba 366
7a903311 367void wxMDIChildFrame::AddChild( wxWindowBase *child )
716b7364 368{
7a903311 369 wxWindow::AddChild(child);
716b7364 370}
c626a8b7 371
716b7364
RR
372void wxMDIChildFrame::SetMenuBar( wxMenuBar *menu_bar )
373{
223d09f6 374 wxASSERT_MSG( m_menuBar == NULL, wxT("Only one menubar allowed") );
5bd9e519 375
83624f79 376 m_menuBar = menu_bar;
a3622daa 377
83624f79 378 if (m_menuBar)
716b7364 379 {
f03fc89f 380 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
83624f79 381
5bd9e519 382 m_menuBar->SetParent( mdi_frame );
c626a8b7 383
ab2b3dd4 384 /* insert the invisible menu bar into the _parent_ mdi frame */
f6bcfd97
BP
385 gtk_pizza_put( GTK_PIZZA(mdi_frame->m_mainWidget),
386 m_menuBar->m_widget,
a2053b27 387 0, 0, mdi_frame->m_width, wxMENU_HEIGHT );
716b7364 388 }
ff7b1510 389}
cf4219e7 390
33b64e6f 391wxMenuBar *wxMDIChildFrame::GetMenuBar() const
cf4219e7 392{
83624f79 393 return m_menuBar;
ff7b1510 394}
c801d85f 395
ab2b3dd4 396void wxMDIChildFrame::Activate()
c801d85f 397{
adde8c98 398 wxMDIParentFrame* parent = (wxMDIParentFrame*) GetParent();
97560ce3 399 GtkNotebook* notebook = GTK_NOTEBOOK(parent->m_widget);
9e691f46 400 gint pageno = gtk_notebook_page_num( notebook, m_widget );
adde8c98 401 gtk_notebook_set_page( notebook, pageno );
ff7b1510 402}
c801d85f 403
f6bcfd97
BP
404void wxMDIChildFrame::OnActivate( wxActivateEvent& WXUNUSED(event) )
405{
406}
407
408void wxMDIChildFrame::OnMenuHighlight( wxMenuEvent& event )
9746a2ba 409{
f6bcfd97
BP
410#if wxUSE_STATUSBAR
411 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
412 if ( !ShowMenuHelp(mdi_frame->GetStatusBar(), event.GetMenuId()) )
413 {
414 // we don't have any help text for this item, but may be the MDI frame
415 // does?
416 mdi_frame->OnMenuHighlight(event);
417 }
418#endif // wxUSE_STATUSBAR
419}
420
421void wxMDIChildFrame::SetTitle( const wxString &title )
422{
423 if ( title == m_title )
424 return;
425
426 m_title = title;
427
428 wxMDIParentFrame* parent = (wxMDIParentFrame*) GetParent();
429 GtkNotebook* notebook = GTK_NOTEBOOK(parent->m_widget);
fab591c5 430 gtk_notebook_set_tab_label_text(notebook, m_widget, wxGTK_CONV( title ) );
ff7b1510 431}
9746a2ba 432
ab2b3dd4
RR
433//-----------------------------------------------------------------------------
434// "size_allocate"
435//-----------------------------------------------------------------------------
436
865bb325 437extern "C" {
ab2b3dd4
RR
438static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
439{
acfd422a
RR
440 if (g_isIdle) wxapp_install_idle_handler();
441
a2053b27
RR
442 if ((win->m_x == alloc->x) &&
443 (win->m_y == alloc->y) &&
444 (win->m_width == alloc->width) &&
445 (win->m_height == alloc->height) &&
446 (win->m_sizeSet))
ab2b3dd4
RR
447 {
448 return;
449 }
450
451 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
452}
865bb325 453}
ab2b3dd4 454
6ca41e57
RR
455//-----------------------------------------------------------------------------
456// InsertChild callback for wxMDIClientWindow
457//-----------------------------------------------------------------------------
458
459static void wxInsertChildInMDI( wxMDIClientWindow* parent, wxMDIChildFrame* child )
460{
72c23f8e 461 wxString s = child->GetTitle();
83624f79 462 if (s.IsNull()) s = _("MDI child");
6ca41e57 463
ed9b9841 464 GtkWidget *label_widget = gtk_label_new( s.mbc_str() );
83624f79 465 gtk_misc_set_alignment( GTK_MISC(label_widget), 0.0, 0.5 );
6ca41e57 466
a2053b27 467 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
83624f79 468 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
c626a8b7 469
a2053b27 470 GtkNotebook *notebook = GTK_NOTEBOOK(parent->m_widget);
c626a8b7 471
a2053b27 472 gtk_notebook_append_page( notebook, child->m_widget, label_widget );
6ca41e57 473
83624f79 474 child->m_page = (GtkNotebookPage*) (g_list_last(notebook->children)->data);
a0fdacee 475
f03fc89f 476 wxMDIParentFrame *parent_frame = (wxMDIParentFrame*) parent->GetParent();
b1d4dd7a 477 parent_frame->m_justInserted = true;
6ca41e57
RR
478}
479
c801d85f
KB
480//-----------------------------------------------------------------------------
481// wxMDIClientWindow
482//-----------------------------------------------------------------------------
483
484IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow,wxWindow)
485
ab2b3dd4 486wxMDIClientWindow::wxMDIClientWindow()
c801d85f 487{
ff7b1510 488}
c801d85f 489
debe6624 490wxMDIClientWindow::wxMDIClientWindow( wxMDIParentFrame *parent, long style )
c801d85f 491{
83624f79 492 CreateClient( parent, style );
ff7b1510 493}
c801d85f 494
ab2b3dd4 495wxMDIClientWindow::~wxMDIClientWindow()
c801d85f 496{
1631916e 497
ff7b1510 498}
c801d85f 499
debe6624 500bool wxMDIClientWindow::CreateClient( wxMDIParentFrame *parent, long style )
c801d85f 501{
b1d4dd7a 502 m_needParent = true;
c626a8b7 503
83624f79 504 m_insertCallback = (wxInsertChildFunction)wxInsertChildInMDI;
a3622daa 505
4dcaf11a 506 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
72c23f8e 507 !CreateBase( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("wxMDIClientWindow") ))
4dcaf11a 508 {
223d09f6 509 wxFAIL_MSG( wxT("wxMDIClientWindow creation failed") );
b1d4dd7a 510 return false;
4dcaf11a 511 }
c801d85f 512
83624f79 513 m_widget = gtk_notebook_new();
a3622daa 514
5e014a0c
RR
515 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
516 GTK_SIGNAL_FUNC(gtk_mdi_page_change_callback), (gpointer)parent );
517
83624f79 518 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
a3622daa 519
f03fc89f 520 m_parent->DoAddChild( this );
c626a8b7 521
83624f79 522 PostCreation();
a3622daa 523
b1d4dd7a 524 Show( true );
a3622daa 525
b1d4dd7a 526 return true;
ff7b1510 527}
c801d85f 528
dcf924a3 529#endif