1 /* 2 * Copyright 2007-2010, Haiku, Inc. All rights reserved. 3 * Copyright (c) 2004 Daniel Furrer <assimil8or@users.sourceforge.net> 4 * Copyright (c) 2003-2004 Kian Duffy <myob@users.sourceforge.net> 5 * Copyright (C) 1998,99 Kazuho Okui and Takashi Murai. 6 * 7 * Distributed under the terms of the MIT license. 8 */ 9 10 #include "TermWindow.h" 11 12 #include <new> 13 #include <stdio.h> 14 #include <string.h> 15 #include <time.h> 16 17 #include <Alert.h> 18 #include <Application.h> 19 #include <Catalog.h> 20 #include <CharacterSet.h> 21 #include <CharacterSetRoster.h> 22 #include <Clipboard.h> 23 #include <Dragger.h> 24 #include <File.h> 25 #include <FindDirectory.h> 26 #include <LayoutBuilder.h> 27 #include <LayoutUtils.h> 28 #include <Locale.h> 29 #include <Menu.h> 30 #include <MenuBar.h> 31 #include <MenuItem.h> 32 #include <Path.h> 33 #include <PopUpMenu.h> 34 #include <PrintJob.h> 35 #include <Roster.h> 36 #include <Screen.h> 37 #include <ScrollBar.h> 38 #include <ScrollView.h> 39 #include <String.h> 40 #include <UTF8.h> 41 42 #include <AutoLocker.h> 43 44 #include "ActiveProcessInfo.h" 45 #include "Arguments.h" 46 #include "AppearPrefView.h" 47 #include "FindWindow.h" 48 #include "Globals.h" 49 #include "PrefWindow.h" 50 #include "PrefHandler.h" 51 #include "SetTitleDialog.h" 52 #include "ShellParameters.h" 53 #include "TermConst.h" 54 #include "TermScrollView.h" 55 #include "TitlePlaceholderMapper.h" 56 57 58 const static int32 kMaxTabs = 6; 59 const static int32 kTermViewOffset = 3; 60 61 // messages constants 62 static const uint32 kNewTab = 'NTab'; 63 static const uint32 kCloseView = 'ClVw'; 64 static const uint32 kCloseOtherViews = 'CloV'; 65 static const uint32 kIncreaseFontSize = 'InFs'; 66 static const uint32 kDecreaseFontSize = 'DcFs'; 67 static const uint32 kSetActiveTab = 'STab'; 68 static const uint32 kUpdateTitles = 'UPti'; 69 static const uint32 kEditTabTitle = 'ETti'; 70 static const uint32 kEditWindowTitle = 'EWti'; 71 static const uint32 kTabTitleChanged = 'TTch'; 72 static const uint32 kWindowTitleChanged = 'WTch'; 73 static const uint32 kUpdateSwitchTerminalsMenuItem = 'Ustm'; 74 75 using namespace BPrivate ; // BCharacterSet stuff 76 77 #undef B_TRANSLATE_CONTEXT 78 #define B_TRANSLATE_CONTEXT "Terminal TermWindow" 79 80 81 // #pragma mark - TermViewContainerView 82 83 84 class TermViewContainerView : public BView { 85 public: 86 TermViewContainerView(TermView* termView) 87 : 88 BView(BRect(), "term view container", B_FOLLOW_ALL, 0), 89 fTermView(termView) 90 { 91 termView->MoveTo(kTermViewOffset, kTermViewOffset); 92 BRect frame(termView->Frame()); 93 ResizeTo(frame.right + kTermViewOffset, frame.bottom + kTermViewOffset); 94 AddChild(termView); 95 } 96 97 TermView* GetTermView() const { return fTermView; } 98 99 virtual void GetPreferredSize(float* _width, float* _height) 100 { 101 float width, height; 102 fTermView->GetPreferredSize(&width, &height); 103 *_width = width + 2 * kTermViewOffset; 104 *_height = height + 2 * kTermViewOffset; 105 } 106 107 private: 108 TermView* fTermView; 109 }; 110 111 112 // #pragma mark - SessionID 113 114 115 TermWindow::SessionID::SessionID(int32 id) 116 : 117 fID(id) 118 { 119 } 120 121 122 TermWindow::SessionID::SessionID(const BMessage& message, const char* field) 123 { 124 if (message.FindInt32(field, &fID) != B_OK) 125 fID = -1; 126 } 127 128 129 status_t 130 TermWindow::SessionID::AddToMessage(BMessage& message, const char* field) const 131 { 132 return message.AddInt32(field, fID); 133 } 134 135 136 // #pragma mark - Session 137 138 139 struct TermWindow::Session { 140 SessionID id; 141 int32 index; 142 Title title; 143 TermViewContainerView* containerView; 144 145 Session(SessionID id, int32 index, TermViewContainerView* containerView) 146 : 147 id(id), 148 index(index), 149 containerView(containerView) 150 { 151 title.title = B_TRANSLATE("Shell "); 152 title.title << index; 153 title.patternUserDefined = false; 154 } 155 }; 156 157 158 // #pragma mark - TermWindow 159 160 161 TermWindow::TermWindow(const BString& title, Arguments* args) 162 : 163 BWindow(BRect(0, 0, 0, 0), title, B_DOCUMENT_WINDOW, 164 B_CURRENT_WORKSPACE | B_QUIT_ON_WINDOW_CLOSE), 165 fTitleUpdateRunner(this, BMessage(kUpdateTitles), 1000000), 166 fNextSessionID(0), 167 fTabView(NULL), 168 fMenuBar(NULL), 169 fSwitchTerminalsMenuItem(NULL), 170 fEncodingMenu(NULL), 171 fPrintSettings(NULL), 172 fPrefWindow(NULL), 173 fFindPanel(NULL), 174 fSavedFrame(0, 0, -1, -1), 175 fSetWindowTitleDialog(NULL), 176 fSetTabTitleDialog(NULL), 177 fFindString(""), 178 fFindNextMenuItem(NULL), 179 fFindPreviousMenuItem(NULL), 180 fFindSelection(false), 181 fForwardSearch(false), 182 fMatchCase(false), 183 fMatchWord(false), 184 fFullScreen(false) 185 { 186 // register this terminal 187 fTerminalRoster.Register(Team(), this); 188 fTerminalRoster.SetListener(this); 189 int32 id = fTerminalRoster.ID(); 190 191 // apply the title settings 192 fTitle.pattern = title; 193 if (fTitle.pattern.Length() == 0) { 194 fTitle.pattern = B_TRANSLATE_SYSTEM_NAME("Terminal"); 195 196 if (id >= 0) 197 fTitle.pattern << " " << id + 1; 198 199 fTitle.patternUserDefined = false; 200 } else 201 fTitle.patternUserDefined = true; 202 203 fTitle.title = fTitle.pattern; 204 fTitle.pattern = title; 205 206 _TitleSettingsChanged(); 207 208 // get the saved window position and workspaces 209 BRect frame; 210 uint32 workspaces; 211 if (_LoadWindowPosition(&frame, &workspaces) == B_OK) { 212 // apply 213 MoveTo(frame.LeftTop()); 214 ResizeTo(frame.Width(), frame.Height()); 215 SetWorkspaces(workspaces); 216 } else { 217 // use computed defaults 218 int i = id / 16; 219 int j = id % 16; 220 int k = (j * 16) + (i * 64) + 50; 221 int l = (j * 16) + 50; 222 223 MoveTo(k, l); 224 } 225 226 // init the GUI and add a tab 227 _InitWindow(); 228 _AddTab(args); 229 230 // Announce our window as no longer minimized. That's not true, since it's 231 // still hidden at this point, but it will be shown very soon. 232 fTerminalRoster.SetWindowInfo(false, Workspaces()); 233 } 234 235 236 TermWindow::~TermWindow() 237 { 238 fTerminalRoster.Unregister(); 239 240 _FinishTitleDialog(); 241 242 if (fPrefWindow) 243 fPrefWindow->PostMessage(B_QUIT_REQUESTED); 244 245 if (fFindPanel && fFindPanel->Lock()) { 246 fFindPanel->Quit(); 247 fFindPanel = NULL; 248 } 249 250 PrefHandler::DeleteDefault(); 251 252 for (int32 i = 0; Session* session = _SessionAt(i); i++) 253 delete session; 254 } 255 256 257 void 258 TermWindow::SessionChanged() 259 { 260 _UpdateSessionTitle(fTabView->Selection()); 261 } 262 263 264 void 265 TermWindow::_InitWindow() 266 { 267 // make menu bar 268 _SetupMenu(); 269 270 // shortcuts to switch tabs 271 for (int32 i = 0; i < 9; i++) { 272 BMessage* message = new BMessage(kSetActiveTab); 273 message->AddInt32("index", i); 274 AddShortcut('1' + i, B_COMMAND_KEY, message); 275 } 276 277 AddShortcut(B_LEFT_ARROW, B_COMMAND_KEY | B_SHIFT_KEY, 278 new BMessage(MSG_MOVE_TAB_LEFT)); 279 AddShortcut(B_RIGHT_ARROW, B_COMMAND_KEY | B_SHIFT_KEY, 280 new BMessage(MSG_MOVE_TAB_RIGHT)); 281 282 BRect textFrame = Bounds(); 283 textFrame.top = fMenuBar->Bounds().bottom + 1.0; 284 285 fTabView = new SmartTabView(textFrame, "tab view", B_WIDTH_FROM_WIDEST); 286 fTabView->SetListener(this); 287 AddChild(fTabView); 288 289 // Make the scroll view one pixel wider than the tab view container view, so 290 // the scroll bar will look good. 291 fTabView->SetInsets(0, 0, -1, 0); 292 } 293 294 295 bool 296 TermWindow::_CanClose(int32 index) 297 { 298 bool warnOnExit = PrefHandler::Default()->getBool(PREF_WARN_ON_EXIT); 299 300 if (!warnOnExit) 301 return true; 302 303 uint32 busyProcessCount = 0; 304 BString busyProcessNames; 305 // all names, separated by "\n\t" 306 307 if (index != -1) { 308 ShellInfo shellInfo; 309 ActiveProcessInfo info; 310 TermView* termView = _TermViewAt(index); 311 if (termView->GetShellInfo(shellInfo) 312 && termView->GetActiveProcessInfo(info) 313 && (info.ID() != shellInfo.ProcessID() 314 || !shellInfo.IsDefaultShell())) { 315 busyProcessCount++; 316 busyProcessNames = info.Name(); 317 } 318 } else { 319 for (int32 i = 0; i < fSessions.CountItems(); i++) { 320 ShellInfo shellInfo; 321 ActiveProcessInfo info; 322 TermView* termView = _TermViewAt(i); 323 if (termView->GetShellInfo(shellInfo) 324 && termView->GetActiveProcessInfo(info) 325 && (info.ID() != shellInfo.ProcessID() 326 || !shellInfo.IsDefaultShell())) { 327 if (++busyProcessCount > 1) 328 busyProcessNames << "\n\t"; 329 busyProcessNames << info.Name(); 330 } 331 } 332 } 333 334 if (busyProcessCount == 0) 335 return true; 336 337 BString alertMessage; 338 if (busyProcessCount == 1) { 339 // Only one pending process. Select the alert text depending on whether 340 // the terminal will be closed. 341 alertMessage = index == -1 || fSessions.CountItems() == 1 342 ? B_TRANSLATE("The process \"%1\" is still running.\n" 343 "If you close the Terminal, the process will be killed.") 344 : B_TRANSLATE("The process \"%1\" is still running.\n" 345 "If you close the tab, the process will be killed."); 346 } else { 347 // multiple pending processes 348 alertMessage = B_TRANSLATE( 349 "The following processes are still running:\n\n" 350 "\t%1\n\n" 351 "If you close the Terminal, the processes will be killed."); 352 } 353 354 alertMessage.ReplaceFirst("%1", busyProcessNames); 355 356 BAlert* alert = new BAlert(B_TRANSLATE("Really close?"), 357 alertMessage, B_TRANSLATE("Close"), B_TRANSLATE("Cancel"), NULL, 358 B_WIDTH_AS_USUAL, B_WARNING_ALERT); 359 alert->SetShortcut(1, B_ESCAPE); 360 return alert->Go() == 0; 361 } 362 363 364 bool 365 TermWindow::QuitRequested() 366 { 367 _FinishTitleDialog(); 368 369 if (!_CanClose(-1)) 370 return false; 371 372 _SaveWindowPosition(); 373 374 return BWindow::QuitRequested(); 375 } 376 377 378 void 379 TermWindow::MenusBeginning() 380 { 381 TermView* view = _ActiveTermView(); 382 383 // Syncronize Encode Menu Pop-up menu and Preference. 384 const BCharacterSet* charset 385 = BCharacterSetRoster::GetCharacterSetByConversionID(view->Encoding()); 386 if (charset != NULL) { 387 BString name(charset->GetPrintName()); 388 const char* mime = charset->GetMIMEName(); 389 if (mime) 390 name << " (" << mime << ")"; 391 392 BMenuItem* item = fEncodingMenu->FindItem(name); 393 if (item != NULL) 394 item->SetMarked(true); 395 } 396 397 BFont font; 398 view->GetTermFont(&font); 399 400 float size = font.Size(); 401 402 fDecreaseFontSizeMenuItem->SetEnabled(size > 9); 403 fIncreaseFontSizeMenuItem->SetEnabled(size < 18); 404 405 BWindow::MenusBeginning(); 406 } 407 408 409 /* static */ 410 BMenu* 411 TermWindow::_MakeEncodingMenu() 412 { 413 BMenu* menu = new (std::nothrow) BMenu(B_TRANSLATE("Text encoding")); 414 if (menu == NULL) 415 return NULL; 416 417 BCharacterSetRoster roster; 418 BCharacterSet charset; 419 while (roster.GetNextCharacterSet(&charset) == B_OK) { 420 int encoding = M_UTF8; 421 const char* mime = charset.GetMIMEName(); 422 if (mime == NULL || strcasecmp(mime, "UTF-8") != 0) 423 encoding = charset.GetConversionID(); 424 425 // filter out currently (???) not supported USC-2 and UTF-16 426 if (encoding == B_UTF16_CONVERSION || encoding == B_UNICODE_CONVERSION) 427 continue; 428 429 BString name(charset.GetPrintName()); 430 if (mime) 431 name << " (" << mime << ")"; 432 433 BMessage *message = new BMessage(MENU_ENCODING); 434 if (message != NULL) { 435 message->AddInt32("op", (int32)encoding); 436 menu->AddItem(new BMenuItem(name, message)); 437 } 438 } 439 440 menu->SetRadioMode(true); 441 442 return menu; 443 } 444 445 446 void 447 TermWindow::_SetupMenu() 448 { 449 BLayoutBuilder::Menu<>(fMenuBar = new BMenuBar(Bounds(), "mbar")) 450 // Terminal 451 .AddMenu(B_TRANSLATE_SYSTEM_NAME("Terminal")) 452 .AddItem(B_TRANSLATE("Switch Terminals"), MENU_SWITCH_TERM, B_TAB) 453 .GetItem(fSwitchTerminalsMenuItem) 454 .AddItem(B_TRANSLATE("New Terminal"), MENU_NEW_TERM, 'N') 455 .AddItem(B_TRANSLATE("New tab"), kNewTab, 'T') 456 .AddSeparator() 457 .AddItem(B_TRANSLATE("Page setup" B_UTF8_ELLIPSIS), MENU_PAGE_SETUP) 458 .AddItem(B_TRANSLATE("Print"), MENU_PRINT,'P') 459 .AddSeparator() 460 .AddItem(B_TRANSLATE("Close window"), B_QUIT_REQUESTED, 'W', 461 B_SHIFT_KEY) 462 .AddItem(B_TRANSLATE("Close active tab"), kCloseView, 'W') 463 .AddItem(B_TRANSLATE("Quit"), B_QUIT_REQUESTED, 'Q') 464 .End() 465 466 // Edit 467 .AddMenu(B_TRANSLATE("Edit")) 468 .AddItem(B_TRANSLATE("Copy"), B_COPY,'C') 469 .AddItem(B_TRANSLATE("Paste"), B_PASTE,'V') 470 .AddSeparator() 471 .AddItem(B_TRANSLATE("Select all"), B_SELECT_ALL, 'A') 472 .AddItem(B_TRANSLATE("Clear all"), MENU_CLEAR_ALL, 'L') 473 .AddSeparator() 474 .AddItem(B_TRANSLATE("Find" B_UTF8_ELLIPSIS), MENU_FIND_STRING,'F') 475 .AddItem(B_TRANSLATE("Find previous"), MENU_FIND_PREVIOUS, 'G', 476 B_SHIFT_KEY) 477 .GetItem(fFindPreviousMenuItem) 478 .SetEnabled(false) 479 .AddItem(B_TRANSLATE("Find next"), MENU_FIND_NEXT, 'G') 480 .GetItem(fFindNextMenuItem) 481 .SetEnabled(false) 482 .AddSeparator() 483 .AddItem(B_TRANSLATE("Window title" B_UTF8_ELLIPSIS), 484 kEditWindowTitle) 485 .End() 486 487 // Settings 488 .AddMenu(B_TRANSLATE("Settings")) 489 .AddItem(_MakeWindowSizeMenu()) 490 .AddItem(fEncodingMenu = _MakeEncodingMenu()) 491 .AddMenu(B_TRANSLATE("Text size")) 492 .AddItem(B_TRANSLATE("Increase"), kIncreaseFontSize, '+', 493 B_COMMAND_KEY) 494 .GetItem(fIncreaseFontSizeMenuItem) 495 .AddItem(B_TRANSLATE("Decrease"), kDecreaseFontSize, '-', 496 B_COMMAND_KEY) 497 .GetItem(fDecreaseFontSizeMenuItem) 498 .End() 499 .AddSeparator() 500 .AddItem(B_TRANSLATE("Settings" B_UTF8_ELLIPSIS), MENU_PREF_OPEN) 501 .AddSeparator() 502 .AddItem(B_TRANSLATE("Save as default"), SAVE_AS_DEFAULT) 503 .End() 504 ; 505 506 AddChild(fMenuBar); 507 508 _UpdateSwitchTerminalsMenuItem(); 509 } 510 511 512 status_t 513 TermWindow::_GetWindowPositionFile(BFile* file, uint32 openMode) 514 { 515 BPath path; 516 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path, true); 517 if (status != B_OK) 518 return status; 519 520 status = path.Append("Terminal"); 521 if (status != B_OK) 522 return status; 523 524 status = path.Append("Windows"); 525 if (status != B_OK) 526 return status; 527 528 return file->SetTo(path.Path(), openMode); 529 } 530 531 532 status_t 533 TermWindow::_LoadWindowPosition(BRect* frame, uint32* workspaces) 534 { 535 status_t status; 536 BMessage position; 537 538 BFile file; 539 status = _GetWindowPositionFile(&file, B_READ_ONLY); 540 if (status != B_OK) 541 return status; 542 543 status = position.Unflatten(&file); 544 545 file.Unset(); 546 547 if (status != B_OK) 548 return status; 549 550 int32 id = fTerminalRoster.ID(); 551 status = position.FindRect("rect", id, frame); 552 if (status != B_OK) 553 return status; 554 555 int32 _workspaces; 556 status = position.FindInt32("workspaces", id, &_workspaces); 557 if (status != B_OK) 558 return status; 559 if (modifiers() & B_SHIFT_KEY) 560 *workspaces = _workspaces; 561 else 562 *workspaces = B_CURRENT_WORKSPACE; 563 564 return B_OK; 565 } 566 567 568 status_t 569 TermWindow::_SaveWindowPosition() 570 { 571 BFile file; 572 BMessage originalSettings; 573 574 // Read the settings file if it exists and is a valid BMessage. 575 status_t status = _GetWindowPositionFile(&file, B_READ_ONLY); 576 if (status == B_OK) { 577 status = originalSettings.Unflatten(&file); 578 file.Unset(); 579 580 if (status != B_OK) 581 status = originalSettings.MakeEmpty(); 582 583 if (status != B_OK) 584 return status; 585 } 586 587 // Replace the settings 588 int32 id = fTerminalRoster.ID(); 589 BRect rect(Frame()); 590 if (originalSettings.ReplaceRect("rect", id, rect) != B_OK) 591 originalSettings.AddRect("rect", rect); 592 593 int32 workspaces = Workspaces(); 594 if (originalSettings.ReplaceInt32("workspaces", id, workspaces) != B_OK) 595 originalSettings.AddInt32("workspaces", workspaces); 596 597 // Resave the whole thing 598 status = _GetWindowPositionFile (&file, 599 B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 600 if (status != B_OK) 601 return status; 602 603 return originalSettings.Flatten(&file); 604 } 605 606 607 void 608 TermWindow::_GetPreferredFont(BFont& font) 609 { 610 // Default to be_fixed_font 611 font = be_fixed_font; 612 613 const char* family = PrefHandler::Default()->getString(PREF_HALF_FONT_FAMILY); 614 const char* style = PrefHandler::Default()->getString(PREF_HALF_FONT_STYLE); 615 616 font.SetFamilyAndStyle(family, style); 617 618 float size = PrefHandler::Default()->getFloat(PREF_HALF_FONT_SIZE); 619 if (size < 6.0f) 620 size = 6.0f; 621 font.SetSize(size); 622 } 623 624 625 void 626 TermWindow::MessageReceived(BMessage *message) 627 { 628 int32 encodingId; 629 bool findresult; 630 631 switch (message->what) { 632 case B_COPY: 633 _ActiveTermView()->Copy(be_clipboard); 634 break; 635 636 case B_PASTE: 637 _ActiveTermView()->Paste(be_clipboard); 638 break; 639 640 case B_SELECT_ALL: 641 _ActiveTermView()->SelectAll(); 642 break; 643 644 case MENU_CLEAR_ALL: 645 _ActiveTermView()->Clear(); 646 break; 647 648 case MENU_SWITCH_TERM: 649 _SwitchTerminal(); 650 break; 651 652 case MENU_NEW_TERM: 653 { 654 // Set our current working directory to that of the active tab, so 655 // that the new terminal and its shell inherit it. 656 // Note: That's a bit lame. We should rather fork() and change the 657 // CWD in the child, but since ATM there aren't any side effects of 658 // changing our CWD, we save ourselves the trouble. 659 ActiveProcessInfo activeProcessInfo; 660 if (_ActiveTermView()->GetActiveProcessInfo(activeProcessInfo)) 661 chdir(activeProcessInfo.CurrentDirectory()); 662 663 app_info info; 664 be_app->GetAppInfo(&info); 665 666 // try launching two different ways to work around possible problems 667 if (be_roster->Launch(&info.ref) != B_OK) 668 be_roster->Launch(TERM_SIGNATURE); 669 break; 670 } 671 672 case MENU_PREF_OPEN: 673 if (!fPrefWindow) { 674 fPrefWindow = new PrefWindow(this); 675 } 676 else 677 fPrefWindow->Activate(); 678 break; 679 680 case MSG_PREF_CLOSED: 681 fPrefWindow = NULL; 682 break; 683 684 case MSG_WINDOW_TITLE_SETTING_CHANGED: 685 case MSG_TAB_TITLE_SETTING_CHANGED: 686 _TitleSettingsChanged(); 687 break; 688 689 case MENU_FIND_STRING: 690 if (!fFindPanel) { 691 fFindPanel = new FindWindow(this, fFindString, fFindSelection, 692 fMatchWord, fMatchCase, fForwardSearch); 693 } 694 else 695 fFindPanel->Activate(); 696 break; 697 698 case MSG_FIND: 699 { 700 fFindPanel->PostMessage(B_QUIT_REQUESTED); 701 message->FindBool("findselection", &fFindSelection); 702 if (!fFindSelection) 703 message->FindString("findstring", &fFindString); 704 else 705 _ActiveTermView()->GetSelection(fFindString); 706 707 if (fFindString.Length() == 0) { 708 const char* errorMsg = !fFindSelection 709 ? B_TRANSLATE("No search string was entered.") 710 : B_TRANSLATE("Nothing is selected."); 711 BAlert* alert = new BAlert(B_TRANSLATE("Find failed"), 712 errorMsg, B_TRANSLATE("OK"), NULL, NULL, 713 B_WIDTH_AS_USUAL, B_WARNING_ALERT); 714 alert->SetShortcut(0, B_ESCAPE); 715 716 alert->Go(); 717 fFindPreviousMenuItem->SetEnabled(false); 718 fFindNextMenuItem->SetEnabled(false); 719 break; 720 } 721 722 message->FindBool("forwardsearch", &fForwardSearch); 723 message->FindBool("matchcase", &fMatchCase); 724 message->FindBool("matchword", &fMatchWord); 725 findresult = _ActiveTermView()->Find(fFindString, fForwardSearch, fMatchCase, fMatchWord); 726 727 if (!findresult) { 728 BAlert* alert = new BAlert(B_TRANSLATE("Find failed"), 729 B_TRANSLATE("Text not found."), 730 B_TRANSLATE("OK"), NULL, NULL, 731 B_WIDTH_AS_USUAL, B_WARNING_ALERT); 732 alert->SetShortcut(0, B_ESCAPE); 733 alert->Go(); 734 fFindPreviousMenuItem->SetEnabled(false); 735 fFindNextMenuItem->SetEnabled(false); 736 break; 737 } 738 739 // Enable the menu items Find Next and Find Previous 740 fFindPreviousMenuItem->SetEnabled(true); 741 fFindNextMenuItem->SetEnabled(true); 742 break; 743 } 744 745 case MENU_FIND_NEXT: 746 case MENU_FIND_PREVIOUS: 747 findresult = _ActiveTermView()->Find(fFindString, 748 (message->what == MENU_FIND_NEXT) == fForwardSearch, 749 fMatchCase, fMatchWord); 750 if (!findresult) { 751 BAlert* alert = new BAlert(B_TRANSLATE("Find failed"), 752 B_TRANSLATE("Not found."), B_TRANSLATE("OK"), 753 NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); 754 alert->SetShortcut(0, B_ESCAPE); 755 alert->Go(); 756 } 757 break; 758 759 case MSG_FIND_CLOSED: 760 fFindPanel = NULL; 761 break; 762 763 case MENU_ENCODING: 764 if (message->FindInt32("op", &encodingId) == B_OK) 765 _ActiveTermView()->SetEncoding(encodingId); 766 break; 767 768 case MSG_COLS_CHANGED: 769 { 770 int32 columns, rows; 771 message->FindInt32("columns", &columns); 772 message->FindInt32("rows", &rows); 773 774 _ActiveTermView()->SetTermSize(rows, columns); 775 776 _ResizeView(_ActiveTermView()); 777 break; 778 } 779 case MSG_HALF_FONT_CHANGED: 780 case MSG_FULL_FONT_CHANGED: 781 case MSG_HALF_SIZE_CHANGED: 782 case MSG_FULL_SIZE_CHANGED: 783 { 784 BFont font; 785 _GetPreferredFont(font); 786 _ActiveTermView()->SetTermFont(&font); 787 788 _ResizeView(_ActiveTermView()); 789 break; 790 } 791 792 case FULLSCREEN: 793 if (!fSavedFrame.IsValid()) { // go fullscreen 794 _ActiveTermView()->DisableResizeView(); 795 float mbHeight = fMenuBar->Bounds().Height() + 1; 796 fSavedFrame = Frame(); 797 BScreen screen(this); 798 for (int32 i = fTabView->CountTabs() - 1; i >=0 ; i--) 799 _TermViewAt(i)->ScrollBar()->ResizeBy(0, (B_H_SCROLL_BAR_HEIGHT - 1)); 800 801 fMenuBar->Hide(); 802 fTabView->ResizeBy(0, mbHeight); 803 fTabView->MoveBy(0, -mbHeight); 804 fSavedLook = Look(); 805 // done before ResizeTo to work around a Dano bug (not erasing the decor) 806 SetLook(B_NO_BORDER_WINDOW_LOOK); 807 ResizeTo(screen.Frame().Width()+1, screen.Frame().Height()+1); 808 MoveTo(screen.Frame().left, screen.Frame().top); 809 fFullScreen = true; 810 } else { // exit fullscreen 811 _ActiveTermView()->DisableResizeView(); 812 float mbHeight = fMenuBar->Bounds().Height() + 1; 813 fMenuBar->Show(); 814 for (int32 i = fTabView->CountTabs() - 1; i >=0 ; i--) 815 _TermViewAt(i)->ScrollBar()->ResizeBy(0, -(B_H_SCROLL_BAR_HEIGHT - 1)); 816 ResizeTo(fSavedFrame.Width(), fSavedFrame.Height()); 817 MoveTo(fSavedFrame.left, fSavedFrame.top); 818 fTabView->ResizeBy(0, -mbHeight); 819 fTabView->MoveBy(0, mbHeight); 820 SetLook(fSavedLook); 821 fSavedFrame = BRect(0,0,-1,-1); 822 fFullScreen = false; 823 } 824 break; 825 826 case MSG_FONT_CHANGED: 827 PostMessage(MSG_HALF_FONT_CHANGED); 828 break; 829 830 case MSG_COLOR_CHANGED: 831 case MSG_COLOR_SCHEMA_CHANGED: 832 { 833 _SetTermColors(_ActiveTermViewContainerView()); 834 _ActiveTermViewContainerView()->Invalidate(); 835 _ActiveTermView()->Invalidate(); 836 break; 837 } 838 839 case SAVE_AS_DEFAULT: 840 { 841 BPath path; 842 if (PrefHandler::GetDefaultPath(path) == B_OK) 843 PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE); 844 break; 845 } 846 case MENU_PAGE_SETUP: 847 _DoPageSetup(); 848 break; 849 850 case MENU_PRINT: 851 _DoPrint(); 852 break; 853 854 case MSG_CHECK_CHILDREN: 855 _CheckChildren(); 856 break; 857 858 case MSG_MOVE_TAB_LEFT: 859 case MSG_MOVE_TAB_RIGHT: 860 _NavigateTab(_IndexOfTermView(_ActiveTermView()), 861 message->what == MSG_MOVE_TAB_LEFT ? -1 : 1, true); 862 break; 863 864 case kTabTitleChanged: 865 { 866 // tab title changed message from SetTitleDialog 867 SessionID sessionID(*message, "session"); 868 if (Session* session = _SessionForID(sessionID)) { 869 BString title; 870 if (message->FindString("title", &title) == B_OK) { 871 session->title.pattern = title; 872 session->title.patternUserDefined = true; 873 } else { 874 session->title.pattern.Truncate(0); 875 session->title.patternUserDefined = false; 876 } 877 _UpdateSessionTitle(_IndexOfSession(session)); 878 } 879 break; 880 } 881 882 case kWindowTitleChanged: 883 { 884 // window title changed message from SetTitleDialog 885 BString title; 886 if (message->FindString("title", &title) == B_OK) { 887 fTitle.pattern = title; 888 fTitle.patternUserDefined = true; 889 } else { 890 fTitle.pattern 891 = PrefHandler::Default()->getString(PREF_WINDOW_TITLE); 892 fTitle.patternUserDefined = false; 893 } 894 895 _UpdateSessionTitle(fTabView->Selection()); 896 // updates the window title as a side effect 897 898 break; 899 } 900 901 case kSetActiveTab: 902 { 903 int32 index; 904 if (message->FindInt32("index", &index) == B_OK 905 && index >= 0 && index < fSessions.CountItems()) { 906 fTabView->Select(index); 907 } 908 break; 909 } 910 911 case kNewTab: 912 _NewTab(); 913 break; 914 915 case kCloseView: 916 { 917 int32 index = -1; 918 SessionID sessionID(*message, "session"); 919 if (sessionID.IsValid()) { 920 if (Session* session = _SessionForID(sessionID)) 921 index = _IndexOfSession(session); 922 } else 923 index = _IndexOfTermView(_ActiveTermView()); 924 925 if (index >= 0) 926 _RemoveTab(index); 927 928 break; 929 } 930 931 case kCloseOtherViews: 932 { 933 Session* session = _SessionForID(SessionID(*message, "session")); 934 if (session == NULL) 935 break; 936 937 int32 count = fSessions.CountItems(); 938 for (int32 i = count - 1; i >= 0; i--) { 939 if (_SessionAt(i) != session) 940 _RemoveTab(i); 941 } 942 943 break; 944 } 945 946 case kIncreaseFontSize: 947 case kDecreaseFontSize: 948 { 949 TermView* view = _ActiveTermView(); 950 BFont font; 951 view->GetTermFont(&font); 952 953 float size = font.Size(); 954 if (message->what == kIncreaseFontSize) 955 size += 1; 956 else 957 size -= 1; 958 959 // limit the font size 960 if (size < 9) 961 size = 9; 962 if (size > 18) 963 size = 18; 964 965 font.SetSize(size); 966 view->SetTermFont(&font); 967 PrefHandler::Default()->setInt32(PREF_HALF_FONT_SIZE, (int32)size); 968 969 _ResizeView(view); 970 break; 971 } 972 973 case kUpdateTitles: 974 _UpdateTitles(); 975 break; 976 977 case kEditTabTitle: 978 { 979 SessionID sessionID(*message, "session"); 980 if (Session* session = _SessionForID(sessionID)) 981 _OpenSetTabTitleDialog(_IndexOfSession(session)); 982 break; 983 } 984 985 case kEditWindowTitle: 986 _OpenSetWindowTitleDialog(); 987 break; 988 989 case kUpdateSwitchTerminalsMenuItem: 990 _UpdateSwitchTerminalsMenuItem(); 991 break; 992 993 default: 994 BWindow::MessageReceived(message); 995 break; 996 } 997 } 998 999 1000 void 1001 TermWindow::WindowActivated(bool activated) 1002 { 1003 if (activated) 1004 _UpdateSwitchTerminalsMenuItem(); 1005 } 1006 1007 1008 void 1009 TermWindow::_SetTermColors(TermViewContainerView* containerView) 1010 { 1011 PrefHandler* handler = PrefHandler::Default(); 1012 rgb_color background = handler->getRGB(PREF_TEXT_BACK_COLOR); 1013 1014 containerView->SetViewColor(background); 1015 1016 TermView *termView = containerView->GetTermView(); 1017 termView->SetTextColor(handler->getRGB(PREF_TEXT_FORE_COLOR), background); 1018 1019 termView->SetSelectColor(handler->getRGB(PREF_SELECT_FORE_COLOR), 1020 handler->getRGB(PREF_SELECT_BACK_COLOR)); 1021 } 1022 1023 1024 status_t 1025 TermWindow::_DoPageSetup() 1026 { 1027 BPrintJob job("PageSetup"); 1028 1029 // display the page configure panel 1030 status_t status = job.ConfigPage(); 1031 1032 // save a pointer to the settings 1033 fPrintSettings = job.Settings(); 1034 1035 return status; 1036 } 1037 1038 1039 void 1040 TermWindow::_DoPrint() 1041 { 1042 BPrintJob job("Print"); 1043 if (fPrintSettings) 1044 job.SetSettings(new BMessage(*fPrintSettings)); 1045 1046 if (job.ConfigJob() != B_OK) 1047 return; 1048 1049 BRect pageRect = job.PrintableRect(); 1050 BRect curPageRect = pageRect; 1051 1052 int pHeight = (int)pageRect.Height(); 1053 int pWidth = (int)pageRect.Width(); 1054 float w, h; 1055 _ActiveTermView()->GetFrameSize(&w, &h); 1056 int xPages = (int)ceil(w / pWidth); 1057 int yPages = (int)ceil(h / pHeight); 1058 1059 job.BeginJob(); 1060 1061 // loop through and draw each page, and write to spool 1062 for (int x = 0; x < xPages; x++) { 1063 for (int y = 0; y < yPages; y++) { 1064 curPageRect.OffsetTo(x * pWidth, y * pHeight); 1065 job.DrawView(_ActiveTermView(), curPageRect, B_ORIGIN); 1066 job.SpoolPage(); 1067 1068 if (!job.CanContinue()) { 1069 // It is likely that the only way that the job was cancelled is 1070 // because the user hit 'Cancel' in the page setup window, in 1071 // which case, the user does *not* need to be told that it was 1072 // cancelled. 1073 // He/she will simply expect that it was done. 1074 return; 1075 } 1076 } 1077 } 1078 1079 job.CommitJob(); 1080 } 1081 1082 1083 void 1084 TermWindow::_NewTab() 1085 { 1086 if (fTabView->CountTabs() < kMaxTabs) { 1087 ActiveProcessInfo info; 1088 if (_ActiveTermView()->GetActiveProcessInfo(info)) 1089 _AddTab(NULL, info.CurrentDirectory()); 1090 else 1091 _AddTab(NULL); 1092 } 1093 } 1094 1095 1096 void 1097 TermWindow::_AddTab(Arguments* args, const BString& currentDirectory) 1098 { 1099 int argc = 0; 1100 const char* const* argv = NULL; 1101 if (args != NULL) 1102 args->GetShellArguments(argc, argv); 1103 ShellParameters shellParameters(argc, argv, currentDirectory); 1104 1105 try { 1106 TermView* view = new TermView( 1107 PrefHandler::Default()->getInt32(PREF_ROWS), 1108 PrefHandler::Default()->getInt32(PREF_COLS), 1109 shellParameters, 1110 PrefHandler::Default()->getInt32(PREF_HISTORY_SIZE)); 1111 view->SetListener(this); 1112 1113 TermViewContainerView* containerView = new TermViewContainerView(view); 1114 BScrollView* scrollView = new TermScrollView("scrollView", 1115 containerView, view, fSessions.IsEmpty()); 1116 if (!fFullScreen) 1117 scrollView->ScrollBar(B_VERTICAL)->ResizeBy(0, -(B_H_SCROLL_BAR_HEIGHT - 1)); 1118 1119 if (fSessions.IsEmpty()) 1120 fTabView->SetScrollView(scrollView); 1121 1122 Session* session = new Session(_NewSessionID(), _NewSessionIndex(), 1123 containerView); 1124 fSessions.AddItem(session); 1125 1126 BFont font; 1127 _GetPreferredFont(font); 1128 view->SetTermFont(&font); 1129 1130 int width, height; 1131 view->GetFontSize(&width, &height); 1132 1133 float minimumHeight = -1; 1134 if (fMenuBar) 1135 minimumHeight += fMenuBar->Bounds().Height() + 1; 1136 if (fTabView && fTabView->CountTabs() > 0) 1137 minimumHeight += fTabView->TabHeight() + 1; 1138 SetSizeLimits(MIN_COLS * width - 1, MAX_COLS * width - 1, 1139 minimumHeight + MIN_ROWS * height - 1, 1140 minimumHeight + MAX_ROWS * height - 1); 1141 // TODO: The size limit computation is apparently broken, since 1142 // the terminal can be resized smaller than MIN_ROWS/MIN_COLS! 1143 1144 // If it's the first time we're called, setup the window 1145 if (fTabView->CountTabs() == 0) { 1146 float viewWidth, viewHeight; 1147 containerView->GetPreferredSize(&viewWidth, &viewHeight); 1148 1149 // Resize Window 1150 ResizeTo(viewWidth + B_V_SCROLL_BAR_WIDTH, 1151 viewHeight + fMenuBar->Bounds().Height() + 1); 1152 // NOTE: Width is one pixel too small, since the scroll view 1153 // is one pixel wider than its parent. 1154 } 1155 1156 BTab* tab = new BTab; 1157 fTabView->AddTab(scrollView, tab); 1158 view->SetScrollBar(scrollView->ScrollBar(B_VERTICAL)); 1159 view->SetMouseClipboard(gMouseClipboard); 1160 1161 const BCharacterSet* charset 1162 = BCharacterSetRoster::FindCharacterSetByName( 1163 PrefHandler::Default()->getString(PREF_TEXT_ENCODING)); 1164 if (charset != NULL) 1165 view->SetEncoding(charset->GetConversionID()); 1166 1167 _SetTermColors(containerView); 1168 1169 int32 tabIndex = fTabView->CountTabs() - 1; 1170 fTabView->Select(tabIndex); 1171 1172 _UpdateSessionTitle(tabIndex); 1173 } catch (...) { 1174 // most probably out of memory. That's bad. 1175 // TODO: Should cleanup, I guess 1176 1177 // Quit the application if we don't have a shell already 1178 if (fTabView->CountTabs() == 0) { 1179 fprintf(stderr, "Terminal couldn't open a shell\n"); 1180 PostMessage(B_QUIT_REQUESTED); 1181 } 1182 } 1183 } 1184 1185 1186 void 1187 TermWindow::_RemoveTab(int32 index) 1188 { 1189 _FinishTitleDialog(); 1190 // always close to avoid confusion 1191 1192 if (fSessions.CountItems() > 1) { 1193 if (!_CanClose(index)) 1194 return; 1195 if (Session* session = (Session*)fSessions.RemoveItem(index)) { 1196 if (fSessions.CountItems() == 1) { 1197 fTabView->SetScrollView(dynamic_cast<BScrollView*>( 1198 _SessionAt(0)->containerView->Parent())); 1199 } 1200 1201 delete session; 1202 delete fTabView->RemoveTab(index); 1203 } 1204 } else 1205 PostMessage(B_QUIT_REQUESTED); 1206 } 1207 1208 1209 void 1210 TermWindow::_NavigateTab(int32 index, int32 direction, bool move) 1211 { 1212 int32 count = fSessions.CountItems(); 1213 if (count <= 1 || index < 0 || index >= count) 1214 return; 1215 1216 int32 newIndex = (index + direction + count) % count; 1217 if (newIndex == index) 1218 return; 1219 1220 if (move) { 1221 // move the given tab to the new index 1222 Session* session = (Session*)fSessions.RemoveItem(index); 1223 fSessions.AddItem(session, newIndex); 1224 fTabView->MoveTab(index, newIndex); 1225 } 1226 1227 // activate the respective tab 1228 fTabView->Select(newIndex); 1229 } 1230 1231 1232 TermViewContainerView* 1233 TermWindow::_ActiveTermViewContainerView() const 1234 { 1235 return _TermViewContainerViewAt(fTabView->Selection()); 1236 } 1237 1238 1239 TermViewContainerView* 1240 TermWindow::_TermViewContainerViewAt(int32 index) const 1241 { 1242 if (Session* session = _SessionAt(index)) 1243 return session->containerView; 1244 return NULL; 1245 } 1246 1247 1248 TermView* 1249 TermWindow::_ActiveTermView() const 1250 { 1251 return _ActiveTermViewContainerView()->GetTermView(); 1252 } 1253 1254 1255 TermView* 1256 TermWindow::_TermViewAt(int32 index) const 1257 { 1258 TermViewContainerView* view = _TermViewContainerViewAt(index); 1259 return view != NULL ? view->GetTermView() : NULL; 1260 } 1261 1262 1263 int32 1264 TermWindow::_IndexOfTermView(TermView* termView) const 1265 { 1266 if (!termView) 1267 return -1; 1268 1269 // find the view 1270 int32 count = fTabView->CountTabs(); 1271 for (int32 i = count - 1; i >= 0; i--) { 1272 if (termView == _TermViewAt(i)) 1273 return i; 1274 } 1275 1276 return -1; 1277 } 1278 1279 1280 TermWindow::Session* 1281 TermWindow::_SessionAt(int32 index) const 1282 { 1283 return (Session*)fSessions.ItemAt(index); 1284 } 1285 1286 1287 TermWindow::Session* 1288 TermWindow::_SessionForID(const SessionID& sessionID) const 1289 { 1290 for (int32 i = 0; Session* session = _SessionAt(i); i++) { 1291 if (session->id == sessionID) 1292 return session; 1293 } 1294 1295 return NULL; 1296 } 1297 1298 1299 int32 1300 TermWindow::_IndexOfSession(Session* session) const 1301 { 1302 return fSessions.IndexOf(session); 1303 } 1304 1305 1306 void 1307 TermWindow::_CheckChildren() 1308 { 1309 int32 count = fSessions.CountItems(); 1310 for (int32 i = count - 1; i >= 0; i--) { 1311 Session* session = _SessionAt(i); 1312 if (session->containerView->GetTermView()->CheckShellGone()) 1313 NotifyTermViewQuit(session->containerView->GetTermView(), 0); 1314 } 1315 } 1316 1317 1318 void 1319 TermWindow::Zoom(BPoint leftTop, float width, float height) 1320 { 1321 _ActiveTermView()->DisableResizeView(); 1322 BWindow::Zoom(leftTop, width, height); 1323 } 1324 1325 1326 void 1327 TermWindow::FrameResized(float newWidth, float newHeight) 1328 { 1329 BWindow::FrameResized(newWidth, newHeight); 1330 1331 TermView* view = _ActiveTermView(); 1332 PrefHandler::Default()->setInt32(PREF_COLS, view->Columns()); 1333 PrefHandler::Default()->setInt32(PREF_ROWS, view->Rows()); 1334 } 1335 1336 1337 void 1338 TermWindow::WorkspacesChanged(uint32 oldWorkspaces, uint32 newWorkspaces) 1339 { 1340 fTerminalRoster.SetWindowInfo(IsMinimized(), Workspaces()); 1341 } 1342 1343 1344 void 1345 TermWindow::WorkspaceActivated(int32 workspace, bool state) 1346 { 1347 fTerminalRoster.SetWindowInfo(IsMinimized(), Workspaces()); 1348 } 1349 1350 1351 void 1352 TermWindow::Minimize(bool minimize) 1353 { 1354 BWindow::Minimize(minimize); 1355 fTerminalRoster.SetWindowInfo(IsMinimized(), Workspaces()); 1356 } 1357 1358 1359 void 1360 TermWindow::TabSelected(SmartTabView* tabView, int32 index) 1361 { 1362 SessionChanged(); 1363 } 1364 1365 1366 void 1367 TermWindow::TabDoubleClicked(SmartTabView* tabView, BPoint point, int32 index) 1368 { 1369 if (index >= 0) { 1370 // clicked on a tab -- open the title dialog 1371 _OpenSetTabTitleDialog(index); 1372 } else { 1373 // not clicked on a tab -- create a new one 1374 _NewTab(); 1375 } 1376 } 1377 1378 1379 void 1380 TermWindow::TabMiddleClicked(SmartTabView* tabView, BPoint point, int32 index) 1381 { 1382 if (index >= 0) 1383 _RemoveTab(index); 1384 } 1385 1386 1387 void 1388 TermWindow::TabRightClicked(SmartTabView* tabView, BPoint point, int32 index) 1389 { 1390 if (index < 0) 1391 return; 1392 1393 TermView* termView = _TermViewAt(index); 1394 if (termView == NULL) 1395 return; 1396 1397 BMessage* closeMessage = new BMessage(kCloseView); 1398 _SessionAt(index)->id.AddToMessage(*closeMessage, "session"); 1399 1400 BMessage* closeOthersMessage = new BMessage(kCloseOtherViews); 1401 _SessionAt(index)->id.AddToMessage(*closeOthersMessage, "session"); 1402 1403 BMessage* editTitleMessage = new BMessage(kEditTabTitle); 1404 _SessionAt(index)->id.AddToMessage(*editTitleMessage, "session"); 1405 1406 BPopUpMenu* popUpMenu = new BPopUpMenu("tab menu"); 1407 BLayoutBuilder::Menu<>(popUpMenu) 1408 .AddItem(B_TRANSLATE("Close tab"), closeMessage) 1409 .AddItem(B_TRANSLATE("Close other tabs"), closeOthersMessage) 1410 .AddSeparator() 1411 .AddItem(B_TRANSLATE("Edit tab title" B_UTF8_ELLIPSIS), 1412 editTitleMessage) 1413 ; 1414 1415 popUpMenu->SetAsyncAutoDestruct(true); 1416 popUpMenu->SetTargetForItems(BMessenger(this)); 1417 1418 BPoint screenWhere = tabView->ConvertToScreen(point); 1419 BRect mouseRect(screenWhere, screenWhere); 1420 mouseRect.InsetBy(-4.0, -4.0); 1421 popUpMenu->Go(screenWhere, true, true, mouseRect, true); 1422 } 1423 1424 1425 void 1426 TermWindow::NotifyTermViewQuit(TermView* view, int32 reason) 1427 { 1428 // Since the notification can come from the view, we send a message to 1429 // ourselves to avoid deleting the caller synchronously. 1430 if (Session* session = _SessionAt(_IndexOfTermView(view))) { 1431 BMessage message(kCloseView); 1432 session->id.AddToMessage(message, "session"); 1433 message.AddInt32("reason", reason); 1434 PostMessage(&message); 1435 } 1436 } 1437 1438 1439 void 1440 TermWindow::SetTermViewTitle(TermView* view, const char* title) 1441 { 1442 int32 index = _IndexOfTermView(view); 1443 if (Session* session = _SessionAt(index)) { 1444 session->title.pattern = title; 1445 session->title.patternUserDefined = true; 1446 _UpdateSessionTitle(index); 1447 } 1448 } 1449 1450 1451 void 1452 TermWindow::TitleChanged(SetTitleDialog* dialog, const BString& title, 1453 bool titleUserDefined) 1454 { 1455 if (dialog == fSetTabTitleDialog) { 1456 // tab title 1457 BMessage message(kTabTitleChanged); 1458 fSetTabTitleSession.AddToMessage(message, "session"); 1459 if (titleUserDefined) 1460 message.AddString("title", title); 1461 1462 PostMessage(&message); 1463 } else if (dialog == fSetWindowTitleDialog) { 1464 // window title 1465 BMessage message(kWindowTitleChanged); 1466 if (titleUserDefined) 1467 message.AddString("title", title); 1468 1469 PostMessage(&message); 1470 } 1471 } 1472 1473 1474 void 1475 TermWindow::SetTitleDialogDone(SetTitleDialog* dialog) 1476 { 1477 if (dialog == fSetTabTitleDialog) { 1478 fSetTabTitleSession = SessionID(); 1479 fSetTabTitleDialog = NULL; 1480 // assuming this is atomic 1481 } 1482 } 1483 1484 1485 void 1486 TermWindow::TerminalInfosUpdated(TerminalRoster* roster) 1487 { 1488 PostMessage(kUpdateSwitchTerminalsMenuItem); 1489 } 1490 1491 1492 void 1493 TermWindow::PreviousTermView(TermView* view) 1494 { 1495 _NavigateTab(_IndexOfTermView(view), -1, false); 1496 } 1497 1498 1499 void 1500 TermWindow::NextTermView(TermView* view) 1501 { 1502 _NavigateTab(_IndexOfTermView(view), 1, false); 1503 } 1504 1505 1506 void 1507 TermWindow::_ResizeView(TermView *view) 1508 { 1509 int fontWidth, fontHeight; 1510 view->GetFontSize(&fontWidth, &fontHeight); 1511 1512 float minimumHeight = -1; 1513 if (fMenuBar) 1514 minimumHeight += fMenuBar->Bounds().Height() + 1; 1515 if (fTabView && fTabView->CountTabs() > 1) 1516 minimumHeight += fTabView->TabHeight() + 1; 1517 1518 SetSizeLimits(MIN_COLS * fontWidth - 1, MAX_COLS * fontWidth - 1, 1519 minimumHeight + MIN_ROWS * fontHeight - 1, 1520 minimumHeight + MAX_ROWS * fontHeight - 1); 1521 1522 float width; 1523 float height; 1524 view->Parent()->GetPreferredSize(&width, &height); 1525 width += B_V_SCROLL_BAR_WIDTH; 1526 // NOTE: Width is one pixel too small, since the scroll view 1527 // is one pixel wider than its parent. 1528 height += fMenuBar->Bounds().Height() + 1; 1529 1530 ResizeTo(width, height); 1531 1532 view->Invalidate(); 1533 } 1534 1535 1536 /* static */ 1537 BMenu* 1538 TermWindow::_MakeWindowSizeMenu() 1539 { 1540 BMenu* menu = new (std::nothrow) BMenu(B_TRANSLATE("Window size")); 1541 if (menu == NULL) 1542 return NULL; 1543 1544 const int32 windowSizes[4][2] = { 1545 { 80, 25 }, 1546 { 80, 40 }, 1547 { 132, 25 }, 1548 { 132, 40 } 1549 }; 1550 1551 const int32 sizeNum = sizeof(windowSizes) / sizeof(windowSizes[0]); 1552 for (int32 i = 0; i < sizeNum; i++) { 1553 char label[32]; 1554 int32 columns = windowSizes[i][0]; 1555 int32 rows = windowSizes[i][1]; 1556 snprintf(label, sizeof(label), "%ldx%ld", columns, rows); 1557 BMessage* message = new BMessage(MSG_COLS_CHANGED); 1558 message->AddInt32("columns", columns); 1559 message->AddInt32("rows", rows); 1560 menu->AddItem(new BMenuItem(label, message)); 1561 } 1562 1563 menu->AddSeparatorItem(); 1564 menu->AddItem(new BMenuItem(B_TRANSLATE("Full screen"), 1565 new BMessage(FULLSCREEN), B_ENTER)); 1566 1567 return menu; 1568 } 1569 1570 1571 void 1572 TermWindow::_UpdateSwitchTerminalsMenuItem() 1573 { 1574 fSwitchTerminalsMenuItem->SetEnabled(_FindSwitchTerminalTarget() >= 0); 1575 } 1576 1577 1578 void 1579 TermWindow::_TitleSettingsChanged() 1580 { 1581 if (!fTitle.patternUserDefined) 1582 fTitle.pattern = PrefHandler::Default()->getString(PREF_WINDOW_TITLE); 1583 1584 fSessionTitlePattern = PrefHandler::Default()->getString(PREF_TAB_TITLE); 1585 1586 _UpdateTitles(); 1587 } 1588 1589 1590 void 1591 TermWindow::_UpdateTitles() 1592 { 1593 int32 sessionCount = fSessions.CountItems(); 1594 for (int32 i = 0; i < sessionCount; i++) 1595 _UpdateSessionTitle(i); 1596 } 1597 1598 1599 void 1600 TermWindow::_UpdateSessionTitle(int32 index) 1601 { 1602 Session* session = _SessionAt(index); 1603 if (session == NULL) 1604 return; 1605 1606 // get the shell and active process infos 1607 ShellInfo shellInfo; 1608 ActiveProcessInfo activeProcessInfo; 1609 TermView* termView = _TermViewAt(index); 1610 if (!termView->GetShellInfo(shellInfo) 1611 || !termView->GetActiveProcessInfo(activeProcessInfo)) { 1612 return; 1613 } 1614 1615 // evaluate the session title pattern 1616 BString sessionTitlePattern = session->title.patternUserDefined 1617 ? session->title.pattern : fSessionTitlePattern; 1618 TabTitlePlaceholderMapper tabMapper(shellInfo, activeProcessInfo, 1619 session->index); 1620 const BString& sessionTitle = PatternEvaluator::Evaluate( 1621 sessionTitlePattern, tabMapper); 1622 1623 // set the tab title 1624 if (sessionTitle != session->title.title) { 1625 session->title.title = sessionTitle; 1626 fTabView->TabAt(index)->SetLabel(session->title.title); 1627 fTabView->Invalidate(); 1628 // Invalidate the complete tab view, since other tabs might change 1629 // their positions. 1630 } 1631 1632 // If this is the active tab, also recompute the window title. 1633 if (index != fTabView->Selection()) 1634 return; 1635 1636 // evaluate the window title pattern 1637 WindowTitlePlaceholderMapper windowMapper(shellInfo, activeProcessInfo, 1638 fTerminalRoster.ID() + 1, sessionTitle); 1639 const BString& windowTitle = PatternEvaluator::Evaluate(fTitle.pattern, 1640 windowMapper); 1641 1642 // set the window title 1643 if (windowTitle != fTitle.title) { 1644 fTitle.title = windowTitle; 1645 SetTitle(fTitle.title); 1646 } 1647 } 1648 1649 1650 void 1651 TermWindow::_OpenSetTabTitleDialog(int32 index) 1652 { 1653 // If a dialog is active, finish it. 1654 _FinishTitleDialog(); 1655 1656 BString toolTip = BString(B_TRANSLATE( 1657 "The pattern specifying the current tab title. The following " 1658 "placeholders\n" 1659 "can be used:\n")) << kTooTipSetTabTitlePlaceholders; 1660 fSetTabTitleDialog = new SetTitleDialog( 1661 B_TRANSLATE("Set tab title"), B_TRANSLATE("Tab title:"), 1662 toolTip); 1663 1664 Session* session = _SessionAt(index); 1665 bool userDefined = session->title.patternUserDefined; 1666 const BString& title = userDefined 1667 ? session->title.pattern : fSessionTitlePattern; 1668 fSetTabTitleSession = session->id; 1669 1670 // place the dialog window directly under the tab, but keep it on screen 1671 BPoint location = fTabView->ConvertToScreen( 1672 fTabView->TabFrame(index).LeftBottom() + BPoint(0, 1)); 1673 BRect frame(fSetTabTitleDialog->Frame().OffsetToCopy(location)); 1674 BSize screenSize(BScreen(fSetTabTitleDialog).Frame().Size()); 1675 fSetTabTitleDialog->MoveTo( 1676 BLayoutUtils::MoveIntoFrame(frame, screenSize).LeftTop()); 1677 1678 fSetTabTitleDialog->Go(title, userDefined, this); 1679 } 1680 1681 1682 void 1683 TermWindow::_OpenSetWindowTitleDialog() 1684 { 1685 // If a dialog is active, finish it. 1686 _FinishTitleDialog(); 1687 1688 BString toolTip = BString(B_TRANSLATE( 1689 "The pattern specifying the window title. The following placeholders\n" 1690 "can be used:\n")) << kTooTipSetTabTitlePlaceholders; 1691 fSetWindowTitleDialog = new SetTitleDialog(B_TRANSLATE("Set window title"), 1692 B_TRANSLATE("Window title:"), toolTip); 1693 1694 // center the dialog in the window frame, but keep it on screen 1695 fSetWindowTitleDialog->CenterIn(Frame()); 1696 BRect frame(fSetWindowTitleDialog->Frame()); 1697 BSize screenSize(BScreen(fSetWindowTitleDialog).Frame().Size()); 1698 fSetWindowTitleDialog->MoveTo( 1699 BLayoutUtils::MoveIntoFrame(frame, screenSize).LeftTop()); 1700 1701 fSetWindowTitleDialog->Go(fTitle.pattern, fTitle.patternUserDefined, this); 1702 } 1703 1704 1705 void 1706 TermWindow::_FinishTitleDialog() 1707 { 1708 SetTitleDialog* oldDialog = fSetTabTitleDialog; 1709 if (oldDialog != NULL && oldDialog->Lock()) { 1710 // might have been unset in the meantime, so recheck 1711 if (fSetTabTitleDialog == oldDialog) { 1712 oldDialog->Finish(); 1713 // this also unsets the variables 1714 } 1715 oldDialog->Unlock(); 1716 return; 1717 } 1718 1719 oldDialog = fSetWindowTitleDialog; 1720 if (oldDialog != NULL && oldDialog->Lock()) { 1721 // might have been unset in the meantime, so recheck 1722 if (fSetWindowTitleDialog == oldDialog) { 1723 oldDialog->Finish(); 1724 // this also unsets the variable 1725 } 1726 oldDialog->Unlock(); 1727 return; 1728 } 1729 } 1730 1731 1732 void 1733 TermWindow::_SwitchTerminal() 1734 { 1735 team_id teamID = _FindSwitchTerminalTarget(); 1736 if (teamID < 0) 1737 return; 1738 1739 BMessenger app(TERM_SIGNATURE, teamID); 1740 app.SendMessage(MSG_ACTIVATE_TERM); 1741 } 1742 1743 1744 team_id 1745 TermWindow::_FindSwitchTerminalTarget() 1746 { 1747 AutoLocker<TerminalRoster> rosterLocker(fTerminalRoster); 1748 1749 team_id myTeamID = Team(); 1750 1751 int32 numTerms = fTerminalRoster.CountTerminals(); 1752 if (numTerms <= 1) 1753 return -1; 1754 1755 // Find our position in the Terminal teams. 1756 int32 i; 1757 1758 for (i = 0; i < numTerms; i++) { 1759 if (myTeamID == fTerminalRoster.TerminalAt(i)->team) 1760 break; 1761 } 1762 1763 if (i == numTerms) { 1764 // we didn't find ourselves -- that shouldn't happen 1765 return -1; 1766 } 1767 1768 uint32 currentWorkspace = 1L << current_workspace(); 1769 1770 while (true) { 1771 if (--i < 0) 1772 i = numTerms - 1; 1773 1774 const TerminalRoster::Info* info = fTerminalRoster.TerminalAt(i); 1775 if (info->team == myTeamID) { 1776 // That's ourselves again. We've run through the complete list. 1777 return -1; 1778 } 1779 1780 if (!info->minimized && (info->workspaces & currentWorkspace) != 0) 1781 return info->team; 1782 } 1783 } 1784 1785 1786 TermWindow::SessionID 1787 TermWindow::_NewSessionID() 1788 { 1789 return fNextSessionID++; 1790 } 1791 1792 1793 int32 1794 TermWindow::_NewSessionIndex() 1795 { 1796 for (int32 id = 1; ; id++) { 1797 bool used = false; 1798 1799 for (int32 i = 0; 1800 Session* session = _SessionAt(i); i++) { 1801 if (id == session->index) { 1802 used = true; 1803 break; 1804 } 1805 } 1806 1807 if (!used) 1808 return id; 1809 } 1810 } 1811