home *** CD-ROM | disk | FTP | other *** search
/ In'side Shareware 1995 March / ish0395.iso / os2 / musicmem / source / easy.h < prev    next >
C/C++ Source or Header  |  1994-12-30  |  33KB  |  1,108 lines

  1. // We need to include a few things from OS2.H
  2.  
  3. #define INCL_WIN
  4. #define INCL_GPI
  5. #define INCL_DOSMISC
  6. #define INCL_DOSPROCESS
  7. #define INCL_DEV
  8. #define INCL_SPL
  9. #define INCL_SPLDOSPRINT
  10. #define INCL_BASE
  11. #define INCL_GPIERRORS
  12.  
  13.  
  14. #include <os2.h> // Do not repeat this in your main application
  15.  
  16. // We need a few things from the C library as well.
  17.  
  18. #include "stdio.h"
  19. #include "string.h"
  20. #include "stdlib.h"
  21.  
  22. class Answers
  23. // These are the anwers for the quick Message Dialoges below
  24. {   public :
  25.     enum { yes=MBID_YES, no=MBID_NO, abort=MBID_CANCEL };
  26. };
  27.  
  28. // For debugging:
  29. void dumplong (long n); // dump a long
  30. void dump (char *s); // dump a string
  31.  
  32. class Window;
  33. void Warning (char *s, char *title); // Will display s and beep
  34. void Message (char *s, char *title);
  35. int Question (char *s, char *title); // returns Answers::yes or Answers::no
  36. int QuestionAbort (char *s, char *title); // may return Answers::abort
  37. void Warning (char *s, char *title, Window &w);
  38. void Message (char *s, char *title, Window &w);
  39. int Question (char *s, char *title, Window &w);
  40. int QuestionAbort (char *s, char *title, Window &w);
  41. void Beep (int frequency, double seconds);
  42.  
  43. class Parameter
  44. // Parameter for messages are passed in 32 Bit values.
  45. // Use this class for easy conversion from and to 32 Bit.
  46. {    MPARAM M;
  47.     public :
  48.     Parameter(long m) { M=(MPARAM)m; }
  49.     Parameter(int m1, int m2) { M=MPFROM2SHORT(m1,m2); }
  50.     Parameter(int m) { M=MPFROMSHORT(m); }
  51.     Parameter(void *p) { M=(MPARAM)p; }
  52.     operator MPARAM () { return M; }
  53.     operator LONG () { return (long)M; }
  54.     int lower () { return SHORT1FROMMP(M); }
  55.     int higher () { return SHORT2FROMMP(M); }
  56. };
  57.  
  58.  
  59. class Time
  60. // Handle primitive time management.
  61. {    double Seconds;
  62.     public :
  63.     void set (); // ask current time
  64.     Time () { set(); } // initialize with current time
  65.     double seconds () { return Seconds; } // return seconds since UNIX 0
  66.     operator double () { set(); return Seconds; } // return current time in seconds
  67.     void sleep (double t) { DosSleep(1000*t); } // wait for t seconds
  68. };
  69.  
  70. class String
  71. // Strings are used througout EASY. They are stored on heap.
  72. {    char *P;
  73.     long Size;
  74.     public :
  75.     static long defaultsize; // set String::defaultsize=... to change it
  76.     String (); // initialized with empty default sized string
  77.     String (char *text, long size=defaultsize);
  78.         // initialized with text and size, or defaultsize of shorter
  79.     String (int id); // id from Resource
  80.         // initialized with ressource string (from STRINGTABLE)
  81.     ~String (); // free heap storage
  82.     char *text () { return P; } // return content
  83.     long size () { return Size-1; } // return size
  84.     void copy (char *text, long size); // copy (get new heap space)
  85.     void copy (char *text);
  86.     char *filename (); // treat string as path and return file name
  87.     void stripfilename (); // get rid of the name, make it the path
  88.     operator char * () { return P; }
  89.     operator PSZ () { return P; }
  90.     int todouble (double &x); // convert to double (return TRUE on success)
  91.     int tolong (long &n); // same with long
  92. };
  93.  
  94. class ConvertString : public String
  95. // Additional features: make a String of long and double
  96. {    public :
  97.     ConvertString (long n) : String("",32) { ltoa(n,*this,10); }
  98.     ConvertString (double x) : String("",32)
  99.         { sprintf(*this,"%-0.10g",x); }
  100. };
  101.  
  102. class Flag
  103. // Used for various things. Can be fixed, so that changes are
  104. // discarded.
  105. {    int F,Fix;
  106.     public :
  107.     Flag (int f=0) { F=f; Fix=0; }
  108.     // The followint should be self explaining
  109.     void set () { if (Fix) return; F=1; }
  110.     void clear () { if (Fix) return; F=0; }
  111.     void toggle () { if (Fix) return; F=!F; }
  112.     operator int () { return F; }
  113.     void fix () { Fix=1; }
  114.     void unfix () { Fix=0; }
  115.     // Make Flag=1 possible.
  116.     int operator = (int flag) { if (Fix) return flag; return F=flag; }
  117. };
  118.  
  119. class Rectangle
  120. // A Rectangle can have negative width or height! Used for rubberboxes.
  121. {    LONG X,Y,W,H;
  122.     public :
  123.     Rectangle (LONG x=0, LONG y=0, LONG w=1, LONG h=1)
  124.     {    X=x; Y=y; W=w; H=h;
  125.     }
  126.     LONG x1 ()
  127.     {    if (W<0) return X+W+1;
  128.         else return X;
  129.     }
  130.     LONG x2 ()
  131.     {    if (W>0) return X+W-1;
  132.         else return X;
  133.     }
  134.     LONG y1 ()
  135.     {    if (H<0) return Y+H+1;
  136.         else return Y;
  137.     }
  138.     LONG y2 ()
  139.     {    if (H>0) return Y+H-1;
  140.         else return Y;
  141.     }
  142.     LONG x () { return X; }
  143.     LONG y () { return Y; }
  144.     LONG w () { return W; }
  145.     LONG h () { return H; }
  146.     void resize (LONG w, LONG h)
  147.     {    W=w; H=h;
  148.     }
  149.     void hrescale (double scale);
  150.     void wrescale (double scale);
  151.     // Rescale it to vertical /horizontal relation
  152.     void minsize (LONG wmin, LONG hmin);
  153. };
  154.  
  155. class Program
  156. // This class must be in every program and the instance must be named program
  157. {    HAB Hab;
  158.     HMQ Hmq;
  159.     QMSG Qmsg;
  160.     public :
  161.     Program();
  162.     ~Program();
  163.     int getmessage();
  164.     void dispatch();
  165.     void loop(); // this is the main loop
  166.     void clear_messages(); // dispatch all open messages
  167.     HAB hab() { return Hab; }
  168.     HMQ hmq() { return Hmq; }
  169. };
  170.  
  171. extern Program program; // declare this in your main module
  172.  
  173. class PS;
  174.  
  175. const int FCF_NORMAL=
  176.     FCF_TITLEBAR|FCF_SYSMENU|FCF_SIZEBORDER|FCF_MINMAX|
  177.     FCF_SHELLPOSITION|FCF_TASKLIST|FCF_ICON;
  178. const int FCF_NORMAL_NOSIZE=
  179.     FCF_TITLEBAR|FCF_SYSMENU|FCF_BORDER|
  180.     FCF_SHELLPOSITION|FCF_TASKLIST|FCF_ICON;
  181.  
  182. class Menu;
  183.  
  184. enum clicktype
  185. // type of mouse messages
  186. {     button1,button2,button3,
  187.     button1double,button2double,button3double,
  188.     button1up,button1down,
  189.     button2up,button2down,
  190.     button3up,button3down,
  191.     mousemove
  192. };
  193.  
  194. enum pointertype
  195. // mouse pointer type
  196. {    pointer_wait=SPTR_WAIT,pointer_arrow=SPTR_ARROW,
  197.     pointer_text=SPTR_TEXT,pointer_move=SPTR_MOVE
  198. };
  199.  
  200. class Window
  201. // Elder class of StandardWindow.
  202. {   protected :
  203.     HWND Handle;
  204.     int Width,Height;
  205.     Flag Visible;
  206.     HPOINTER Old,New;
  207.     public :
  208.     Window ();
  209.     HWND handle () { return Handle; }
  210.     int width () { return Width; }
  211.     int height () { return Height; }
  212.     void update () { WinInvalidateRect(Handle,NULL,TRUE); }
  213.     void size (long w, long h); // call size of StandardWindow
  214.     void pos (long x, long y); // dto.
  215.     void validate () { WinValidateRect(Handle,NULL,TRUE); }
  216.         // make the content valid (sometimes good after redraw)
  217.     int visible () { return Visible; } // yes, if not iconized
  218.     void setpointer (pointertype p); // set pointer for the window
  219.     void resetpointer (); // old pointer
  220.     void showpointer ();
  221.     void hidepointer ();
  222.     void message (int msg, Parameter mp1=0, Parameter mp2=0)
  223.     // send a message to the window
  224.     {    WinPostMsg(Handle,msg,mp1,mp2);
  225.     }
  226.     void capture (int flag=1)
  227.     // capture mouse control, needs to be released (capture(0))
  228.     {    WinSetCapture(HWND_DESKTOP,flag?Handle:NULLHANDLE);
  229.     }
  230.     void pos (long &x, long &y, long &w, long &h);
  231.     // ask window position and size
  232.     void quit () { message(WM_CLOSE); }
  233.     // send quit message, best way to leave a program
  234. };
  235.  
  236. class Desktop : public Window
  237. {    public :
  238.     Desktop () { Handle=HWND_DESKTOP; }
  239. };
  240.  
  241. class Scroll
  242. // scroll messages
  243. {    public :
  244.     enum {
  245.         left=SB_LINELEFT,right=SB_LINERIGHT,
  246.         pageleft=SB_PAGELEFT,pageright=SB_PAGERIGHT,
  247.         position=SB_SLIDERPOSITION,
  248.         up=SB_LINEUP,down=SB_LINEDOWN,
  249.         pageup=SB_PAGEUP,pagedown=SB_PAGEDOWN};
  250. };
  251.  
  252. class Alignment
  253. // text alignments
  254. {    public :
  255.     enum {
  256.         left=TA_LEFT,center=TA_CENTER,right=TA_RIGHT,
  257.         top=TA_TOP,middle=TA_HALF,bottom=TA_BOTTOM};
  258. };
  259.  
  260. class Keycode
  261. // key flags
  262. {    public :
  263.     enum {
  264.         up=KC_KEYUP,virtualkey=KC_VIRTUALKEY,
  265.         charkey=KC_CHAR };
  266. };
  267.  
  268. enum {RUBBER_ZERO,RUBBER_START,RUBBER_CANCEL,RUBBER_DONE};
  269.  
  270. class StandardWindow : public Window
  271. // This is the normal window. No subwindows are declared.
  272. // Just a drawing area.
  273. {    HWND FrameHandle;
  274.     Menu *Windowmenu; // pointer to menu
  275.     int Id; // the resource ID
  276.     int Rubber;
  277.     String Name; // the title
  278.     ULONG Flags;
  279.     public :
  280.     enum {normal=FCF_NORMAL,menu=FCF_MENU,keys=FCF_ACCELTABLE,
  281.         vscrollbar=FCF_VERTSCROLL,hscrollbar=FCF_HORZSCROLL,
  282.         titlebar=FCF_TITLEBAR,sysmemu=FCF_SYSMENU,
  283.         sizeborder=FCF_SIZEBORDER,minmax=FCF_MINMAX,
  284.         min=FCF_MINBUTTON,max=FCF_MAXBUTTON,
  285.         shellposition=FCF_SHELLPOSITION,tasklist=FCF_TASKLIST,
  286.         icon=FCF_ICON,normal_nosize=FCF_NORMAL_NOSIZE};
  287.     // declare flags for additional controls
  288.     StandardWindow (int id,
  289.         char *name="",
  290.         ULONG flags=FCF_NORMAL);
  291.     ~StandardWindow ();
  292.     void init ();
  293.     void setmenu (Menu *m) { Windowmenu=m; }
  294.         // automatically called from Menu constructor
  295.     HWND framehandle () { return FrameHandle; }
  296.     void top (); // top the window
  297.     int rubberbox (LONG x, LONG y, clicktype click,
  298.         Rectangle &R, LONG wmin=0, LONG hmin=0,
  299.         double wscale=0, double hscale=0);
  300.         // can be called from clicked. wmin,hmin are minimal sizes.
  301.         // either a width or a height scaling factor are optional.
  302.         // returns RUBBER_DONE etc.
  303.     virtual void redraw (PS &ps);
  304.         // the redraw routine. called by message handler
  305.     virtual void sized () {}
  306.         // called upon resize message
  307.     virtual void clicked (LONG x, LONG y, clicktype click) {}
  308.         // called from message handler upon mouse events
  309.         // clicktype is defined above
  310.     virtual int key (int flags, int code, int scan) { return 0; }
  311.         // called from message handler upon keyboard events
  312.         // flags is of Keycode class
  313.         // code is the ASCII key code, if flag is Keycode::charkey
  314.         // scan is the scan value or the virtual key, if
  315.         // falgs contains Keycode::virtualkey
  316.     void size (LONG w, LONG h);
  317.         // size the windows draw area
  318.     virtual int vscrolled (int scroll, int pos) { return 0; }
  319.         // called from message handler upon scroll messages
  320.     virtual int hscrolled (int scroll, int pos) { return 0; }
  321.     virtual void pres_changed () { update(); }
  322.         // called from message handler, if the windows default
  323.         // values change
  324.     void vscroll (int pos, int min=0, int max=100);
  325.     void hscroll (int pos, int min=0, int max=100);
  326.         // set the scroll bars
  327.     friend MRESULT EXPENTRY MainWindowProc (HWND hwnd,
  328.             ULONG msg,MPARAM mp1, MPARAM mp2);
  329.     void title (char *s); // set window title
  330.     virtual int close () { return 1; }
  331.         // user tries to close the window
  332.         // return 0, if that is not allowed
  333.     void starttimer (double interval, int i=1)
  334.     {    WinStartTimer(program.hab(),Handle,i,interval*1000);
  335.     }
  336.         // start a window timer with the specified interval
  337.         // i is the number of the timer
  338.     void stoptimer (int i=1)
  339.     {    WinStopTimer(program.hab(),Handle,i);
  340.     }
  341.     virtual void timer (int i) {}
  342.         // called from message handler on timer events
  343. };
  344.  
  345. typedef void Menuproc ();
  346.  
  347. class Menuentry
  348. // Internal class to represent a menu entry
  349. {   int Command;
  350.     Menuproc *Proc;
  351.     Menuentry *Next;
  352.     public :
  353.     Menuentry (int command, Menuproc *proc,
  354.         Menuentry *next=NULL)
  355.     {    Command=command;
  356.         Proc=proc;
  357.         Next=next;
  358.     }
  359.     Menuentry *next () { return Next; }
  360.     void call (int command) { Proc(); }
  361.     int command () { return Command; }
  362. };
  363.  
  364. class Menu
  365. // menus appear in a specified window
  366. // all entries must be entered with Menu::add
  367. {   int Command;
  368.     Menuentry *Mp;
  369.     StandardWindow *W;
  370.     public :
  371.     Menu (StandardWindow &window)
  372.     {    Mp=NULL;
  373.         W=&window;
  374.         W->setmenu(this);
  375.     }
  376.     ~Menu ();
  377.     void add (ULONG command, Menuproc *proc)
  378.     {   Mp=new Menuentry(command,proc,Mp);
  379.     }
  380.     int call (int command);
  381.     int command () { return Command; }
  382.         // returns the ID of the last menu selection
  383.     void enable (int id, int flag);
  384.         // enable or disable menu entries
  385.     void check (int id, int flag);
  386.         // check or uncheck menu entries
  387. };
  388.  
  389. class Rgb
  390. // Used to represent colors as ULONG values
  391. {    ULONG Value;
  392.     public :
  393.     Rgb (int red, int green, int blue)
  394.     {    Value=((unsigned long)red<<16)+((unsigned long)green<<8)+blue;
  395.     }
  396.     Rgb (ULONG value=0) { Value=value; }
  397.     operator ULONG () { return Value; }
  398. };
  399.  
  400. class Colors
  401. // The default 16 color palette
  402. {   public :
  403.     enum
  404.     {    def=CLR_DEFAULT,white=CLR_WHITE,black=CLR_BLACK,
  405.         blue=CLR_BLUE,red=CLR_RED,pink=CLR_PINK,green=CLR_GREEN,
  406.         cyan=CLR_CYAN,yellow=CLR_YELLOW,darkgray=CLR_DARKGRAY,
  407.         darkblue=CLR_DARKBLUE,darkred=CLR_DARKRED,
  408.         darkpink=CLR_DARKPINK,darkgreen=CLR_DARKGREEN,
  409.         darkcyan=CLR_DARKCYAN,brown=CLR_BROWN,palegray=CLR_PALEGRAY,
  410.         gray=CLR_PALEGRAY,neutral=CLR_NEUTRAL
  411.     };
  412. };
  413.  
  414. class Markers
  415. // Marker types for PS::mark
  416. {    public :
  417.     enum
  418.     {    def=MARKSYM_DEFAULT,cross=MARKSYM_CROSS,plus=MARKSYM_PLUS,
  419.         diamond=MARKSYM_DIAMOND,star=MARKSYM_SIXPOINTSTAR,
  420.         square=MARKSYM_SQUARE,solidsquare=MARKSYM_SOLIDSQUARE,
  421.         soliddiamond=MARKSYM_SOLIDDIAMOND,
  422.         sixpointstar=MARKSYM_SIXPOINTSTAR,
  423.         eightpointstart=MARKSYM_EIGHTPOINTSTAR,
  424.         dot=MARKSYM_DOT,circle=MARKSYM_SMALLCIRCLE,
  425.         blank=MARKSYM_BLANK
  426.     };
  427. };
  428.  
  429. class Font;
  430. class PS
  431. // The basic interface class for the graphic routines.
  432. // Has WindowPS, RedrawPS, PrinterPS, BitmapPS
  433. // and MetafilePS as children.
  434. {   POINTL P;
  435.     ULONG Color,Alignment;
  436.     protected :
  437.     HPS Handle;
  438.     SIZEL S;
  439.     long X,Y;
  440.     public :
  441.     void setdefaults () // Default Initialisation
  442.     {   X=0; Y=0;
  443.         Handle=NULLHANDLE;
  444.         Color=CLR_DEFAULT; Alignment=TA_LEFT;
  445.     }
  446.     PS ()
  447.     {      setdefaults();
  448.     }
  449.     PS (HPS hps)
  450.     {    setdefaults(); Handle=hps; GpiQueryPS(Handle,&S);
  451.     }
  452.     void clip (long x, long y, long w, long h); // set clipping
  453.     void offset (long x, long y) { X=x; Y=y; } // set zero offset
  454.     long xoffset () { return X; } // return offset
  455.     long yoffset () { return Y; }
  456.     HPS handle () { return Handle; } // return PS handle
  457.     void erase () { GpiErase(Handle); }
  458.     LONG width () { return S.cx; }
  459.     LONG height () { return S.cy; }
  460.     void color (ULONG color) // set draw color
  461.     {    if (Color!=color)
  462.         {    GpiSetColor(Handle,color);
  463.             Color=color;
  464.         }
  465.     }
  466.     void directcolor (int pure=0);
  467.     void setcolor (int index, Rgb rgb, int pure=0);
  468.         // set color index to value
  469.         // pure=1 avoids dithered colors
  470.     void defaultcolors ();
  471.         // reset default palette
  472.     ULONG backcolor () { return GpiQueryBackColor(Handle); }
  473.     void mix (int mode) { GpiSetMix(Handle,mode); }
  474.         // set background mix mode
  475.     void move (LONG c, LONG r, ULONG color=CLR_DEFAULT);
  476.         // move draw position to (c,r)
  477.     void lineto (LONG c, LONG r, ULONG color=CLR_DEFAULT);
  478.     void linerel (LONG w, LONG h, ULONG color=CLR_DEFAULT);
  479.     void point (LONG w, LONG h, ULONG color=CLR_DEFAULT);
  480.     void text (char *s, ULONG color=CLR_DEFAULT,
  481.         ULONG alignment=TA_LEFT, ULONG valignment=TA_BASE);
  482.     void textbox (char *s, long &width, long &height);
  483.         // compute with and height of text
  484.     double textextend (char *s, double vx, double vy);
  485.     void framedarea (Rectangle &R, int r, ULONG col=CLR_DEFAULT);
  486.     void frame (Rectangle &R, int r=0, ULONG color=CLR_DEFAULT);
  487.     void area (Rectangle &R, int r=0, ULONG color=CLR_DEFAULT);
  488.     void framedarea (LONG w, LONG h, int r, ULONG col=CLR_DEFAULT);
  489.     void frame (LONG w, LONG h, int r=0, ULONG color=CLR_DEFAULT);
  490.     void area (LONG w, LONG h, int r=0, ULONG color=CLR_DEFAULT);
  491.         // These function draw a rectangle (filled or not,
  492.         // frame or not), r ist the corner radius for rounded
  493.         // corners.
  494.     void backarea (LONG w, LONG h);
  495.     void mark (LONG w, LONG h, ULONG type=MARKSYM_DEFAULT,
  496.         ULONG color=CLR_DEFAULT);
  497.         // draw a marker
  498.     void circle (LONG c, LONG r, LONG rad, double factor,
  499.         ULONG col=CLR_DEFAULT);
  500.     void filledcircle (LONG c, LONG r, LONG rad, double factor,
  501.         ULONG col=CLR_DEFAULT, ULONG fillcol=CLR_DEFAULT);
  502.         // draw an ellipse
  503.     void arc (LONG c, LONG r, LONG rad, double factor,
  504.         double phi1, double phi2,
  505.         ULONG col=CLR_DEFAULT);
  506.     void pie (LONG c, LONG r, LONG rad, double factor,
  507.         double phi1, double phi2,
  508.         ULONG col=CLR_DEFAULT);
  509.     void filledpie (LONG c, LONG r, LONG rad, double factor,
  510.         double phi1, double phi2,
  511.         ULONG col=CLR_DEFAULT, ULONG fillcol=CLR_DEFAULT);
  512.         // an arc
  513.     void setfont (Font &font, int id=1);
  514.         // set the drawing font
  515.     void image (long w, long h, unsigned char *data);
  516.         // draw a B/W image
  517. };
  518.  
  519. inline void StandardWindow::redraw (PS &ps)
  520. {    ps.erase();
  521. }
  522.  
  523. class WindowPS : public PS
  524. // A PS, which is assigned to a screen window
  525. {   Window *W;
  526.     int getps;
  527.     void set (HPS handle, Window &window)
  528.     {    W=&window;
  529.         Handle=handle;
  530.         S.cx=window.width(); S.cy=window.height();
  531.     }
  532.     public :
  533.     WindowPS (HPS handle, Window &window)
  534.     {    set(handle,window);
  535.         getps=0;
  536.     }
  537.     WindowPS (Window &window)
  538.     {    set(WinGetPS(window.handle()),window);
  539.         getps=1;
  540.     }
  541.     ~WindowPS () { if (getps) WinReleasePS(handle()); }
  542. };
  543.  
  544. class RedrawPS : public WindowPS
  545. // A PS which is generated within a redraw event
  546. // This is only used by the message handler
  547. {    public :
  548.     RedrawPS (HWND hwnd, Window &window) :
  549.         WindowPS(WinBeginPaint(hwnd,NULLHANDLE,NULL),window) {}
  550.     ~RedrawPS () { WinEndPaint(handle()); }
  551. };
  552.  
  553. class MetafilePS : public PS
  554. // A PS accosiciated to a meta file
  555. // The Metafile may be passed to the clipboard
  556. {   HMF Metafilehandle;
  557.     HDC Hdc;
  558.     public :
  559.     MetafilePS (Window &window);
  560.     ~MetafilePS ();
  561.     HMF metafilehandle () { return Metafilehandle; }
  562.     void close ();
  563. };
  564.  
  565. class CopyMode
  566. {      public :
  567.     enum {
  568.         copy=ROP_SRCCOPY, or=ROP_SRCPAINT, xor=ROP_SRCINVERT,
  569.     };
  570. };
  571.  
  572. class BitmapPS : public PS
  573. // A PS for a bitmap assigned and sized to window or
  574. // to the screen with own size.
  575. {    HDC DeviceHandle;
  576.     HBITMAP BitmapHandle;
  577.     PBITMAPINFO2 Info;
  578.     int Valid,Planes,Colorbits;
  579.     public :
  580.     BitmapPS (Window &window); // window size
  581.     BitmapPS (long w, long h);
  582.     ~BitmapPS ();
  583.     void copy (PS &ps, int mode=CopyMode::copy, long x=0, long y=0);
  584.         // copy the bitmap to a PS
  585.     void save (char *filename);
  586.         // save the bitmap on a file
  587.     HBITMAP bitmaphandle () { return BitmapHandle; }
  588. };
  589.  
  590. class Queues
  591. // Gets all queues and selects the default one or a named one.
  592. {    ULONG NQueues;
  593.     PRQINFO3 *Queues;
  594.     PRQINFO3 ChosenQueue;
  595.     public :
  596.     Queues (char *name="");
  597.     ~Queues () { if (Queues) delete Queues; }
  598.     ULONG number () { return NQueues; }
  599.         // number of all queues
  600.     PRQINFO3 *chosen () { return &ChosenQueue; }
  601.         // the default or named queue
  602.     PRQINFO3 *all ();
  603.         // return all queues (number())
  604. };
  605.  
  606. class PrinterPS : public PS
  607. // A PS for the printer
  608. {   HDC HandlePrinter;
  609.     DEVOPENSTRUC Dos;
  610.     String Myname;
  611.     PRQINFO3 Queue;
  612.     public :
  613.     Flag Valid,Open;
  614.     PrinterPS (char *name="Print", int op=1);
  615.         // name is the output name, op=0 means: do not open
  616.         // This will take the default queue
  617.     PrinterPS (Queues &q, char *name="Print", int op=1);
  618.         // The prefered way.
  619.         // Declare a qeueu instance and pass it to the PrinterPS
  620.     ~PrinterPS () { if (Open) close(); }
  621.     char *queuename () { return Dos.pszLogAddress; }
  622.     char *drivername () { return Dos.pszDriverName; }
  623.     void open (); // open (normally at construction time)
  624.     void close (); // close (called from destructor)
  625.     void newpage (); // start a new print page
  626. };
  627.  
  628. class Bitmap
  629. // for bitmaps from the ressource
  630. // used by ValueSetItems in dialogs
  631. {    HBITMAP Handle;
  632.     HDC DeviceHandle;
  633.     HPS PsHandle;
  634.     SIZEL S;
  635.     public :
  636.     Bitmap (int id, int width=0, int height=0);
  637.         // id is the ressource ID
  638.     ~Bitmap ();
  639.     HBITMAP handle () { return Handle; }
  640. };
  641.  
  642. class Help
  643. // help interface
  644. // the program should have a *.HLP file associated to it
  645. // you need to declare a HELPTABLE and at least one HELPSUBTABLE
  646. // HELPTABLE ID_Help
  647. //        BEGIN
  648. //         HELPITEM ID_Window,ID_Helpsubtable,ID_GeneralHelpPage
  649. //      END
  650. // ID_GeneralHelpPage is an ID in your IPF file
  651. // HELPSUBTABLE ID_Helpsubtable
  652. //        BEGIN
  653. //        END
  654. {   HWND Handle;
  655.     Help *H;
  656.     Flag Valid;
  657.     public :
  658.     Help (StandardWindow &window,
  659.         int id, char *filename, char *title="");
  660.         // id is the help tables id
  661.     void Help::general (void) // display general help page
  662.     {    if (Valid) WinSendMsg(Handle,HM_EXT_HELP,NULL,NULL);
  663.     }
  664.     void Help::index (void) // display help index
  665.     {    if (Valid) WinSendMsg(Handle,HM_HELP_INDEX,NULL,NULL);
  666.     }
  667.     void Help::content (void) // display content
  668.     {   if (Valid) WinSendMsg(Handle,HM_HELP_CONTENTS,NULL,NULL);
  669.     }
  670.     void Help::display (int id) // display specific page
  671.     {    if (Valid) WinSendMsg(Handle,HM_DISPLAY_HELP,
  672.             MPFROMSHORT(id),HM_RESOURCEID);
  673.     }
  674.     int valid () { return Valid; } // help file found?
  675. };
  676.  
  677.  
  678. class Thread
  679. // a class to start a a thread
  680. // the instance will call a function of type "int f (Parameter)"
  681. // this allows a Parameter to be passed to the function
  682. // the function returns an exit code
  683. // You need to declare a separate instance of Thread for each thread
  684. {    int (*F) (Parameter);
  685.     TID Tid;
  686.     int Started,Expected;
  687.     long Stacksize;
  688.     Parameter P;
  689.     public :
  690.     Thread (int (*f) (Parameter), long stacksize=4096) : P(0)
  691.     {    F=f;
  692.         Started=0; Stacksize=stacksize;
  693.         Expected=0;
  694.     }
  695.     void start (Parameter p=0); // call the function
  696.     void stop (); // stop the thread destructively
  697.     void suspend (); // suspend it
  698.     void resume (); // resume it after suspension
  699.     void wait ();
  700.         // tell the function that you wait for it
  701.         // the function may call expected() to check this
  702.     int expected () { return Expected; }
  703.     Parameter p () { return P; }
  704.     int call () { return F(P); }
  705.     int started () { return Started; }
  706.         // was the thread already started?
  707.     void finished () { Started=0; }
  708. };
  709.  
  710. class Dialogitem;
  711. class Dialog
  712. // class to display and handle a dialog
  713. // create the dialog in your ressource script
  714. // use the Dialogitem class to implement the various controls
  715. // many controls are already defined here, but you may wish
  716. // to add others
  717. // the dialog knows about the controls added to it and calls
  718. // their handlers on init and exit and change
  719. {    int Result;
  720.     String S;
  721.     Dialogitem *Items;
  722.     Help *H;
  723.     int Hid;
  724.     void init (Window &window, int id);
  725.     Dialog *Next;
  726.     protected :
  727.     int Id;
  728.     HWND Handle;
  729.     Window *W;
  730.     public :    enum { ok=DID_OK,cancel=DID_CANCEL }; // return values
  731.     Dialog (Window &window, int id); // id from ressource
  732.     Dialog (Window &window, int id, Help &h, int hid);
  733.         // add help to a dialog (hid is the help page)
  734.     Dialogitem *entry (Dialogitem *item);
  735.     virtual void carryout (); // display the dialog and start it
  736.     virtual void start () {}
  737.         // you may do additional initializations
  738.     virtual void stop () {}
  739.         // you may add additional exit things
  740.     virtual int handler (int command) { return 0; }
  741.         // you may add a handler, which returns 0 on any
  742.         // command it could handle
  743.     int result () { return Result; }
  744.     char *gettext (int id, char *text, long size=String::defaultsize);
  745.     char *gettext (int id);
  746.     void settext (int id, char *text);
  747.     MRESULT message (int id, int msg,
  748.         Parameter mp1=NULL, Parameter mp2=NULL);
  749.     HWND handle () { return Handle; }
  750.     friend MRESULT EXPENTRY dialogproc (HWND hwnd, ULONG msg,
  751.         MPARAM mp1, MPARAM mp2);
  752.     void finish ()
  753.     {    WinSendMsg(Handle,WM_COMMAND,MPARAM(DID_OK),0);
  754.     }
  755.     virtual int key (int flags, int code, int scan) { return 0; }
  756.     Window *w () { return W; }
  757.     virtual int close () { return 1; }
  758. };
  759.  
  760. class DialogPanel : public Dialog
  761. // A Dialog, which remains open, until it is finished
  762. {    public :
  763.     DialogPanel (Window &window, int id)
  764.      : Dialog(window,id) {}
  765.     DialogPanel (Window &window, int id, Help &h, int hid)
  766.      : Dialog(window,id,h,hid) {}
  767.     virtual void carryout ();
  768.     virtual void show () { carryout(); }
  769.     virtual int close () { return 0; }
  770. };
  771.  
  772. class Dialogitem
  773. // this is a control item in a dialog
  774. // note: if the dialog is declared globally, then all its items
  775. // must be declared globally too.
  776. {    Dialogitem *Next;
  777.     protected :
  778.     int Id;
  779.     Dialog *D;
  780.     public :
  781.     Dialogitem (int id, Dialog &dialog);
  782.         // id is the ressource ID for the control
  783.     Dialogitem *next () { return Next; }
  784.     int id () { return Id; }
  785.     virtual void init () {} // called on dialog start
  786.     virtual void exit () {} // called on dialog end
  787.     virtual void command (Parameter mp1, Parameter mp2) {}
  788.         // called, if a command refers to the item ID
  789.     virtual void notify () {}
  790.         // some child items call this, if the control
  791.         // changes its value
  792.     void finish () { D->finish(); }
  793.         // aborts the dialog
  794. };
  795.  
  796. class ButtonItem : public Dialogitem
  797. // A Button, normally handled by a Dialog::handler()
  798. {    Flag Pressed;
  799.     public :
  800.     ButtonItem (int id, Dialog &dialog)
  801.         : Dialogitem(id,dialog),Pressed(0) {}
  802.     virtual void command (Parameter mp1, Parameter mp2)
  803.     {   Pressed.set();
  804.         notify();
  805.     }
  806.     int flag () { return Pressed; }
  807. };
  808.  
  809. class CheckItem : public Dialogitem
  810. // A check box control item
  811. // You may declare it globally and use reinit(f) to
  812. // set its value
  813. {    int F;
  814.     public :
  815.     CheckItem (int id, Dialog &dialog, int flag=0)
  816.         : Dialogitem(id,dialog),F(flag) {}
  817.         // flag==TRUE checks the item initially
  818.     virtual void init ();
  819.     virtual void exit ();
  820.     virtual void command (Parameter mp1, Parameter mp2);
  821.     void set (int f) { F=f; }
  822.     void reinit (int f) { set(f); init(); }
  823.     int flag () { return F; }
  824.     operator int () { return F; } // return check or not check
  825. };
  826.  
  827.  
  828. class RadioItem;
  829. class RadioButton : public Dialogitem
  830. {    int N;
  831.     Flag F;
  832.     RadioItem *R;
  833.     public :
  834.     RadioButton (int id, Dialog &dialog, int flag,
  835.         RadioItem &r, int n) :
  836.             Dialogitem(id,dialog),N(n),R(&r),F(flag) {}
  837.     virtual void command (Parameter mp1, Parameter mp2);
  838.     virtual void init ();
  839. };
  840.  
  841. class RadioItem
  842. // A radio box item
  843. // it is initialized with the IDs of all its buttons
  844. {    int I,N,*Ids;
  845.     RadioButton **Rb;
  846.     Dialog *D;
  847.     public :
  848.     RadioItem (int *ids, int n, Dialog &dialog, int i=1);
  849.     ~RadioItem ();
  850.     operator int () { return I; } // index of selection
  851.     void sel (int sel);
  852.     virtual void notify () {}
  853. };
  854.  
  855.  
  856. class StringItem : public Dialogitem
  857. // An input line control box
  858. {   String S;
  859.     int Length;
  860.     Flag Readonly;
  861.     public :
  862.     StringItem (int id, Dialog &dialog, char *string, int length=64)
  863.         : Dialogitem(id,dialog),S(string) { Length=length; }
  864.         // initializw with any string
  865.     virtual void init ();
  866.     virtual void exit ();
  867.     void set (char *text) { S.copy(text); }
  868.     void reinit (char *text) { set(text); init(); }
  869.     void limit (int length); // extend limit
  870.     void readonly (int flag=1) { Readonly=flag; }
  871.         // make the control read only
  872.     operator char * () { return S; }
  873. };
  874.  
  875. class DoubleItem : public Dialogitem
  876. // a input line which is interpreted as a double value
  877. {   double X;
  878.     String S;
  879.     Flag Readonly;
  880.     public :
  881.     DoubleItem (int id, Dialog &dialog, double x)
  882.         : Dialogitem(id,dialog),S("",64) { X=x; }
  883.     virtual void init ();
  884.     virtual void exit ();
  885.     void set (double x) { X=x; }
  886.     void reinit (double x) { set(x); init(); }
  887.     void readonly (int flag=1) { Readonly=flag; }
  888.     operator double () { return X; }
  889. };
  890.  
  891.  
  892. class LongItem : public Dialogitem
  893. // same as double item for long values
  894. {   long N;
  895.     String S;
  896.     Flag Readonly;
  897.     public :
  898.     LongItem (int id, Dialog &dialog, long n)
  899.         : Dialogitem(id,dialog),S("",64) { N=n; }
  900.     virtual void init ();
  901.     virtual void exit ();
  902.     void set (long n) { N=n; }
  903.     void reinit (long n) { set(n); init(); }
  904.     void readonly (int flag=1) { Readonly=flag; }
  905.     operator long () { return N; }
  906. };
  907.  
  908. class SpinItem : public Dialogitem
  909. // A spin control box
  910. {   long N,Lower,Upper;
  911.     String S;
  912.     public :
  913.     SpinItem (int id, Dialog &dialog, long n,
  914.         long lower, long upper)
  915.         : Dialogitem(id,dialog),S("",64)
  916.     {    N=n; Lower=lower; Upper=upper;
  917.     }
  918.     // you may specify lower and upper limits
  919.     virtual void init ();
  920.     virtual void exit ();
  921.     void set (long n, long lower, long upper)
  922.     {    N=n; Lower=lower; Upper=upper; }
  923.     void reinit (long n, long lower, long upper)
  924.     {    set(n,lower,upper); init();
  925.     }
  926.     void set (long n) { N=n; }
  927.     void reinit (long n) { set(n); init(); }
  928.     operator long () { return N; }
  929. };
  930.  
  931.  
  932. class SliderItem : public Dialogitem
  933. // a slider control box
  934. // you probably want to modify init to set ticks and labels
  935. // just subclass it with new virtual init
  936. // call SliderItem::init() from there
  937. {   long N;
  938.     String S;
  939.     public :
  940.     SliderItem (int id, Dialog &dialog, long n)
  941.         : Dialogitem(id,dialog),S("",64)
  942.     {    N=n;
  943.     }
  944.     virtual void init ();
  945.     virtual void exit ();
  946.     void set (long n)
  947.     {    N=n; }
  948.     void reinit (long n)
  949.     {    set(n); init();
  950.     }
  951.     void tick (int n, int size=3);
  952.     void label (int n, char *text);
  953.     operator long () { return N; }
  954. };
  955.  
  956.  
  957. class ValuesetItem : public Dialogitem
  958. // a value set control box (for colors or bitmap choices)
  959. // again, you must subclass it to modify init() to enter
  960. // your specific value items
  961. {    int Col,Row;
  962.     public :
  963.     ValuesetItem (int id, Dialog &dialog, int r=1, int c=1)
  964.         : Dialogitem(id,dialog),Col(c),Row(r) {}
  965.     void select (int row, int col);
  966.     void setbitmap (int row, int col, Bitmap &b);
  967.         // set a field to a bitmap
  968.     void setcolor (int row, int col, ULONG color);
  969.         // set a field to a color
  970.     virtual void init () { select(Row,Col); }
  971.     virtual void exit ();
  972.     virtual void command (Parameter mp1, Parameter mp2);
  973.         // messages are passed to this (for reactions to changes)
  974.     int col () { return Col; } // get selected column
  975.     int row () { return Row; } // get selected row
  976. };
  977.  
  978. class ListItem : public Dialogitem
  979. // list control box
  980. // you must subclass to define an init, which inserts the list entries
  981. {   int Selection;
  982.     public :
  983.     ListItem (int id, Dialog &dialog)
  984.         : Dialogitem(id,dialog) {}
  985.     void select (int sel);
  986.     virtual void init () { select(0); }
  987.     virtual void exit ();
  988.     virtual void command (Parameter mp1, Parameter mp2);
  989.     void insert (char *text); // insert a list entry
  990.     operator int () { return Selection; }
  991. };
  992.  
  993. class MultilineItem : public Dialogitem
  994. // multiline control box (a small editor)
  995. // no need to subclass this time
  996. {   String S;
  997.     Flag Readonly;
  998.     public :
  999.     MultilineItem (int id, Dialog &dialog, char *string,
  1000.             int length=1024)
  1001.         : Dialogitem(id,dialog),S(string,length) {}
  1002.     virtual void init ();
  1003.     virtual void exit ();
  1004.     void set (char *text) { S.copy(text); }
  1005.     void reinit (char *text) { set(text); init(); }
  1006.     void limit (int length);
  1007.     void readonly (int flag=1) { Readonly=flag; }
  1008.     operator char * () { return S; }
  1009. };
  1010.  
  1011. class FileSelector
  1012. // a file selector
  1013. // should be static, so that path information is not lost
  1014. {    int Freturn;
  1015.     Window *W;
  1016.     FILEDLG Fd;
  1017.     String Filter,Title,Ok;
  1018.     public :
  1019.     FileSelector(Window &window,
  1020.         char *filter, int saving,
  1021.         char *title="", char *ok=0);
  1022.         // example ...(window,"*.c",1,"Save C text","Save")
  1023.         // saving==FALSE means a load dialog
  1024.     char *select ();
  1025.         // start the dialog
  1026. };
  1027.  
  1028. class FontSelector
  1029. // a font selector
  1030. // after selection, the instance can be used to create a font
  1031. // with the Font class
  1032. {   FONTDLG Fd;
  1033.     String Facename;
  1034.     int Codepage,Type,Result;
  1035.     double Pointsize;
  1036.     public :
  1037.     enum { screen, printer };
  1038.     FontSelector (int type=screen,
  1039.         char *name="", double pointsize=10, int codepage=0)
  1040.         : Type(type),Facename(name,66),Pointsize(pointsize),
  1041.             Codepage(codepage),Result(DID_CANCEL) {}
  1042.     int select (Window &window);
  1043.     FATTRS *fat () { return &Fd.fAttrs; }
  1044.     FONTDLG *fd () { return &Fd; }
  1045.     operator int () { return Result; }
  1046. };
  1047.  
  1048.  
  1049. class Fonts
  1050. // A list of all fonts
  1051. {    long Count;
  1052.     FONTMETRICS *AllFonts;
  1053.     public :
  1054.     Fonts (PS &ps);
  1055.     ~Fonts ();
  1056.     long count () { return Count; }
  1057.     FONTMETRICS *fonts () { return AllFonts; }
  1058. };
  1059.  
  1060. class Font
  1061. // can be initialized by a font selector
  1062. // the window font can be set to any instance of Font
  1063. {    FATTRS Fat;
  1064.     double NominalPointSize,PointSize;
  1065.     public :
  1066.     Font (FontSelector &fs);
  1067.     FATTRS *fat () { return &Fat; }
  1068.     char *name () { return Fat.szFacename; }
  1069.     double nominalsize () { return NominalPointSize; }
  1070.     double pointsize () { return PointSize; }
  1071. };
  1072.  
  1073. class Profile
  1074. // system or user profiles are used to personalize an application
  1075. {    int P;
  1076.     String S,H;
  1077.     public :
  1078.     enum { user=HINI_USERPROFILE,system=HINI_SYSTEMPROFILE };
  1079.     Profile (char *prog, int p=user) : P(p),S(prog),H("") {}
  1080.         // prog is the programs profile name
  1081.     void write (char *k, void *p, unsigned long size)
  1082.     // write binary data to the profile (k is the key)
  1083.     {    PrfWriteProfileData(P,S,k,p,size);
  1084.     }
  1085.     void writestring (char *k, char *i);
  1086.     void writedouble (char *k, double x);
  1087.     void writelong (char *k, long x);
  1088.     void writeint (char *k, int x);
  1089.         // specific key formats
  1090.     int read (char *k, void *p, unsigned long size)
  1091.     {    return PrfQueryProfileData(P,S,k,p,&size);
  1092.     }
  1093.     // read binary data from the profile
  1094.     char *readstring (char *k, char *d="",
  1095.         long size=String::defaultsize);
  1096.     double readdouble (char *k, double x=0);
  1097.     long readlong (char *k, long x=0);
  1098.     int readint (char *k, int x=0);
  1099. };
  1100.  
  1101. class Clipboard
  1102. // clipboard interface
  1103. {    public :
  1104.     void copy (MetafilePS &meta);
  1105.         // copy a meta file to the clipboard
  1106. };
  1107.  
  1108.