/**** ファイル名 : BFindWindow.h ****/
#ifndef FINDWINDOW #define FINDWINDOW //--------------------------------------------------------------------- #include <Be.h> //--------------------------------------------------------------------- #define FINDWINDOW_TITLE "Find" #define FINDWINDOW_POSITION_LEFT 80 #define FINDWINDOW_POSITION_TOP 80 #define FINDWINDOW_POSITION_WIDTH 352 #define FINDWINDOW_POSITION_HEIGHT 68 #define FINDWINDOW_WINDOWSTYLE (B_TITLED_WINDOW) //--------------------------------------------------------------------- #define MSG_FIND 'mfnd' #define MSG_REPLACEALL 'mrpa' //--------------------------------------------------------------------- class BFindView : public BView { public: BFindView(BRect frame); }; //--------------------------------------------------------------------- class BFindWindow : public BWindow { public: BFindWindow(BRect frame,const char *title); virtual void MessageReceived(BMessage *msg); }; //--------------------------------------------------------------------- #endif
/**** ファイル名 : BFindWindow.h ****/
//--------------------------------------------------------------------- #include "BFindWindow.h" //--------------------------------------------------------------------- BFindWindow::BFindWindow(BRect frame,const char *title) :BWindow(frame,title,FINDWINDOW_WINDOWSTYLE,0) { BFindView *findview=new BFindView(Bounds()); AddChild(findview); SetDefaultButton((BButton *)findview->FindView("btnfind")); findview->FindView("txtfind")->MakeFocus(); } //--------------------------------------------------------------------- void BFindWindow::MessageReceived(BMessage *msg) { switch(msg->what) { case MSG_FIND: case MSG_REPLACEALL: { msg->AddString("findword", ((BTextControl *)FindView("txtfind"))->Text()); msg->AddString("replaceword", ((BTextControl *)FindView("txtrepl"))->Text()); msg->AddBool("ignorecase", (((BCheckBox *)FindView("chkcase"))->Value() ==B_CONTROL_ON)); be_app->PostMessage(msg); PostMessage(B_QUIT_REQUESTED); } break; default: BWindow::MessageReceived(msg); } } //--------------------------------------------------------------------- BFindView::BFindView(BRect frame) :BView(frame,"findview",B_FOLLOW_ALL,B_WILL_DRAW) { BTextControl *txtfind=new BTextControl(BRect(8,8,240,20),"txtfind", "Find","",NULL); BTextControl *txtrepl=new BTextControl(BRect(8,28,240,40),"txtrepl", "Replace","",NULL); BCheckBox *chkcase=new BCheckBox(BRect(8,48,240,60),"chkcase", "Ignore case",NULL); txtfind->SetDivider(40); AddChild(txtfind); txtrepl->SetDivider(40); AddChild(txtrepl); chkcase->SetValue(B_CONTROL_OFF); AddChild(chkcase); AddChild(new BButton(BRect(256,8,336,20),"btnfind","Find", new BMessage(MSG_FIND))); AddChild(new BButton(BRect(256,38,336,58),"btnfind","Replace All", new BMessage(MSG_REPLACEALL))); } //---------------------------------------------------------------------
//--------------------------------------------------------------------- BFindWindow::BFindWindow(BRect frame,const char *title, const BString &findword,const BString &replaceword, const bool ignorecase) :BWindow(frame,title,FINDWINDOW_WINDOWSTYLE,0) { BFindView *findview=new BFindView(Bounds(),findword,replaceword, ignorecase); AddChild(findview); SetDefaultButton((BButton *)findview->FindView("btnfind")); findview->FindView("txtfind")->MakeFocus(); } //--------------------------------------------------------------------- //--------------------------------------------------------------------- BFindView::BFindView(BRect frame, const BString &findword,const BString &replaceword, const bool ignorecase) :BView(frame,"findview",B_FOLLOW_ALL,B_WILL_DRAW) { BTextControl *txtfind=new BTextControl(BRect(8,8,240,20),"txtfind", "Find",findword.String(), NULL); BTextControl *txtrepl=new BTextControl(BRect(8,28,240,40),"txtrepl", "Replace", replaceword.String(),NULL); BCheckBox *chkcase=new BCheckBox(BRect(8,48,240,60),"chkcase", "Ignore case",NULL); txtfind->SetDivider(40); AddChild(txtfind); txtrepl->SetDivider(40); AddChild(txtrepl); chkcase->SetValue(ignorecase?B_CONTROL_ON:B_CONTROL_OFF); AddChild(chkcase); AddChild(new BButton(BRect(256,8,336,20),"btnfind","Find", new BMessage(MSG_FIND))); AddChild(new BButton(BRect(256,38,336,58),"btnfind","Replace All", new BMessage(MSG_REPLACEALL))); } //---------------------------------------------------------------------
/**** ファイル名 : BFindWindow.h ****/
//--------------------------------------------------------------------- BTinyEditorApp::BTinyEditorApp() :BApplication(APPLICATION_SIGNATURE) { EditorList=new BList(); open_filepanel= new BFilePanel(); findword=""; replaceword=""; ignorecase=false; } //---------------------------------------------------------------------
searchmenu->AddSeparatorItem(); searchmenu->AddItem(new BMenuItem("Replace in Selection", new BMessage(MSG_REPLACEINSELECTION)));として、MSG_REPLACEINSELECTIONメッセージを発行するようにしておきます。
/**** ファイル名 : MainWindow.h ****/
//-------------------------------------------------------------------- void BEditorWindow::ReplaceAll(const BString &findword, const BString &replaceword,const bool ignorecase) { BMemoView *memo=(BMemoView *)FindView("memo"); int32 old_start,old_finish; memo->GetSelection(&old_start,&old_finish); ReplaceWords(findword,replaceword,ignorecase, 0,strlen(memo->Text())); memo->Select(old_start,old_finish); } //-------------------------------------------------------------------- void BEditorWindow::ReplaceInSelection(const BString &findword, const BString &replaceword,const bool ignorecase) { BMemoView *memo=(BMemoView *)FindView("memo"); int32 old_start,old_finish; memo->GetSelection(&old_start,&old_finish); ReplaceWords(findword,replaceword,ignorecase, old_start,old_finish); memo->Select(old_finish,old_finish); } //-------------------------------------------------------------------- void BEditorWindow::ReplaceWords(const BString &findword, const BString &replaceword,const bool ignorecase, const int32 start,const int32 finish) { BMemoView *memo=(BMemoView *)FindView("memo"); int32 point; BString wkstr; int32 init_length,wkfinish; wkstr.SetTo(memo->Text()); point=finish; wkfinish=finish; init_length=wkstr.Length(); while(point!=B_ERROR && point>=start) { if(ignorecase) point=wkstr.IFindLast(findword,point); else point=wkstr.FindLast(findword,point); if(point==B_ERROR) { wkstr.SetTo(memo->Text()); wkfinish-=init_length-wkstr.Length(); if(ignorecase) point=wkstr.IFindFirst(findword); else point=wkstr.FindFirst(findword); } if(point!=B_ERROR && point>=start && point<=wkfinish) { memo->Select(point,point+findword.Length()); ReplaceWord(replaceword); } } } //--------------------------------------------------------------------
圧縮ファイル R5 Intel環境で確認 |
BTinyEditor20000612.zip |
ソースファイル | BTinyEditor.cpp |
BTinyEditor.h | |
BFindWindow.cpp | |
BFindWindow.h | |
BMemoView.cpp | |
BMemoView.h | |
main.cpp | |
MainWindow.cpp | |
MainWindow.h |