1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/***********************************************************************
* xpmview.cpp - Image Viewer for X PixMap (XPM) images *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2022 Markus Gans *
* *
* 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. *
* *
* 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/>. *
***********************************************************************/
#include <final/final.h>
#include "xpmimage.h"
using finalcut::FPoint;
using finalcut::FRect;
using finalcut::FSize;
//----------------------------------------------------------------------
// class XpmPicture
//----------------------------------------------------------------------
class XpmPicture final : public finalcut::FScrollView
{
public:
// Constructor
explicit XpmPicture (finalcut::FWidget* = nullptr);
// Method
void open (const finalcut::FString&);
auto getImageSize() const -> finalcut::FSize;
auto getImageColors() const -> std::size_t;
private:
// Methods
void draw() override;
// Data member
XpmImage xmp_image{};
};
//----------------------------------------------------------------------
XpmPicture::XpmPicture (finalcut::FWidget* parent)
: finalcut::FScrollView{parent}
{ }
//----------------------------------------------------------------------
void XpmPicture::open (const finalcut::FString& filename)
{
auto&& data = xmp_image.xpmFileToVector (filename.toString());
xmp_image.parseXPM3(data);
}
//----------------------------------------------------------------------
auto XpmPicture::getImageSize() const -> finalcut::FSize
{
auto size = xmp_image.getSize();
return size;
}
//----------------------------------------------------------------------
auto XpmPicture::getImageColors() const -> std::size_t
{
return xmp_image.getColorCount();
}
//----------------------------------------------------------------------
void XpmPicture::draw()
{
if ( finalcut::FVTerm::getFOutput()->isMonochron() )
setReverse(true);
const auto& wc = getColorTheme();
setColor (wc->label_inactive_fg, wc->dialog_bg);
clearArea();
print() << FPoint{1, 1};
const auto& term_buffer = xmp_image.getTermBuffer();
print(term_buffer);
if ( finalcut::FVTerm::getFOutput()->isMonochron() )
setReverse(false);
FScrollView::draw();
}
//----------------------------------------------------------------------
// class XpmWindow
//----------------------------------------------------------------------
class XpmWindow final : public finalcut::FDialog
{
public:
// Using-declaration
using finalcut::FDialog::setGeometry;
// Constructor
explicit XpmWindow ( const finalcut::FString&
, finalcut::FWidget* = nullptr );
// Method
void setSize (const FSize&, bool = true) override;
void setGeometry (const FPoint&, const FSize&, bool = true) override;
private:
// Method
void initLayout() override;
void adjustSize() override;
void open (const finalcut::FString&);
// Event handler
void onKeyPress (finalcut::FKeyEvent*) override;
// Data members
XpmPicture xpm{this};
};
//----------------------------------------------------------------------
XpmWindow::XpmWindow ( const finalcut::FString& filename
, finalcut::FWidget* parent )
: finalcut::FDialog{parent}
{
open(filename);
}
//----------------------------------------------------------------------
void XpmWindow::setSize (const FSize& size, bool adjust)
{
finalcut::FDialog::setSize (size, adjust);
xpm.setSize(FSize{getWidth(), getHeight() - 1});
}
//----------------------------------------------------------------------
void XpmWindow::setGeometry (const FPoint& p, const FSize& size, bool adjust)
{
finalcut::FDialog::setGeometry (p, size, adjust);
xpm.setGeometry(FPoint{1, 2}, FSize{getWidth(), getHeight() - 1});
}
//----------------------------------------------------------------------
void XpmWindow::initLayout()
{
// The scrolling viewport widget
setResizeable();
setMinimumSize (FSize{15, 5});
xpm.ignorePadding();
xpm.setGeometry(FPoint{1, 2}, FSize{getWidth(), getHeight() - 1});
FDialog::initLayout();
}
//----------------------------------------------------------------------
void XpmWindow::adjustSize()
{
FDialog::adjustSize();
xpm.setGeometry(FPoint{1, 2}, FSize{getWidth(), getHeight() - 1});
if ( isZoomed() )
return;
auto desktop_size = FSize{getDesktopWidth(), getDesktopHeight() - 2};
FRect screen(FPoint{1, 1}, desktop_size);
// Centering of the window when it is not in the visible area
if ( ! screen.contains(getPos()) )
{
int x = 1 + int((getDesktopWidth() - getWidth()) / 2);
int y = 1 + int((getDesktopHeight() -2 - getHeight()) / 2);
setPos(FPoint{x, y});
}
}
//----------------------------------------------------------------------
void XpmWindow::open (const finalcut::FString& filename)
{
xpm.open(filename);
auto c_string = const_cast<char*>(filename.c_str());
const auto base_name = finalcut::FString(basename(c_string));
FDialog::setText(base_name);
const auto size = xpm.getImageSize();
const auto img_width = size.getWidth();
const auto img_heigth = size.getHeight() / 2;
xpm.setScrollSize({img_width, img_heigth});
setMaximumSize({img_width + 2, img_heigth + 3});
const auto width = std::min(img_width + 2, getDesktopWidth());
const auto height = std::min(img_heigth + 3, getDesktopHeight() - 2);
setSize({width, height});
const auto x = int((getDesktopWidth() - img_width) / 2);
const auto y = int((getDesktopHeight() - img_heigth - 2) / 2);
setPos({x, y});
auto msg = finalcut::FString(std::to_string(img_width))
+ finalcut::UniChar::Times
+ std::to_string(size.getHeight())
+ L" pixels, "
+ std::to_string(xpm.getImageColors())
+ L" colors";
setStatusbarMessage(msg);
if ( getStatusBar() )
{
getStatusBar()->setMessage(msg);
getStatusBar()->drawMessage();
}
}
//----------------------------------------------------------------------
void XpmWindow::onKeyPress (finalcut::FKeyEvent* ev)
{
if ( ! ev )
return;
if ( ev->key() == finalcut::FKey::Ctrl_w )
{
close();
ev->accept();
}
else
{
xpm.onKeyPress(ev);
if ( ! ev->isAccepted() )
finalcut::FDialog::onKeyPress(ev);
}
}
//----------------------------------------------------------------------
// class MainWidget
//----------------------------------------------------------------------
class MainWidget final : public finalcut::FWidget
{
public:
explicit MainWidget (finalcut::FWidget* parent = nullptr);
void open (const finalcut::FString&);
private:
// Event handler
void onAccel (finalcut::FAccelEvent*) override;
void onClose (finalcut::FCloseEvent*) override;
// Callback method
void cb_fileOpen();
finalcut::FMenuBar Menubar{this};
finalcut::FMenu File{"&File", &Menubar};
finalcut::FDialogListMenu Windowlist{"&Window", &Menubar};
finalcut::FMenuItem Open{"&Open", &File};
finalcut::FMenuItem Line{&File};
finalcut::FMenuItem Quit{"&Quit", &File};
finalcut::FStatusBar Statusbar{this};
finalcut::FString directory{L"."};
};
//----------------------------------------------------------------------
MainWidget::MainWidget (finalcut::FWidget* parent)
: finalcut::FWidget{parent}
{
File.setStatusbarMessage ("File management commands");
Windowlist.setStatusbarMessage ("List of all windows");
Open.setStatusbarMessage ("Locate and open a text file");
Quit.setStatusbarMessage("Exit the program");
Open.addAccelerator (finalcut::FKey::Ctrl_o); // Ctrl + O
Quit.addAccelerator (finalcut::FKey::Ctrl_q); // Ctrl + Q
addAccelerator (finalcut::FKey('q')); // Q
Line.setSeparator();
Open.addCallback("clicked", this, &MainWidget::cb_fileOpen);
Quit.addCallback
(
"clicked",
finalcut::getFApplication(),
&finalcut::FApplication::cb_exitApp,
this
);
}
//----------------------------------------------------------------------
void MainWidget::open (const finalcut::FString& filename)
{
const auto& xpm_window = new XpmWindow(filename, this);
xpm_window->show();
}
//----------------------------------------------------------------------
void MainWidget::onAccel (finalcut::FAccelEvent* ev)
{
close();
ev->accept();
}
//----------------------------------------------------------------------
void MainWidget::onClose (finalcut::FCloseEvent* ev)
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
}
//----------------------------------------------------------------------
void MainWidget::cb_fileOpen()
{
finalcut::FString filename = [this] ()
{
finalcut::FString filter("*.xpm");
return finalcut::FFileDialog::fileOpenChooser(this, directory, filter);
}();
if ( filename.isEmpty() )
return;
directory = dirname(filename.c_str());
open(filename);
}
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
auto main (int argc, char* argv[]) -> int
{
if ( argv[1] && ( strcmp(argv[1], "--help") == 0
|| strcmp(argv[1], "-h") == 0 ) )
{
std::cout << "XPM viewer\n"
<< "Usage: " << basename(argv[0]) << " [FILE]...\n\n";
}
// Create the application object
finalcut::FApplication app{argc, argv};
// Create the main widget object
MainWidget xpmviewer(&app);
finalcut::FWidget::setMainWidget(&xpmviewer);
// Show the main widget
xpmviewer.show();
for (int i{1}; i < argc; i++)
xpmviewer.open(argv[i]);
return app.exec();
}
|