xref: /haiku/src/apps/stylededit/ReplaceWindow.cpp (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
1 #include <Box.h>
2 #include <Button.h>
3 #include <CheckBox.h>
4 #include <String.h>
5 #include <TextControl.h>
6 #include <Window.h>
7 
8 #include <Constants.h>
9 #include <ReplaceWindow.h>
10 
11 ReplaceWindow::ReplaceWindow(BRect frame, BHandler *_handler, BString *searchString, BString *replaceString, bool *caseState, bool *wrapState, bool *backState)
12 				: BWindow(frame, "", B_MODAL_WINDOW, B_NOT_RESIZABLE,B_CURRENT_WORKSPACE)
13 			{
14 	AddChild(fReplaceView=new BBox(BRect(-1,-1,Bounds().Width()+1.5,Bounds().Height()+1.5),"",B_FOLLOW_ALL_SIDES,B_WILL_DRAW));
15 	fReplaceView->SetViewColor(216,216,216);
16 
17 	char * findLabel = "Find:";
18 	float findWidth = fReplaceView->StringWidth(findLabel);
19 	fReplaceView->AddChild (fSearchString= new BTextControl(BRect(5,10,290,50), "", findLabel,NULL, NULL,
20 		B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW|B_NAVIGABLE));
21 
22 	char * replaceWithLabel = "Replace with:";
23 	float replaceWithWidth = fReplaceView->StringWidth(replaceWithLabel);
24 	fReplaceView->AddChild(fReplaceString=new BTextControl(BRect(5,35,290,50), "", replaceWithLabel,NULL,
25 		 NULL,B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW|B_NAVIGABLE));
26 	float maxWidth = (replaceWithWidth > findWidth ? replaceWithWidth : findWidth) + 1;
27 	fSearchString->SetDivider(maxWidth);
28 	fReplaceString->SetDivider(maxWidth);
29 
30 	fReplaceView->AddChild(fCaseSensBox=new BCheckBox(BRect(maxWidth+8,60,290,52),"","Case-sensitive", NULL,
31 		B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW|B_NAVIGABLE));
32 	fReplaceView->AddChild(fWrapBox=new BCheckBox(BRect(maxWidth+8,80,290,70),"","Wrap-around search", NULL,
33 		B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW|B_NAVIGABLE));
34 	fReplaceView->AddChild(fBackSearchBox=new BCheckBox(BRect(maxWidth+8,100,290,95),"","Search backwards", NULL,
35 		B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW|B_NAVIGABLE));
36 	fReplaceView->AddChild(fAllWindowsBox=new BCheckBox(BRect(maxWidth+8,120,290,95),"","Replace in all windows",
37 		new BMessage(CHANGE_WINDOW) ,B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW|B_NAVIGABLE));
38 		fUichange=false;
39 
40 	//vertical separator at 110 in StyledEdit
41 	fReplaceView->AddChild(fReplaceAllButton=new BButton(BRect(10,150,98,166),"","Replace All",new BMessage(MSG_REPLACE_ALL),
42 		B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW|B_NAVIGABLE));
43 	fReplaceView->AddChild(fCancelButton=new BButton(BRect(141,150,211,166),"","Cancel",new BMessage(B_QUIT_REQUESTED),
44 		B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW|B_NAVIGABLE));
45 	fReplaceView->AddChild(fReplaceButton=new BButton(BRect(221,150,291,166),"","Replace",new BMessage(MSG_REPLACE),
46 		B_FOLLOW_LEFT|B_FOLLOW_TOP,B_WILL_DRAW|B_NAVIGABLE));
47 	fReplaceButton->MakeDefault(true);
48 
49 	fHandler= _handler;
50 
51 	const char *searchtext= searchString->String();
52 	const char *replacetext= replaceString->String();
53 
54 	fSearchString->SetText(searchtext);
55 	fReplaceString->SetText(replacetext);
56 	fSearchString-> MakeFocus(true); //021021
57 
58 	if(*caseState== true)
59 		fCaseSensBox->SetValue(B_CONTROL_ON);
60 	else
61 		fCaseSensBox->SetValue(B_CONTROL_OFF);
62 
63 	if(*wrapState== true)
64 		fWrapBox->SetValue(B_CONTROL_ON);
65 	else
66 		fWrapBox->SetValue(B_CONTROL_OFF);
67 
68 	if(*backState== true)
69 		fBackSearchBox->SetValue(B_CONTROL_ON);
70 	else
71 		fBackSearchBox->SetValue(B_CONTROL_OFF);
72 
73 	Show();
74 }
75 
76 void ReplaceWindow::MessageReceived(BMessage *msg){
77 	switch(msg->what){
78 		case B_QUIT_REQUESTED:
79 			Quit();
80 		break;
81 		case MSG_REPLACE:
82 			ExtractToMsg(new BMessage(MSG_REPLACE));
83 		break;
84 		case CHANGE_WINDOW:
85 			ChangeUi();
86 		break;
87 		case MSG_REPLACE_ALL:
88 			ExtractToMsg(new BMessage(MSG_REPLACE_ALL));
89 		break;
90 	default:
91 		BWindow::MessageReceived(msg);
92 		break;
93 	}
94 }
95 
96 void ReplaceWindow::ChangeUi(){
97 
98 	if(!fUichange){
99 		fReplaceAllButton->MakeDefault(true);
100 		fReplaceButton->SetEnabled(false);
101 		fWrapBox->SetValue(B_CONTROL_ON);
102 		fWrapBox->SetEnabled(false);
103 		fBackSearchBox->SetEnabled(false);
104 		fUichange=true;
105 		}
106 	else{
107 		fReplaceButton->MakeDefault(true);
108 		fReplaceButton->SetEnabled(true);
109 		fReplaceAllButton->SetEnabled(true);
110 		fWrapBox->SetValue(B_CONTROL_OFF);
111 		fWrapBox->SetEnabled(true);
112 		fBackSearchBox->SetEnabled(true);
113 		fUichange=false;
114 		}
115 
116 }/***ReplaceWindow::ChangeUi()***/
117 
118 void ReplaceWindow::DispatchMessage(BMessage *message, BHandler *fHandler){
119 	int8	key;
120 	if ( message->what == B_KEY_DOWN ) {
121 		status_t result;
122 		result= message-> FindInt8("byte", 0, &key);
123 
124 		if (result== B_OK) {
125 			if(key== B_ESCAPE){
126 				message-> MakeEmpty();
127 				message-> what= B_QUIT_REQUESTED;
128 			}
129 		}
130 	}
131 	BWindow::DispatchMessage(message,fHandler);
132 }
133 
134 void ReplaceWindow::ExtractToMsg(BMessage *message ){
135 
136 	int32	caseBoxState;
137 	int32 	wrapBoxState;
138 	int32 	backBoxState;
139 	int32	allWinBoxState;
140 
141 	caseBoxState= fCaseSensBox-> Value();
142 	wrapBoxState= fWrapBox-> Value();
143 	backBoxState= fBackSearchBox-> Value();
144 	allWinBoxState= fAllWindowsBox-> Value();
145 
146 	bool 	caseSens;
147 	bool 	wrapIt;
148 	bool 	backSearch;
149 	bool	allWindows;
150 
151 	if(caseBoxState== B_CONTROL_ON)
152 		caseSens= true;
153 	else
154 		caseSens= false;
155 
156 	if(wrapBoxState== B_CONTROL_ON)
157 		wrapIt= true;
158 	else
159 		wrapIt= false;
160 
161 	if(backBoxState== B_CONTROL_ON)
162 		backSearch= true;
163 	else
164 		backSearch= false;
165 
166 	if(allWinBoxState== B_CONTROL_ON)
167 		allWindows= true;
168 	else
169 		allWindows= false;
170 
171 	message->AddString("FindText",fSearchString->Text());
172 	message->AddString("ReplaceText",fReplaceString->Text());
173 	message->AddBool("casesens", caseSens);
174 	message->AddBool("wrap", wrapIt);
175 	message->AddBool("backsearch", backSearch);
176 	message->AddBool("allwindows", allWindows);
177 
178 	fHandler->Looper()->PostMessage(message,fHandler);
179 	delete(message);
180 	PostMessage(B_QUIT_REQUESTED);
181 }
182 
183 
184 
185 
186 
187