11b370545SJessica Hamilton /*
21b370545SJessica Hamilton * Copyright 2022, Haiku. All rights reserved.
31b370545SJessica Hamilton * Distributed under the terms of the MIT License.
41b370545SJessica Hamilton */
51b370545SJessica Hamilton
61b370545SJessica Hamilton
71b370545SJessica Hamilton #include "ThemeWindow.h"
81b370545SJessica Hamilton
91b370545SJessica Hamilton #include <stdio.h>
101b370545SJessica Hamilton
111b370545SJessica Hamilton #include <Button.h>
121b370545SJessica Hamilton #include <Catalog.h>
131b370545SJessica Hamilton #include <Directory.h>
141b370545SJessica Hamilton #include <File.h>
151b370545SJessica Hamilton #include <FilePanel.h>
161b370545SJessica Hamilton #include <FindDirectory.h>
171b370545SJessica Hamilton #include <LayoutBuilder.h>
181b370545SJessica Hamilton #include <Locale.h>
191b370545SJessica Hamilton #include <Messenger.h>
201b370545SJessica Hamilton #include <Path.h>
211b370545SJessica Hamilton #include <SeparatorView.h>
221b370545SJessica Hamilton
231b370545SJessica Hamilton #include "PrefHandler.h"
241b370545SJessica Hamilton #include "TermConst.h"
251b370545SJessica Hamilton #include "ThemeView.h"
261b370545SJessica Hamilton
271b370545SJessica Hamilton
281b370545SJessica Hamilton #undef B_TRANSLATION_CONTEXT
291b370545SJessica Hamilton #define B_TRANSLATION_CONTEXT "Terminal ThemeWindow"
301b370545SJessica Hamilton
311b370545SJessica Hamilton // local messages
321b370545SJessica Hamilton const uint32 MSG_DEFAULTS_PRESSED = 'defl';
331b370545SJessica Hamilton const uint32 MSG_SAVEAS_PRESSED = 'canl';
341b370545SJessica Hamilton const uint32 MSG_REVERT_PRESSED = 'revt';
351b370545SJessica Hamilton
361b370545SJessica Hamilton
ThemeWindow(const BMessenger & messenger)371b370545SJessica Hamilton ThemeWindow::ThemeWindow(const BMessenger& messenger)
381b370545SJessica Hamilton :
39*9e76d619SHumdinger BWindow(BRect(0, 0, 0, 0), B_TRANSLATE("Colors"), B_TITLED_WINDOW,
401b370545SJessica Hamilton B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
411b370545SJessica Hamilton fPreviousPref(new PrefHandler(PrefHandler::Default())),
421b370545SJessica Hamilton fSavePanel(NULL),
431b370545SJessica Hamilton fDirty(false),
441b370545SJessica Hamilton fTerminalMessenger(messenger)
451b370545SJessica Hamilton {
461b370545SJessica Hamilton fDefaultsButton = new BButton("defaults", B_TRANSLATE("Defaults"),
471b370545SJessica Hamilton new BMessage(MSG_DEFAULTS_PRESSED), B_WILL_DRAW);
481b370545SJessica Hamilton
491b370545SJessica Hamilton fRevertButton = new BButton("revert", B_TRANSLATE("Revert"),
501b370545SJessica Hamilton new BMessage(MSG_REVERT_PRESSED), B_WILL_DRAW);
511b370545SJessica Hamilton
521b370545SJessica Hamilton fSaveAsFileButton = new BButton("savebutton",
531b370545SJessica Hamilton B_TRANSLATE("Save to file" B_UTF8_ELLIPSIS),
541b370545SJessica Hamilton new BMessage(MSG_SAVEAS_PRESSED), B_WILL_DRAW);
551b370545SJessica Hamilton
56281ece0bSHumdinger fThemeView = new ThemeView("Theme", fTerminalMessenger);
571b370545SJessica Hamilton
581b370545SJessica Hamilton BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
591b370545SJessica Hamilton .SetInsets(B_USE_WINDOW_SPACING)
601b370545SJessica Hamilton .Add(fThemeView)
611b370545SJessica Hamilton .AddGroup(B_HORIZONTAL)
621b370545SJessica Hamilton .Add(fDefaultsButton)
631b370545SJessica Hamilton .Add(fRevertButton)
641b370545SJessica Hamilton .AddGlue()
651b370545SJessica Hamilton .Add(fSaveAsFileButton)
661b370545SJessica Hamilton .SetInsets(0, B_USE_DEFAULT_SPACING, 0, 0);
671b370545SJessica Hamilton
681b370545SJessica Hamilton fRevertButton->SetEnabled(fDirty);
691b370545SJessica Hamilton
701b370545SJessica Hamilton AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
711b370545SJessica Hamilton AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
721b370545SJessica Hamilton
731b370545SJessica Hamilton CenterOnScreen();
741b370545SJessica Hamilton Show();
751b370545SJessica Hamilton }
761b370545SJessica Hamilton
771b370545SJessica Hamilton
781b370545SJessica Hamilton void
Quit()791b370545SJessica Hamilton ThemeWindow::Quit()
801b370545SJessica Hamilton {
811b370545SJessica Hamilton fTerminalMessenger.SendMessage(MSG_THEME_CLOSED);
821b370545SJessica Hamilton delete fPreviousPref;
831b370545SJessica Hamilton delete fSavePanel;
841b370545SJessica Hamilton BWindow::Quit();
851b370545SJessica Hamilton }
861b370545SJessica Hamilton
871b370545SJessica Hamilton
881b370545SJessica Hamilton bool
QuitRequested()891b370545SJessica Hamilton ThemeWindow::QuitRequested()
901b370545SJessica Hamilton {
911b370545SJessica Hamilton if (fDirty)
921b370545SJessica Hamilton _Save();
931b370545SJessica Hamilton
941b370545SJessica Hamilton return true;
951b370545SJessica Hamilton }
961b370545SJessica Hamilton
971b370545SJessica Hamilton
981b370545SJessica Hamilton void
_SaveAs()991b370545SJessica Hamilton ThemeWindow::_SaveAs()
1001b370545SJessica Hamilton {
1011b370545SJessica Hamilton if (!fSavePanel) {
1021b370545SJessica Hamilton BMessenger messenger(this);
1031b370545SJessica Hamilton fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger);
1041b370545SJessica Hamilton }
1051b370545SJessica Hamilton
1061b370545SJessica Hamilton BPath path;
1071b370545SJessica Hamilton if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
1081b370545SJessica Hamilton path.Append("Terminal/Themes");
1091b370545SJessica Hamilton create_directory(path.Path(), 0755);
1101b370545SJessica Hamilton fSavePanel->SetPanelDirectory(path.Path());
1111b370545SJessica Hamilton }
1121b370545SJessica Hamilton
1131b370545SJessica Hamilton fSavePanel->Show();
1141b370545SJessica Hamilton }
1151b370545SJessica Hamilton
1161b370545SJessica Hamilton
1171b370545SJessica Hamilton void
_SaveRequested(BMessage * msg)1181b370545SJessica Hamilton ThemeWindow::_SaveRequested(BMessage *msg)
1191b370545SJessica Hamilton {
1201b370545SJessica Hamilton entry_ref dirref;
1211b370545SJessica Hamilton const char* filename;
1221b370545SJessica Hamilton
1231b370545SJessica Hamilton msg->FindRef("directory", &dirref);
1241b370545SJessica Hamilton msg->FindString("name", &filename);
1251b370545SJessica Hamilton
1261b370545SJessica Hamilton BDirectory dir(&dirref);
1271b370545SJessica Hamilton BPath path(&dir, filename);
1281b370545SJessica Hamilton
1291b370545SJessica Hamilton PrefHandler *prefHandler = PrefHandler::Default();
1301b370545SJessica Hamilton
1311b370545SJessica Hamilton BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
1321b370545SJessica Hamilton char buffer[512];
1331b370545SJessica Hamilton
1341b370545SJessica Hamilton for (const char** table = ThemeView::kColorTable; *table != NULL; ++table) {
1351b370545SJessica Hamilton int len = snprintf(buffer, sizeof(buffer), "\"%s\" , \"%s\"\n",
1361b370545SJessica Hamilton *table, prefHandler->getString(*table));
1371b370545SJessica Hamilton file.Write(buffer, len);
1381b370545SJessica Hamilton }
1391b370545SJessica Hamilton
1401b370545SJessica Hamilton // Name the theme after the filename
1411b370545SJessica Hamilton int len = snprintf(buffer, sizeof(buffer), "\"%s\" , \"%s\"\n",
1421b370545SJessica Hamilton PREF_THEME_NAME, filename);
1431b370545SJessica Hamilton file.Write(buffer, len);
1441b370545SJessica Hamilton
1451b370545SJessica Hamilton fThemeView->UpdateMenu();
1461b370545SJessica Hamilton }
1471b370545SJessica Hamilton
1481b370545SJessica Hamilton
1491b370545SJessica Hamilton void
_Save()1501b370545SJessica Hamilton ThemeWindow::_Save()
1511b370545SJessica Hamilton {
1521b370545SJessica Hamilton delete fPreviousPref;
1531b370545SJessica Hamilton fPreviousPref = new PrefHandler(PrefHandler::Default());
1541b370545SJessica Hamilton
1551b370545SJessica Hamilton PrefHandler::Default()->SaveDefaultAsText();
1561b370545SJessica Hamilton fDirty = false;
1571b370545SJessica Hamilton }
1581b370545SJessica Hamilton
1591b370545SJessica Hamilton
1601b370545SJessica Hamilton void
_Revert()1611b370545SJessica Hamilton ThemeWindow::_Revert()
1621b370545SJessica Hamilton {
1631b370545SJessica Hamilton if (fDirty) {
1641b370545SJessica Hamilton PrefHandler::SetDefault(new PrefHandler(fPreviousPref));
1651b370545SJessica Hamilton
1661b370545SJessica Hamilton fThemeView->Revert();
1671b370545SJessica Hamilton
1681b370545SJessica Hamilton fDirty = false;
1691b370545SJessica Hamilton fRevertButton->SetEnabled(fDirty);
1701b370545SJessica Hamilton }
1711b370545SJessica Hamilton }
1721b370545SJessica Hamilton
1731b370545SJessica Hamilton
1741b370545SJessica Hamilton void
MessageReceived(BMessage * message)1751b370545SJessica Hamilton ThemeWindow::MessageReceived(BMessage *message)
1761b370545SJessica Hamilton {
1771b370545SJessica Hamilton switch (message->what) {
1781b370545SJessica Hamilton
1791b370545SJessica Hamilton case MSG_SAVEAS_PRESSED:
1801b370545SJessica Hamilton _SaveAs();
1811b370545SJessica Hamilton break;
1821b370545SJessica Hamilton
1831b370545SJessica Hamilton case MSG_REVERT_PRESSED:
1841b370545SJessica Hamilton _Revert();
1851b370545SJessica Hamilton break;
1861b370545SJessica Hamilton
1871b370545SJessica Hamilton case MSG_DEFAULTS_PRESSED:
1881b370545SJessica Hamilton PrefHandler::SetDefault(new PrefHandler(false));
1891b370545SJessica Hamilton fThemeView->SetDefaults();
1901b370545SJessica Hamilton // fallthrough
1911b370545SJessica Hamilton
1921b370545SJessica Hamilton case MSG_THEME_MODIFIED:
1931b370545SJessica Hamilton fDirty = true;
1941b370545SJessica Hamilton fRevertButton->SetEnabled(fDirty);
1951b370545SJessica Hamilton break;
1961b370545SJessica Hamilton
1971b370545SJessica Hamilton case B_SAVE_REQUESTED:
1981b370545SJessica Hamilton _SaveRequested(message);
1991b370545SJessica Hamilton break;
2001b370545SJessica Hamilton
2011b370545SJessica Hamilton default:
2021b370545SJessica Hamilton BWindow::MessageReceived(message);
2031b370545SJessica Hamilton break;
2041b370545SJessica Hamilton }
2051b370545SJessica Hamilton }
206