home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / src / msgbox.cpp < prev    next >
C/C++ Source or Header  |  1998-01-19  |  4KB  |  181 lines

  1. /*
  2.  * msgbox.cc
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. #define Uses_MsgBox
  13. #define Uses_TObject
  14. #define Uses_TDialog
  15. #define Uses_TStaticText
  16. #define Uses_TRect
  17. #define Uses_TButton
  18. #define Uses_TProgram
  19. #define Uses_TInputLine
  20. #define Uses_TDeskTop
  21. #define Uses_TLabel
  22. #include <tvision/tv.h>
  23.  
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27.  
  28. static const char *buttonName[] =
  29. {
  30.     MsgBoxText::yesText,
  31.     MsgBoxText::noText,
  32.     MsgBoxText::okText,
  33.     MsgBoxText::cancelText
  34. };
  35.  
  36. static ushort commands[] =
  37. {
  38.     cmYes,
  39.     cmNo,
  40.     cmOK,
  41.     cmCancel
  42. };
  43.  
  44. static const char *Titles[] =
  45. {
  46.     MsgBoxText::warningText,
  47.     MsgBoxText::errorText,
  48.     MsgBoxText::informationText,
  49.     MsgBoxText::confirmText
  50. };
  51.  
  52. ushort messageBoxRect( const TRect &r, const char *msg, ushort aOptions )
  53. {
  54.     TDialog *dialog;
  55.     short i, x, buttonCount;
  56.     TView* buttonList[5];
  57.     ushort ccode;
  58.  
  59.     dialog = new TDialog( r, Titles[aOptions & 0x3] );
  60.  
  61.     dialog->insert(
  62.         new TStaticText(TRect(3, 2, dialog->size.x - 2, dialog->size.y - 3),
  63.                         msg) );
  64.  
  65.     for( i = 0, x = -2, buttonCount = 0; i < 4; i++ )
  66.         {
  67.         if( (aOptions & (0x0100 << i)) != 0)
  68.             {
  69.             buttonList[buttonCount] =
  70.                 new TButton( TRect(0, 0, 10, 2), buttonName[i], commands[i], bfNormal );
  71.             x += buttonList[buttonCount++]->size.x + 2;
  72.             }
  73.         }
  74.  
  75.     x = (dialog->size.x - x) / 2;
  76.  
  77.     for( i = 0; i < buttonCount; i++ )
  78.         {
  79.         dialog->insert(buttonList[i]);
  80.         buttonList[i]->moveTo(x, dialog->size.y - 3);
  81.         x += buttonList[i]->size.x + 2;
  82.         }
  83.  
  84.     dialog->selectNext(False);
  85.  
  86.     ccode = TProgram::application->execView(dialog);
  87.  
  88.     TObject::destroy( dialog );
  89.  
  90.     return ccode;
  91. }
  92.  
  93. ushort messageBoxRect( const TRect &r,
  94.                        ushort aOptions,
  95.                        const char *fmt,
  96.                        ... )
  97. {
  98.     va_list argptr;
  99.     va_start( argptr, fmt );
  100.  
  101.     char msg[256];
  102.     vsprintf( msg, fmt, argptr );
  103.  
  104.     va_end( argptr );
  105.  
  106.     return messageBoxRect( r, msg, aOptions );
  107. }
  108.  
  109. static TRect makeRect()
  110. {
  111.     TRect r( 0, 0, 40, 9 );
  112.     r.move((TProgram::deskTop->size.x - r.b.x) / 2,
  113.            (TProgram::deskTop->size.y - r.b.y) / 2);
  114.     return r;
  115. }
  116.  
  117. ushort messageBox( const char *msg, ushort aOptions )
  118. {
  119.     return messageBoxRect( makeRect(), msg, aOptions );
  120. }
  121.  
  122. ushort messageBox( unsigned aOptions, const char *fmt, ... )
  123. {
  124.     va_list argptr;
  125.     va_start( argptr, fmt );
  126.  
  127.     char msg[256];
  128.     vsprintf( msg, fmt, argptr );
  129.  
  130.     va_end( argptr );
  131.  
  132.     return messageBoxRect( makeRect(), msg, aOptions );
  133. }
  134.  
  135. ushort inputBox( const char *Title, const char *aLabel, char *s, uchar limit )
  136. {
  137.     TRect r(0, 0, 60, 8);
  138.     r.move((TProgram::deskTop->size.x - r.b.x) / 2,
  139.            (TProgram::deskTop->size.y - r.b.y) / 2);
  140.     return inputBoxRect(r, Title, aLabel, s, limit);
  141. }
  142.  
  143. ushort inputBoxRect( const TRect &bounds,
  144.                      const char *Title,
  145.                      const char *aLabel,
  146.                      char *s,
  147.                      uchar limit )
  148. {
  149.     TDialog *dialog;
  150.     TView* control;
  151.     TRect r;
  152.     ushort c;
  153.  
  154.     dialog = new TDialog(bounds, Title);
  155.  
  156.     r = TRect( 4 + strlen(aLabel), 2, dialog->size.x - 3, 3 );
  157.     control = new TInputLine( r, limit );
  158.     dialog->insert( control );
  159.  
  160.     r = TRect(2, 2, 3 + strlen(aLabel), 3);
  161.     dialog->insert( new TLabel( r, aLabel, control ) );
  162.  
  163.     r = TRect( dialog->size.x - 24, dialog->size.y - 4,
  164.                dialog->size.x - 14, dialog->size.y - 2);
  165.     dialog->insert( new TButton(r, MsgBoxText::okText, cmOK, bfDefault));
  166.  
  167.     r.a.x += 12;
  168.     r.b.x += 12;
  169.     dialog->insert( new TButton(r, MsgBoxText::cancelText, cmCancel, bfNormal));
  170.  
  171.     r.a.x += 12;
  172.     r.b.x += 12;
  173.     dialog->selectNext(False);
  174.     dialog->setData(s);
  175.     c = TProgram::application->execView(dialog);
  176.     if( c != cmCancel )
  177.         dialog->getData(s);
  178.     TObject::destroy( dialog );
  179.     return c;
  180. }
  181.