xref: /haiku/src/apps/stylededit/FindWindow.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 #include <Button.h>
2 #include <CheckBox.h>
3 #include <String.h>
4 #include <TextControl.h>
5 #include <Window.h>
6 #include <Box.h>
7 #include "Constants.h"
8 #include "FindWindow.h"
9 
10 // FindWindow::FindWindow()
11 FindWindow::FindWindow(BRect frame, BHandler *_handler, BString *searchString, bool *caseState, bool *wrapState, bool *backState)
12 			: BWindow(frame," ", B_MODAL_WINDOW, B_NOT_RESIZABLE|B_ASYNCHRONOUS_CONTROLS, B_CURRENT_WORKSPACE) {
13 	AddChild(fFindView=new BBox(BRect(-1,-1,Bounds().Width()+1.5,Bounds().Height()+1.5),"",B_FOLLOW_ALL_SIDES,B_WILL_DRAW));
14 	fFindView->SetViewColor(221,222,221);
15 
16 	font_height height;
17 	fFindView->GetFontHeight(&height);
18 	float lineHeight = height.ascent+height.descent+height.leading;
19 
20 	float findWidth = fFindView->StringWidth("Find:")+6;
21 
22 	float searchBottom = 12 + 2 + lineHeight + 2 + 1;
23 	float buttonTop = frame.Height()-19-lineHeight;
24 	float wrapBoxTop = (buttonTop+searchBottom-lineHeight)/2;
25 	float wrapBoxBottom = (buttonTop+searchBottom+lineHeight)/2;
26 	float caseBoxTop = (searchBottom+wrapBoxTop-lineHeight)/2;
27 	float caseBoxBottom = (searchBottom+wrapBoxTop+lineHeight)/2;
28 	float backBoxTop = (buttonTop+wrapBoxBottom-lineHeight)/2;
29 	float backBoxBottom = (buttonTop+wrapBoxBottom+lineHeight)/2;
30 
31 	fFindView->AddChild(fSearchString= new BTextControl(BRect(14,12,frame.Width()-10,searchBottom), "", "Find:", NULL, NULL));
32 		fSearchString->SetDivider(findWidth);
33 
34 	fFindView->AddChild(fCaseSensBox= new BCheckBox(BRect(16+findWidth,caseBoxTop,frame.Width()-12,caseBoxBottom),"","Case-sensitive", NULL));
35 	fFindView->AddChild(fWrapBox= new BCheckBox(BRect(16+findWidth,wrapBoxTop,frame.Width()-12,wrapBoxBottom),"","Wrap-around search", NULL));
36 	fFindView->AddChild(fBackSearchBox= new BCheckBox(BRect(16+findWidth,backBoxTop,frame.Width()-12,backBoxBottom),"","Search backwards", NULL));
37 
38 	fFindView->AddChild(fCancelButton= new BButton(BRect(142,buttonTop,212,frame.Height()-7),"","Cancel",new BMessage(B_QUIT_REQUESTED)));
39 	fFindView->AddChild(fSearchButton= new BButton(BRect(221,buttonTop,291,frame.Height()-7),"","Find",new BMessage(MSG_SEARCH)));
40 
41 	fSearchButton->MakeDefault(true);
42 	fHandler=_handler;
43 
44 	const char *text= searchString->String();
45 
46     fSearchString->SetText(text);
47 	fSearchString-> MakeFocus(true); //021021
48 
49 	if(*caseState== true)
50 		fCaseSensBox->SetValue(B_CONTROL_ON);
51 	else
52 		fCaseSensBox->SetValue(B_CONTROL_OFF);
53 
54 	if(*wrapState== true)
55 		fWrapBox->SetValue(B_CONTROL_ON);
56 	else
57 		fWrapBox->SetValue(B_CONTROL_OFF);
58 
59 	if(*backState== true)
60 		fBackSearchBox->SetValue(B_CONTROL_ON);
61 	else
62 		fBackSearchBox->SetValue(B_CONTROL_OFF);
63 
64 	Show();
65 }
66 
67 void FindWindow::MessageReceived(BMessage *msg){
68 	switch(msg->what){
69 		case B_QUIT_REQUESTED:
70 			Quit();
71 		break;
72 		case MSG_SEARCH:
73 			ExtractToMsg(new BMessage(MSG_SEARCH));
74 		break;
75 	default:
76 		BWindow::MessageReceived(msg);
77 		break;
78 	}
79 }
80 
81 void FindWindow::DispatchMessage(BMessage *message, BHandler *handler)
82 
83 {
84 	int8	key;
85 
86 	if ( message->what == B_KEY_DOWN ) {
87 		status_t result;
88 		result= message->FindInt8("byte", 0, &key);
89 
90 		if (result== B_OK){
91 			if (key== B_ESCAPE){
92 				message-> MakeEmpty();
93 				message-> what= B_QUIT_REQUESTED;
94 			}
95 		}
96 	}
97 	BWindow::DispatchMessage(message,handler);
98 }
99 
100 void FindWindow::ExtractToMsg(BMessage *message){
101 
102 	int32	caseBoxState;
103 	int32 	wrapBoxState;
104 	int32 	backBoxState;
105 
106 	caseBoxState= fCaseSensBox-> Value();
107 	wrapBoxState= fWrapBox-> Value();
108 	backBoxState= fBackSearchBox-> Value();
109 
110 	bool 	caseSens;
111 	bool 	wrapIt;
112 	bool 	backSearch;
113 
114 	if(caseBoxState== B_CONTROL_ON)
115 		caseSens=true;
116 	else
117 		caseSens= false;
118 
119 	if(wrapBoxState== B_CONTROL_ON)
120 		wrapIt= true;
121 	else
122 		wrapIt= false;
123 
124 	if(backBoxState== B_CONTROL_ON)
125 		backSearch= true;
126 	else
127 		backSearch= false;
128 
129 	//Add the string
130 	message->AddString("findtext",fSearchString->Text());
131 
132 	//Add searchparameters from checkboxes
133 	message->AddBool("casesens", caseSens);
134 	message->AddBool("wrap", wrapIt);
135 	message->AddBool("backsearch", backSearch);
136 
137 	fHandler->Looper()->PostMessage(message,fHandler);
138 	delete(message);
139 	PostMessage(B_QUIT_REQUESTED);
140 }
141 
142 
143