finalcut/examples/menu.cpp

293 lines
11 KiB
C++
Raw Normal View History

/************************************************************************
* menu.cpp - A menu example *
* *
* This file is part of the Final Cut widget toolkit *
* *
* Copyright 2015-2017 Markus Gans *
* *
* The Final Cut is free software; you can redistribute it and/or modify *
* it under the terms of the GNU 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 General Public License for more details. *
* *
* You should have received a copy of the GNU 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 FDialog
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit Menu (FWidget* = 0);
2017-09-11 03:06:02 +02:00
// Destructor
~Menu();
2017-09-11 03:06:02 +02:00
private:
// Disable copy constructor
Menu (const Menu&);
2017-09-11 03:06:02 +02:00
// Disable assignment operator (=)
Menu& operator = (const Menu&);
2017-09-11 03:06:02 +02:00
// Methods
void defaultCallback (FMenuList*);
void adjustSize();
2017-09-11 03:06:02 +02:00
// Event handler
void onClose (FCloseEvent*);
// Callback method
2017-09-11 03:06:02 +02:00
void cb_message (FWidget*, data_ptr);
2015-12-08 21:05:00 +01:00
};
#pragma pack(pop)
//----------------------------------------------------------------------
Menu::Menu (FWidget* parent)
: FDialog(parent)
{
// menu bar
FMenuBar* Menubar = new FMenuBar (this);
// menu bar items
FMenu* File = new FMenu ("&File", Menubar);
File->setStatusbarMessage ("File management commands");
FMenu* Edit = new FMenu ("&Edit", Menubar);
Edit->setStatusbarMessage ("Cut-and-paste editing commands");
FMenu* Choice = new FMenu ("&Choice", Menubar);
Choice->setStatusbarMessage ("Choice menu");
FMenuItem* Window = new FMenuItem ("&Window", Menubar);
Window->setDisable();
FMenuItem* Help = new FMenuItem ("&Help", Menubar);
Help->setStatusbarMessage ("Show version and copyright information");
// "File" menu items
FMenuItem* New = new FMenuItem ("&New", File);
2017-09-11 03:06:02 +02:00
New->addAccelerator (fc::Fckey_n); // Ctrl + N
2015-12-08 21:05:00 +01:00
New->setStatusbarMessage ("Create a new file");
FMenuItem* Open = new FMenuItem ("&Open...", File);
2017-09-11 03:06:02 +02:00
Open->addAccelerator (fc::Fckey_o); // Ctrl + O
2015-12-08 21:05:00 +01:00
Open->setStatusbarMessage ("Locate and open a text file");
FMenuItem* Save = new FMenuItem ("&Save", File);
2017-09-11 03:06:02 +02:00
Save->addAccelerator (fc::Fckey_s); // Ctrl + S
2015-12-08 21:05:00 +01:00
Save->setStatusbarMessage ("Save the file");
FMenuItem* SaveAs = new FMenuItem ("&Save as...", File);
SaveAs->setStatusbarMessage ("Save the current file under a different name");
FMenuItem* Close = new FMenuItem ("&Close", File);
2017-09-11 03:06:02 +02:00
Close->addAccelerator (fc::Fckey_w); // Ctrl + W
2015-12-08 21:05:00 +01:00
Close->setStatusbarMessage ("Close the current file");
FMenuItem* Line1 = new FMenuItem (File);
Line1->setSeparator();
FMenuItem* Print = new FMenuItem ("&Print", File);
2017-09-11 03:06:02 +02:00
Print->addAccelerator (fc::Fckey_p); // Ctrl + P
2015-12-08 21:05:00 +01:00
Print->setStatusbarMessage ("Print the current file");
FMenuItem* Line2 = new FMenuItem (File);
Line2->setSeparator();
FMenuItem* Quit = new FMenuItem ("&Quit", File);
2017-09-11 03:06:02 +02:00
Quit->addAccelerator (fc::Fmkey_x); // Meta/Alt + X
2015-12-08 21:05:00 +01:00
Quit->setStatusbarMessage ("Exit the program");
// "Edit" menu items
FMenuItem* Undo = new FMenuItem (fc::Fckey_z, "&Undo", Edit);
Undo->setStatusbarMessage ("Undo the previous operation");
FMenuItem* Redo = new FMenuItem (fc::Fckey_y, "&Redo", Edit);
Redo->setDisable();
FMenuItem* Line3 = new FMenuItem (Edit);
Line3->setSeparator();
FMenuItem* Cut = new FMenuItem (fc::Fckey_x, "Cu&t", Edit);
2017-09-11 03:06:02 +02:00
Cut->setStatusbarMessage ( "Remove the input text "
"and put it in the clipboard" );
2015-12-08 21:05:00 +01:00
FMenuItem* Copy = new FMenuItem (fc::Fckey_c, "&Copy", Edit);
Copy->setStatusbarMessage ("Copy the input text into the clipboad");
FMenuItem* Paste = new FMenuItem (fc::Fckey_v, "&Paste", Edit);
Paste->setStatusbarMessage ("Insert text form clipboard");
FMenuItem* Line4 = new FMenuItem (Edit);
Line4->setSeparator();
FMenuItem* Search = new FMenuItem (fc::Fckey_f, "&Search", Edit);
Search->setStatusbarMessage ("Search for text");
FMenuItem* Next = new FMenuItem (fc::Fkey_f3, "Search &next", Edit);
Next->setStatusbarMessage ("Repeat the last search command");
FMenuItem* Line5 = new FMenuItem (Edit);
Line5->setSeparator();
FMenuItem* SelectAll = new FMenuItem (fc::Fckey_a, "Select &all", Edit);
SelectAll->setStatusbarMessage ("Select the whole text");
// "Choice" menu items
FMenu* Color = new FMenu ("&Color", Choice);
Color->setStatusbarMessage ("Choose a color");
FMenu* Style = new FMenu ("&Style", Choice);
Style->setStatusbarMessage ("Choose a Style");
FMenu* Border = new FMenu ("&Border", Choice);
Border->setStatusbarMessage ("Choose Border");
// "Color" menu items
FRadioMenuItem* Color1 = new FRadioMenuItem ("Red", Color);
Color1->setStatusbarMessage ("Set text red");
FRadioMenuItem* Color2 = new FRadioMenuItem ("Green", Color);
Color2->setStatusbarMessage ("Set text green");
FRadioMenuItem* Color3 = new FRadioMenuItem ("Yellow", Color);
Color3->setStatusbarMessage ("Set text yellow");
FRadioMenuItem* Color4 = new FRadioMenuItem ("Brue", Color);
Color4->setStatusbarMessage ("Set text brue");
FRadioMenuItem* Color5 = new FRadioMenuItem ("Black", Color);
Color5->setStatusbarMessage ("Set text black");
Color5->setChecked();
// "Style" menu items
FCheckMenuItem* Bold = new FCheckMenuItem ("Bold", Style);
Bold->setStatusbarMessage ("Set text bold");
FCheckMenuItem* Italic = new FCheckMenuItem ("Italic", Style);
Italic->setStatusbarMessage ("Set text italic");
// "Border" menu items
FMenu* BColor = new FMenu ("&Color", Border);
BColor->setStatusbarMessage ("Choose the border color");
FMenu* BStyle = new FMenu ("&Style", Border);
BStyle->setStatusbarMessage ("Choose the border Style");
// "BColor" menu items
FRadioMenuItem* BColor1 = new FRadioMenuItem ("Red", BColor);
BColor1->setStatusbarMessage ("Set red border");
FRadioMenuItem* BColor2 = new FRadioMenuItem ("Blue", BColor);
BColor2->setStatusbarMessage ("Set blue border");
// "BStyle" menu items
FString line(13, wchar_t(fc::BoxDrawingsHorizontal));
FRadioMenuItem* BStyle1 = new FRadioMenuItem (line, BStyle);
BStyle1->setChecked();
BStyle1->setStatusbarMessage ("Set border 1");
FRadioMenuItem* BStyle2 = new FRadioMenuItem ("-------------", BStyle);
BStyle2->setStatusbarMessage ("Set border 2");
FRadioMenuItem* BStyle3 = new FRadioMenuItem ("- - - - - - -", BStyle);
BStyle3->setStatusbarMessage ("Set border 3");
FRadioMenuItem* BStyle4 = new FRadioMenuItem ("- - - - -", BStyle);
BStyle4->setStatusbarMessage ("Set border 4");
2015-12-18 21:48:27 +01:00
// Add default menu item callback
defaultCallback (Menubar);
2015-12-18 21:48:27 +01:00
// Add quit menu item callback
2015-12-08 21:05:00 +01:00
Quit->addCallback
(
"clicked",
F_METHOD_CALLBACK (this, &FApplication::cb_exitApp)
2015-12-08 21:05:00 +01:00
);
// Statusbar at the bottom
FStatusBar* statusbar = new FStatusBar (this);
statusbar->setMessage("Status bar message");
// Headline labels
FLabel* Headline1 = new FLabel(" Key ", this);
Headline1->ignorePadding();
2017-09-11 03:06:02 +02:00
Headline1->setGeometry(3, 2, 5, 1);
2015-12-08 21:05:00 +01:00
Headline1->setEmphasis();
2015-12-12 00:50:59 +01:00
2015-12-08 21:05:00 +01:00
FLabel* Headline2 = new FLabel(" Function ", this);
Headline2->ignorePadding();
2017-09-11 03:06:02 +02:00
Headline2->setGeometry(19, 2, 10, 1);
2015-12-08 21:05:00 +01:00
Headline2->setEmphasis();
2015-12-12 00:50:59 +01:00
2015-12-08 21:05:00 +01:00
// Info label
FLabel* Info = new FLabel("<F10> Activate menu bar\n"
"<Ctrl>+<Space> Activate menu bar\n"
"<Meta>+<X> Exit", this);
2017-09-11 03:06:02 +02:00
Info->setGeometry(2, 1, 36, 3);
2015-12-08 21:05:00 +01:00
}
//----------------------------------------------------------------------
Menu::~Menu()
{ }
2015-12-18 21:48:27 +01:00
//----------------------------------------------------------------------
void Menu::defaultCallback (FMenuList* mb)
{
for (uInt i = 1; i <= mb->getCount(); i++)
2015-12-18 21:48:27 +01:00
{
FMenuItem* item = mb->getItem(int(i));
2015-12-18 21:48:27 +01:00
if ( item
&& 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 = getParentWidget()->getWidth();
int ph = getParentWidget()->getHeight();
setX (1 + (pw - getWidth()) / 2, false);
setY (1 + (ph - getHeight()) / 4, false);
FDialog::adjustSize();
}
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
void Menu::onClose (FCloseEvent* ev)
{
FApplication::closeConfirmationDialog (this, ev);
2015-12-08 21:05:00 +01:00
}
//----------------------------------------------------------------------
void Menu::cb_message (FWidget* widget, data_ptr)
2015-12-08 21:05:00 +01:00
{
FMenuItem* menuitem = static_cast<FMenuItem*>(widget);
FString text = menuitem->getText();
2015-12-08 21:05:00 +01:00
text = text.replace('&', "");
FMessageBox::info (this, "Info", "You have chosen \"" + text + "\"");
}
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
2015-12-08 21:05:00 +01:00
FApplication app (argc, argv);
// 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();
}