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