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 23 CONNECTION 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 30 registered trademarks of Be Incorporated in the United States and other 31 countries. Other brand product names are registered trademarks or trademarks 32 of their respective holders. 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 45 #include <Application.h> 46 #include <Box.h> 47 #include <Button.h> 48 #include <Catalog.h> 49 #include <Locale.h> 50 #include <String.h> 51 #include <TextView.h> 52 53 54 #define B_TRANSLATE_CONTEXT "Mail" 55 56 57 enum { 58 M_FIND_STRING_CHANGED = 'fsch' 59 }; 60 61 62 #define FINDBUTTON 'find' 63 64 static BString sPreviousFind = ""; 65 66 FindWindow* FindWindow::fFindWindow = NULL; 67 BRect FindWindow::fLastPosition(BRect(100, 300, 300, 374)); 68 69 void FindWindow::DoFind(BWindow* window, const char* text) 70 { 71 if (window == NULL) { 72 long i = 0; 73 while ((window = be_app->WindowAt(i++)) != NULL) { 74 // Send the text to a waiting window 75 if (window != fFindWindow) 76 if (dynamic_cast<TMailWindow*>(window) != NULL) 77 break; // Found a window 78 } 79 } 80 81 /* ask that window who is in the front */ 82 window = dynamic_cast<TMailWindow*>(window)->FrontmostWindow(); 83 if (window == NULL) 84 return; 85 86 // Found a window, send a find message 87 88 if (!window->Lock()) 89 return; 90 BView* focus = window->FindView("m_content"); 91 window->Unlock(); 92 93 if (focus) 94 { 95 BMessage msg(M_FIND); 96 msg.AddString("findthis",text); 97 window->PostMessage(&msg, focus); 98 } 99 } 100 101 102 FindPanel::FindPanel(BRect rect) 103 : 104 BBox(rect, "FindPanel", B_FOLLOW_LEFT_RIGHT, B_WILL_DRAW) 105 { 106 BRect r = Bounds().InsetByCopy(10, 10); 107 108 fTextControl = new BTextControl(r, "BTextControl", NULL, 109 sPreviousFind.String(), new BMessage(M_FIND_STRING_CHANGED), 110 B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP); 111 112 fTextControl->SetText(sPreviousFind.String()); 113 fTextControl->MakeFocus(); 114 AddChild(fTextControl); 115 116 fFindButton = new BButton(BRect(0, 0, 90, 20),"FINDBUTTON", 117 B_TRANSLATE("Find"), 118 new BMessage(FINDBUTTON),B_FOLLOW_LEFT | B_FOLLOW_BOTTOM); 119 fFindButton->ResizeToPreferred(); 120 AddChild(fFindButton); 121 r = fFindButton->Bounds(); 122 123 fFindButton->MoveTo(Bounds().right - r.Width() - 8, 124 Bounds().bottom - r.Height() - 8); 125 fFindButton->SetEnabled(sPreviousFind.Length()); 126 } 127 128 129 FindPanel::~FindPanel() 130 { 131 sPreviousFind = fTextControl->Text(); 132 } 133 134 135 void FindPanel::AttachedToWindow() 136 { 137 BView::AttachedToWindow(); 138 SetViewColor(216, 216, 216); 139 Window()->SetDefaultButton(fFindButton); 140 fFindButton->SetTarget(this); 141 142 fTextControl->SetTarget(this); 143 fTextControl->ResizeToPreferred(); 144 fTextControl->ResizeTo(Bounds().Width() - 20, 145 fTextControl->Frame().Height()); 146 147 fTextControl->MakeFocus(true); 148 fTextControl->TextView()->SelectAll(); 149 } 150 151 152 void FindPanel::MouseDown(BPoint point) 153 { 154 Window()->Activate(); 155 BView::MouseDown(point); 156 } 157 158 159 void FindPanel::Draw(BRect) 160 { 161 // TextBevel(*this, mBTextView->Frame()); 162 } 163 164 165 void FindPanel::KeyDown(const char*, int32) 166 { 167 int32 length = fTextControl->TextView()->TextLength(); 168 bool enabled = fFindButton->IsEnabled(); 169 170 if (length > 0 && !enabled) 171 fFindButton->SetEnabled(true); 172 else if (length == 0 && enabled) 173 fFindButton->SetEnabled(false); 174 } 175 176 177 void FindPanel::MessageReceived(BMessage* msg) 178 { 179 switch (msg->what) { 180 case M_FIND_STRING_CHANGED: { 181 if (strlen(fTextControl->Text()) == 0) 182 fFindButton->SetEnabled(false); 183 else 184 fFindButton->SetEnabled(true); 185 break; 186 } 187 case FINDBUTTON: { 188 Find(); 189 Window()->PostMessage(B_QUIT_REQUESTED); 190 break; 191 } 192 default: 193 BView::MessageReceived(msg); 194 } 195 } 196 197 198 void FindPanel::Find() 199 { 200 fTextControl->TextView()->SelectAll(); 201 const char* text = fTextControl->Text(); 202 if (text == NULL || text[0] == 0) return; 203 204 BWindow* window = NULL; 205 long i = 0; 206 while ((bool)(window = be_app->WindowAt(i++))) { 207 // Send the text to a waiting window 208 if (window != FindWindow::fFindWindow) 209 break; // Found a window 210 } 211 212 if (window) 213 FindWindow::DoFind(window, text); 214 } 215 216 217 FindWindow::FindWindow() 218 : BWindow(FindWindow::fLastPosition, B_TRANSLATE("Find"), 219 B_FLOATING_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE 220 | B_WILL_ACCEPT_FIRST_CLICK | B_CLOSE_ON_ESCAPE) 221 { 222 fFindPanel = new FindPanel(Bounds()); 223 AddChild(fFindPanel); 224 fFindWindow = this; 225 Show(); 226 } 227 228 229 FindWindow::~FindWindow() 230 { 231 FindWindow::fLastPosition = Frame(); 232 fFindWindow = NULL; 233 } 234 235 236 void FindWindow::Find(BWindow* window) 237 { 238 // eliminate unused parameter warning 239 (void)window; 240 241 if (fFindWindow == NULL) { 242 fFindWindow = new FindWindow(); 243 } else 244 fFindWindow->Activate(); 245 } 246 247 248 void FindWindow::FindAgain(BWindow* window) 249 { 250 if (fFindWindow) { 251 fFindWindow->Lock(); 252 fFindWindow->fFindPanel->Find(); 253 fFindWindow->Unlock(); 254 } else if (sPreviousFind.Length() != 0) 255 DoFind(window, sPreviousFind.String()); 256 else 257 Find(window); 258 } 259 260 261 void FindWindow::SetFindString(const char* string) 262 { 263 sPreviousFind = string; 264 } 265 266 267 const char* FindWindow::GetFindString() 268 { 269 return sPreviousFind.String(); 270 } 271 272