1 /* 2 * Copyright 2007, Haiku, Inc. 3 * Copyright 2003-2004 Kian Duffy, myob@users.sourceforge.net 4 * Parts Copyright 1998-1999 Kazuho Okui and Takashi Murai. 5 * All rights reserved. Distributed under the terms of the MIT license. 6 */ 7 8 9 #include "FindWindow.h" 10 11 #include <Box.h> 12 #include <Button.h> 13 #include <CheckBox.h> 14 #include <RadioButton.h> 15 #include <String.h> 16 #include <TextControl.h> 17 18 19 const uint32 MSG_FIND_HIDE = 'Fhid'; 20 21 22 FindWindow::FindWindow (BRect frame, BMessenger messenger , BString &str, 23 bool findSelection, bool matchWord, bool matchCase, bool forwardSearch) 24 : 25 BWindow(frame, "Find", B_FLOATING_WINDOW, 26 B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_CLOSE_ON_ESCAPE), 27 fFindDlgMessenger(messenger) 28 { 29 AddShortcut((ulong)'W', (ulong)B_COMMAND_KEY, new BMessage(MSG_FIND_HIDE)); 30 31 //Build up view 32 fFindView = new BView(Bounds(), "FindView", B_FOLLOW_ALL, B_WILL_DRAW); 33 fFindView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 34 AddChild(fFindView); 35 36 font_height height; 37 fFindView->GetFontHeight(&height); 38 float lineHeight = height.ascent + height.descent + height.leading; 39 40 //These labels are from the bottom up 41 float buttonsTop = frame.Height() - 19 - lineHeight; 42 float matchWordBottom = buttonsTop - 4; 43 float matchWordTop = matchWordBottom - lineHeight - 8; 44 float matchCaseBottom = matchWordTop - 4; 45 float matchCaseTop = matchCaseBottom - lineHeight - 8; 46 float forwardSearchBottom = matchCaseTop - 4; 47 float forwardSearchTop = forwardSearchBottom - lineHeight - 8; 48 49 //These things are calculated from the top 50 float textRadioTop = 12; 51 float textRadioBottom = textRadioTop + 2 + lineHeight + 2 + 1; 52 float textRadioRight = fFindView->StringWidth("Use Text: ") + 30; 53 float selectionRadioTop = textRadioBottom + 4; 54 float selectionRadioBottom = selectionRadioTop + lineHeight + 8; 55 56 //Divider 57 float dividerHeight = (selectionRadioBottom + forwardSearchTop) / 2; 58 59 //Button Coordinates 60 float searchButtonLeft = (frame.Width() - fFindView->StringWidth("Find") - 60) / 2; 61 float searchButtonRight = searchButtonLeft + fFindView->StringWidth("Find") + 60; 62 63 //Build the Views 64 fTextRadio = new BRadioButton(BRect(14, textRadioTop, textRadioRight, textRadioBottom), 65 "fTextRadio", "Use Text: ", NULL); 66 fFindView->AddChild(fTextRadio); 67 68 fFindLabel = new BTextControl(BRect(textRadioRight + 4, textRadioTop, frame.Width() - 14, textRadioBottom), 69 "fFindLabel", "", "", NULL); 70 fFindLabel->SetDivider(0); 71 fFindView->AddChild(fFindLabel); 72 if (!findSelection) 73 fFindLabel->SetText(str.String()); 74 fFindLabel->MakeFocus(true); 75 76 fSelectionRadio = new BRadioButton(BRect(14, selectionRadioTop, frame.Width() - 14, selectionRadioBottom), 77 "fSelectionRadio", "Use Selection", NULL); 78 fFindView->AddChild(fSelectionRadio); 79 80 if (findSelection) 81 fSelectionRadio->SetValue(B_CONTROL_ON); 82 else 83 fTextRadio->SetValue(B_CONTROL_ON); 84 85 fSeparator = new BBox(BRect(6, dividerHeight, frame.Width() - 6, dividerHeight + 1)); 86 fFindView->AddChild(fSeparator); 87 88 fForwardSearchBox = new BCheckBox(BRect(14, forwardSearchTop, frame.Width() - 14, forwardSearchBottom), 89 "fForwardSearchBox", "Search Forward", NULL); 90 fFindView->AddChild(fForwardSearchBox); 91 if (forwardSearch) 92 fForwardSearchBox->SetValue(B_CONTROL_ON); 93 94 fMatchCaseBox = new BCheckBox(BRect(14, matchCaseTop, frame.Width() - 14, matchCaseBottom), 95 "fMatchCaseBox", "Match Case", NULL); 96 fFindView->AddChild(fMatchCaseBox); 97 if (matchCase) 98 fMatchCaseBox->SetValue(B_CONTROL_ON); 99 100 fMatchWordBox = new BCheckBox(BRect(14, matchWordTop, frame.Width() - 14, matchWordBottom), 101 "fMatchWordBox", "Match Word", NULL); 102 fFindView->AddChild(fMatchWordBox); 103 if (matchWord) 104 fMatchWordBox->SetValue(B_CONTROL_ON); 105 106 fFindButton = new BButton(BRect(searchButtonLeft, buttonsTop, searchButtonRight, frame.Height() - 14), 107 "fFindButton", "Find", new BMessage(MSG_FIND)); 108 fFindButton->MakeDefault(true); 109 fFindView->AddChild(fFindButton); 110 111 Show(); 112 } 113 114 115 FindWindow::~FindWindow() 116 { 117 } 118 119 120 void 121 FindWindow::MessageReceived(BMessage *msg) 122 { 123 switch (msg->what) { 124 case B_QUIT_REQUESTED: 125 Quit(); 126 break; 127 128 case MSG_FIND: 129 _SendFindMessage(); 130 break; 131 132 case MSG_FIND_HIDE: 133 Quit(); 134 break; 135 136 default: 137 BWindow::MessageReceived(msg); 138 break; 139 } 140 } 141 142 143 void 144 FindWindow::Quit() 145 { 146 fFindDlgMessenger.SendMessage(MSG_FIND_CLOSED); 147 BWindow::Quit(); 148 } 149 150 151 void 152 FindWindow::_SendFindMessage() 153 { 154 BMessage message(MSG_FIND); 155 156 if (fTextRadio->Value() == B_CONTROL_ON) { 157 message.AddString("findstring", fFindLabel->Text()); 158 message.AddBool("findselection", false); 159 } else 160 message.AddBool("findselection", true); 161 162 //Add the other parameters 163 message.AddBool("usetext", fTextRadio->Value() == B_CONTROL_ON); 164 message.AddBool("forwardsearch", fForwardSearchBox->Value() == B_CONTROL_ON); 165 message.AddBool("matchcase", fMatchCaseBox->Value() == B_CONTROL_ON); 166 message.AddBool("matchword", fMatchWordBox->Value() == B_CONTROL_ON); 167 168 fFindDlgMessenger.SendMessage(&message); 169 } 170