/* * Copyright 2024, Santiago Lema * All rights reserved. Distributed under the terms of the MIT license. */ #include "MainWindow.h" static int progressAnim = 0; #include #include #include #include #include #include #include #include #include #include #include #include "Conversation.h" #undef B_TRANSLATION_CONTEXT #define B_TRANSLATION_CONTEXT "Window" static const uint32 kMsgNewFile = 'fnew'; static const uint32 kMsgOpenFile = 'fopn'; static const uint32 kMsgSaveFile = 'fsav'; static const uint32 kPulse = 'plse'; static const uint32 kSendPrompt = 'kspt'; static const uint32 kQuestionChanged = 'kqch'; MainWindow::MainWindow() : BWindow(BRect(100, 100, 600, 400), B_TRANSLATE("BeDumb"), B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE) { //Without this conversation would never get bmessages from HttpRequest LockLooper(); AddHandler(_conversation); UnlockLooper(); BMenuBar *menuBar = _BuildMenu(); BLayoutBuilder::Group<>(this, B_VERTICAL, 0).Add(menuBar).AddGlue().End(); _inputField = new BTextView("input_view", B_WILL_DRAW | B_FOLLOW_ALL); _inputField->SetText("What is the matrix ?"); _inputField->MakeEditable(true); _inputField->MakeSelectable(true); _inputField->SetWordWrap(true); _progress = new BStatusBar("prog"); _progress->SetMaxValue(100); _progress->SetTo(0); _progress->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); BStringView *header = new BStringView("biglabel", "Let's Be Dumber!"); BFont font; header->GetFont(&font); font.SetSize(20); header->SetFont(&font); // Info view, only one line high _infoView = new BTextView("info"); _infoView->SetText("..."); _infoView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); _infoView->MakeEditable(false); _infoView->MakeSelectable(false); _infoView->SetWordWrap(false); float lineHeight = _infoView->LineHeight(0); _infoView->SetExplicitMinSize(BSize(B_SIZE_UNSET, lineHeight)); _infoView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, lineHeight)); float askH = lineHeight * 5; _inputField->SetExplicitMinSize(BSize(B_SIZE_UNSET, askH)); BButton *sendButton = new BButton("send", B_TRANSLATE("Send"), new BMessage(kSendPrompt), B_WILL_DRAW | B_NAVIGABLE); _answerView = new BTextView("answer", B_WILL_DRAW | B_FOLLOW_ALL); _answerView->MakeEditable(false); // Disable editing _answerView->MakeSelectable(true); // Enable text selection _answerView->SetWordWrap(true); BScrollView *scrollView = new BScrollView("scroll_view", _answerView, B_FOLLOW_ALL | B_WILL_DRAW, 0, false, true); // horizontal and vertical scrollbars BLayoutBuilder::Group<>(this, B_VERTICAL, 0) .AddGlue(0.1) .Add(header) .AddGlue(0.1) .AddGroup(B_HORIZONTAL, 0, 1) .Add(_inputField) .AddGroup(B_HORIZONTAL, 0, 1) .Add(sendButton) .End() .End() .AddGlue(0.1) .Add(scrollView) .Add(_progress) .Add(_infoView) .SetInsets(5, 5, 5, 5) .End(); BMessageRunner* runner = new BMessageRunner( this, // target BHandler new BMessage(kPulse), 50000 // interval in μs (0 ms) ); } MainWindow::~MainWindow() {} void MainWindow::MessageReceived(BMessage *message) { switch (message->what) { case kPulse: { if (progressAnim >=1 && progressAnim <= 85) { _progress->SetTo(progressAnim); progressAnim++; } break; } // case kMsgNewFile: { // fSaveMenuItem->SetEnabled(false); // printf("New\n"); // } break; // case kMsgOpenFile: { // fSaveMenuItem->SetEnabled(true); // printf("Open\n"); // } break; // case kMsgSaveFile: { // printf("Save\n"); // } break; case kSendPrompt: { _progress->SetMaxValue(100); _progress->SetTo(0); _answerView->SetText("..."); progressAnim = 1;//will trigger animation printf("Button Pressed\n"); _infoView->SetText("Asking..."); _conversation->ask(std::string(_inputField->Text())); } break; case kSendReply: { printf("Conversation returned!\n"); _infoView->SetText("Answer Received"); progressAnim = 100; const char* text; if (message->FindString("text", &text) == B_OK) { printf("Received text: %s\n", text); // Do something with text (e.g., set it to a BTextView) _answerView->SetText(text); } else { printf("No text found in message.\n"); _answerView->SetText("NO TEXT IN REPLY"); } _progress->SetMaxValue(100); _progress->SetTo(100); } break; default: { // message->PrintToStream(); BHandler::MessageReceived( message); // call the parent handler for other messages // _infoView->SetText(message->FindMessage()); break; } } // end switch } // end function BMenuBar *MainWindow::_BuildMenu() { BMenuBar *menuBar = new BMenuBar("menubar"); BMenu *menu; BMenuItem *item; // menu 'File' menu = new BMenu(B_TRANSLATE("File")); // item = new BMenuItem(B_TRANSLATE("New"), new BMessage(kMsgNewFile), 'N'); // menu->AddItem(item); // item = new BMenuItem(B_TRANSLATE("Open" B_UTF8_ELLIPSIS), // new BMessage(kMsgOpenFile), 'O'); // menu->AddItem(item); // fSaveMenuItem = // new BMenuItem(B_TRANSLATE("Save"), new BMessage(kMsgSaveFile), 'S'); // fSaveMenuItem->SetEnabled(false); // menu->AddItem(fSaveMenuItem); // menu->AddSeparatorItem(); item = new BMenuItem(B_TRANSLATE("About" B_UTF8_ELLIPSIS), new BMessage(B_ABOUT_REQUESTED)); item->SetTarget(be_app); menu->AddItem(item); item = new BMenuItem(B_TRANSLATE("Quit"), new BMessage(B_QUIT_REQUESTED), 'Q'); menu->AddItem(item); menuBar->AddItem(menu); return menuBar; }