]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/mdi.cpp
add more flexible and safer template Connect() overloads (#10000)
[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 68 event1.SetEventObject( child);
937013e0 69 child->HandleWindowEvent( event1 );
e90196a5 70 }
f6bcfd97 71
e90196a5 72 // send activate event to new child
f6bcfd97 73
d2824cdb
VZ
74 wxMDIClientWindowBase * const client_window = parent->GetClientWindow();
75 if ( !client_window )
e90196a5
RR
76 return;
77
d2824cdb 78 child = NULL;
e90196a5 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 102 event2.SetEventObject( child);
937013e0 103 child->HandleWindowEvent( 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;
d2824cdb 116 m_clientWindow = NULL;
ff7b1510 117}
c801d85f 118
f6bcfd97
BP
119bool wxMDIParentFrame::Create(wxWindow *parent,
120 wxWindowID id,
121 const wxString& title,
122 const wxPoint& pos,
123 const wxSize& size,
124 long style,
125 const wxString& name )
c801d85f 126{
6e42617a
VZ
127 if ( !wxFrame::Create( parent, id, title, pos, size, style, name ) )
128 return false;
a3622daa 129
6e42617a 130 m_clientWindow = OnCreateClient();
d2824cdb
VZ
131 if ( !m_clientWindow->CreateClient(this, GetWindowStyleFlag()) )
132 return false;
a3622daa 133
d2824cdb 134 return true;
ff7b1510 135}
c801d85f 136
716b7364 137void wxMDIParentFrame::GtkOnSize( int x, int y, int width, int height )
c801d85f 138{
83624f79 139 wxFrame::GtkOnSize( x, y, width, height );
a0fdacee 140
ab2b3dd4
RR
141 wxMDIChildFrame *child_frame = GetActiveChild();
142 if (!child_frame) return;
a0fdacee 143
ab2b3dd4
RR
144 wxMenuBar *menu_bar = child_frame->m_menuBar;
145 if (!menu_bar) return;
a2053b27 146 if (!menu_bar->m_widget) return;
a0fdacee 147
121a3581
RR
148 menu_bar->m_x = 0;
149 menu_bar->m_y = 0;
150 menu_bar->m_width = m_width;
151 menu_bar->m_height = wxMENU_HEIGHT;
f6bcfd97
BP
152 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
153 menu_bar->m_widget,
f03fc89f 154 0, 0, m_width, wxMENU_HEIGHT );
ab2b3dd4
RR
155}
156
157void wxMDIParentFrame::OnInternalIdle()
158{
159 /* if a an MDI child window has just been inserted
160 it has to be brought to the top in idle time. we
161 simply set the last notebook page active as new
162 pages can only be appended at the end */
163
164 if (m_justInserted)
83624f79 165 {
a2053b27 166 GtkNotebook *notebook = GTK_NOTEBOOK(m_clientWindow->m_widget);
a0fdacee
VZ
167 gtk_notebook_set_page( notebook, g_list_length( notebook->children ) - 1 );
168
147d6669 169 /* need to set the menubar of the child */
1d608029 170 wxMDIChildFrame *active_child_frame = GetActiveChild();
1b10056f 171 if (active_child_frame != NULL)
147d6669 172 {
1b10056f
RD
173 wxMenuBar *menu_bar = active_child_frame->m_menuBar;
174 if (menu_bar)
175 {
176 menu_bar->m_width = m_width;
177 menu_bar->m_height = wxMENU_HEIGHT;
178 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
179 menu_bar->m_widget,
180 0, 0, m_width, wxMENU_HEIGHT );
181 menu_bar->SetInvokingWindow(active_child_frame);
182 }
147d6669 183 }
b1d4dd7a 184 m_justInserted = false;
a0fdacee 185 return;
83624f79 186 }
a0fdacee 187
ab2b3dd4 188 wxFrame::OnInternalIdle();
c626a8b7 189
ab2b3dd4 190 wxMDIChildFrame *active_child_frame = GetActiveChild();
b1d4dd7a 191 bool visible_child_menu = false;
a0fdacee 192
222ed1d6 193 wxWindowList::compatibility_iterator node = m_clientWindow->GetChildren().GetFirst();
ab2b3dd4 194 while (node)
83624f79 195 {
b1d4dd7a
RL
196 wxMDIChildFrame *child_frame = wxDynamicCast( node->GetData(), wxMDIChildFrame );
197
f6bcfd97 198 if ( child_frame )
a0fdacee 199 {
f6bcfd97
BP
200 wxMenuBar *menu_bar = child_frame->m_menuBar;
201 if ( menu_bar )
f03fc89f 202 {
f6bcfd97
BP
203 if (child_frame == active_child_frame)
204 {
b1d4dd7a 205 if (menu_bar->Show(true))
f6bcfd97
BP
206 {
207 menu_bar->m_width = m_width;
208 menu_bar->m_height = wxMENU_HEIGHT;
209 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
210 menu_bar->m_widget,
211 0, 0, m_width, wxMENU_HEIGHT );
212 menu_bar->SetInvokingWindow( child_frame );
213 }
b1d4dd7a 214 visible_child_menu = true;
f6bcfd97
BP
215 }
216 else
217 {
b1d4dd7a 218 if (menu_bar->Show(false))
f6bcfd97
BP
219 {
220 menu_bar->UnsetInvokingWindow( child_frame );
221 }
222 }
f03fc89f 223 }
a0fdacee 224 }
f6bcfd97 225
b1d4dd7a 226 node = node->GetNext();
83624f79 227 }
a0fdacee 228
e27ce4e9 229 /* show/hide parent menu bar as required */
5bd9e519
RR
230 if ((m_frameMenuBar) &&
231 (m_frameMenuBar->IsShown() == visible_child_menu))
232 {
233 if (visible_child_menu)
f6bcfd97 234 {
b1d4dd7a 235 m_frameMenuBar->Show( false );
f6bcfd97
BP
236 m_frameMenuBar->UnsetInvokingWindow( this );
237 }
238 else
239 {
b1d4dd7a 240 m_frameMenuBar->Show( true );
f6bcfd97
BP
241 m_frameMenuBar->SetInvokingWindow( this );
242
243 m_frameMenuBar->m_width = m_width;
244 m_frameMenuBar->m_height = wxMENU_HEIGHT;
245 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
246 m_frameMenuBar->m_widget,
247 0, 0, m_width, wxMENU_HEIGHT );
248 }
5bd9e519 249 }
ff7b1510 250}
716b7364 251
ab2b3dd4
RR
252wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
253{
d2824cdb 254 if (!m_clientWindow) return NULL;
a0fdacee 255
a2053b27 256 GtkNotebook *notebook = GTK_NOTEBOOK(m_clientWindow->m_widget);
d2824cdb 257 if (!notebook) return NULL;
a0fdacee 258
ab2b3dd4 259 gint i = gtk_notebook_get_current_page( notebook );
d2824cdb 260 if (i < 0) return NULL;
a0fdacee 261
ab2b3dd4 262 GtkNotebookPage* page = (GtkNotebookPage*) (g_list_nth(notebook->children,i)->data);
d2824cdb 263 if (!page) return NULL;
a0fdacee 264
222ed1d6 265 wxWindowList::compatibility_iterator node = m_clientWindow->GetChildren().GetFirst();
ab2b3dd4
RR
266 while (node)
267 {
b1d4dd7a
RL
268 wxMDIChildFrame *child_frame = wxDynamicCast( node->GetData(), wxMDIChildFrame );
269
270 wxASSERT_MSG( child_frame, _T("child is not a wxMDIChildFrame") );
271
ab2b3dd4
RR
272 if (child_frame->m_page == page)
273 return child_frame;
b1d4dd7a 274 node = node->GetNext();
ab2b3dd4 275 }
a0fdacee 276
d2824cdb 277 return NULL;
ff7b1510 278}
c801d85f 279
ab2b3dd4 280void wxMDIParentFrame::ActivateNext()
c801d85f 281{
83624f79 282 if (m_clientWindow)
a2053b27 283 gtk_notebook_next_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
ff7b1510 284}
c801d85f 285
ab2b3dd4 286void wxMDIParentFrame::ActivatePrevious()
716b7364 287{
83624f79 288 if (m_clientWindow)
a2053b27 289 gtk_notebook_prev_page( GTK_NOTEBOOK(m_clientWindow->m_widget) );
ff7b1510 290}
716b7364 291
c801d85f
KB
292//-----------------------------------------------------------------------------
293// wxMDIChildFrame
294//-----------------------------------------------------------------------------
295
cf4219e7 296IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame,wxFrame)
c626a8b7 297
cf4219e7 298BEGIN_EVENT_TABLE(wxMDIChildFrame, wxFrame)
83624f79 299 EVT_ACTIVATE(wxMDIChildFrame::OnActivate)
f6bcfd97 300 EVT_MENU_HIGHLIGHT_ALL(wxMDIChildFrame::OnMenuHighlight)
716b7364
RR
301END_EVENT_TABLE()
302
d2824cdb 303void wxMDIChildFrame::Init()
c801d85f 304{
d2824cdb
VZ
305 m_menuBar = NULL;
306 m_page = NULL;
72c23f8e 307}
c801d85f
KB
308
309bool wxMDIChildFrame::Create( wxMDIParentFrame *parent,
debe6624 310 wxWindowID id, const wxString& title,
33d0b396 311 const wxPoint& WXUNUSED(pos), const wxSize& size,
debe6624 312 long style, const wxString& name )
c801d85f 313{
83624f79 314 m_title = title;
c626a8b7 315
83624f79 316 return wxWindow::Create( parent->GetClientWindow(), id, wxDefaultPosition, size, style, name );
ff7b1510 317}
c801d85f 318
d2824cdb 319wxMDIChildFrame::~wxMDIChildFrame()
716b7364 320{
d2824cdb 321 delete m_menuBar;
716b7364 322}
c626a8b7 323
716b7364
RR
324void wxMDIChildFrame::SetMenuBar( wxMenuBar *menu_bar )
325{
223d09f6 326 wxASSERT_MSG( m_menuBar == NULL, wxT("Only one menubar allowed") );
5bd9e519 327
83624f79 328 m_menuBar = menu_bar;
a3622daa 329
83624f79 330 if (m_menuBar)
716b7364 331 {
f03fc89f 332 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
83624f79 333
5bd9e519 334 m_menuBar->SetParent( mdi_frame );
c626a8b7 335
ab2b3dd4 336 /* insert the invisible menu bar into the _parent_ mdi frame */
f6bcfd97
BP
337 gtk_pizza_put( GTK_PIZZA(mdi_frame->m_mainWidget),
338 m_menuBar->m_widget,
a2053b27 339 0, 0, mdi_frame->m_width, wxMENU_HEIGHT );
716b7364 340 }
ff7b1510 341}
cf4219e7 342
33b64e6f 343wxMenuBar *wxMDIChildFrame::GetMenuBar() const
cf4219e7 344{
83624f79 345 return m_menuBar;
ff7b1510 346}
c801d85f 347
d2824cdb
VZ
348GtkNotebook *wxMDIChildFrame::GTKGetNotebook() const
349{
350 wxMDIClientWindow * const
351 client = wxStaticCast(GetParent(), wxMDIClientWindow);
352 wxCHECK( client, NULL );
353
354 return GTK_NOTEBOOK(client->m_widget);
355}
356
ab2b3dd4 357void wxMDIChildFrame::Activate()
c801d85f 358{
d2824cdb
VZ
359 GtkNotebook * const notebook = GTKGetNotebook();
360 wxCHECK_RET( notebook, "no parent notebook?" );
361
9e691f46 362 gint pageno = gtk_notebook_page_num( notebook, m_widget );
adde8c98 363 gtk_notebook_set_page( notebook, pageno );
ff7b1510 364}
c801d85f 365
f6bcfd97
BP
366void wxMDIChildFrame::OnActivate( wxActivateEvent& WXUNUSED(event) )
367{
368}
369
370void wxMDIChildFrame::OnMenuHighlight( wxMenuEvent& event )
9746a2ba 371{
f6bcfd97
BP
372#if wxUSE_STATUSBAR
373 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
722ed5be 374 if ( !ShowMenuHelp(event.GetMenuId()) )
f6bcfd97
BP
375 {
376 // we don't have any help text for this item, but may be the MDI frame
377 // does?
378 mdi_frame->OnMenuHighlight(event);
379 }
380#endif // wxUSE_STATUSBAR
381}
382
383void wxMDIChildFrame::SetTitle( const wxString &title )
384{
385 if ( title == m_title )
386 return;
387
388 m_title = title;
389
d2824cdb
VZ
390 GtkNotebook * const notebook = GTKGetNotebook();
391 wxCHECK_RET( notebook, "no parent notebook?" );
392
fab591c5 393 gtk_notebook_set_tab_label_text(notebook, m_widget, wxGTK_CONV( title ) );
ff7b1510 394}
9746a2ba 395
ab2b3dd4
RR
396//-----------------------------------------------------------------------------
397// "size_allocate"
398//-----------------------------------------------------------------------------
399
865bb325 400extern "C" {
ab2b3dd4
RR
401static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
402{
acfd422a
RR
403 if (g_isIdle) wxapp_install_idle_handler();
404
a2053b27
RR
405 if ((win->m_x == alloc->x) &&
406 (win->m_y == alloc->y) &&
407 (win->m_width == alloc->width) &&
408 (win->m_height == alloc->height) &&
409 (win->m_sizeSet))
ab2b3dd4
RR
410 {
411 return;
412 }
413
414 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
415}
865bb325 416}
ab2b3dd4 417
6ca41e57
RR
418//-----------------------------------------------------------------------------
419// InsertChild callback for wxMDIClientWindow
420//-----------------------------------------------------------------------------
421
422static void wxInsertChildInMDI( wxMDIClientWindow* parent, wxMDIChildFrame* child )
423{
72c23f8e 424 wxString s = child->GetTitle();
83624f79 425 if (s.IsNull()) s = _("MDI child");
6ca41e57 426
ed9b9841 427 GtkWidget *label_widget = gtk_label_new( s.mbc_str() );
83624f79 428 gtk_misc_set_alignment( GTK_MISC(label_widget), 0.0, 0.5 );
6ca41e57 429
a2053b27 430 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
83624f79 431 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
c626a8b7 432
a2053b27 433 GtkNotebook *notebook = GTK_NOTEBOOK(parent->m_widget);
c626a8b7 434
a2053b27 435 gtk_notebook_append_page( notebook, child->m_widget, label_widget );
6ca41e57 436
83624f79 437 child->m_page = (GtkNotebookPage*) (g_list_last(notebook->children)->data);
a0fdacee 438
f03fc89f 439 wxMDIParentFrame *parent_frame = (wxMDIParentFrame*) parent->GetParent();
b1d4dd7a 440 parent_frame->m_justInserted = true;
6ca41e57
RR
441}
442
c801d85f
KB
443//-----------------------------------------------------------------------------
444// wxMDIClientWindow
445//-----------------------------------------------------------------------------
446
447IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow,wxWindow)
448
debe6624 449bool wxMDIClientWindow::CreateClient( wxMDIParentFrame *parent, long style )
c801d85f 450{
b1d4dd7a 451 m_needParent = true;
c626a8b7 452
83624f79 453 m_insertCallback = (wxInsertChildFunction)wxInsertChildInMDI;
a3622daa 454
4dcaf11a 455 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
72c23f8e 456 !CreateBase( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("wxMDIClientWindow") ))
4dcaf11a 457 {
223d09f6 458 wxFAIL_MSG( wxT("wxMDIClientWindow creation failed") );
b1d4dd7a 459 return false;
4dcaf11a 460 }
c801d85f 461
83624f79 462 m_widget = gtk_notebook_new();
a3622daa 463
5e014a0c
RR
464 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
465 GTK_SIGNAL_FUNC(gtk_mdi_page_change_callback), (gpointer)parent );
466
83624f79 467 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
a3622daa 468
f03fc89f 469 m_parent->DoAddChild( this );
c626a8b7 470
83624f79 471 PostCreation();
a3622daa 472
b1d4dd7a 473 Show( true );
a3622daa 474
b1d4dd7a 475 return true;
ff7b1510 476}
c801d85f 477
dcf924a3 478#endif