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_windows"); 521 if (status != B_OK) 522 return status; 523 524 return file->SetTo(path.Path(), openMode); 525 } 526 527 528 status_t 529 TermWindow::_LoadWindowPosition(BRect* frame, uint32* workspaces) 530 { 531 status_t status; 532 BMessage position; 533 534 BFile file; 535 status = _GetWindowPositionFile(&file, B_READ_ONLY); 536 if (status != B_OK) 537 return status; 538 539 status = position.Unflatten(&file); 540 541 file.Unset(); 542 543 if (status != B_OK) 544 return status; 545 546 int32 id = fTerminalRoster.ID(); 547 status = position.FindRect("rect", id, frame); 548 if (status != B_OK) 549 return status; 550 551 int32 _workspaces; 552 status = position.FindInt32("workspaces", id, &_workspaces); 553 if (status != B_OK) 554 return status; 555 if (modifiers() & B_SHIFT_KEY) 556 *workspaces = _workspaces; 557 else 558 *workspaces = B_CURRENT_WORKSPACE; 559 560 return B_OK; 561 } 562 563 564 status_t 565 TermWindow::_SaveWindowPosition() 566 { 567 BFile file; 568 BMessage originalSettings; 569 570 // Read the settings file if it exists and is a valid BMessage. 571 status_t status = _GetWindowPositionFile(&file, B_READ_ONLY); 572 if (status == B_OK) { 573 status = originalSettings.Unflatten(&file); 574 file.Unset(); 575 576 if (status != B_OK) 577 status = originalSettings.MakeEmpty(); 578 579 if (status != B_OK) 580 return status; 581 } 582 583 // Replace the settings 584 int32 id = fTerminalRoster.ID(); 585 BRect rect(Frame()); 586 if (originalSettings.ReplaceRect("rect", id, rect) != B_OK) 587 originalSettings.AddRect("rect", rect); 588 589 int32 workspaces = Workspaces(); 590 if (originalSettings.ReplaceInt32("workspaces", id, workspaces) != B_OK) 591 originalSettings.AddInt32("workspaces", workspaces); 592 593 // Resave the whole thing 594 status = _GetWindowPositionFile (&file, 595 B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 596 if (status != B_OK) 597 return status; 598 599 return originalSettings.Flatten(&file); 600 } 601 602 603 void 604 TermWindow::_GetPreferredFont(BFont& font) 605 { 606 // Default to be_fixed_font 607 font = be_fixed_font; 608 609 const char* family = PrefHandler::Default()->getString(PREF_HALF_FONT_FAMILY); 610 const char* style = PrefHandler::Default()->getString(PREF_HALF_FONT_STYLE); 611 612 font.SetFamilyAndStyle(family, style); 613 614 float size = PrefHandler::Default()->getFloat(PREF_HALF_FONT_SIZE); 615 if (size < 6.0f) 616 size = 6.0f; 617 font.SetSize(size); 618 } 619 620 621 void 622 TermWindow::MessageReceived(BMessage *message) 623 { 624 int32 encodingId; 625 bool findresult; 626 627 switch (message->what) { 628 case B_COPY: 629 _ActiveTermView()->Copy(be_clipboard); 630 break; 631 632 case B_PASTE: 633 _ActiveTermView()->Paste(be_clipboard); 634 break; 635 636 case B_SELECT_ALL: 637 _ActiveTermView()->SelectAll(); 638 break; 639 640 case MENU_CLEAR_ALL: 641 _ActiveTermView()->Clear(); 642 break; 643 644 case MENU_SWITCH_TERM: 645 _SwitchTerminal(); 646 break; 647 648 case MENU_NEW_TERM: 649 { 650 // Set our current working directory to that of the active tab, so 651 // that the new terminal and its shell inherit it. 652 // Note: That's a bit lame. We should rather fork() and change the 653 // CWD in the child, but since ATM there aren't any side effects of 654 // changing our CWD, we save ourselves the trouble. 655 ActiveProcessInfo activeProcessInfo; 656 if (_ActiveTermView()->GetActiveProcessInfo(activeProcessInfo)) 657 chdir(activeProcessInfo.CurrentDirectory()); 658 659 app_info info; 660 be_app->GetAppInfo(&info); 661 662 // try launching two different ways to work around possible problems 663 if (be_roster->Launch(&info.ref) != B_OK) 664 be_roster->Launch(TERM_SIGNATURE); 665 break; 666 } 667 668 case MENU_PREF_OPEN: 669 if (!fPrefWindow) { 670 fPrefWindow = new PrefWindow(this); 671 } 672 else 673 fPrefWindow->Activate(); 674 break; 675 676 case MSG_PREF_CLOSED: 677 fPrefWindow = NULL; 678 break; 679 680 case MSG_WINDOW_TITLE_SETTING_CHANGED: 681 case MSG_TAB_TITLE_SETTING_CHANGED: 682 _TitleSettingsChanged(); 683 break; 684 685 case MENU_FIND_STRING: 686 if (!fFindPanel) { 687 fFindPanel = new FindWindow(this, fFindString, fFindSelection, 688 fMatchWord, fMatchCase, fForwardSearch); 689 } 690 else 691 fFindPanel->Activate(); 692 break; 693 694 case MSG_FIND: 695 { 696 fFindPanel->PostMessage(B_QUIT_REQUESTED); 697 message->FindBool("findselection", &fFindSelection); 698 if (!fFindSelection) 699 message->FindString("findstring", &fFindString); 700 else 701 _ActiveTermView()->GetSelection(fFindString); 702 703 if (fFindString.Length() == 0) { 704 const char* errorMsg = !fFindSelection 705 ? B_TRANSLATE("No search string was entered.") 706 : B_TRANSLATE("Nothing is selected."); 707 BAlert* alert = new BAlert(B_TRANSLATE("Find failed"), 708 errorMsg, B_TRANSLATE("OK"), NULL, NULL, 709 B_WIDTH_AS_USUAL, B_WARNING_ALERT); 710 alert->SetShortcut(0, B_ESCAPE); 711 712 alert->Go(); 713 fFindPreviousMenuItem->SetEnabled(false); 714 fFindNextMenuItem->SetEnabled(false); 715 break; 716 } 717 718 message->FindBool("forwardsearch", &fForwardSearch); 719 message->FindBool("matchcase", &fMatchCase); 720 message->FindBool("matchword", &fMatchWord); 721 findresult = _ActiveTermView()->Find(fFindString, fForwardSearch, fMatchCase, fMatchWord); 722 723 if (!findresult) { 724 BAlert* alert = new BAlert(B_TRANSLATE("Find failed"), 725 B_TRANSLATE("Text not found."), 726 B_TRANSLATE("OK"), NULL, NULL, 727 B_WIDTH_AS_USUAL, B_WARNING_ALERT); 728 alert->SetShortcut(0, B_ESCAPE); 729 alert->Go(); 730 fFindPreviousMenuItem->SetEnabled(false); 731 fFindNextMenuItem->SetEnabled(false); 732 break; 733 } 734 735 // Enable the menu items Find Next and Find Previous 736 fFindPreviousMenuItem->SetEnabled(true); 737 fFindNextMenuItem->SetEnabled(true); 738 break; 739 } 740 741 case MENU_FIND_NEXT: 742 case MENU_FIND_PREVIOUS: 743 findresult = _ActiveTermView()->Find(fFindString, 744 (message->what == MENU_FIND_NEXT) == fForwardSearch, 745 fMatchCase, fMatchWord); 746 if (!findresult) { 747 BAlert* alert = new BAlert(B_TRANSLATE("Find failed"), 748 B_TRANSLATE("Not found."), B_TRANSLATE("OK"), 749 NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); 750 alert->SetShortcut(0, B_ESCAPE); 751 alert->Go(); 752 } 753 break; 754 755 case MSG_FIND_CLOSED: 756 fFindPanel = NULL; 757 break; 758 759 case MENU_ENCODING: 760 if (message->FindInt32("op", &encodingId) == B_OK) 761 _ActiveTermView()->SetEncoding(encodingId); 762 break; 763 764 case MSG_COLS_CHANGED: 765 { 766 int32 columns, rows; 767 message->FindInt32("columns", &columns); 768 message->FindInt32("rows", &rows); 769 770 _ActiveTermView()->SetTermSize(rows, columns); 771 772 _ResizeView(_ActiveTermView()); 773 break; 774 } 775 case MSG_HALF_FONT_CHANGED: 776 case MSG_FULL_FONT_CHANGED: 777 case MSG_HALF_SIZE_CHANGED: 778 case MSG_FULL_SIZE_CHANGED: 779 { 780 BFont font; 781 _GetPreferredFont(font); 782 _ActiveTermView()->SetTermFont(&font); 783 784 _ResizeView(_ActiveTermView()); 785 break; 786 } 787 788 case FULLSCREEN: 789 if (!fSavedFrame.IsValid()) { // go fullscreen 790 _ActiveTermView()->DisableResizeView(); 791 float mbHeight = fMenuBar->Bounds().Height() + 1; 792 fSavedFrame = Frame(); 793 BScreen screen(this); 794 for (int32 i = fTabView->CountTabs() - 1; i >=0 ; i--) 795 _TermViewAt(i)->ScrollBar()->ResizeBy(0, (B_H_SCROLL_BAR_HEIGHT - 1)); 796 797 fMenuBar->Hide(); 798 fTabView->ResizeBy(0, mbHeight); 799 fTabView->MoveBy(0, -mbHeight); 800 fSavedLook = Look(); 801 // done before ResizeTo to work around a Dano bug (not erasing the decor) 802 SetLook(B_NO_BORDER_WINDOW_LOOK); 803 ResizeTo(screen.Frame().Width()+1, screen.Frame().Height()+1); 804 MoveTo(screen.Frame().left, screen.Frame().top); 805 fFullScreen = true; 806 } else { // exit fullscreen 807 _ActiveTermView()->DisableResizeView(); 808 float mbHeight = fMenuBar->Bounds().Height() + 1; 809 fMenuBar->Show(); 810 for (int32 i = fTabView->CountTabs() - 1; i >=0 ; i--) 811 _TermViewAt(i)->ScrollBar()->ResizeBy(0, -(B_H_SCROLL_BAR_HEIGHT - 1)); 812 ResizeTo(fSavedFrame.Width(), fSavedFrame.Height()); 813 MoveTo(fSavedFrame.left, fSavedFrame.top); 814 fTabView->ResizeBy(0, -mbHeight); 815 fTabView->MoveBy(0, mbHeight); 816 SetLook(fSavedLook); 817 fSavedFrame = BRect(0,0,-1,-1); 818 fFullScreen = false; 819 } 820 break; 821 822 case MSG_FONT_CHANGED: 823 PostMessage(MSG_HALF_FONT_CHANGED); 824 break; 825 826 case MSG_COLOR_CHANGED: 827 case MSG_COLOR_SCHEMA_CHANGED: 828 { 829 _SetTermColors(_ActiveTermViewContainerView()); 830 _ActiveTermViewContainerView()->Invalidate(); 831 _ActiveTermView()->Invalidate(); 832 break; 833 } 834 835 case SAVE_AS_DEFAULT: 836 { 837 BPath path; 838 if (PrefHandler::GetDefaultPath(path) == B_OK) 839 PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE); 840 break; 841 } 842 case MENU_PAGE_SETUP: 843 _DoPageSetup(); 844 break; 845 846 case MENU_PRINT: 847 _DoPrint(); 848 break; 849 850 case MSG_CHECK_CHILDREN: 851 _CheckChildren(); 852 break; 853 854 case MSG_MOVE_TAB_LEFT: 855 case MSG_MOVE_TAB_RIGHT: 856 _NavigateTab(_IndexOfTermView(_ActiveTermView()), 857 message->what == MSG_MOVE_TAB_LEFT ? -1 : 1, true); 858 break; 859 860 case kTabTitleChanged: 861 { 862 // tab title changed message from SetTitleDialog 863 SessionID sessionID(*message, "session"); 864 if (Session* session = _SessionForID(sessionID)) { 865 BString title; 866 if (message->FindString("title", &title) == B_OK) { 867 session->title.pattern = title; 868 session->title.patternUserDefined = true; 869 } else { 870 session->title.pattern.Truncate(0); 871 session->title.patternUserDefined = false; 872 } 873 _UpdateSessionTitle(_IndexOfSession(session)); 874 } 875 break; 876 } 877 878 case kWindowTitleChanged: 879 { 880 // window title changed message from SetTitleDialog 881 BString title; 882 if (message->FindString("title", &title) == B_OK) { 883 fTitle.pattern = title; 884 fTitle.patternUserDefined = true; 885 } else { 886 fTitle.pattern 887 = PrefHandler::Default()->getString(PREF_WINDOW_TITLE); 888 fTitle.patternUserDefined = false; 889 } 890 891 _UpdateSessionTitle(fTabView->Selection()); 892 // updates the window title as a side effect 893 894 break; 895 } 896 897 case kSetActiveTab: 898 { 899 int32 index; 900 if (message->FindInt32("index", &index) == B_OK 901 && index >= 0 && index < fSessions.CountItems()) { 902 fTabView->Select(index); 903 } 904 break; 905 } 906 907 case kNewTab: 908 _NewTab(); 909 break; 910 911 case kCloseView: 912 { 913 int32 index = -1; 914 SessionID sessionID(*message, "session"); 915 if (sessionID.IsValid()) { 916 if (Session* session = _SessionForID(sessionID)) 917 index = _IndexOfSession(session); 918 } else 919 index = _IndexOfTermView(_ActiveTermView()); 920 921 if (index >= 0) 922 _RemoveTab(index); 923 924 break; 925 } 926 927 case kCloseOtherViews: 928 { 929 Session* session = _SessionForID(SessionID(*message, "session")); 930 if (session == NULL) 931 break; 932 933 int32 count = fSessions.CountItems(); 934 for (int32 i = count - 1; i >= 0; i--) { 935 if (_SessionAt(i) != session) 936 _RemoveTab(i); 937 } 938 939 break; 940 } 941 942 case kIncreaseFontSize: 943 case kDecreaseFontSize: 944 { 945 TermView* view = _ActiveTermView(); 946 BFont font; 947 view->GetTermFont(&font); 948 949 float size = font.Size(); 950 if (message->what == kIncreaseFontSize) 951 size += 1; 952 else 953 size -= 1; 954 955 // limit the font size 956 if (size < 9) 957 size = 9; 958 if (size > 18) 959 size = 18; 960 961 font.SetSize(size); 962 view->SetTermFont(&font); 963 PrefHandler::Default()->setInt32(PREF_HALF_FONT_SIZE, (int32)size); 964 965 _ResizeView(view); 966 break; 967 } 968 969 case kUpdateTitles: 970 _UpdateTitles(); 971 break; 972 973 case kEditTabTitle: 974 { 975 SessionID sessionID(*message, "session"); 976 if (Session* session = _SessionForID(sessionID)) 977 _OpenSetTabTitleDialog(_IndexOfSession(session)); 978 break; 979 } 980 981 case kEditWindowTitle: 982 _OpenSetWindowTitleDialog(); 983 break; 984 985 case kUpdateSwitchTerminalsMenuItem: 986 _UpdateSwitchTerminalsMenuItem(); 987 break; 988 989 default: 990 BWindow::MessageReceived(message); 991 break; 992 } 993 } 994 995 996 void 997 TermWindow::WindowActivated(bool activated) 998 { 999 if (activated) 1000 _UpdateSwitchTerminalsMenuItem(); 1001 } 1002 1003 1004 void 1005 TermWindow::_SetTermColors(TermViewContainerView* containerView) 1006 { 1007 PrefHandler* handler = PrefHandler::Default(); 1008 rgb_color background = handler->getRGB(PREF_TEXT_BACK_COLOR); 1009 1010 containerView->SetViewColor(background); 1011 1012 TermView *termView = containerView->GetTermView(); 1013 termView->SetTextColor(handler->getRGB(PREF_TEXT_FORE_COLOR), background); 1014 1015 termView->SetSelectColor(handler->getRGB(PREF_SELECT_FORE_COLOR), 1016 handler->getRGB(PREF_SELECT_BACK_COLOR)); 1017 } 1018 1019 1020 status_t 1021 TermWindow::_DoPageSetup() 1022 { 1023 BPrintJob job("PageSetup"); 1024 1025 // display the page configure panel 1026 status_t status = job.ConfigPage(); 1027 1028 // save a pointer to the settings 1029 fPrintSettings = job.Settings(); 1030 1031 return status; 1032 } 1033 1034 1035 void 1036 TermWindow::_DoPrint() 1037 { 1038 if (!fPrintSettings || _DoPageSetup() != B_OK) { 1039 BAlert* alert = new BAlert(B_TRANSLATE("Cancel"), 1040 B_TRANSLATE("Print cancelled."), B_TRANSLATE("OK")); 1041 alert->SetShortcut(0, B_ESCAPE); 1042 alert->Go(); 1043 return; 1044 } 1045 1046 BPrintJob job("Print"); 1047 job.SetSettings(new BMessage(*fPrintSettings)); 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