| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/msw/mdi.cpp |
| 3 | // Purpose: MDI classes for wxMSW |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: Vadim Zeitlin on 2008-11-04 to use the base classes |
| 6 | // Created: 04/01/98 |
| 7 | // Copyright: (c) 1998 Julian Smart |
| 8 | // (c) 2008-2009 Vadim Zeitlin |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // =========================================================================== |
| 13 | // declarations |
| 14 | // =========================================================================== |
| 15 | |
| 16 | // --------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // --------------------------------------------------------------------------- |
| 19 | |
| 20 | // For compilers that support precompilation, includes "wx.h". |
| 21 | #include "wx/wxprec.h" |
| 22 | |
| 23 | #ifdef __BORLANDC__ |
| 24 | #pragma hdrstop |
| 25 | #endif |
| 26 | |
| 27 | #if wxUSE_MDI && !defined(__WXUNIVERSAL__) |
| 28 | |
| 29 | #include "wx/mdi.h" |
| 30 | |
| 31 | #ifndef WX_PRECOMP |
| 32 | #include "wx/frame.h" |
| 33 | #include "wx/menu.h" |
| 34 | #include "wx/app.h" |
| 35 | #include "wx/utils.h" |
| 36 | #include "wx/dialog.h" |
| 37 | #include "wx/statusbr.h" |
| 38 | #include "wx/settings.h" |
| 39 | #include "wx/intl.h" |
| 40 | #include "wx/log.h" |
| 41 | #include "wx/toolbar.h" |
| 42 | #endif |
| 43 | |
| 44 | #include "wx/stockitem.h" |
| 45 | #include "wx/msw/private.h" |
| 46 | |
| 47 | #include <string.h> |
| 48 | |
| 49 | // --------------------------------------------------------------------------- |
| 50 | // global variables |
| 51 | // --------------------------------------------------------------------------- |
| 52 | |
| 53 | extern wxMenu *wxCurrentPopupMenu; |
| 54 | |
| 55 | extern void wxRemoveHandleAssociation(wxWindow *win); |
| 56 | |
| 57 | namespace |
| 58 | { |
| 59 | |
| 60 | // --------------------------------------------------------------------------- |
| 61 | // constants |
| 62 | // --------------------------------------------------------------------------- |
| 63 | |
| 64 | // First ID for the MDI child menu item in the "Window" menu. |
| 65 | const int wxFIRST_MDI_CHILD = 4100; |
| 66 | |
| 67 | // There can be no more than 9 children in the "Window" menu as beginning with |
| 68 | // the tenth one they're not shown and "More windows..." menu item is used |
| 69 | // instead. |
| 70 | const int wxLAST_MDI_CHILD = wxFIRST_MDI_CHILD + 8; |
| 71 | |
| 72 | // The ID of the "More windows..." menu item is the next one after the last |
| 73 | // child. |
| 74 | const int wxID_MDI_MORE_WINDOWS = wxLAST_MDI_CHILD + 1; |
| 75 | |
| 76 | // The MDI "Window" menu label |
| 77 | const char *WINDOW_MENU_LABEL = gettext_noop("&Window"); |
| 78 | |
| 79 | // --------------------------------------------------------------------------- |
| 80 | // private functions |
| 81 | // --------------------------------------------------------------------------- |
| 82 | |
| 83 | // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu |
| 84 | // of the parent of win (which is supposed to be the MDI client window) |
| 85 | void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow); |
| 86 | |
| 87 | // insert the window menu (subMenu) into menu just before "Help" submenu or at |
| 88 | // the very end if not found |
| 89 | void MDIInsertWindowMenu(wxWindow *win, WXHMENU hMenu, HMENU subMenu); |
| 90 | |
| 91 | // Remove the window menu |
| 92 | void MDIRemoveWindowMenu(wxWindow *win, WXHMENU hMenu); |
| 93 | |
| 94 | // unpack the parameters of WM_MDIACTIVATE message |
| 95 | void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam, |
| 96 | WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact); |
| 97 | |
| 98 | // return the HMENU of the MDI menu |
| 99 | // |
| 100 | // this function works correctly even when we don't have a window menu and just |
| 101 | // returns 0 then |
| 102 | inline HMENU GetMDIWindowMenu(wxMDIParentFrame *frame) |
| 103 | { |
| 104 | wxMenu *menu = frame->GetWindowMenu(); |
| 105 | return menu ? GetHmenuOf(menu) : 0; |
| 106 | } |
| 107 | |
| 108 | } // anonymous namespace |
| 109 | |
| 110 | // =========================================================================== |
| 111 | // implementation |
| 112 | // =========================================================================== |
| 113 | |
| 114 | // --------------------------------------------------------------------------- |
| 115 | // wxWin macros |
| 116 | // --------------------------------------------------------------------------- |
| 117 | |
| 118 | IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame, wxFrame) |
| 119 | IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame, wxFrame) |
| 120 | IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxWindow) |
| 121 | |
| 122 | BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame) |
| 123 | EVT_SIZE(wxMDIParentFrame::OnSize) |
| 124 | EVT_ICONIZE(wxMDIParentFrame::OnIconized) |
| 125 | EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged) |
| 126 | |
| 127 | #if wxUSE_MENUS |
| 128 | EVT_MENU_RANGE(wxFIRST_MDI_CHILD, wxLAST_MDI_CHILD, |
| 129 | wxMDIParentFrame::OnMDIChild) |
| 130 | EVT_MENU_RANGE(wxID_MDI_WINDOW_FIRST, wxID_MDI_WINDOW_LAST, |
| 131 | wxMDIParentFrame::OnMDICommand) |
| 132 | #endif // wxUSE_MENUS |
| 133 | END_EVENT_TABLE() |
| 134 | |
| 135 | BEGIN_EVENT_TABLE(wxMDIChildFrame, wxFrame) |
| 136 | EVT_IDLE(wxMDIChildFrame::OnIdle) |
| 137 | END_EVENT_TABLE() |
| 138 | |
| 139 | BEGIN_EVENT_TABLE(wxMDIClientWindow, wxWindow) |
| 140 | EVT_SCROLL(wxMDIClientWindow::OnScroll) |
| 141 | END_EVENT_TABLE() |
| 142 | |
| 143 | // =========================================================================== |
| 144 | // wxMDIParentFrame: the frame which contains the client window which manages |
| 145 | // the children |
| 146 | // =========================================================================== |
| 147 | |
| 148 | void wxMDIParentFrame::Init() |
| 149 | { |
| 150 | #if wxUSE_MENUS && wxUSE_ACCEL |
| 151 | // the default menu doesn't have any accelerators (even if we have it) |
| 152 | m_accelWindowMenu = NULL; |
| 153 | #endif // wxUSE_MENUS && wxUSE_ACCEL |
| 154 | } |
| 155 | |
| 156 | bool wxMDIParentFrame::Create(wxWindow *parent, |
| 157 | wxWindowID id, |
| 158 | const wxString& title, |
| 159 | const wxPoint& pos, |
| 160 | const wxSize& size, |
| 161 | long style, |
| 162 | const wxString& name) |
| 163 | { |
| 164 | // this style can be used to prevent a window from having the standard MDI |
| 165 | // "Window" menu |
| 166 | if ( !(style & wxFRAME_NO_WINDOW_MENU) ) |
| 167 | { |
| 168 | // normal case: we have the window menu, so construct it |
| 169 | m_windowMenu = new wxMenu; |
| 170 | |
| 171 | m_windowMenu->Append(wxID_MDI_WINDOW_CASCADE, _("&Cascade")); |
| 172 | m_windowMenu->Append(wxID_MDI_WINDOW_TILE_HORZ, _("Tile &Horizontally")); |
| 173 | m_windowMenu->Append(wxID_MDI_WINDOW_TILE_VERT, _("Tile &Vertically")); |
| 174 | m_windowMenu->AppendSeparator(); |
| 175 | m_windowMenu->Append(wxID_MDI_WINDOW_ARRANGE_ICONS, _("&Arrange Icons")); |
| 176 | m_windowMenu->Append(wxID_MDI_WINDOW_NEXT, _("&Next")); |
| 177 | m_windowMenu->Append(wxID_MDI_WINDOW_PREV, _("&Previous")); |
| 178 | } |
| 179 | |
| 180 | if (!parent) |
| 181 | wxTopLevelWindows.Append(this); |
| 182 | |
| 183 | SetName(name); |
| 184 | m_windowStyle = style; |
| 185 | |
| 186 | if ( parent ) |
| 187 | parent->AddChild(this); |
| 188 | |
| 189 | if ( id != wxID_ANY ) |
| 190 | m_windowId = id; |
| 191 | else |
| 192 | m_windowId = NewControlId(); |
| 193 | |
| 194 | WXDWORD exflags; |
| 195 | WXDWORD msflags = MSWGetCreateWindowFlags(&exflags); |
| 196 | msflags &= ~WS_VSCROLL; |
| 197 | msflags &= ~WS_HSCROLL; |
| 198 | |
| 199 | if ( !wxWindow::MSWCreate(wxApp::GetRegisteredClassName(wxT("wxMDIFrame")), |
| 200 | title.t_str(), |
| 201 | pos, size, |
| 202 | msflags, |
| 203 | exflags) ) |
| 204 | { |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE)); |
| 209 | |
| 210 | // unlike (almost?) all other windows, frames are created hidden |
| 211 | m_isShown = false; |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | wxMDIParentFrame::~wxMDIParentFrame() |
| 217 | { |
| 218 | // see comment in ~wxMDIChildFrame |
| 219 | #if wxUSE_TOOLBAR |
| 220 | m_frameToolBar = NULL; |
| 221 | #endif |
| 222 | #if wxUSE_STATUSBAR |
| 223 | m_frameStatusBar = NULL; |
| 224 | #endif // wxUSE_STATUSBAR |
| 225 | |
| 226 | #if wxUSE_MENUS && wxUSE_ACCEL |
| 227 | delete m_accelWindowMenu; |
| 228 | #endif // wxUSE_MENUS && wxUSE_ACCEL |
| 229 | |
| 230 | DestroyChildren(); |
| 231 | |
| 232 | // the MDI frame menubar is not automatically deleted by Windows unlike for |
| 233 | // the normal frames |
| 234 | if ( m_hMenu ) |
| 235 | ::DestroyMenu((HMENU)m_hMenu); |
| 236 | |
| 237 | if ( m_clientWindow ) |
| 238 | { |
| 239 | if ( m_clientWindow->MSWGetOldWndProc() ) |
| 240 | m_clientWindow->UnsubclassWin(); |
| 241 | |
| 242 | m_clientWindow->SetHWND(0); |
| 243 | delete m_clientWindow; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // ---------------------------------------------------------------------------- |
| 248 | // wxMDIParentFrame child management |
| 249 | // ---------------------------------------------------------------------------- |
| 250 | |
| 251 | wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const |
| 252 | { |
| 253 | HWND hWnd = (HWND)::SendMessage(GetWinHwnd(GetClientWindow()), |
| 254 | WM_MDIGETACTIVE, 0, 0L); |
| 255 | if ( !hWnd ) |
| 256 | return NULL; |
| 257 | |
| 258 | return static_cast<wxMDIChildFrame *>(wxFindWinFromHandle(hWnd)); |
| 259 | } |
| 260 | |
| 261 | int wxMDIParentFrame::GetChildFramesCount() const |
| 262 | { |
| 263 | int count = 0; |
| 264 | for ( wxWindowList::const_iterator i = GetChildren().begin(); |
| 265 | i != GetChildren().end(); |
| 266 | ++i ) |
| 267 | { |
| 268 | if ( wxDynamicCast(*i, wxMDIChildFrame) ) |
| 269 | count++; |
| 270 | } |
| 271 | |
| 272 | return count; |
| 273 | } |
| 274 | |
| 275 | #if wxUSE_MENUS |
| 276 | |
| 277 | void wxMDIParentFrame::AddMDIChild(wxMDIChildFrame * WXUNUSED(child)) |
| 278 | { |
| 279 | switch ( GetChildFramesCount() ) |
| 280 | { |
| 281 | case 1: |
| 282 | // first MDI child was just added, we need to insert the window |
| 283 | // menu now if we have it |
| 284 | AddWindowMenu(); |
| 285 | |
| 286 | // and disable the items which can't be used until we have more |
| 287 | // than one child |
| 288 | UpdateWindowMenu(false); |
| 289 | break; |
| 290 | |
| 291 | case 2: |
| 292 | // second MDI child was added, enable the menu items which were |
| 293 | // disabled because they didn't make sense for a single window |
| 294 | UpdateWindowMenu(true); |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void wxMDIParentFrame::RemoveMDIChild(wxMDIChildFrame * WXUNUSED(child)) |
| 300 | { |
| 301 | switch ( GetChildFramesCount() ) |
| 302 | { |
| 303 | case 1: |
| 304 | // last MDI child is being removed, remove the now unnecessary |
| 305 | // window menu too |
| 306 | RemoveWindowMenu(); |
| 307 | |
| 308 | // there is no need to call UpdateWindowMenu(true) here so this is |
| 309 | // not quite symmetric to AddMDIChild() above |
| 310 | break; |
| 311 | |
| 312 | case 2: |
| 313 | // only one MDI child is going to remain, disable the menu commands |
| 314 | // which don't make sense for a single child window |
| 315 | UpdateWindowMenu(false); |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // ---------------------------------------------------------------------------- |
| 321 | // wxMDIParentFrame window menu handling |
| 322 | // ---------------------------------------------------------------------------- |
| 323 | |
| 324 | void wxMDIParentFrame::AddWindowMenu() |
| 325 | { |
| 326 | if ( m_windowMenu ) |
| 327 | { |
| 328 | // For correct handling of the events from this menu we also must |
| 329 | // attach it to the menu bar. |
| 330 | m_windowMenu->Attach(GetMenuBar()); |
| 331 | |
| 332 | MDIInsertWindowMenu(GetClientWindow(), m_hMenu, GetMDIWindowMenu(this)); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void wxMDIParentFrame::RemoveWindowMenu() |
| 337 | { |
| 338 | if ( m_windowMenu ) |
| 339 | { |
| 340 | MDIRemoveWindowMenu(GetClientWindow(), m_hMenu); |
| 341 | |
| 342 | m_windowMenu->Detach(); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void wxMDIParentFrame::UpdateWindowMenu(bool enable) |
| 347 | { |
| 348 | if ( m_windowMenu ) |
| 349 | { |
| 350 | m_windowMenu->Enable(wxID_MDI_WINDOW_NEXT, enable); |
| 351 | m_windowMenu->Enable(wxID_MDI_WINDOW_PREV, enable); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | #if wxUSE_MENUS_NATIVE |
| 356 | |
| 357 | void wxMDIParentFrame::InternalSetMenuBar() |
| 358 | { |
| 359 | if ( GetActiveChild() ) |
| 360 | { |
| 361 | AddWindowMenu(); |
| 362 | } |
| 363 | else // we don't have any MDI children yet |
| 364 | { |
| 365 | // wait until we do to add the window menu but do set the main menu for |
| 366 | // now (this is done by AddWindowMenu() as a side effect) |
| 367 | MDISetMenu(GetClientWindow(), (HMENU)m_hMenu, NULL); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | #endif // wxUSE_MENUS_NATIVE |
| 372 | |
| 373 | void wxMDIParentFrame::SetWindowMenu(wxMenu* menu) |
| 374 | { |
| 375 | if ( menu != m_windowMenu ) |
| 376 | { |
| 377 | // notice that Remove/AddWindowMenu() are safe to call even when |
| 378 | // m_windowMenu is NULL |
| 379 | RemoveWindowMenu(); |
| 380 | |
| 381 | delete m_windowMenu; |
| 382 | |
| 383 | m_windowMenu = menu; |
| 384 | |
| 385 | AddWindowMenu(); |
| 386 | } |
| 387 | |
| 388 | #if wxUSE_ACCEL |
| 389 | wxDELETE(m_accelWindowMenu); |
| 390 | |
| 391 | if ( menu && menu->HasAccels() ) |
| 392 | m_accelWindowMenu = menu->CreateAccelTable(); |
| 393 | #endif // wxUSE_ACCEL |
| 394 | } |
| 395 | |
| 396 | // ---------------------------------------------------------------------------- |
| 397 | // wxMDIParentFrame other menu-related stuff |
| 398 | // ---------------------------------------------------------------------------- |
| 399 | |
| 400 | void wxMDIParentFrame::DoMenuUpdates(wxMenu* menu) |
| 401 | { |
| 402 | wxMDIChildFrame *child = GetActiveChild(); |
| 403 | if ( child ) |
| 404 | { |
| 405 | wxEvtHandler* source = child->GetEventHandler(); |
| 406 | wxMenuBar* bar = child->GetMenuBar(); |
| 407 | |
| 408 | if (menu) |
| 409 | { |
| 410 | menu->UpdateUI(source); |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | if ( bar != NULL ) |
| 415 | { |
| 416 | int nCount = bar->GetMenuCount(); |
| 417 | for (int n = 0; n < nCount; n++) |
| 418 | bar->GetMenu(n)->UpdateUI(source); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | wxFrameBase::DoMenuUpdates(menu); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | wxMenuItem *wxMDIParentFrame::FindItemInMenuBar(int menuId) const |
| 429 | { |
| 430 | // We must look in the child menu first: if it has an item with the same ID |
| 431 | // as in our own menu bar, the child item should be used to determine |
| 432 | // whether it's currently enabled. |
| 433 | wxMenuItem *item = GetActiveChild() |
| 434 | ? GetActiveChild()->FindItemInMenuBar(menuId) |
| 435 | : NULL; |
| 436 | if ( !item ) |
| 437 | item = wxFrame::FindItemInMenuBar(menuId); |
| 438 | |
| 439 | if ( !item && m_windowMenu ) |
| 440 | item = m_windowMenu->FindItem(menuId); |
| 441 | |
| 442 | return item; |
| 443 | } |
| 444 | |
| 445 | WXHMENU wxMDIParentFrame::MSWGetActiveMenu() const |
| 446 | { |
| 447 | wxMDIChildFrame * const child = GetActiveChild(); |
| 448 | if ( child ) |
| 449 | { |
| 450 | const WXHMENU hmenu = child->MSWGetActiveMenu(); |
| 451 | if ( hmenu ) |
| 452 | return hmenu; |
| 453 | } |
| 454 | |
| 455 | return wxFrame::MSWGetActiveMenu(); |
| 456 | } |
| 457 | |
| 458 | #endif // wxUSE_MENUS |
| 459 | |
| 460 | // ---------------------------------------------------------------------------- |
| 461 | // wxMDIParentFrame event handling |
| 462 | // ---------------------------------------------------------------------------- |
| 463 | |
| 464 | void wxMDIParentFrame::UpdateClientSize() |
| 465 | { |
| 466 | if ( GetClientWindow() ) |
| 467 | { |
| 468 | int width, height; |
| 469 | GetClientSize(&width, &height); |
| 470 | |
| 471 | GetClientWindow()->SetSize(0, 0, width, height); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | void wxMDIParentFrame::OnSize(wxSizeEvent& WXUNUSED(event)) |
| 476 | { |
| 477 | UpdateClientSize(); |
| 478 | |
| 479 | // do not call event.Skip() here, it somehow messes up MDI client window |
| 480 | } |
| 481 | |
| 482 | void wxMDIParentFrame::OnIconized(wxIconizeEvent& event) |
| 483 | { |
| 484 | event.Skip(); |
| 485 | |
| 486 | if ( !event.IsIconized() ) |
| 487 | UpdateClientSize(); |
| 488 | } |
| 489 | |
| 490 | // Responds to colour changes, and passes event on to children. |
| 491 | void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event) |
| 492 | { |
| 493 | if ( m_clientWindow ) |
| 494 | { |
| 495 | m_clientWindow->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE)); |
| 496 | m_clientWindow->Refresh(); |
| 497 | } |
| 498 | |
| 499 | event.Skip(); |
| 500 | } |
| 501 | |
| 502 | WXHICON wxMDIParentFrame::GetDefaultIcon() const |
| 503 | { |
| 504 | // we don't have any standard icons (any more) |
| 505 | return (WXHICON)0; |
| 506 | } |
| 507 | |
| 508 | // --------------------------------------------------------------------------- |
| 509 | // MDI operations |
| 510 | // --------------------------------------------------------------------------- |
| 511 | |
| 512 | void wxMDIParentFrame::Cascade() |
| 513 | { |
| 514 | ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE, 0, 0); |
| 515 | } |
| 516 | |
| 517 | void wxMDIParentFrame::Tile(wxOrientation orient) |
| 518 | { |
| 519 | wxASSERT_MSG( orient == wxHORIZONTAL || orient == wxVERTICAL, |
| 520 | wxT("invalid orientation value") ); |
| 521 | |
| 522 | ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE, |
| 523 | orient == wxHORIZONTAL ? MDITILE_HORIZONTAL |
| 524 | : MDITILE_VERTICAL, 0); |
| 525 | } |
| 526 | |
| 527 | void wxMDIParentFrame::ArrangeIcons() |
| 528 | { |
| 529 | ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE, 0, 0); |
| 530 | } |
| 531 | |
| 532 | void wxMDIParentFrame::ActivateNext() |
| 533 | { |
| 534 | ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 0); |
| 535 | } |
| 536 | |
| 537 | void wxMDIParentFrame::ActivatePrevious() |
| 538 | { |
| 539 | ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 1); |
| 540 | } |
| 541 | |
| 542 | // --------------------------------------------------------------------------- |
| 543 | // the MDI parent frame window proc |
| 544 | // --------------------------------------------------------------------------- |
| 545 | |
| 546 | WXLRESULT wxMDIParentFrame::MSWWindowProc(WXUINT message, |
| 547 | WXWPARAM wParam, |
| 548 | WXLPARAM lParam) |
| 549 | { |
| 550 | WXLRESULT rc = 0; |
| 551 | bool processed = false; |
| 552 | |
| 553 | switch ( message ) |
| 554 | { |
| 555 | case WM_ACTIVATE: |
| 556 | { |
| 557 | WXWORD state, minimized; |
| 558 | WXHWND hwnd; |
| 559 | UnpackActivate(wParam, lParam, &state, &minimized, &hwnd); |
| 560 | |
| 561 | processed = HandleActivate(state, minimized != 0, hwnd); |
| 562 | } |
| 563 | break; |
| 564 | |
| 565 | case WM_COMMAND: |
| 566 | // system messages such as SC_CLOSE are sent as WM_COMMANDs to the |
| 567 | // parent MDI frame and we must let the DefFrameProc() have them |
| 568 | // for these commands to work (without it, closing the maximized |
| 569 | // MDI children doesn't work, for example) |
| 570 | { |
| 571 | WXWORD id, cmd; |
| 572 | WXHWND hwnd; |
| 573 | UnpackCommand(wParam, lParam, &id, &hwnd, &cmd); |
| 574 | |
| 575 | if ( id == wxID_MDI_MORE_WINDOWS || |
| 576 | (cmd == 0 /* menu */ && |
| 577 | id >= SC_SIZE /* first system menu command */) ) |
| 578 | { |
| 579 | MSWDefWindowProc(message, wParam, lParam); |
| 580 | processed = true; |
| 581 | } |
| 582 | } |
| 583 | break; |
| 584 | |
| 585 | case WM_CREATE: |
| 586 | m_clientWindow = OnCreateClient(); |
| 587 | // Uses own style for client style |
| 588 | if ( !m_clientWindow->CreateClient(this, GetWindowStyleFlag()) ) |
| 589 | { |
| 590 | wxLogMessage(_("Failed to create MDI parent frame.")); |
| 591 | |
| 592 | rc = -1; |
| 593 | } |
| 594 | |
| 595 | processed = true; |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | if ( !processed ) |
| 600 | rc = wxFrame::MSWWindowProc(message, wParam, lParam); |
| 601 | |
| 602 | return rc; |
| 603 | } |
| 604 | |
| 605 | bool wxMDIParentFrame::HandleActivate(int state, bool minimized, WXHWND activate) |
| 606 | { |
| 607 | bool processed = false; |
| 608 | |
| 609 | if ( wxWindow::HandleActivate(state, minimized, activate) ) |
| 610 | { |
| 611 | // already processed |
| 612 | processed = true; |
| 613 | } |
| 614 | |
| 615 | // If this window is an MDI parent, we must also send an OnActivate message |
| 616 | // to the current child. |
| 617 | if ( GetActiveChild() && |
| 618 | ((state == WA_ACTIVE) || (state == WA_CLICKACTIVE)) ) |
| 619 | { |
| 620 | wxActivateEvent event(wxEVT_ACTIVATE, true, GetActiveChild()->GetId()); |
| 621 | event.SetEventObject( GetActiveChild() ); |
| 622 | if ( GetActiveChild()->HandleWindowEvent(event) ) |
| 623 | processed = true; |
| 624 | } |
| 625 | |
| 626 | return processed; |
| 627 | } |
| 628 | |
| 629 | #if wxUSE_MENUS |
| 630 | |
| 631 | void wxMDIParentFrame::OnMDIChild(wxCommandEvent& event) |
| 632 | { |
| 633 | wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); |
| 634 | while ( node ) |
| 635 | { |
| 636 | wxWindow *child = node->GetData(); |
| 637 | if ( child->GetHWND() ) |
| 638 | { |
| 639 | int childId = wxGetWindowId(child->GetHWND()); |
| 640 | if ( childId == event.GetId() ) |
| 641 | { |
| 642 | wxStaticCast(child, wxMDIChildFrame)->Activate(); |
| 643 | return; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | node = node->GetNext(); |
| 648 | } |
| 649 | |
| 650 | wxFAIL_MSG( "unknown MDI child selected?" ); |
| 651 | } |
| 652 | |
| 653 | void wxMDIParentFrame::OnMDICommand(wxCommandEvent& event) |
| 654 | { |
| 655 | WXWPARAM wParam = 0; |
| 656 | WXLPARAM lParam = 0; |
| 657 | int msg; |
| 658 | switch ( event.GetId() ) |
| 659 | { |
| 660 | case wxID_MDI_WINDOW_CASCADE: |
| 661 | msg = WM_MDICASCADE; |
| 662 | wParam = MDITILE_SKIPDISABLED; |
| 663 | break; |
| 664 | |
| 665 | case wxID_MDI_WINDOW_TILE_HORZ: |
| 666 | wParam |= MDITILE_HORIZONTAL; |
| 667 | // fall through |
| 668 | |
| 669 | case wxID_MDI_WINDOW_TILE_VERT: |
| 670 | if ( !wParam ) |
| 671 | wParam = MDITILE_VERTICAL; |
| 672 | msg = WM_MDITILE; |
| 673 | wParam |= MDITILE_SKIPDISABLED; |
| 674 | break; |
| 675 | |
| 676 | case wxID_MDI_WINDOW_ARRANGE_ICONS: |
| 677 | msg = WM_MDIICONARRANGE; |
| 678 | break; |
| 679 | |
| 680 | case wxID_MDI_WINDOW_NEXT: |
| 681 | msg = WM_MDINEXT; |
| 682 | lParam = 0; // next child |
| 683 | break; |
| 684 | |
| 685 | case wxID_MDI_WINDOW_PREV: |
| 686 | msg = WM_MDINEXT; |
| 687 | lParam = 1; // previous child |
| 688 | break; |
| 689 | |
| 690 | default: |
| 691 | wxFAIL_MSG( "unknown MDI command" ); |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | ::SendMessage(GetWinHwnd(GetClientWindow()), msg, wParam, lParam); |
| 696 | } |
| 697 | |
| 698 | #endif // wxUSE_MENUS |
| 699 | |
| 700 | WXLRESULT wxMDIParentFrame::MSWDefWindowProc(WXUINT message, |
| 701 | WXWPARAM wParam, |
| 702 | WXLPARAM lParam) |
| 703 | { |
| 704 | WXHWND clientWnd; |
| 705 | if ( GetClientWindow() ) |
| 706 | clientWnd = GetClientWindow()->GetHWND(); |
| 707 | else |
| 708 | clientWnd = 0; |
| 709 | |
| 710 | return DefFrameProc(GetHwnd(), (HWND)clientWnd, message, wParam, lParam); |
| 711 | } |
| 712 | |
| 713 | bool wxMDIParentFrame::MSWTranslateMessage(WXMSG* msg) |
| 714 | { |
| 715 | MSG *pMsg = (MSG *)msg; |
| 716 | |
| 717 | // first let the current child get it |
| 718 | wxMDIChildFrame * const child = GetActiveChild(); |
| 719 | if ( child && child->MSWTranslateMessage(msg) ) |
| 720 | { |
| 721 | return true; |
| 722 | } |
| 723 | |
| 724 | // then try out accelerator table (will also check the accelerators for the |
| 725 | // normal menu items) |
| 726 | if ( wxFrame::MSWTranslateMessage(msg) ) |
| 727 | { |
| 728 | return true; |
| 729 | } |
| 730 | |
| 731 | #if wxUSE_MENUS && wxUSE_ACCEL |
| 732 | // but it doesn't check for the (custom) accelerators of the window menu |
| 733 | // items as it's not part of the menu bar as it's handled by Windows itself |
| 734 | // so we need to do this explicitly |
| 735 | if ( m_accelWindowMenu && m_accelWindowMenu->Translate(this, msg) ) |
| 736 | return true; |
| 737 | #endif // wxUSE_MENUS && wxUSE_ACCEL |
| 738 | |
| 739 | // finally, check for MDI specific built-in accelerators |
| 740 | if ( pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN ) |
| 741 | { |
| 742 | if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg)) |
| 743 | return true; |
| 744 | } |
| 745 | |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | // =========================================================================== |
| 750 | // wxMDIChildFrame |
| 751 | // =========================================================================== |
| 752 | |
| 753 | void wxMDIChildFrame::Init() |
| 754 | { |
| 755 | m_needsResize = true; |
| 756 | m_needsInitialShow = true; |
| 757 | } |
| 758 | |
| 759 | bool wxMDIChildFrame::Create(wxMDIParentFrame *parent, |
| 760 | wxWindowID id, |
| 761 | const wxString& title, |
| 762 | const wxPoint& pos, |
| 763 | const wxSize& size, |
| 764 | long style, |
| 765 | const wxString& name) |
| 766 | { |
| 767 | m_mdiParent = parent; |
| 768 | |
| 769 | SetName(name); |
| 770 | |
| 771 | if ( id != wxID_ANY ) |
| 772 | m_windowId = id; |
| 773 | else |
| 774 | m_windowId = NewControlId(); |
| 775 | |
| 776 | if ( parent ) |
| 777 | { |
| 778 | parent->AddChild(this); |
| 779 | } |
| 780 | |
| 781 | int x = pos.x; |
| 782 | int y = pos.y; |
| 783 | int width = size.x; |
| 784 | int height = size.y; |
| 785 | |
| 786 | MDICREATESTRUCT mcs; |
| 787 | |
| 788 | wxString className = |
| 789 | wxApp::GetRegisteredClassName(wxT("wxMDIChildFrame"), COLOR_WINDOW); |
| 790 | if ( !(style & wxFULL_REPAINT_ON_RESIZE) ) |
| 791 | className += wxApp::GetNoRedrawClassSuffix(); |
| 792 | |
| 793 | mcs.szClass = className.t_str(); |
| 794 | mcs.szTitle = title.t_str(); |
| 795 | mcs.hOwner = wxGetInstance(); |
| 796 | if (x != wxDefaultCoord) |
| 797 | mcs.x = x; |
| 798 | else |
| 799 | mcs.x = CW_USEDEFAULT; |
| 800 | |
| 801 | if (y != wxDefaultCoord) |
| 802 | mcs.y = y; |
| 803 | else |
| 804 | mcs.y = CW_USEDEFAULT; |
| 805 | |
| 806 | if (width != wxDefaultCoord) |
| 807 | mcs.cx = width; |
| 808 | else |
| 809 | mcs.cx = CW_USEDEFAULT; |
| 810 | |
| 811 | if (height != wxDefaultCoord) |
| 812 | mcs.cy = height; |
| 813 | else |
| 814 | mcs.cy = CW_USEDEFAULT; |
| 815 | |
| 816 | DWORD msflags = WS_OVERLAPPED | WS_CLIPCHILDREN; |
| 817 | if (style & wxMINIMIZE_BOX) |
| 818 | msflags |= WS_MINIMIZEBOX; |
| 819 | if (style & wxMAXIMIZE_BOX) |
| 820 | msflags |= WS_MAXIMIZEBOX; |
| 821 | if (style & wxRESIZE_BORDER) |
| 822 | msflags |= WS_THICKFRAME; |
| 823 | if (style & wxSYSTEM_MENU) |
| 824 | msflags |= WS_SYSMENU; |
| 825 | if ((style & wxMINIMIZE) || (style & wxICONIZE)) |
| 826 | msflags |= WS_MINIMIZE; |
| 827 | if (style & wxMAXIMIZE) |
| 828 | msflags |= WS_MAXIMIZE; |
| 829 | if (style & wxCAPTION) |
| 830 | msflags |= WS_CAPTION; |
| 831 | |
| 832 | mcs.style = msflags; |
| 833 | |
| 834 | mcs.lParam = 0; |
| 835 | |
| 836 | wxWindowCreationHook hook(this); |
| 837 | |
| 838 | m_hWnd = (WXHWND)::SendMessage(GetWinHwnd(parent->GetClientWindow()), |
| 839 | WM_MDICREATE, 0, (LPARAM)&mcs); |
| 840 | |
| 841 | if ( !m_hWnd ) |
| 842 | { |
| 843 | wxLogLastError(wxT("WM_MDICREATE")); |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | SubclassWin(m_hWnd); |
| 848 | |
| 849 | parent->AddMDIChild(this); |
| 850 | |
| 851 | return true; |
| 852 | } |
| 853 | |
| 854 | wxMDIChildFrame::~wxMDIChildFrame() |
| 855 | { |
| 856 | // if we hadn't been created, there is nothing to destroy |
| 857 | if ( !m_hWnd ) |
| 858 | return; |
| 859 | |
| 860 | GetMDIParent()->RemoveMDIChild(this); |
| 861 | |
| 862 | // will be destroyed by DestroyChildren() but reset them before calling it |
| 863 | // to avoid using dangling pointers if a callback comes in the meanwhile |
| 864 | #if wxUSE_TOOLBAR |
| 865 | m_frameToolBar = NULL; |
| 866 | #endif |
| 867 | #if wxUSE_STATUSBAR |
| 868 | m_frameStatusBar = NULL; |
| 869 | #endif // wxUSE_STATUSBAR |
| 870 | |
| 871 | DestroyChildren(); |
| 872 | |
| 873 | MDIRemoveWindowMenu(NULL, m_hMenu); |
| 874 | |
| 875 | MSWDestroyWindow(); |
| 876 | } |
| 877 | |
| 878 | bool wxMDIChildFrame::Show(bool show) |
| 879 | { |
| 880 | m_needsInitialShow = false; |
| 881 | |
| 882 | if (!wxFrame::Show(show)) |
| 883 | return false; |
| 884 | |
| 885 | // KH: Without this call, new MDI children do not become active. |
| 886 | // This was added here after the same BringWindowToTop call was |
| 887 | // removed from wxTopLevelWindow::Show (November 2005) |
| 888 | if ( show ) |
| 889 | ::BringWindowToTop(GetHwnd()); |
| 890 | |
| 891 | // we need to refresh the MDI frame window menu to include (or exclude if |
| 892 | // we've been hidden) this frame |
| 893 | wxMDIParentFrame * const parent = GetMDIParent(); |
| 894 | MDISetMenu(parent->GetClientWindow(), NULL, NULL); |
| 895 | |
| 896 | return true; |
| 897 | } |
| 898 | |
| 899 | void |
| 900 | wxMDIChildFrame::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
| 901 | { |
| 902 | // we need to disable client area origin adjustments used for the child |
| 903 | // windows for the frame itself |
| 904 | wxMDIChildFrameBase::DoSetSize(x, y, width, height, sizeFlags); |
| 905 | } |
| 906 | |
| 907 | // Set the client size (i.e. leave the calculation of borders etc. |
| 908 | // to wxWidgets) |
| 909 | void wxMDIChildFrame::DoSetClientSize(int width, int height) |
| 910 | { |
| 911 | HWND hWnd = GetHwnd(); |
| 912 | |
| 913 | RECT rect; |
| 914 | ::GetClientRect(hWnd, &rect); |
| 915 | |
| 916 | RECT rect2; |
| 917 | GetWindowRect(hWnd, &rect2); |
| 918 | |
| 919 | // Find the difference between the entire window (title bar and all) |
| 920 | // and the client area; add this to the new client size to move the |
| 921 | // window |
| 922 | int actual_width = rect2.right - rect2.left - rect.right + width; |
| 923 | int actual_height = rect2.bottom - rect2.top - rect.bottom + height; |
| 924 | |
| 925 | #if wxUSE_STATUSBAR |
| 926 | if (GetStatusBar() && GetStatusBar()->IsShown()) |
| 927 | { |
| 928 | int sx, sy; |
| 929 | GetStatusBar()->GetSize(&sx, &sy); |
| 930 | actual_height += sy; |
| 931 | } |
| 932 | #endif // wxUSE_STATUSBAR |
| 933 | |
| 934 | POINT point; |
| 935 | point.x = rect2.left; |
| 936 | point.y = rect2.top; |
| 937 | |
| 938 | // If there's an MDI parent, must subtract the parent's top left corner |
| 939 | // since MoveWindow moves relative to the parent |
| 940 | wxMDIParentFrame * const mdiParent = GetMDIParent(); |
| 941 | ::ScreenToClient(GetHwndOf(mdiParent->GetClientWindow()), &point); |
| 942 | |
| 943 | MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)true); |
| 944 | |
| 945 | wxSize size(width, height); |
| 946 | wxSizeEvent event(size, m_windowId); |
| 947 | event.SetEventObject( this ); |
| 948 | HandleWindowEvent(event); |
| 949 | } |
| 950 | |
| 951 | // Unlike other wxTopLevelWindowBase, the mdi child's "GetPosition" is not the |
| 952 | // same as its GetScreenPosition |
| 953 | void wxMDIChildFrame::DoGetScreenPosition(int *x, int *y) const |
| 954 | { |
| 955 | HWND hWnd = GetHwnd(); |
| 956 | |
| 957 | RECT rect; |
| 958 | ::GetWindowRect(hWnd, &rect); |
| 959 | if (x) |
| 960 | *x = rect.left; |
| 961 | if (y) |
| 962 | *y = rect.top; |
| 963 | } |
| 964 | |
| 965 | |
| 966 | void wxMDIChildFrame::DoGetPosition(int *x, int *y) const |
| 967 | { |
| 968 | RECT rect; |
| 969 | GetWindowRect(GetHwnd(), &rect); |
| 970 | POINT point; |
| 971 | point.x = rect.left; |
| 972 | point.y = rect.top; |
| 973 | |
| 974 | // Since we now have the absolute screen coords, |
| 975 | // if there's a parent we must subtract its top left corner |
| 976 | wxMDIParentFrame * const mdiParent = GetMDIParent(); |
| 977 | ::ScreenToClient(GetHwndOf(mdiParent->GetClientWindow()), &point); |
| 978 | |
| 979 | if (x) |
| 980 | *x = point.x; |
| 981 | if (y) |
| 982 | *y = point.y; |
| 983 | } |
| 984 | |
| 985 | void wxMDIChildFrame::InternalSetMenuBar() |
| 986 | { |
| 987 | wxMDIParentFrame * const parent = GetMDIParent(); |
| 988 | |
| 989 | MDIInsertWindowMenu(parent->GetClientWindow(), |
| 990 | m_hMenu, GetMDIWindowMenu(parent)); |
| 991 | } |
| 992 | |
| 993 | void wxMDIChildFrame::DetachMenuBar() |
| 994 | { |
| 995 | MDIRemoveWindowMenu(NULL, m_hMenu); |
| 996 | wxFrame::DetachMenuBar(); |
| 997 | } |
| 998 | |
| 999 | WXHICON wxMDIChildFrame::GetDefaultIcon() const |
| 1000 | { |
| 1001 | // we don't have any standard icons (any more) |
| 1002 | return (WXHICON)0; |
| 1003 | } |
| 1004 | |
| 1005 | // --------------------------------------------------------------------------- |
| 1006 | // MDI operations |
| 1007 | // --------------------------------------------------------------------------- |
| 1008 | |
| 1009 | void wxMDIChildFrame::Maximize(bool maximize) |
| 1010 | { |
| 1011 | wxMDIParentFrame * const parent = GetMDIParent(); |
| 1012 | if ( parent && parent->GetClientWindow() ) |
| 1013 | { |
| 1014 | ::SendMessage(GetWinHwnd(parent->GetClientWindow()), |
| 1015 | maximize ? WM_MDIMAXIMIZE : WM_MDIRESTORE, |
| 1016 | (WPARAM)GetHwnd(), 0); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | void wxMDIChildFrame::Restore() |
| 1021 | { |
| 1022 | wxMDIParentFrame * const parent = GetMDIParent(); |
| 1023 | if ( parent && parent->GetClientWindow() ) |
| 1024 | { |
| 1025 | ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIRESTORE, |
| 1026 | (WPARAM) GetHwnd(), 0); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | void wxMDIChildFrame::Activate() |
| 1031 | { |
| 1032 | wxMDIParentFrame * const parent = GetMDIParent(); |
| 1033 | if ( parent && parent->GetClientWindow() ) |
| 1034 | { |
| 1035 | // Activating an iconized MDI frame doesn't do anything, so restore it |
| 1036 | // first to really present it to the user. |
| 1037 | if ( IsIconized() ) |
| 1038 | Restore(); |
| 1039 | |
| 1040 | ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIACTIVATE, |
| 1041 | (WPARAM) GetHwnd(), 0); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | // --------------------------------------------------------------------------- |
| 1046 | // MDI window proc and message handlers |
| 1047 | // --------------------------------------------------------------------------- |
| 1048 | |
| 1049 | WXLRESULT wxMDIChildFrame::MSWWindowProc(WXUINT message, |
| 1050 | WXWPARAM wParam, |
| 1051 | WXLPARAM lParam) |
| 1052 | { |
| 1053 | WXLRESULT rc = 0; |
| 1054 | bool processed = false; |
| 1055 | |
| 1056 | switch ( message ) |
| 1057 | { |
| 1058 | case WM_GETMINMAXINFO: |
| 1059 | processed = HandleGetMinMaxInfo((MINMAXINFO *)lParam); |
| 1060 | break; |
| 1061 | |
| 1062 | case WM_MDIACTIVATE: |
| 1063 | { |
| 1064 | WXWORD act; |
| 1065 | WXHWND hwndAct, hwndDeact; |
| 1066 | UnpackMDIActivate(wParam, lParam, &act, &hwndAct, &hwndDeact); |
| 1067 | |
| 1068 | processed = HandleMDIActivate(act, hwndAct, hwndDeact); |
| 1069 | } |
| 1070 | // fall through |
| 1071 | |
| 1072 | case WM_MOVE: |
| 1073 | // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client |
| 1074 | // scrollbars if necessary |
| 1075 | |
| 1076 | // fall through |
| 1077 | |
| 1078 | case WM_SIZE: |
| 1079 | // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird |
| 1080 | // things happen |
| 1081 | MSWDefWindowProc(message, wParam, lParam); |
| 1082 | break; |
| 1083 | |
| 1084 | case WM_WINDOWPOSCHANGING: |
| 1085 | processed = HandleWindowPosChanging((LPWINDOWPOS)lParam); |
| 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | if ( !processed ) |
| 1090 | rc = wxFrame::MSWWindowProc(message, wParam, lParam); |
| 1091 | |
| 1092 | return rc; |
| 1093 | } |
| 1094 | |
| 1095 | bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate), |
| 1096 | WXHWND hwndAct, |
| 1097 | WXHWND hwndDeact) |
| 1098 | { |
| 1099 | wxMDIParentFrame * const parent = GetMDIParent(); |
| 1100 | |
| 1101 | WXHMENU hMenuToSet = 0; |
| 1102 | |
| 1103 | bool activated; |
| 1104 | |
| 1105 | if ( m_hWnd == hwndAct ) |
| 1106 | { |
| 1107 | activated = true; |
| 1108 | parent->SetActiveChild(this); |
| 1109 | |
| 1110 | WXHMENU hMenuChild = m_hMenu; |
| 1111 | if ( hMenuChild ) |
| 1112 | hMenuToSet = hMenuChild; |
| 1113 | } |
| 1114 | else if ( m_hWnd == hwndDeact ) |
| 1115 | { |
| 1116 | wxASSERT_MSG( parent->GetActiveChild() == this, |
| 1117 | wxT("can't deactivate MDI child which wasn't active!") ); |
| 1118 | |
| 1119 | activated = false; |
| 1120 | parent->SetActiveChild(NULL); |
| 1121 | |
| 1122 | WXHMENU hMenuParent = parent->m_hMenu; |
| 1123 | |
| 1124 | // activate the parent menu only when there is no other child |
| 1125 | // that has been activated |
| 1126 | if ( hMenuParent && !hwndAct ) |
| 1127 | hMenuToSet = hMenuParent; |
| 1128 | } |
| 1129 | else |
| 1130 | { |
| 1131 | // we have nothing to do with it |
| 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | if ( hMenuToSet ) |
| 1136 | { |
| 1137 | MDISetMenu(parent->GetClientWindow(), |
| 1138 | (HMENU)hMenuToSet, GetMDIWindowMenu(parent)); |
| 1139 | } |
| 1140 | |
| 1141 | wxActivateEvent event(wxEVT_ACTIVATE, activated, m_windowId); |
| 1142 | event.SetEventObject( this ); |
| 1143 | |
| 1144 | ResetWindowStyle(NULL); |
| 1145 | |
| 1146 | return HandleWindowEvent(event); |
| 1147 | } |
| 1148 | |
| 1149 | bool wxMDIChildFrame::HandleWindowPosChanging(void *pos) |
| 1150 | { |
| 1151 | WINDOWPOS *lpPos = (WINDOWPOS *)pos; |
| 1152 | |
| 1153 | if (!(lpPos->flags & SWP_NOSIZE)) |
| 1154 | { |
| 1155 | RECT rectClient; |
| 1156 | DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE); |
| 1157 | DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE); |
| 1158 | if (ResetWindowStyle((void *) & rectClient) && (dwStyle & WS_MAXIMIZE)) |
| 1159 | { |
| 1160 | ::AdjustWindowRectEx(&rectClient, dwStyle, false, dwExStyle); |
| 1161 | lpPos->x = rectClient.left; |
| 1162 | lpPos->y = rectClient.top; |
| 1163 | lpPos->cx = rectClient.right - rectClient.left; |
| 1164 | lpPos->cy = rectClient.bottom - rectClient.top; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | return false; |
| 1169 | } |
| 1170 | |
| 1171 | bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo) |
| 1172 | { |
| 1173 | MINMAXINFO *info = (MINMAXINFO *)mmInfo; |
| 1174 | |
| 1175 | // let the default window proc calculate the size of MDI children |
| 1176 | // frames because it is based on the size of the MDI client window, |
| 1177 | // not on the values specified in wxWindow m_max variables |
| 1178 | bool processed = MSWDefWindowProc(WM_GETMINMAXINFO, 0, (LPARAM)mmInfo) != 0; |
| 1179 | |
| 1180 | int minWidth = GetMinWidth(), |
| 1181 | minHeight = GetMinHeight(); |
| 1182 | |
| 1183 | // but allow GetSizeHints() to set the min size |
| 1184 | if ( minWidth != wxDefaultCoord ) |
| 1185 | { |
| 1186 | info->ptMinTrackSize.x = minWidth; |
| 1187 | |
| 1188 | processed = true; |
| 1189 | } |
| 1190 | |
| 1191 | if ( minHeight != wxDefaultCoord ) |
| 1192 | { |
| 1193 | info->ptMinTrackSize.y = minHeight; |
| 1194 | |
| 1195 | processed = true; |
| 1196 | } |
| 1197 | |
| 1198 | return processed; |
| 1199 | } |
| 1200 | |
| 1201 | // --------------------------------------------------------------------------- |
| 1202 | // MDI specific message translation/preprocessing |
| 1203 | // --------------------------------------------------------------------------- |
| 1204 | |
| 1205 | WXLRESULT wxMDIChildFrame::MSWDefWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
| 1206 | { |
| 1207 | return DefMDIChildProc(GetHwnd(), |
| 1208 | (UINT)message, (WPARAM)wParam, (LPARAM)lParam); |
| 1209 | } |
| 1210 | |
| 1211 | bool wxMDIChildFrame::MSWTranslateMessage(WXMSG* msg) |
| 1212 | { |
| 1213 | // we must pass the parent frame to ::TranslateAccelerator(), otherwise it |
| 1214 | // doesn't do its job correctly for MDI child menus |
| 1215 | return MSWDoTranslateMessage(GetMDIParent(), msg); |
| 1216 | } |
| 1217 | |
| 1218 | // --------------------------------------------------------------------------- |
| 1219 | // misc |
| 1220 | // --------------------------------------------------------------------------- |
| 1221 | |
| 1222 | void wxMDIChildFrame::MSWDestroyWindow() |
| 1223 | { |
| 1224 | wxMDIParentFrame * const parent = GetMDIParent(); |
| 1225 | |
| 1226 | // Must make sure this handle is invalidated (set to NULL) since all sorts |
| 1227 | // of things could happen after the child client is destroyed, but before |
| 1228 | // the wxFrame is destroyed. |
| 1229 | |
| 1230 | HWND oldHandle = (HWND)GetHWND(); |
| 1231 | SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIDESTROY, |
| 1232 | (WPARAM)oldHandle, 0); |
| 1233 | |
| 1234 | if (parent->GetActiveChild() == NULL) |
| 1235 | ResetWindowStyle(NULL); |
| 1236 | |
| 1237 | if (m_hMenu) |
| 1238 | { |
| 1239 | ::DestroyMenu((HMENU) m_hMenu); |
| 1240 | m_hMenu = 0; |
| 1241 | } |
| 1242 | wxRemoveHandleAssociation(this); |
| 1243 | m_hWnd = 0; |
| 1244 | } |
| 1245 | |
| 1246 | // Change the client window's extended style so we don't get a client edge |
| 1247 | // style when a child is maximised (a double border looks silly.) |
| 1248 | bool wxMDIChildFrame::ResetWindowStyle(void *vrect) |
| 1249 | { |
| 1250 | RECT *rect = (RECT *)vrect; |
| 1251 | wxMDIParentFrame * const pFrameWnd = GetMDIParent(); |
| 1252 | wxMDIChildFrame* pChild = pFrameWnd->GetActiveChild(); |
| 1253 | |
| 1254 | if (!pChild || (pChild == this)) |
| 1255 | { |
| 1256 | HWND hwndClient = GetWinHwnd(pFrameWnd->GetClientWindow()); |
| 1257 | DWORD dwStyle = ::GetWindowLong(hwndClient, GWL_EXSTYLE); |
| 1258 | |
| 1259 | // we want to test whether there is a maximized child, so just set |
| 1260 | // dwThisStyle to 0 if there is no child at all |
| 1261 | DWORD dwThisStyle = pChild |
| 1262 | ? ::GetWindowLong(GetWinHwnd(pChild), GWL_STYLE) : 0; |
| 1263 | DWORD dwNewStyle = dwStyle; |
| 1264 | if ( dwThisStyle & WS_MAXIMIZE ) |
| 1265 | dwNewStyle &= ~(WS_EX_CLIENTEDGE); |
| 1266 | else |
| 1267 | dwNewStyle |= WS_EX_CLIENTEDGE; |
| 1268 | |
| 1269 | if (dwStyle != dwNewStyle) |
| 1270 | { |
| 1271 | // force update of everything |
| 1272 | ::RedrawWindow(hwndClient, NULL, NULL, |
| 1273 | RDW_INVALIDATE | RDW_ALLCHILDREN); |
| 1274 | ::SetWindowLong(hwndClient, GWL_EXSTYLE, dwNewStyle); |
| 1275 | ::SetWindowPos(hwndClient, NULL, 0, 0, 0, 0, |
| 1276 | SWP_FRAMECHANGED | SWP_NOACTIVATE | |
| 1277 | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | |
| 1278 | SWP_NOCOPYBITS); |
| 1279 | if (rect) |
| 1280 | ::GetClientRect(hwndClient, rect); |
| 1281 | |
| 1282 | return true; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | return false; |
| 1287 | } |
| 1288 | |
| 1289 | // =========================================================================== |
| 1290 | // wxMDIClientWindow: the window of predefined (by Windows) class which |
| 1291 | // contains the child frames |
| 1292 | // =========================================================================== |
| 1293 | |
| 1294 | bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style) |
| 1295 | { |
| 1296 | m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE); |
| 1297 | |
| 1298 | CLIENTCREATESTRUCT ccs; |
| 1299 | m_windowStyle = style; |
| 1300 | m_parent = parent; |
| 1301 | |
| 1302 | ccs.hWindowMenu = GetMDIWindowMenu(parent); |
| 1303 | ccs.idFirstChild = wxFIRST_MDI_CHILD; |
| 1304 | |
| 1305 | DWORD msStyle = MDIS_ALLCHILDSTYLES | WS_VISIBLE | WS_CHILD | |
| 1306 | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; |
| 1307 | |
| 1308 | if ( style & wxHSCROLL ) |
| 1309 | msStyle |= WS_HSCROLL; |
| 1310 | if ( style & wxVSCROLL ) |
| 1311 | msStyle |= WS_VSCROLL; |
| 1312 | |
| 1313 | DWORD exStyle = WS_EX_CLIENTEDGE; |
| 1314 | |
| 1315 | wxWindowCreationHook hook(this); |
| 1316 | m_hWnd = (WXHWND)::CreateWindowEx |
| 1317 | ( |
| 1318 | exStyle, |
| 1319 | wxT("MDICLIENT"), |
| 1320 | NULL, |
| 1321 | msStyle, |
| 1322 | 0, 0, 0, 0, |
| 1323 | GetWinHwnd(parent), |
| 1324 | NULL, |
| 1325 | wxGetInstance(), |
| 1326 | (LPSTR)(LPCLIENTCREATESTRUCT)&ccs); |
| 1327 | if ( !m_hWnd ) |
| 1328 | { |
| 1329 | wxLogLastError(wxT("CreateWindowEx(MDI client)")); |
| 1330 | |
| 1331 | return false; |
| 1332 | } |
| 1333 | |
| 1334 | SubclassWin(m_hWnd); |
| 1335 | |
| 1336 | return true; |
| 1337 | } |
| 1338 | |
| 1339 | // Explicitly call default scroll behaviour |
| 1340 | void wxMDIClientWindow::OnScroll(wxScrollEvent& event) |
| 1341 | { |
| 1342 | // Note: for client windows, the scroll position is not set in |
| 1343 | // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what |
| 1344 | // scroll position we're at. |
| 1345 | // This makes it hard to paint patterns or bitmaps in the background, |
| 1346 | // and have the client area scrollable as well. |
| 1347 | |
| 1348 | if ( event.GetOrientation() == wxHORIZONTAL ) |
| 1349 | m_scrollX = event.GetPosition(); // Always returns zero! |
| 1350 | else |
| 1351 | m_scrollY = event.GetPosition(); // Always returns zero! |
| 1352 | |
| 1353 | event.Skip(); |
| 1354 | } |
| 1355 | |
| 1356 | void wxMDIClientWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
| 1357 | { |
| 1358 | // Try to fix a problem whereby if you show an MDI child frame, then reposition the |
| 1359 | // client area, you can end up with a non-refreshed portion in the client window |
| 1360 | // (see OGL studio sample). So check if the position is changed and if so, |
| 1361 | // redraw the MDI child frames. |
| 1362 | |
| 1363 | const wxPoint oldPos = GetPosition(); |
| 1364 | |
| 1365 | wxWindow::DoSetSize(x, y, width, height, sizeFlags | wxSIZE_FORCE); |
| 1366 | |
| 1367 | const wxPoint newPos = GetPosition(); |
| 1368 | |
| 1369 | if ((newPos.x != oldPos.x) || (newPos.y != oldPos.y)) |
| 1370 | { |
| 1371 | if (GetParent()) |
| 1372 | { |
| 1373 | wxWindowList::compatibility_iterator node = GetParent()->GetChildren().GetFirst(); |
| 1374 | while (node) |
| 1375 | { |
| 1376 | wxWindow *child = node->GetData(); |
| 1377 | if (wxDynamicCast(child, wxMDIChildFrame)) |
| 1378 | { |
| 1379 | ::RedrawWindow(GetHwndOf(child), |
| 1380 | NULL, |
| 1381 | NULL, |
| 1382 | RDW_FRAME | |
| 1383 | RDW_ALLCHILDREN | |
| 1384 | RDW_INVALIDATE); |
| 1385 | } |
| 1386 | node = node->GetNext(); |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | void wxMDIChildFrame::OnIdle(wxIdleEvent& event) |
| 1393 | { |
| 1394 | // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted |
| 1395 | // in flicker e.g. when the frame contained controls with non-trivial |
| 1396 | // layout. Since 2.5.3, the frame is created hidden as all other top level |
| 1397 | // windows. In order to maintain backward compatibility, the frame is shown |
| 1398 | // in OnIdle, unless Show(false) was called by the programmer before. |
| 1399 | if ( m_needsInitialShow ) |
| 1400 | { |
| 1401 | Show(true); |
| 1402 | } |
| 1403 | |
| 1404 | // MDI child frames get their WM_SIZE when they're constructed but at this |
| 1405 | // moment they don't have any children yet so all child windows will be |
| 1406 | // positioned incorrectly when they are added later - to fix this, we |
| 1407 | // generate an artificial size event here |
| 1408 | if ( m_needsResize ) |
| 1409 | { |
| 1410 | m_needsResize = false; // avoid any possibility of recursion |
| 1411 | |
| 1412 | SendSizeEvent(); |
| 1413 | } |
| 1414 | |
| 1415 | event.Skip(); |
| 1416 | } |
| 1417 | |
| 1418 | // --------------------------------------------------------------------------- |
| 1419 | // private helper functions |
| 1420 | // --------------------------------------------------------------------------- |
| 1421 | |
| 1422 | namespace |
| 1423 | { |
| 1424 | |
| 1425 | void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow) |
| 1426 | { |
| 1427 | if ( hmenuFrame || hmenuWindow ) |
| 1428 | { |
| 1429 | if ( !::SendMessage(GetWinHwnd(win), |
| 1430 | WM_MDISETMENU, |
| 1431 | (WPARAM)hmenuFrame, |
| 1432 | (LPARAM)hmenuWindow) ) |
| 1433 | { |
| 1434 | DWORD err = ::GetLastError(); |
| 1435 | if ( err ) |
| 1436 | { |
| 1437 | wxLogApiError(wxT("SendMessage(WM_MDISETMENU)"), err); |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | // update menu bar of the parent window |
| 1443 | wxWindow *parent = win->GetParent(); |
| 1444 | wxCHECK_RET( parent, wxT("MDI client without parent frame? weird...") ); |
| 1445 | |
| 1446 | ::SendMessage(GetWinHwnd(win), WM_MDIREFRESHMENU, 0, 0L); |
| 1447 | |
| 1448 | ::DrawMenuBar(GetWinHwnd(parent)); |
| 1449 | } |
| 1450 | |
| 1451 | void MDIInsertWindowMenu(wxWindow *win, WXHMENU hMenu, HMENU menuWin) |
| 1452 | { |
| 1453 | HMENU hmenu = (HMENU)hMenu; |
| 1454 | |
| 1455 | if ( menuWin ) |
| 1456 | { |
| 1457 | // Try to insert Window menu in front of Help, otherwise append it. |
| 1458 | int N = GetMenuItemCount(hmenu); |
| 1459 | bool inserted = false; |
| 1460 | for ( int i = 0; i < N; i++ ) |
| 1461 | { |
| 1462 | wxChar buf[256]; |
| 1463 | if ( !::GetMenuString(hmenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION) ) |
| 1464 | { |
| 1465 | wxLogLastError(wxT("GetMenuString")); |
| 1466 | |
| 1467 | continue; |
| 1468 | } |
| 1469 | |
| 1470 | const wxString label = wxStripMenuCodes(buf); |
| 1471 | if ( label == wxGetStockLabel(wxID_HELP, wxSTOCK_NOFLAGS) ) |
| 1472 | { |
| 1473 | inserted = true; |
| 1474 | ::InsertMenu(hmenu, i, MF_BYPOSITION | MF_POPUP | MF_STRING, |
| 1475 | (UINT_PTR)menuWin, |
| 1476 | wxString(wxGetTranslation(WINDOW_MENU_LABEL)).t_str()); |
| 1477 | break; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | if ( !inserted ) |
| 1482 | { |
| 1483 | ::AppendMenu(hmenu, MF_POPUP, |
| 1484 | (UINT_PTR)menuWin, |
| 1485 | wxString(wxGetTranslation(WINDOW_MENU_LABEL)).t_str()); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | MDISetMenu(win, hmenu, menuWin); |
| 1490 | } |
| 1491 | |
| 1492 | void MDIRemoveWindowMenu(wxWindow *win, WXHMENU hMenu) |
| 1493 | { |
| 1494 | HMENU hmenu = (HMENU)hMenu; |
| 1495 | |
| 1496 | if ( hmenu ) |
| 1497 | { |
| 1498 | wxChar buf[1024]; |
| 1499 | |
| 1500 | int N = ::GetMenuItemCount(hmenu); |
| 1501 | for ( int i = 0; i < N; i++ ) |
| 1502 | { |
| 1503 | if ( !::GetMenuString(hmenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION) ) |
| 1504 | { |
| 1505 | // Ignore successful read of menu string with length 0 which |
| 1506 | // occurs, for example, for a maximized MDI child system menu |
| 1507 | if ( ::GetLastError() != 0 ) |
| 1508 | { |
| 1509 | wxLogLastError(wxT("GetMenuString")); |
| 1510 | } |
| 1511 | |
| 1512 | continue; |
| 1513 | } |
| 1514 | |
| 1515 | if ( wxStrcmp(buf, wxGetTranslation(WINDOW_MENU_LABEL)) == 0 ) |
| 1516 | { |
| 1517 | if ( !::RemoveMenu(hmenu, i, MF_BYPOSITION) ) |
| 1518 | { |
| 1519 | wxLogLastError(wxT("RemoveMenu")); |
| 1520 | } |
| 1521 | |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | if ( win ) |
| 1528 | { |
| 1529 | // we don't change the windows menu, but we update the main one |
| 1530 | MDISetMenu(win, hmenu, NULL); |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam, |
| 1535 | WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact) |
| 1536 | { |
| 1537 | *activate = true; |
| 1538 | *hwndAct = (WXHWND)lParam; |
| 1539 | *hwndDeact = (WXHWND)wParam; |
| 1540 | } |
| 1541 | |
| 1542 | } // anonymous namespace |
| 1543 | |
| 1544 | #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__) |