home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 March / PCWorld_2001-03_cd.bin / Software / TemaCD / devc / _SETUP.6 / Group20 / mdi_unit.c < prev    next >
C/C++ Source or Header  |  2000-04-24  |  17KB  |  507 lines

  1. #include <windows.h>
  2. #include <commctrl.h>
  3.  
  4. #include "mdi_unit.rh"
  5.  
  6. #define ID_STATUSBAR       4997
  7. #define ID_TOOLBAR         4998
  8.  
  9. #define ID_MDI_CLIENT      4999
  10. #define ID_MDI_FIRSTCHILD  50000
  11.  
  12. #define IDC_CHILD_EDIT      2000
  13.  
  14. LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
  15. LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT Message, WPARAM wParam,
  16.    LPARAM lParam);
  17.  
  18. char g_szAppName[] = "MyMDIWindow";
  19. char g_szChild[] = "MyMDIChild";
  20. HINSTANCE g_hInst;
  21. HWND g_hMDIClient, g_hStatusBar, g_hToolBar;
  22. HWND g_hMainWindow;
  23.  
  24. BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
  25. {
  26.    HANDLE hFile;
  27.    BOOL bSuccess = FALSE;
  28.  
  29.    hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  30.       OPEN_EXISTING, NULL, NULL);
  31.    if(hFile != INVALID_HANDLE_VALUE)
  32.    {
  33.       DWORD dwFileSize;
  34.       dwFileSize = GetFileSize(hFile, NULL);
  35.       if(dwFileSize != 0xFFFFFFFF)
  36.       {
  37.          LPSTR pszFileText;
  38.          pszFileText = LPSTR(GlobalAlloc(GPTR, dwFileSize + 1));
  39.          if(pszFileText != NULL)
  40.          {
  41.             DWORD dwRead;
  42.             if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
  43.             {
  44.                pszFileText[dwFileSize] = 0; // Null terminator
  45.                if(SetWindowText(hEdit, pszFileText))
  46.                   bSuccess = TRUE; // It worked!
  47.             }
  48.             GlobalFree(pszFileText);
  49.          }
  50.       }
  51.       CloseHandle(hFile);
  52.    }
  53.    return bSuccess;
  54. }
  55.  
  56. BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
  57. {
  58.    HANDLE hFile;
  59.    BOOL bSuccess = FALSE;
  60.  
  61.    hFile = CreateFile(pszFileName, GENERIC_WRITE, NULL, NULL,
  62.       CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  63.    if(hFile != INVALID_HANDLE_VALUE)
  64.    {
  65.       DWORD dwTextLength;
  66.       dwTextLength = GetWindowTextLength(hEdit);
  67.       if(dwTextLength > 0)// No need to bother if there's no text.
  68.       {
  69.          LPSTR pszText;
  70.          pszText = LPSTR(GlobalAlloc(GPTR, dwTextLength + 1));
  71.          if(pszText != NULL)
  72.          {
  73.             if(GetWindowText(hEdit, pszText, dwTextLength + 1))
  74.             {
  75.                DWORD dwWritten;
  76.                if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
  77.                   bSuccess = TRUE;
  78.             }
  79.             GlobalFree(pszText);
  80.          }
  81.       }
  82.       CloseHandle(hFile);
  83.    }
  84.    return bSuccess;
  85. }
  86.  
  87. BOOL GetFileName(HWND hwnd, LPSTR pszFileName, BOOL bSave)
  88. {
  89.    OPENFILENAME ofn;
  90.  
  91.    ZeroMemory(&ofn, sizeof(ofn));
  92.    pszFileName[0] = 0;
  93.  
  94.    ofn.lStructSize = sizeof(ofn);
  95.    ofn.hwndOwner = hwnd;
  96.    ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
  97.    ofn.lpstrFile = pszFileName;
  98.    ofn.nMaxFile = MAX_PATH;
  99.    ofn.lpstrDefExt = "txt";
  100.  
  101.    if(bSave)
  102.    {
  103.       ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
  104.          OFN_OVERWRITEPROMPT;
  105.       if(!GetSaveFileName(&ofn))
  106.          return FALSE;
  107.    }
  108.    else
  109.    {
  110.       ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  111.       if(!GetOpenFileName(&ofn))
  112.          return FALSE;
  113.    }
  114.    return TRUE;
  115. }
  116.  
  117. #pragma argsused
  118. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  119.    LPSTR lpszCmdParam, int nCmdShow)
  120. {
  121.    MSG  Msg;
  122.    WNDCLASSEX WndClassEx;
  123.  
  124.    InitCommonControls();
  125.  
  126.    g_hInst = hInstance;
  127.  
  128.    WndClassEx.cbSize          = sizeof(WNDCLASSEX);
  129.    WndClassEx.style           = CS_HREDRAW | CS_VREDRAW;
  130.    WndClassEx.lpfnWndProc     = WndProc;
  131.    WndClassEx.cbClsExtra      = 0;
  132.    WndClassEx.cbWndExtra      = 0;
  133.    WndClassEx.hInstance       = hInstance;
  134.    WndClassEx.hIcon           = LoadIcon(NULL, IDI_APPLICATION);
  135.    WndClassEx.hCursor         = LoadCursor(NULL, IDC_ARROW);
  136.    WndClassEx.hbrBackground   = (HBRUSH)(COLOR_3DSHADOW+1);
  137.    WndClassEx.lpszMenuName       = "MAIN";
  138.    WndClassEx.lpszClassName   = g_szAppName;
  139.    WndClassEx.hIconSm           = LoadIcon(NULL, IDI_APPLICATION);
  140.  
  141.    if(!RegisterClassEx(&WndClassEx))
  142.    {
  143.       MessageBox(0, "Could Not Register Window", "Oh Oh...",
  144.          MB_ICONEXCLAMATION | MB_OK);
  145.       return -1;
  146.    }
  147.  
  148.    WndClassEx.lpfnWndProc     = MDIChildWndProc;
  149.    WndClassEx.lpszMenuName       = NULL;
  150.    WndClassEx.lpszClassName   = g_szChild;
  151.    WndClassEx.hbrBackground   = (HBRUSH)(COLOR_3DFACE+1);
  152.  
  153.    if(!RegisterClassEx(&WndClassEx))
  154.    {
  155.       MessageBox(0, "Could Not Register Child Window", "Oh Oh...",
  156.          MB_ICONEXCLAMATION | MB_OK);
  157.       return -1;
  158.    }
  159.  
  160.     g_hMainWindow = CreateWindowEx(NULL, g_szAppName,
  161.       "MDI File Editor", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  162.       CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  163.       NULL, NULL, hInstance, NULL);
  164.  
  165.    if (g_hMainWindow == NULL){
  166.       MessageBox(0, "No Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
  167.       return -1;
  168.    }
  169.  
  170.    ShowWindow(g_hMainWindow, nCmdShow);
  171.    UpdateWindow(g_hMainWindow);
  172.  
  173.    while(GetMessage(&Msg, NULL, 0, 0))
  174.    {
  175.       if (!TranslateMDISysAccel(g_hMDIClient, &Msg))
  176.       {
  177.          TranslateMessage(&Msg);
  178.          DispatchMessage(&Msg);
  179.       }
  180.    }
  181.    return Msg.wParam;
  182. }
  183.  
  184.  
  185. LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  186. {
  187.    switch(Message)
  188.    {
  189.       case WM_CREATE:
  190.       {
  191.          CLIENTCREATESTRUCT ccs;
  192.          int iStatusWidths[] = {200, 300, -1};
  193.          TBADDBITMAP tbab;
  194.          TBBUTTON tbb[9];
  195.  
  196.          // Find window menu where children will be listed
  197.          ccs.hWindowMenu  = GetSubMenu(GetMenu(hwnd), 2);
  198.          ccs.idFirstChild = ID_MDI_FIRSTCHILD;
  199.          g_hMDIClient = CreateWindowEx(WS_EX_CLIENTEDGE, "mdiclient", NULL,
  200.             WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL,
  201.             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  202.             hwnd, (HMENU)ID_MDI_CLIENT, g_hInst, (LPVOID)&ccs);
  203.          ShowWindow(g_hMDIClient, SW_SHOW);
  204.  
  205.          g_hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, NULL,
  206.             WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
  207.             hwnd, (HMENU)ID_STATUSBAR, g_hInst, NULL);
  208.          SendMessage(g_hStatusBar, SB_SETPARTS, 3, (LPARAM)iStatusWidths);
  209.          SendMessage(g_hStatusBar, SB_SETTEXT, 2, (LPARAM)"Toolbar & Statusbar Example");
  210.  
  211.          g_hToolBar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
  212.             WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
  213.             hwnd, (HMENU)ID_TOOLBAR, g_hInst, NULL);
  214.  
  215.          // Send the TB_BUTTONSTRUCTSIZE message, which is required for
  216.          // backward compatibility.
  217.          SendMessage(g_hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
  218.  
  219.          tbab.hInst = HINST_COMMCTRL;
  220.          tbab.nID = IDB_STD_SMALL_COLOR;
  221.          SendMessage(g_hToolBar, TB_ADDBITMAP, 0, (LPARAM)&tbab);
  222.  
  223.          ZeroMemory(tbb, sizeof(tbb));
  224.  
  225.          tbb[0].iBitmap = STD_FILENEW;
  226.          tbb[0].fsState = TBSTATE_ENABLED;
  227.          tbb[0].fsStyle = TBSTYLE_BUTTON;
  228.          tbb[0].idCommand = CM_FILE_NEW;
  229.  
  230.          tbb[1].iBitmap = STD_FILEOPEN;
  231.          tbb[1].fsState = TBSTATE_ENABLED;
  232.          tbb[1].fsStyle = TBSTYLE_BUTTON;
  233.          tbb[1].idCommand = CM_FILE_OPEN;
  234.  
  235.          tbb[2].iBitmap = STD_FILESAVE;
  236.          tbb[2].fsStyle = TBSTYLE_BUTTON;
  237.          tbb[2].idCommand = CM_FILE_SAVE;
  238.  
  239.          tbb[3].fsStyle = TBSTYLE_SEP;
  240.  
  241.          tbb[4].iBitmap = STD_CUT;
  242.          tbb[4].fsStyle = TBSTYLE_BUTTON;
  243.          tbb[4].idCommand = CM_EDIT_CUT;
  244.  
  245.          tbb[5].iBitmap = STD_COPY;
  246.          tbb[5].fsStyle = TBSTYLE_BUTTON;
  247.          tbb[5].idCommand = CM_EDIT_COPY;
  248.  
  249.          tbb[6].iBitmap = STD_PASTE;
  250.          tbb[6].fsStyle = TBSTYLE_BUTTON;
  251.          tbb[6].idCommand = CM_EDIT_PASTE;
  252.  
  253.          tbb[7].fsStyle = TBSTYLE_SEP;
  254.  
  255.          tbb[8].iBitmap = STD_UNDO;
  256.          tbb[8].fsStyle = TBSTYLE_BUTTON;
  257.          tbb[8].idCommand = CM_EDIT_UNDO;
  258.  
  259.          SendMessage(g_hToolBar, TB_ADDBUTTONS, 9, (LPARAM)&tbb);
  260.  
  261.          return 0;
  262.       }
  263.       case WM_COMMAND:
  264.       {
  265.          switch(LOWORD(wParam))
  266.          {
  267.             case CM_FILE_EXIT:
  268.                PostMessage(hwnd, WM_CLOSE, 0, 0);
  269.             break;
  270.             case CM_FILE_NEW:
  271.             {
  272.                MDICREATESTRUCT mcs;
  273.                HWND hChild;
  274.  
  275.                mcs.szTitle = "[Untitled]";
  276.                mcs.szClass = g_szChild;
  277.                mcs.hOwner  = g_hInst;
  278.                mcs.x = mcs.cx = CW_USEDEFAULT;
  279.                mcs.y = mcs.cy = CW_USEDEFAULT;
  280.                mcs.style = MDIS_ALLCHILDSTYLES;
  281.  
  282.                hChild = (HWND)SendMessage(g_hMDIClient, WM_MDICREATE,
  283.                   0, (LONG)&mcs);
  284.                if(!hChild)
  285.                {
  286.                   MessageBox(hwnd, "MDI Child creation failed.", "Oh Oh...",
  287.                      MB_ICONEXCLAMATION | MB_OK);
  288.                }
  289.             }
  290.             break;
  291.             case CM_FILE_OPEN:
  292.             {
  293.                MDICREATESTRUCT mcs;
  294.                HWND hChild;
  295.                char szFileName[MAX_PATH];
  296.  
  297.                if(!GetFileName(hwnd, szFileName, FALSE))
  298.                   break;
  299.  
  300.                mcs.szTitle = szFileName;
  301.                mcs.szClass = g_szChild;
  302.                mcs.hOwner  = g_hInst;
  303.                mcs.x = mcs.cx = CW_USEDEFAULT;
  304.                mcs.y = mcs.cy = CW_USEDEFAULT;
  305.                mcs.style = MDIS_ALLCHILDSTYLES;
  306.  
  307.                hChild = (HWND)SendMessage(g_hMDIClient, WM_MDICREATE,
  308.                   0, (LONG)&mcs);
  309.  
  310.                if(!hChild)
  311.                {
  312.                   MessageBox(hwnd, "MDI Child creation failed.", "Oh Oh...",
  313.                      MB_ICONEXCLAMATION | MB_OK);
  314.                }
  315.             }
  316.             break;
  317.             case CM_WINDOW_TILEHORZ:
  318.                PostMessage(g_hMDIClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
  319.             break;
  320.             case CM_WINDOW_TILEVERT:
  321.                PostMessage(g_hMDIClient, WM_MDITILE, MDITILE_VERTICAL, 0);
  322.             break;
  323.             case CM_WINDOW_CASCADE:
  324.                PostMessage(g_hMDIClient, WM_MDICASCADE, 0, 0);
  325.             break;
  326.             case CM_WINDOW_ARRANGE:
  327.                PostMessage(g_hMDIClient, WM_MDIICONARRANGE, 0, 0);
  328.             break;
  329.             default:
  330.             {
  331.                if(LOWORD(wParam) >= ID_MDI_FIRSTCHILD){
  332.                   DefFrameProc(hwnd, g_hMDIClient, Message, wParam, lParam);
  333.                }
  334.                else {
  335.                   HWND hChild;
  336.                   hChild = (HWND)SendMessage(g_hMDIClient, WM_MDIGETACTIVE,0,0);
  337.                   if(hChild){
  338.                      SendMessage(hChild, WM_COMMAND, wParam, lParam);
  339.                   }
  340.                }
  341.             }
  342.          }
  343.       }
  344.       break;
  345.       case WM_SIZE:
  346.       {
  347.          RECT rectClient, rectStatus, rectTool;
  348.          UINT uToolHeight, uStatusHeight, uClientAlreaHeight;
  349.  
  350.          SendMessage(g_hToolBar, TB_AUTOSIZE, 0, 0);
  351.          SendMessage(g_hStatusBar, WM_SIZE, 0, 0);
  352.  
  353.          GetClientRect(hwnd, &rectClient);
  354.          GetWindowRect(g_hStatusBar, &rectStatus);
  355.          GetWindowRect(g_hToolBar, &rectTool);
  356.  
  357.          uToolHeight = rectTool.bottom - rectTool.top;
  358.          uStatusHeight = rectStatus.bottom - rectStatus.top;
  359.          uClientAlreaHeight = rectClient.bottom;
  360.  
  361.          MoveWindow(g_hMDIClient, 0, uToolHeight, rectClient.right, uClientAlreaHeight - uStatusHeight - uToolHeight, TRUE);
  362.       }
  363.       break;
  364.       case WM_CLOSE:
  365.          DestroyWindow(hwnd);
  366.       break;
  367.       case WM_DESTROY:
  368.          PostQuitMessage(0);
  369.       break;
  370.       default:
  371.          return DefFrameProc(hwnd, g_hMDIClient, Message, wParam, lParam);
  372.    }
  373.    return 0;
  374. }
  375.  
  376. LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT Message, WPARAM wParam,
  377.    LPARAM lParam)
  378. {
  379.    switch(Message)
  380.    {
  381.       case WM_CREATE:
  382.       {
  383.          char szFileName[MAX_PATH];
  384.          HWND hEdit;
  385.  
  386.          hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
  387.             WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
  388.                ES_WANTRETURN,
  389.             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  390.             hwnd, (HMENU)IDC_CHILD_EDIT, g_hInst, NULL);
  391.  
  392.          SendMessage(hEdit, WM_SETFONT,
  393.             (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
  394.  
  395.          GetWindowText(hwnd, szFileName, MAX_PATH);
  396.          if(*szFileName != '[')
  397.          {
  398.             if(!LoadFile(hEdit, szFileName))
  399.             {
  400.                MessageBox(hwnd, "Couldn't Load File.", "Error.",
  401.                   MB_OK | MB_ICONEXCLAMATION);
  402.                return -1; //cancel window creation
  403.             }
  404.          }
  405.       }
  406.       break;
  407.       case WM_SIZE:
  408.          if(wParam != SIZE_MINIMIZED)
  409.             MoveWindow(GetDlgItem(hwnd, IDC_CHILD_EDIT), 0, 0, LOWORD(lParam),
  410.                HIWORD(lParam), TRUE);
  411.       break;
  412.       case WM_MDIACTIVATE:
  413.       {
  414.          HMENU hMenu, hFileMenu;
  415.          BOOL EnableFlag;
  416.          char szFileName[MAX_PATH];
  417.  
  418.          hMenu = GetMenu(g_hMainWindow);
  419.          if(hwnd == (HWND)lParam){      //being activated
  420.             EnableFlag = TRUE;
  421.          }
  422.          else{
  423.             EnableFlag = FALSE;    //being de-activated
  424.          }
  425.          EnableMenuItem(hMenu, 1, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
  426.          EnableMenuItem(hMenu, 2, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
  427.  
  428.          hFileMenu = GetSubMenu(hMenu, 0);
  429.          EnableMenuItem(hFileMenu, CM_FILE_SAVE, MF_BYCOMMAND | (EnableFlag ? MF_ENABLED : MF_GRAYED));
  430.          EnableMenuItem(hFileMenu, CM_FILE_SAVEAS, MF_BYCOMMAND | (EnableFlag ? MF_ENABLED : MF_GRAYED));
  431.  
  432.          DrawMenuBar(g_hMainWindow);
  433.  
  434.          SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_FILE_SAVE, MAKELONG(EnableFlag, 0));
  435.          SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_UNDO, MAKELONG(EnableFlag, 0));
  436.          SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_CUT, MAKELONG(EnableFlag, 0));
  437.          SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_COPY, MAKELONG(EnableFlag, 0));
  438.          SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_PASTE, MAKELONG(EnableFlag, 0));
  439.  
  440.          GetWindowText(hwnd, szFileName, MAX_PATH);
  441.          SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)(EnableFlag ? szFileName : ""));
  442.       }
  443.       break;
  444.       case WM_SETFOCUS:
  445.          SetFocus(GetDlgItem(hwnd, IDC_CHILD_EDIT));
  446.       break;
  447.       case WM_COMMAND:
  448.          switch(LOWORD(wParam))
  449.          {
  450.             case CM_FILE_SAVE:
  451.             {
  452.                char szFileName[MAX_PATH];
  453.  
  454.                GetWindowText(hwnd, szFileName, MAX_PATH);
  455.                if(*szFileName != '[')
  456.                {
  457.                   if(!SaveFile(GetDlgItem(hwnd, IDC_CHILD_EDIT), szFileName))
  458.                   {
  459.                      MessageBox(hwnd, "Couldn't Save File.", "Error.",
  460.                         MB_OK | MB_ICONEXCLAMATION);
  461.                      return 0;
  462.                   }
  463.                }
  464.                else
  465.                {
  466.                   PostMessage(hwnd, WM_COMMAND,
  467.                      MAKEWPARAM(CM_FILE_SAVEAS, 0), 0);
  468.                }
  469.             }
  470.             return 0;
  471.             case CM_FILE_SAVEAS:
  472.             {
  473.                char szFileName[MAX_PATH];
  474.  
  475.                if(GetFileName(hwnd, szFileName, TRUE))
  476.                {
  477.                   if(!SaveFile(GetDlgItem(hwnd, IDC_CHILD_EDIT), szFileName))
  478.                   {
  479.                      MessageBox(hwnd, "Couldn't Save File.", "Error.",
  480.                         MB_OK | MB_ICONEXCLAMATION);
  481.                      return 0;
  482.                   }
  483.                   else
  484.                   {
  485.                      SetWindowText(hwnd, szFileName);
  486.                   }
  487.                }
  488.             }
  489.             return 0;
  490.             case CM_EDIT_UNDO:
  491.                SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, EM_UNDO, 0, 0);
  492.             break;
  493.             case CM_EDIT_CUT:
  494.                SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_CUT, 0, 0);
  495.             break;
  496.             case CM_EDIT_COPY:
  497.                SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_COPY, 0, 0);
  498.             break;
  499.             case CM_EDIT_PASTE:
  500.                SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_PASTE, 0, 0);
  501.             break;
  502.          }
  503.       return 0;
  504.    }
  505.    return DefMDIChildProc(hwnd, Message, wParam, lParam);
  506. }
  507.