File: menu.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 (335 lines) | stat: -rw-r--r-- 12,100 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
/***********************************************************************
* menu.cpp - A menu example                                            *
*                                                                      *
* This file is part of the FINAL CUT widget toolkit                    *
*                                                                      *
* Copyright 2015-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>

using FKey = finalcut::FKey;
using finalcut::FPoint;
using finalcut::FSize;


//----------------------------------------------------------------------
// class Menu
//----------------------------------------------------------------------

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

  private:
    // Methods
    void configureFileMenuItems();
    void configureEditMenuItems();
    void configureChoiceMenuItems();
    void configureColorMenuItems();
    void configureStyleMenuItems();
    void configureBorderMenuItems();
    void defaultCallback (const finalcut::FMenuList*);
    void initLayout() override;
    void adjustSize() override;

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

    // Callback method
    void cb_message (const finalcut::FMenuItem*);

    // Data members
    finalcut::FString        line{13, finalcut::UniChar::BoxDrawingsHorizontal};
    finalcut::FMenuBar       Menubar{this};
    finalcut::FMenu          File{"&File", &Menubar};
    finalcut::FMenu          Edit{"&Edit", &Menubar};
    finalcut::FMenu          Choice{"&Choice", &Menubar};
    finalcut::FMenuItem      Window{"&Window", &Menubar};
    finalcut::FMenuItem      Help{"&Help", &Menubar};
    finalcut::FMenuItem      New{"&New", &File};
    finalcut::FMenuItem      Open{"&Open...", &File};
    finalcut::FMenuItem      Save{"&Save", &File};
    finalcut::FMenuItem      SaveAs{"&Save as...", &File};
    finalcut::FMenuItem      Close{"&Close", &File};
    finalcut::FMenuItem      Line1{&File};
    finalcut::FMenuItem      Print{"&Print", &File};
    finalcut::FMenuItem      Line2{&File};
    finalcut::FMenuItem      Quit{"&Quit", &File};
    finalcut::FMenuItem      Undo{FKey::Ctrl_z, "&Undo", &Edit};
    finalcut::FMenuItem      Redo{FKey::Ctrl_y, "&Redo", &Edit};
    finalcut::FMenuItem      Line3{&Edit};
    finalcut::FMenuItem      Cut{FKey::Ctrl_x, "Cu&t", &Edit};
    finalcut::FMenuItem      Copy{FKey::Ctrl_c, "&Copy", &Edit};
    finalcut::FMenuItem      Paste{FKey::Ctrl_v, "&Paste", &Edit};
    finalcut::FMenuItem      Line4{&Edit};
    finalcut::FMenuItem      Search{FKey::Ctrl_f, "&Search", &Edit};
    finalcut::FMenuItem      Next{FKey::F3, "Search &next", &Edit};
    finalcut::FMenuItem      Line5{&Edit};
    finalcut::FMenuItem      SelectAll{FKey::Ctrl_a, "Select &all", &Edit};
    finalcut::FMenu          Color{"&Color", &Choice};
    finalcut::FMenu          Style{"&Style", &Choice};
    finalcut::FMenu          Border{"&Border", &Choice};
    finalcut::FRadioMenuItem Color1{"Red", &Color};
    finalcut::FRadioMenuItem Color2{"Green", &Color};
    finalcut::FRadioMenuItem Color3{"Yellow", &Color};
    finalcut::FRadioMenuItem Color4{"Brue", &Color};
    finalcut::FRadioMenuItem Color5{"Black", &Color};
    finalcut::FCheckMenuItem Bold{"Bold", &Style};
    finalcut::FCheckMenuItem Italic{"Italic", &Style};
    finalcut::FMenu          BColor{"&Color", &Border};
    finalcut::FMenu          BStyle{"&Style", &Border};
    finalcut::FRadioMenuItem BColor1{"Red", &BColor};
    finalcut::FRadioMenuItem BColor2{"Blue", &BColor};
    finalcut::FRadioMenuItem BStyle1{std::move(line), &BStyle};
    finalcut::FRadioMenuItem BStyle2{"-------------", &BStyle};
    finalcut::FRadioMenuItem BStyle3{"- - - - - - -", &BStyle};
    finalcut::FRadioMenuItem BStyle4{"-  -  -  -  -", &BStyle};
    finalcut::FStatusBar     Statusbar{this};
    finalcut::FLabel         Headline1{this};
    finalcut::FLabel         Headline2{this};
    finalcut::FLabel         Info{this};
};

//----------------------------------------------------------------------
Menu::Menu (finalcut::FWidget* parent)
  : finalcut::FDialog{parent}
{
  // Menu bar itms
  File.setStatusbarMessage ("File management commands");
  Edit.setStatusbarMessage ("Cut-and-paste editing commands");
  Choice.setStatusbarMessage ("Choice menu");
  Window.setDisable();
  Help.setStatusbarMessage ("Show version and copyright information");

  // Menu items
  configureFileMenuItems();
  configureEditMenuItems();
  configureChoiceMenuItems();

  // Add default menu item callback
  defaultCallback (&Menubar);

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

  // Headline labels
  Headline1 << " Key ";
  Headline1.ignorePadding();
  Headline1.setEmphasis();

  Headline2 << " Function ";
  Headline2.ignorePadding();
  Headline2.setEmphasis();

  // Info label
  Info << "<F10>            Activate menu bar\n"
       << "<Ctrl>+<Space>   Activate menu bar\n"
       << "<Menu>           Activate menu bar\n"
       << "<Shift>+<Menu>   Open dialog menu\n"
       << "<Meta>+<X>       Exit";
}

//----------------------------------------------------------------------
void Menu::configureFileMenuItems()
{
  // "File" menu items
  New.addAccelerator (FKey::Ctrl_n);  // Ctrl + N
  New.setStatusbarMessage ("Create a new file");
  Open.addAccelerator (FKey::Ctrl_o);  // Ctrl + O
  Open.setStatusbarMessage ("Locate and open a text file");
  Save.addAccelerator (FKey::Ctrl_s);  // Ctrl + S
  Save.setStatusbarMessage ("Save the file");
  SaveAs.setStatusbarMessage ("Save the current file under a different name");
  Close.addAccelerator (FKey::Ctrl_w);  // Ctrl + W
  Close.setStatusbarMessage ("Close the current file");
  Line1.setSeparator();
  Print.addAccelerator (FKey::Ctrl_p);  // Ctrl + P
  Print.setStatusbarMessage ("Print the current file");
  Line2.setSeparator();
  Quit.addAccelerator (FKey::Meta_x);  // Meta/Alt + X
  Quit.setStatusbarMessage ("Exit the program");

  // Add quit menu item callback
  Quit.addCallback
  (
    "clicked",
    finalcut::getFApplication(),
    &finalcut::FApplication::cb_exitApp,
    this
  );
}

//----------------------------------------------------------------------
void Menu::configureEditMenuItems()
{
  // "Edit" menu items
  Undo.setStatusbarMessage ("Undo the previous operation");
  Redo.setDisable();
  Line3.setSeparator();
  Cut.setStatusbarMessage ("Remove the input text "
                           "and put it in the clipboard");
  Copy.setStatusbarMessage ("Copy the input text into the clipboad");
  Paste.setStatusbarMessage ("Insert text form clipboard");
  Line4.setSeparator();
  Search.setStatusbarMessage ("Search for text");
  Next.setStatusbarMessage ("Repeat the last search command");
  Line5.setSeparator();
  SelectAll.setStatusbarMessage ("Select the whole text");
}

//----------------------------------------------------------------------
void Menu::configureChoiceMenuItems()
{
  // "Choice" menu items
  Color.setStatusbarMessage ("Choose a color");
  Style.setStatusbarMessage ("Choose a Style");
  Border.setStatusbarMessage ("Choose Border");

  configureColorMenuItems();
  configureStyleMenuItems();
  configureBorderMenuItems();
}

//----------------------------------------------------------------------
void Menu::configureColorMenuItems()
{
  // "Color" menu items
  Color1.setStatusbarMessage ("Set text red");
  Color2.setStatusbarMessage ("Set text green");
  Color3.setStatusbarMessage ("Set text yellow");
  Color4.setStatusbarMessage ("Set text brue");
  Color5.setStatusbarMessage ("Set text black");
  Color5.setChecked();
}

//----------------------------------------------------------------------
void Menu::configureStyleMenuItems()
{
  // "Style" menu items
  Bold.setStatusbarMessage ("Set text bold");
  Italic.setStatusbarMessage ("Set text italic");
}

//----------------------------------------------------------------------
void Menu::configureBorderMenuItems()
{
  // "Border" menu items
  BColor.setStatusbarMessage ("Choose the border color");
  BStyle.setStatusbarMessage ("Choose the border Style");

  // "BColor" menu items
  BColor1.setStatusbarMessage ("Set red border");
  BColor2.setStatusbarMessage ("Set blue border");

  // "BStyle" menu items
  BStyle1.setChecked();
  BStyle1.setStatusbarMessage ("Set border 1");
  BStyle2.setStatusbarMessage ("Set border 2");
  BStyle3.setStatusbarMessage ("Set border 3");
  BStyle4.setStatusbarMessage ("Set border 4");
}

//----------------------------------------------------------------------
void Menu::defaultCallback (const finalcut::FMenuList* mb)
{
  for (std::size_t i{1}; i <= mb->getCount(); i++)
  {
    auto item = mb->getItem(int(i));

    if ( item
      && item->isEnabled()
      && item->acceptFocus()
      && item->isVisible()
      && ! item->isSeparator()
      && item->getText() != "&Quit" )
    {
      // Add the callback function
      item->addCallback
      (
        "clicked",
        this, &Menu::cb_message,
        item
      );

      // Call sub-menu
      if ( item->hasMenu() )
        defaultCallback (item->getMenu());
    }
  }
}

//----------------------------------------------------------------------
void Menu::initLayout()
{
  Headline1.setGeometry (FPoint{3, 2}, FSize{5, 1});
  Headline2.setGeometry (FPoint{19, 2}, FSize{10, 1});
  Info.setGeometry(FPoint{2, 1}, FSize{36, 5});
  FDialog::initLayout();
}

//----------------------------------------------------------------------
void Menu::adjustSize()
{
  const auto pw = int(getDesktopWidth());
  const auto ph = int(getDesktopHeight());
  setX (1 + (pw - int(getWidth())) / 2, false);
  setY (1 + (ph - int(getHeight())) / 4, false);
  finalcut::FDialog::adjustSize();
}

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

//----------------------------------------------------------------------
void Menu::cb_message (const finalcut::FMenuItem* menuitem)
{
  auto text = menuitem->getText();
  text = text.replace('&', "");
  finalcut::FMessageBox::info ( this
                              , "Info"
                              , "You have chosen \"" + text + "\"" );
}


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

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

  // Create main dialog object
  Menu main_dlg {&app};
  main_dlg.setText ("Menu example");
  main_dlg.setSize ({40, 8});
  main_dlg.setShadow();

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

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