src/video/haiku/SDL_bmessagebox.cc
changeset 13230 57ed423da32a
child 13238 007002587d5d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/haiku/SDL_bmessagebox.cc	Mon Nov 11 16:44:40 2019 -0500
     1.3 @@ -0,0 +1,425 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     1.7 +  Copyright (C) 2018 EXL <exlmotodev@gmail.com>
     1.8 +
     1.9 +  This software is provided 'as-is', without any express or implied
    1.10 +  warranty.  In no event will the authors be held liable for any damages
    1.11 +  arising from the use of this software.
    1.12 +
    1.13 +  Permission is granted to anyone to use this software for any purpose,
    1.14 +  including commercial applications, and to alter it and redistribute it
    1.15 +  freely, subject to the following restrictions:
    1.16 +
    1.17 +  1. The origin of this software must not be misrepresented; you must not
    1.18 +     claim that you wrote the original software. If you use this software
    1.19 +     in a product, an acknowledgment in the product documentation would be
    1.20 +     appreciated but is not required.
    1.21 +  2. Altered source versions must be plainly marked as such, and must not be
    1.22 +     misrepresented as being the original software.
    1.23 +  3. This notice may not be removed or altered from any source distribution.
    1.24 +*/
    1.25 +
    1.26 +#include "../../SDL_internal.h"
    1.27 +
    1.28 +#if SDL_VIDEO_DRIVER_HAIKU
    1.29 +
    1.30 +#include "SDL_messagebox.h"
    1.31 +
    1.32 +/* For application signature. */
    1.33 +#include "../../main/haiku/SDL_BeApp.h"
    1.34 +
    1.35 +#include <Alert.h>
    1.36 +#include <Application.h>
    1.37 +#include <Button.h>
    1.38 +#include <Font.h>
    1.39 +#include <Layout.h>
    1.40 +#include <String.h>
    1.41 +#include <TextView.h>
    1.42 +#include <View.h>
    1.43 +#include <Window.h>
    1.44 +
    1.45 +#include <InterfaceDefs.h>
    1.46 +#include <SupportDefs.h>
    1.47 +#include <GraphicsDefs.h>
    1.48 +
    1.49 +#include <new>
    1.50 +#include <vector>
    1.51 +#include <algorithm>
    1.52 +
    1.53 +enum
    1.54 +{
    1.55 +	G_CLOSE_BUTTON_ID   = -1,
    1.56 +	G_DEFAULT_BUTTON_ID = 0,
    1.57 +	G_MAX_STRING_LENGTH_BYTES = 120
    1.58 +};
    1.59 +
    1.60 +class BE_SDL_MessageBox : public BAlert
    1.61 +{
    1.62 +	float fComputedMessageBoxWidth;
    1.63 +
    1.64 +	BTextView *fMessageBoxTextView;
    1.65 +
    1.66 +	int fCloseButton;
    1.67 +	int fDefaultButton;
    1.68 +
    1.69 +	bool fCustomColorScheme;
    1.70 +	bool fThereIsLongLine;
    1.71 +	rgb_color fTextColor;
    1.72 +
    1.73 +	const char *fTitle;
    1.74 +	const char *BE_SDL_DefTitle;
    1.75 +	const char *BE_SDL_DefMessage;
    1.76 +	const char *BE_SDL_DefButton;
    1.77 +
    1.78 +	std::vector<const SDL_MessageBoxButtonData *> fButtons;
    1.79 +
    1.80 +	static bool
    1.81 +	SortButtonsPredicate(const SDL_MessageBoxButtonData *aButtonLeft,
    1.82 +	                                 const SDL_MessageBoxButtonData *aButtonRight)
    1.83 +	{
    1.84 +		return aButtonLeft->buttonid < aButtonRight->buttonid;
    1.85 +	}
    1.86 +
    1.87 +	alert_type
    1.88 +	ConvertMessageBoxType(const SDL_MessageBoxFlags aWindowType) const
    1.89 +	{
    1.90 +		switch (aWindowType)
    1.91 +		{
    1.92 +			default:
    1.93 +			case SDL_MESSAGEBOX_WARNING:
    1.94 +			{
    1.95 +				return B_WARNING_ALERT;
    1.96 +			}
    1.97 +			case SDL_MESSAGEBOX_ERROR:
    1.98 +			{
    1.99 +				return B_STOP_ALERT;
   1.100 +			}
   1.101 +			case SDL_MESSAGEBOX_INFORMATION:
   1.102 +			{
   1.103 +				return B_INFO_ALERT;
   1.104 +			}
   1.105 +		}
   1.106 +	}
   1.107 +
   1.108 +	rgb_color
   1.109 +	ConvertColorType(const SDL_MessageBoxColor *aColor) const
   1.110 +	{
   1.111 +		rgb_color color = { aColor->r, aColor->g, aColor->b, color.alpha = 255 };
   1.112 +		return color;
   1.113 +	}
   1.114 +
   1.115 +	int32
   1.116 +	GetLeftPanelWidth(void) const
   1.117 +	{
   1.118 +		// See file "haiku/src/kits/interface/Alert.cpp" for this magic numbers.
   1.119 +		//    IconStripeWidth = 30 * Scale
   1.120 +		//    IconSize = 32 * Scale
   1.121 +		//    Scale = max_c(1, ((int32)be_plain_font->Size() + 15) / 16)
   1.122 +		//    RealWidth = (IconStripeWidth * Scale) + (IconSize * Scale)
   1.123 +
   1.124 +		int32 scale = max_c(1, ((int32)be_plain_font->Size() + 15) / 16);
   1.125 +		return (30 * scale) + (32 * scale);
   1.126 +	}
   1.127 +
   1.128 +	void
   1.129 +	UpdateTextViewWidth(void)
   1.130 +	{
   1.131 +		fComputedMessageBoxWidth = fMessageBoxTextView->PreferredSize().Width() + GetLeftPanelWidth();
   1.132 +	}
   1.133 +
   1.134 +	void
   1.135 +	ParseSdlMessageBoxData(const SDL_MessageBoxData *aMessageBoxData)
   1.136 +	{
   1.137 +		if (aMessageBoxData == NULL)
   1.138 +		{
   1.139 +			SetTitle(BE_SDL_DefTitle);
   1.140 +			SetMessageText(BE_SDL_DefMessage);
   1.141 +			AddButton(BE_SDL_DefButton);
   1.142 +			return;
   1.143 +		}
   1.144 +
   1.145 +		if (aMessageBoxData->numbuttons <= 0)
   1.146 +		{
   1.147 +			AddButton(BE_SDL_DefButton);
   1.148 +		}
   1.149 +		else
   1.150 +		{
   1.151 +			AddSdlButtons(aMessageBoxData->buttons, aMessageBoxData->numbuttons);
   1.152 +		}
   1.153 +
   1.154 +		if (aMessageBoxData->colorScheme != NULL)
   1.155 +		{
   1.156 +			fCustomColorScheme = true;
   1.157 +			ApplyAndParseColorScheme(aMessageBoxData->colorScheme);
   1.158 +		}
   1.159 +
   1.160 +		(aMessageBoxData->title != NULL) ?
   1.161 +			SetTitle(aMessageBoxData->title) : SetTitle(BE_SDL_DefTitle);
   1.162 +		(aMessageBoxData->message != NULL) ?
   1.163 +			SetMessageText(aMessageBoxData->message) : SetMessageText(BE_SDL_DefMessage);
   1.164 +
   1.165 +		SetType(ConvertMessageBoxType(static_cast<SDL_MessageBoxFlags>(aMessageBoxData->flags)));
   1.166 +	}
   1.167 +
   1.168 +	void
   1.169 +	ApplyAndParseColorScheme(const SDL_MessageBoxColorScheme *aColorScheme)
   1.170 +	{
   1.171 +		SetBackgroundColor(&aColorScheme->colors[SDL_MESSAGEBOX_COLOR_BACKGROUND]);
   1.172 +		fTextColor = ConvertColorType(&aColorScheme->colors[SDL_MESSAGEBOX_COLOR_TEXT]);
   1.173 +		SetButtonColors(&aColorScheme->colors[SDL_MESSAGEBOX_COLOR_BUTTON_BORDER],
   1.174 +		                &aColorScheme->colors[SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND],
   1.175 +		                &aColorScheme->colors[SDL_MESSAGEBOX_COLOR_TEXT],
   1.176 +		                &aColorScheme->colors[SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED]);
   1.177 +	}
   1.178 +
   1.179 +	void
   1.180 +	SetButtonColors(const SDL_MessageBoxColor *aBorderColor,
   1.181 +	                const SDL_MessageBoxColor *aBackgroundColor,
   1.182 +	                const SDL_MessageBoxColor *aTextColor,
   1.183 +	                const SDL_MessageBoxColor *aSelectedColor)
   1.184 +	{
   1.185 +		if (fCustomColorScheme)
   1.186 +		{
   1.187 +			int32 countButtons = CountButtons();
   1.188 +			for (int i = 0; i < countButtons; ++i)
   1.189 +			{
   1.190 +				ButtonAt(i)->SetViewColor(ConvertColorType(aBorderColor));
   1.191 +				ButtonAt(i)->SetLowColor(ConvertColorType(aBackgroundColor));
   1.192 +
   1.193 +				// This doesn't work. See this why:
   1.194 +				// https://github.com/haiku/haiku/commit/de9c53f8f5008c7b3b0af75d944a628e17f6dffe
   1.195 +				// Let it remain.
   1.196 +				ButtonAt(i)->SetHighColor(ConvertColorType(aTextColor));
   1.197 +			}
   1.198 +		}
   1.199 +		// TODO: Not Implemented.
   1.200 +		// Is it even necessary?!
   1.201 +		(void)aSelectedColor;
   1.202 +	}
   1.203 +
   1.204 +	void
   1.205 +	SetBackgroundColor(const SDL_MessageBoxColor *aColor)
   1.206 +	{
   1.207 +		rgb_color background = ConvertColorType(aColor);
   1.208 +
   1.209 +		GetLayout()->View()->SetViewColor(background);
   1.210 +		// See file "haiku/src/kits/interface/Alert.cpp", the "TAlertView" is the internal name of the left panel.
   1.211 +		FindView("TAlertView")->SetViewColor(background);
   1.212 +		fMessageBoxTextView->SetViewColor(background);
   1.213 +	}
   1.214 +
   1.215 +	bool
   1.216 +	CheckLongLines(const char *aMessage)
   1.217 +	{
   1.218 +		int final = 0;
   1.219 +
   1.220 +		// This UTF-8 friendly.
   1.221 +		BString message = aMessage;
   1.222 +		int32 length = message.CountChars();
   1.223 +
   1.224 +		for (int i = 0, c = 0; i < length; ++i)
   1.225 +		{
   1.226 +			c++;
   1.227 +			if (*(message.CharAt(i)) == '\n')
   1.228 +			{
   1.229 +				c = 0;
   1.230 +			}
   1.231 +			if (c > final)
   1.232 +			{
   1.233 +				final = c;
   1.234 +			}
   1.235 +		}
   1.236 +
   1.237 +		return (final > G_MAX_STRING_LENGTH_BYTES);
   1.238 +	}
   1.239 +
   1.240 +	void
   1.241 +	SetMessageText(const char *aMessage)
   1.242 +	{
   1.243 +		fThereIsLongLine = CheckLongLines(aMessage);
   1.244 +		if (fThereIsLongLine)
   1.245 +		{
   1.246 +			fMessageBoxTextView->SetWordWrap(true);
   1.247 +		}
   1.248 +
   1.249 +		rgb_color textColor = ui_color(B_PANEL_TEXT_COLOR);
   1.250 +		if (fCustomColorScheme)
   1.251 +		{
   1.252 +			textColor = fTextColor;
   1.253 +		}
   1.254 +
   1.255 +		/*
   1.256 +		if (fNoTitledWindow)
   1.257 +		{
   1.258 +			fMessageBoxTextView->SetFontAndColor(be_bold_font);
   1.259 +			fMessageBoxTextView->Insert(fTitle);
   1.260 +			fMessageBoxTextView->Insert("\n\n");
   1.261 +			fMessageBoxTextView->SetFontAndColor(be_plain_font);
   1.262 +		}
   1.263 +		*/
   1.264 +
   1.265 +		fMessageBoxTextView->SetFontAndColor(be_plain_font, B_FONT_ALL, &textColor);
   1.266 +		fMessageBoxTextView->Insert(aMessage);
   1.267 +
   1.268 +		// Be sure to call update width method.
   1.269 +		UpdateTextViewWidth();
   1.270 +	}
   1.271 +
   1.272 +	void
   1.273 +	AddSdlButtons(const SDL_MessageBoxButtonData *aButtons, int aNumButtons)
   1.274 +	{
   1.275 +		for (int i = 0; i < aNumButtons; ++i)
   1.276 +		{
   1.277 +			fButtons.push_back(&aButtons[i]);
   1.278 +		}
   1.279 +
   1.280 +		std::sort(fButtons.begin(), fButtons.end(), &BE_SDL_MessageBox::SortButtonsPredicate);
   1.281 +
   1.282 +		size_t countButtons = fButtons.size();
   1.283 +		for (size_t i = 0; i < countButtons; ++i)
   1.284 +		{
   1.285 +			switch (fButtons[i]->flags)
   1.286 +			{
   1.287 +				case SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT:
   1.288 +				{
   1.289 +					fCloseButton = static_cast<int>(i);
   1.290 +					break;
   1.291 +				}
   1.292 +				case SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT:
   1.293 +				{
   1.294 +					fDefaultButton = static_cast<int>(i);
   1.295 +					break;
   1.296 +				}
   1.297 +				default:
   1.298 +				{
   1.299 +					break;
   1.300 +				}
   1.301 +			}
   1.302 +			AddButton(fButtons[i]->text);
   1.303 +		}
   1.304 +
   1.305 +		SetDefaultButton(ButtonAt(fDefaultButton));
   1.306 +	}
   1.307 +
   1.308 +public:
   1.309 +	explicit
   1.310 +	BE_SDL_MessageBox(const SDL_MessageBoxData *aMessageBoxData)
   1.311 +		: BAlert(NULL, NULL, NULL, NULL, NULL, B_WIDTH_FROM_LABEL, B_WARNING_ALERT),
   1.312 +		  fComputedMessageBoxWidth(0.0f),
   1.313 +		  fCloseButton(G_CLOSE_BUTTON_ID), fDefaultButton(G_DEFAULT_BUTTON_ID),
   1.314 +		  fCustomColorScheme(false), fThereIsLongLine(false),
   1.315 +		  BE_SDL_DefTitle("SDL2 MessageBox"),
   1.316 +		  BE_SDL_DefMessage("Some information has been lost."),
   1.317 +		  BE_SDL_DefButton("OK")
   1.318 +	{
   1.319 +		// MessageBox settings.
   1.320 +		// We need a title to display it.
   1.321 +		SetLook(B_TITLED_WINDOW_LOOK);
   1.322 +		SetFlags(Flags() | B_CLOSE_ON_ESCAPE);
   1.323 +
   1.324 +		// MessageBox TextView settings.
   1.325 +		fMessageBoxTextView = TextView();
   1.326 +		fMessageBoxTextView->SetWordWrap(false);
   1.327 +		fMessageBoxTextView->SetStylable(true);
   1.328 +
   1.329 +		ParseSdlMessageBoxData(aMessageBoxData);
   1.330 +	}
   1.331 +
   1.332 +	int
   1.333 +	GetCloseButtonId(void) const
   1.334 +	{
   1.335 +		return fCloseButton;
   1.336 +	}
   1.337 +
   1.338 +	virtual
   1.339 +	~BE_SDL_MessageBox(void)
   1.340 +	{
   1.341 +		fButtons.clear();
   1.342 +	}
   1.343 +
   1.344 +protected:
   1.345 +	virtual void
   1.346 +	FrameResized(float aNewWidth, float aNewHeight)
   1.347 +	{
   1.348 +		if (fComputedMessageBoxWidth > aNewWidth)
   1.349 +		{
   1.350 +			ResizeTo(fComputedMessageBoxWidth, aNewHeight);
   1.351 +		}
   1.352 +		else
   1.353 +		{
   1.354 +			BAlert::FrameResized(aNewWidth, aNewHeight);
   1.355 +		}
   1.356 +	}
   1.357 +
   1.358 +	virtual void
   1.359 +	SetTitle(const char* aTitle)
   1.360 +	{
   1.361 +		fTitle = aTitle;
   1.362 +		BAlert::SetTitle(aTitle);
   1.363 +	}
   1.364 +};
   1.365 +
   1.366 +#ifdef __cplusplus
   1.367 +extern "C" {
   1.368 +#endif
   1.369 +
   1.370 +int
   1.371 +BE_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
   1.372 +{
   1.373 +	// Initialize button by closed or error value first.
   1.374 +	*buttonid = G_CLOSE_BUTTON_ID;
   1.375 +
   1.376 +	// We need to check "be_app" pointer to "NULL". The "messageboxdata->window" pointer isn't appropriate here
   1.377 +	// because it is possible to create a MessageBox from another thread. This fixes the following errors:
   1.378 +	// "You need a valid BApplication object before interacting with the app_server."
   1.379 +	// "2 BApplication objects were created. Only one is allowed."
   1.380 +	BApplication *application = NULL;
   1.381 +	if (be_app == NULL)
   1.382 +	{
   1.383 +		application = new(std::nothrow) BApplication(signature);
   1.384 +		if (application == NULL)
   1.385 +		{
   1.386 +			return SDL_SetError("Cannot create the BApplication object. Lack of memory?");
   1.387 +		}
   1.388 +	}
   1.389 +
   1.390 +	BE_SDL_MessageBox *SDL_MessageBox = new(std::nothrow) BE_SDL_MessageBox(messageboxdata);
   1.391 +	if (SDL_MessageBox == NULL)
   1.392 +	{
   1.393 +		return SDL_SetError("Cannot create the BE_SDL_MessageBox (BAlert inheritor) object. Lack of memory?");
   1.394 +	}
   1.395 +	const int closeButton = SDL_MessageBox->GetCloseButtonId();
   1.396 +	int pushedButton = SDL_MessageBox->Go();
   1.397 +
   1.398 +	// The close button is equivalent to pressing Escape.
   1.399 +	if (closeButton != G_CLOSE_BUTTON_ID && pushedButton == G_CLOSE_BUTTON_ID)
   1.400 +	{
   1.401 +		pushedButton = closeButton;
   1.402 +	}
   1.403 +
   1.404 +	// It's deleted by itself after the "Go()" method was executed.
   1.405 +	/*
   1.406 +	if (messageBox != NULL)
   1.407 +	{
   1.408 +		delete messageBox;
   1.409 +	}
   1.410 +	*/
   1.411 +	if (application != NULL)
   1.412 +	{
   1.413 +		delete application;
   1.414 +	}
   1.415 +
   1.416 +	// Initialize button by real pushed value then.
   1.417 +	*buttonid = pushedButton;
   1.418 +
   1.419 +	return 0;
   1.420 +}
   1.421 +
   1.422 +#ifdef __cplusplus
   1.423 +}
   1.424 +#endif
   1.425 +
   1.426 +#endif /* SDL_VIDEO_DRIVER_HAIKU */
   1.427 +
   1.428 +/* vi: set ts=4 sw=4 expandtab: */