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