1*94457adeSPulkoMandy /* 2*94457adeSPulkoMandy * Copyright 2019-2023, Adrien Destugues, pulkomandy@pulkomandy.tk. 3*94457adeSPulkoMandy * Distributed under the terms of the MIT License. 4*94457adeSPulkoMandy */ 5*94457adeSPulkoMandy 6*94457adeSPulkoMandy 7*94457adeSPulkoMandy #include "AlertWithCheckbox.h" 8*94457adeSPulkoMandy 9*94457adeSPulkoMandy #include <algorithm> 10*94457adeSPulkoMandy #include <stdio.h> 11*94457adeSPulkoMandy 12*94457adeSPulkoMandy #include <Button.h> 13*94457adeSPulkoMandy #include <Catalog.h> 14*94457adeSPulkoMandy #include <CheckBox.h> 15*94457adeSPulkoMandy #include <ControlLook.h> 16*94457adeSPulkoMandy #include <GroupLayout.h> 17*94457adeSPulkoMandy #include <LayoutBuilder.h> 18*94457adeSPulkoMandy #include <Locale.h> 19*94457adeSPulkoMandy #include <IconUtils.h> 20*94457adeSPulkoMandy #include <Resources.h> 21*94457adeSPulkoMandy #include <StripeView.h> 22*94457adeSPulkoMandy #include <TextView.h> 23*94457adeSPulkoMandy 24*94457adeSPulkoMandy #undef B_TRANSLATION_CONTEXT 25*94457adeSPulkoMandy #define B_TRANSLATION_CONTEXT "DebugServer" 26*94457adeSPulkoMandy 27*94457adeSPulkoMandy 28*94457adeSPulkoMandy enum { 29*94457adeSPulkoMandy kButton1Message, 30*94457adeSPulkoMandy kButton2Message, 31*94457adeSPulkoMandy kButton3Message 32*94457adeSPulkoMandy }; 33*94457adeSPulkoMandy 34*94457adeSPulkoMandy 35*94457adeSPulkoMandy AlertWithCheckbox::AlertWithCheckbox(const char* title, const char* messageText, 36*94457adeSPulkoMandy const char* checkboxLabel, const char* label1, const char* label2, const char* label3) 37*94457adeSPulkoMandy : 38*94457adeSPulkoMandy BWindow(BRect(0, 0, 100, 50), title, 39*94457adeSPulkoMandy B_MODAL_WINDOW_LOOK, B_FLOATING_ALL_WINDOW_FEEL, 40*94457adeSPulkoMandy B_CLOSE_ON_ESCAPE | B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS), 41*94457adeSPulkoMandy fBitmap(IconSize(), B_RGBA32), 42*94457adeSPulkoMandy fSemaphore(create_sem(0, "AlertWithCheckbox")), 43*94457adeSPulkoMandy fAction(0) 44*94457adeSPulkoMandy { 45*94457adeSPulkoMandy BResources resources; 46*94457adeSPulkoMandy resources.SetToImage(B_TRANSLATION_CONTEXT); 47*94457adeSPulkoMandy size_t size; 48*94457adeSPulkoMandy const uint8* iconData = (const uint8*)resources.LoadResource('VICN', 1, 49*94457adeSPulkoMandy &size); 50*94457adeSPulkoMandy 51*94457adeSPulkoMandy // TODO load "info" icon from app_server instead? 52*94457adeSPulkoMandy BIconUtils::GetVectorIcon(iconData, size, &fBitmap); 53*94457adeSPulkoMandy BStripeView *stripeView = new BStripeView(fBitmap); 54*94457adeSPulkoMandy 55*94457adeSPulkoMandy BTextView *message = new BTextView("_tv_"); 56*94457adeSPulkoMandy message->SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 57*94457adeSPulkoMandy rgb_color textColor = ui_color(B_PANEL_TEXT_COLOR); 58*94457adeSPulkoMandy message->SetFontAndColor(be_plain_font, B_FONT_ALL, &textColor); 59*94457adeSPulkoMandy message->MakeEditable(false); 60*94457adeSPulkoMandy message->MakeSelectable(false); 61*94457adeSPulkoMandy message->SetWordWrap(true); 62*94457adeSPulkoMandy message->SetText(messageText); 63*94457adeSPulkoMandy message->SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNSET)); 64*94457adeSPulkoMandy float width = message->StringWidth("W") * 40; 65*94457adeSPulkoMandy message->SetExplicitMinSize(BSize(width, B_SIZE_UNSET)); 66*94457adeSPulkoMandy 67*94457adeSPulkoMandy fDontAskAgain = new BCheckBox("checkbox", 68*94457adeSPulkoMandy checkboxLabel, NULL); 69*94457adeSPulkoMandy 70*94457adeSPulkoMandy BButton* button1 = new BButton(label1, label1, new BMessage(kButton1Message)); 71*94457adeSPulkoMandy BButton* button2 = new BButton(label2, label2, new BMessage(kButton2Message)); 72*94457adeSPulkoMandy BButton* button3 = new BButton(label3, label3, new BMessage(kButton3Message)); 73*94457adeSPulkoMandy button1->MakeDefault(true); 74*94457adeSPulkoMandy 75*94457adeSPulkoMandy BLayoutBuilder::Group<>(this) 76*94457adeSPulkoMandy .AddGroup(B_HORIZONTAL) 77*94457adeSPulkoMandy .Add(stripeView) 78*94457adeSPulkoMandy .AddGroup(B_VERTICAL) 79*94457adeSPulkoMandy .SetInsets(B_USE_SMALL_SPACING) 80*94457adeSPulkoMandy .Add(message) 81*94457adeSPulkoMandy .AddGroup(B_HORIZONTAL, 0) 82*94457adeSPulkoMandy .Add(fDontAskAgain) 83*94457adeSPulkoMandy .AddGlue() 84*94457adeSPulkoMandy .End() 85*94457adeSPulkoMandy .AddGroup(B_HORIZONTAL) 86*94457adeSPulkoMandy .AddGlue() 87*94457adeSPulkoMandy .Add(button1) 88*94457adeSPulkoMandy .Add(button2) 89*94457adeSPulkoMandy .Add(button3) 90*94457adeSPulkoMandy .End() 91*94457adeSPulkoMandy .End() 92*94457adeSPulkoMandy .End(); 93*94457adeSPulkoMandy 94*94457adeSPulkoMandy ResizeToPreferred(); 95*94457adeSPulkoMandy CenterOnScreen(); 96*94457adeSPulkoMandy } 97*94457adeSPulkoMandy 98*94457adeSPulkoMandy 99*94457adeSPulkoMandy AlertWithCheckbox::~AlertWithCheckbox() 100*94457adeSPulkoMandy { 101*94457adeSPulkoMandy delete_sem(fSemaphore); 102*94457adeSPulkoMandy } 103*94457adeSPulkoMandy 104*94457adeSPulkoMandy 105*94457adeSPulkoMandy void 106*94457adeSPulkoMandy AlertWithCheckbox::MessageReceived(BMessage* message) 107*94457adeSPulkoMandy { 108*94457adeSPulkoMandy switch(message->what) { 109*94457adeSPulkoMandy case B_QUIT_REQUESTED: 110*94457adeSPulkoMandy release_sem(fSemaphore); 111*94457adeSPulkoMandy break; 112*94457adeSPulkoMandy 113*94457adeSPulkoMandy case 0: 114*94457adeSPulkoMandy case 1: 115*94457adeSPulkoMandy case 2: 116*94457adeSPulkoMandy fAction = message->what; 117*94457adeSPulkoMandy PostMessage(B_QUIT_REQUESTED); 118*94457adeSPulkoMandy return; 119*94457adeSPulkoMandy } 120*94457adeSPulkoMandy 121*94457adeSPulkoMandy BWindow::MessageReceived(message); 122*94457adeSPulkoMandy } 123*94457adeSPulkoMandy 124*94457adeSPulkoMandy 125*94457adeSPulkoMandy int32 126*94457adeSPulkoMandy AlertWithCheckbox::Go(bool& dontAskAgain) 127*94457adeSPulkoMandy { 128*94457adeSPulkoMandy Show(); 129*94457adeSPulkoMandy 130*94457adeSPulkoMandy // Wait for user to close the window 131*94457adeSPulkoMandy acquire_sem(fSemaphore); 132*94457adeSPulkoMandy dontAskAgain = fDontAskAgain->Value(); 133*94457adeSPulkoMandy return fAction; 134*94457adeSPulkoMandy } 135*94457adeSPulkoMandy 136*94457adeSPulkoMandy 137*94457adeSPulkoMandy BRect 138*94457adeSPulkoMandy AlertWithCheckbox::IconSize() 139*94457adeSPulkoMandy { 140*94457adeSPulkoMandy return BRect(BPoint(0, 0), be_control_look->ComposeIconSize(32)); 141*94457adeSPulkoMandy } 142