23 #include "../../SDL_internal.h"
25 #if SDL_VIDEO_DRIVER_HAIKU
30 #include "../../main/haiku/SDL_BeApp.h"
33 #include <Application.h>
42 #include <InterfaceDefs.h>
43 #include <SupportDefs.h>
44 #include <GraphicsDefs.h>
52 G_CLOSE_BUTTON_ID = -1,
53 G_DEFAULT_BUTTON_ID = 0,
54 G_MAX_STRING_LENGTH_BYTES = 120
57 class HAIKU_SDL_MessageBox :
public BAlert
59 float fComputedMessageBoxWidth;
61 BTextView *fMessageBoxTextView;
66 bool fCustomColorScheme;
67 bool fThereIsLongLine;
71 const char *HAIKU_SDL_DefTitle;
72 const char *HAIKU_SDL_DefMessage;
73 const char *HAIKU_SDL_DefButton;
75 std::vector<const SDL_MessageBoxButtonData *> fButtons;
92 return B_WARNING_ALERT;
108 rgb_color
color = { aColor->
r, aColor->
g, aColor->
b,
color.alpha = 255 };
113 GetLeftPanelWidth(
void)
const
121 int32
scale = max_c(1, ((int32)be_plain_font->Size() + 15) / 16);
126 UpdateTextViewWidth(
void)
128 fComputedMessageBoxWidth = fMessageBoxTextView->PreferredSize().Width() + GetLeftPanelWidth();
134 if (aMessageBoxData ==
NULL)
136 SetTitle(HAIKU_SDL_DefTitle);
137 SetMessageText(HAIKU_SDL_DefMessage);
138 AddButton(HAIKU_SDL_DefButton);
144 AddButton(HAIKU_SDL_DefButton);
153 fCustomColorScheme =
true;
154 ApplyAndParseColorScheme(aMessageBoxData->
colorScheme);
158 SetTitle(aMessageBoxData->
title) : SetTitle(HAIKU_SDL_DefTitle);
160 SetMessageText(aMessageBoxData->
message) : SetMessageText(HAIKU_SDL_DefMessage);
182 if (fCustomColorScheme)
184 int32 countButtons = CountButtons();
185 for (
int i = 0;
i < countButtons; ++
i)
187 ButtonAt(
i)->SetViewColor(ConvertColorType(aBorderColor));
188 ButtonAt(
i)->SetLowColor(ConvertColorType(aBackgroundColor));
193 ButtonAt(
i)->SetHighColor(ConvertColorType(aTextColor));
198 (
void)aSelectedColor;
204 rgb_color
background = ConvertColorType(aColor);
206 GetLayout()->View()->SetViewColor(
background);
208 FindView(
"TAlertView")->SetViewColor(
background);
209 fMessageBoxTextView->SetViewColor(
background);
213 CheckLongLines(
const char *aMessage)
234 return (
final > G_MAX_STRING_LENGTH_BYTES);
238 SetMessageText(
const char *aMessage)
240 fThereIsLongLine = CheckLongLines(aMessage);
241 if (fThereIsLongLine)
243 fMessageBoxTextView->SetWordWrap(
true);
246 rgb_color
textColor = ui_color(B_PANEL_TEXT_COLOR);
247 if (fCustomColorScheme)
262 fMessageBoxTextView->SetFontAndColor(be_plain_font, B_FONT_ALL, &
textColor);
263 fMessageBoxTextView->Insert(aMessage);
266 UpdateTextViewWidth();
272 for (
int i = 0;
i < aNumButtons; ++
i)
274 fButtons.push_back(&aButtons[
i]);
277 std::sort(fButtons.begin(), fButtons.end(), &HAIKU_SDL_MessageBox::SortButtonsPredicate);
279 size_t countButtons = fButtons.size();
280 for (
size_t i = 0;
i < countButtons; ++
i)
282 switch (fButtons[
i]->
flags)
286 fCloseButton =
static_cast<int>(
i);
291 fDefaultButton =
static_cast<int>(
i);
299 AddButton(fButtons[
i]->
text);
302 SetDefaultButton(ButtonAt(fDefaultButton));
309 fComputedMessageBoxWidth(0.0
f),
310 fCloseButton(G_CLOSE_BUTTON_ID), fDefaultButton(G_DEFAULT_BUTTON_ID),
311 fCustomColorScheme(false), fThereIsLongLine(false),
312 HAIKU_SDL_DefTitle(
"SDL2 MessageBox"),
313 HAIKU_SDL_DefMessage(
"Some information has been lost."),
314 HAIKU_SDL_DefButton(
"OK")
318 SetLook(B_TITLED_WINDOW_LOOK);
319 SetFlags(Flags() | B_CLOSE_ON_ESCAPE);
322 fMessageBoxTextView = TextView();
323 fMessageBoxTextView->SetWordWrap(
false);
324 fMessageBoxTextView->SetStylable(
true);
326 ParseSdlMessageBoxData(aMessageBoxData);
330 GetCloseButtonId(
void)
const
336 ~HAIKU_SDL_MessageBox(
void)
343 FrameResized(
float aNewWidth,
float aNewHeight)
345 if (fComputedMessageBoxWidth > aNewWidth)
347 ResizeTo(fComputedMessageBoxWidth, aNewHeight);
351 BAlert::FrameResized(aNewWidth, aNewHeight);
356 SetTitle(
const char* aTitle)
359 BAlert::SetTitle(aTitle);
371 *buttonid = G_CLOSE_BUTTON_ID;
377 BApplication *application =
NULL;
380 application =
new(std::nothrow) BApplication(
signature);
381 if (application ==
NULL)
383 return SDL_SetError(
"Cannot create the BApplication object. Lack of memory?");
387 HAIKU_SDL_MessageBox *SDL_MessageBox =
new(std::nothrow) HAIKU_SDL_MessageBox(messageboxdata);
388 if (SDL_MessageBox ==
NULL)
390 return SDL_SetError(
"Cannot create the HAIKU_SDL_MessageBox (BAlert inheritor) object. Lack of memory?");
392 const int closeButton = SDL_MessageBox->GetCloseButtonId();
393 int pushedButton = SDL_MessageBox->Go();
396 if (closeButton != G_CLOSE_BUTTON_ID && pushedButton == G_CLOSE_BUTTON_ID)
398 pushedButton = closeButton;
408 if (application !=
NULL)
414 *buttonid = pushedButton;