finalcut/src/fdialog.cpp

1788 lines
40 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* fdialog.cpp - Widget FDialog *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2012-2018 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* The Final Cut is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License *
* as published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* The Final Cut is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
2015-05-23 13:35:12 +02:00
#include "final/fapplication.h"
#include "final/fdialog.h"
#include "final/fstatusbar.h"
2015-05-23 13:35:12 +02:00
namespace finalcut
{
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
// class FDialog
//----------------------------------------------------------------------
// constructor and destructor
//----------------------------------------------------------------------
FDialog::FDialog (FWidget* parent)
2015-09-22 04:18:20 +02:00
: FWindow(parent)
, tb_text()
, result_code(FDialog::Reject)
, zoom_button_pressed(false)
, zoom_button_active(false)
, setPos_error(false)
, setSize_error(false)
, titlebar_click_pos()
, resize_click_pos()
, save_geometry()
2016-06-12 23:06:58 +02:00
, dialog_menu()
, dgl_menuitem()
, move_size_item()
, zoom_item()
, close_item()
, tooltip()
2015-05-23 13:35:12 +02:00
{
2015-09-22 04:18:20 +02:00
init();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FDialog::FDialog (const FString& txt, FWidget* parent)
: FWindow(parent)
, tb_text(txt)
, result_code(FDialog::Reject)
, zoom_button_pressed(false)
, zoom_button_active(false)
, setPos_error(false)
, setSize_error(false)
, titlebar_click_pos()
, resize_click_pos()
, save_geometry()
2016-06-12 23:06:58 +02:00
, dialog_menu()
, dgl_menuitem()
, move_size_item()
, zoom_item()
, close_item()
, tooltip(0)
2015-05-23 13:35:12 +02:00
{
2015-09-22 04:18:20 +02:00
init();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FDialog::~FDialog() // destructor
{
2016-07-28 01:04:27 +02:00
FApplication* fapp = static_cast<FApplication*>(getRootWidget());
bool is_quit = fapp->isQuit();
delete dialog_menu;
dgl_menuitem = 0;
2015-05-23 13:35:12 +02:00
delete accelerator_list;
accelerator_list = 0;
2016-07-28 01:04:27 +02:00
if ( ! is_quit )
switchToPrevWindow();
delDialog(this);
2015-05-23 13:35:12 +02:00
if ( isModal() )
unsetModal();
}
// public methods of FDialog
//----------------------------------------------------------------------
bool FDialog::setDialogWidget (bool on)
{
if ( isDialogWidget() == on )
return true;
if ( on )
{
flags |= fc::dialog_widget;
setTermOffsetWithPadding();
}
else
{
flags &= ~fc::dialog_widget;
setParentOffset();
}
2016-06-12 23:06:58 +02:00
return on;
}
//----------------------------------------------------------------------
bool FDialog::setModal (bool on)
{
if ( isModal() == on )
return true;
if ( on )
2016-06-12 23:06:58 +02:00
{
flags |= fc::modal;
modal_dialogs++;
}
else
{
flags &= ~fc::modal;
modal_dialogs--;
2016-06-12 23:06:58 +02:00
}
return on;
}
//----------------------------------------------------------------------
bool FDialog::setScrollable (bool on)
{
if ( on )
flags |= fc::scrollable;
else
flags &= ~fc::scrollable;
return on;
}
//----------------------------------------------------------------------
bool FDialog::setResizeable (bool on)
{
FWindow::setResizeable (on);
2016-06-12 23:06:58 +02:00
if ( on )
zoom_item->setEnable();
else
zoom_item->setDisable();
return on;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::show()
2015-05-23 13:35:12 +02:00
{
if ( ! isVisible() )
return;
2015-05-23 13:35:12 +02:00
FWindow::show();
if ( isModal() )
2015-05-23 13:35:12 +02:00
{
FApplication* fapp = static_cast<FApplication*>(getRootWidget());
fapp->enter_loop();
if ( this == getMainWidget() )
fapp->quit();
}
}
//----------------------------------------------------------------------
void FDialog::hide()
{
FWindow::hide();
if ( isModal() )
2015-05-23 13:35:12 +02:00
{
FApplication* fapp = static_cast<FApplication*>(getRootWidget());
fapp->exit_loop();
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
int FDialog::exec()
2015-05-23 13:35:12 +02:00
{
result_code = FDialog::Reject;
show();
return result_code;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FDialog::setPos (int x, int y, bool)
{
int rsw, bsh, width, height;
FRect old_geometry;
setPos_error = false;
if ( getX() == x && getY() == y )
{
setPos_error = true;
return;
}
width = getWidth();
height = getHeight();
// Avoid to move widget completely outside the terminal
if ( x + width <= 1 || x > getMaxWidth() || y < 1 || y > getMaxHeight() )
{
setPos_error = true;
return;
}
2015-10-23 23:57:00 +02:00
if ( isZoomed() )
{
setPos_error = true;
return;
}
int dx = getX() - x
, dy = getY() - y
, old_x = getTermX()
, old_y = getTermY();
const FPoint& shadow = getShadow();
rsw = shadow.getX(); // right shadow width;
bsh = shadow.getY(); // bottom shadow height
old_geometry = getTermGeometryWithShadow();
// move to the new position
FWindow::setPos(x, y, false);
putArea (getTermPos(), vwin);
// restoring the non-covered terminal areas
if ( getTermGeometry().overlap(old_geometry) )
2016-06-12 23:06:58 +02:00
{
// dx > 0 : move left
// dx = 0 : move vertical
// dx < 0 : move right
// dy > 0 : move up
// dy = 0 : move horizontal
// dy < 0 : move down
if ( dx > 0 )
{
if ( dy > 0 )
restoreVTerm ( old_x + width + rsw - dx, old_y
, dx, getHeight() + bsh - dy );
else
restoreVTerm ( old_x + width + rsw - dx, old_y + std::abs(dy)
, dx, height + bsh - std::abs(dy));
}
2016-06-12 23:06:58 +02:00
else
{
if ( dy > 0 )
restoreVTerm (old_x, old_y, std::abs(dx), height + bsh - dy);
else
restoreVTerm ( old_x, old_y + std::abs(dy)
, std::abs(dx), height + bsh - std::abs(dy) );
}
if ( dy > 0 )
restoreVTerm (old_x, old_y + height + bsh - dy, width + rsw, dy);
else
restoreVTerm (old_x, old_y, width + rsw, std::abs(dy));
2016-06-12 23:06:58 +02:00
}
2015-05-23 13:35:12 +02:00
else
2016-06-12 23:06:58 +02:00
{
restoreVTerm (old_geometry);
}
restoreOverlaidWindows();
FWindow::adjustSize();
setCursorToFocusWidget();
updateTerminal();
}
//----------------------------------------------------------------------
2017-12-27 01:51:28 +01:00
void FDialog::move (int dx, int dy)
{
setPos (getX() + dx, getY() + dy);
}
//----------------------------------------------------------------------
inline bool FDialog::moveUp (int n)
{
move (0, -n);
return ! setPos_error;
}
//----------------------------------------------------------------------
inline bool FDialog::moveDown (int n)
{
move (0, n);
return ! setPos_error;
}
//----------------------------------------------------------------------
inline bool FDialog::moveLeft (int n)
{
move (-n, 0);
return ! setPos_error;
}
//----------------------------------------------------------------------
inline bool FDialog::moveRight (int n)
{
move (n, 0);
return ! setPos_error;
}
//----------------------------------------------------------------------
void FDialog::setSize (int w, int h, bool adjust)
{
setSize_error = false;
if ( getWidth() == w && getHeight() == h )
{
setSize_error = true;
return;
}
if ( isZoomed() )
{
setSize_error = true;
return;
}
int x = getTermX()
, y = getTermY()
, old_width = getWidth()
, old_height = getHeight()
, dw = old_width - w
, dh = old_height - h;
const FPoint& shadow = getShadow();
int rsw = shadow.getX(); // right shadow width;
int bsh = shadow.getY(); // bottom shadow height
2015-05-23 13:35:12 +02:00
FWindow::setSize (w, h, adjust);
2015-05-23 13:35:12 +02:00
// get adjust width and height
w = getWidth();
h = getHeight();
2015-05-23 13:35:12 +02:00
// dw > 0 : scale down width
// dw = 0 : scale only height
// dw < 0 : scale up width
// dh > 0 : scale down height
// dh = 0 : scale only width
// dh < 0 : scale up height
// restoring the non-covered terminal areas
if ( dw > 0 )
restoreVTerm (x + w + rsw, y, dw, h + bsh + dh); // restore right
if ( dh > 0 )
restoreVTerm (x, y + h + bsh, w + rsw + dw, dh); // restore bottom
2016-06-13 22:44:15 +02:00
redraw();
// handle overlaid windows
if ( window_list && ! window_list->empty() )
{
bool overlaid = false;
widgetList::const_iterator iter, last;
iter = window_list->begin();
last = window_list->end();
while ( iter != last )
{
if ( overlaid )
putArea ((*iter)->getTermPos(), (*iter)->getVWin());
2016-06-13 22:44:15 +02:00
if ( vwin == (*iter)->getVWin() )
overlaid = true;
++iter;
}
}
// set the cursor to the focus widget
FWidget* focus_widget = FWidget::getFocusWidget();
if ( focus_widget
2017-11-26 22:37:18 +01:00
&& focus_widget->isVisible()
&& focus_widget->hasVisibleCursor() )
{
FPoint cursor_pos = focus_widget->getCursorPos();
focus_widget->setCursorPos(cursor_pos);
}
}
//----------------------------------------------------------------------
bool FDialog::reduceHeight (int n)
{
if ( ! isResizeable() )
return false;
setSize (getWidth(), getHeight() - n);
return ! setSize_error;
}
//----------------------------------------------------------------------
bool FDialog::expandHeight (int n)
{
if ( ! isResizeable() || getHeight() + getY() > getMaxHeight() )
return false;
setSize (getWidth(), getHeight() + n);
return ! setSize_error;
}
//----------------------------------------------------------------------
bool FDialog::reduceWidth (int n)
{
if ( ! isResizeable() )
return false;
setSize (getWidth() - n, getHeight());
return ! setSize_error;
}
//----------------------------------------------------------------------
bool FDialog::expandWidth (int n)
{
if ( ! isResizeable() || getWidth() + getX() > getMaxWidth() )
return false;
setSize (getWidth() + n, getHeight());
return ! setSize_error;
}
//----------------------------------------------------------------------
void FDialog::activateDialog()
{
if ( isWindowActive() )
return;
FWidget* old_focus = FWidget::getFocusWidget();
FWidget* win_focus = getWindowFocusWidget();
setActiveWindow(this);
setFocus();
setFocusWidget(this);
if ( win_focus && numOfFocusableChildren() > 1 )
{
win_focus->setFocus();
win_focus->redraw();
if ( old_focus )
old_focus->redraw();
}
else if ( old_focus )
{
if ( ! focusFirstChild() )
old_focus->unsetFocus();
if ( ! old_focus->isWindowWidget() )
old_focus->redraw();
}
if ( getStatusBar() )
getStatusBar()->drawMessage();
updateTerminal();
}
//----------------------------------------------------------------------
void FDialog::onKeyPress (FKeyEvent* ev)
2015-05-23 13:35:12 +02:00
{
if ( ! isEnabled() )
return;
cancelMouseResize();
2017-11-26 22:37:18 +01:00
if ( ev->key() == fc::Fckey_caret // Ctrl+^ (Ctrl+6)
|| ev->key() == fc::Fkey_f22 ) // Shift+F10
{
ev->accept();
// open the titlebar menu
openMenu();
// focus to the first enabled item
selectFirstMenuItem();
}
// Dialog move and resize functions
if ( getMoveSizeWidget() )
moveSizeKey(ev);
if ( this == getMainWidget() )
2015-05-23 13:35:12 +02:00
return;
if ( ev->key() == fc::Fkey_escape
2017-11-26 22:37:18 +01:00
|| ev->key() == fc::Fkey_escape_mintty )
2015-05-23 13:35:12 +02:00
{
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-23 13:35:12 +02:00
if ( isModal() )
done (FDialog::Reject);
else
close();
}
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FDialog::onMouseDown (FMouseEvent* ev)
2015-05-23 13:35:12 +02:00
{
mouseStates ms =
{
ev->getX(),
ev->getY(),
ev->getTermPos(),
getZoomButtonWidth(),
false // mouse_over_menu
};
deactivateZoomButton();
2015-05-23 13:35:12 +02:00
2016-01-17 02:57:08 +01:00
if ( ev->getButton() == fc::LeftButton )
2015-05-23 13:35:12 +02:00
{
// Click on titlebar or window: raise + activate
raiseActivateDialog();
if ( ms.mouse_x >= 4
&& ms.mouse_x <= getWidth() - ms.zoom_btn
&& ms.mouse_y == 1 )
titlebar_click_pos.setPoint (ev->getTermX(), ev->getTermY());
2015-05-23 13:35:12 +02:00
else
titlebar_click_pos.setPoint (0,0);
2015-05-23 13:35:12 +02:00
// Click on titlebar menu button
if ( ms.mouse_x < 4 && ms.mouse_y == 1 )
openMenu();
else
activateZoomButton(ms);
// Click on the lower right resize corner
resizeMouseDown(ms);
2016-06-12 23:06:58 +02:00
}
else // ev->getButton() != fc::LeftButton
{
// Click on titlebar menu button
if ( ms.mouse_x < 4 && ms.mouse_y == 1
&& dialog_menu->isVisible() )
2016-06-13 22:44:15 +02:00
leaveMenu(); // close menu
cancelMouseResize(); // Cancel resize
2015-05-23 13:35:12 +02:00
}
// Click on titlebar: just activate
if ( ev->getButton() == fc::RightButton
&& ms.mouse_x >= 4
&& ms.mouse_x <= getWidth()
&& ms.mouse_y == 1 )
activateDialog();
2015-05-23 13:35:12 +02:00
// Click on titlebar: lower + activate
if ( ev->getButton() == fc::MiddleButton
&& ms.mouse_x >= 4 && ms.mouse_x <= getWidth()
&& ms.mouse_y == 1 )
lowerActivateDialog();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FDialog::onMouseUp (FMouseEvent* ev)
2015-05-23 13:35:12 +02:00
{
mouseStates ms =
{
ev->getX(),
ev->getY(),
ev->getTermPos(),
getZoomButtonWidth(),
false // mouse_over_menu
};
2015-05-23 13:35:12 +02:00
2016-01-17 02:57:08 +01:00
if ( ev->getButton() == fc::LeftButton )
2015-05-23 13:35:12 +02:00
{
int titlebar_x = titlebar_click_pos.getX()
, titlebar_y = titlebar_click_pos.getY();
2016-06-12 23:06:58 +02:00
if ( ! titlebar_click_pos.isNull()
2017-11-26 22:37:18 +01:00
&& titlebar_x > getTermX() + 3
&& titlebar_x < getTermX() + getWidth()
&& titlebar_y == getTermY() )
2015-05-23 13:35:12 +02:00
{
FPoint deltaPos = ms.termPos - titlebar_click_pos;
move (deltaPos);
titlebar_click_pos = ms.termPos;
2015-05-23 13:35:12 +02:00
}
2016-06-12 23:06:58 +02:00
// Click on titlebar menu button
if ( ms.mouse_x < 4
&& ms.mouse_y == 1
2017-11-26 22:37:18 +01:00
&& dialog_menu->isVisible()
&& ! dialog_menu->hasSelectedItem() )
2016-06-12 23:06:58 +02:00
{
// Sets focus to the first item
selectFirstMenuItem();
2016-06-12 23:06:58 +02:00
}
else
{
// Zoom to maximum or restore the window size
pressZoomButton(ms);
}
// Resize the dialog
resizeMouseUpMove (ms, true);
}
deactivateZoomButton();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FDialog::onMouseMove (FMouseEvent* ev)
2015-05-23 13:35:12 +02:00
{
mouseStates ms =
2015-05-23 13:35:12 +02:00
{
ev->getX(),
ev->getY(),
ev->getTermPos(),
getZoomButtonWidth(),
isMouseOverMenu(ev->getTermPos())
};
if ( ev->getButton() != fc::LeftButton )
return;
if ( ! titlebar_click_pos.isNull() )
{
FPoint deltaPos = ms.termPos - titlebar_click_pos;
move (deltaPos);
titlebar_click_pos = ms.termPos;
}
// Mouse event handover to the menu
if ( ms.mouse_over_menu )
passEventToSubMenu (ms, ev);
leaveZoomButton(ms); // Check zoom button pressed
resizeMouseUpMove(ms); // Resize the dialog
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void FDialog::onMouseDoubleClick (FMouseEvent* ev)
2015-05-23 13:35:12 +02:00
{
mouseStates ms =
{
ev->getX(),
ev->getY(),
ev->getTermPos(),
getZoomButtonWidth(),
false // mouse_over_menu
};
int x, y;
2015-05-23 13:35:12 +02:00
2016-01-17 02:57:08 +01:00
if ( ev->getButton() != fc::LeftButton )
2015-05-23 13:35:12 +02:00
return;
x = getTermX();
y = getTermY();
2015-05-23 13:35:12 +02:00
FRect title_button(x, y, 3, 1);
FPoint tPos = ms.termPos;
if ( title_button.contains(tPos) )
2015-05-23 13:35:12 +02:00
{
// Double click on title button
FWidget* window_focus_widget;
2016-06-12 23:06:58 +02:00
dialog_menu->unselectItem();
dialog_menu->hide();
activateWindow();
raiseWindow();
window_focus_widget = getWindowFocusWidget();
if ( window_focus_widget )
window_focus_widget->setFocus();
2015-05-23 13:35:12 +02:00
setClickedWidget(0);
2016-06-12 23:06:58 +02:00
if ( isModal() )
done (FDialog::Reject);
2016-06-12 23:06:58 +02:00
else
close();
2015-05-23 13:35:12 +02:00
}
else if ( isResizeable()
&& ms.mouse_x >= 4
&& ms.mouse_x <= getWidth() - ms.zoom_btn
&& ms.mouse_y == 1 )
{
// Double click on titlebar
zoomWindow(); // window zoom/unzoom
setZoomItem();
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::onAccel (FAccelEvent*)
{
if ( ! (isWindowHidden() || isWindowActive()) )
{
2016-07-25 23:50:57 +02:00
bool has_raised = raiseWindow();
activateDialog();
2016-07-25 23:50:57 +02:00
if ( has_raised )
redraw();
2016-07-25 23:50:57 +02:00
updateTerminal();
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FDialog::onWindowActive (FEvent*)
{
if ( isVisible() && isShown() )
drawTitleBar();
if ( ! FWidget::getFocusWidget() )
2015-05-23 13:35:12 +02:00
{
FWidget* win_focus = getWindowFocusWidget();
if ( win_focus
2017-11-26 22:37:18 +01:00
&& win_focus->isVisible()
&& win_focus->isShown() )
{
win_focus->setFocus();
win_focus->redraw();
}
else
focusFirstChild();
2015-05-23 13:35:12 +02:00
}
if ( getStatusBar() )
getStatusBar()->drawMessage();
updateTerminal();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::onWindowInactive (FEvent*)
{
if ( dialog_menu && ! dialog_menu->isVisible() )
FWindow::previous_window = this;
2015-05-23 13:35:12 +02:00
if ( isVisible() && isEnabled() )
drawTitleBar();
if ( hasFocus() )
unsetFocus();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::onWindowRaised (FEvent*)
{
if ( ! (isVisible() && isShown()) )
2015-09-15 23:07:24 +02:00
return;
putArea (getTermPos(), vwin);
2015-05-23 13:35:12 +02:00
// Handle always-on-top windows
if ( always_on_top_list && ! always_on_top_list->empty() )
{
widgetList::const_iterator iter, last;
iter = always_on_top_list->begin();
last = always_on_top_list->end();
while ( iter != last )
{
putArea ((*iter)->getTermPos(), (*iter)->getVWin());
++iter;
}
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::onWindowLowered (FEvent*)
{
widgetList::const_iterator iter, last;
2015-05-23 13:35:12 +02:00
if ( ! window_list )
return;
2015-05-23 13:35:12 +02:00
if ( window_list->empty() )
return;
iter = window_list->begin();
last = window_list->end();
2015-05-23 13:35:12 +02:00
while ( iter != last )
2015-05-23 13:35:12 +02:00
{
putArea ((*iter)->getTermPos(), (*iter)->getVWin());
2015-05-23 13:35:12 +02:00
++iter;
}
}
// protected methods of FDialog
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FDialog::done(int result)
2015-05-23 13:35:12 +02:00
{
hide();
result_code = result;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FDialog::draw()
{
if ( tooltip && ! getMoveSizeWidget() )
2015-05-23 13:35:12 +02:00
{
2017-08-12 20:10:27 +02:00
delete tooltip;
tooltip = 0;
2015-05-23 13:35:12 +02:00
}
// Fill the background
setColor();
if ( isMonochron() )
setReverse(true);
2016-12-28 16:29:49 +01:00
clearArea();
drawBorder();
drawTitleBar();
setCursorPos(2, getHeight() - 1);
if ( (flags & fc::shadow) != 0 )
drawDialogShadow();
if ( isMonochron() )
setReverse(false);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::drawDialogShadow()
2015-05-23 13:35:12 +02:00
{
if ( isMonochron() && (flags & fc::trans_shadow) == 0 )
return;
2015-05-23 13:35:12 +02:00
drawShadow();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::onShow (FShowEvent*)
{ }
//----------------------------------------------------------------------
void FDialog::onHide (FHideEvent*)
{ }
//----------------------------------------------------------------------
void FDialog::onClose (FCloseEvent* ev)
2015-05-23 13:35:12 +02:00
{
ev->accept();
2015-05-23 13:35:12 +02:00
result_code = FDialog::Reject;
}
// private methods of FDialog
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FDialog::init()
2015-05-23 13:35:12 +02:00
{
FWidget* old_focus;
setTopPadding(2);
setLeftPadding(1);
setBottomPadding(1);
setRightPadding(1);
ignorePadding();
setDialogWidget();
// Initialize geometry values
setGeometry (1, 1, 10, 10, false);
setMinimumSize (15, 4);
addDialog(this);
setActiveWindow(this);
setTransparentShadow();
setForegroundColor (wc.dialog_fg);
setBackgroundColor (wc.dialog_bg);
old_focus = FWidget::getFocusWidget();
if ( old_focus )
2015-05-23 13:35:12 +02:00
{
setFocus();
old_focus->redraw();
}
2015-05-23 13:35:12 +02:00
2017-08-12 22:55:29 +02:00
try
{
accelerator_list = new Accelerators();
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
// Add the dialog menu
initDialogMenu();
}
//----------------------------------------------------------------------
void FDialog::initDialogMenu()
{
// Create the dialog Menu (access via Shift-F10 or Ctrl-^)
2017-08-12 22:55:29 +02:00
try
{
dialog_menu = new FMenu ("-", this);
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
dialog_menu->setPos (getX(), getY() + 1);
dgl_menuitem = dialog_menu->getItem();
if ( dgl_menuitem )
{
dgl_menuitem->ignorePadding();
dgl_menuitem->unsetFocusable();
}
2018-02-18 22:45:49 +01:00
// Add the move/size menu item
initMoveSizeMenuItem (dialog_menu);
// Add the zoom menu item
initZoomMenuItem (dialog_menu);
// Add the close menu item
initCloseMenuItem (dialog_menu);
}
//----------------------------------------------------------------------
void FDialog::initMoveSizeMenuItem (FMenu* menu)
{
2017-08-12 22:55:29 +02:00
try
{
2018-02-18 22:45:49 +01:00
move_size_item = new FMenuItem (menu);
2017-08-12 22:55:29 +02:00
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
move_size_item->setText ("&Move/Size");
move_size_item->setStatusbarMessage ("Move or change the size of the window");
move_size_item->addCallback
(
"clicked",
F_METHOD_CALLBACK (this, &FDialog::cb_move)
);
2018-02-18 22:45:49 +01:00
}
2018-02-18 22:45:49 +01:00
//----------------------------------------------------------------------
void FDialog::initZoomMenuItem (FMenu* menu)
{
2017-08-12 22:55:29 +02:00
try
{
2018-02-18 22:45:49 +01:00
zoom_item = new FMenuItem (menu);
2017-08-12 22:55:29 +02:00
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
setZoomItem();
zoom_item->setDisable();
zoom_item->addCallback
(
"clicked",
F_METHOD_CALLBACK (this, &FDialog::cb_zoom)
);
2018-02-18 22:45:49 +01:00
}
2018-02-18 22:45:49 +01:00
//----------------------------------------------------------------------
void FDialog::initCloseMenuItem (FMenu* menu)
{
2017-08-12 22:55:29 +02:00
try
{
2018-02-18 22:45:49 +01:00
close_item = new FMenuItem ("&Close", menu);
2017-08-12 22:55:29 +02:00
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
close_item->setStatusbarMessage ("Close this window");
close_item->addCallback
(
"clicked",
F_METHOD_CALLBACK (this, &FDialog::cb_close)
);
}
//----------------------------------------------------------------------
void FDialog::drawBorder()
{
int x1 = 1
, x2 = 1 + getWidth() - 1
, y1 = 2
, y2 = 1 + getHeight() - 1;
if ( (getMoveSizeWidget() == this || ! resize_click_pos.isNull() )
2017-11-26 22:37:18 +01:00
&& ! isZoomed() )
setColor (wc.dialog_resize_fg, getBackgroundColor());
else
setColor();
if ( isNewFont() )
{
for (int y = y1; y < y2; y++)
2015-05-23 13:35:12 +02:00
{
setPrintPos (x1, y);
// Border left ⎸
print (fc::NF_border_line_left);
setPrintPos (x2, y);
// Border right⎹
print (fc::NF_rev_border_line_right);
2015-05-23 13:35:12 +02:00
}
setPrintPos (x1, y2);
// Lower left corner border ⎣
print (fc::NF_border_corner_lower_left);
2017-09-11 03:06:02 +02:00
for (int x = 1; x < getWidth() - 1; x++) // low line _
print (fc::NF_border_line_bottom);
setPrintPos (x2, y2);
// Lower right corner border ⎦
print (fc::NF_rev_border_corner_lower_right);
}
else
{
FWidget::drawBorder(x1, y1, x2, y2);
}
}
//----------------------------------------------------------------------
void FDialog::drawTitleBar()
{
// Draw the title button
drawBarButton();
// Print the text bar
drawTextBar();
// Draw the zoom/unzoom button
drawZoomButton();
if ( isMonochron() )
setReverse(false);
#if DEBUG
if ( PRINT_WIN_NUMBER )
{
// Print the number of window in stack
setPrintPos (getWidth() - 2, 1);
printf ("(%d)", getWindowLayer(this));
}
#endif // DEBUG
}
//----------------------------------------------------------------------
void FDialog::drawBarButton()
{
// Print the title button
setPrintPos (1, 1);
if ( dialog_menu && dialog_menu->isVisible() )
setColor (wc.titlebar_button_focus_fg, wc.titlebar_button_focus_bg);
else
setColor (wc.titlebar_button_fg, wc.titlebar_button_bg);
if ( isMonochron() )
{
if ( isWindowActive() )
setReverse(false);
2015-05-23 13:35:12 +02:00
else
setReverse(true);
}
if ( isNewFont() )
{
print (fc::NF_rev_menu_button1);
print (fc::NF_rev_menu_button2);
print (fc::NF_rev_menu_button3);
}
else if ( isMonochron() )
{
print ('[');
if ( dgl_menuitem )
print (dgl_menuitem->getText());
2015-05-23 13:35:12 +02:00
else
print ('-');
print (']');
2015-05-23 13:35:12 +02:00
}
else
{
print (' ');
if ( dgl_menuitem )
print (dgl_menuitem->getText());
else
print ('-');
print (' ');
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
void FDialog::drawZoomButton()
{
// Draw the zoom/unzoom button
if ( ! isResizeable() )
return;
if ( zoom_button_pressed )
setColor (wc.titlebar_button_focus_fg, wc.titlebar_button_focus_bg);
else
setColor (wc.titlebar_button_fg, wc.titlebar_button_bg);
if ( isZoomed() )
2018-02-24 19:19:28 +01:00
drawRestoreSizeButton();
else
drawZoomedButton();
}
//----------------------------------------------------------------------
inline void FDialog::drawRestoreSizeButton()
{
if ( isNewFont() )
{
2018-02-24 19:19:28 +01:00
print (fc::NF_rev_down_pointing_triangle1);
print (fc::NF_rev_down_pointing_triangle2);
}
else
{
if ( isMonochron() )
{
2018-02-24 19:19:28 +01:00
print ('[');
print (fc::BlackDownPointingTriangle); // ▼
print (']');
}
else
{
2018-02-24 19:19:28 +01:00
print (' ');
print (fc::BlackDownPointingTriangle); // ▼
print (' ');
}
}
2018-02-24 19:19:28 +01:00
}
//----------------------------------------------------------------------
inline void FDialog::drawZoomedButton()
{
if ( isNewFont() )
{
2018-02-24 19:19:28 +01:00
print (fc::NF_rev_up_pointing_triangle1);
print (fc::NF_rev_up_pointing_triangle2);
}
else
{
if ( isMonochron() )
{
2018-02-24 19:19:28 +01:00
print ('[');
print (fc::BlackUpPointingTriangle); // ▲
print (']');
}
else
{
2018-02-24 19:19:28 +01:00
print (' ');
print (fc::BlackUpPointingTriangle); // ▲
print (' ');
}
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FDialog::drawTextBar()
{
// Fill with spaces (left of the title)
int center_offset
, zoom_btn
, length
, x;
if ( getMaxColor() < 16 )
setBold();
2015-05-23 13:35:12 +02:00
if ( isWindowActive() || (dialog_menu && dialog_menu->isVisible()) )
setColor (wc.titlebar_active_fg, wc.titlebar_active_bg);
else
setColor (wc.titlebar_inactive_fg, wc.titlebar_inactive_bg);
zoom_btn = getZoomButtonWidth();
length = int(tb_text.getLength());
center_offset = int((getWidth() - length - MENU_BTN - zoom_btn) / 2);
for (x = 1; x <= center_offset; x++)
print (' ');
2017-12-21 00:25:58 +01:00
// Print title bar text
2018-03-21 00:02:43 +01:00
if ( ! tb_text.isEmpty() )
{
if ( length <= getWidth() - MENU_BTN - zoom_btn )
print (tb_text);
else
{
2017-12-21 00:25:58 +01:00
// Print ellipsis
print (tb_text.left(getWidth() - MENU_BTN - zoom_btn - 2));
print ("..");
2015-05-23 13:35:12 +02:00
}
}
2017-12-21 00:25:58 +01:00
// Fill the rest of the bar
for ( ; x + 1 + length < getWidth() - zoom_btn - 1; x++)
print (' ');
2015-05-23 13:35:12 +02:00
if ( getMaxColor() < 16 )
unsetBold();
}
//----------------------------------------------------------------------
void FDialog::restoreOverlaidWindows()
{
// Restoring overlaid windows
if ( ! window_list || window_list->empty() )
return;
bool overlaid = false;
widgetList::const_iterator iter, last;
iter = window_list->begin();
last = window_list->end();
while ( iter != last )
{
if ( overlaid )
putArea ((*iter)->getTermPos(), (*iter)->getVWin());
if ( vwin == (*iter)->getVWin() )
overlaid = true;
++iter;
}
}
//----------------------------------------------------------------------
void FDialog::setCursorToFocusWidget()
{
// Set the cursor to the focus widget
FWidget* focus_widget = FWidget::getFocusWidget();
if ( focus_widget
&& focus_widget->isVisible()
&& focus_widget->hasVisibleCursor() )
{
FPoint cursor_pos = focus_widget->getCursorPos();
focus_widget->setCursorPos(cursor_pos);
updateVTermCursor(vwin);
}
}
//----------------------------------------------------------------------
void FDialog::leaveMenu()
{
dialog_menu->unselectItem();
dialog_menu->hide();
activateWindow();
raiseWindow();
if ( getWindowFocusWidget() )
getWindowFocusWidget()->setFocus();
redraw();
if ( getStatusBar() )
getStatusBar()->drawMessage();
updateTerminal();
flush_out();
}
//----------------------------------------------------------------------
void FDialog::openMenu()
{
// Open the titlebar menu
if ( ! dialog_menu )
return;
if ( dialog_menu->isVisible() )
{
leaveMenu();
drawTitleBar();
}
else
{
setOpenMenu(dialog_menu);
dialog_menu->setPos (getX(), getY() + 1);
dialog_menu->setVisible();
drawTitleBar();
dialog_menu->show();
2017-08-12 20:10:27 +02:00
dialog_menu->raiseWindow();
dialog_menu->redraw();
}
}
2016-07-25 23:50:57 +02:00
//----------------------------------------------------------------------
void FDialog::selectFirstMenuItem()
2016-07-25 23:50:57 +02:00
{
// Focus to the first enabled menu item
FMenuItem* first_item;
dialog_menu->selectFirstItem();
first_item = dialog_menu->getSelectedItem();
2016-07-25 23:50:57 +02:00
if ( first_item )
first_item->setFocus();
2016-07-25 23:50:57 +02:00
dialog_menu->redraw();
2016-07-25 23:50:57 +02:00
if ( getStatusBar() )
getStatusBar()->drawMessage();
2016-07-25 23:50:57 +02:00
updateTerminal();
flush_out();
2016-07-25 23:50:57 +02:00
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FDialog::setZoomItem()
2015-05-23 13:35:12 +02:00
{
if ( isZoomed() )
{
zoom_item->setText ("&Unzoom");
zoom_item->setStatusbarMessage ("Restore the window size");
move_size_item->setDisable();
}
2015-05-23 13:35:12 +02:00
else
{
zoom_item->setText ("&Zoom");
zoom_item->setStatusbarMessage ("Enlarge the window to the entire desktop");
move_size_item->setEnable();
}
}
//----------------------------------------------------------------------
inline int FDialog::getZoomButtonWidth()
{
if ( ! isResizeable() )
return 0;
else if ( isNewFont() )
return 2;
else
return 3;
}
//----------------------------------------------------------------------
inline void FDialog::deactivateZoomButton()
{
if ( ! zoom_button_pressed && ! zoom_button_active )
return;
zoom_button_pressed = false;
zoom_button_active = false;
drawTitleBar();
}
//----------------------------------------------------------------------
inline void FDialog::activateZoomButton (mouseStates& ms)
{
if ( ms.mouse_x <= getWidth() - ms.zoom_btn
|| ms.mouse_y != 1 )
return;
zoom_button_pressed = true;
zoom_button_active = true;
drawTitleBar();
}
//----------------------------------------------------------------------
inline void FDialog::leaveZoomButton (mouseStates& ms)
{
bool zoom_button_pressed_before = zoom_button_pressed;
if ( ms.mouse_x > getWidth() - ms.zoom_btn
&& ms.mouse_x <= getWidth()
&& ms.mouse_y == 1
&& zoom_button_active )
zoom_button_pressed = true;
else if ( zoom_button_pressed )
zoom_button_pressed = false;
if ( zoom_button_pressed_before != zoom_button_pressed )
drawTitleBar();
}
//----------------------------------------------------------------------
void FDialog::pressZoomButton (mouseStates& ms)
{
if ( ms.mouse_x <= getWidth() - ms.zoom_btn
|| ms.mouse_y != 1
|| ! zoom_button_pressed )
return;
// Zoom to maximum or restore the window size
zoomWindow();
setZoomItem();
}
//----------------------------------------------------------------------
inline bool FDialog::isMouseOverMenu (const FPoint& termpos)
{
const FRect& menu_geometry = dialog_menu->getTermGeometry();
if ( dialog_menu->getCount() > 0 && menu_geometry.contains(termpos) )
return true;
return false;
}
//----------------------------------------------------------------------
inline void FDialog::passEventToSubMenu ( mouseStates& ms
, FMouseEvent* ev )
{
// Mouse event handover to the dialog menu
if ( ! ms.mouse_over_menu
|| ! dialog_menu->isVisible()
|| ! dialog_menu->isShown() )
return;
const FPoint& g = ms.termPos;
const FPoint& p = dialog_menu->termToWidgetPos(g);
int b = ev->getButton();
try
{
FMouseEvent* _ev = new FMouseEvent (fc::MouseMove_Event, p, g, b);
dialog_menu->mouse_down = true;
setClickedWidget(dialog_menu);
dialog_menu->onMouseMove(_ev);
delete _ev;
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
}
//----------------------------------------------------------------------
inline void FDialog::moveSizeKey (FKeyEvent* ev)
{
switch ( ev->key() )
{
case fc::Fkey_up:
moveUp(1);
ev->accept();
break;
case fc::Fkey_down:
moveDown(1);
ev->accept();
break;
case fc::Fkey_left:
moveLeft(1);
ev->accept();
break;
case fc::Fkey_right:
moveRight(1);
ev->accept();
break;
case fc::Fmkey_up:
case fc::Fkey_sr:
if ( reduceHeight(1) )
ev->accept();
break;
case fc::Fmkey_down:
case fc::Fkey_sf:
if ( expandHeight(1) )
ev->accept();
break;
case fc::Fmkey_left:
case fc::Fkey_sleft:
if ( reduceWidth(1) )
ev->accept();
break;
case fc::Fmkey_right:
case fc::Fkey_sright:
if ( expandWidth(1) )
ev->accept();
break;
case fc::Fkey_return:
case fc::Fkey_enter:
acceptMoveSize();
ev->accept();
break;
case fc::Fkey_escape:
case fc::Fkey_escape_mintty:
cancelMoveSize();
ev->accept();
return;
default:
break;
}
}
//----------------------------------------------------------------------
inline void FDialog::raiseActivateDialog()
{
bool has_raised = raiseWindow();
activateDialog();
if ( has_raised )
redraw();
}
//----------------------------------------------------------------------
inline void FDialog::lowerActivateDialog()
{
bool has_lowered = lowerWindow();
if ( ! isWindowActive() )
activateDialog();
else if ( has_lowered )
updateTerminal();
}
//----------------------------------------------------------------------
bool FDialog::isLowerRightResizeCorner (mouseStates& ms)
{
// 3 characters in the lower right corner |
// x
// -----xx
if ( (ms.mouse_x == getWidth() && ms.mouse_y == getHeight() - 1)
|| ( ( ms.mouse_x == getWidth() - 1
|| ms.mouse_x == getWidth() ) && ms.mouse_y == getHeight() ) )
{
return true;
}
else
{
return false;
}
}
//----------------------------------------------------------------------
void FDialog::resizeMouseDown (mouseStates& ms)
{
// Click on the lower right resize corner
if ( isResizeable() && isLowerRightResizeCorner(ms) )
{
resize_click_pos = ms.termPos;
FPoint lower_right_pos = getTermGeometry().getLowerRightPos();
if ( ms.termPos != lower_right_pos )
{
FPoint deltaPos = ms.termPos - lower_right_pos;
int w = lower_right_pos.getX() + deltaPos.getX() - getTermX() + 1;
int h = lower_right_pos.getY() + deltaPos.getY() - getTermY() + 1;
setSize (w, h);
}
else
drawBorder();
}
else
resize_click_pos.setPoint (0,0);
}
//----------------------------------------------------------------------
void FDialog::resizeMouseUpMove (mouseStates& ms, bool mouse_up)
{
// Resize the dialog
if ( isResizeable() && ! resize_click_pos.isNull() )
{
FWidget* r = getRootWidget();
resize_click_pos = ms.termPos;
int x2 = resize_click_pos.getX()
, y2 = resize_click_pos.getY()
, x2_offset = 0
, y2_offset = 0;
if ( r )
{
x2_offset = r->getLeftPadding();
y2_offset = r->getTopPadding();
}
if ( ms.termPos != getTermGeometry().getLowerRightPos() )
{
int w, h;
FPoint deltaPos = ms.termPos - resize_click_pos;
if ( x2 - x2_offset <= getMaxWidth() )
w = resize_click_pos.getX() + deltaPos.getX() - getTermX() + 1;
else
w = getMaxWidth() - getTermX() + x2_offset + 1;
if ( y2 - y2_offset <= getMaxHeight() )
h = resize_click_pos.getY() + deltaPos.getY() - getTermY() + 1;
else
h = getMaxHeight() - getTermY() + y2_offset + 1;
setSize (w, h);
}
if ( mouse_up )
{
// Reset the border color
resize_click_pos.setPoint (0,0);
// redraw() is required to draw the standard (black) border
// and client objects with ignorePadding() option.
redraw();
}
}
}
//----------------------------------------------------------------------
void FDialog::cancelMouseResize()
{
// Cancel resize by mouse
if ( resize_click_pos.isNull() )
return;
resize_click_pos.setPoint (0,0);
drawBorder();
updateTerminal();
}
//----------------------------------------------------------------------
inline void FDialog::acceptMoveSize()
{
setMoveSizeWidget(0);
if ( tooltip )
delete tooltip;
tooltip = 0;
redraw();
}
//----------------------------------------------------------------------
inline void FDialog::cancelMoveSize()
{
setMoveSizeWidget(0);
if ( tooltip )
delete tooltip;
tooltip = 0;
setPos (save_geometry.getPos());
if ( isResizeable() )
setSize (save_geometry.getWidth(), save_geometry.getHeight());
redraw();
}
//----------------------------------------------------------------------
void FDialog::addDialog (FWidget* obj)
{
// Add the dialog object obj to the dialog list
if ( dialog_list )
dialog_list->push_back(obj);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::delDialog (FWidget* obj)
{
// Delete the dialog object obj from the dialog list
if ( ! dialog_list || dialog_list->empty() )
return;
widgetList::iterator iter;
iter = dialog_list->begin();
while ( iter != dialog_list->end() )
{
if ( (*iter) == obj )
{
dialog_list->erase(iter);
return;
}
++iter;
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FDialog::cb_move (FWidget*, data_ptr)
2015-05-23 13:35:12 +02:00
{
if ( isZoomed() )
return;
2015-05-23 13:35:12 +02:00
setMoveSizeWidget(this);
if ( isMonochron() )
2017-08-12 22:55:29 +02:00
setReverse(true);
drawBorder();
if ( isMonochron() )
2017-08-12 22:55:29 +02:00
setReverse(false);
save_geometry = getGeometry();
2017-08-12 22:55:29 +02:00
try
{
tooltip = new FToolTip(this);
}
catch (const std::bad_alloc& ex)
{
std::cerr << "not enough memory to alloc " << ex.what() << std::endl;
return;
}
if ( isResizeable() )
2015-05-23 13:35:12 +02:00
{
if ( isLinuxTerm() )
tooltip->setText ( " Arrow keys: Move\n"
"Shift + Arrow keys: Resize\n"
" Enter: Done\n"
" Esc: Cancel" );
else
tooltip->setText ( " Arrow keys: Move\n"
"Meta + Arrow keys: Resize\n"
" Enter: Done\n"
" Esc: Cancel" );
2015-05-23 13:35:12 +02:00
}
else
{
tooltip->setText ( "Arrow keys: Move\n"
" Enter: Done\n"
" Esc: Cancel" );
2015-05-23 13:35:12 +02:00
}
tooltip->show();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::cb_zoom (FWidget*, data_ptr)
2015-05-23 13:35:12 +02:00
{
dialog_menu->unselectItem();
dialog_menu->hide();
setClickedWidget(0);
drawTitleBar();
zoomWindow();
setZoomItem();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FDialog::cb_close (FWidget*, data_ptr)
2015-05-23 13:35:12 +02:00
{
dialog_menu->unselectItem();
dialog_menu->hide();
setClickedWidget(0);
drawTitleBar();
close();
2015-05-23 13:35:12 +02:00
}
} // namespace finalcut