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
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,
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
00036
00037
00038 vuMainWindow::vuMainWindow()
00039 {
00040 m_Opened = false;
00041 m_Saved = true;
00042 m_Data = 0;
00043 };
00044
00045
00046
00047
00048
00049 vuMainWindow::~vuMainWindow()
00050 {
00051 if (isOpened()) close();
00052 };
00053
00054
00055
00056
00057
00058 bool vuMainWindow::init(int argc, char* argv[])
00059 {
00060
00061 if (!initWindow()) return false;
00062
00063 setClosed();
00064
00065
00066 if (!parseCommandLine(argc,argv)) return false;
00067
00068 Show(true);
00069 return true;
00070 }
00071
00072
00073
00074
00075
00076 bool vuMainWindow::initWindow()
00077
00078 {
00079
00080 bool success = Create(NULL,-1,"vuGUI");
00081 if (!success) return false;
00082
00083
00084
00085 wxMenu *file_menu = new wxMenu;
00086 file_menu->Append(MENU_OPEN, "&Open...", "Open a volume data file");
00087
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
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
00103
00104
00105 void vuMainWindow::setOpened()
00106
00107 {
00108
00109 wxMenuBar *menubar = GetMenuBar();
00110 menubar->Enable(MENU_OPEN,false);
00111
00112 menubar->Enable(MENU_CLOSE,true);
00113
00114
00115 int utilindex = menubar->FindMenu("&Utility");
00116 delete menubar->Replace(utilindex,createUtilityMenu(),"&Utility");
00117
00118
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
00130
00131
00132 void vuMainWindow::setClosed()
00133
00134 {
00135
00136 wxMenuBar *menubar = GetMenuBar();
00137 menubar->Enable(MENU_OPEN,true);
00138
00139 menubar->Enable(MENU_CLOSE,false);
00140 menubar->Enable(MENU_ADD,false);
00141
00142
00143 SetTitle("vuGUI");
00144 };
00145
00146
00147
00148
00149
00150 wxMenu* vuMainWindow::createUtilityMenu()
00151 {
00152 wxMenu *utility_menu = new wxMenu;
00153 wxMenu *utility_add_menu = new wxMenu;
00154
00155
00156 wxStringList l_utilities = vuUtilityFactory::listAvailable(m_FileType);
00157 int num = l_utilities.GetCount();
00158 char **utilities = l_utilities.ListToArray();
00159
00160
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
00175
00176
00177 bool vuMainWindow::addUtility(const char *Name)
00178
00179
00180
00181
00182 {
00183
00184
00185 if (!isOpened()) return false;
00186
00187
00188 vuUtilityWindow *window = vuUtilityFactory::create(Name);
00189 if (window == 0) return false;
00190
00191
00192 bool success = window->init(this,m_FileName);
00193
00194 if (success)
00195
00196 m_Windows.Append((wxObject *)window);
00197 else
00198 window->Destroy();
00199
00200 return success;
00201 }
00202
00203
00204
00205
00206
00207 bool vuMainWindow::parseCommandLine(int argc,char *argv[])
00208 {
00209
00210 if (argc <= 1) return true;
00211
00212
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
00221
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
00235
00236
00237 void vuMainWindow::notifyDataChanged()
00238 {
00239
00240 m_Saved = false;
00241
00242
00243 for (size_t i = 0; i < m_Windows.GetCount(); i++)
00244 ((vuUtilityWindow *) m_Windows.Item(i)->GetData())->notifyDataChanged();
00245 }
00246
00247
00248
00249
00250
00251 void vuMainWindow::notifyClosed(vuUtilityWindow *window)
00252 {
00253
00254
00255 m_Windows.DeleteObject((wxObject *)window);
00256 }
00257
00258
00259
00260
00261
00262 bool vuMainWindow::open(const char *file)
00263 {
00264
00265 if (isOpened()) close();
00266
00267
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
00274
00275
00276
00277 m_Opened = true;
00278 m_Saved = true;
00279
00280
00281 setOpened();
00282
00283 return true;
00284 }
00285
00286
00287
00288
00289
00290 void vuMainWindow::close()
00291 {
00292
00293 if (!isOpened()) return;
00294
00295
00296 vuUtilityWindow *window;
00297 while (m_Windows.GetCount() > 0)
00298 {
00299 window = (vuUtilityWindow *) m_Windows.GetFirst()->GetData();
00300 window->close();
00301
00302 m_Windows.DeleteObject((wxObject *)window);
00303 };
00304
00305
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
00317 setClosed();
00318 }
00319
00320
00321
00322
00323
00324 bool vuMainWindow::isOpened()
00325 {
00326 return m_Opened;
00327 }
00328
00329
00330
00331
00332
00333 bool vuMainWindow::save(const char *file)
00334 {
00335
00336
00337
00338
00339 m_Saved = true;
00340 return true;
00341 }
00342
00343
00344
00345
00346
00347 bool vuMainWindow::isSaved()
00348 {
00349 return m_Saved;
00350 }
00351
00352
00353
00354
00355
00356 void vuMainWindow::saveData()
00357 {
00358
00359 wxFileDialog dialog(this,"Save Volume Data","./","","*.*",
00360 wxSAVE|wxOVERWRITE_PROMPT);
00361 if (dialog.ShowModal() == wxID_OK)
00362 {
00363
00364 bool success = save(dialog.GetPath());
00365 if (!success)
00366 {
00367
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
00377
00378
00379 void vuMainWindow::OnOpenData(wxCommandEvent& WXUNUSED(event))
00380 {
00381
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
00392 bool success = open(dialog.GetPath());
00393 if (!success)
00394 {
00395
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
00407
00408
00409 void vuMainWindow::OnSaveData(wxCommandEvent& WXUNUSED(event))
00410 {
00411 saveData();
00412 }
00413
00414
00415
00416
00417
00418 void vuMainWindow::OnCloseData(wxCommandEvent& WXUNUSED(event))
00419 {
00420
00421 if (!isSaved())
00422 {
00423
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
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;
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
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
00465 name= vuUtilityFactory::listAvailable(m_FileType).ListToArray()[id-(MENU_ADD+2)];
00466
00467 addUtility(name);
00468 }
00469
00470
00471
00472
00473
00474 void vuMainWindow::OnSize(wxSizeEvent& WXUNUSED(event))
00475 {
00476
00477
00478 #ifndef __WXGTK__
00479
00480
00481 int w, h;
00482 GetClientSize(&w,&h);
00483
00484 GetClientWindow()->SetSize(0,0,w,h);
00485
00486 #endif
00487 }
00488
00489
00490
00491
00492
00493 void vuMainWindow::OnExit(wxCommandEvent& WXUNUSED(event))
00494 {
00495 Close();
00496 };
00497
00498
00499
00500
00501
00502 void vuMainWindow::OnClose(wxCloseEvent& event)
00503 {
00504
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