194457adeSPulkoMandy /*
294457adeSPulkoMandy * Copyright 2019-2023, Adrien Destugues, pulkomandy@pulkomandy.tk.
394457adeSPulkoMandy * Distributed under the terms of the MIT License.
494457adeSPulkoMandy */
594457adeSPulkoMandy
694457adeSPulkoMandy
794457adeSPulkoMandy #include "AlertWithCheckbox.h"
894457adeSPulkoMandy
994457adeSPulkoMandy #include <algorithm>
1094457adeSPulkoMandy #include <stdio.h>
1194457adeSPulkoMandy
1294457adeSPulkoMandy #include <Button.h>
1394457adeSPulkoMandy #include <Catalog.h>
1494457adeSPulkoMandy #include <CheckBox.h>
1594457adeSPulkoMandy #include <ControlLook.h>
1694457adeSPulkoMandy #include <GroupLayout.h>
1794457adeSPulkoMandy #include <LayoutBuilder.h>
1894457adeSPulkoMandy #include <Locale.h>
1994457adeSPulkoMandy #include <IconUtils.h>
2094457adeSPulkoMandy #include <Resources.h>
2194457adeSPulkoMandy #include <StripeView.h>
2294457adeSPulkoMandy #include <TextView.h>
2394457adeSPulkoMandy
2494457adeSPulkoMandy #undef B_TRANSLATION_CONTEXT
2594457adeSPulkoMandy #define B_TRANSLATION_CONTEXT "DebugServer"
2694457adeSPulkoMandy
2794457adeSPulkoMandy
2894457adeSPulkoMandy enum {
2994457adeSPulkoMandy kButton1Message,
3094457adeSPulkoMandy kButton2Message,
3194457adeSPulkoMandy kButton3Message
3294457adeSPulkoMandy };
3394457adeSPulkoMandy
3494457adeSPulkoMandy
AlertWithCheckbox(const char * title,const char * messageText,const char * checkboxLabel,const char * label1,const char * label2,const char * label3)3594457adeSPulkoMandy AlertWithCheckbox::AlertWithCheckbox(const char* title, const char* messageText,
3694457adeSPulkoMandy const char* checkboxLabel, const char* label1, const char* label2, const char* label3)
3794457adeSPulkoMandy :
3894457adeSPulkoMandy BWindow(BRect(0, 0, 100, 50), title,
3994457adeSPulkoMandy B_MODAL_WINDOW_LOOK, B_FLOATING_ALL_WINDOW_FEEL,
4094457adeSPulkoMandy B_CLOSE_ON_ESCAPE | B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS),
4194457adeSPulkoMandy fBitmap(IconSize(), B_RGBA32),
4294457adeSPulkoMandy fSemaphore(create_sem(0, "AlertWithCheckbox")),
4394457adeSPulkoMandy fAction(0)
4494457adeSPulkoMandy {
45*4dbd4747SPulkoMandy BIconUtils::GetSystemIcon("dialog-information", &fBitmap);
4694457adeSPulkoMandy BStripeView *stripeView = new BStripeView(fBitmap);
4794457adeSPulkoMandy
4894457adeSPulkoMandy BTextView *message = new BTextView("_tv_");
4994457adeSPulkoMandy message->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
5094457adeSPulkoMandy rgb_color textColor = ui_color(B_PANEL_TEXT_COLOR);
5194457adeSPulkoMandy message->SetFontAndColor(be_plain_font, B_FONT_ALL, &textColor);
5294457adeSPulkoMandy message->MakeEditable(false);
5394457adeSPulkoMandy message->MakeSelectable(false);
5494457adeSPulkoMandy message->SetText(messageText);
556a06a562SPulkoMandy BRect textRect = message->TextRect();
566a06a562SPulkoMandy textRect.PrintToStream();
576a06a562SPulkoMandy message->SetWordWrap(true);
5894457adeSPulkoMandy message->SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNSET));
5994457adeSPulkoMandy float width = message->StringWidth("W") * 40;
606a06a562SPulkoMandy if (width < textRect.Width()) {
6194457adeSPulkoMandy message->SetExplicitMinSize(BSize(width, B_SIZE_UNSET));
626a06a562SPulkoMandy }
6394457adeSPulkoMandy
64217bbbf4SPulkoMandy fDontAskAgain = new BCheckBox("checkbox", checkboxLabel, NULL);
6594457adeSPulkoMandy
66217bbbf4SPulkoMandy BGroupView* buttonGroup = new BGroupView(B_HORIZONTAL);
67217bbbf4SPulkoMandy BLayoutBuilder::Group<> layoutBuilder(buttonGroup);
68217bbbf4SPulkoMandy
69217bbbf4SPulkoMandy layoutBuilder.AddGlue();
70217bbbf4SPulkoMandy
71217bbbf4SPulkoMandy if (label1) {
72217bbbf4SPulkoMandy BButton* button = new BButton(label1, label1, new BMessage(kButton1Message));
73217bbbf4SPulkoMandy button->MakeDefault(true);
74217bbbf4SPulkoMandy layoutBuilder.Add(button);
75217bbbf4SPulkoMandy }
76217bbbf4SPulkoMandy
77217bbbf4SPulkoMandy if (label2) {
78217bbbf4SPulkoMandy BButton* button = new BButton(label2, label2, new BMessage(kButton2Message));
79217bbbf4SPulkoMandy layoutBuilder.Add(button);
80217bbbf4SPulkoMandy }
81217bbbf4SPulkoMandy
82217bbbf4SPulkoMandy if (label3) {
83217bbbf4SPulkoMandy BButton* button = new BButton(label3, label3, new BMessage(kButton3Message));
84217bbbf4SPulkoMandy layoutBuilder.Add(button);
85217bbbf4SPulkoMandy }
8694457adeSPulkoMandy
8794457adeSPulkoMandy BLayoutBuilder::Group<>(this)
8894457adeSPulkoMandy .AddGroup(B_HORIZONTAL)
8994457adeSPulkoMandy .Add(stripeView)
9094457adeSPulkoMandy .AddGroup(B_VERTICAL)
9194457adeSPulkoMandy .SetInsets(B_USE_SMALL_SPACING)
9294457adeSPulkoMandy .Add(message)
9394457adeSPulkoMandy .AddGroup(B_HORIZONTAL, 0)
9494457adeSPulkoMandy .Add(fDontAskAgain)
9594457adeSPulkoMandy .AddGlue()
9694457adeSPulkoMandy .End()
97217bbbf4SPulkoMandy .Add(buttonGroup)
9894457adeSPulkoMandy .End()
9994457adeSPulkoMandy .End();
10094457adeSPulkoMandy
10194457adeSPulkoMandy ResizeToPreferred();
10294457adeSPulkoMandy CenterOnScreen();
10394457adeSPulkoMandy }
10494457adeSPulkoMandy
10594457adeSPulkoMandy
~AlertWithCheckbox()10694457adeSPulkoMandy AlertWithCheckbox::~AlertWithCheckbox()
10794457adeSPulkoMandy {
10894457adeSPulkoMandy delete_sem(fSemaphore);
10994457adeSPulkoMandy }
11094457adeSPulkoMandy
11194457adeSPulkoMandy
11294457adeSPulkoMandy void
MessageReceived(BMessage * message)11394457adeSPulkoMandy AlertWithCheckbox::MessageReceived(BMessage* message)
11494457adeSPulkoMandy {
11594457adeSPulkoMandy switch(message->what) {
11694457adeSPulkoMandy case B_QUIT_REQUESTED:
11794457adeSPulkoMandy release_sem(fSemaphore);
11894457adeSPulkoMandy break;
11994457adeSPulkoMandy
12094457adeSPulkoMandy case 0:
12194457adeSPulkoMandy case 1:
12294457adeSPulkoMandy case 2:
12394457adeSPulkoMandy fAction = message->what;
12494457adeSPulkoMandy PostMessage(B_QUIT_REQUESTED);
12594457adeSPulkoMandy return;
12694457adeSPulkoMandy }
12794457adeSPulkoMandy
12894457adeSPulkoMandy BWindow::MessageReceived(message);
12994457adeSPulkoMandy }
13094457adeSPulkoMandy
13194457adeSPulkoMandy
13294457adeSPulkoMandy int32
Go(bool & dontAskAgain)13394457adeSPulkoMandy AlertWithCheckbox::Go(bool& dontAskAgain)
13494457adeSPulkoMandy {
13594457adeSPulkoMandy Show();
13694457adeSPulkoMandy
13794457adeSPulkoMandy // Wait for user to close the window
13894457adeSPulkoMandy acquire_sem(fSemaphore);
13994457adeSPulkoMandy dontAskAgain = fDontAskAgain->Value();
14094457adeSPulkoMandy return fAction;
14194457adeSPulkoMandy }
14294457adeSPulkoMandy
14394457adeSPulkoMandy
14494457adeSPulkoMandy BRect
IconSize()14594457adeSPulkoMandy AlertWithCheckbox::IconSize()
14694457adeSPulkoMandy {
14794457adeSPulkoMandy return BRect(BPoint(0, 0), be_control_look->ComposeIconSize(32));
14894457adeSPulkoMandy }
149