home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / menu / menu.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  7.5 KB  |  314 lines

  1. /****************************************************************************
  2. ** $Id:  qt/menu.cpp   3.0.0   edited Jul 30 17:10 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "menu.h"
  12. #include <qpopupmenu.h>
  13. #include <qapplication.h>
  14. #include <qmessagebox.h>
  15. #include <qpixmap.h>
  16. #include <qpainter.h>
  17.  
  18. /* XPM */
  19. static const char * p1_xpm[] = {
  20. "16 16 3 1",
  21. "     c None",
  22. ".    c #000000000000",
  23. "X    c #FFFFFFFF0000",
  24. "                ",
  25. "                ",
  26. "         ....   ",
  27. "        .XXXX.  ",
  28. " .............. ",
  29. " .XXXXXXXXXXXX. ",
  30. " .XXXXXXXXXXXX. ",
  31. " .XXXXXXXXXXXX. ",
  32. " .XXXXXXXXXXXX. ",
  33. " .XXXXXXXXXXXX. ",
  34. " .XXXXXXXXXXXX. ",
  35. " .XXXXXXXXXXXX. ",
  36. " .XXXXXXXXXXXX. ",
  37. " .XXXXXXXXXXXX. ",
  38. " .............. ",
  39. "                "};
  40.  
  41. /* XPM */
  42. static const char * p2_xpm[] = {
  43. "16 16 3 1",
  44. "     c None",
  45. ".    c #000000000000",
  46. "X    c #FFFFFFFFFFFF",
  47. "                ",
  48. "   ......       ",
  49. "   .XXX.X.      ",
  50. "   .XXX.XX.     ",
  51. "   .XXX.XXX.    ",
  52. "   .XXX.....    ",
  53. "   .XXXXXXX.    ",
  54. "   .XXXXXXX.    ",
  55. "   .XXXXXXX.    ",
  56. "   .XXXXXXX.    ",
  57. "   .XXXXXXX.    ",
  58. "   .XXXXXXX.    ",
  59. "   .XXXXXXX.    ",
  60. "   .........    ",
  61. "                ",
  62. "                "};
  63.  
  64. /* XPM */
  65. static const char * p3_xpm[] = {
  66. "16 16 3 1",
  67. "     c None",
  68. ".    c #000000000000",
  69. "X    c #FFFFFFFFFFFF",
  70. "                ",
  71. "                ",
  72. "   .........    ",
  73. "  ...........   ",
  74. "  ........ ..   ",
  75. "  ...........   ",
  76. "  ...........   ",
  77. "  ...........   ",
  78. "  ...........   ",
  79. "  ...XXXXX...   ",
  80. "  ...XXXXX...   ",
  81. "  ...XXXXX...   ",
  82. "  ...XXXXX...   ",
  83. "   .........    ",
  84. "                ",
  85. "                "};
  86.  
  87.  
  88. /*
  89.   Auxiliary class to provide fancy menu items with different
  90.   fonts. Used for the "bold" and "underline" menu items in the options
  91.   menu.
  92.  */
  93. class MyMenuItem : public QCustomMenuItem
  94. {
  95. public:
  96.     MyMenuItem( const QString& s, const QFont& f )
  97.     : string( s ), font( f ){};
  98.     ~MyMenuItem(){}
  99.  
  100.     void paint( QPainter* p, const QColorGroup& /*cg*/, bool /*act*/, bool /*enabled*/, int x, int y, int w, int h )
  101.     {
  102.     p->setFont ( font );
  103.     p->drawText( x, y, w, h, AlignLeft | AlignVCenter | ShowPrefix | DontClip, string );
  104.     }
  105.  
  106.     QSize sizeHint()
  107.     {
  108.     return QFontMetrics( font ).size( AlignLeft | AlignVCenter | ShowPrefix | DontClip,  string );
  109.     }
  110. private:
  111.     QString string;
  112.     QFont font;
  113. };
  114.  
  115.  
  116. MenuExample::MenuExample( QWidget *parent, const char *name )
  117.     : QWidget( parent, name )
  118. {
  119.     QPixmap p1( p1_xpm );
  120.     QPixmap p2( p2_xpm );
  121.     QPixmap p3( p3_xpm );
  122.  
  123.     QPopupMenu *print = new QPopupMenu( this );
  124.     Q_CHECK_PTR( print );
  125.     print->insertTearOffHandle();
  126.     print->insertItem( "&Print to printer", this, SLOT(printer()) );
  127.     print->insertItem( "Print to &file", this, SLOT(file()) );
  128.     print->insertItem( "Print to fa&x", this, SLOT(fax()) );
  129.     print->insertSeparator();
  130.     print->insertItem( "Printer &Setup", this, SLOT(printerSetup()) );
  131.  
  132.     QPopupMenu *file = new QPopupMenu( this );
  133.     Q_CHECK_PTR( file );
  134.     file->insertItem( p1, "&Open",  this, SLOT(open()), CTRL+Key_O );
  135.     file->insertItem( p2, "&New", this, SLOT(news()), CTRL+Key_N );
  136.     file->insertItem( p3, "&Save", this, SLOT(save()), CTRL+Key_S );
  137.     file->insertItem( "&Close", this, SLOT(closeDoc()), CTRL+Key_W );
  138.     file->insertSeparator();
  139.     file->insertItem( "&Print", print, CTRL+Key_P );
  140.     file->insertSeparator();
  141.     file->insertItem( "E&xit",  qApp, SLOT(quit()), CTRL+Key_Q );
  142.  
  143.     QPopupMenu *edit = new QPopupMenu( this );
  144.     Q_CHECK_PTR( edit );
  145.     int undoID = edit->insertItem( "&Undo", this, SLOT(undo()) );
  146.     int redoID = edit->insertItem( "&Redo", this, SLOT(redo()) );
  147.     edit->setItemEnabled( undoID, FALSE );
  148.     edit->setItemEnabled( redoID, FALSE );
  149.  
  150.     QPopupMenu* options = new QPopupMenu( this );
  151.     Q_CHECK_PTR( options );
  152.     options->insertTearOffHandle();
  153.     options->setCaption("Options");
  154.     options->insertItem( "&Normal Font", this, SLOT(normal()) );
  155.     options->insertSeparator();
  156.  
  157.     options->polish(); // adjust system settings
  158.     QFont f = options->font();
  159.     f.setBold( TRUE );
  160.     boldID = options->insertItem( new MyMenuItem( "&Bold", f ) );
  161.     options->setAccel( CTRL+Key_B, boldID );
  162.     options->connectItem( boldID, this, SLOT(bold()) );
  163.     f = font();
  164.     f.setUnderline( TRUE );
  165.     underlineID = options->insertItem( new MyMenuItem( "&Underline", f ) );
  166.     options->setAccel( CTRL+Key_U, underlineID );
  167.     options->connectItem( underlineID, this, SLOT(underline()) );
  168.  
  169.     isBold = FALSE;
  170.     isUnderline = FALSE;
  171.     options->setCheckable( TRUE );
  172.  
  173.  
  174.     QPopupMenu *help = new QPopupMenu( this );
  175.     Q_CHECK_PTR( help );
  176.     help->insertItem( "&About", this, SLOT(about()), CTRL+Key_H );
  177.     help->insertItem( "About &Qt", this, SLOT(aboutQt()) );
  178.  
  179.     menu = new QMenuBar( this );
  180.     Q_CHECK_PTR( menu );
  181.     menu->insertItem( "&File", file );
  182.     menu->insertItem( "&Edit", edit );
  183.     menu->insertItem( "&Options", options );
  184.     menu->insertSeparator();
  185.     menu->insertItem( "&Help", help );
  186.     menu->setSeparator( QMenuBar::InWindowsStyle );
  187.  
  188.     label = new QLabel( this );
  189.     Q_CHECK_PTR( label );
  190.     label->setGeometry( 20, rect().center().y()-20, width()-40, 40 );
  191.     label->setFrameStyle( QFrame::Box | QFrame::Raised );
  192.     label->setLineWidth( 1 );
  193.     label->setAlignment( AlignCenter );
  194.  
  195.     connect( this,  SIGNAL(explain(const QString&)),
  196.          label, SLOT(setText(const QString&)) );
  197.  
  198.     setMinimumSize( 100, 80 );
  199. }
  200.  
  201.  
  202. void MenuExample::open()
  203. {
  204.     emit explain( "File/Open selected" );
  205. }
  206.  
  207.  
  208. void MenuExample::news()
  209. {
  210.     emit explain( "File/New selected" );
  211. }
  212.  
  213. void MenuExample::save()
  214. {
  215.     emit explain( "File/Save selected" );
  216. }
  217.  
  218.  
  219. void MenuExample::closeDoc()
  220. {
  221.     emit explain( "File/Close selected" );
  222. }
  223.  
  224.  
  225. void MenuExample::undo()
  226. {
  227.     emit explain( "Edit/Undo selected" );
  228. }
  229.  
  230.  
  231. void MenuExample::redo()
  232. {
  233.     emit explain( "Edit/Redo selected" );
  234. }
  235.  
  236.  
  237. void MenuExample::normal()
  238. {
  239.     isBold = FALSE;
  240.     isUnderline = FALSE;
  241.     menu->setItemChecked( boldID, isBold );
  242.     menu->setItemChecked( underlineID, isUnderline );
  243.     emit explain( "Options/Normal selected" );
  244. }
  245.  
  246.  
  247. void MenuExample::bold()
  248. {
  249.     isBold = !isBold;
  250.     menu->setItemChecked( boldID, isBold );
  251.     emit explain( "Options/Bold selected" );
  252. }
  253.  
  254.  
  255. void MenuExample::underline()
  256. {
  257.     isUnderline = !isUnderline;
  258.     menu->setItemChecked( underlineID, isUnderline );
  259.     emit explain( "Options/Underline selected" );
  260. }
  261.  
  262.  
  263. void MenuExample::about()
  264. {
  265.     QMessageBox::about( this, "Qt Menu Example",
  266.             "This example demonstrates simple use of Qt menus.\n"
  267.             "You can cut and paste lines from it to your own\n"
  268.             "programs." );
  269. }
  270.  
  271.  
  272. void MenuExample::aboutQt()
  273. {
  274.     QMessageBox::aboutQt( this, "Qt Menu Example" );
  275. }
  276.  
  277.  
  278. void MenuExample::printer()
  279. {
  280.     emit explain( "File/Printer/Print selected" );
  281. }
  282.  
  283. void MenuExample::file()
  284. {
  285.     emit explain( "File/Printer/Print To File selected" );
  286. }
  287.  
  288. void MenuExample::fax()
  289. {
  290.     emit explain( "File/Printer/Print To Fax selected" );
  291. }
  292.  
  293. void MenuExample::printerSetup()
  294. {
  295.     emit explain( "File/Printer/Printer Setup selected" );
  296. }
  297.  
  298.  
  299. void MenuExample::resizeEvent( QResizeEvent * )
  300. {
  301.     label->setGeometry( 20, rect().center().y()-20, width()-40, 40 );
  302. }
  303.  
  304.  
  305. int main( int argc, char ** argv )
  306. {
  307.     QApplication a( argc, argv );
  308.     MenuExample m;
  309.     m.setCaption("Qt Examples - Menus");
  310.     a.setMainWidget( &m );
  311.     m.show();
  312.     return a.exec();
  313. }
  314.