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