2 * Copyright 2017 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
10 #include "AddRepoWindow.h"
13 #include <Application.h>
15 #include <Clipboard.h>
16 #include <LayoutBuilder.h>
19 #include "constants.h"
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "AddRepoWindow"
24 static float sAddWindowWidth
= 500.0;
27 AddRepoWindow::AddRepoWindow(BRect size
, const BMessenger
& messenger
)
29 BWindow(BRect(0, 0, sAddWindowWidth
, 10), "AddWindow", B_MODAL_WINDOW
,
30 B_ASYNCHRONOUS_CONTROLS
| B_AUTO_UPDATE_SIZE_LIMITS
| B_CLOSE_ON_ESCAPE
),
31 fReplyMessenger(messenger
)
33 fText
= new BTextControl("text", B_TRANSLATE_COMMENT("Repository URL:",
34 "Text box label"), "", new BMessage(ADD_BUTTON_PRESSED
));
35 fAddButton
= new BButton(B_TRANSLATE_COMMENT("Add", "Button label"),
36 new BMessage(ADD_BUTTON_PRESSED
));
37 fAddButton
->MakeDefault(true);
38 fCancelButton
= new BButton(kCancelLabel
,
39 new BMessage(CANCEL_BUTTON_PRESSED
));
41 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
42 .SetInsets(B_USE_WINDOW_SPACING
)
44 .AddGroup(B_HORIZONTAL
, B_USE_DEFAULT_SPACING
)
53 // Move to the center of the preflet window
55 float widthDifference
= size
.Width() - Frame().Width();
56 if (widthDifference
< 0)
57 MoveBy(widthDifference
/ 2.0, 0);
65 fReplyMessenger
.SendMessage(ADD_WINDOW_CLOSED
);
71 AddRepoWindow::MessageReceived(BMessage
* message
)
73 switch (message
->what
)
75 case CANCEL_BUTTON_PRESSED
:
80 case ADD_BUTTON_PRESSED
:
82 BString
url(fText
->Text());
84 // URL must have a protocol
86 if (!newRepoUrl
.IsValid()) {
87 BAlert
* alert
= new BAlert("error",
88 B_TRANSLATE_COMMENT("This is not a valid URL.",
89 "Add URL error message"),
90 kOKLabel
, NULL
, NULL
, B_WIDTH_AS_USUAL
, B_STOP_ALERT
);
91 alert
->SetFeel(B_MODAL_APP_WINDOW_FEEL
);
93 // Center the alert to this window and move down some
94 alert
->CenterIn(Frame());
95 alert
->MoveBy(0, kAddWindowOffset
);
97 BMessage
* addMessage
= new BMessage(ADD_REPO_URL
);
98 addMessage
->AddString(key_url
, url
);
99 fReplyMessenger
.SendMessage(addMessage
);
107 BWindow::MessageReceived(message
);
113 AddRepoWindow::FrameResized(float newWidth
, float newHeight
)
115 sAddWindowWidth
= newWidth
;
120 AddRepoWindow::_GetClipboardData()
122 if (be_clipboard
->Lock()) {
125 BMessage
* clip
= be_clipboard
->Data();
126 clip
->FindData("text/plain", B_MIME_TYPE
, (const void **)&string
,
128 be_clipboard
->Unlock();
130 // The string must be a valid url
131 BString
clipString(string
, stringLen
);
132 BUrl
testUrl(clipString
.String());
133 if (!testUrl
.IsValid())
136 fText
->SetText(clipString
);