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 "ReplaceWindow.h" 13 14 #include <Button.h> 15 #include <CheckBox.h> 16 #include <String.h> 17 #include <TextControl.h> 18 #include <Window.h> 19 20 21 ReplaceWindow::ReplaceWindow(BRect frame, BHandler *_handler, BString *searchString, 22 BString *replaceString, bool *caseState, bool *wrapState, bool *backState) 23 : BWindow(frame, "ReplaceWindow", B_MODAL_WINDOW, 24 B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS, 25 B_CURRENT_WORKSPACE) 26 { 27 fReplaceView = new BView(Bounds(), "ReplaceView", B_FOLLOW_ALL, B_WILL_DRAW); 28 fReplaceView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 29 AddChild(fReplaceView); 30 31 char* findLabel = "Find:"; 32 float findWidth = fReplaceView->StringWidth(findLabel); 33 fReplaceView->AddChild(fSearchString = new BTextControl(BRect(5, 10, 290, 50), "", 34 findLabel, NULL, NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE)); 35 36 char* replaceWithLabel = "Replace with:"; 37 float replaceWithWidth = fReplaceView->StringWidth(replaceWithLabel); 38 fReplaceView->AddChild(fReplaceString = new BTextControl(BRect(5, 35, 290, 50), "", 39 replaceWithLabel, NULL, NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, 40 B_WILL_DRAW | B_NAVIGABLE)); 41 float maxWidth = (replaceWithWidth > findWidth ? replaceWithWidth : findWidth) + 1; 42 fSearchString->SetDivider(maxWidth); 43 fReplaceString->SetDivider(maxWidth); 44 45 fReplaceView->AddChild(fCaseSensBox = new BCheckBox(BRect(maxWidth + 8, 60, 290, 52), 46 "", "Case-sensitive", NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE)); 47 fReplaceView->AddChild(fWrapBox = new BCheckBox(BRect(maxWidth + 8, 80, 290, 70), 48 "", "Wrap-around search", NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, 49 B_WILL_DRAW | B_NAVIGABLE)); 50 fReplaceView->AddChild(fBackSearchBox = new BCheckBox(BRect(maxWidth + 8, 100, 290, 95), 51 "", "Search backwards", NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, 52 B_WILL_DRAW | B_NAVIGABLE)); 53 fReplaceView->AddChild(fAllWindowsBox = new BCheckBox(BRect(maxWidth + 8, 120, 290, 95), 54 "", "Replace in all windows", new BMessage(CHANGE_WINDOW), 55 B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE)); 56 fUIchange = false; 57 58 fReplaceView->AddChild(fReplaceAllButton = new BButton(BRect(10, 150, 98, 166), 59 "", "Replace All", new BMessage(MSG_REPLACE_ALL), B_FOLLOW_LEFT | B_FOLLOW_TOP, 60 B_WILL_DRAW | B_NAVIGABLE)); 61 fReplaceView->AddChild(fCancelButton = new BButton(BRect(141, 150, 211, 166), 62 "", "Cancel", new BMessage(B_QUIT_REQUESTED), B_FOLLOW_LEFT | B_FOLLOW_TOP, 63 B_WILL_DRAW | B_NAVIGABLE)); 64 fReplaceView->AddChild(fReplaceButton = new BButton(BRect(221, 150, 291, 166), 65 "", "Replace", new BMessage(MSG_REPLACE), B_FOLLOW_LEFT | B_FOLLOW_TOP, 66 B_WILL_DRAW | B_NAVIGABLE)); 67 fReplaceButton->MakeDefault(true); 68 69 fHandler = _handler; 70 71 const char *searchtext = searchString->String(); 72 const char *replacetext = replaceString->String(); 73 74 fSearchString->SetText(searchtext); 75 fReplaceString->SetText(replacetext); 76 fSearchString->MakeFocus(true); 77 78 fCaseSensBox->SetValue(*caseState ? B_CONTROL_ON : B_CONTROL_OFF); 79 fWrapBox->SetValue(*wrapState ? B_CONTROL_ON : B_CONTROL_OFF); 80 fBackSearchBox->SetValue(*backState ? B_CONTROL_ON : B_CONTROL_OFF); 81 } 82 83 84 void 85 ReplaceWindow::MessageReceived(BMessage *msg) 86 { 87 switch (msg->what){ 88 case MSG_REPLACE: 89 _SendMessage(MSG_REPLACE); 90 break; 91 case CHANGE_WINDOW: 92 _ChangeUI(); 93 break; 94 case MSG_REPLACE_ALL: 95 _SendMessage(MSG_REPLACE_ALL); 96 break; 97 98 default: 99 BWindow::MessageReceived(msg); 100 break; 101 } 102 } 103 104 105 void 106 ReplaceWindow::_ChangeUI() 107 { 108 if (!fUIchange) { 109 fReplaceAllButton->MakeDefault(true); 110 fReplaceButton->SetEnabled(false); 111 fWrapBox->SetValue(B_CONTROL_ON); 112 fWrapBox->SetEnabled(false); 113 fBackSearchBox->SetEnabled(false); 114 fUIchange = true; 115 } else { 116 fReplaceButton->MakeDefault(true); 117 fReplaceButton->SetEnabled(true); 118 fReplaceAllButton->SetEnabled(true); 119 fWrapBox->SetValue(B_CONTROL_OFF); 120 fWrapBox->SetEnabled(true); 121 fBackSearchBox->SetEnabled(true); 122 fUIchange = false; 123 } 124 } 125 126 127 void 128 ReplaceWindow::DispatchMessage(BMessage *message, BHandler *handler) 129 { 130 if (message->what == B_KEY_DOWN) { 131 int8 key; 132 if (message->FindInt8("byte", 0, &key) == B_OK) { 133 if (key == B_ESCAPE) { 134 message->MakeEmpty(); 135 message->what = B_QUIT_REQUESTED; 136 } 137 } 138 } 139 140 BWindow::DispatchMessage(message, handler); 141 } 142 143 144 void 145 ReplaceWindow::_SendMessage(uint32 what) 146 { 147 BMessage message(what); 148 149 // Add the strings 150 message.AddString("FindText", fSearchString->Text()); 151 message.AddString("ReplaceText", fReplaceString->Text()); 152 153 // Add searchparameters from checkboxes 154 message.AddBool("casesens", fCaseSensBox->Value() == B_CONTROL_ON); 155 message.AddBool("wrap", fWrapBox->Value() == B_CONTROL_ON); 156 message.AddBool("backsearch", fBackSearchBox->Value() == B_CONTROL_ON); 157 message.AddBool("allwindows", fAllWindowsBox->Value() == B_CONTROL_ON); 158 159 fHandler->Looper()->PostMessage(&message, fHandler); 160 161 PostMessage(B_QUIT_REQUESTED); 162 } 163 164