www.pudn.com > TurboPadSource.tar.gz > child.cpp


/** 
*    \file Child Frame implementation 
* 
*    Implementation of Child functions. 
*/ 
 
#include "child.h" 
 
Child::Child(PPtr parent, wxWindowID id, const wxString& title, 
              const wxPoint& pos, const wxSize& size, 
              long style, wxString fName) 
     : wxMDIChildFrame(parent, id, title, pos, size, style, "Child"), 
     fileName(fName), highlighter("Text") 
{ 
    SetIcon(wxICON(Text)); 
 
    statusBar = new wxStatusBar(this, -1); 
    SetStatusBar(statusBar); 
 
    text = new STextCtrl(this, -1, wxDefaultPosition, GetClientSize(), wxSUNKEN_BORDER); 
 
    UpdatePos(); 
 
    GetParent()->AddFrame(this); 
    GetParent()->SetCWin(this); 
 
    GetParent()->SetHil(highlighter); 
 
    if (GetParent()->GetFCount() == 1)  // This is the first window opened, enable the menus 
        GetParent()->EnableMenus(); 
 
    GetParent()->UpdateMenus(); 
} 
 
Child::~Child() 
{ 
    if (GetParent()->GetFrames().Index(this) == wxNOT_FOUND) 
        wxSafeShowMessage("Error Closing Child Frame", "Child not found in array."); 
 
    text->Destroy(); 
    statusBar->Destroy(); 
 
    GetParent()->RemoveFrame(this); 
 
    // If this is the last frame and the program is not being closed 
    // disable the menus and reset the highlighter since no more child frames exist 
    if (!GetParent()->GetFCount() && GetParent()->GetWindowStatus()->GetStatus() != 3) 
    { 
        GetParent()->EnableMenus(false); 
 
        if (GetParent()->GetHil() != "Text") 
            GetParent()->ChangeHil("Text"); 
    } 
} 
 
inline void Child::CheckModTime() 
{ 
    if (fileName == "" || fileName == "Untitled" || fileName == "Untitled*")  
        return; 
         
    wxFileName file(fileName); 
     
    if (!file.FileExists()) 
    { 
        wxMessageBox("The file '" + GetTitle().BeforeLast('*') + 
                     "' has been\nmoved, renamed or deleted.", "File Moved, Renamed or Deleted",  
                     wxOK | wxICON_INFORMATION); 
        return; 
    } 
     
    if (file.GetModificationTime() > modTime) 
    { 
        int reload = wxMessageBox("The file '" + wxFileNameFromPath(fileName) + 
                                  "' has been modified externally. \nWould you"  
                                  " like to reload the file?", 
                                  "File Modified Externally",  
                                  wxYES_NO | wxICON_QUESTION); 
        if (reload == wxYES) 
            RefreshFile(); 
             
        UpdateModTime(); 
    }         
} 
 
inline PPtr Child::GetParent() 
{ 
    return static_cast  (wxMDIChildFrame::GetParent()); 
} 
 
void Child::OnActivate(wxActivateEvent &event) 
{ 
    // If this function has already been executed 
    // for this activate event, don't do it again 
    if (GetParent()->GetCWin() == this) return; 
     
    if (GetParent()->GetWindowStatus()->IsIdle()) // Don't do this when opening or closing files 
    { 
        GetParent()->SetCWin(this); 
        GetParent()->UpdateMenus(); 
 
        CheckModTime(); 
        text->SetFocus(); 
    } 
 
    GetParent()->SetHil(highlighter); 
} 
 
void Child::OnWindowClose(wxCloseEvent& event) 
{ 
    int closeDialog; 
 
    if (event.CanVeto() && GetTitle().Contains("*")) 
    { 
        closeDialog = wxMessageBox("The file '" + GetTitle().BeforeLast('*') + 
                                "' has been modified. \nWould you like to save the changes?", 
                                "File Modified", wxYES_NO | wxCANCEL | wxICON_EXCLAMATION, GetParent()); 
 
        // Save Changes 
        if (closeDialog == wxYES) 
            GetParent()->Command(Par_Save); 
 
        // Cancel 
        if (closeDialog == wxCANCEL) 
        { 
            if (GetParent()->GetWindowStatus()->GetStatus() != 0) // If windows are being closed, make sure the 
                GetParent()->GetWindowStatus()->SetVeto(true);    // action is vetoed because the user pressed cancel 
 
            event.Veto(); 
            return; 
        } 
    } 
 
    event.Skip(); 
} 
 
inline bool Child::RefreshFile() 
{ 
    if (fileName == "Untitled") return false; 
     
    else 
    { 
        if (!wxFileExists(fileName)) 
        { 
            wxMessageBox(wxFileNameFromPath(fileName) + " has been\n" 
                         "renamed or deleted.", "File Renamed or Deleted", 
                         wxOK | wxICON_ERROR); 
            return false; 
        } 
 
        text->LoadFile(fileName); 
        GetParent()->UpdateMenus(); 
    } 
     
    return true; 
} 
 
inline void Child::UpdateModTime() 
{ 
    if (fileName == "" || fileName == "Untitled" || fileName == "Untitled*")  
        return; 
     
    wxFileName file(fileName); 
    modTime = file.GetModificationTime(); 
} 
 
inline void Child::UpdatePos() 
{ 
    int cPos = text->GetCurrentPos(); 
    wxString pos; 
 
    pos.Printf("  %d : %d", text->GetColumn(cPos)+1, text->LineFromPosition(cPos)+1); 
 
    statusBar->SetStatusText(pos, 0); 
} 
 
BEGIN_EVENT_TABLE(Child, wxMDIChildFrame) 
    EVT_ACTIVATE(Child::OnActivate) 
    EVT_CLOSE(Child::OnWindowClose) 
END_EVENT_TABLE()