1 /* 2 * Copyright 2006, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan Aßmus <superstippi@gmx.de> 7 */ 8 9 #include <stdio.h> 10 11 #include <Button.h> 12 #include <GroupLayoutBuilder.h> 13 #include <SpaceLayoutItem.h> 14 #include <Screen.h> 15 #include <TextControl.h> 16 17 #include "NamePanel.h" 18 19 enum { 20 MSG_PANEL_OK, 21 MSG_PANEL_CANCEL, 22 }; 23 24 // constructor 25 NamePanel::NamePanel(const char* label, 26 const char* text, 27 BWindow *window, 28 BHandler* target, 29 BMessage* message, 30 BRect frame) 31 : Panel(frame, "Name Panel", 32 B_MODAL_WINDOW_LOOK, B_MODAL_SUBSET_WINDOW_FEEL, 33 B_ASYNCHRONOUS_CONTROLS | B_NOT_V_RESIZABLE), 34 fWindow(window), 35 fTarget(target), 36 fMessage(message) 37 { 38 BButton* defaultButton = new BButton("Ok", new BMessage(MSG_PANEL_OK)); 39 BButton* cancelButton = new BButton("Cancel", new BMessage(MSG_PANEL_CANCEL)); 40 fNameTC = new BTextControl(label, text, NULL); 41 42 BView* topView = BGroupLayoutBuilder(B_VERTICAL, 10) 43 .AddGlue() 44 45 // controls 46 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 5) 47 .Add(BSpaceLayoutItem::CreateHorizontalStrut(5)) 48 49 // text control 50 .Add(fNameTC->CreateLabelLayoutItem()) 51 .Add(fNameTC->CreateTextViewLayoutItem()) 52 53 .Add(BSpaceLayoutItem::CreateHorizontalStrut(5)) 54 ) 55 56 .AddGlue() 57 58 // buttons 59 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 5) 60 .Add(BSpaceLayoutItem::CreateGlue()) 61 .Add(cancelButton) 62 .Add(defaultButton) 63 .Add(BSpaceLayoutItem::CreateHorizontalStrut(5)) 64 ) 65 66 .AddGlue() 67 ; 68 69 SetLayout(new BGroupLayout(B_HORIZONTAL)); 70 AddChild(topView); 71 72 SetDefaultButton(defaultButton); 73 fNameTC->MakeFocus(true); 74 75 if (fWindow && fWindow->Lock()) { 76 fSavedTargetWindowFeel = fWindow->Feel(); 77 if (fSavedTargetWindowFeel != B_NORMAL_WINDOW_FEEL) 78 fWindow->SetFeel(B_NORMAL_WINDOW_FEEL); 79 fWindow->Unlock(); 80 } 81 82 AddToSubset(fWindow); 83 Hide(); 84 Show(); 85 if (Lock()) { 86 frame = _CalculateFrame(Frame()); 87 MoveTo(frame.LeftTop()); 88 // ResizeTo(frame.Width(), frame.Height()); 89 Show(); 90 Unlock(); 91 } 92 } 93 94 // destructor 95 NamePanel::~NamePanel() 96 { 97 if (fWindow && fWindow->Lock()) { 98 fWindow->SetFeel(fSavedTargetWindowFeel); 99 fWindow->Unlock(); 100 } 101 delete fMessage; 102 } 103 104 // MessageReceived 105 void NamePanel::MessageReceived(BMessage* message) 106 { 107 switch (message->what) { 108 case MSG_PANEL_CANCEL: 109 Quit(); 110 break; 111 case MSG_PANEL_OK: { 112 if (!fTarget) 113 fTarget = fWindow; 114 BLooper* looper = fTarget ? fTarget->Looper() : NULL; 115 if (fMessage && looper) { 116 BMessage cloneMessage(*fMessage); 117 cloneMessage.AddString("name", fNameTC->Text()); 118 cloneMessage.AddRect("frame", Frame()); 119 looper->PostMessage(&cloneMessage, fTarget); 120 } 121 Quit(); 122 break; 123 } 124 default: 125 Panel::MessageReceived(message); 126 } 127 } 128 129 // _CalculateFrame 130 BRect 131 NamePanel::_CalculateFrame(BRect frame) 132 { 133 BScreen screen(this); 134 BRect screenFrame = screen.Frame(); 135 if (!frame.IsValid()) 136 frame.Set(-1000.0, -1000.0, -900.0, -900.0); 137 if (!screenFrame.Contains(frame)) { 138 float width = frame.Width(); 139 float height = frame.Height(); 140 BPoint center; 141 center.x = screenFrame.left + screenFrame.Width() / 2.0; 142 center.y = screenFrame.top + screenFrame.Height() / 4.0; 143 frame.left = center.x - width / 2.0; 144 frame.right = frame.left + width; 145 frame.top = center.y - height / 2.0; 146 frame.bottom = frame.top + height; 147 } 148 return frame; 149 } 150