1 /* PoorManWindow.cpp 2 * 3 * Philip Harrison 4 * Started: 4/25/2004 5 * Version: 0.1 6 */ 7 8 #include "PoorManApplication.h" 9 #include "PoorManPreferencesWindow.h" 10 #include "PoorManWindow.h" 11 #include "constants.h" 12 13 14 #include <Box.h> 15 #include <Directory.h> 16 #include <Alert.h> 17 18 #include <stdio.h> 19 20 21 //#include <iostream> 22 23 PoorManWindow::PoorManWindow(BRect frame) 24 : BWindow(frame, STR_APP_NAME, B_TITLED_WINDOW, 0), 25 status(false), hits(0), last_width(325), last_height(155) 26 { 27 DefaultSettings(); 28 29 30 31 // PoorMan Window 32 SetSizeLimits(318, 1600, 53, 1200); 33 // limit the size of the size of the window 34 35 //SetZoomLimits(1024, 768); 36 37 //frame.Set(30.0f, 30.0f, 355.0f, 185.0f); 38 frame.OffsetTo(B_ORIGIN); 39 frame = Bounds(); 40 frame.top += 19.0; 41 42 mainView = new PoorManView(frame, STR_APP_NAME); 43 mainView->SetViewColor(216,216,216,255); 44 45 mainView->SetFont(be_bold_font); 46 mainView->SetFontSize(12); 47 AddChild(mainView); 48 49 // BBox tests 50 BRect br; 51 br = frame; 52 br.top = 1.0; 53 54 BBox * bb = new BBox(br, "Background", B_FOLLOW_ALL_SIDES, 55 B_WILL_DRAW | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE); 56 bb->SetHighColor(WHITE); 57 bb->SetLowColor(GRAY); 58 bb->SetBorder(B_PLAIN_BORDER); 59 mainView->AddChild(bb); 60 61 // ----------------------------------------------------------------- 62 // Three Labels 63 64 // Status String 65 BRect statusRect; 66 statusRect = Bounds(); 67 statusRect.left += 5; 68 statusRect.top += 3; 69 statusRect.bottom = statusRect.top + 15; 70 statusRect.right = statusRect.left + 75; // make the width wide enough for the string to display 71 72 statusView = new BStringView(statusRect, "Status View", "Status: Stopped"); 73 bb->AddChild(statusView); 74 75 // Directory String 76 BRect dirRect; 77 dirRect = Bounds(); 78 dirRect.top = statusRect.bottom - 1; 79 dirRect.bottom = dirRect.top + 15; 80 dirRect.left = statusRect.left; 81 dirRect.right -= 5; 82 83 dirView = new BStringView(dirRect, "Dir View", "Directory: /boot/home/", B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP); 84 bb->AddChild(dirView); 85 86 // Hits String 87 BRect hitsRect; 88 hitsRect = bb->Bounds(); 89 hitsRect.InsetBy(5, 5); 90 hitsRect.top = statusRect.top; 91 hitsRect.bottom = statusRect.bottom; 92 hitsRect.left = statusRect.right + 20; 93 94 hitsView = new BStringView(hitsRect, "Hit View", "Hits: 0", B_FOLLOW_RIGHT | B_FOLLOW_TOP); 95 hitsView->SetAlignment(B_ALIGN_RIGHT); 96 bb->AddChild(hitsView); 97 98 // ----------------------------------------------------------------- 99 // Logging View 100 101 // logRect 102 BRect logRect(5.0, 36.0, 306.0, 131.0); 103 104 // textRect 105 BRect textRect; //(1.0, 1.0, 175.0, 75.0); 106 textRect = logRect; 107 textRect.InsetBy(1.0, 1.0); 108 textRect.top = 0.0; 109 textRect.left = 0.0; 110 111 loggingView = new BTextView(logRect, STR_TXT_VIEW, textRect, 112 B_FOLLOW_ALL_SIDES, B_WILL_DRAW ); 113 114 loggingView->MakeEditable(false); // user cannot change the text 115 loggingView->MakeSelectable(true); 116 loggingView->SetViewColor(WHITE); 117 118 loggingView->Insert("This is PoorMan.\n"); // text_run_array 119 //loggingView->MakeFocus(true); 120 //AddChild(loggingView); 121 122 123 // create the scroll view 124 scrollView = new BScrollView("Scroll View", loggingView, B_FOLLOW_ALL_SIDES, 125 B_WILL_DRAW | B_FRAME_EVENTS, 126 // Make sure articles on border do not occur when resizing 127 false, true); 128 bb->AddChild(scrollView); 129 130 131 132 // ----------------------------------------------------------------- 133 // menu bar 134 BRect menuRect; 135 menuRect = Bounds(); 136 menuRect.bottom = 18.0f; 137 138 FileMenuBar = new BMenuBar(menuRect, "File Menu Bar"); 139 140 // menus 141 FileMenu = BuildFileMenu(); 142 if (FileMenu) 143 FileMenuBar->AddItem(FileMenu); 144 145 EditMenu = BuildEditMenu(); 146 if (EditMenu) 147 FileMenuBar->AddItem(EditMenu); 148 149 ControlsMenu = BuildControlsMenu(); 150 if (ControlsMenu) 151 FileMenuBar->AddItem(ControlsMenu); 152 153 // File Panels 154 BWindow * change_title; 155 156 saveConsoleFilePanel = new BFilePanel( 157 B_SAVE_PANEL, 158 new BMessenger(this), 159 NULL, 160 B_FILE_NODE, 161 false, 162 new BMessage(MSG_FILE_PANEL_SAVE_CONSOLE) 163 ); 164 change_title = saveConsoleFilePanel->Window(); 165 change_title->SetTitle(STR_FILEPANEL_SAVE_CONSOLE); 166 167 saveConsoleSelectionFilePanel = new BFilePanel( 168 B_SAVE_PANEL, 169 new BMessenger(this), 170 NULL, 171 B_FILE_NODE, 172 false, 173 new BMessage(MSG_FILE_PANEL_SAVE_CONSOLE_SELECTION) 174 ); 175 change_title = saveConsoleSelectionFilePanel->Window(); 176 change_title->SetTitle(STR_FILEPANEL_SAVE_CONSOLE_SELECTION); 177 178 179 AddChild(FileMenuBar); 180 } 181 182 void 183 PoorManWindow::MessageReceived(BMessage* message) 184 { 185 switch (message->what) { 186 case MSG_MENU_FILE_SAVE_AS: 187 saveConsoleFilePanel->Show(); 188 break; 189 case MSG_FILE_PANEL_SAVE_CONSOLE: 190 printf("FilePanel: Save Console\n"); 191 SaveConsole(message, false); 192 break; 193 case MSG_MENU_FILE_SAVE_SELECTION: 194 saveConsoleSelectionFilePanel->Show(); 195 break; 196 case MSG_FILE_PANEL_SAVE_CONSOLE_SELECTION: 197 printf("FilePanel: Save Console Selection\n"); 198 SaveConsole(message, true); 199 break; 200 case MSG_MENU_EDIT_PREF: 201 prefWindow = new PoorManPreferencesWindow(BRect(30.0f, 30.0f, 410.0f, 310.0f), STR_WIN_NAME_PREF); 202 break; 203 case MSG_MENU_CTRL_RUN: 204 BMenuItem * RunServer; 205 RunServer = ControlsMenu->FindItem(STR_MNU_CTRL_RUN_SERVER); 206 if (RunServer) 207 { 208 //status = httpd->Run(); 209 /* For Testing 210 if (status) 211 status = false; 212 else 213 status = true; 214 */ 215 216 UpdateStatusLabel(status); 217 if (status) 218 { 219 RunServer->SetMarked(true); 220 } else { 221 RunServer->SetMarked(false); 222 } 223 } 224 break; 225 case MSG_MENU_CTRL_CLEAR_HIT: 226 SetHits(0); 227 UpdateHitsLabel(); 228 break; 229 case MSG_MENU_CTRL_CLEAR_CONSOLE: 230 loggingView->SelectAll(); 231 loggingView->Delete(); 232 break; 233 case MSG_MENU_CTRL_CLEAR_LOG: 234 FILE * f; 235 f = fopen(log_path.String(), "w"); 236 fclose(f); 237 break; 238 default: 239 BWindow::MessageReceived(message); 240 break; 241 } 242 } 243 244 245 void 246 PoorManWindow::FrameResized(float width, float height) 247 { 248 if (is_zoomed) 249 { 250 last_width = width; 251 last_height = height; 252 } 253 printf("(%f, %f)\n", width, height); 254 } 255 256 bool 257 PoorManWindow::QuitRequested() 258 { 259 SaveSettings(); 260 be_app->PostMessage(B_QUIT_REQUESTED); 261 return (true); 262 } 263 264 void 265 PoorManWindow::Zoom(BPoint origin, float width, float height) 266 { 267 printf("Zoom: Is Zoomed before: %d (%f, %f)\n", is_zoomed, width, height); 268 if (is_zoomed) 269 { // Change to the Minimal size 270 is_zoomed = false; 271 ResizeTo(318, 53); 272 printf("ResizedTo(318, 53)\n"); 273 } 274 else 275 { // Change to the Zoomed size 276 is_zoomed = true; 277 ResizeTo(last_width, last_height); 278 printf("ResizedTo(%f, %f)\n", last_width, last_height); 279 } 280 } 281 282 // Private: Methods ------------------------------------------ 283 284 BMenu * 285 PoorManWindow::BuildFileMenu() const 286 { 287 BMenu * ptrFileMenu = new BMenu(STR_MNU_FILE); 288 289 ptrFileMenu->AddItem(new BMenuItem(STR_MNU_FILE_SAVE_AS, 290 new BMessage(MSG_MENU_FILE_SAVE_AS), CMD_FILE_SAVE_AS)); 291 292 ptrFileMenu->AddItem(new BMenuItem(STR_MNU_FILE_SAVE_SELECTION, 293 new BMessage(MSG_MENU_FILE_SAVE_SELECTION))); 294 295 ptrFileMenu->AddSeparatorItem(); 296 297 // about box 298 BMenuItem * AboutItem = new BMenuItem(STR_MNU_FILE_ABOUT, 299 new BMessage(B_ABOUT_REQUESTED)); 300 AboutItem->SetTarget(NULL, be_app); 301 ptrFileMenu->AddItem(AboutItem); 302 303 ptrFileMenu->AddSeparatorItem(); 304 305 306 ptrFileMenu->AddItem(new BMenuItem(STR_MNU_FILE_QUIT, 307 new BMessage(B_QUIT_REQUESTED), CMD_FILE_QUIT)); 308 309 return ptrFileMenu; 310 } 311 312 BMenu * 313 PoorManWindow::BuildEditMenu() const 314 { 315 BMenu * ptrEditMenu = new BMenu(STR_MNU_EDIT); 316 317 BMenuItem * CopyMenuItem = new BMenuItem(STR_MNU_EDIT_COPY, 318 new BMessage(B_COPY), CMD_EDIT_COPY); 319 320 ptrEditMenu->AddItem(CopyMenuItem); 321 CopyMenuItem->SetTarget(loggingView, NULL); 322 323 ptrEditMenu->AddSeparatorItem(); 324 325 BMenuItem * SelectAllMenuItem = new BMenuItem(STR_MNU_EDIT_SELECT_ALL, 326 new BMessage(B_SELECT_ALL), CMD_EDIT_SELECT_ALL); 327 328 ptrEditMenu->AddItem(SelectAllMenuItem); 329 SelectAllMenuItem->SetTarget(loggingView, NULL); 330 331 ptrEditMenu->AddSeparatorItem(); 332 333 BMenuItem * PrefMenuItem = new BMenuItem(STR_MNU_EDIT_PREF, 334 new BMessage(MSG_MENU_EDIT_PREF)); 335 ptrEditMenu->AddItem(PrefMenuItem); 336 337 return ptrEditMenu; 338 } 339 340 BMenu * 341 PoorManWindow::BuildControlsMenu() const 342 { 343 BMenu * ptrControlMenu = new BMenu(STR_MNU_CTRL); 344 345 BMenuItem * RunServerMenuItem = new BMenuItem(STR_MNU_CTRL_RUN_SERVER, 346 new BMessage(MSG_MENU_CTRL_RUN)); 347 RunServerMenuItem->SetMarked(false); 348 ptrControlMenu->AddItem(RunServerMenuItem); 349 350 BMenuItem * ClearHitCounterMenuItem = new BMenuItem(STR_MNU_CTRL_CLEAR_HIT_COUNTER, 351 new BMessage(MSG_MENU_CTRL_CLEAR_HIT)); 352 ptrControlMenu->AddItem(ClearHitCounterMenuItem); 353 354 ptrControlMenu->AddSeparatorItem(); 355 356 BMenuItem * ClearConsoleLogMenuItem = new BMenuItem(STR_MNU_CTRL_CLEAR_CONSOLE, 357 new BMessage(MSG_MENU_CTRL_CLEAR_CONSOLE)); 358 ptrControlMenu->AddItem(ClearConsoleLogMenuItem); 359 360 BMenuItem * ClearLogFileMenuItem = new BMenuItem(STR_MNU_CTRL_CLEAR_LOG_FILE, 361 new BMessage(MSG_MENU_CTRL_CLEAR_LOG)); 362 ptrControlMenu->AddItem(ClearLogFileMenuItem); 363 364 return ptrControlMenu; 365 } 366 367 void 368 PoorManWindow::SetDirLabel(const char * name) 369 { 370 BString dirPath("Directory: "); 371 int32 length = dirPath.Length() + 1; 372 dirPath.LockBuffer(length); 373 dirPath.Append(name); 374 375 dirPath.UnlockBuffer(length + strlen(name)); 376 377 if (Lock()) 378 { 379 dirView->SetText(dirPath.String()); 380 Unlock(); 381 } 382 } 383 384 void 385 PoorManWindow::UpdateStatusLabel(bool set) 386 { 387 if (Lock()) 388 { 389 if (set) 390 statusView->SetText("Status: Running"); 391 else 392 statusView->SetText("Status: Stopped"); 393 394 Unlock(); 395 } 396 } 397 398 void 399 PoorManWindow::UpdateHitsLabel() 400 { 401 if (Lock()) 402 { 403 sprintf(hitsLabel, "Hits: %lu", GetHits()); 404 hitsView->SetText(hitsLabel); 405 406 Unlock(); 407 } 408 } 409 410 status_t 411 PoorManWindow::SaveConsole(BMessage * message, bool selection) 412 { 413 entry_ref ref; 414 const char * name; 415 BPath path; 416 BEntry entry; 417 status_t err = B_OK; 418 FILE *f; 419 420 if (err = message->FindRef("directory", &ref) != B_OK) 421 return err; 422 423 if (err = message->FindString("name", &name) != B_OK) 424 return err; 425 426 if (err = entry.SetTo(&ref) != B_OK) 427 return err; 428 429 entry.GetPath(&path); 430 path.Append(name); 431 432 if (!(f = fopen(path.Path(), "w"))) 433 return B_ERROR; 434 435 if (!selection) 436 { 437 // write the data to the file 438 err = fwrite(loggingView->Text(), 1, loggingView->TextLength(), f); 439 } else { 440 // find the selected text and write it to a file 441 int32 start = 0, end = 0; 442 loggingView->GetSelection(&start, &end); 443 444 BString buffer; 445 char * buffData = buffer.LockBuffer(end - start + 1); 446 // copy the selected text from the TextView to the buffer 447 loggingView->GetText(start, end - start, buffData); 448 buffer.UnlockBuffer(end - start + 1); 449 450 err = fwrite(buffer.String(), 1, end - start + 1, f); 451 } 452 fclose(f); 453 454 return err; 455 } 456 457 458 void 459 PoorManWindow::DefaultSettings() 460 { 461 // Site Tab 462 SetIndexFileName("index.html"); 463 SetDirListFlag(false); 464 // Logging Tab 465 SetLogConsoleFlag(true); 466 SetLogFileFlag(false); 467 // Advanced Tab 468 SetMaxConnections(32); 469 470 BDirectory webDir; 471 if (!webDir.Contains("/boot/home/public_html", B_DIRECTORY_NODE)) 472 { 473 BAlert * serverAlert = new BAlert("Error Server", STR_ERR_CANT_START, "OK"); 474 475 BAlert * dirAlert = new BAlert("Error Dir", STR_ERR_WEB_DIR, 476 "Cancel", "Select", "Default", B_WIDTH_AS_USUAL, B_OFFSET_SPACING); 477 dirAlert->SetShortcut(0, B_ESCAPE); 478 int32 buttonIndex = dirAlert->Go(); 479 480 481 482 // process dirAlert 483 switch (buttonIndex) 484 { 485 case 0: 486 serverAlert->Go(); 487 QuitRequested(); 488 break; 489 case 1: 490 serverAlert->Go(); 491 prefWindow = new PoorManPreferencesWindow(BRect(30.0f, 30.0f, 410.0f, 310.0f), STR_WIN_NAME_PREF); 492 prefWindow->ShowWebDirFilePanel(); 493 break; 494 case 2: 495 496 break; 497 } 498 } else { 499 printf("BDirectory contains: /boot/home/public_html\n"); 500 SetWebDir("/boot/home/public_html"); 501 } 502 503 } 504 505 status_t 506 PoorManWindow::ReadSettings() 507 { 508 } 509 510 status_t 511 PoorManWindow::SaveSettings() 512 { 513 FILE * f; 514 size_t s; 515 516 f = fopen(STR_SETTINGS_NEW_FILE_PATH, "wb"); 517 if (f) 518 { 519 // Need to rewrite 520 /* 521 //fprintf(f, "%s", web_directory.String()); 522 s = fwrite(web_directory.String(), sizeof(char), web_directory.Length() + 1, f); 523 524 fprintf(f, "%s", index_file_name.String()); 525 fwrite(dir_list_flag, sizeof(bool), 1, f); 526 //fprintf(f, "%uc", dir_list_flag); 527 528 // logging tab 529 fwrite(log_file_flag, sizeof(bool), 1, f); 530 //fprintf(f, "%uc", log_console_flag); 531 fprintf(f, "%uc", log_file_flag); 532 fprintf(f, "%s", log_path.String()); 533 // advanced tab 534 fprintf(f, "%ld", max_connections); 535 536 fprintf(f, "%uc", is_zoomed); 537 fprintf(f, "%f", last_width); 538 fprintf(f, "%f", last_height); 539 */ 540 } 541 fclose(f); 542 } 543