Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vuMainWindow.cpp

Go to the documentation of this file.
00001 #include "vuMainWindow.h"
00002 #include "vuUtilityFactory.h"
00003 #include <wx/menu.h>
00004 #include <wx/filedlg.h>
00005 #include <wx/msgdlg.h>
00006 #include <wx/choicdlg.h>
00007 #include "vuFileHelper.h"
00008 
00009 //----------------------------------------------------------------------------
00010 //------------------------- The vuMainWindow event table ---------------------
00011 //----------------------------------------------------------------------------
00012 
00013 enum
00014 {
00015     MENU_EXIT = 100,
00016     MENU_OPEN,
00017     MENU_SAVE,
00018     MENU_CLOSE,
00019     MENU_ADD,
00020     MENU_ADDLAST = MENU_ADD + 100,   //These are reserved for utility id's.
00021 };
00022 
00023 BEGIN_EVENT_TABLE(vuMainWindow, wxPARENTWINDOW)
00024     EVT_MENU(MENU_EXIT, vuMainWindow::OnExit)
00025     EVT_MENU(MENU_OPEN, vuMainWindow::OnOpenData)
00026     EVT_MENU(MENU_SAVE, vuMainWindow::OnSaveData)
00027     EVT_MENU(MENU_CLOSE, vuMainWindow::OnCloseData)
00028     EVT_MENU_RANGE(MENU_ADD, MENU_ADDLAST, vuMainWindow::OnAddUtility)
00029 
00030     EVT_SIZE(vuMainWindow::OnSize)
00031     EVT_CLOSE(vuMainWindow::OnClose)
00032 END_EVENT_TABLE()
00033 
00034 //----------------------------------------------------------------------------
00035 //------------------------- public: vuMainWindow() ---------------------------
00036 //----------------------------------------------------------------------------
00037 
00038 vuMainWindow::vuMainWindow()
00039 {
00040     m_Opened = false;
00041     m_Saved = true;
00042     m_Data = 0;
00043 };
00044 
00045 //----------------------------------------------------------------------------
00046 //------------------------- public: ~vuMainWindow() --------------------------
00047 //----------------------------------------------------------------------------
00048 
00049 vuMainWindow::~vuMainWindow()
00050 {
00051     if (isOpened()) close();
00052 };
00053 
00054 //----------------------------------------------------------------------------
00055 //------------------------- public: init() -----------------------------------
00056 //----------------------------------------------------------------------------
00057 
00058 bool vuMainWindow::init(int argc, char* argv[])
00059 {
00060         //Create the main window
00061     if (!initWindow()) return false;
00062     //Put it in the closed state.
00063     setClosed();
00064     //Parse any command line parameters now that the main window is
00065         //initialized.
00066     if (!parseCommandLine(argc,argv)) return false;
00067     //Finally show the main window.
00068     Show(true);
00069     return true;
00070 }
00071 
00072 //----------------------------------------------------------------------------
00073 //------------------------- private: initWindow() ----------------------------
00074 //----------------------------------------------------------------------------
00075 
00076 bool vuMainWindow::initWindow()
00077     //This method is resposible for laying out and initializing the main window
00078 {
00079     //Create the window
00080     bool success = Create(NULL,-1,"vuGUI");
00081     if (!success) return false;
00082 
00083     // Make a menubar
00084     // The file menu...
00085     wxMenu *file_menu = new wxMenu;
00086     file_menu->Append(MENU_OPEN, "&Open...", "Open a volume data file");
00087     //file_menu->Append(MENU_SAVE, "&Save As...", "Save volume data to a file");
00088     file_menu->Append(MENU_CLOSE, "&Close", "Close volume data");
00089     file_menu->AppendSeparator();
00090     file_menu->Append(MENU_EXIT, "&Exit", "Exit the program");
00091 
00092     // Add the menus to the menu bar
00093     wxMenuBar *menu_bar = new wxMenuBar;
00094     menu_bar->Append(file_menu, "&File");
00095     menu_bar->Append(createUtilityMenu(),"&Utility");
00096     SetMenuBar(menu_bar);
00097 
00098     return true;
00099 }
00100 
00101 //----------------------------------------------------------------------------
00102 //------------------------- private: setOpened() -----------------------------
00103 //----------------------------------------------------------------------------
00104 
00105 void vuMainWindow::setOpened()
00106     //This method puts the window into the opened state.
00107 {
00108     //Update the window menu bar
00109     wxMenuBar *menubar = GetMenuBar();
00110     menubar->Enable(MENU_OPEN,false);
00111     //menubar->Enable(MENU_SAVE,true);
00112     menubar->Enable(MENU_CLOSE,true);
00113 
00114     //Create and add a new utility menu based on the opened file
00115     int utilindex = menubar->FindMenu("&Utility");
00116     delete menubar->Replace(utilindex,createUtilityMenu(),"&Utility");
00117 
00118     //Now add the name of the file to the window title.
00119     const char *name=strrchr(m_FileName,'\\');
00120     if (name==NULL) name = strrchr(m_FileName,'/');
00121     if (name==NULL) name = m_FileName;
00122     else name++;
00123     wxString title;
00124     title << "vuGUI - [" << name << "]";
00125     SetTitle(title);
00126 };
00127 
00128 //----------------------------------------------------------------------------
00129 //------------------------- private: setClosed() -----------------------------
00130 //----------------------------------------------------------------------------
00131 
00132 void vuMainWindow::setClosed()
00133     //This method puts the window into the Closed state.
00134 {
00135     //Update the window menu bar
00136     wxMenuBar *menubar = GetMenuBar();
00137     menubar->Enable(MENU_OPEN,true);
00138     //menubar->Enable(MENU_SAVE,false);
00139     menubar->Enable(MENU_CLOSE,false);
00140     menubar->Enable(MENU_ADD,false);
00141     
00142     //Now set the title to the generic name.
00143     SetTitle("vuGUI");
00144 };
00145 
00146 //----------------------------------------------------------------------------
00147 //------------------------- private: createUtilityMenu() ---------------------
00148 //----------------------------------------------------------------------------
00149 
00150 wxMenu* vuMainWindow::createUtilityMenu()
00151 {
00152     wxMenu *utility_menu = new wxMenu;
00153     wxMenu *utility_add_menu = new wxMenu;
00154     
00155     //Find out which utilities are available for the file
00156     wxStringList l_utilities = vuUtilityFactory::listAvailable(m_FileType);
00157     int num = l_utilities.GetCount();
00158     char **utilities = l_utilities.ListToArray();
00159     
00160     //Add them to the submenu
00161     utility_add_menu->Append(MENU_ADD+1, "Add Multiple...", 
00162                              "Add multiple utilities");
00163     utility_add_menu->AppendSeparator();
00164     for (int i= 0; i < num; i++)
00165         utility_add_menu->Append(MENU_ADD+i+2, utilities[i],
00166           "Add the chosen utility");
00167     utility_menu->Append(MENU_ADD, "&Add", utility_add_menu, 
00168                          "Add a utility window");
00169 
00170     return utility_menu;
00171 }
00172 
00173 //----------------------------------------------------------------------------
00174 //------------------------- private: addUtility() ----------------------------
00175 //----------------------------------------------------------------------------
00176 
00177 bool vuMainWindow::addUtility(const char *Name)
00178     //Creates and initializes a utility window of the given name.  The window 
00179     //is opened up and available to the user.  The method returns true if 
00180     //successful, false if not.  (This would happen if the Name does not refer
00181     //to an existing utility).
00182 {
00183         //First check if the program is in the opened state.  This is the only
00184     //state that utility windows can be added in.
00185     if (!isOpened()) return false;
00186 
00187     //Ask the utility factory to create the utility window
00188     vuUtilityWindow *window = vuUtilityFactory::create(Name);
00189     if (window == 0) return false;
00190     
00191     //Initialize the window.
00192     bool success = window->init(this,m_FileName);
00193     
00194     if (success)
00195         //Add the utility to the utility list
00196         m_Windows.Append((wxObject *)window);
00197     else
00198         window->Destroy();
00199     
00200     return success;
00201 }
00202 
00203 //----------------------------------------------------------------------------
00204 //------------------------- private: parseCommandLine() ----------------------
00205 //----------------------------------------------------------------------------
00206 
00207 bool vuMainWindow::parseCommandLine(int argc,char *argv[])
00208 {
00209         //Check if there are any parameters given
00210     if (argc <= 1) return true;
00211 
00212     //Open the volume data.
00213     bool success = open(argv[1]);
00214     if (!success) 
00215     {
00216         cout <<"Error: Could not read the volume data file " <<argv[1]<< ".\n";
00217         return false;
00218     }
00219 
00220     //Now create the desired utility windows.
00221     //First check if the named utilities are available.
00222     for (int i = 2; i < argc; i++)
00223     {
00224         if (!addUtility(argv[i])) 
00225         {
00226             cout <<"Error: Could not initialize the "<<argv[i]<< " utility.\n";
00227             success = false;
00228         }
00229     }
00230     return success;
00231 }
00232 
00233 //----------------------------------------------------------------------------
00234 //------------------------- public: notifyDataChanged() ----------------------
00235 //----------------------------------------------------------------------------
00236 
00237 void vuMainWindow::notifyDataChanged()
00238 {
00239     //Indicate that the data has been changed.
00240     m_Saved = false;
00241     
00242     //Go through all the utility windows and notify them of the change.
00243     for (size_t i = 0; i < m_Windows.GetCount(); i++)
00244         ((vuUtilityWindow *) m_Windows.Item(i)->GetData())->notifyDataChanged();
00245 }
00246 
00247 //----------------------------------------------------------------------------
00248 //------------------------- public: notifyClosed() ---------------------------
00249 //----------------------------------------------------------------------------
00250 
00251 void vuMainWindow::notifyClosed(vuUtilityWindow *window)
00252 {
00253         //Take the window out of the window list.
00254         //Note that wxWindows owns the memory, so we don't delete it ourselves!
00255     m_Windows.DeleteObject((wxObject *)window);
00256 }
00257 
00258 //----------------------------------------------------------------------------
00259 //------------------------- protected: open() --------------------------------
00260 //----------------------------------------------------------------------------
00261 
00262 bool vuMainWindow::open(const char *file)
00263 {
00264     //First make sure that the window is in the closed state.
00265     if (isOpened()) close();
00266 
00267     //Check to see whether the chosen file is useable by the program.
00268     m_FileName = file;
00269     m_FileType = vuFileHelper::getFileType(m_FileName);
00270     cerr << "getFileType=" << m_FileType << endl;
00271     if (m_FileType.IsEmpty()) return false;
00272 
00273     //*********Open the volume data here.************
00274     //********This is not used right now.************
00275 
00276     //Update the internal variables. 
00277     m_Opened = true;
00278     m_Saved = true;
00279 
00280     //Finally update the user interface.
00281     setOpened();
00282 
00283     return true;
00284 }
00285 
00286 //----------------------------------------------------------------------------
00287 //------------------------- protected: close() -------------------------------
00288 //----------------------------------------------------------------------------
00289 
00290 void vuMainWindow::close()
00291 {
00292     //Check if the window is already in the closed state
00293     if (!isOpened()) return;
00294 
00295     //Close all the utility windows
00296     vuUtilityWindow *window;
00297     while (m_Windows.GetCount() > 0)
00298     {
00299         window = (vuUtilityWindow *) m_Windows.GetFirst()->GetData();
00300         window->close();
00301             //Note that wxWindows owns the memory,so we don't delete ourselves!
00302         m_Windows.DeleteObject((wxObject *)window);
00303     };
00304     
00305     //Update the internal variables.
00306     if (m_Data != 0)
00307     {
00308         delete m_Data;
00309         m_Data = 0;
00310     }
00311     m_FileName = "";
00312     m_FileType = "";
00313     m_Opened = false;
00314     m_Saved = true;
00315     
00316     //Finally update the user interface.
00317     setClosed();
00318 }
00319 
00320 //----------------------------------------------------------------------------
00321 //------------------------- protected: isOpened() ----------------------------
00322 //----------------------------------------------------------------------------
00323 
00324 bool vuMainWindow::isOpened()
00325 {
00326     return m_Opened;
00327 }
00328 
00329 //----------------------------------------------------------------------------
00330 //------------------------- protected: save() --------------------------------
00331 //----------------------------------------------------------------------------
00332 
00333 bool vuMainWindow::save(const char *file)
00334 {
00335         //*********Save the volume data here.************
00336         //*********This is not used right now.***********
00337 
00338         //Notify that the data is now saved to file.
00339     m_Saved = true;
00340     return true;
00341 }
00342 
00343 //----------------------------------------------------------------------------
00344 //------------------------- protected: isSaved() -----------------------------
00345 //----------------------------------------------------------------------------
00346 
00347 bool vuMainWindow::isSaved()
00348 {
00349     return m_Saved;
00350 }
00351 
00352 //----------------------------------------------------------------------------
00353 //------------------------- private: saveData() ------------------------------
00354 //----------------------------------------------------------------------------
00355 
00356 void vuMainWindow::saveData()
00357 {
00358         //Ask the user for a file name
00359     wxFileDialog dialog(this,"Save Volume Data","./","","*.*",
00360                         wxSAVE|wxOVERWRITE_PROMPT);
00361     if (dialog.ShowModal() == wxID_OK)
00362     {
00363             //Save the data file
00364         bool success = save(dialog.GetPath());
00365         if (!success)
00366         {
00367                 //If the file could not be saved, tell the user.
00368             wxMessageDialog mdlg(this,"Couldn't save to the chosen file name.",
00369                                  "Error",wxOK|wxICON_EXCLAMATION);
00370             mdlg.ShowModal();
00371         }
00372     }
00373 }
00374 
00375 //----------------------------------------------------------------------------
00376 //------------------------- private: OnOpenData() ----------------------------
00377 //----------------------------------------------------------------------------
00378 
00379 void vuMainWindow::OnOpenData(wxCommandEvent& WXUNUSED(event))
00380 {
00381     //Ask the user for a file name
00382     wxFileDialog dialog(this,"Open Volume Data", wxGetCwd(), "",
00383                         "CG-TUWien Dat File (*.dat)|*.dat|"
00384                         "Volume Data (*.vud)|*.vud|"
00385                         "Lightfield Data (*.vul)|*.vul|"
00386                         "Frequency Data (*.vuf)|*.vuf",
00387                         wxOPEN);
00388 
00389     if (dialog.ShowModal() == wxID_OK)
00390     {
00391         //Open the data file
00392         bool success = open(dialog.GetPath());
00393         if (!success)
00394         {
00395             //If the file could not be read, tell the user.
00396             wxString message;
00397             message << dialog.GetFilename() 
00398                     << " is not a data file recognized by the program.";
00399             wxMessageDialog mdlg(this,message,"Error",wxOK|wxICON_EXCLAMATION);
00400             mdlg.ShowModal();
00401         }
00402     }
00403 }
00404 
00405 //----------------------------------------------------------------------------
00406 //------------------------- private: OnSaveData() ----------------------------
00407 //----------------------------------------------------------------------------
00408 
00409 void vuMainWindow::OnSaveData(wxCommandEvent& WXUNUSED(event))
00410 {
00411     saveData();
00412 }
00413 
00414 //----------------------------------------------------------------------------
00415 //------------------------- private: OnCloseData() ---------------------------
00416 //----------------------------------------------------------------------------
00417 
00418 void vuMainWindow::OnCloseData(wxCommandEvent& WXUNUSED(event))
00419 {
00420         //Check whether the data has been saved first.
00421     if (!isSaved())
00422     {
00423             //If not then offer the user the chance to save it.
00424         wxMessageDialog mdlg(this,
00425         "The volume data has been changed.  Do you want to save the changes?",
00426         "Save Data",wxYES_NO|wxCANCEL|wxICON_EXCLAMATION);
00427 
00428         int answer = mdlg.ShowModal();
00429         if (answer == wxID_YES) saveData();
00430         else if (answer == wxID_CANCEL) return;
00431     }
00432     close();
00433 }
00434 
00435 //----------------------------------------------------------------------------
00436 //------------------------- private: OnAddUtility() --------------------------
00437 //----------------------------------------------------------------------------
00438 
00439 void vuMainWindow::OnAddUtility(wxCommandEvent& event)
00440 {
00441     int id = event.GetId();
00442     wxString name;
00443 
00444     if (id==MENU_ADD)
00445         return; //in case there is an event for the Add submenu popping up
00446     else if (id==MENU_ADD+1)
00447     {
00448         wxStringList slist = vuUtilityFactory::listAvailable(m_FileType);
00449         wxString utilist[slist.GetCount()];
00450         dword e;
00451         for(e=0;e<slist.GetCount();e++)
00452           utilist[e]=slist[e];
00453         
00454         //Bring up a choice dialog.
00455         wxSingleChoiceDialog dialog(this,
00456                                     wxString("Select which utility to add:"),
00457                                     wxString("Add Utility"),
00458                                     (int)slist.GetCount(),
00459                                     utilist);
00460         if (dialog.ShowModal() != wxID_OK) return;
00461         name = dialog.GetStringSelection();
00462     }
00463     else
00464         //Get the name corresponding to the menu choice
00465         name= vuUtilityFactory::listAvailable(m_FileType).ListToArray()[id-(MENU_ADD+2)];
00466 
00467     addUtility(name);
00468 }
00469     
00470 //----------------------------------------------------------------------------
00471 //------------------------- private: OnSize() --------------------------------
00472 //----------------------------------------------------------------------------
00473 
00474 void vuMainWindow::OnSize(wxSizeEvent& WXUNUSED(event))
00475 {
00476     //If we are working with MDI windows, then the client window needs to be
00477     //resized when the main window size is changed.
00478 #ifndef __WXGTK__
00479 
00480         //Get the size of the drawing area.
00481     int w, h;
00482     GetClientSize(&w,&h);
00483         //Set the client window to this size
00484     GetClientWindow()->SetSize(0,0,w,h);
00485 
00486 #endif
00487 }
00488 
00489 //----------------------------------------------------------------------------
00490 //------------------------- private: OnQuit() --------------------------------
00491 //----------------------------------------------------------------------------
00492 
00493 void vuMainWindow::OnExit(wxCommandEvent& WXUNUSED(event))
00494 {
00495     Close();
00496 };
00497 
00498 //----------------------------------------------------------------------------
00499 //------------------------- private: OnClose() -------------------------------
00500 //----------------------------------------------------------------------------
00501 
00502 void vuMainWindow::OnClose(wxCloseEvent& event)
00503 {
00504         //Check whether the data has been saved.
00505     if (event.CanVeto() && !isSaved())
00506     {
00507         wxMessageDialog mdlg(this,"The volume data has been changed.  Do you want to save the changes before exiting?",
00508                              "Save Data",wxYES_NO|wxCANCEL|wxICON_EXCLAMATION);
00509         int answer = mdlg.ShowModal();
00510         if (answer == wxID_YES) saveData();
00511         else if (answer == wxID_CANCEL)
00512         {
00513             event.Veto();
00514             return;
00515         }
00516     }
00517     event.Skip();
00518 }
00519 

Generated on Wed Dec 15 21:20:35 2004 for vuVolume by  doxygen 1.3.9.1