Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message box #107

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/widgets_example.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ namespace WidgetsExample
progress->setCaption("Loading");
progress->setWidth(100);

std::vector<std::string> button_names{"0", "25", "50", "75", "100", "-1", "+1"};
messageBox = std::make_unique<gcn::MessageBox>("Change progression",
"Set progression value",
button_names);
messageBox->setMovable(true);
messageBox->setFrameSize(1);
messageBox->addActionListener(this);

guisanLogoImage.reset(gcn::Image::load("guisan-logo.png"));
guisanLogoIcon = std::make_unique<gcn::Icon>(guisanLogoImage.get());
window->add(guisanLogoIcon.get());
Expand Down Expand Up @@ -138,6 +146,7 @@ namespace WidgetsExample
top->add(nestedScrollArea.get(), 440, 350);

top->add(progress.get(), 580, 200);
top->add(messageBox.get(), 480, 220);
}

~MainContainer() = default;
Expand All @@ -159,6 +168,20 @@ namespace WidgetsExample
label->adjustSize();
}
}
else if (actionEvent.getSource() == messageBox.get())
{
switch (messageBox->getClickedButton())
{
default:
case 0: progress->setValue(0); break;
case 1: progress->setValue(25); break;
case 2: progress->setValue(50); break;
case 3: progress->setValue(75); break;
case 4: progress->setValue(100); break;
case 5: progress->setValue(progress->getValue() - 1); break;
case 6: progress->setValue(progress->getValue() + 1); break;
}
}
}

private:
Expand Down Expand Up @@ -186,6 +209,7 @@ namespace WidgetsExample
std::unique_ptr<gcn::Slider> slider; // A slider
std::unique_ptr<gcn::Image> image; // An image for the icon
std::unique_ptr<gcn::Window> window;
std::unique_ptr<gcn::MessageBox> messageBox;
std::unique_ptr<gcn::ProgressBar> progress;
std::unique_ptr<gcn::Image> guisanLogoImage;
std::unique_ptr<gcn::Icon> guisanLogoIcon;
Expand Down
126 changes: 29 additions & 97 deletions include/guisan/widgets/messagebox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@
#ifndef GCN_MESSAGEBOX_HPP
#define GCN_MESSAGEBOX_HPP

#include <string>

#include "guisan/graphics.hpp"
#include "guisan/mouselistener.hpp"
#include "guisan/platform.hpp"
#include "guisan/widgets/window.hpp"
#include "guisan/widgets/button.hpp"
#include "guisan/widgets/label.hpp"
#include "guisan/widgets/icon.hpp"
#include "guisan/widgets/window.hpp"

#include <memory>
#include <string>
#include <vector>

namespace gcn
{
Expand All @@ -75,25 +74,38 @@ namespace gcn
class GCN_CORE_DECLSPEC MessageBox : public Window
{
public:

/**
* Constructor.
* This version only has a single button labeled "OK".
* This version only has a single button labeled "OK".
*
* @param caption the MessageBox caption.
* @param message the message to display in the MessageBox
*/
MessageBox(const std::string& caption, const std::string& message);

/**
* Constructor.
*
* @param caption the MessageBox caption.
* @param message the message to display in the MessageBox
* @param buttons strings to display as button captions
* @param button_captions strings to display as button captions
* @param size length of the buttons array
*/
MessageBox(const std::string& caption, const std::string& message, const std::string *buttons, int size);
MessageBox(const std::string& caption,
const std::string& message,
const std::string* button_captions,
int size);

/**
* Constructor.
*
* @param caption the MessageBox caption.
* @param message the message to display in the MessageBox
* @param button_captions strings to display as button captions
*/
MessageBox(const std::string& caption,
const std::string& message,
const std::vector<std::string>& button_captions);

/**
* Destructor.
Expand All @@ -107,20 +119,6 @@ namespace gcn
*/
int getClickedButton() const;

/**
* Sets the MessageBox caption.
*
* @param caption the MessageBox caption.
*/
void setCaption(const std::string& caption);

/**
* Gets the MessageBox caption.
*
* @return the MessageBox caption.
*/
const std::string& getCaption() const;

/**
* Sets the position for the button(s) in the MessageBox.
*
Expand All @@ -136,91 +134,25 @@ namespace gcn
Graphics::Alignment getButtonAlignment() const;

/**
* Sets the padding of the window which is the distance between the
* window border and the content.
*
* @param padding the padding value.
*/
void setPadding(unsigned int padding);

/**
* Gets the padding.
*
* @return the padding value.
*/
unsigned int getPadding() const;

/**
* Sets the title bar height.
*
* @param height the title height value.
*/
void setTitleBarHeight(unsigned int height);

/**
* Gets the title bar height.
*
* @return the title bar height.
*/
unsigned int getTitleBarHeight();

/**
* Check if the window is movable.
*
* @return true or false.
*/
bool isMovable() const;

/**
* Sets the MessageBox to be opaque. If it's not opaque, the content area
* will not be filled with a color.
*
* @param opaque true or false.
*/
void setOpaque(bool opaque);

/**
* Checks if the MessageBox is opaque.
*
* @return true or false.
*/
bool isOpaque();

/**
* Add this MessageBox to a parent container, centered both horizontally and vertically
* If instead, you want to place it somewhere else, use Container::add().
* Add this MessageBox to a parent container,
* centered both horizontally and vertically.
* If instead, you want to place it somewhere else, use Container::add().
*
* @param container parent container
*/
void addToContainer(Container* container);

// Inherit from Window

/**
* Resizes the container to fit the content exactly.
*/
void resizeToContent() override;

// Inherited from Widget

Rectangle getChildrenArea() override;
void draw(Graphics* graphics) override;

// Inherited from MouseListener

void mousePressed(MouseEvent& mouseEvent) override;
void mouseDragged(MouseEvent& mouseEvent) override;
void mouseReleased(MouseEvent& mouseEvent) override;

protected:
std::string mMessage;
int mNbButtons;
Graphics::Alignment mButtonAlignment = Graphics::Alignment::Left;
int mClickedButton = -1;

Button **mButtons = nullptr;
Label *mLabel = nullptr;
std::vector<std::unique_ptr<Button>> mButtons;
std::unique_ptr<Label> mLabel;
};
}
} // namespace gcn

#endif // end GCN_MESSAGEBOX_HPP
Loading