1/* ----------------------------------------------------------------------- 2 * Copyright (c) 2004 Waldemar Kornewald, Waldemar.Kornewald@web.de 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 * DEALINGS IN THE SOFTWARE. 21 * ----------------------------------------------------------------------- */ 22 23#include "TextRequestDialog.h" 24#include "InterfaceUtils.h" 25 26#include <Button.h> 27#include <TextControl.h> 28#include <TextView.h> 29 30 31// GUI constants 32static const uint32 kWindowWidth = 250; 33static const uint32 kWindowHeight = 10 + 20 + 10 + 25 + 5; 34static const BRect kWindowRect(0, 0, kWindowWidth, kWindowHeight); 35static const uint32 kDefaultButtonWidth = 80; 36 37// message constants 38static const uint32 kMsgButton = 'MBTN'; 39static const uint32 kMsgUpdateControls = 'UCTL'; 40 41// labels 42static const char *kLabelOK = "OK"; 43static const char *kLabelCancel = "Cancel"; 44 45 46TextRequestDialog::TextRequestDialog(const char *title, const char *information, 47 const char *request, const char *text) 48 : BWindow(kWindowRect, title, B_MODAL_WINDOW, B_NOT_RESIZABLE | B_NOT_CLOSABLE, 0), 49 fInvoker(NULL) 50{ 51 BRect rect = Bounds(); 52 BView *backgroundView = new BView(rect, "background", B_FOLLOW_ALL_SIDES, 0); 53 backgroundView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 54 rect.InsetBy(5, 5); 55 rect.bottom = rect.top; 56 // init 57 58 if(information) { 59 BRect textRect(rect); 60 textRect.OffsetTo(0, 0); 61 fTextView = new BTextView(rect, "TextView", textRect, B_FOLLOW_NONE, 62 B_WILL_DRAW); 63 fTextView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 64 fTextView->MakeSelectable(false); 65 fTextView->MakeEditable(false); 66 fTextView->SetText(information); 67 float textHeight = fTextView->TextHeight(0, fTextView->CountLines()); 68 backgroundView->ResizeBy(0, textHeight + 5); 69 ResizeBy(0, textHeight + 5); 70 fTextView->ResizeBy(0, textHeight - textRect.Height()); 71 rect.bottom += textHeight + 5; 72 backgroundView->AddChild(fTextView); 73 } else 74 fTextView = NULL; 75 76 rect.top = rect.bottom + 5; 77 rect.bottom = rect.top + 20; 78 fTextControl = new BTextControl(rect, "request", request, text, NULL); 79 fTextControl->SetModificationMessage(new BMessage(kMsgUpdateControls)); 80 fTextControl->SetDivider(fTextControl->StringWidth(fTextControl->Label()) + 5); 81 if(text && strlen(text) > 0) 82 fTextControl->TextView()->SelectAll(); 83 84 rect.top = rect.bottom + 10; 85 rect.bottom = rect.top + 25; 86 rect.left = rect.right - kDefaultButtonWidth; 87 BMessage message(kMsgButton); 88 message.AddInt32("which", 1); 89 fOKButton = new BButton(rect, "okButton", kLabelOK, new BMessage(message)); 90 rect.right = rect.left - 10; 91 rect.left = rect.right - kDefaultButtonWidth; 92 message.ReplaceInt32("which", 0); 93 BButton *cancelButton = new BButton(rect, "cancelButton", kLabelCancel, 94 new BMessage(message)); 95 backgroundView->AddChild(cancelButton); 96 backgroundView->AddChild(fOKButton); 97 backgroundView->AddChild(fTextControl); 98 AddChild(backgroundView); 99 100 fTextControl->MakeFocus(true); 101 SetDefaultButton(fOKButton); 102 103 UpdateControls(); 104} 105 106 107TextRequestDialog::~TextRequestDialog() 108{ 109 delete fInvoker; 110} 111 112 113void 114TextRequestDialog::MessageReceived(BMessage *message) 115{ 116 switch(message->what) { 117 case kMsgButton: { 118 if(!fInvoker || !fInvoker->Message()) 119 return; 120 121 int32 which; 122 message->FindInt32("which", &which); 123 BMessage toSend(*fInvoker->Message()); 124 toSend.AddInt32("which", which); 125 if(which == 1) 126 toSend.AddString("text", fTextControl->Text()); 127 128 fInvoker->Invoke(&toSend); 129 PostMessage(B_QUIT_REQUESTED); 130 } break; 131 132 case kMsgUpdateControls: 133 UpdateControls(); 134 break; 135 136 default: 137 BWindow::MessageReceived(message); 138 } 139} 140 141 142bool 143TextRequestDialog::QuitRequested() 144{ 145 return true; 146} 147 148 149status_t 150TextRequestDialog::Go(BInvoker *invoker) 151{ 152 fInvoker = invoker; 153 MoveTo(center_on_screen(Bounds())); 154 Show(); 155 156 return B_OK; 157} 158 159 160void 161TextRequestDialog::UpdateControls() 162{ 163 fOKButton->SetEnabled(fTextControl->TextView()->TextLength() > 0); 164} 165