Annotation of mstools/mfc/samples/oserver/mainwnd.cpp, revision 1.1.1.1

1.1       root        1: // This is a part of the Microsoft Foundation Classes C++ library.
                      2: // Copyright (C) 1992 Microsoft Corporation
                      3: // All rights reserved.
                      4: //
                      5: // This source code is only intended as a supplement to the
                      6: // Microsoft Foundation Classes Reference and Microsoft
                      7: // QuickHelp documentation provided with the library.
                      8: // See these sources for detailed information regarding the
                      9: // Microsoft Foundation Classes product.
                     10: 
                     11: 
                     12: #include "bibref.h"
                     13: #include "mainwnd.h"
                     14: #include "bibdoc.h"
                     15: 
                     16: /////////////////////////////////////////////////////////////////////////////
                     17: 
                     18: BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
                     19:        ON_WM_INITMENUPOPUP()
                     20:        ON_WM_SIZE()
                     21:        ON_WM_CREATE()
                     22:        ON_WM_CLOSE()
                     23:        ON_WM_DESTROY()
                     24:        ON_WM_SETFOCUS()
                     25:        ON_COMMAND(IDM_ABOUT, OnAbout)
                     26:        ON_COMMAND(IDM_EXIT, OnClose)       // Exit just closes main window
                     27: 
                     28:        // only make sense if run non-embedded
                     29:        ON_COMMAND(IDM_ITEM_ADD, OnItemAdd)
                     30:        ON_COMMAND(IDM_ITEM_DELETE, OnItemDelete)
                     31:        ON_COMMAND(IDM_ITEM_MODIFY, OnItemModify)
                     32: 
                     33:        // for embedded case
                     34:        ON_COMMAND(IDM_UPDATE, OnUpdateClient)
                     35: END_MESSAGE_MAP()
                     36: 
                     37: /////////////////////////////////////////////////////////////////////////////
                     38: // Basic creation etc
                     39: 
                     40: CMainWnd::CMainWnd(BOOL bEmbedded)
                     41: {
                     42:        CRect rect;
                     43:        char szRect[32];
                     44: 
                     45:        if (GetPrivateProfileString("Settings", "Last Position", "",
                     46:                szRect, sizeof(szRect), ::app.strIniFile) == 0 ||
                     47:                sscanf(szRect, "%d %d %d %d", &rect.left, &rect.top,
                     48:                        &rect.right, &rect.bottom) != 4)
                     49:        {
                     50:                // use initial value
                     51:                rect = rectDefault;
                     52:        }
                     53: 
                     54:        Create(NULL, "Bibliographic References",
                     55:                WS_OVERLAPPEDWINDOW, rect, NULL,
                     56:                bEmbedded ? "EmbeddedMenu" : "MainMenu");
                     57: }
                     58: 
                     59: void CMainWnd::OnDestroy()
                     60: {
                     61:        CRect rect;
                     62:        GetWindowRect(&rect);
                     63: 
                     64:        char szRect[32];
                     65:        sprintf(szRect, "%d %d %d %d", 
                     66:                rect.left, rect.top, rect.right, rect.bottom);
                     67:        WritePrivateProfileString("Settings", "Last Position",
                     68:                szRect, ::app.strIniFile);
                     69: 
                     70:        CFrameWnd::OnDestroy();
                     71: }
                     72: 
                     73: 
                     74: int CMainWnd::OnCreate(LPCREATESTRUCT)
                     75: {
                     76:        // Create the listbox child window
                     77:        CRect       rect;
                     78:        GetClientRect(&rect);
                     79: 
                     80:        if (!m_listbox.Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_VSCROLL |
                     81:          LBS_SORT | LBS_USETABSTOPS | LBS_NOINTEGRALHEIGHT,
                     82:          rect, this, -1))
                     83:        {
                     84:                TRACE("Error creating listbox\n");
                     85:                return -1;
                     86:        }
                     87: 
                     88:        m_listbox.SetTabStops(40);      // 10 characters wide
                     89: 
                     90:        m_pDoc = new CBibDoc(&m_listbox, ::app.strIniFile, "Items");
                     91: 
                     92:        if (!m_pDoc->Load())
                     93:        {
                     94:                MessageBox("Load failed");
                     95:                delete m_pDoc;
                     96:                return -1;
                     97:        }
                     98: 
                     99:        return 0;   // ok
                    100: }
                    101: 
                    102: /////////////////////////////////////////////////////////////////////////////
                    103: // Closing the window/server
                    104: 
                    105: void CMainWnd::OnClose()
                    106: {
                    107:        // prompt if open and something selected (assume dirty)
                    108:        if (m_pDoc->IsOpen() && m_listbox.GetCurSel() != -1)
                    109:        {
                    110:                // optionally update the client on exit
                    111:                char szPrompt[256];
                    112:                wsprintf(szPrompt, "Update %s %s?", (LPCSTR)GetDocument()->m_strHost,
                    113:                        (LPCSTR)GetDocument()->m_strHostObj);
                    114: 
                    115:                // Note: should only prompt if document is dirty, instead of all
                    116:                //  the time as here.
                    117:                if (MessageBox(szPrompt, AfxGetAppName(), MB_YESNO) == IDYES)
                    118:                        OnUpdateClient();
                    119:        }
                    120: 
                    121:        app.ShutDown();
                    122: }
                    123: 
                    124: /////////////////////////////////////////////////////////////////////////////
                    125: 
                    126: void CMainWnd::OnSize(UINT, int, int)
                    127: {
                    128:        // Resize child
                    129:        CRect       rect;
                    130:        GetClientRect(&rect);
                    131:        m_listbox.MoveWindow(rect);
                    132: }
                    133: 
                    134: void CMainWnd::OnSetFocus(CWnd*)
                    135: {
                    136:        // forward the focus to our one and only child
                    137:        m_listbox.SetFocus();
                    138: }
                    139: 
                    140: void CMainWnd::OnInitMenuPopup(CMenu* pPopupMenu, UINT, BOOL bSysMenu)
                    141: {
                    142:        if (bSysMenu)
                    143:                return;
                    144: 
                    145:        // if no item selected - disable delete and modify
                    146:        UINT    mf = MF_GRAYED|MF_DISABLED;
                    147: 
                    148:        if (m_listbox.GetCurSel() != -1)
                    149:                mf = MF_ENABLED;
                    150:        pPopupMenu->EnableMenuItem(IDM_ITEM_DELETE, mf);
                    151:        pPopupMenu->EnableMenuItem(IDM_ITEM_MODIFY, mf);
                    152:        pPopupMenu->EnableMenuItem(IDM_UPDATE, mf);
                    153: }
                    154: 
                    155: /////////////////////////////////////////////////////////////////////////////
                    156: // File menu commands
                    157: 
                    158: void CMainWnd::OnAbout()
                    159: {
                    160:        CModalDialog about(IDM_ABOUT, this);
                    161:        about.DoModal();
                    162: }
                    163: 
                    164: /////////////////////////////////////////////////////////////////////////////
                    165: // Item menu commands
                    166: 
                    167: class CPromptDlg : public CModalDialog
                    168: {
                    169:        CString&    m_rKey;
                    170:        CString&    m_rValue;
                    171: public:
                    172:        CPromptDlg(CString& key, CString& value)
                    173:                : CModalDialog(IDM_ITEM_ADD), m_rKey(key), m_rValue(value)
                    174:                { }
                    175: 
                    176: protected:
                    177:        CEdit&      Edit1()
                    178:                                        { return *((CEdit*) GetDlgItem(IDC_EDIT1)); } 
                    179:        CEdit&      Edit2()
                    180:                                        { return *((CEdit*) GetDlgItem(IDC_EDIT2)); } 
                    181: 
                    182:        BOOL    OnInitDialog()
                    183:        {
                    184:                Edit1().SetWindowText(m_rKey);
                    185:                Edit2().SetWindowText(m_rValue);
                    186:                return TRUE;
                    187:        }
                    188: 
                    189:        void OnOK()
                    190:        {
                    191:                Edit1().GetWindowText(m_rKey);
                    192:                Edit2().GetWindowText(m_rValue);
                    193: 
                    194:                if (m_rKey == "" || m_rValue == "")
                    195:                {
                    196:                        MessageBox("Both key and value must be specified");
                    197:                        return;
                    198:                }
                    199:                EndDialog(IDOK);
                    200:        }
                    201: };
                    202: 
                    203: void CMainWnd::OnItemAdd()
                    204: {
                    205:        CString key, value;
                    206:        CPromptDlg  dlg(key, value);
                    207: 
                    208:        if (dlg.DoModal() != IDOK)
                    209:                return;
                    210: 
                    211:        CString oldValue;
                    212:        int nOldIndex = m_pDoc->GetItemValue(key, oldValue);
                    213:        if (nOldIndex != -1)
                    214:                m_pDoc->DeleteItem(key, nOldIndex);
                    215: 
                    216:        // add the item
                    217:        m_pDoc->AddItem(key, value);
                    218: }
                    219: 
                    220: void CMainWnd::OnItemDelete()
                    221: {
                    222:        int nIndex = m_listbox.GetCurSel();
                    223:        CString key, value;
                    224:        m_pDoc->GetItemKeyValue(nIndex, key, value);
                    225: 
                    226:        // delete the key (in listbox and in ini file)
                    227:        m_pDoc->DeleteItem(key, nIndex);
                    228: }
                    229: 
                    230: void CMainWnd::OnItemModify()
                    231: {
                    232:        int nIndex = m_listbox.GetCurSel();
                    233:        CString key, value;
                    234:        m_pDoc->GetItemKeyValue(nIndex, key, value);
                    235: 
                    236:        CString originalKey = key;
                    237:        CPromptDlg  dlg(key, value);
                    238: 
                    239:        if (dlg.DoModal() != IDOK)
                    240:                return;
                    241: 
                    242:        m_pDoc->DeleteItem(originalKey, nIndex);
                    243:        m_pDoc->AddItem(key, value);
                    244: }
                    245: 
                    246: /////////////////////////////////////////////////////////////////////////////
                    247: // Update Client
                    248: 
                    249: void CMainWnd::OnUpdateClient()
                    250: {
                    251:        int nIndex = m_listbox.GetCurSel();
                    252:        if (nIndex == -1)
                    253:                return;
                    254: 
                    255:        CString key, value;
                    256:        m_pDoc->GetItemKeyValue(nIndex, key, value);
                    257:        if (!m_pDoc->UpdateClient(key))
                    258:                MessageBox("Couldn't update client");
                    259: }
                    260: 
                    261: /////////////////////////////////////////////////////////////////////////////

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.