00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00048 #ifdef _USE_WXWIDGETS_
00049
00050 #include <iostream>
00051 #include "bbtkWxGUITextEditor.h"
00052 #include "bbtkWxBlackBox.h"
00053 #include "bbtkConfigurationFile.h"
00054 #include "bbtkUtilities.h"
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #include "../data/icons/wxart_new.xpm"
00065 #include "../data/icons/wxart_fileopen.xpm"
00066 #include "../data/icons/wxart_filesave.xpm"
00067 #include "../data/icons/wxart_filesaveas.xpm"
00068
00069
00070
00071 #include "../data/icons/wxart_run.xpm"
00072 #include "../data/icons/wxart_runbbi.xpm"
00073 #include "../data/icons/wxart_graphsimple.xpm"
00074 #include "../data/icons/wxart_graphdetail.xpm"
00075 #include "../data/icons/wxart_eldel.xpm"
00076
00077 namespace bbtk
00078 {
00079
00080
00081
00082 class WxTextCtrlGettingKeyEvents : public wxTextCtrl
00083 {
00084 public:
00085 WxTextCtrlGettingKeyEvents(wxWindow *parent, wxWindowID id, const wxString &value,
00086 const wxPoint &pos, const wxSize &size, int style = 0)
00087 : wxTextCtrl(parent, id, value, pos, size, style)
00088 {
00089 }
00090
00091 void SetWxGUITextEditor(WxGUITextEditor* e)
00092 { mWxGUITextEditor = e; }
00093
00094 void OnKeyDown(wxKeyEvent& event);
00095 void OnKeyUp(wxKeyEvent& event);
00096 void OnChar(wxKeyEvent& event);
00097 private :
00098 WxGUITextEditor* mWxGUITextEditor;
00099
00100 DECLARE_EVENT_TABLE()
00101 };
00102
00103 BEGIN_EVENT_TABLE(WxTextCtrlGettingKeyEvents, wxTextCtrl)
00104 EVT_KEY_DOWN(WxTextCtrlGettingKeyEvents::OnKeyDown)
00105 EVT_KEY_UP(WxTextCtrlGettingKeyEvents::OnKeyUp)
00106 EVT_CHAR(WxTextCtrlGettingKeyEvents::OnChar)
00107 END_EVENT_TABLE()
00108
00109
00110 void WxTextCtrlGettingKeyEvents::OnChar(wxKeyEvent& event)
00111 {
00112 event.Skip();
00113 }
00114
00115 void WxTextCtrlGettingKeyEvents::OnKeyUp(wxKeyEvent& event)
00116 {
00117 mWxGUITextEditor->OnKeyUp(event);
00118 event.Skip();
00119 }
00120
00121 void WxTextCtrlGettingKeyEvents::OnKeyDown(wxKeyEvent& event)
00122 {
00123 mWxGUITextEditor->OnKeyDown(event);
00124 event.Skip();
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 WxGUITextEditorPage::WxGUITextEditorPage(wxWindow* parent,
00137 WxGUITextEditor* editor)
00138 : wxPanel(parent,-1),
00139 mEditor(editor),
00140 mName(""),
00141 mAskFilename(true)
00142 {
00143
00144
00145
00146 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
00147
00148 mwxInputText =
00149
00150 new WxTextCtrlGettingKeyEvents(this,
00151 -1,
00152 _T(""),
00153 wxDefaultPosition,
00154 wxDefaultSize,
00155 wxTE_MULTILINE
00156
00157
00158
00159
00160 );
00161 mwxInputText->SetWxGUITextEditor(mEditor);
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 mFixedFont = new wxFont(10,
00174 wxFONTFAMILY_MODERN,
00175 wxFONTSTYLE_NORMAL,
00176 wxFONTWEIGHT_NORMAL,
00177 false);
00178
00179 mwxInputTextAttr = new wxTextAttr;
00180 mwxInputTextAttr->SetFont(*mFixedFont);
00181
00182 sizer->Add(mwxInputText,1,wxGROW);
00183 SetSizer(sizer);
00184 SetAutoLayout(true);
00185 Layout();
00186 }
00187
00188
00189
00190 WxGUITextEditorPage::~WxGUITextEditorPage()
00191 {
00192 delete mwxInputTextAttr;
00193 delete mFixedFont;
00194 }
00195
00196
00197 bool WxGUITextEditorPage::IsModified()
00198 { return mwxInputText->IsModified(); }
00199
00200 std::string WxGUITextEditorPage::GetText()
00201 {
00202 return wx2std(GetTextCtrl()->GetValue());
00203 }
00204
00205
00206 void WxGUITextEditorPage::Load(const std::string& filename)
00207 {
00208
00209
00210
00211 mName = filename;
00212 mAskFilename = false;
00213 mwxInputText->LoadFile(std2wx(mName));
00214 }
00215
00216
00217
00218 void WxGUITextEditorPage::Save(const std::string& filter)
00219 {
00220
00221 if (mAskFilename)
00222 {
00223 wxFileDialog* fd = new wxFileDialog(this,_T("Save file"),_T(""),
00224 _T(""),std2wx(filter),
00225 wxSAVE | wxOVERWRITE_PROMPT );
00226 int result_fd = fd->ShowModal();
00227
00228
00229 fd->SetReturnCode( result_fd );
00230
00231 if (fd->GetReturnCode()==wxID_OK)
00232 {
00233 mName = wx2std(fd->GetPath());
00234 mAskFilename = false;
00235 }
00236 else
00237 {
00238
00239
00240 return;
00241 }
00242 }
00243
00244
00245 SaveFile(mName);
00246 mwxInputText->SetModified(false);
00247 }
00248
00249
00250
00251
00252 void WxGUITextEditorPage::SaveFile(const std::string& filename)
00253 {
00254 mwxInputText->SaveFile( std2wx(filename) );
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 WxGUITextEditor::WxGUITextEditor( wxWindow *parent,
00276 WxGUITextEditorUser* user )
00277 : wxPanel(parent, -1),
00278 mUser(user),
00279 mFileNameFilter("*.*")
00280 {
00281
00282
00283
00284
00285
00286 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
00287
00288 mwxNotebook = new wxNotebook(this,-1,
00289 wxDefaultPosition, wxDefaultSize,
00290 wxNB_TOP
00291
00292
00293 );
00294
00295 sizer->Add(mwxNotebook,1,wxGROW);
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 wxBitmap bmp_new(new_xpm);
00335 wxBitmap bmp_open(fileopen_xpm);
00336 wxBitmap bmp_close(eldel_xpm);
00337 wxBitmap bmp_save(filesave_xpm);
00338 wxBitmap bmp_saveas(filesaveas_xpm);
00339 wxBitmap bmp_run(wxart_run_xpm);
00340 wxBitmap bmp_runbbi(wxart_runbbi_xpm);
00341 wxBitmap bmp_graphsimple(wxart_graphsimple_xpm);
00342 wxBitmap bmp_graphdetail(wxart_graphdetail_xpm);
00343
00344 mwxToolBar = new wxToolBar(this, wxID_ANY,
00345 wxDefaultPosition, wxDefaultSize,
00346 wxTB_FLAT | wxTB_NODIVIDER);
00347
00348 mwxToolBar->AddTool(ID_ButtonNew, _T("New"),
00349 bmp_new, wxNullBitmap, wxITEM_NORMAL,
00350 _T("New file"), _T("Create a new file"));
00351 mwxToolBar->AddTool(ID_ButtonOpen, _T("Open"),
00352 bmp_open, wxNullBitmap, wxITEM_NORMAL,
00353 _T("Open file"), _T("This is help for new file tool"));
00354 mwxToolBar->AddTool(ID_ButtonClose, _T("Close"),
00355 bmp_close, wxNullBitmap, wxITEM_NORMAL,
00356 _T("Close file"), _T("Close current file"));
00357 mwxToolBar->AddTool(ID_ButtonSave, _T("New"),
00358 bmp_save, wxNullBitmap, wxITEM_NORMAL,
00359 _T("Save file"), _T("Save current file"));
00360 mwxToolBar->AddTool(ID_ButtonSaveAs, _T("New"),
00361 bmp_saveas, wxNullBitmap, wxITEM_NORMAL,
00362 _T("Save file as"), _T("Save current file as"));
00363 mwxToolBar->AddTool(ID_ButtonRun, _T("Run"),
00364 bmp_run, wxNullBitmap, wxITEM_NORMAL,
00365 _T("Run file"), _T("Run current file"));
00366
00367
00368 mwxToolBar->AddTool(ID_ButtonGraphSimple, _T("Graph (simple)"),
00369 bmp_graphsimple, wxNullBitmap, wxITEM_NORMAL,
00370 _T("Graph simple"), _T("Simple graph of actual script"));
00371
00372 mwxToolBar->AddTool(ID_ButtonGraphDetail, _T("Graph (detail)"),
00373 bmp_graphdetail, wxNullBitmap, wxITEM_NORMAL,
00374 _T("Graph (detail)"), _T("Detail graph of actual script"));
00375
00376 mwxToolBar->AddTool(ID_ButtonRunBBI, _T("Run external bbi "),
00377 bmp_runbbi, wxNullBitmap, wxITEM_NORMAL,
00378 _T("Run external bbi"), _T("Run external bbi"));
00379
00380
00381
00382 mwxToolBar->AddSeparator();
00383 mwxPosition = new wxStaticText ( mwxToolBar, -1, _T(""));
00384 mwxToolBar->AddControl(mwxPosition);
00385 mwxToolBar->Realize();
00386
00387 sizer->Add(mwxToolBar,0,wxGROW);
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 SetSizer(sizer);
00400
00401
00402
00403 SetAutoLayout(true);
00404 Layout();
00405
00406 NewPage("");
00407 UpdateInfo();
00408
00409 }
00410
00411
00412
00413 WxGUITextEditor::~WxGUITextEditor()
00414 {
00415
00416
00417
00418 }
00419
00420
00421
00422 void WxGUITextEditor::NewPage(const std::string& filename)
00423 {
00424 WxGUITextEditorPage* page =
00425 new WxGUITextEditorPage(mwxNotebook,this);
00426 std::string name("untitled");
00427 if (filename.size()!=0)
00428 {
00429 name = filename;
00430 page->Load(name);
00431 }
00432 page->SetPageName(name);
00433 std::string fname = Utilities::get_file_name(name);
00434 mwxNotebook->AddPage(page,std2wx(fname),true);
00435 FocusOnCurrentPage();
00436 }
00437
00438
00439
00440 WxGUITextEditorPage* WxGUITextEditor::GetCurrentPage()
00441 {
00442 return (WxGUITextEditorPage*)
00443 mwxNotebook->GetPage(mwxNotebook->GetSelection());
00444 }
00445
00446
00447
00448 void WxGUITextEditor::FocusOnCurrentPage()
00449 {
00450 if (mwxNotebook->GetPageCount()==0) return;
00451 GetCurrentPage()->SetFocus();
00452 }
00453
00454
00455
00456 void WxGUITextEditor::OnToolLeftClick(wxCommandEvent& event)
00457 {
00458 switch (event.GetId())
00459 {
00460 case ID_ButtonNew :
00461 New();
00462 FocusOnCurrentPage();
00463 break;
00464 case ID_ButtonOpen :
00465 Open();
00466 FocusOnCurrentPage();
00467 break;
00468 case ID_ButtonClose :
00469 CloseCurrentPage();
00470 break;
00471 case ID_ButtonSave :
00472 SaveCurrentPage();
00473 break;
00474 case ID_ButtonSaveAs :
00475 if (mwxNotebook->GetPageCount()==0) break;
00476 GetCurrentPage()->SetAskFilename(true);
00477 SaveCurrentPage();
00478 break;
00479 case ID_ButtonRun :
00480 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
00481 mUser->WxGUITextEditorRun();
00482 FocusOnCurrentPage();
00483 break;
00484
00485 case ID_ButtonGraphSimple :
00486 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
00487 mUser->WxGUITextEditorGraphSimple();
00488 FocusOnCurrentPage();
00489 break;
00490
00491 case ID_ButtonGraphDetail :
00492 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
00493 mUser->WxGUITextEditorGraphDetail();
00494 FocusOnCurrentPage();
00495 break;
00496
00497 case ID_ButtonRunBBI :
00498 if ((mUser!=0) && (mwxNotebook->GetPageCount()>0))
00499 mUser->WxGUITextEditorRunBBI();
00500 FocusOnCurrentPage();
00501 break;
00502
00503 }
00504 }
00505
00506
00507
00508 void WxGUITextEditor::OnToolRightClick(wxCommandEvent& event)
00509 {
00510 }
00511
00512
00513
00514 void WxGUITextEditor::New()
00515 {
00516 NewPage("");
00517 UpdateInfo();
00518 }
00519
00520
00521
00522 void WxGUITextEditor::Open()
00523 {
00524
00525
00526 wxFileDialog* fd = new wxFileDialog(this,_T("Open file"),_T(""),
00527 _T(""),std2wx(mFileNameFilter),
00528 wxOPEN | wxFILE_MUST_EXIST );
00529 int result_fd = fd->ShowModal();
00530
00531
00532 fd->SetReturnCode( result_fd );
00533
00534 if (fd->GetReturnCode()==wxID_OK)
00535 {
00536 std::string filename = wx2std(fd->GetPath());
00537 std::cout << "file [" << filename << "]" <<std::endl;
00538 Open(filename);
00539 }
00540 else
00541 {
00542
00543 }
00544 }
00545
00546
00547
00548 void WxGUITextEditor::Open(const std::string& filename)
00549 {
00550 NewPage(filename);
00551 UpdateInfo();
00552 }
00553
00554
00555
00556
00557 bool WxGUITextEditor::CloseCurrentPage()
00558 {
00559 if (mwxNotebook->GetPageCount()==0) return true;
00560
00561 if (GetCurrentPage()->IsModified())
00562 {
00563 wxString mess = std2wx(GetCurrentPage()->GetPageName());
00564 mess += _T(" modified. Save it ?");
00565 wxMessageDialog* d =
00566 new wxMessageDialog(this,
00567 mess,
00568 _T("Save buffer"),
00569 wxYES_NO | wxCANCEL | wxICON_QUESTION);
00570 switch (d->ShowModal())
00571 {
00572 case wxID_CANCEL :
00573 return false;
00574 break;
00575 case wxID_YES :
00576 GetCurrentPage()->Save(mFileNameFilter);
00577 break;
00578 case wxID_NO : ;
00579 }
00580 }
00581 mwxNotebook->DeletePage(mwxNotebook->GetSelection());
00582 FocusOnCurrentPage();
00583 return true;
00584 }
00585
00586
00587
00588 bool WxGUITextEditor::CloseAllPages()
00589 {
00590 bool ok = true;
00591 while (mwxNotebook->GetPageCount()!=0)
00592 {
00593 if (!CloseCurrentPage())
00594 {
00595 ok = false;
00596 break;
00597 }
00598 }
00599 return ok;
00600 }
00601
00602
00603
00604 void WxGUITextEditor::SaveCurrentPage()
00605 {
00606 if (mwxNotebook->GetPageCount()==0) return;
00607 GetCurrentPage()->Save(mFileNameFilter);
00608 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),
00609 std2wx(GetCurrentPage()->GetPageName()));
00610 }
00611
00612
00613
00614
00615 void WxGUITextEditor::OnPageClose(wxAuiNotebookEvent& evt)
00616 {
00617 if (!CloseCurrentPage()) evt.Veto();
00618 }
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 void WxGUITextEditor::UpdateInfo()
00673 {
00674 if (mwxNotebook->GetPageCount()==0) return;
00675 WxTextCtrlGettingKeyEvents* text = GetCurrentPage()->GetTextCtrl();
00676
00677 long line, column, pos;
00678 pos = text->GetInsertionPoint();
00679 text->PositionToXY(pos, &column, &line);
00680
00681
00682
00683
00684
00685 char mess[200];
00686 sprintf(mess,"%ld:%ld/%ld:%ld",
00687 line+1,column+1,
00688 (long)text->GetNumberOfLines(),
00689 (long)text->GetLineLength(line)+1);
00690
00691
00692
00693
00694 mwxPosition->SetLabel(std2wx(mess));
00695 mwxPosition->Show();
00696
00697 if (text->IsModified())
00698 {
00699 std::string title("*");
00700 title += GetCurrentPage()->GetPageName();
00701 mwxNotebook->SetPageText(mwxNotebook->GetSelection(),std2wx(title));
00702 }
00703 }
00704
00705
00706 void WxGUITextEditor::OnKeyUp(wxKeyEvent& event)
00707 {
00708
00709 UpdateInfo();
00710 }
00711
00712
00713
00714 void WxGUITextEditor::OnKeyDown(wxKeyEvent& event)
00715 {
00716
00717
00718 if ( event.ControlDown() )
00719 {
00720 switch (event.GetKeyCode())
00721 {
00722 case 'n': case 'N' : New(); break;
00723 case 's': case 'S' : SaveCurrentPage(); break;
00724 case 'o': case 'O' : Open(); break;
00725
00726
00727 }
00728 }
00729 }
00730
00731
00732
00733 BEGIN_EVENT_TABLE(WxGUITextEditor, wxPanel)
00734 EVT_MENU(wxID_ANY, WxGUITextEditor::OnToolLeftClick)
00735 EVT_TOOL_RCLICKED(wxID_ANY, WxGUITextEditor::OnToolRightClick)
00736 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, WxGUITextEditor::OnPageClose)
00737 END_EVENT_TABLE()
00738
00739
00740
00741
00742
00743
00744
00745 WxGUITextEditorWindow::WxGUITextEditorWindow( wxWindow *parent,
00746 wxString title,
00747 wxSize size)
00748 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
00749 {
00750
00751
00752 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
00753
00754 mEditor = new WxGUITextEditor(this);
00755 sizer->Add(mEditor,1,wxGROW);
00756
00757
00758
00759
00760 SetSizer(sizer);
00761
00762
00763
00764 Wx::SetTopWindow(this);
00765
00766
00767
00768
00769 SetAutoLayout(true);
00770 Layout();
00771 }
00772
00773
00774
00775 WxGUITextEditorWindow::~WxGUITextEditorWindow()
00776 {
00777 }
00778
00779
00780
00781 }
00782
00783
00784 #endif //_USE_WXWIDGETS_