/* ********************************************************************* This source file is a part of the standard library of Scol For the latest info, see http://www.scolring.org Copyright (c) 2014 Stephane Bisaro aka Iri. This program 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 2 of the License, or (at your option) any later version. This program 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ********************************************************************* */ /* * Functions for dialog boxes * See http://redmine.scolring.org/projects/tutorials/wiki/Scol_usage * for more informations */ /*! \file dialog.pkg * \author Scol team * \version 0.1 * \copyright GNU Lesser General Public License 2.0 or later * \brief Customized dialog boxes API * * \remark The packages lib/std/stdlib.pkg , lib/2dos/bitmap.pkg must be loaded before or after * this package. * * This library is under development yet. **/ var L2D_DIALOG_TYPETREE = 0;; var L2D_DIALOG_POSCENTER = 0;; var L2D_DIALOG_POSMOUSE = 1;; var L2D_DIALOG_BUTTONOK = 0;; /*!< button type : OK */ var L2D_DIALOG_BUTTONYES = 1;; /*!< button type : YES */ var L2D_DIALOG_BUTTONCANCEL = 2;; /*!< button type : CANCEL */ var L2D_DIALOG_BUTTONNO = 3;; /*!< button type : NO */ var L2D_DIALOG_BUTTONAPPLY = 4;; /*!< button type : APPLY */ var L2D_DIALOG_BUTTONOTHER = 99;; /*!< button type : OTHER */ typeof L2D_DIALOG_FLAGTOP = I;; typeof L2D_DIALOG_FLAGDOWN = I;; typeof L2D_DIALOG_FLAGDND = I;; typeof L2D_DIALOG_FLAGMENU = I;; typeof L2D_DIALOG_FLAGNOBORDER = I;; typeof L2D_DIALOG_FLAGNOSCOL = I;; typeof L2D_DIALOG_SPACING = I;; // default typeof L2D_DIALOG_BUTTONTYPEICONS = [[I S S] r1];; // nom par defaut et icone en fonction du type de bouton /*! \struct L2D_DIALOG * * \brief Opaque internal structure. You should not call it directly, use * API instead ! * **/ struct L2D_DIALOG = [ iDlgType : I, /*!< current type (info, tree, ...) */ oDlgChn : Chn, /*!< propriatery channel */ oDlgWin : ObjWin, /*!< main window */ iDlgWinFlags : I, /*!< flags of the main window */ oDlgMother : ObjWin, /*!< mother window, can be nil */ oDlgFont : ObjFont, /*!< used font */ iDlgPos : I, /*!< initial position, when the dialog is created */ oDlgSize : [I I], /*!< size of the dialog */ oDlgSpacing : I, /*!< spacing between each graphical elements */ oDlgButtons : [[S I ObjButton ObjBitmap fun [ObjButton S] I] r1], oDlgText : [S ObjText] ] mkL2D_DIALOG;; /*proto std_objIsNil = fun [u0] I;; proto lib2d_bmpLoad = fun [S] ObjBitmap;;*/ // return the size of the first text, or the default values if any. Nil if the structure is not defined yet. fun lib2d_dlg_getsizestr (strDlg)= if std_objIsNil strDlg then nil else let hd strDlg.oDlgTexts -> [s _] in if (std_objIsNil strDlg.oDlgFont) && (std_objIsNil s) then [300 100] // values by default ... else let _GETstringSize strDlg.oDlgFont s -> [ws hs] in [ws+(2*strDlg.oDlgSpacing) hs+(2*strDlg.oDlgSpacing)+20];; /*! \brief Set the size of the dialog box * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG I I] I * * \param L2D_DIALOG : a dialog object * \param I : the new width (in piexel, mini 10 px). Can be nil. * \param I : the new height (in piexel, mini 10 px). Can be nil. * \return I : 0 if success or 1 if an error occurs : * - 1 : the dialog object is nil * \remark If a value is nil, a value will be set from the size of the first defined text * if a such text is already defined. Otherwise, an arbitrary value will be set. **/ fun lib2d_dlgSetSize (strDlg, width, height)= if std_objIsNil strDlg then 1 else ( if (std_objIsNil width) && (std_objIsNil height) then set strDlg.oDlgSize = lib2d_dlg_getsizestr strDlg else if (std_objIsNil width) && (height >= 10) then let lib2d_dlg_getsizestr strDlg -> [w _] in set strDlg.oDlgSize = [w height] else if (width >= 10) && (std_objIsNil height) then let lib2d_dlg_getsizestr strDlg -> [_ h] in set strDlg.oDlgSize = [width h] else if (width >= 10) && (height >= 10) then set strDlg.oDlgSize = [width height] else set strDlg.oDlgSize = [10 10]; 0 );; /*! \brief Get the size of the dialog box * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG] [I I] * * \param L2D_DIALOG : a dialog object * \return [I I] : the width and the height if success or nil if an error occurs. **/ fun lib2d_dlgGetSize (strDlg)= if std_objIsNil strDlg then nil else strDlg.oDlgSize;; /*! \brief Set the initial position of the dialog box * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG I] I * * \param L2D_DIALOG : a dialog object * \param I : the position to set : * - L2D_DIALOG_POSCENTER (centered on the screen), * - L2D_DIALOG_POSMOUSE (centered of the mouse pointer position). * \return I : 0 if success or 1 if an error occurs : * - 1 : the dialog object is nil **/ fun lib2d_dlgSetPos (strDlg, pos)= if std_objIsNil strDlg then 1 else ( set pos = std_clamp pos L2D_DIALOG_POSCENTER L2D_DIALOG_POSMOUSE; set strDlg.iDlgPos = pos; 0 );; /*! \brief Get the initial position of the dialog box * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG] I * * \param L2D_DIALOG : a dialog object * \return I : the position if success or nil if an error occurs : * - L2D_DIALOG_POSCENTER (centered on the screen), * - L2D_DIALOG_POSMOUSE (centered of the mouse pointer position). **/ fun lib2d_dlgGetPos (strDlg)= if std_objIsNil strDlg then nil else strDlg.iDlgPos;; /*! \brief Set the window object of the dialog box. * This is optional but the developer can customized the window himself/herself. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG ObjWin] I * * \param L2D_DIALOG : a dialog object * \param ObjWin : the window object to set. * \return I : 0 if success or 1 if an error occurs : * - 1 : the dialog object is nil * - 2 : the window object is nil **/ fun lib2d_dlgSetWin (strDlg, win)= if std_objIsNil strDlg then 1 else if std_objIsNil win then 2 else ( set strDlg.oDlgWin = win; 0 );; /*! \brief Get the window object of the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG] ObjWin * * \param L2D_DIALOG : a dialog object * \return ObjWin : the window object if success or nil if an error occurs. **/ fun lib2d_dlgGetWin (strDlg)= if std_objIsNil strDlg then nil else strDlg.oDlgWin;; /*! \brief Set the window mother object of the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG ObjWin] I * * \param L2D_DIALOG : a dialog object * \param ObjWin : the mother object to set. Can be nil if no parent to set. * \return I : 0 if success or 1 if an error occurs : * - 1 : the dialog object is nil **/ fun lib2d_dlgSetMother (strDlg, win)= if std_objIsNil strDlg then 1 else ( set strDlg.oDlgMother = win; 0 );; /*! \brief Set the window flags of the dialog box * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG [I r1]] I * * \param L2D_DIALOG : a dialog object * \param [I r1] : a list of flags to set. Each flag can be one of these * following values : * - L2D_DIALOG_FLAGTOP : always on top, * - L2D_DIALOG_FLAGDOWN : display a 3d border, * - L2D_DIALOG_FLAGDND : allow drag and drop inside the dialog, * - L2D_DIALOG_FLAGMENU : add a menu bar to the dialog, * - L2D_DIALOG_FLAGNOBORDER : window has no border, * - L2D_DIALOG_FLAGNOSCOL : removes the "(Scol)" word in dialog title. * \return I : 0 if success or 1 if an error occurs : * - 1 : the dialog object is nil * \remark If the list is nil, the flag L2D_DIALOG_FLAGMENU is set by default. * \remark If a element of the list is another value, it is ignored. **/ fun lib2d_dlgSetWinFlags (strDlg, lFlags)= if std_objIsNil strDlg then 1 else if std_objIsNil lFlags then ( set strDlg.iDlgWinFlags = L2D_DIALOG_FLAGMENU; 0 ) else ( while lFlags != nil do ( set strDlg.iDlgWinFlags = strDlg.iDlgWinFlags + std_clampT hd lFlags [L2D_DIALOG_FLAGTOP L2D_DIALOG_FLAGDOWN L2D_DIALOG_FLAGDND L2D_DIALOG_FLAGMENU L2D_DIALOG_FLAGNOBORDER L2D_DIALOG_FLAGNOSCOL] 0; set lFlags = tl lFlags ); 0 );; /*! \brief Create the font as the default font. It is the Arial. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG I] I * * \param L2D_DIALOG : a dialog object * \param I : the size (should be > 6, otherwise the size will be set to 6). * \return I : 0 if success or a positive value if an error occurs : * - 1 : the dialog object is nil * - 2 : unable to create the font **/ fun lib2d_dlgSetFontDefault (strDlg, size)= if std_objIsNil strDlg then 1 else ( set size = if (size < 6) || (std_objIsNil size) then 6 else size; let _CRfont strDlg.oDlgChn size 0 0 "Arial" -> font in if std_objIsNil font then 2 else ( set strDlg.oDlgFont = font; 0 );; /*! \brief Set the font of the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG ObjFont] I * * \param L2D_DIALOG : a dialog object * \param ObjFont : a scol font object (_CRfont ...). * \return I : 0 if success or a positive value if an error occurs : * - 1 : the dialog object is nil * - 2 : the given font was nil but it is replaced by the default font, with success * - 3 : unable to create the default font if the given font was nil **/ fun lib2d_dlgSetFont (strDlg, font)= if std_objIsNil strDlg then 1 else if std_objIsNil font then if !lib2d_dlgSetFontDefault strDlg 12 then 2 else 3 else ( set strDlg.oDlgFont = font; 0 );; /*! \brief Get the font object used by the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG] ObjFont * * \param L2D_DIALOG : a dialog object * \return ObjFont : the scol font object if success or nil if an error occurs. **/ fun lib2d_dlgGetFont (strDlg)= if std_objIsNil strDlg then nil else strDlg.oDlgFont;; /*! \brief Set the proprietary channel of the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG Chn] I * * \param L2D_DIALOG : a dialog object * \param Chn : a scol channel object. * \return I : 0 if success or a positive value if an error occurs : * - 1 : the dialog object is nil * - 2 : the given channel is nil **/ fun lib2d_dlgSetChannel (strDlg, ch)= if std_objIsNil strDlg then 1 else if std_objIsNil ch then 2 else ( set strDlg.oDlgChn = ch; 0 );; /*! \brief Get the proprietary channel object of the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG] Chn * * \param L2D_DIALOG : a dialog object * \return Chn : the scol channel object if success or nil if an error occurs. **/ fun lib2d_dlgGetChannel (strDlg)= if std_objIsNil strDlg then nil else strDlg.oDlgChn;; /*! \brief Set the spacing between each graphical element of the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG I] I * * \param L2D_DIALOG : a dialog object * \param I : a spacing to set. * \return I : 0 if success or a positive value if an error occurs : * - 1 : the dialog object is nil * - 2 : the given channel is nil **/ fun lib2d_dlgSetSpacing (strDlg, iSp)= if std_objIsNil strDlg then 1 else ( set iSp = if (iSp < 0) || (std_objIsNil iSp) then L2D_DIALOG_SPACING else iSp; set strDlg.oDlgSpacing = iSp; 0 );; /*! \brief Get the spacing of the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG] I * * \param L2D_DIALOG : a dialog object * \return I : the spacing if success or nil if an error occurs. **/ fun lib2d_dlgGetSpacing (strDlg)= if std_objIsNil strDlg then nil else strDlg.oDlgSpacing;; /*! \brief Get the default spacing value. * * \ingroup _2dos_dialog * Prototype : fun [] I * * \return I : the default spacing value. **/ fun lib2d_dlgGetSpacingDefault ()= L2D_DIALOG_SPACING;; /*! \brief Set the main text of the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG S] I * * \param L2D_DIALOG : a dialog object * \param S : the main text to set. * \return I : 0 if success or a positive value if an error occurs : * - 1 : the dialog object is nil **/ fun lib2d_dlgSetText (strDlg, szText)= if std_objIsNil strDlg then 1 else ( set strDlg.oDlgText = szText; 0 );; /*! \brief Get the main text of the dialog box. * * \ingroup _2dos_dialog * Prototype : fun [L2D_DIALOG] S * * \param L2D_DIALOG : a dialog object * \return S : the main text if success or nil if an error occurs. **/ fun lib2d_dlgGetText (strDlg)= if std_objIsNil strDlg then nil else strDlg.oDlgText;; fun lib2d_dlggeticonname (l, iType)= if std_objIsNil tl nil then let hd l -> [_ szDefault szName] in [szDefault szName] else let hd l -> [iT szDefault szName] in if iT == iType then [szDefault szName] else lib2d_dlggeticonname tl l iType;; fun lib2d_dlgsetbuttons (lBut, strDlg)= if std_objIsNil lBut then 0 else let hd lBut -> [szTitle iType funCb] in let if std_objIsNil iType then ["" nil] else lib2d_dlggeticonname L2D_DIALOG_BUTTONTYPEICONS iType -> [szDefault szIconName] in //let lib2d_dlggeticonname L2D_DIALOG_BUTTONTYPEICONS iType -> [szDefault szIconName] in let if std_objIsNil szTitle then szDefault else szTitle -> title in let if std_objIsNil szIconName then nil else lib2d_bmpLoad strcat "lib/2dos/rsc/" szIconName -> bmp in let _CBbutton _CRbuttonBitmap strDlg.oDlgChn bmp funCb -> b in ( set strDlg.oDlgButtons = [szTitle iType bmp funCb] :: strDlg.oDlgButtons; lib2d_dlgsetbuttons tl lBut strDlg );; // oDlgButtons : [[S I ObjButton ObjBitmap fun [ObjButton S] I] r1], fun lib2d_dlgSetButtons (strDlg, lBut)= if std_objIsNil strDlg then 1 else lib2d_dlgsetbuttons lBut strDlg;; // create the window dialog. Return 0 if success. fun lib2d_dlg_crwindow (strDlg)= if std_objIsNil strDlg then 1 else let lib2d_dlgGetSize strDlg -> [w h] in let if strDlg.iDlgPos == L2D_DIALOG_POSMOUSE then _GETscreenPos else let _GETdesktopSize -> [wd hd] in [wd/2 hd/2] -> [x y] in ( set strDlg.oDlgWin = _CRwindow strDlg.oDlgChn strDlg.oDlgMother x-(w/2) y-(h/2) w h strDlg.iDlgWinFlags nil; if std_objIsNil strDlg.oDlgWin then 2 else 0 );; fun lib2d_dlgAbout ()= 0;; fun lib2d_dlg_init ()= set L2D_DIALOG_FLAGTOP = WN_TOPMOST; set L2D_DIALOG_FLAGDOWN = WN_DOWN; set L2D_DIALOG_FLAGDND = WN_DRAGDROP; set L2D_DIALOG_FLAGMENU = WN_MENU; set L2D_DIALOG_FLAGNOBORDER = WN_NOBORDER; set L2D_DIALOG_FLAGNOSCOL = WN_NOSCOL; set L2D_DIALOG_SPACING = 10; set L2D_DIALOG_BUTTONTYPEICONS = [[L2D_DIALOG_BUTTONOK "Ok" "apply2.png"] :: [L2D_DIALOG_BUTTONYES "Yes" "yes.png"] :: [L2D_DIALOG_BUTTONCANCEL "Cancel" "cancel.png"] :: [L2D_DIALOG_BUTTONNO "No" "no.png"] :: [L2D_DIALOG_BUTTONAPPLY "Apply" "apply.png"] :: [L2D_DIALOG_BUTTONOTHER "Button" "unknown.png"] :: nil; 0;; /*! \brief Create a new dialog box object. * * \ingroup _2dos_dialog * Prototype : fun [] L2D_DIALOG * * The dialog box is created with the mostly current parameters. The present API * can set other values for them. Theses default values are : * - the channel is the current channel, * - the dialog box flag is L2D_DIALOG_FLAGMENU to add a menu bar, * - the initial position is L2D_DIALOG_POSCENTER, center of the scrren, * - the spacing between each graphical element is 10 pixel. * \remark Parameters are not set as the used font and other things. See lib2d_dlgSet* * to set them. **/ fun lib2d_dlgNew ()= lib2d_dlg_init; mkL2D_DIALOG [nil _channel nil L2D_DIALOG_FLAGMENU nil nil L2D_DIALOG_POSCENTER nil 10 nil nil];;