Skip to content
Closed
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
2 changes: 1 addition & 1 deletion cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ FetchContent_MakeAvailable(googletest)
find_package(PkgConfig REQUIRED)

# Check for gtkmm-4.0 specifically, not just gtk4
pkg_check_modules(GTKMM REQUIRED gtkmm-4.0)
pkg_check_modules(GTKMM REQUIRED gtkmm-4.0)
2 changes: 1 addition & 1 deletion src/ap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ target_sources(ap
target_link_libraries(ap PRIVATE gtk4-settings)

add_subdirectory(mvc)
# add_subdirectory(mvvm)
# add_subdirectory(mvvm)
6 changes: 3 additions & 3 deletions src/ap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ View → Controller → Model

### 4. Examples
### 4.1. simple_ap
- Cos:
- Pros:
- Quick, Simple
- Pos:
- Cons:
- Dependency: e.g. what happen when we delete Gtk::Label m_labelMonitorA;
- Scalability:
- Reusability:

### 4.2. mvc_ap
### 4.2. mvc_ap
2 changes: 1 addition & 1 deletion src/ap/mvc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ target_sources(mvc_ap
controller/Controller.cpp
)

target_link_libraries(mvc_ap PRIVATE gtk4-settings)
target_link_libraries(mvc_ap PRIVATE gtk4-settings)
3 changes: 2 additions & 1 deletion src/ap/mvc/IObserver.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include <string>

class IObserver {
public:
virtual ~IObserver() = default;
virtual void onDataChanged(const std::string& newData) = 0;
};
};
2 changes: 1 addition & 1 deletion src/ap/mvc/controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ void Controller::updateRequest(const std::string& text) {
// the Controller updates the Model when receiving new data from the View,
// which then notifies all Views of the change
model_->setData(text);
}
}
2 changes: 1 addition & 1 deletion src/ap/mvc/controller/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ class Controller {

private:
std::shared_ptr<SharedData> model_;
};
};
2 changes: 1 addition & 1 deletion src/ap/mvc/model/SharedData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ void SharedData::addObserver(IObserver* obs) {

std::string SharedData::getData() const {
return data_;
}
}
2 changes: 1 addition & 1 deletion src/ap/mvc/model/SharedData.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class SharedData {
private:
std::string data_;
std::vector<IObserver*> observers_;
};
};
2 changes: 1 addition & 1 deletion src/ap/mvc/mvc_ap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ ContainerWindow::ContainerWindow()
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create("org.gtkmm.example.singlemvc");
return app->make_window_and_run<ContainerWindow>(argc, argv);
}
}
11 changes: 6 additions & 5 deletions src/ap/mvc/view/DisplayWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
DisplayWidget::DisplayWidget(const std::string& title, const std::string& color,
const std::string& startData)
: Gtk::Box(Gtk::Orientation::VERTICAL),
color_(color),
innerBox_(Gtk::Orientation::VERTICAL) {
innerBox_(Gtk::Orientation::VERTICAL),
color_(color) {
frame_.set_label(title);
frame_.set_margin(10);

Expand All @@ -18,12 +18,13 @@ DisplayWidget::DisplayWidget(const std::string& title, const std::string& color,
}

void DisplayWidget::updateLabel(const std::string& text) {
// Use HTML markup to change text color
// Escape text to prevent Pango markup injection
auto escaped = Glib::Markup::escape_text(text);
std::string markup = "<span foreground='" + color_ +
"' size='x-large' weight='bold'>" + text + "</span>";
"' size='x-large' weight='bold'>" + escaped + "</span>";
labelData_.set_markup(markup);
}

void DisplayWidget::onDataChanged(const std::string& newData) {
updateLabel(newData);
}
}
5 changes: 3 additions & 2 deletions src/ap/mvc/view/DisplayWidget.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <gtkmm.h>
#include "../IObserver.h"
#include "../controller/Controller.h"

class DisplayWidget : public Gtk::Box, public IObserver {
public:
DisplayWidget(const std::string& title, const std::string& color,
Expand All @@ -14,4 +15,4 @@ class DisplayWidget : public Gtk::Box, public IObserver {
Gtk::Box innerBox_;
Gtk::Label labelData_;
std::string color_;
};
};
18 changes: 7 additions & 11 deletions src/ap/mvc/view/EditorWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "EditorWidget.h"
#include <iostream>

EditorWidget::EditorWidget(std::shared_ptr<Controller> c,
const std::string& initData)
: Gtk::Box(Gtk::Orientation::VERTICAL),
controller_(c),
innerBox_(Gtk::Orientation::VERTICAL) {
innerBox_(Gtk::Orientation::VERTICAL),
controller_(c) {
// Create a beautiful border
frame_.set_label("ZONE 1: EDITOR (Input View)");
frame_.set_margin(10);
Expand All @@ -22,18 +22,14 @@ EditorWidget::EditorWidget(std::shared_ptr<Controller> c,

frame_.set_child(innerBox_);
this->append(frame_); // Add a frame to the main Box of this class
controller_ = c;

// MVC logic to help the Controller receive update input from the View
button_.signal_clicked().connect([this]() {
std::cout << "1\n";
controller_->updateRequest(entry_.get_text());
std::cout << "2\n";
});
button_.signal_clicked().connect(
[this]() { controller_->updateRequest(entry_.get_text()); });
}

void EditorWidget::onDataChanged(const std::string& newData) {
// Fix string comparison error or do not thing ?
if (entry_.get_text() != Glib::ustring(newData)) {
entry_.set_text(newData);
}
}
}
2 changes: 1 addition & 1 deletion src/ap/mvc/view/EditorWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class EditorWidget : public Gtk::Box, public IObserver {
Gtk::Button button_;

std::shared_ptr<Controller> controller_;
};
};
9 changes: 6 additions & 3 deletions src/ap/simple_ap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ class MainWindow : public Gtk::Window {
if (text.empty())
return;

// Escape text to prevent Pango markup injection
auto escaped = Glib::Markup::escape_text(text);

// Step 2: Update Monitor A (Hard-coded) directly
m_labelMonitorA.set_markup("<span foreground='blue' size='x-large'>" +
text + "</span>");
escaped + "</span>");

// Step 3: Update Monitor B (Hard-coded) directly
m_labelMonitorB.set_markup("<span foreground='red' size='x-large'>" +
text + "</span>");
escaped + "</span>");
std::cout << "Updated directly without Model!" << std::endl;
});
}
Expand All @@ -102,4 +105,4 @@ class MainWindow : public Gtk::Window {
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create("org.gtkmm.example.nomvc");
return app->make_window_and_run<MainWindow>(argc, argv);
}
}