source: trunk/Lucide/SOURCE/gui/progressDlg.cpp @ 47

Last change on this file since 47 was 35, checked in by Eugene Romanenko, 16 years ago

added licence block to source files

File size: 8.1 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: CDDL 1.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the COMMON DEVELOPMENT AND
5 * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6 * this file except in compliance with the License. You may obtain a copy of
7 * the License at http://www.sun.com/cddl/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is
15 * Eugene Romanenko, netlabs.org.
16 * Portions created by the Initial Developer are Copyright (C) 2006
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the LGPL are applicable instead of those
24 * above. If you wish to allow use of your version of this file only under the
25 * terms of the LGPL, and not to allow others to use your version of this file
26 * under the terms of the CDDL, indicate your decision by deleting the
27 * provisions above and replace them with the notice and other provisions
28 * required by the LGPL. If you do not delete the provisions above, a recipient
29 * may use your version of this file under the terms of any one of the CDDL
30 * or the LGPL.
31 *
32 * ***** END LICENSE BLOCK ***** */
33
34
35#define INCL_GPI
36#define INCL_WIN
37#include <os2.h>
38
39#include <process.h>
40
41#include "luutils.h"
42#include "progressDlg.h"
43#include "Lucide_res.h"
44#include "messages.h"
45
46#define TID_PAINT   1
47
48
49ProgressDlg::ProgressDlg( HWND hWndFrame )
50{
51    hFrame      = hWndFrame;
52    text        = newstrdup( "" );
53    threadFn    = NULL;
54    threadData  = NULL;
55    fn          = NULL;
56    data        = NULL;
57    pOldBarProc = NULL;
58    hpsBuffer   = NULLHANDLE;
59    hdcBuffer   = NULLHANDLE;
60    startPos    = 0;
61}
62
63ProgressDlg::~ProgressDlg()
64{
65    delete text;
66}
67
68void ProgressDlg::show( progressThreadFn _threadFn, void *_threadData )
69{
70    threadFn = _threadFn;
71    threadData = _threadData;
72    WinDlgBox( HWND_DESKTOP, hFrame, progressDlgProc,
73               NULLHANDLE, IDD_PROGRESS, this );
74}
75
76void ProgressDlg::hide()
77{
78    if ( hDialog != NULLHANDLE ) {
79        WinPostMsg( hDialog, WM_COMMAND, MPFROMSHORT( DID_OK ), (MPARAM)0 );
80    }
81}
82
83void ProgressDlg::setBreakFunc( progressBrkFn _fn, void *_data )
84{
85    fn   = _fn;
86    data = _data;
87}
88
89void ProgressDlg::setText( const char *_text )
90{
91    delete text;
92    text = newstrdup( _text );
93    if ( hDialog != NULLHANDLE ) {
94        WinSetDlgItemText( hDialog, IDC_PTEXT, text );
95    }
96}
97
98MRESULT EXPENTRY ProgressDlg::progressDlgProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
99{
100    // This is a static method, so we don't know which instantiation we're
101    // dealing with. But we can get a pseudo-this from the parameter to
102    // WM_INITDLG, which we therafter store with the window and retrieve
103    // as follows:
104    ProgressDlg *_this = (ProgressDlg *)WinQueryWindowULong( hwnd, QWL_USER );
105
106    switch (msg)
107    {
108
109        // Dialog has just been created
110        case WM_INITDLG:
111        {
112            // Save the mp2 into our user data so that subsequent calls have
113            // access to the parent C++ object
114            WinSetWindowULong( hwnd, QWL_USER, (ULONG)mp2 );
115            _this = (ProgressDlg *)mp2;
116            localizeDialog( hwnd );
117            centerWindow( _this->hFrame, hwnd );
118
119            _this->hDialog = hwnd;
120            WinSetDlgItemText( hwnd, IDC_PTEXT, _this->text );
121
122            _this->startPos = 0;
123            HWND hBar = WinWindowFromID( hwnd, IDC_PBAR );
124            HAB hBarHab = WinQueryAnchorBlock( hBar );
125
126            if ( ( _this->hpsBuffer != NULLHANDLE ) &&
127                 ( _this->hdcBuffer != NULLHANDLE ) ) {
128                DestroyGraphicsBuffer( _this->hpsBuffer, _this->hdcBuffer );
129                _this->hpsBuffer = _this->hdcBuffer = NULLHANDLE;
130            }
131
132            RECTL rcl;
133            WinQueryWindowRect( hBar, &rcl );
134            HPS hps = WinGetPS( hBar );
135            CreateGraphicsBuffer( hBarHab, &rcl, hps, &_this->hpsBuffer, &_this->hdcBuffer );
136            WinReleasePS( hps );
137
138            WinSetWindowULong( hBar, QWL_USER, (ULONG)_this );
139            _this->pOldBarProc = WinSubclassWindow( hBar, progressBarProc );
140            WinStartTimer( hBarHab, hBar, TID_PAINT, 1 );
141
142            _beginthread( _this->threadFn, NULL, 65536, _this->threadData );
143
144            return (MRESULT)FALSE;
145        }
146
147        case WM_DESTROY:
148        {
149            _this->hDialog = NULLHANDLE;
150            if ( ( _this->hpsBuffer != NULLHANDLE ) &&
151                 ( _this->hdcBuffer != NULLHANDLE ) ) {
152                DestroyGraphicsBuffer( _this->hpsBuffer, _this->hdcBuffer );
153                _this->hpsBuffer = _this->hdcBuffer = NULLHANDLE;
154            }
155        }
156        break;
157
158        case WM_COMMAND:
159            switch (SHORT1FROMMP(mp1))
160            {
161                case DID_OK:
162                    WinDismissDlg( hwnd, DID_OK );
163                    return (MRESULT)FALSE;
164
165                case DID_CANCEL:
166                    WinEnableControl( hwnd, DID_CANCEL, FALSE );
167                    _this->fn( _this->data );
168                    return (MRESULT)FALSE;
169            };
170            return (MRESULT)FALSE;
171    }
172    return WinDefDlgProc( hwnd, msg, mp1, mp2 );
173}
174
175#define SHAPE_LEN   30
176
177MRESULT EXPENTRY ProgressDlg::progressBarProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
178{
179    ProgressDlg *_this = (ProgressDlg *)WinQueryWindowULong( hwnd, QWL_USER );
180
181    switch ( msg )
182    {
183        case WM_PAINT:
184            _this->wmPaintBar( hwnd );
185            return (MRESULT)FALSE;
186
187        case WM_TIMER:
188            if ( SHORT1FROMMP( mp1 ) == TID_PAINT )
189            {
190                WinInvalidateRect( hwnd, NULL, FALSE );
191                _this->startPos += 3;
192                if ( _this->startPos > ( ( SHAPE_LEN * 2 ) - 1 ) ) {
193                    _this->startPos = 0;
194                }
195            }
196            break;
197    }
198    return _this->pOldBarProc( hwnd, msg, mp1, mp2 );
199}
200
201void ProgressDlg::drawShape( HPS hps, PRECTL r )
202{
203    POINTL ptlStart, ptlEnd;
204    ptlStart.x = r->xLeft;      // x coordinate, lower-left corner of box
205    ptlStart.y = r->yBottom;    // y coordinate, lower-left corner of box
206    ptlEnd.x   = r->xRight;     // x coordinate, upper-right corner of box
207    ptlEnd.y   = r->yTop;       // y coordinate, upper-right corner of box
208    GpiMove( hps, &ptlStart );
209    GpiBox( hps, DRO_FILL, &ptlEnd, 0, 0 ); // Draw sheared box
210}
211
212void ProgressDlg::wmPaintBar( HWND hwnd )
213{
214    RECTL rcl;
215    HPS hps = WinBeginPaint( hwnd, 0, NULL );
216    WinQueryWindowRect( hwnd, &rcl );
217
218    // 161,192,224
219    LONG lclr1 = ( 161 << 16 ) | ( 192 << 8 ) | 224;
220    LONG lclr2 = ( 121 << 16 ) | ( 144 << 8 ) | 168;
221    LONG ltabl[ 2 ] = { lclr1, lclr2 };
222    GpiCreateLogColorTable( hpsBuffer, 0, LCOLF_CONSECRGB, 100, 2, ltabl );
223    WinFillRect( hpsBuffer, &rcl, 100 );
224    GpiSetColor( hpsBuffer, 101 );
225
226    MATRIXLF matlfTransform;
227    matlfTransform.fxM11 = MAKEFIXED( 1, 0 );
228    matlfTransform.fxM12 = MAKEFIXED( 0, 0 );
229    matlfTransform.lM13  = 0;
230    matlfTransform.fxM21 = MAKEFIXED( 0, 65536 / 2 );  // Shear factor .5
231    matlfTransform.fxM22 = MAKEFIXED( 1, 0 );
232    matlfTransform.lM23  = 0;
233    matlfTransform.lM31  = 0;               // Translate 0 units right
234    matlfTransform.lM32  = 0;
235    matlfTransform.lM33  = 1;
236    GpiSetDefaultViewMatrix( hpsBuffer, 9, &matlfTransform, TRANSFORM_REPLACE );
237
238    LONG xPos = startPos - ( SHAPE_LEN * 2 );
239    for ( ; xPos < rcl.xRight; xPos += ( SHAPE_LEN * 2 ) )
240    {
241        RECTL r = { xPos, rcl.yBottom, xPos + SHAPE_LEN, rcl.yTop - 1 };
242        drawShape( hpsBuffer, &r );
243    }
244
245    BlitGraphicsBuffer( hps, hpsBuffer, &rcl );
246    WinEndPaint( hps );
247}
248
Note: See TracBrowser for help on using the repository browser.