144f919daSRyan Leavengood /* 2091440e0SStephan Aßmus * Copyright (c) 2007-2010, Haiku, Inc. 344f919daSRyan Leavengood * Distributed under the terms of the MIT license. 444f919daSRyan Leavengood * 544f919daSRyan Leavengood * Author: 644f919daSRyan Leavengood * Łukasz 'Sil2100' Zemczak <sil2100@vexillium.org> 744f919daSRyan Leavengood */ 844f919daSRyan Leavengood 9*f51cdb44SStephan Aßmus 1044f919daSRyan Leavengood #include "PackageTextViewer.h" 1144f919daSRyan Leavengood 1244f919daSRyan Leavengood #include <Button.h> 13091440e0SStephan Aßmus #include <Catalog.h> 14091440e0SStephan Aßmus #include <Locale.h> 1544f919daSRyan Leavengood #include <ScrollView.h> 1644f919daSRyan Leavengood 1744f919daSRyan Leavengood #include <GroupLayout.h> 1844f919daSRyan Leavengood #include <GroupLayoutBuilder.h> 1944f919daSRyan Leavengood 2044f919daSRyan Leavengood 2144f919daSRyan Leavengood enum { 2244f919daSRyan Leavengood P_MSG_ACCEPT = 'pmac', 2344f919daSRyan Leavengood P_MSG_DECLINE 2444f919daSRyan Leavengood }; 2544f919daSRyan Leavengood 26091440e0SStephan Aßmus #undef TR_CONTEXT 27091440e0SStephan Aßmus #define TR_CONTEXT "PackageTextViewer" 2844f919daSRyan Leavengood 2944f919daSRyan Leavengood 3044f919daSRyan Leavengood PackageTextViewer::PackageTextViewer(const char *text, bool disclaimer) 31091440e0SStephan Aßmus : 32091440e0SStephan Aßmus BWindow(BRect(125, 125, 675, 475), TR("Disclaimer"), B_MODAL_WINDOW, 3344f919daSRyan Leavengood B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_NOT_CLOSABLE), 3444f919daSRyan Leavengood fValue(0) 3544f919daSRyan Leavengood { 3644f919daSRyan Leavengood _InitView(text, disclaimer); 3744f919daSRyan Leavengood } 3844f919daSRyan Leavengood 3944f919daSRyan Leavengood 4044f919daSRyan Leavengood PackageTextViewer::~PackageTextViewer() 4144f919daSRyan Leavengood { 4244f919daSRyan Leavengood } 4344f919daSRyan Leavengood 4444f919daSRyan Leavengood 4544f919daSRyan Leavengood int32 4644f919daSRyan Leavengood PackageTextViewer::Go() 4744f919daSRyan Leavengood { 4844f919daSRyan Leavengood // Since this class can be thought of as a modified BAlert window, no use 4944f919daSRyan Leavengood // to reinvent a well fledged wheel. This concept has been borrowed from 5044f919daSRyan Leavengood // the current BAlert implementation 5144f919daSRyan Leavengood fSemaphore = create_sem(0, "TextViewer"); 5244f919daSRyan Leavengood if (fSemaphore < B_OK) { 5344f919daSRyan Leavengood Quit(); 5444f919daSRyan Leavengood return B_ERROR; 5544f919daSRyan Leavengood } 5644f919daSRyan Leavengood 5744f919daSRyan Leavengood BWindow *parent = 5844f919daSRyan Leavengood dynamic_cast<BWindow *>(BLooper::LooperForThread(find_thread(NULL))); 5944f919daSRyan Leavengood Show(); 6044f919daSRyan Leavengood 6144f919daSRyan Leavengood if (parent) { 6244f919daSRyan Leavengood status_t ret; 6344f919daSRyan Leavengood for (;;) { 6444f919daSRyan Leavengood do { 6544f919daSRyan Leavengood ret = acquire_sem_etc(fSemaphore, 1, B_RELATIVE_TIMEOUT, 50000); 6644f919daSRyan Leavengood } while (ret == B_INTERRUPTED); 6744f919daSRyan Leavengood 6844f919daSRyan Leavengood if (ret == B_BAD_SEM_ID) 6944f919daSRyan Leavengood break; 7044f919daSRyan Leavengood parent->UpdateIfNeeded(); 7144f919daSRyan Leavengood } 7244f919daSRyan Leavengood } 7344f919daSRyan Leavengood else { 7444f919daSRyan Leavengood // Since there are no spinlocks, wait until the semaphore is free 7544f919daSRyan Leavengood while (acquire_sem(fSemaphore) == B_INTERRUPTED) { 7644f919daSRyan Leavengood } 7744f919daSRyan Leavengood } 7844f919daSRyan Leavengood 7944f919daSRyan Leavengood int32 value = fValue; 8044f919daSRyan Leavengood if (Lock()) 8144f919daSRyan Leavengood Quit(); 8244f919daSRyan Leavengood 8344f919daSRyan Leavengood return value; 8444f919daSRyan Leavengood } 8544f919daSRyan Leavengood 8644f919daSRyan Leavengood 8744f919daSRyan Leavengood void 8844f919daSRyan Leavengood PackageTextViewer::MessageReceived(BMessage *msg) 8944f919daSRyan Leavengood { 9044f919daSRyan Leavengood if (msg->what == P_MSG_ACCEPT) { 9144f919daSRyan Leavengood if (fSemaphore >= B_OK) { 9244f919daSRyan Leavengood fValue = 1; 9344f919daSRyan Leavengood delete_sem(fSemaphore); 9444f919daSRyan Leavengood fSemaphore = -1; 9544f919daSRyan Leavengood } 96091440e0SStephan Aßmus } else if (msg->what == P_MSG_DECLINE) { 9744f919daSRyan Leavengood if (fSemaphore >= B_OK) { 9844f919daSRyan Leavengood fValue = 0; 9944f919daSRyan Leavengood delete_sem(fSemaphore); 10044f919daSRyan Leavengood fSemaphore = -1; 10144f919daSRyan Leavengood } 102091440e0SStephan Aßmus } else 10344f919daSRyan Leavengood BWindow::MessageReceived(msg); 10444f919daSRyan Leavengood } 10544f919daSRyan Leavengood 10644f919daSRyan Leavengood 10744f919daSRyan Leavengood // #pragma mark - 10844f919daSRyan Leavengood 10944f919daSRyan Leavengood 11044f919daSRyan Leavengood void 11144f919daSRyan Leavengood PackageTextViewer::_InitView(const char *text, bool disclaimer) 11244f919daSRyan Leavengood { 11344f919daSRyan Leavengood fBackground = new BView(Bounds(), "background_view", 0, 0); 11444f919daSRyan Leavengood fBackground->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 11544f919daSRyan Leavengood 11644f919daSRyan Leavengood BRect bounds; 11744f919daSRyan Leavengood BRect rect = Bounds(); 11844f919daSRyan Leavengood if (disclaimer) { 119091440e0SStephan Aßmus BButton *button = new BButton(BRect(0, 0, 1, 1), "accept", 120091440e0SStephan Aßmus TR("Accept"), new BMessage(P_MSG_ACCEPT)); 12144f919daSRyan Leavengood button->ResizeToPreferred(); 12244f919daSRyan Leavengood 12344f919daSRyan Leavengood bounds = button->Bounds(); 12444f919daSRyan Leavengood rect.top = rect.bottom - bounds.bottom - 5.0f; 12544f919daSRyan Leavengood rect.left = rect.right - bounds.right - 5.0f; 12644f919daSRyan Leavengood rect.bottom = bounds.bottom; 12744f919daSRyan Leavengood rect.right = bounds.right; 12844f919daSRyan Leavengood button->MoveTo(rect.LeftTop()); 12944f919daSRyan Leavengood button->MakeDefault(true); 13044f919daSRyan Leavengood fBackground->AddChild(button); 13144f919daSRyan Leavengood 132091440e0SStephan Aßmus button = new BButton(BRect(0, 0, 1, 1), "decline", TR("Decline"), 13344f919daSRyan Leavengood new BMessage(P_MSG_DECLINE)); 13444f919daSRyan Leavengood button->ResizeToPreferred(); 13544f919daSRyan Leavengood 13644f919daSRyan Leavengood bounds = button->Bounds(); 13744f919daSRyan Leavengood rect.left -= bounds.right + 7.0f; 13844f919daSRyan Leavengood button->MoveTo(rect.LeftTop()); 13944f919daSRyan Leavengood fBackground->AddChild(button); 140091440e0SStephan Aßmus } else { 141091440e0SStephan Aßmus BButton *button = new BButton(BRect(0, 0, 1, 1), "accept", 142091440e0SStephan Aßmus TR("Continue"), new BMessage(P_MSG_ACCEPT)); 14344f919daSRyan Leavengood button->ResizeToPreferred(); 14444f919daSRyan Leavengood 14544f919daSRyan Leavengood bounds = button->Bounds(); 14644f919daSRyan Leavengood rect.top = rect.bottom - bounds.bottom - 5.0f; 14744f919daSRyan Leavengood rect.left = rect.right - bounds.right - 5.0f; 14844f919daSRyan Leavengood rect.bottom = bounds.bottom; 14944f919daSRyan Leavengood rect.right = bounds.right; 15044f919daSRyan Leavengood button->MoveTo(rect.LeftTop()); 15144f919daSRyan Leavengood button->MakeDefault(true); 15244f919daSRyan Leavengood fBackground->AddChild(button); 15344f919daSRyan Leavengood } 15444f919daSRyan Leavengood 15544f919daSRyan Leavengood bounds = Bounds().InsetBySelf(5.0f, 5.0f); 15644f919daSRyan Leavengood bounds.bottom = rect.top - 6.0f; 15744f919daSRyan Leavengood bounds.right -= B_V_SCROLL_BAR_WIDTH; 15844f919daSRyan Leavengood 15944f919daSRyan Leavengood fText = new BTextView(bounds, "text_view", BRect(0, 0, bounds.Width(), 16044f919daSRyan Leavengood bounds.Height()), B_FOLLOW_NONE, B_WILL_DRAW); 16144f919daSRyan Leavengood fText->MakeEditable(false); 16244f919daSRyan Leavengood fText->MakeSelectable(true); 16344f919daSRyan Leavengood fText->SetText(text); 16444f919daSRyan Leavengood 16544f919daSRyan Leavengood BScrollView *scroll = new BScrollView("scroll_view", fText, 16644f919daSRyan Leavengood B_FOLLOW_LEFT | B_FOLLOW_TOP, 0, false, true); 16744f919daSRyan Leavengood 16844f919daSRyan Leavengood fBackground->AddChild(scroll); 16944f919daSRyan Leavengood 17044f919daSRyan Leavengood AddChild(fBackground); 17144f919daSRyan Leavengood } 17244f919daSRyan Leavengood 17344f919daSRyan Leavengood 17444f919daSRyan Leavengood /*void 17544f919daSRyan Leavengood PackageTextViewer::_InitView(const char *text, bool disclaimer) 17644f919daSRyan Leavengood { 17744f919daSRyan Leavengood SetLayout(new BGroupLayout(B_HORIZONTAL)); 17844f919daSRyan Leavengood 17944f919daSRyan Leavengood fText = new BTextView(BRect(0, 0, 1, 1), "text_view", BRect(0, 0, 1, 1), 18044f919daSRyan Leavengood B_FOLLOW_NONE, B_WILL_DRAW | B_SUPPORTS_LAYOUT); 18144f919daSRyan Leavengood fText->MakeEditable(false); 18244f919daSRyan Leavengood fText->MakeSelectable(true); 18344f919daSRyan Leavengood BScrollView *scroll = new BScrollView("scroll_view", fText, 18444f919daSRyan Leavengood B_FOLLOW_LEFT | B_FOLLOW_TOP, 0, false, true); 18544f919daSRyan Leavengood 18644f919daSRyan Leavengood if (disclaimer) { 187091440e0SStephan Aßmus BButton *accept = new BButton("accept", TR("Accept"), 18844f919daSRyan Leavengood new BMessage(P_MSG_ACCEPT)); 18944f919daSRyan Leavengood 190091440e0SStephan Aßmus BButton *decline = new BButton("decline", TR("Decline"), 19144f919daSRyan Leavengood new BMessage(P_MSG_DECLINE)); 19244f919daSRyan Leavengood 19344f919daSRyan Leavengood fBackground = BGroupLayoutBuilder(B_VERTICAL) 19444f919daSRyan Leavengood .Add(scroll) 19544f919daSRyan Leavengood .AddGroup(B_HORIZONTAL, 5.0f) 19644f919daSRyan Leavengood .AddGlue() 19744f919daSRyan Leavengood .Add(accept) 19844f919daSRyan Leavengood .Add(decline) 19944f919daSRyan Leavengood .End(); 20044f919daSRyan Leavengood } 20144f919daSRyan Leavengood else { 202091440e0SStephan Aßmus BButton *button = new BButton("accept", TR("Continue"), 20344f919daSRyan Leavengood new BMessage(P_MSG_ACCEPT)); 20444f919daSRyan Leavengood 20544f919daSRyan Leavengood fBackground = BGroupLayoutBuilder(B_VERTICAL) 20644f919daSRyan Leavengood .Add(scroll) 20744f919daSRyan Leavengood .AddGroup(B_HORIZONTAL, 5.0f) 20844f919daSRyan Leavengood .AddGlue() 20944f919daSRyan Leavengood .Add(button) 21044f919daSRyan Leavengood .End(); 21144f919daSRyan Leavengood } 21244f919daSRyan Leavengood 21344f919daSRyan Leavengood AddChild(fBackground); 21444f919daSRyan Leavengood 21544f919daSRyan Leavengood fBackground->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 21644f919daSRyan Leavengood fText->SetText(text); 21744f919daSRyan Leavengood }*/ 21844f919daSRyan Leavengood 219