finalcut/examples/menu.cpp

391 lines
14 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* = 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 createFileMenuItems (finalcut::FMenu*);
void createEditMenuItems (finalcut::FMenu*);
void createChoiceMenuItems (finalcut::FMenu*);
void createColorMenuItems (finalcut::FMenu*);
void createStyleMenuItems (finalcut::FMenu*);
void createBorderMenuItems (finalcut::FMenu*);
void createBorderColorMenuItems (finalcut::FMenu*);
void createBorderStyleMenuItems (finalcut::FMenu*);
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);
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
finalcut::FMenuBar* Menubar = new finalcut::FMenuBar(this);
2015-12-08 21:05:00 +01:00
// Menu bar items
finalcut::FMenu* File = \
new finalcut::FMenu ("&File", Menubar);
2015-12-08 21:05:00 +01:00
File->setStatusbarMessage ("File management commands");
finalcut::FMenu* Edit = \
new finalcut::FMenu ("&Edit", Menubar);
2015-12-08 21:05:00 +01:00
Edit->setStatusbarMessage ("Cut-and-paste editing commands");
finalcut::FMenu* Choice = \
new finalcut::FMenu ("&Choice", Menubar);
2015-12-08 21:05:00 +01:00
Choice->setStatusbarMessage ("Choice menu");
finalcut::FMenuItem* Window = \
new finalcut::FMenuItem ("&Window", Menubar);
2015-12-08 21:05:00 +01:00
Window->setDisable();
finalcut::FMenuItem* Help = \
new finalcut::FMenuItem ("&Help", Menubar);
2015-12-08 21:05:00 +01:00
Help->setStatusbarMessage ("Show version and copyright information");
// Menu items
createFileMenuItems (File);
createEditMenuItems (Edit);
createChoiceMenuItems (Choice);
// Add default menu item callback
defaultCallback (Menubar);
// Statusbar at the bottom
finalcut::FStatusBar* Statusbar = \
new finalcut::FStatusBar (this);
2018-09-02 03:57:57 +02:00
Statusbar->setMessage("Status bar message");
// Headline labels
finalcut::FLabel* Headline1 = \
new finalcut::FLabel(" Key ", this);
Headline1->ignorePadding();
Headline1->setGeometry(3, 2, 5, 1);
Headline1->setEmphasis();
finalcut::FLabel* Headline2 = \
new finalcut::FLabel(" Function ", this);
Headline2->ignorePadding();
Headline2->setGeometry(19, 2, 10, 1);
Headline2->setEmphasis();
// Info label
finalcut::FLabel* Info = \
new finalcut::FLabel( "<F10> Activate menu bar\n"
"<Ctrl>+<Space> Activate menu bar\n"
"<Meta>+<X> Exit", this );
Info->setGeometry(2, 1, 36, 3);
}
//----------------------------------------------------------------------
Menu::~Menu()
{ }
//----------------------------------------------------------------------
void Menu::createFileMenuItems (finalcut::FMenu* File)
{
2015-12-08 21:05:00 +01:00
// "File" menu items
finalcut::FMenuItem* New = \
new finalcut::FMenuItem ("&New", File);
New->addAccelerator (finalcut::fc::Fckey_n); // Ctrl + N
2015-12-08 21:05:00 +01:00
New->setStatusbarMessage ("Create a new file");
finalcut::FMenuItem* Open = \
new finalcut::FMenuItem ("&Open...", File);
Open->addAccelerator (finalcut::fc::Fckey_o); // Ctrl + O
2015-12-08 21:05:00 +01:00
Open->setStatusbarMessage ("Locate and open a text file");
finalcut::FMenuItem* Save = \
new finalcut::FMenuItem ("&Save", File);
Save->addAccelerator (finalcut::fc::Fckey_s); // Ctrl + S
2015-12-08 21:05:00 +01:00
Save->setStatusbarMessage ("Save the file");
finalcut::FMenuItem* SaveAs = \
new finalcut::FMenuItem ("&Save as...", File);
2015-12-08 21:05:00 +01:00
SaveAs->setStatusbarMessage ("Save the current file under a different name");
finalcut::FMenuItem* Close = \
new finalcut::FMenuItem ("&Close", File);
Close->addAccelerator (finalcut::fc::Fckey_w); // Ctrl + W
2015-12-08 21:05:00 +01:00
Close->setStatusbarMessage ("Close the current file");
finalcut::FMenuItem* Line1 = \
new finalcut::FMenuItem (File);
2015-12-08 21:05:00 +01:00
Line1->setSeparator();
finalcut::FMenuItem* Print = \
new finalcut::FMenuItem ("&Print", File);
Print->addAccelerator (finalcut::fc::Fckey_p); // Ctrl + P
2015-12-08 21:05:00 +01:00
Print->setStatusbarMessage ("Print the current file");
finalcut::FMenuItem* Line2 = \
new finalcut::FMenuItem (File);
2015-12-08 21:05:00 +01:00
Line2->setSeparator();
finalcut::FMenuItem* Quit = \
new finalcut::FMenuItem ("&Quit", File);
Quit->addAccelerator (finalcut::fc::Fmkey_x); // Meta/Alt + X
2015-12-08 21:05:00 +01:00
Quit->setStatusbarMessage ("Exit the program");
// Add quit menu item callback
Quit->addCallback
(
"clicked",
F_METHOD_CALLBACK (this, &finalcut::FApplication::cb_exitApp)
);
}
//----------------------------------------------------------------------
void Menu::createEditMenuItems (finalcut::FMenu* Edit)
{
2015-12-08 21:05:00 +01:00
// "Edit" menu items
finalcut::FMenuItem* Undo = \
new finalcut::FMenuItem (finalcut::fc::Fckey_z, "&Undo", Edit);
2015-12-08 21:05:00 +01:00
Undo->setStatusbarMessage ("Undo the previous operation");
finalcut::FMenuItem* Redo = \
new finalcut::FMenuItem (finalcut::fc::Fckey_y, "&Redo", Edit);
2015-12-08 21:05:00 +01:00
Redo->setDisable();
finalcut::FMenuItem* Line3 = \
new finalcut::FMenuItem (Edit);
2015-12-08 21:05:00 +01:00
Line3->setSeparator();
finalcut::FMenuItem* Cut = \
new finalcut::FMenuItem (finalcut::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" );
finalcut::FMenuItem* Copy = \
new finalcut::FMenuItem (finalcut::fc::Fckey_c, "&Copy", Edit);
2015-12-08 21:05:00 +01:00
Copy->setStatusbarMessage ("Copy the input text into the clipboad");
finalcut::FMenuItem* Paste = \
new finalcut::FMenuItem (finalcut::fc::Fckey_v, "&Paste", Edit);
2015-12-08 21:05:00 +01:00
Paste->setStatusbarMessage ("Insert text form clipboard");
finalcut::FMenuItem* Line4 = \
new finalcut::FMenuItem (Edit);
2015-12-08 21:05:00 +01:00
Line4->setSeparator();
finalcut::FMenuItem* Search = \
new finalcut::FMenuItem (finalcut::fc::Fckey_f, "&Search", Edit);
2015-12-08 21:05:00 +01:00
Search->setStatusbarMessage ("Search for text");
finalcut::FMenuItem* Next = \
new finalcut::FMenuItem (finalcut::fc::Fkey_f3, "Search &next", Edit);
2015-12-08 21:05:00 +01:00
Next->setStatusbarMessage ("Repeat the last search command");
finalcut::FMenuItem* Line5 = \
new finalcut::FMenuItem (Edit);
2015-12-08 21:05:00 +01:00
Line5->setSeparator();
finalcut::FMenuItem* SelectAll = \
new finalcut::FMenuItem (finalcut::fc::Fckey_a, "Select &all", Edit);
2015-12-08 21:05:00 +01:00
SelectAll->setStatusbarMessage ("Select the whole text");
}
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
void Menu::createChoiceMenuItems (finalcut::FMenu* Choice)
{
2015-12-08 21:05:00 +01:00
// "Choice" menu items
finalcut::FMenu* Color = new finalcut::FMenu ("&Color", Choice);
2015-12-08 21:05:00 +01:00
Color->setStatusbarMessage ("Choose a color");
finalcut::FMenu* Style = new finalcut::FMenu ("&Style", Choice);
2015-12-08 21:05:00 +01:00
Style->setStatusbarMessage ("Choose a Style");
finalcut::FMenu* Border = new finalcut::FMenu ("&Border", Choice);
2015-12-08 21:05:00 +01:00
Border->setStatusbarMessage ("Choose Border");
createColorMenuItems (Color);
createStyleMenuItems (Style);
createBorderMenuItems (Border);
}
//----------------------------------------------------------------------
void Menu::createColorMenuItems (finalcut::FMenu* Color)
{
2015-12-08 21:05:00 +01:00
// "Color" menu items
finalcut::FRadioMenuItem* Color1 = \
new finalcut::FRadioMenuItem ("Red", Color);
2015-12-08 21:05:00 +01:00
Color1->setStatusbarMessage ("Set text red");
finalcut::FRadioMenuItem* Color2 = \
new finalcut::FRadioMenuItem ("Green", Color);
2015-12-08 21:05:00 +01:00
Color2->setStatusbarMessage ("Set text green");
finalcut::FRadioMenuItem* Color3 = \
new finalcut::FRadioMenuItem ("Yellow", Color);
2015-12-08 21:05:00 +01:00
Color3->setStatusbarMessage ("Set text yellow");
finalcut::FRadioMenuItem* Color4 = \
new finalcut::FRadioMenuItem ("Brue", Color);
2015-12-08 21:05:00 +01:00
Color4->setStatusbarMessage ("Set text brue");
finalcut::FRadioMenuItem* Color5 = \
new finalcut::FRadioMenuItem ("Black", Color);
2015-12-08 21:05:00 +01:00
Color5->setStatusbarMessage ("Set text black");
Color5->setChecked();
}
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
void Menu::createStyleMenuItems (finalcut::FMenu* Style)
{
2015-12-08 21:05:00 +01:00
// "Style" menu items
finalcut::FCheckMenuItem* Bold = \
new finalcut::FCheckMenuItem ("Bold", Style);
2015-12-08 21:05:00 +01:00
Bold->setStatusbarMessage ("Set text bold");
finalcut::FCheckMenuItem* Italic = \
new finalcut::FCheckMenuItem ("Italic", Style);
2015-12-08 21:05:00 +01:00
Italic->setStatusbarMessage ("Set text italic");
}
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
void Menu::createBorderMenuItems (finalcut::FMenu* Border)
{
2015-12-08 21:05:00 +01:00
// "Border" menu items
finalcut::FMenu* BColor = new finalcut::FMenu ("&Color", Border);
2015-12-08 21:05:00 +01:00
BColor->setStatusbarMessage ("Choose the border color");
finalcut::FMenu* BStyle = new finalcut::FMenu ("&Style", Border);
2015-12-08 21:05:00 +01:00
BStyle->setStatusbarMessage ("Choose the border Style");
createBorderColorMenuItems (BColor);
createBorderStyleMenuItems (BStyle);
}
//----------------------------------------------------------------------
void Menu::createBorderColorMenuItems (finalcut::FMenu* BColor)
{
2015-12-08 21:05:00 +01:00
// "BColor" menu items
finalcut::FRadioMenuItem* BColor1 = \
new finalcut::FRadioMenuItem ("Red", BColor);
2015-12-08 21:05:00 +01:00
BColor1->setStatusbarMessage ("Set red border");
finalcut::FRadioMenuItem* BColor2 = \
new finalcut::FRadioMenuItem ("Blue", BColor);
2015-12-08 21:05:00 +01:00
BColor2->setStatusbarMessage ("Set blue border");
}
2015-12-08 21:05:00 +01:00
//----------------------------------------------------------------------
void Menu::createBorderStyleMenuItems (finalcut::FMenu* BStyle)
{
2015-12-08 21:05:00 +01:00
// "BStyle" menu items
finalcut::FString line(13, wchar_t(finalcut::fc::BoxDrawingsHorizontal));
finalcut::FRadioMenuItem* BStyle1 = \
new finalcut::FRadioMenuItem (line, BStyle);
2015-12-08 21:05:00 +01:00
BStyle1->setChecked();
BStyle1->setStatusbarMessage ("Set border 1");
finalcut::FRadioMenuItem* BStyle2 = \
new finalcut::FRadioMenuItem ("-------------", BStyle);
2015-12-08 21:05:00 +01:00
BStyle2->setStatusbarMessage ("Set border 2");
finalcut::FRadioMenuItem* BStyle3 = \
new finalcut::FRadioMenuItem ("- - - - - - -", BStyle);
2015-12-08 21:05:00 +01:00
BStyle3->setStatusbarMessage ("Set border 3");
finalcut::FRadioMenuItem* BStyle4 = \
new finalcut::FRadioMenuItem ("- - - - -", BStyle);
2015-12-08 21:05:00 +01:00
BStyle4->setStatusbarMessage ("Set border 4");
}
//----------------------------------------------------------------------
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 = getParentWidget()->getWidth();
int ph = getParentWidget()->getHeight();
setX (1 + (pw - getWidth()) / 2, false);
setY (1 + (ph - 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();
}