src/video/haiku/SDL_bmessagebox.cc
changeset 13238 007002587d5d
parent 13230 57ed423da32a
child 13422 fd6a12de91c7
equal deleted inserted replaced
13237:f5298e605071 13238:007002587d5d
     1 /*
     1 /*
     2   Simple DirectMedia Layer
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     3   Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
     4   Copyright (C) 2018 EXL <exlmotodev@gmail.com>
     4   Copyright (C) 2018-2019 EXL <exlmotodev@gmail.com>
     5 
     5 
     6   This software is provided 'as-is', without any express or implied
     6   This software is provided 'as-is', without any express or implied
     7   warranty.  In no event will the authors be held liable for any damages
     7   warranty.  In no event will the authors be held liable for any damages
     8   arising from the use of this software.
     8   arising from the use of this software.
     9 
     9 
    52 	G_CLOSE_BUTTON_ID   = -1,
    52 	G_CLOSE_BUTTON_ID   = -1,
    53 	G_DEFAULT_BUTTON_ID = 0,
    53 	G_DEFAULT_BUTTON_ID = 0,
    54 	G_MAX_STRING_LENGTH_BYTES = 120
    54 	G_MAX_STRING_LENGTH_BYTES = 120
    55 };
    55 };
    56 
    56 
    57 class BE_SDL_MessageBox : public BAlert
    57 class HAIKU_SDL_MessageBox : public BAlert
    58 {
    58 {
    59 	float fComputedMessageBoxWidth;
    59 	float fComputedMessageBoxWidth;
    60 
    60 
    61 	BTextView *fMessageBoxTextView;
    61 	BTextView *fMessageBoxTextView;
    62 
    62 
    66 	bool fCustomColorScheme;
    66 	bool fCustomColorScheme;
    67 	bool fThereIsLongLine;
    67 	bool fThereIsLongLine;
    68 	rgb_color fTextColor;
    68 	rgb_color fTextColor;
    69 
    69 
    70 	const char *fTitle;
    70 	const char *fTitle;
    71 	const char *BE_SDL_DefTitle;
    71 	const char *HAIKU_SDL_DefTitle;
    72 	const char *BE_SDL_DefMessage;
    72 	const char *HAIKU_SDL_DefMessage;
    73 	const char *BE_SDL_DefButton;
    73 	const char *HAIKU_SDL_DefButton;
    74 
    74 
    75 	std::vector<const SDL_MessageBoxButtonData *> fButtons;
    75 	std::vector<const SDL_MessageBoxButtonData *> fButtons;
    76 
    76 
    77 	static bool
    77 	static bool
    78 	SortButtonsPredicate(const SDL_MessageBoxButtonData *aButtonLeft,
    78 	SortButtonsPredicate(const SDL_MessageBoxButtonData *aButtonLeft,
   131 	void
   131 	void
   132 	ParseSdlMessageBoxData(const SDL_MessageBoxData *aMessageBoxData)
   132 	ParseSdlMessageBoxData(const SDL_MessageBoxData *aMessageBoxData)
   133 	{
   133 	{
   134 		if (aMessageBoxData == NULL)
   134 		if (aMessageBoxData == NULL)
   135 		{
   135 		{
   136 			SetTitle(BE_SDL_DefTitle);
   136 			SetTitle(HAIKU_SDL_DefTitle);
   137 			SetMessageText(BE_SDL_DefMessage);
   137 			SetMessageText(HAIKU_SDL_DefMessage);
   138 			AddButton(BE_SDL_DefButton);
   138 			AddButton(HAIKU_SDL_DefButton);
   139 			return;
   139 			return;
   140 		}
   140 		}
   141 
   141 
   142 		if (aMessageBoxData->numbuttons <= 0)
   142 		if (aMessageBoxData->numbuttons <= 0)
   143 		{
   143 		{
   144 			AddButton(BE_SDL_DefButton);
   144 			AddButton(HAIKU_SDL_DefButton);
   145 		}
   145 		}
   146 		else
   146 		else
   147 		{
   147 		{
   148 			AddSdlButtons(aMessageBoxData->buttons, aMessageBoxData->numbuttons);
   148 			AddSdlButtons(aMessageBoxData->buttons, aMessageBoxData->numbuttons);
   149 		}
   149 		}
   153 			fCustomColorScheme = true;
   153 			fCustomColorScheme = true;
   154 			ApplyAndParseColorScheme(aMessageBoxData->colorScheme);
   154 			ApplyAndParseColorScheme(aMessageBoxData->colorScheme);
   155 		}
   155 		}
   156 
   156 
   157 		(aMessageBoxData->title != NULL) ?
   157 		(aMessageBoxData->title != NULL) ?
   158 			SetTitle(aMessageBoxData->title) : SetTitle(BE_SDL_DefTitle);
   158 			SetTitle(aMessageBoxData->title) : SetTitle(HAIKU_SDL_DefTitle);
   159 		(aMessageBoxData->message != NULL) ?
   159 		(aMessageBoxData->message != NULL) ?
   160 			SetMessageText(aMessageBoxData->message) : SetMessageText(BE_SDL_DefMessage);
   160 			SetMessageText(aMessageBoxData->message) : SetMessageText(HAIKU_SDL_DefMessage);
   161 
   161 
   162 		SetType(ConvertMessageBoxType(static_cast<SDL_MessageBoxFlags>(aMessageBoxData->flags)));
   162 		SetType(ConvertMessageBoxType(static_cast<SDL_MessageBoxFlags>(aMessageBoxData->flags)));
   163 	}
   163 	}
   164 
   164 
   165 	void
   165 	void
   272 		for (int i = 0; i < aNumButtons; ++i)
   272 		for (int i = 0; i < aNumButtons; ++i)
   273 		{
   273 		{
   274 			fButtons.push_back(&aButtons[i]);
   274 			fButtons.push_back(&aButtons[i]);
   275 		}
   275 		}
   276 
   276 
   277 		std::sort(fButtons.begin(), fButtons.end(), &BE_SDL_MessageBox::SortButtonsPredicate);
   277 		std::sort(fButtons.begin(), fButtons.end(), &HAIKU_SDL_MessageBox::SortButtonsPredicate);
   278 
   278 
   279 		size_t countButtons = fButtons.size();
   279 		size_t countButtons = fButtons.size();
   280 		for (size_t i = 0; i < countButtons; ++i)
   280 		for (size_t i = 0; i < countButtons; ++i)
   281 		{
   281 		{
   282 			switch (fButtons[i]->flags)
   282 			switch (fButtons[i]->flags)
   302 		SetDefaultButton(ButtonAt(fDefaultButton));
   302 		SetDefaultButton(ButtonAt(fDefaultButton));
   303 	}
   303 	}
   304 
   304 
   305 public:
   305 public:
   306 	explicit
   306 	explicit
   307 	BE_SDL_MessageBox(const SDL_MessageBoxData *aMessageBoxData)
   307 	HAIKU_SDL_MessageBox(const SDL_MessageBoxData *aMessageBoxData)
   308 		: BAlert(NULL, NULL, NULL, NULL, NULL, B_WIDTH_FROM_LABEL, B_WARNING_ALERT),
   308 		: BAlert(NULL, NULL, NULL, NULL, NULL, B_WIDTH_FROM_LABEL, B_WARNING_ALERT),
   309 		  fComputedMessageBoxWidth(0.0f),
   309 		  fComputedMessageBoxWidth(0.0f),
   310 		  fCloseButton(G_CLOSE_BUTTON_ID), fDefaultButton(G_DEFAULT_BUTTON_ID),
   310 		  fCloseButton(G_CLOSE_BUTTON_ID), fDefaultButton(G_DEFAULT_BUTTON_ID),
   311 		  fCustomColorScheme(false), fThereIsLongLine(false),
   311 		  fCustomColorScheme(false), fThereIsLongLine(false),
   312 		  BE_SDL_DefTitle("SDL2 MessageBox"),
   312 		  HAIKU_SDL_DefTitle("SDL2 MessageBox"),
   313 		  BE_SDL_DefMessage("Some information has been lost."),
   313 		  HAIKU_SDL_DefMessage("Some information has been lost."),
   314 		  BE_SDL_DefButton("OK")
   314 		  HAIKU_SDL_DefButton("OK")
   315 	{
   315 	{
   316 		// MessageBox settings.
   316 		// MessageBox settings.
   317 		// We need a title to display it.
   317 		// We need a title to display it.
   318 		SetLook(B_TITLED_WINDOW_LOOK);
   318 		SetLook(B_TITLED_WINDOW_LOOK);
   319 		SetFlags(Flags() | B_CLOSE_ON_ESCAPE);
   319 		SetFlags(Flags() | B_CLOSE_ON_ESCAPE);
   331 	{
   331 	{
   332 		return fCloseButton;
   332 		return fCloseButton;
   333 	}
   333 	}
   334 
   334 
   335 	virtual
   335 	virtual
   336 	~BE_SDL_MessageBox(void)
   336 	~HAIKU_SDL_MessageBox(void)
   337 	{
   337 	{
   338 		fButtons.clear();
   338 		fButtons.clear();
   339 	}
   339 	}
   340 
   340 
   341 protected:
   341 protected:
   363 #ifdef __cplusplus
   363 #ifdef __cplusplus
   364 extern "C" {
   364 extern "C" {
   365 #endif
   365 #endif
   366 
   366 
   367 int
   367 int
   368 BE_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
   368 HAIKU_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
   369 {
   369 {
   370 	// Initialize button by closed or error value first.
   370 	// Initialize button by closed or error value first.
   371 	*buttonid = G_CLOSE_BUTTON_ID;
   371 	*buttonid = G_CLOSE_BUTTON_ID;
   372 
   372 
   373 	// We need to check "be_app" pointer to "NULL". The "messageboxdata->window" pointer isn't appropriate here
   373 	// We need to check "be_app" pointer to "NULL". The "messageboxdata->window" pointer isn't appropriate here
   382 		{
   382 		{
   383 			return SDL_SetError("Cannot create the BApplication object. Lack of memory?");
   383 			return SDL_SetError("Cannot create the BApplication object. Lack of memory?");
   384 		}
   384 		}
   385 	}
   385 	}
   386 
   386 
   387 	BE_SDL_MessageBox *SDL_MessageBox = new(std::nothrow) BE_SDL_MessageBox(messageboxdata);
   387 	HAIKU_SDL_MessageBox *SDL_MessageBox = new(std::nothrow) HAIKU_SDL_MessageBox(messageboxdata);
   388 	if (SDL_MessageBox == NULL)
   388 	if (SDL_MessageBox == NULL)
   389 	{
   389 	{
   390 		return SDL_SetError("Cannot create the BE_SDL_MessageBox (BAlert inheritor) object. Lack of memory?");
   390 		return SDL_SetError("Cannot create the HAIKU_SDL_MessageBox (BAlert inheritor) object. Lack of memory?");
   391 	}
   391 	}
   392 	const int closeButton = SDL_MessageBox->GetCloseButtonId();
   392 	const int closeButton = SDL_MessageBox->GetCloseButtonId();
   393 	int pushedButton = SDL_MessageBox->Go();
   393 	int pushedButton = SDL_MessageBox->Go();
   394 
   394 
   395 	// The close button is equivalent to pressing Escape.
   395 	// The close button is equivalent to pressing Escape.