finalcut/src/fwindow.cpp

898 lines
20 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* fwindow.cpp - Intermediate base class for all window objects *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2015-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/fmenubar.h"
#include "final/fstatusbar.h"
#include "final/fwindow.h"
2015-05-23 13:35:12 +02:00
namespace finalcut
{
// static attributes
FWindow* FWindow::previous_window = nullptr;
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
// class FWindow
//----------------------------------------------------------------------
// constructor and destructor
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2015-09-22 04:18:20 +02:00
FWindow::FWindow(FWidget* parent)
: FWidget(parent)
{
setWindowWidget();
FRect geometry = getTermGeometry();
2017-09-11 03:06:02 +02:00
geometry.move(-1, -1);
createArea (geometry, getShadow(), vwin);
2016-10-09 02:06:06 +02:00
addWindow (this);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FWindow::~FWindow() // destructor
{
auto fapp = static_cast<FApplication*>(getRootWidget());
2016-10-09 02:06:06 +02:00
if ( previous_window == this )
previous_window = nullptr;
if ( isAlwaysOnTop() )
deleteFromAlwaysOnTopList (this);
2016-10-09 02:06:06 +02:00
2017-03-12 00:29:56 +01:00
// unset the global active window
if ( this == FWindow::getActiveWindow() )
unsetActiveWindow();
2016-10-09 02:06:06 +02:00
delWindow (this);
if ( ! fapp->isQuit() )
{
const FRect& t_geometry = getTermGeometryWithShadow();
restoreVTerm (t_geometry);
}
removeArea (vwin);
}
// public methods of FWindow
//----------------------------------------------------------------------
FWidget* FWindow::getWindowFocusWidget() const
{
// returns the focused widget of this window
return win_focus_widget;
}
//----------------------------------------------------------------------
bool FWindow::setWindowWidget (bool on)
{
if ( isWindowWidget() == on )
return true;
flags.window_widget = on;
if ( on )
setTermOffset();
else
setParentOffset();
return on;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
void FWindow::setActiveWindow (FWindow* window)
{
// activate FWindow object window
if ( ! window_list )
return;
if ( window_list->empty() )
return;
auto iter = window_list->begin();
auto end = window_list->end();
while ( iter != end )
{
if ( *iter == window )
{
if ( ! window->isWindowActive() )
{
window->activateWindow();
FEvent ev(fc::WindowActive_Event);
FApplication::sendEvent(window, &ev);
}
}
else
{
auto w = static_cast<FWindow*>(*iter);
if ( w->isWindowActive() )
{
w->deactivateWindow();
FEvent ev(fc::WindowInactive_Event);
FApplication::sendEvent(*iter, &ev);
}
}
++iter;
}
}
//----------------------------------------------------------------------
2017-08-12 20:10:27 +02:00
void FWindow::setWindowFocusWidget (const FWidget* obj)
{
// set focus widget of this window
2017-08-12 20:10:27 +02:00
win_focus_widget = const_cast<FWidget*>(obj);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FWindow::activateWindow (bool on)
2015-05-23 13:35:12 +02:00
{
// activate/deactivate this window
if ( on )
2015-05-23 13:35:12 +02:00
{
FWidget::setActiveWindow (this);
active_area = getVWin();
}
2015-05-23 13:35:12 +02:00
return (window_active = on);
}
2015-05-23 13:35:12 +02:00
2017-03-12 00:29:56 +01:00
//----------------------------------------------------------------------
void FWindow::unsetActiveWindow()
{
// unset the active FWindow object
FWidget::setActiveWindow (0);
2017-03-12 00:29:56 +01:00
}
//----------------------------------------------------------------------
bool FWindow::setResizeable (bool on)
{
return (flags.resizeable = on);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FWindow::setTransparentShadow (bool on)
{
flags.shadow = flags.trans_shadow = on;
if ( on )
2018-11-20 21:11:04 +01:00
setShadowSize (2, 1);
else
2018-11-20 21:11:04 +01:00
setShadowSize (0, 0);
return on;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FWindow::setShadow (bool on)
{
if ( isMonochron() )
return false;
2015-05-23 13:35:12 +02:00
if ( on )
{
flags.shadow = true;
flags.trans_shadow = false;
2018-11-20 21:11:04 +01:00
setShadowSize (1, 1);
}
else
{
flags.shadow = false;
flags.trans_shadow = false;
2018-11-20 21:11:04 +01:00
setShadowSize (0, 0);
}
2015-05-23 13:35:12 +02:00
return on;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FWindow::setAlwaysOnTop (bool on)
{
if ( isAlwaysOnTop() == on )
return true;
flags.always_on_top = on;
if ( on )
{
if ( always_on_top_list )
{
deleteFromAlwaysOnTopList (this);
always_on_top_list->push_back (this);
}
}
else
deleteFromAlwaysOnTopList (this);
return on;
}
//----------------------------------------------------------------------
bool FWindow::isWindowHidden() const
{
// returns the window hidden state
2017-01-22 23:04:40 +01:00
if ( isVirtualWindow() )
return ! vwin->visible;
else
return false;
}
//----------------------------------------------------------------------
void FWindow::drawBorder()
{
if ( isNewFont() )
{
int x1 = 1
, x2 = 1 + int(getWidth()) - 1
, y1 = 1
, y2 = 1 + int(getHeight()) - 1;
setPrintPos (x1, y1);
2017-09-11 03:06:02 +02:00
print (fc::NF_border_corner_upper_left); // ⎡
for (int x = x1 + 1; x < x2; x++)
2017-09-11 03:06:02 +02:00
print (fc::NF_border_line_upper); // ¯
2017-09-11 03:06:02 +02:00
print (fc::NF_rev_border_corner_upper_right); // ⎤
for (int y = y1 + 1; y < y2; y++)
{
setPrintPos (x1, y);
// border left ⎸
print (fc::NF_border_line_left);
setPrintPos (x2, y);
// border right⎹
print (fc::NF_rev_border_line_right);
}
setPrintPos (x1, y2);
// lower left corner border ⎣
print (fc::NF_border_corner_lower_left);
for (std::size_t x = 2; x < getWidth(); 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();
}
}
//----------------------------------------------------------------------
void FWindow::show()
{
2017-01-22 23:04:40 +01:00
if ( isVirtualWindow() )
vwin->visible = true;
FWidget::show();
}
//----------------------------------------------------------------------
void FWindow::hide()
{
2017-01-22 23:04:40 +01:00
if ( isVirtualWindow() )
vwin->visible = false;
FWidget::hide();
}
//----------------------------------------------------------------------
void FWindow::setX (int x, bool adjust)
{
FWidget::setX (x, adjust);
2017-01-22 23:04:40 +01:00
if ( isVirtualWindow() )
vwin->offset_left = getTermX() - 1;
}
//----------------------------------------------------------------------
void FWindow::setY (int y, bool adjust)
{
if ( y < 1 )
y = 1;
FWidget::setY (y, adjust);
2017-01-22 23:04:40 +01:00
if ( isVirtualWindow() )
vwin->offset_top = getTermY() - 1;
}
//----------------------------------------------------------------------
void FWindow::setPos (int x, int y, bool adjust)
{
if ( y < 1 )
y = 1;
FWidget::setPos (x, y, adjust);
2017-01-22 23:04:40 +01:00
if ( isVirtualWindow() )
{
vwin->offset_left = getTermX() - 1;
vwin->offset_top = getTermY() - 1;
}
}
//----------------------------------------------------------------------
void FWindow::setWidth (std::size_t w, bool adjust)
{
std::size_t old_width = getWidth();
FWidget::setWidth (w, adjust);
2017-01-22 23:04:40 +01:00
if ( isVirtualWindow() && getWidth() != old_width )
{
FRect geometry = getTermGeometry();
2017-09-11 03:06:02 +02:00
geometry.move(-1, -1);
resizeArea (geometry, getShadow(), vwin);
}
}
//----------------------------------------------------------------------
void FWindow::setHeight (std::size_t h, bool adjust)
{
std::size_t old_height = getHeight();
FWidget::setHeight (h, adjust);
2017-01-22 23:04:40 +01:00
if ( isVirtualWindow() && getHeight() != old_height )
{
FRect geometry = getTermGeometry();
2017-09-11 03:06:02 +02:00
geometry.move(-1, -1);
resizeArea (geometry, getShadow(), vwin);
}
}
//----------------------------------------------------------------------
void FWindow::setSize (std::size_t w, std::size_t h, bool adjust)
{
std::size_t old_width = getWidth();
std::size_t old_height = getHeight();
FWidget::setSize (w, h, adjust);
2017-09-11 03:06:02 +02:00
if ( isVirtualWindow()
2017-11-26 22:37:18 +01:00
&& (getWidth() != old_width || getHeight() != old_height) )
{
FRect geometry = getTermGeometry();
2017-09-11 03:06:02 +02:00
geometry.move(-1, -1);
resizeArea (geometry, getShadow(), vwin);
}
}
//----------------------------------------------------------------------
2018-10-24 00:16:45 +02:00
void FWindow::setGeometry ( int x, int y
, std::size_t w, std::size_t h
, bool adjust )
{
// Sets the geometry of the widget
int old_x = getX();
int old_y = getY();
std::size_t old_width = getWidth();
std::size_t old_height = getHeight();
if ( y < 1 )
y = 1;
FWidget::setGeometry (x, y, w, h, adjust);
2017-01-22 23:04:40 +01:00
if ( ! isVirtualWindow() )
return;
if ( getWidth() != old_width || getHeight() != old_height )
{
FRect geometry = getTermGeometry();
2017-09-11 03:06:02 +02:00
geometry.move(-1, -1);
resizeArea (geometry, getShadow(), vwin);
}
else
{
if ( getX() != old_x )
vwin->offset_left = getTermX() - 1;
if ( getY() != old_y )
vwin->offset_top = getTermY() - 1;
}
}
//----------------------------------------------------------------------
void FWindow::move (int dx, int dy)
{
2017-09-11 03:06:02 +02:00
FWidget::move (dx, dy);
2017-01-22 23:04:40 +01:00
if ( isVirtualWindow() )
{
vwin->offset_left = getTermX() - 1;
vwin->offset_top = getTermY() - 1;
}
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FWindow* FWindow::getWindowWidgetAt (int x, int y)
2015-05-23 13:35:12 +02:00
{
// returns the window object to the corresponding coordinates
2015-05-23 13:35:12 +02:00
if ( window_list && ! window_list->empty() )
{
auto iter = window_list->end();
auto begin = window_list->begin();
2015-05-23 13:35:12 +02:00
do
{
--iter;
2015-10-19 00:07:07 +02:00
if ( *iter )
{
auto w = static_cast<FWindow*>(*iter);
if ( ! w->isWindowHidden()
2017-11-26 22:37:18 +01:00
&& w->getTermGeometry().contains(x, y) )
2015-10-19 00:07:07 +02:00
return w;
}
2015-05-23 13:35:12 +02:00
}
while ( iter != begin );
}
2015-05-23 13:35:12 +02:00
return 0;
}
//----------------------------------------------------------------------
void FWindow::addWindow (FWidget* obj)
{
// add the window object obj to the window list
2015-05-23 13:35:12 +02:00
if ( window_list )
window_list->push_back(obj);
processAlwaysOnTop();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FWindow::delWindow (FWidget* obj)
{
// delete the window object obj from the window list
if ( ! window_list || window_list->empty() )
return;
2015-05-23 13:35:12 +02:00
auto iter = window_list->begin();
while ( iter != window_list->end() )
{
if ( (*iter) == obj )
{
window_list->erase (iter);
return;
2015-05-23 13:35:12 +02:00
}
++iter;
2015-05-23 13:35:12 +02:00
}
}
//----------------------------------------------------------------------
2017-08-12 20:10:27 +02:00
FWindow* FWindow::getWindowWidget (const FWidget* obj)
2015-05-23 13:35:12 +02:00
{
// returns the window object to the given widget obj
auto p_obj = obj->getParentWidget();
while ( ! obj->isWindowWidget() && p_obj )
2015-05-23 13:35:12 +02:00
{
obj = p_obj;
p_obj = p_obj->getParentWidget();
2015-05-23 13:35:12 +02:00
}
if ( obj->isWindowWidget() )
2017-08-12 20:10:27 +02:00
return const_cast<FWindow*>(static_cast<const FWindow*>(obj));
2015-05-23 13:35:12 +02:00
else
return 0;
}
//----------------------------------------------------------------------
2017-08-12 20:10:27 +02:00
int FWindow::getWindowLayer (const FWidget* obj)
2015-05-23 13:35:12 +02:00
{
// returns the window layer from the widget obj
2017-08-12 20:10:27 +02:00
const FWidget* window;
2015-05-23 13:35:12 +02:00
if ( ! window_list )
return -1;
2015-05-23 13:35:12 +02:00
if ( window_list->empty() )
return -1;
if ( ! obj->isWindowWidget() )
2015-05-23 13:35:12 +02:00
{
if ( (window = getWindowWidget(obj)) == 0 )
return -1;
}
else
window = obj;
auto iter = window_list->begin();
auto end = window_list->end();
2015-05-23 13:35:12 +02:00
while ( iter != end )
{
if ( *iter == window )
break;
2015-05-23 13:35:12 +02:00
++iter;
}
return int(std::distance(window_list->begin(), iter) + 1);
}
//----------------------------------------------------------------------
void FWindow::swapWindow (FWidget* obj1, FWidget* obj2)
{
// swaps the window layer between obj1 and obj2
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;
if ( obj1->getFlags().modal )
2015-05-23 13:35:12 +02:00
return;
if ( obj2->getFlags().modal )
2015-05-23 13:35:12 +02:00
return;
auto iter = window_list->begin();
auto end = window_list->end();
auto iter1 = end;
auto iter2 = end;
2015-05-23 13:35:12 +02:00
while ( iter != end )
{
if ( (*iter) == obj1 )
iter1 = iter;
else if ( (*iter) == obj2 )
iter2 = iter;
2015-05-23 13:35:12 +02:00
++iter;
}
if ( iter1 != end && iter2 != end )
std::swap (iter1, iter2);
}
//----------------------------------------------------------------------
bool FWindow::raiseWindow (FWidget* obj)
{
// raises the window widget obj to the top
2015-05-23 13:35:12 +02:00
if ( ! window_list )
return false;
2015-05-23 13:35:12 +02:00
if ( window_list->empty() )
return false;
if ( ! obj->isWindowWidget() )
2015-05-23 13:35:12 +02:00
return false;
2015-05-23 13:35:12 +02:00
if ( window_list->back() == obj )
return false;
if ( window_list->back()->getFlags().modal
2017-11-26 22:37:18 +01:00
&& ! obj->isMenuWidget() )
2015-05-23 13:35:12 +02:00
return false;
auto iter = window_list->begin();
2015-05-23 13:35:12 +02:00
while ( iter != window_list->end() )
{
if ( *iter == obj )
{
window_list->erase (iter);
window_list->push_back (obj);
2016-01-17 02:57:08 +01:00
FEvent ev(fc::WindowRaised_Event);
2015-05-23 13:35:12 +02:00
FApplication::sendEvent(obj, &ev);
processAlwaysOnTop();
2015-05-23 13:35:12 +02:00
return true;
}
2015-05-23 13:35:12 +02:00
++iter;
}
2015-05-23 13:35:12 +02:00
return false;
}
//----------------------------------------------------------------------
bool FWindow::lowerWindow (FWidget* obj)
{
// lowers the window widget obj to the bottom
2015-05-23 13:35:12 +02:00
if ( ! window_list )
return false;
2015-05-23 13:35:12 +02:00
if ( window_list->empty() )
return false;
if ( ! obj->isWindowWidget() )
2015-05-23 13:35:12 +02:00
return false;
2015-05-23 13:35:12 +02:00
if ( window_list->front() == obj )
return false;
if ( obj->getFlags().modal )
2015-05-23 13:35:12 +02:00
return false;
auto iter = window_list->begin();
2015-05-23 13:35:12 +02:00
while ( iter != window_list->end() )
{
if ( *iter == obj )
{
window_list->erase (iter);
window_list->insert (window_list->begin(), obj);
2016-01-17 02:57:08 +01:00
FEvent ev(fc::WindowLowered_Event);
2015-05-23 13:35:12 +02:00
FApplication::sendEvent(obj, &ev);
return true;
}
2015-05-23 13:35:12 +02:00
++iter;
}
2015-05-23 13:35:12 +02:00
return false;
}
//----------------------------------------------------------------------
bool FWindow::zoomWindow()
{
if ( zoomed )
{
zoomed = false;
FRect oldGeometry = getTermGeometryWithShadow();
setGeometry (normalGeometry);
restoreVTerm (oldGeometry);
redraw();
}
else
{
zoomed = true;
// save the current geometry
normalGeometry = getGeometry();
FRect oldGeometry = getTermGeometryWithShadow();
setGeometry (1, 1, getMaxWidth(), getMaxHeight());
restoreVTerm (oldGeometry);
redraw();
}
return zoomed;
}
//----------------------------------------------------------------------
void FWindow::switchToPrevWindow (FWidget* widget)
{
// switch to previous window
// Disable terminal updates to avoid flickering
// when redrawing the focused widget
if ( widget )
widget->updateTerminal (FVTerm::stop_refresh);
bool is_activated = activatePrevWindow();
auto active_win = static_cast<FWindow*>(getActiveWindow());
if ( ! is_activated )
{
// no previous window -> looking for another window
if ( window_list && window_list->size() > 1 )
{
auto iter = window_list->end();
auto begin = window_list->begin();
do
{
--iter;
auto w = static_cast<FWindow*>(*iter);
if ( w
2018-12-01 21:28:25 +01:00
&& w != active_win
2017-11-26 22:37:18 +01:00
&& ! (w->isWindowHidden() || w->isWindowActive())
&& w != static_cast<FWindow*>(getStatusBar())
&& w != static_cast<FWindow*>(getMenuBar()) )
{
setActiveWindow(w);
break;
}
}
while ( iter != begin );
}
}
2018-12-01 21:28:25 +01:00
if ( active_win )
{
auto focus = active_win->getWindowFocusWidget();
2018-12-01 21:28:25 +01:00
if ( ! active_win->isWindowActive() )
setActiveWindow(active_win);
2018-12-01 21:28:25 +01:00
if ( focus)
{
2018-12-01 21:28:25 +01:00
focus->setFocus();
2016-07-31 20:25:25 +02:00
2018-12-01 21:28:25 +01:00
if ( ! focus->isWindowWidget() )
focus->redraw();
}
}
// Enable terminal updates again
if ( widget )
widget->updateTerminal (FVTerm::continue_refresh);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
bool FWindow::activatePrevWindow()
{
// activate the previous window
auto w = previous_window;
if ( w )
2015-05-23 13:35:12 +02:00
{
if ( w->isWindowActive() )
return true;
if ( ! w->isWindowHidden() )
{
setActiveWindow(w);
return true;
}
2015-05-23 13:35:12 +02:00
}
return false;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
void FWindow::setShadowSize (int right, int bottom)
2015-05-23 13:35:12 +02:00
{
int old_right = getShadow().getX()
, old_bottom = getShadow().getY();
FWidget::setShadowSize (right, bottom);
int new_right = getShadow().getX()
, new_bottom = getShadow().getY();
2017-09-11 03:06:02 +02:00
if ( isVirtualWindow()
2017-11-26 22:37:18 +01:00
&& (new_right != old_right || new_bottom != old_bottom) )
{
auto geometry = getTermGeometry();
2017-09-11 03:06:02 +02:00
geometry.move(-1, -1);
resizeArea (geometry, getShadow(), vwin);
}
2015-05-23 13:35:12 +02:00
}
2015-10-19 00:07:07 +02:00
// protected methods of FWindow
2016-10-09 02:06:06 +02:00
//----------------------------------------------------------------------
void FWindow::adjustSize()
2016-10-09 02:06:06 +02:00
{
int old_x = getX();
int old_y = getY();
FWidget::adjustSize();
2016-10-09 02:06:06 +02:00
if ( zoomed )
setGeometry (1, 1, getMaxWidth(), getMaxHeight(), false);
2017-01-22 23:04:40 +01:00
else if ( isVirtualWindow() )
{
if ( getX() != old_x )
vwin->offset_left = getTermX() - 1;
if ( getY() != old_y )
vwin->offset_top = getTermY() - 1;
}
2016-10-09 02:06:06 +02:00
}
//----------------------------------------------------------------------
bool FWindow::event (FEvent* ev)
2016-10-09 02:06:06 +02:00
{
switch ( ev->type() )
2016-10-09 02:06:06 +02:00
{
case fc::WindowActive_Event:
onWindowActive (ev);
break;
case fc::WindowInactive_Event:
onWindowInactive (ev);
break;
case fc::WindowRaised_Event:
onWindowRaised (ev);
break;
case fc::WindowLowered_Event:
onWindowLowered (ev);
break;
default:
return FWidget::event(ev);
2016-10-09 02:06:06 +02:00
}
return true;
2016-10-09 02:06:06 +02:00
}
//----------------------------------------------------------------------
void FWindow::onWindowActive (FEvent*)
{ }
2016-10-09 02:06:06 +02:00
//----------------------------------------------------------------------
void FWindow::onWindowInactive (FEvent*)
{ }
2016-10-09 02:06:06 +02:00
//----------------------------------------------------------------------
void FWindow::onWindowRaised (FEvent*)
{ }
2016-10-09 02:06:06 +02:00
//----------------------------------------------------------------------
void FWindow::onWindowLowered (FEvent*)
{ }
// private methods of FWindow
//----------------------------------------------------------------------
void FWindow::deleteFromAlwaysOnTopList (FWidget* obj)
2016-10-09 02:06:06 +02:00
{
// delete the window object obj from the always-on-top list
if ( ! always_on_top_list || always_on_top_list->empty() )
return;
2016-10-09 02:06:06 +02:00
auto iter = always_on_top_list->begin();
while ( iter != always_on_top_list->end() )
{
if ( *iter == obj )
{
always_on_top_list->erase (iter);
return;
}
++iter;
}
2016-10-09 02:06:06 +02:00
}
//----------------------------------------------------------------------
void FWindow::processAlwaysOnTop()
{
// Raise all always-on-top windows
if ( ! always_on_top_list || always_on_top_list->empty() )
return;
auto iter = always_on_top_list->begin();
while ( iter != always_on_top_list->end() )
{
delWindow (*iter);
if ( window_list )
window_list->push_back(*iter);
++iter;
}
}
} // namespace finalcut