Skip to content

Commit

Permalink
LibGUI: Add a simple GGroupBox widget.
Browse files Browse the repository at this point in the history
This needs some work on the visual side, but it gets the job done already.
  • Loading branch information
awesomekling committed Apr 10, 2019
1 parent b980c32 commit f6543c5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
22 changes: 13 additions & 9 deletions Applications/FontEditor/FontEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <LibGUI/GTextBox.h>
#include <LibGUI/GCheckBox.h>
#include <LibGUI/GSpinBox.h>
#include <LibGUI/GGroupBox.h>
#include <stdlib.h>

FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& edited_font, GWidget* parent)
Expand All @@ -26,36 +27,39 @@ FontEditorWidget::FontEditorWidget(const String& path, RetainPtr<Font>&& edited_
m_glyph_editor_widget = new GlyphEditorWidget(*m_edited_font, this);
m_glyph_editor_widget->move_to({ 5, 5 });

auto* fixed_width_checkbox = new GCheckBox(this);
fixed_width_checkbox->set_caption("Fixed width");
fixed_width_checkbox->set_checked(m_edited_font->is_fixed_width());
fixed_width_checkbox->set_relative_rect({ 5, 195, 100, 20 });
auto* font_group_box = new GGroupBox("Font metadata", this);
font_group_box->set_relative_rect(5, 195, 210, 70);

m_name_textbox = new GTextBox(this);
m_name_textbox->set_relative_rect({ 5, 220, 300, 20 });
m_name_textbox = new GTextBox(font_group_box);
m_name_textbox->set_relative_rect(10, 20, 180, 20);
m_name_textbox->set_text(m_edited_font->name());
m_name_textbox->on_change = [this] {
m_edited_font->set_name(m_name_textbox->text());
};

auto* fixed_width_checkbox = new GCheckBox(font_group_box);
fixed_width_checkbox->set_relative_rect(10, 45, 190, 20);
fixed_width_checkbox->set_caption("Fixed width");
fixed_width_checkbox->set_checked(m_edited_font->is_fixed_width());

m_path_textbox = new GTextBox(this);
m_path_textbox->set_relative_rect({ 5, 245, 300, 20 });
m_path_textbox->set_relative_rect(5, 270, 210, 20);
m_path_textbox->set_text(m_path);
m_path_textbox->on_change = [this] {
m_path = m_path_textbox->text();
};

auto* save_button = new GButton(this);
save_button->set_caption("Save");
save_button->set_relative_rect({ 5, 270, 100, 20 });
save_button->set_relative_rect({ 5, 300, 105, 20 });
save_button->on_click = [this] (GButton&) {
dbgprintf("write to file: '%s'\n", m_path.characters());
m_edited_font->write_to_file(m_path);
};

auto* quit_button = new GButton(this);
quit_button->set_caption("Quit");
quit_button->set_relative_rect({ 110, 270, 100, 20 });
quit_button->set_relative_rect({ 110, 300, 105, 20 });
quit_button->on_click = [] (GButton&) {
exit(0);
};
Expand Down
2 changes: 1 addition & 1 deletion Applications/FontEditor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int main(int argc, char** argv)

auto* window = new GWindow;
window->set_title("FontEditor");
window->set_rect({ 50, 50, 390, 295 });
window->set_rect({ 50, 50, 390, 325 });
auto* font_editor = new FontEditorWidget(path, move(edited_font));
window->set_main_widget(font_editor);
window->set_should_exit_event_loop_on_close(true);
Expand Down
31 changes: 31 additions & 0 deletions LibGUI/GGroupBox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <LibGUI/GGroupBox.h>
#include <LibGUI/GPainter.h>
#include <SharedGraphics/StylePainter.h>

GGroupBox::GGroupBox(const String& name, GWidget* parent)
: GWidget(parent)
, m_name(name)
{
set_fill_with_background_color(true);
set_background_color(Color::LightGray);
}

GGroupBox::~GGroupBox()
{
}

void GGroupBox::paint_event(GPaintEvent& event)
{
GPainter painter(*this);
painter.add_clip_rect(event.rect());

Rect frame_rect {
0, font().glyph_height() / 2,
width(), height() - font().glyph_height() / 2
};
StylePainter::paint_frame(painter, frame_rect, FrameShape::Panel, FrameShadow::Sunken, 1);

Rect text_rect { 4, 0, font().width(m_name) + 6, font().glyph_height() };
painter.fill_rect(text_rect, background_color());
painter.draw_text(text_rect, m_name, TextAlignment::Center, foreground_color());
}
19 changes: 19 additions & 0 deletions LibGUI/GGroupBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <LibGUI/GWidget.h>

class GGroupBox : public GWidget {
public:
GGroupBox(const String& name, GWidget* parent);
virtual ~GGroupBox() override;

String name() const { return m_name; }

virtual const char* class_name() const override { return "GGroupBox"; }

protected:
virtual void paint_event(GPaintEvent&) override;

private:
String m_name;
};
1 change: 1 addition & 0 deletions LibGUI/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ LIBGUI_OBJS = \
GHttpResponse.o \
GHttpJob.o \
GSpinBox.o \
GGroupBox.o \
GWindow.o

OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)
Expand Down

0 comments on commit f6543c5

Please sign in to comment.