1 /* 2 * Copyright 2002-2006, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Mattias Sundblad 7 * Andrew Bachmann 8 */ 9 10 11 #include "Constants.h" 12 #include "FindWindow.h" 13 14 #include <Button.h> 15 #include <Catalog.h> 16 #include <CheckBox.h> 17 #include <GroupLayoutBuilder.h> 18 #include <GridLayoutBuilder.h> 19 #include <Locale.h> 20 #include <LayoutBuilder.h> 21 #include <String.h> 22 #include <TextControl.h> 23 24 25 #undef TR_CONTEXT 26 #define TR_CONTEXT "FindandReplaceWindow" 27 28 FindWindow::FindWindow(BRect frame, BHandler* _handler, BString* searchString, 29 bool caseState, bool wrapState, bool backState) 30 : BWindow(frame, "FindWindow", B_MODAL_WINDOW, 31 B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS, 32 B_CURRENT_WORKSPACE) 33 { 34 AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); 35 36 fSearchString = new BTextControl("", TR("Find:"), NULL, NULL); 37 fCaseSensBox = new BCheckBox("", TR("Case-sensitive"), NULL); 38 fWrapBox = new BCheckBox("", TR("Wrap-around search"), NULL); 39 fBackSearchBox = new BCheckBox("", TR("Search backwards"), NULL); 40 fCancelButton = new BButton("", TR("Cancel"), new BMessage(B_QUIT_REQUESTED)); 41 fSearchButton = new BButton("", TR("Find"), new BMessage(MSG_SEARCH)); 42 43 SetLayout(new BGroupLayout(B_HORIZONTAL)); 44 AddChild(BGroupLayoutBuilder(B_VERTICAL, 4) 45 .Add(BGridLayoutBuilder(6, 2) 46 .Add(fSearchString->CreateLabelLayoutItem(), 0, 0) 47 .Add(fSearchString->CreateTextViewLayoutItem(), 1, 0) 48 .Add(fCaseSensBox, 1, 1) 49 .Add(fWrapBox, 1, 2) 50 .Add(fBackSearchBox, 1, 3) 51 ) 52 .AddGroup(B_HORIZONTAL, 10) 53 .AddGlue() 54 .Add(fCancelButton) 55 .Add(fSearchButton) 56 .End() 57 .SetInsets(10, 10, 10, 10) 58 ); 59 60 fSearchButton->MakeDefault(true); 61 fHandler = _handler; 62 63 const char* text = searchString->String(); 64 65 fSearchString->SetText(text); 66 fSearchString->MakeFocus(true); 67 68 fCaseSensBox->SetValue(caseState ? B_CONTROL_ON : B_CONTROL_OFF); 69 fWrapBox->SetValue(wrapState ? B_CONTROL_ON : B_CONTROL_OFF); 70 fBackSearchBox->SetValue(backState ? B_CONTROL_ON : B_CONTROL_OFF); 71 } 72 73 74 void 75 FindWindow::MessageReceived(BMessage* msg) 76 { 77 switch (msg->what) { 78 case B_QUIT_REQUESTED: 79 Quit(); 80 break; 81 case MSG_SEARCH: 82 _SendMessage(); 83 break; 84 85 default: 86 BWindow::MessageReceived(msg); 87 break; 88 } 89 } 90 91 92 void 93 FindWindow::DispatchMessage(BMessage* message, BHandler* handler) 94 { 95 if (message->what == B_KEY_DOWN) { 96 int8 key; 97 if (message->FindInt8("byte", 0, &key) == B_OK) { 98 if (key == B_ESCAPE) { 99 message->MakeEmpty(); 100 message->what = B_QUIT_REQUESTED; 101 } 102 } 103 } 104 105 BWindow::DispatchMessage(message, handler); 106 } 107 108 109 void 110 FindWindow::_SendMessage() 111 { 112 BMessage message(MSG_SEARCH); 113 114 // Add the string 115 message.AddString("findtext", fSearchString->Text()); 116 117 // Add searchparameters from checkboxes 118 message.AddBool("casesens", fCaseSensBox->Value() == B_CONTROL_ON); 119 message.AddBool("wrap", fWrapBox->Value() == B_CONTROL_ON); 120 message.AddBool("backsearch", fBackSearchBox->Value() == B_CONTROL_ON); 121 122 fHandler->Looper()->PostMessage(&message, fHandler); 123 124 PostMessage(B_QUIT_REQUESTED); 125 } 126 127 128