Author Topic: msg_box_ask utility  (Read 3901 times)

MindprisM

  • Senior Community Member
  • Posts: 127
  • Hero Points: 8
msg_box_ask utility
« on: September 18, 2009, 11:41:32 pm »
Code: [Select]
typeless msg_box_ask(_str string, _str buttons='', _str icon='', _str title='',typeless retcode=0){
   //=========================================================================================
   // DOCUMENTATION
   // ________________________________________________________________________________________
   // msg_box_ask(
   //              _str string,        // no default
   //              _str buttons='',    // '' or o,ok; c,oc; y,n,yn; ync; a,i,ari; r,rc
   //              _str icon='',       // '' or n,h,q,e,i,s
   //              _str title='',      // "Visual Slickedit"
   //              typeless retcode=0  // default 0/1, button index for tri-choice; 1=IDxxxx;
   //                                  // ''= '<button>'; '<button>' = true if button pressed
   //            )
   // params:
   //    ---------------------
   //    string - display text
   //    ---------------------
   //    buttons - one of:
   //                     Input         | Buttons            | Default Icon
   //                     ..............|....................|.............
   //                     '','o','ok'   | OK                 | None
   //                     'c','oc'      | OK cancel          | Question
   //                     'a','i','ari' | ABORT retry ignore | Exclamation
   //                     'y','n','yn'  | YES no             | Question
   //                     'ync'         | YES no cancel      | Question
   //                     'r','rc'      | RETRY cance;       | Stop
   //             default buttons are show in upper case above and can be
   //              otherwise designated by using caps in the param: ie 'yNc'
   //    ---------------------
   //    icon - one of:
   //                  Char  | Meaning
   //                  ......|......................
   //                  ''    | Default of button set
   //                  'n'   | None
   //                  'h'   | Hand
   //                  'q'   | Question
   //                  'e'   | Exclamation
   //                  'i'   | Information
   //                  's'   | Stop
   //    ---------------------
   //    title - Title Bar text, defaults to "Visual SlickEdit"
   //    ---------------------
   //    retcode - one of:
   //                     Param        | Meaning
   //                     .............|.....................................................
   //                     0 or default | return 1/0 for yes/no | ok/cancel | retry/cancel
   //                                  | or return button pressed index,
   //                                  | ie when buttons='ync' and user presses Yes, return 1
   //                     1            | return normal constants
   //                     ''           | return full strings in lowcase 'yes','no' etc
   //                     'yes'        | return boolean for 'yes'==button clicked
   //    ---------------------
   //    returns: -1 or 'fail' or false on confusion
   // =================================================================================================================
   // usage:
   //    msg_box_ask('Show OK with <default> no icon');                                            // returns 1
   //    msg_box_ask('Show OK with <default> no icon','o');                                        // returns 1
   //    msg_box_ask('Show OK with <default> no icon','ok');                                       // returns 1
   //    msg_box_ask('Show OK with <specific> question icon','','q');                              // returns 1
   //    msg_box_ask('Show OK cancel <default> question icon','oc');                               // returns 1
   //    msg_box_ask('Show ok CANCEL <default> question icon','oC');                               // returns 0
   //    msg_box_ask('Show ok CANCEL <default> question icon','c');                                // returns 0
   //    msg_box_ask('Show yes no CANCEL <default> question icon, return index','ynC');            // returns 3
   //    msg_box_ask('Show yes NO cancel <default> question icon, return index','yNc');            // returns 2
   //    msg_box_ask('Show yes no CANCEL <default> question icon, return index','ynC','','',1);    // returns 2,IDCANCEL
   //    msg_box_ask('Show yes NO cancel <default> question icon, return index','yNc','','',1);    // returns 7,IDNO
   //    msg_box_ask('Show yes NO cancel <default> question icon, return index','yNc','','','no'); // returns true
   //    msg_box_ask('Show yes NO cancel <default> question icon, return index','yNc','','','');   // returns 'no'
   // =================================================================================================================
   //
   if (title=='') {
      title='Visual SlickEdit';
   }
   but=0;
   b=lowcase(buttons);
   if (b==''||b=='o') {but=MB_OK;}
   if (b=='oc'||b=='c') {but=MB_OKCANCEL;}
   if (b=='ari'||b=='a'||b=='i') {but=MB_ABORTRETRYIGNORE;}
   if (b=='y'||b=='n'||b=='yn') {but=MB_YESNO;}
   if (b=='ync') {but=MB_YESNOCANCEL;}
   if (b=='r'||b=='rc') {but=MB_RETRYCANCEL;}
   d=0;
   if (b=='n') {d=MB_DEFBUTTON2;}
   if (b=='y') {d=MB_DEFBUTTON1;}
   if (b=='c') {d=MB_DEFBUTTON2;}
   if (b=='a') {d=MB_DEFBUTTON1;}
   if (b=='i') {d=MB_DEFBUTTON3;}
   b=buttons;
   if (pos('C',b)==2) {d=MB_DEFBUTTON2;}
   if (pos('C',b)==3) {d=MB_DEFBUTTON3;}
   if (pos('R',b)==2) {d=MB_DEFBUTTON2;}
   if (pos('R',b)==1) {d=MB_DEFBUTTON1;}
   if (pos('I',b)==3||pos('I',b)==1) {d=MB_DEFBUTTON3;}
   if (pos('N',b)==2) {d=MB_DEFBUTTON2;}
   i=MB_ICONNONE;
   // h,q,e,i,s,n
   if (icon=='n') {i=MB_ICONNONE;}
   if (icon=='h') {i=MB_ICONHAND;}
   if (icon=='q') {i=MB_ICONQUESTION;}
   if (icon=='e') {i=MB_ICONEXCLAMATION;}
   if (icon=='i') {i=MB_ICONINFORMATION;}
   if (icon=='s') {i=MB_ICONSTOP;}
   if (icon=='n') {i=MB_ICONNONE;}
   if (icon=='')  {
      if (but==MB_OK)               {i=MB_ICONNONE;}
      if (but==MB_OKCANCEL)         {i=MB_ICONQUESTION;}
      if (but==MB_ABORTRETRYIGNORE) {i=MB_ICONEXCLAMATION;}
      if (but==MB_YESNO)            {i=MB_ICONQUESTION;}
      if (but==MB_YESNOCANCEL)      {i=MB_ICONQUESTION;}
      if (but==MB_RETRYCANCEL)      {i=MB_ICONSTOP;}
   }
   v=_message_box(string,title,but|d|i);
   if (retcode==0&&retcode._varformat()==VF_INT) {
      if (but==MB_OK) {
         if (v==IDOK) {return 1;}else{return 0;}
      }
      if (but==MB_OKCANCEL) {
         if (v==IDOK) {return 1;}else{return 0;}
      }
      if (but==MB_ABORTRETRYIGNORE) {
         if (v==IDABORT) {return 1;}
         if (v==IDRETRY) {return 2;}
         if (v==IDIGNORE) {return 3;}
      }
      if (but==MB_YESNO) {
         if (v==IDYES) {return 1;}else{return 0;}
      }
      if (but==MB_YESNOCANCEL) {
         if (v==IDYES) {return 1;}
         if (v==IDNO) {return 2;}
         if (v==IDCANCEL) {return 3;}
      }
      if (but==MB_RETRYCANCEL) {
         if (v==IDRETRY) {return 1;}else{return 0;}
      }
      return(0);
   }
   if (retcode==1&&retcode._varformat()==VF_INT) {return v;}
   if (retcode._varformat()==VF_LSTR) {
      if (retcode=='') {
         if (but==MB_OK) {
            if (v==IDOK) {return 'ok';}
         }
         if (but==MB_OKCANCEL) {
            if (v==IDOK) {return 'ok';}else{return 'cancel';}
         }
         if (but==MB_ABORTRETRYIGNORE) {
            if (v==IDABORT) {return 'abort';}
            if (v==IDRETRY) {return 'retry';}
            if (v==IDIGNORE) {return 'ignore';}
         }
         if (but==MB_YESNO) {
            if (v==IDYES) {return 'yes';}else{return 'no';}
         }
         if (but==MB_YESNOCANCEL) {
            if (v==IDYES) {return 'yes';}
            if (v==IDNO) {return 'no';}
            if (v==IDCANCEL) {return 'cancel';}
         }
         if (but==MB_RETRYCANCEL) {
            if (v==IDRETRY) {return 'retry'}else{return 'cancel';}
         }
         return'fail';
      }else{
         r=retcode;
         if (but==MB_OK) {
            if (v==IDOK) {return ('ok'==r);}
         }
         if (but==MB_OKCANCEL) {
            if (v==IDOK) {return ('ok'==r);}else{return ('cancel'==r);}
         }
         if (but==MB_ABORTRETRYIGNORE) {
            if (v==IDABORT) {return ('abort'==r);}
            if (v==IDRETRY) {return ('retry'==r);}
            if (v==IDIGNORE) {return ('ignore'==r);}
         }
         if (but==MB_YESNO) {
            if (v==IDYES) {return ('yes'==r);}else{return ('no'==r);}
         }
         if (but==MB_YESNOCANCEL) {
            if (v==IDYES) {return ('yes'==r);}
            if (v==IDNO) {return ('no'==r);}
            if (v==IDCANCEL) {Msg(111);return ('cancel'==r);}
         }
         if (but==MB_RETRYCANCEL) {
            if (v==IDRETRY) {return ('retry'==r)}else{return ('cancel'==r);}
         }
         return false;
      }
   }
   return(-1);
}