xref: /haiku/src/apps/mail/FindWindow.cpp (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 BeMail(TM), Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 // ===========================================================================
36 //	FindWindow.cpp
37 // 	Copyright 1996 by Peter Barrett, All rights reserved.
38 // ===========================================================================
39 
40 #include "FindWindow.h"
41 #include "MailApp.h"
42 #include "MailWindow.h"
43 #include "Messages.h"
44 #include "AutoTextControl.h"
45 
46 #include <TextView.h>
47 #include <Button.h>
48 #include <Application.h>
49 #include <String.h>
50 #include <Box.h>
51 
52 #include <MDRLanguage.h>
53 
54 enum {
55 	M_FIND_STRING_CHANGED = 'fsch'
56 };
57 
58 void TextBevel(BView& view, BRect r);
59 
60 // ============================================================================
61 void TextBevel(BView& view, BRect r)
62 {
63 	r.InsetBy(-1,-1);
64 	view.SetHighColor(96,96,96);
65 	view.MovePenTo(r.left,r.bottom);
66 	view.StrokeLine(BPoint(r.left,r.top));
67 	view.StrokeLine(BPoint(r.right,r.top));
68 	view.SetHighColor(216,216,216);
69 	view.StrokeLine(BPoint(r.right,r.bottom));
70 	view.StrokeLine(BPoint(r.left,r.bottom));
71 	r.InsetBy(-1,-1);
72 	view.SetHighColor(192,192,192);
73 	view.MovePenTo(r.left,r.bottom);
74 	view.StrokeLine(BPoint(r.left,r.top));
75 	view.StrokeLine(BPoint(r.right,r.top));
76 	view.SetHighColor(255,255,255);
77 	view.StrokeLine(BPoint(r.right,r.bottom));
78 	view.StrokeLine(BPoint(r.left,r.bottom));
79 	view.SetHighColor(0,0,0);
80 }
81 
82 //	FindWindow is modeless...
83 
84 #define FINDBUTTON 'find'
85 
86 static BString sPreviousFind = "";
87 
88 FindWindow* FindWindow::mFindWindow = NULL;
89 BRect FindWindow::mLastPosition(BRect(100,300,300,374));
90 
91 void FindWindow::DoFind(BWindow *window, const char *text)
92 {
93 	if (window == NULL) {
94 		long i=0;
95 		while ((bool)(window = be_app->WindowAt(i++))) {	// Send the text to a waiting window
96 			if (window != mFindWindow)
97 				if (dynamic_cast<TMailWindow *>(window) != NULL)
98 					break;	// Found a window
99 		}
100 	}
101 
102 	/* ask that window who is in the front */
103 	window = dynamic_cast<TMailWindow *>(window)->FrontmostWindow();
104 	if (window == NULL)
105 		return;
106 
107 //	Found a window, send a find message
108 
109 	if (!window->Lock())
110 		return;
111 	BView *focus = window->FindView("m_content");
112 	window->Unlock();
113 
114 	if (focus)
115 	{
116 		BMessage msg(M_FIND);
117 		msg.AddString("findthis",text);
118 		window->PostMessage(&msg, focus);
119 	}
120 }
121 
122 FindPanel::FindPanel(BRect rect)
123 	: BBox(rect, "FindPanel", B_FOLLOW_LEFT_RIGHT,
124 		B_WILL_DRAW)
125 {
126 	BRect r = Bounds().InsetByCopy(10,10);
127 
128 	mBTextControl = new AutoTextControl(r,"BTextControl",NULL,sPreviousFind.String(),
129 									new BMessage(M_FIND_STRING_CHANGED),
130 									B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
131 	mBTextControl->SetText(sPreviousFind.String());
132 	mBTextControl->MakeFocus();
133 	mBTextControl->SetEscapeCancel(true);
134 	AddChild(mBTextControl);
135 
136 	mFindButton = new BButton(BRect(0,0,90,20),"FINDBUTTON",
137 		MDR_DIALECT_CHOICE ("Find","検索"),
138 		new BMessage(FINDBUTTON),B_FOLLOW_LEFT | B_FOLLOW_BOTTOM);
139 	mFindButton->ResizeToPreferred();
140 	AddChild(mFindButton);
141 	r = mFindButton->Bounds();
142 
143 	mFindButton->MoveTo(Bounds().right - r.Width() - 8,
144 		Bounds().bottom - r.Height() - 8);
145 	mFindButton->SetEnabled(sPreviousFind.Length());
146 }
147 
148 FindPanel::~FindPanel()
149 {
150 	sPreviousFind = mBTextControl->Text();
151 }
152 
153 void FindPanel::AttachedToWindow()
154 {
155 	BView::AttachedToWindow();
156 	SetViewColor(216,216,216);
157 	Window()->SetDefaultButton(mFindButton);
158 	mFindButton->SetTarget(this);
159 
160 	mBTextControl->SetTarget(this);
161 	mBTextControl->ResizeToPreferred();
162 	mBTextControl->ResizeTo(Bounds().Width() - 20, mBTextControl->Frame().Height());
163 
164 	mBTextControl->MakeFocus(true);
165 	mBTextControl->TextView()->SelectAll();
166 }
167 
168 void FindPanel::MouseDown(BPoint point)
169 {
170 	Window()->Activate();
171 	BView::MouseDown(point);
172 }
173 
174 void FindPanel::Draw(BRect)
175 {
176 //	TextBevel(*this,mBTextView->Frame());
177 }
178 
179 void FindPanel::KeyDown(const char *, int32)
180 {
181 	int32 length = mBTextControl->TextView()->TextLength();
182 	bool enabled = mFindButton->IsEnabled();
183 
184 	if (length > 0 && !enabled)
185 		mFindButton->SetEnabled(true);
186 	else if (length == 0 && enabled)
187 		mFindButton->SetEnabled(false);
188 }
189 
190 void FindPanel::MessageReceived(BMessage *msg)
191 {
192 	switch (msg->what) {
193 		case M_FIND_STRING_CHANGED: {
194 			if (strlen(mBTextControl->Text()) == 0)
195 				mFindButton->SetEnabled(false);
196 			else
197 				mFindButton->SetEnabled(true);
198 			break;
199 		}
200 		case FINDBUTTON: {
201 			Find();
202 			Window()->PostMessage(B_QUIT_REQUESTED);
203 			break;
204 		}
205 		default:
206 			BView::MessageReceived(msg);
207 	}
208 }
209 
210 void FindPanel::Find()
211 {
212 	mBTextControl->TextView()->SelectAll();
213 	const char *text = mBTextControl->Text();
214 	if (text == NULL || text[0] == 0) return;
215 
216 	BWindow *window = NULL;
217 	long i=0;
218 	while ((bool)(window = be_app->WindowAt(i++))) {	// Send the text to a waiting window
219 		if (window != FindWindow::mFindWindow)
220 			break;	// Found a window
221 	}
222 
223 	if (window)
224 		FindWindow::DoFind(window, text);
225 }
226 
227 // ============================================================================
228 
229 
230 FindWindow::FindWindow()
231 	: BWindow(FindWindow::mLastPosition,
232 		MDR_DIALECT_CHOICE ("Find","検索"),
233 		B_FLOATING_WINDOW,
234 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_WILL_ACCEPT_FIRST_CLICK)
235 {
236 	mFindPanel = new FindPanel(Bounds());
237 	AddChild(mFindPanel);
238 	mFindWindow = this;
239 	Show();
240 }
241 
242 FindWindow::~FindWindow()
243 {
244 	FindWindow::mLastPosition = Frame();
245 	mFindWindow = NULL;
246 }
247 
248 void FindWindow::Find(BWindow *window)
249 {
250 	// eliminate unused parameter warning
251 	(void)window;
252 
253 	if (mFindWindow == NULL) {
254 		mFindWindow = new FindWindow();
255 	} else
256 		mFindWindow->Activate();
257 }
258 
259 void FindWindow::FindAgain(BWindow *window)
260 {
261 	if (mFindWindow) {
262 		mFindWindow->Lock();
263 		mFindWindow->mFindPanel->Find();
264 		mFindWindow->Unlock();
265 	} else if (sPreviousFind.Length() != 0)
266 		DoFind(window, sPreviousFind.String());
267 	else
268 		Find(window);
269 }
270 
271 void FindWindow::SetFindString(const char *string)
272 {
273 	sPreviousFind = string;
274 }
275 
276 const char *FindWindow::GetFindString()
277 {
278 	return sPreviousFind.String();
279 }
280