]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/mdi.cpp
corrected off by 1 error in cMB2WC() call (thanks valgrind)
[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
1d66b099
PC
295bool wxMDIParentFrame::HasVisibleMenubar() const
296{
297 if (wxFrame::HasVisibleMenubar())
298 return true;
299
300 wxMDIChildFrame* active_child_frame = GetActiveChild();
301 wxMenuBar* menubar = NULL;
302 if (active_child_frame)
303 menubar = active_child_frame->m_menuBar;
304 return menubar && menubar->IsShown();
305}
306
c801d85f
KB
307//-----------------------------------------------------------------------------
308// wxMDIChildFrame
309//-----------------------------------------------------------------------------
310
cf4219e7 311IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame,wxFrame)
c626a8b7 312
cf4219e7 313BEGIN_EVENT_TABLE(wxMDIChildFrame, wxFrame)
83624f79 314 EVT_ACTIVATE(wxMDIChildFrame::OnActivate)
f6bcfd97 315 EVT_MENU_HIGHLIGHT_ALL(wxMDIChildFrame::OnMenuHighlight)
716b7364
RR
316END_EVENT_TABLE()
317
ab2b3dd4 318wxMDIChildFrame::wxMDIChildFrame()
c801d85f 319{
83624f79
RR
320 m_menuBar = (wxMenuBar *) NULL;
321 m_page = (GtkNotebookPage *) NULL;
ff7b1510 322}
c801d85f
KB
323
324wxMDIChildFrame::wxMDIChildFrame( wxMDIParentFrame *parent,
debe6624 325 wxWindowID id, const wxString& title,
33d0b396 326 const wxPoint& WXUNUSED(pos), const wxSize& size,
debe6624 327 long style, const wxString& name )
c801d85f 328{
83624f79
RR
329 m_menuBar = (wxMenuBar *) NULL;
330 m_page = (GtkNotebookPage *) NULL;
331 Create( parent, id, title, wxDefaultPosition, size, style, name );
ff7b1510 332}
c801d85f 333
ab2b3dd4 334wxMDIChildFrame::~wxMDIChildFrame()
c801d85f 335{
83624f79 336 if (m_menuBar)
83624f79 337 delete m_menuBar;
72c23f8e 338}
c801d85f
KB
339
340bool wxMDIChildFrame::Create( wxMDIParentFrame *parent,
debe6624 341 wxWindowID id, const wxString& title,
33d0b396 342 const wxPoint& WXUNUSED(pos), const wxSize& size,
debe6624 343 long style, const wxString& name )
c801d85f 344{
83624f79 345 m_title = title;
c626a8b7 346
83624f79 347 return wxWindow::Create( parent->GetClientWindow(), id, wxDefaultPosition, size, style, name );
ff7b1510 348}
c801d85f 349
3811dacb
RR
350bool wxMDIChildFrame::Destroy()
351{
352 // delayed destruction: the frame will be deleted during
353 // the next idle loop iteration.
354 // I'm not sure if delayed destruction really makes so
355 // much sense for MDI child frames, actually, but hiding
356 // it doesn't make any sense.
357 if ( !wxPendingDelete.Member(this) )
358 wxPendingDelete.Append(this);
359
360 return true;
361}
362
ac0c857a
RR
363void wxMDIChildFrame::DoSetSize( int x, int y, int width, int height, int sizeFlags )
364{
365 wxWindow::DoSetSize( x, y, width, height, sizeFlags );
366}
367
7a903311 368void wxMDIChildFrame::AddChild( wxWindowBase *child )
716b7364 369{
7a903311 370 wxWindow::AddChild(child);
716b7364 371}
c626a8b7 372
716b7364
RR
373void wxMDIChildFrame::SetMenuBar( wxMenuBar *menu_bar )
374{
223d09f6 375 wxASSERT_MSG( m_menuBar == NULL, wxT("Only one menubar allowed") );
5bd9e519 376
83624f79 377 m_menuBar = menu_bar;
a3622daa 378
83624f79 379 if (m_menuBar)
716b7364 380 {
f03fc89f 381 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
83624f79 382
5bd9e519 383 m_menuBar->SetParent( mdi_frame );
c626a8b7 384
ab2b3dd4 385 /* insert the invisible menu bar into the _parent_ mdi frame */
f6bcfd97
BP
386 gtk_pizza_put( GTK_PIZZA(mdi_frame->m_mainWidget),
387 m_menuBar->m_widget,
a2053b27 388 0, 0, mdi_frame->m_width, wxMENU_HEIGHT );
716b7364 389 }
ff7b1510 390}
cf4219e7 391
33b64e6f 392wxMenuBar *wxMDIChildFrame::GetMenuBar() const
cf4219e7 393{
83624f79 394 return m_menuBar;
ff7b1510 395}
c801d85f 396
ab2b3dd4 397void wxMDIChildFrame::Activate()
c801d85f 398{
adde8c98 399 wxMDIParentFrame* parent = (wxMDIParentFrame*) GetParent();
97560ce3 400 GtkNotebook* notebook = GTK_NOTEBOOK(parent->m_widget);
9e691f46 401 gint pageno = gtk_notebook_page_num( notebook, m_widget );
38f1df7c 402 gtk_notebook_set_current_page( notebook, pageno );
ff7b1510 403}
c801d85f 404
f6bcfd97
BP
405void wxMDIChildFrame::OnActivate( wxActivateEvent& WXUNUSED(event) )
406{
407}
408
409void wxMDIChildFrame::OnMenuHighlight( wxMenuEvent& event )
9746a2ba 410{
f6bcfd97
BP
411#if wxUSE_STATUSBAR
412 wxMDIParentFrame *mdi_frame = (wxMDIParentFrame*)m_parent->GetParent();
722ed5be 413 if ( !ShowMenuHelp(event.GetMenuId()) )
f6bcfd97
BP
414 {
415 // we don't have any help text for this item, but may be the MDI frame
416 // does?
417 mdi_frame->OnMenuHighlight(event);
418 }
419#endif // wxUSE_STATUSBAR
420}
421
422void wxMDIChildFrame::SetTitle( const wxString &title )
423{
424 if ( title == m_title )
425 return;
426
427 m_title = title;
428
429 wxMDIParentFrame* parent = (wxMDIParentFrame*) GetParent();
430 GtkNotebook* notebook = GTK_NOTEBOOK(parent->m_widget);
fab591c5 431 gtk_notebook_set_tab_label_text(notebook, m_widget, wxGTK_CONV( title ) );
ff7b1510 432}
9746a2ba 433
ab2b3dd4
RR
434//-----------------------------------------------------------------------------
435// "size_allocate"
436//-----------------------------------------------------------------------------
437
865bb325 438extern "C" {
59678f3f 439static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxMDIChildFrame *win )
ab2b3dd4 440{
a2053b27
RR
441 if ((win->m_x == alloc->x) &&
442 (win->m_y == alloc->y) &&
443 (win->m_width == alloc->width) &&
444 (win->m_height == alloc->height) &&
445 (win->m_sizeSet))
ab2b3dd4
RR
446 {
447 return;
448 }
449
450 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
451}
865bb325 452}
ab2b3dd4 453
6ca41e57
RR
454//-----------------------------------------------------------------------------
455// InsertChild callback for wxMDIClientWindow
456//-----------------------------------------------------------------------------
457
c821db16 458static void wxInsertChildInMDI(wxWindow* parent, wxWindow* child)
6ca41e57 459{
c821db16
PC
460 wxMDIChildFrame* child_frame = wx_static_cast(wxMDIChildFrame*, child);
461 wxString s = child_frame->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
9fa72bd2
MR
467 g_signal_connect (child->m_widget, "size_allocate",
468 G_CALLBACK (gtk_page_size_callback), 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
c821db16 474 child_frame->m_page = (GtkNotebookPage*) (g_list_last(notebook->children)->data);
a0fdacee 475
c821db16 476 wxMDIParentFrame *parent_frame = wx_static_cast(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{
c821db16 502 m_insertCallback = wxInsertChildInMDI;
a3622daa 503
4dcaf11a 504 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
72c23f8e 505 !CreateBase( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("wxMDIClientWindow") ))
4dcaf11a 506 {
223d09f6 507 wxFAIL_MSG( wxT("wxMDIClientWindow creation failed") );
b1d4dd7a 508 return false;
4dcaf11a 509 }
c801d85f 510
83624f79 511 m_widget = gtk_notebook_new();
a3622daa 512
9fa72bd2
MR
513 g_signal_connect (m_widget, "switch_page",
514 G_CALLBACK (gtk_mdi_page_change_callback), parent);
5e014a0c 515
83624f79 516 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
a3622daa 517
f03fc89f 518 m_parent->DoAddChild( this );
c626a8b7 519
83624f79 520 PostCreation();
a3622daa 521
b1d4dd7a 522 Show( true );
a3622daa 523
b1d4dd7a 524 return true;
ff7b1510 525}
c801d85f 526
e8375af8 527#endif // wxUSE_MDI