finalcut/examples/menu.cpp

337 lines
12 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* menu.cpp - A menu example *
* *
* 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-12-08 21:05:00 +01:00
#include <final/final.h>
2015-12-18 21:48:27 +01:00
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
// class Menu
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class Menu : public finalcut::FDialog
2015-12-08 21:05:00 +01:00
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit Menu (finalcut::FWidget* = nullptr);
// Disable copy constructor
Menu (const Menu&) = delete;
2017-09-11 03:06:02 +02:00
// Destructor
~Menu();
2017-09-11 03:06:02 +02:00
// Disable assignment operator (=)
Menu& operator = (const Menu&) = delete;
private:
2017-09-11 03:06:02 +02:00
// Methods
void configureFileMenuItems();
void configureEditMenuItems();
void configureChoiceMenuItems();
void configureColorMenuItems();
void configureStyleMenuItems();
void configureBorderMenuItems();
void defaultCallback (finalcut::FMenuList*);
2018-09-24 04:02:35 +02:00
virtual void adjustSize();
2017-09-11 03:06:02 +02:00
// Event handler
2018-09-24 04:02:35 +02:00
virtual void onClose (finalcut::FCloseEvent*);
// Callback method
void cb_message (finalcut::FWidget*, data_ptr);
// Data Members
finalcut::FString line{13, finalcut::fc::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{finalcut::fc::Fckey_z, "&Undo", &Edit};
finalcut::FMenuItem Redo{finalcut::fc::Fckey_y, "&Redo", &Edit};
finalcut::FMenuItem Line3{&Edit};
finalcut::FMenuItem Cut{finalcut::fc::Fckey_x, "Cu&t", &Edit};
finalcut::FMenuItem Copy{finalcut::fc::Fckey_c, "&Copy", &Edit};
finalcut::FMenuItem Paste{finalcut::fc::Fckey_v, "&Paste", &Edit};
finalcut::FMenuItem Line4{&Edit};
finalcut::FMenuItem Search{finalcut::fc::Fckey_f, "&Search", &Edit};
finalcut::FMenuItem Next{finalcut::fc::Fkey_f3, "Search &next", &Edit};
finalcut::FMenuItem Line5{&Edit};
finalcut::FMenuItem SelectAll{finalcut::fc::Fckey_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{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};
2015-12-08 21:05:00 +01:00
};
#pragma pack(pop)
//----------------------------------------------------------------------
Menu::Menu (finalcut::FWidget* parent)
: finalcut::FDialog(parent)
2015-12-08 21:05:00 +01:00
{
// 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");
2015-12-08 21:05:00 +01:00
// 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.setGeometry(3, 2, 5, 1);
Headline1.setEmphasis();
Headline2 << " Function ";
Headline2.ignorePadding();
Headline2.setGeometry(19, 2, 10, 1);
Headline2.setEmphasis();
// Info label
Info << "<F10> Activate menu bar\n"
<< "<Ctrl>+<Space> Activate menu bar\n"
<< "<Meta>+<X> Exit";
Info.setGeometry(2, 1, 36, 3);
}
//----------------------------------------------------------------------
Menu::~Menu()
{ }
//----------------------------------------------------------------------
void Menu::configureFileMenuItems()
{
2015-12-08 21:05:00 +01:00
// "File" menu items
New.addAccelerator (finalcut::fc::Fckey_n); // Ctrl + N
New.setStatusbarMessage ("Create a new file");
Open.addAccelerator (finalcut::fc::Fckey_o); // Ctrl + O
Open.setStatusbarMessage ("Locate and open a text file");
Save.addAccelerator (finalcut::fc::Fckey_s); // Ctrl + S
Save.setStatusbarMessage ("Save the file");
SaveAs.setStatusbarMessage ("Save the current file under a different name");
Close.addAccelerator (finalcut::fc::Fckey_w); // Ctrl + W
Close.setStatusbarMessage ("Close the current file");
Line1.setSeparator();
Print.addAccelerator (finalcut::fc::Fckey_p); // Ctrl + P
Print.setStatusbarMessage ("Print the current file");
Line2.setSeparator();
Quit.addAccelerator (finalcut::fc::Fmkey_x); // Meta/Alt + X
Quit.setStatusbarMessage ("Exit the program");
2015-12-08 21:05:00 +01:00
// Add quit menu item callback
Quit.addCallback
(
"clicked",
F_METHOD_CALLBACK (this, &finalcut::FApplication::cb_exitApp)
);
}
//----------------------------------------------------------------------
void Menu::configureEditMenuItems()
{
2015-12-08 21:05:00 +01:00
// "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");
}
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
void Menu::configureChoiceMenuItems()
{
2015-12-08 21:05:00 +01:00
// "Choice" menu items
Color.setStatusbarMessage ("Choose a color");
Style.setStatusbarMessage ("Choose a Style");
Border.setStatusbarMessage ("Choose Border");
2015-12-08 21:05:00 +01:00
configureColorMenuItems();
configureStyleMenuItems();
configureBorderMenuItems();
}
//----------------------------------------------------------------------
void Menu::configureColorMenuItems()
{
2015-12-08 21:05:00 +01:00
// "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();
}
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
void Menu::configureStyleMenuItems()
{
2015-12-08 21:05:00 +01:00
// "Style" menu items
Bold.setStatusbarMessage ("Set text bold");
Italic.setStatusbarMessage ("Set text italic");
}
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
void Menu::configureBorderMenuItems()
{
2015-12-08 21:05:00 +01:00
// "Border" menu items
BColor.setStatusbarMessage ("Choose the border color");
BStyle.setStatusbarMessage ("Choose the border Style");
2015-12-08 21:05:00 +01:00
// "BColor" menu items
BColor1.setStatusbarMessage ("Set red border");
BColor2.setStatusbarMessage ("Set blue border");
2015-12-08 21:05:00 +01:00
// "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");
2015-12-08 21:05:00 +01:00
}
//----------------------------------------------------------------------
void Menu::defaultCallback (finalcut::FMenuList* mb)
2015-12-18 21:48:27 +01:00
{
for (uInt i = 1; i <= mb->getCount(); i++)
2015-12-18 21:48:27 +01:00
{
finalcut::FMenuItem* item = mb->getItem(int(i));
2015-12-18 21:48:27 +01:00
if ( item
2017-11-26 22:37:18 +01:00
&& item->isEnabled()
&& item->acceptFocus()
&& item->isVisible()
&& ! item->isSeparator()
&& item->getText() != "&Quit" )
2015-12-18 21:48:27 +01:00
{
// Add the callback function
item->addCallback
(
"clicked",
F_METHOD_CALLBACK (this, &Menu::cb_message)
2015-12-18 21:48:27 +01:00
);
// Call sub-menu
if ( item->hasMenu() )
defaultCallback (item->getMenu());
}
}
}
//----------------------------------------------------------------------
void Menu::adjustSize()
{
int pw = int(getParentWidget()->getWidth());
int ph = int(getParentWidget()->getHeight());
setX (1 + (pw - int(getWidth())) / 2, false);
setY (1 + (ph - int(getHeight())) / 4, false);
finalcut::FDialog::adjustSize();
}
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
void Menu::onClose (finalcut::FCloseEvent* ev)
2015-12-08 21:05:00 +01:00
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
2015-12-08 21:05:00 +01:00
}
//----------------------------------------------------------------------
void Menu::cb_message (finalcut::FWidget* widget, data_ptr)
2015-12-08 21:05:00 +01:00
{
finalcut::FMenuItem* menuitem = \
static_cast<finalcut::FMenuItem*>(widget);
finalcut::FString text = menuitem->getText();
2015-12-08 21:05:00 +01:00
text = text.replace('&', "");
finalcut::FMessageBox::info ( this
, "Info"
, "You have chosen \"" + text + "\"" );
2015-12-08 21:05:00 +01:00
}
2015-12-18 21:48:27 +01:00
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
// Create the application object
finalcut::FApplication app (argc, argv);
2015-12-08 21:05:00 +01:00
// Create main dialog object
2015-12-08 21:05:00 +01:00
Menu main_dlg (&app);
main_dlg.setText ("Menu example");
main_dlg.setGeometry (int(1 + (app.getWidth() - 40) / 2), 2, 40, 6);
2015-12-08 21:05:00 +01:00
main_dlg.setShadow();
// Set dialog main_dlg as main widget
2015-12-08 21:05:00 +01:00
app.setMainWidget (&main_dlg);
// Show and start the application
2015-12-08 21:05:00 +01:00
main_dlg.show();
return app.exec();
}