File: windows.cpp

package info (click to toggle)
finalcut 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: cpp: 90,264; makefile: 546; sh: 412
file content (596 lines) | stat: -rw-r--r-- 17,911 bytes parent folder | download
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/***********************************************************************
* windows.cpp - Shows window handling                                  *
*                                                                      *
* This file is part of the FINAL CUT widget toolkit                    *
*                                                                      *
* Copyright 2016-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 <utility>
#include <vector>

#include <final/final.h>

using finalcut::FPoint;
using finalcut::FSize;


//----------------------------------------------------------------------
// class SmallWindow
//----------------------------------------------------------------------

class SmallWindow final : public finalcut::FDialog
{
  public:
    // Constructor
    explicit SmallWindow (finalcut::FWidget* = nullptr);

  private:
    // Method
    void initLayout() override;
    void adjustSize() override;

    // Event handlers
    void onShow (finalcut::FShowEvent*) override;
    void onTimer (finalcut::FTimerEvent*) override;

    // Data members
    finalcut::FLabel left_arrow{this};
    finalcut::FLabel right_arrow_1{this};
    finalcut::FLabel right_arrow_2{this};
    finalcut::FLabel top_left_label{this};
    finalcut::FLabel top_right_label{this};
    finalcut::FLabel bottom_label{this};
};


//----------------------------------------------------------------------
SmallWindow::SmallWindow (finalcut::FWidget* parent)
  : finalcut::FDialog{parent}
{
  const auto& wc = getColorTheme();
  const auto arrow_up = finalcut::UniChar::BlackUpPointingTriangle;
  const auto arrow_down = finalcut::UniChar::BlackDownPointingTriangle;

  left_arrow = arrow_up;
  left_arrow.setForegroundColor (wc->label_inactive_fg);
  left_arrow.setEmphasis();
  left_arrow.ignorePadding();

  right_arrow_1 = arrow_up;
  right_arrow_1.setForegroundColor (wc->label_inactive_fg);
  right_arrow_1.setEmphasis();
  right_arrow_1.ignorePadding();

  right_arrow_2 = arrow_up;
  right_arrow_2.setForegroundColor (wc->label_inactive_fg);
  right_arrow_2.setEmphasis();
  right_arrow_2.ignorePadding();

  top_left_label.setText("menu");
  top_left_label.setForegroundColor (wc->label_inactive_fg);
  top_left_label.setEmphasis();

  top_right_label.setText("minimize/zoom");
  top_right_label.setAlignment (finalcut::Align::Right);
  top_right_label.setForegroundColor (wc->label_inactive_fg);
  top_right_label.setEmphasis();

  finalcut::FString bottom_label_text { "resize\n"
                                        "corner\n" };
  bottom_label_text += arrow_down;
  bottom_label = bottom_label_text;
  bottom_label.setAlignment (finalcut::Align::Right);
  bottom_label.setForegroundColor (wc->label_inactive_fg);
  bottom_label.setEmphasis();
}

//----------------------------------------------------------------------
void SmallWindow::initLayout()
{
  left_arrow.setGeometry (FPoint{2, 2}, FSize{1, 1});
  right_arrow_1.setGeometry (FPoint{int(getWidth()) - 4, 2}, FSize{1, 1});
  right_arrow_2.setGeometry (FPoint{int(getWidth()) - 1, 2}, FSize{1, 1});
  top_left_label.setGeometry (FPoint{1, 1}, FSize{6, 1});
  top_right_label.setGeometry (FPoint{int(getClientWidth()) - 16, 1}, FSize{17, 1});
  bottom_label.setGeometry (FPoint{13, 3}, FSize{6, 3});
  FDialog::initLayout();
}

//----------------------------------------------------------------------
void SmallWindow::adjustSize()
{
  finalcut::FDialog::adjustSize();

  if ( isZoomed() )
  {
    top_right_label.setGeometry ( FPoint{int(getClientWidth()) - 14, 1}
                                , FSize{15, 1} );
    top_right_label = "minimize/unzoom";
    bottom_label.hide();
  }
  else
  {
    top_right_label.setGeometry ( FPoint{int(getClientWidth()) - 12, 1}
                                , FSize{13, 1} );
    top_right_label = "minimize/zoom";
    bottom_label.show();
  }

  right_arrow_1.setGeometry ( FPoint{int(getWidth()) - 4, 2}
                            , FSize{1, 1} );
  right_arrow_2.setGeometry ( FPoint{int(getWidth()) - 1, 2}
                            , FSize{1, 1} );
  bottom_label.setGeometry ( FPoint{1, int(getClientHeight()) - 2}
                           , FSize{getClientWidth(), 3} );
}

//----------------------------------------------------------------------
void SmallWindow::onShow (finalcut::FShowEvent*)
{
  addTimer(1000);
}

//----------------------------------------------------------------------
void SmallWindow::onTimer (finalcut::FTimerEvent*)
{
  left_arrow.unsetEmphasis();
  left_arrow.redraw();
  right_arrow_1.unsetEmphasis();
  right_arrow_1.redraw();
  right_arrow_2.unsetEmphasis();
  right_arrow_2.redraw();
  top_left_label.unsetEmphasis();
  top_left_label.redraw();
  top_right_label.unsetEmphasis();
  top_right_label.redraw();
  bottom_label.unsetEmphasis();
  bottom_label.redraw();
  delOwnTimers();
}


//----------------------------------------------------------------------
// class Window
//----------------------------------------------------------------------

class Window final : public finalcut::FDialog
{
  public:
    // Constructor
    explicit Window (finalcut::FWidget* = nullptr);

    // Destructor
    ~Window() override;

  private:
    struct WinData
    {
        // Constructor
        WinData() = default;

        // copy constructor
        WinData (const WinData&) = default;

        // move constructor
        WinData (WinData&&) noexcept = default;

        // copy assignment operator (=)
        auto operator = (const WinData&) -> WinData& = default;

        // move assignment operator (=)
        auto operator = (WinData&&) noexcept -> WinData& = default;

        // Data members
        bool is_open{false};
        finalcut::FString title{};
        SmallWindow* dgl{nullptr};
    };

    // Method
    void configureFileMenuItems();
    void configureDialogButtons();
    void activateWindow (finalcut::FDialog*) const;

    void initLayout() override;
    void adjustSize() override;
    template <typename InstanceT
            , typename CallbackT
            , typename... Args>
    void addClickedCallback ( finalcut::FWidget*
                            , InstanceT&&, CallbackT&&, Args&&... );
    template <typename IteratorT>
    auto getNext (IteratorT) -> finalcut::FDialog*;
    template <typename IteratorT>
    auto getPrevious (IteratorT iter) -> finalcut::FDialog*;

    // Event handlers
    void onClose (finalcut::FCloseEvent*) override;

    // Callback methods
    void cb_createWindows();
    void cb_closeWindows();
    void cb_next();
    void cb_previous();
    void cb_destroyWindow (WinData&) const;

    // Data members
    std::vector<WinData>      windows{};
    finalcut::FString         drop_down_symbol{finalcut::UniChar::BlackDownPointingTriangle};
    finalcut::FMenuBar        Menubar{this};
    finalcut::FMenu           File{"&File", &Menubar};
    finalcut::FDialogListMenu DglList{std::move(drop_down_symbol), &Menubar};
    finalcut::FStatusBar      Statusbar{this};
    finalcut::FMenuItem       New{"&New", &File};
    finalcut::FMenuItem       Close{"&Close", &File};
    finalcut::FMenuItem       Line1{&File};
    finalcut::FMenuItem       Next{"Ne&xt window", &File};
    finalcut::FMenuItem       Previous{"&Previous window", &File};
    finalcut::FMenuItem       Line2{&File};
    finalcut::FMenuItem       Quit{"&Quit", &File};
    finalcut::FButton         CreateButton{this};
    finalcut::FButton         CloseButton{this};
    finalcut::FButton         QuitButton{this};
};


//----------------------------------------------------------------------
Window::Window (finalcut::FWidget* parent)
  : finalcut::FDialog{parent}
{
  FDialog::setSize ({40, 6});

  // Menu bar item
  File.setStatusbarMessage ("File management commands");

  // Dialog list menu item
  DglList.setStatusbarMessage ("List of all the active dialogs");

  // File menu items
  configureFileMenuItems();

  // Dialog buttons
  configureDialogButtons();

  // Statusbar at the bottom
  Statusbar.setMessage("Status bar message");

  // Generate data vector for the windows
  for (uInt n{1}; n < 7; n++)
  {
    WinData win_dat;
    win_dat.title.sprintf("Window %1u", n);
    windows.emplace_back(std::move(win_dat));
  }
}

//----------------------------------------------------------------------
Window::~Window()
{
  auto iter = windows.begin();

  while ( iter != windows.end() )
  {
    auto& win_dat = *iter;

    // Remove all callbacks before Window::cb_destroyWindow() will be called
    if ( win_dat.is_open && win_dat.dgl )
      win_dat.dgl->delCallback();

    iter = windows.erase(iter);
  }
}

//----------------------------------------------------------------------
void Window::configureFileMenuItems()
{
  // "File" menu item setting
  New.setStatusbarMessage ("Create the windows");
  Close.setStatusbarMessage ("Close the windows");
  Line1.setSeparator();
  Next.addAccelerator (finalcut::FKey::Meta_page_down);  // Meta/Alt + PgDn
  Next.setStatusbarMessage ("Switch to the next window");
  Previous.addAccelerator (finalcut::FKey::Meta_page_up);  // Meta/Alt + PgUp
  Previous.setStatusbarMessage ("Switch to the previous window");
  Line2.setSeparator();
  Quit.addAccelerator (finalcut::FKey::Meta_x);  // Meta/Alt + X
  Quit.setStatusbarMessage ("Exit the program");

  // Add menu item callback
  addClickedCallback (&New, this, &Window::cb_createWindows);
  addClickedCallback (&Close, this, &Window::cb_closeWindows);
  addClickedCallback (&Next, this, &Window::cb_next);
  addClickedCallback (&Previous, this, &Window::cb_previous);
  addClickedCallback ( &Quit
                     , finalcut::getFApplication()
                     , &finalcut::FApplication::cb_exitApp
                     , this );
}

//----------------------------------------------------------------------
void Window::configureDialogButtons()
{
  // Dialog buttons
  CreateButton.setText (L"&Create");
  CloseButton.setText (L"C&lose");
  QuitButton.setText (L"&Quit");

  // Add button callback
  addClickedCallback (&CreateButton, this, &Window::cb_createWindows);
  addClickedCallback (&CloseButton, this, &Window::cb_closeWindows);
  addClickedCallback ( &QuitButton
                     , finalcut::getFApplication()
                     , &finalcut::FApplication::cb_exitApp
                     , this );
}

//----------------------------------------------------------------------
void Window::activateWindow (finalcut::FDialog* win) const
{
  if ( ! win || win->isWindowHidden() || win->isWindowActive() )
    return;

  const bool has_raised = finalcut::FWindow::raiseWindow(win);
  win->activateDialog();

  if ( has_raised )
    win->redraw();
}

//----------------------------------------------------------------------
void Window::initLayout()
{
  CreateButton.setGeometry (FPoint{2, 2}, FSize{9, 1});
  CloseButton.setGeometry (FPoint{15, 2}, FSize{9, 1});
  QuitButton.setGeometry (FPoint{28, 2}, FSize{9, 1});
  FDialog::initLayout();
}

//----------------------------------------------------------------------
void Window::adjustSize()
{
  finalcut::FDialog::adjustSize();

  const std::size_t w = getDesktopWidth();
  const std::size_t h = getDesktopHeight();
  const auto X = int(1 + (w - 40) / 2);
  auto Y = int(1 + (h - 22) / 2);
  const int dx = ( w > 80 ) ? int(w - 80) / 2 : 0;
  const int dy = ( h > 24 ) ? int(h - 24) / 2 : 0;

  if ( Y < 2 )
    Y = 2;

  setPos (FPoint{X, Y});
  const auto& first = windows.begin();
  auto iter = first;

  while ( iter != windows.end() )
  {
    if ( (*iter).is_open )
    {
      const auto n = int(std::distance(first, iter));
      const int x = dx + 5 + (n % 3) * 25 + int(n / 3) * 3;
      const int y = dy + 11 + int(n / 3) * 3;
      (*iter).dgl->setPos (FPoint{x, y});
    }

    ++iter;
  }
}

//----------------------------------------------------------------------
template <typename InstanceT
        , typename CallbackT
        , typename... Args>
void Window::addClickedCallback ( finalcut::FWidget* widget
                                , InstanceT&& instance
                                , CallbackT&& callback
                                , Args&&... args )
{
  widget->addCallback
  (
    "clicked",
    std::bind ( std::forward<CallbackT>(callback)
              , std::forward<InstanceT>(instance)
              , std::forward<Args>(args)... )
  );
}

//----------------------------------------------------------------------
template <typename IteratorT>
auto Window::getNext (IteratorT iter) -> finalcut::FDialog*
{
  auto next_element = iter;
  finalcut::FDialog* next{nullptr};

  do
  {
    ++next_element;

    if ( next_element == getDialogList()->end() )
      next_element = getDialogList()->begin();

    next = static_cast<finalcut::FDialog*>(*next_element);
  } while ( ! next->isEnabled()
         || ! next->acceptFocus()
         || ! next->isVisible()
         || ! next->isWindowWidget() );

  return next;
}

//----------------------------------------------------------------------
template <typename IteratorT>
auto Window::getPrevious (IteratorT iter) -> finalcut::FDialog*
{
  auto prev_element = iter;
  finalcut::FDialog* prev;

  do
  {
    if ( prev_element == getDialogList()->begin() )
      prev_element = getDialogList()->end();

    --prev_element;
    prev = static_cast<finalcut::FDialog*>(*prev_element);
  } while ( ! prev->isEnabled()
         || ! prev->acceptFocus()
         || ! prev->isVisible()
         || ! prev->isWindowWidget() );

  return prev;
}

//----------------------------------------------------------------------
void Window::onClose (finalcut::FCloseEvent* ev)
{
  finalcut::FApplication::closeConfirmationDialog (this, ev);
}

//----------------------------------------------------------------------
void Window::cb_createWindows()
{
  const auto& first = windows.begin();
  auto iter = first;
  const auto w = getRootWidget()->getWidth();
  const auto h = getRootWidget()->getHeight();
  const int dx = ( w > 80 ) ? int(w - 80) / 2 : 0;
  const int dy = ( h > 24 ) ? int(h - 24) / 2 : 0;

  while ( iter != windows.end() )
  {
    if ( ! (*iter).is_open )
    {
      auto& win_dat = *iter;
      auto win = new SmallWindow(this);
      win_dat.dgl = win;
      win_dat.is_open = true;
      win->setText(win_dat.title);
      const auto n = int(std::distance(first, iter));
      const int x = dx + 2 + (n % 3) * 26 + int(n / 3) * 3;
      const int y = dy + 11 + int(n / 3) * 3;
      win->setGeometry (FPoint{x, y}, FSize{21, 8});
      win->setMinimumSize (FSize{20, 8});
      win->setResizeable();
      win->setMinimizable();
      win->show();

      win->addCallback
      (
        "destroy",
        this, &Window::cb_destroyWindow,
        std::ref(win_dat)
      );
    }

    ++iter;
  }

  activateWindow(this);
}

//----------------------------------------------------------------------
void Window::cb_closeWindows()
{
  if ( ! getDialogList() || getDialogList()->empty() )
    return;

  auto iter = getDialogList()->end();
  const auto& first = getDialogList()->begin();
  activateWindow(this);

  do
  {
    --iter;

    if ( (*iter) != this )
      (*iter)->close();
  }
  while ( iter != first );
}

//----------------------------------------------------------------------
void Window::cb_next()
{
  if ( ! getDialogList() || getDialogList()->empty() )
    return;

  auto iter = getDialogList()->begin();

  while ( iter != getDialogList()->end() )
  {
    if ( static_cast<finalcut::FWindow*>(*iter)->isWindowActive() )
    {
      activateWindow(getNext(iter));
      break;
    }

    ++iter;
  }
}

//----------------------------------------------------------------------
void Window::cb_previous()
{
  if ( ! getDialogList() || getDialogList()->empty() )
    return;

  auto iter = getDialogList()->end();

  do
  {
    --iter;

    if ( (*iter)->isDialogWidget()
      && static_cast<finalcut::FWindow*>(*iter)->isWindowActive() )
    {
      activateWindow(getPrevious(iter));
      break;
    }
  }
  while ( iter != getDialogList()->begin() );
}

//----------------------------------------------------------------------
void Window::cb_destroyWindow (WinData& win_dat) const
{
  win_dat.is_open = false;
  win_dat.dgl = nullptr;
}


//----------------------------------------------------------------------
//                               main part
//----------------------------------------------------------------------

auto main (int argc, char* argv[]) -> int
{
  // Create the application object
  finalcut::FApplication app {argc, argv};

  // Create main dialog object
  Window main_dlg {&app};
  main_dlg.setText ("Main window");

  // Set dialog main_dlg as main widget
  finalcut::FWidget::setMainWidget (&main_dlg);

  // Show and start the application
  main_dlg.show();
  return app.exec();
}