home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 10.ddi / TVSRC.ZIP / MSGBOX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.1 KB  |  193 lines

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