Listing 5: Member functions for tmdi_manager and tmdi_child_window classes


LRESULT CALLBACK tmdi_window::default_window_proc
    (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) {
    return (LRESULT CALLBACK) DefMDIChildProc
    (hwnd,message,wParam,lParam);}

tmdi_manager::tmdi_manager (HINSTANCE hInstance,
     tmdi_frame_window *parent,LRESULT CALLBACK
     window_proc,int window_extra,LPCSTR menu_name,
     LPCSTR title_name,LPCSTR class_name):
    tmdi_window (hInstance,window_proc,
       window_extra,menu_name,title_name,class_name){
    parent_frame = parent;
    window_list = new object_list
       (MAX_NO_OF_ACTIVE_WINDOWS); }

int tmdi_manager::respond_wm_create (HWND hwnd) {
    child_window_struct *child;
    hwndClient = GetParent (hwindow) ;
    hwndFrame  = GetParent (hwndClient) ;
    child = new child_window_struct;
    child->hwnd = hwnd;
    child->window = new_child_window(hwnd,the_title);
    add_child_window (hwnd,child);
    return 1;
    }

tmdi_child_window *tmdi_manager::new_child_window
    (HWND hwnd,LPCSTR title_name) {
    // Override with specific child window classes.
    return new tmdi_child_window(hwnd,this,the_title);}

int tmdi_manager::add_child_window (HWND hwnd,
    child_window_struct *child) {
    if (!(window_list->add_item (child))) {
       MessageBox (hwndClient,"Too many active windows!",
     "MDI Window Manager",MB_ICONEXCLAMATION |
        MB_OKCANCEL);
       SendMessage (GetParent (hwnd), WM_MDIDESTROY,
       (WPARAM) hwnd,0L);
       delete child->window;
       delete child;
       return 1; }
    if (!(child->window->respond_wm_create(hwnd)))
       SendMessage (GetParent (hwnd), WM_MDIDESTROY,
       (WPARAM) hwnd, 0L);
       // respond_wm_destroy takes care of deleting child in this case
       return 1; }

int tmdi_manager::respond_wm_destroy (HWND hwnd) {
    child_window_struct *child;
    if (!(child = get_child_window (hwnd,
       &active_index))) { // user cancelled open window
       return 0; }
    (child->window)->respond_wm_destroy(hwnd);
    delete child->window;
    delete child;
    window_list->delete_item(active_index);
    return 0; }    // allow default Windows processing

HWND tmdi_manager::get_active_hwnd (void) {
    HWND active_hwnd;
    hwndClient = GetParent (hwindow);
    active_hwnd = (HWND) LOWORD (SendMessage(hwndClient,
      WM_MDIGETACTIVE,0,0L));
    if (IsWindow (active_hwnd))
       return active_hwnd;
    return 0; }

child_window_struct *tmdi_manager::get_child_window
    (HWND hwnd, int *index) {
    *index = 0;
    for (int i=1;i<=window_list->get_count();i++)
     if (((child_window_struct *)
        (window_list->at(i)))->hwnd == hwnd) {
     *index = i;
     return (child_window_struct *)window_list->at(i);
     }
    return NULL; }

LRESULT CALLBACK tmdi_manager::handle_message (HWND hwnd,
    UINT message,WPARAM wParam,LPARAM lParam) {
    child_window_struct *child;
    hwindow = hwnd; // Handle for currently active window
   switch (message) {
     case WM_CREATE:
       return (LRESULT CALLBACK) respond_wm_create(hwnd);
     case WM_DESTROY:
       return (LRESULT CALLBACK) respond_wm_destroy(hwnd);
    // Isolate messages to be handled by child window:
     case WM_COMMAND:
     case WM_CLOSE:
     case WM_PAINT:
     case WM_LBUTTONDOWN:
     case WM_MOUSEMOVE:
     case WM_LBUTTONUP:
     case WM_RBUTTONDOWN:
     case WM_MDIACTIVATE:
     case WM_QUERYENDSESSION: {
       if (!(child = get_child_window
        (hwnd,&active_index))){
      // This shouldn't happen in de-bugged system:
      MessageBox (hwndClient,
         "No window match for hwnd!",
         "MDI Manager Handle Message",
         MB_ICONEXCLAMATION | MB_OKCANCEL);
      return 0; }
       return (child->window)->handle_message(hwnd,
      message,wParam,lParam); }
       }  // end switch
      return tmdi_window::handle_message(hwnd,message,
      wParam,lParam); }

void tmdi_manager::init_menu (HINSTANCE hinst,
      LPCSTR menu_rc_name,WPARAM window_submenu_pos) {
   window_menu = (HMENU)LoadMenu (hinst,menu_rc_name);
   window_submenu = (HMENU)GetSubMenu (window_menu,
      window_submenu_pos);}

void tmdi_manager::set_frame_menu (HMENU the_frame_menu,
        HMENU the_frame_submenu) {
     frame_menu = the_frame_menu;
     frame_submenu = the_frame_submenu; }

tmdi_manager::~tmdi_manager() {
     for (int i=1;i<=window_list->get_count();i++) {
      delete ((child_window_struct *)
        (window_list->at(i)))->window;
      delete window_list->at(i); }
     delete window_list;
     DestroyMenu (this->window_menu); }

tmdi_child_window::tmdi_child_window (HWND hwnd,
      tmdi_manager *the_manager,LPCSTR title_name):
      tmdi_window (0,0,0,0,title_name,0) {
      hwindow = hwnd;
      manager = the_manager; }

int tmdi_child_window::respond_wm_create (HWND hwnd) {
    return 1; }

int tmdi_child_window::respond_wm_mdiactivate
    (HWND hwnd, WPARAM wParam,LPARAM lParam) {
    if ACTIVATE_MDI_CHILD_WINDOW(hwnd,wParam,lParam)
       SendMessage (manager->hwndClient,WM_MDISETMENU,
       MDI_SETMENU_MSGPARAMS(manager->window_menu,
       manager->window_submenu)) ;
    else
       SendMessage (manager->hwndClient,WM_MDISETMENU,
      MDI_SETMENU_MSGPARAMS(manager->frame_menu,
      manager->frame_submenu)) ;
       DrawMenuBar (manager->hwndFrame) ;
       return 1; }

int tmdi_child_window::respond_wm_queryendsession()
       { return respond_wm_close (); }

int tmdi_child_window::respond_wm_close () {
       if (IDOK != MessageBox (hwindow,
      "OK to close window?",the_title,
      MB_ICONQUESTION | MB_OKCANCEL))
       return 1 ;
       return 0 ; } // ie, call default_window_proc
//End of File