source: trunk/Lucide/gui/progressDlg.cpp @ 367

Last change on this file since 367 was 367, checked in by dmik, 11 years ago

Merged bramches/kmk (r294:365) to trunk.

File size: 8.2 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#include "os2all.h"
36
37#include <process.h>
38#include <stdio.h>
39
40#include "globals.h"
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               _hmod, 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            WinEnableControl( hwnd, DID_CANCEL, _this->fn != NULL );
122
123            _this->startPos = 0;
124            HWND hBar = WinWindowFromID( hwnd, IDC_PBAR );
125            HAB hBarHab = WinQueryAnchorBlock( hBar );
126
127            if ( ( _this->hpsBuffer != NULLHANDLE ) &&
128                 ( _this->hdcBuffer != NULLHANDLE ) ) {
129                DestroyGraphicsBuffer( _this->hpsBuffer, _this->hdcBuffer );
130                _this->hpsBuffer = _this->hdcBuffer = NULLHANDLE;
131            }
132
133            RECTL rcl;
134            WinQueryWindowRect( hBar, &rcl );
135            HPS hps = WinGetPS( hBar );
136            CreateGraphicsBuffer( hBarHab, &rcl, hps, &_this->hpsBuffer, &_this->hdcBuffer );
137            WinReleasePS( hps );
138
139            WinSetWindowULong( hBar, QWL_USER, (ULONG)_this );
140            _this->pOldBarProc = WinSubclassWindow( hBar, progressBarProc );
141            WinStartTimer( hBarHab, hBar, TID_PAINT, 1 );
142
143            _beginthread( _this->threadFn, NULL, 262144, _this->threadData );
144
145            return (MRESULT)FALSE;
146        }
147
148        case WM_DESTROY:
149        {
150            _this->hDialog = NULLHANDLE;
151            if ( ( _this->hpsBuffer != NULLHANDLE ) &&
152                 ( _this->hdcBuffer != NULLHANDLE ) ) {
153                DestroyGraphicsBuffer( _this->hpsBuffer, _this->hdcBuffer );
154                _this->hpsBuffer = _this->hdcBuffer = NULLHANDLE;
155            }
156        }
157        break;
158
159        case WM_COMMAND:
160            switch (SHORT1FROMMP(mp1))
161            {
162                case DID_OK:
163                    WinDismissDlg( hwnd, DID_OK );
164                    return (MRESULT)FALSE;
165
166                case DID_CANCEL:
167                    WinEnableControl( hwnd, DID_CANCEL, FALSE );
168                    if ( _this->fn != NULL ) {
169                        _this->fn( _this->data );
170                    }
171                    return (MRESULT)FALSE;
172            };
173            return (MRESULT)FALSE;
174    }
175    return WinDefDlgProc( hwnd, msg, mp1, mp2 );
176}
177
178#define SHAPE_LEN   30
179
180MRESULT EXPENTRY ProgressDlg::progressBarProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
181{
182    ProgressDlg *_this = (ProgressDlg *)WinQueryWindowULong( hwnd, QWL_USER );
183
184    switch ( msg )
185    {
186        case WM_PAINT:
187            _this->wmPaintBar( hwnd );
188            return (MRESULT)FALSE;
189
190        case WM_TIMER:
191            if ( SHORT1FROMMP( mp1 ) == TID_PAINT )
192            {
193                WinInvalidateRect( hwnd, NULL, FALSE );
194                _this->startPos += 3;
195                if ( _this->startPos > ( ( SHAPE_LEN * 2 ) - 1 ) ) {
196                    _this->startPos = 0;
197                }
198            }
199            break;
200    }
201    return _this->pOldBarProc( hwnd, msg, mp1, mp2 );
202}
203
204void ProgressDlg::drawShape( HPS hps, PRECTL r )
205{
206    POINTL ptlStart, ptlEnd;
207    ptlStart.x = r->xLeft;      // x coordinate, lower-left corner of box
208    ptlStart.y = r->yBottom;    // y coordinate, lower-left corner of box
209    ptlEnd.x   = r->xRight;     // x coordinate, upper-right corner of box
210    ptlEnd.y   = r->yTop;       // y coordinate, upper-right corner of box
211    GpiMove( hps, &ptlStart );
212    GpiBox( hps, DRO_FILL, &ptlEnd, 0, 0 ); // Draw sheared box
213}
214
215void ProgressDlg::wmPaintBar( HWND hwnd )
216{
217    RECTL rcl;
218    HPS hps = WinBeginPaint( hwnd, 0, NULL );
219    WinQueryWindowRect( hwnd, &rcl );
220
221    // 161,192,224
222    LONG lclr1 = ( 161 << 16 ) | ( 192 << 8 ) | 224;
223    LONG lclr2 = ( 121 << 16 ) | ( 144 << 8 ) | 168;
224    LONG ltabl[ 2 ] = { lclr1, lclr2 };
225    GpiCreateLogColorTable( hpsBuffer, 0, LCOLF_CONSECRGB, 100, 2, ltabl );
226    WinFillRect( hpsBuffer, &rcl, 100 );
227    GpiSetColor( hpsBuffer, 101 );
228
229    MATRIXLF matlfTransform;
230    matlfTransform.fxM11 = MAKEFIXED( 1, 0 );
231    matlfTransform.fxM12 = MAKEFIXED( 0, 0 );
232    matlfTransform.lM13  = 0;
233    matlfTransform.fxM21 = MAKEFIXED( 0, 65536 / 2 );  // Shear factor .5
234    matlfTransform.fxM22 = MAKEFIXED( 1, 0 );
235    matlfTransform.lM23  = 0;
236    matlfTransform.lM31  = 0;               // Translate 0 units right
237    matlfTransform.lM32  = 0;
238    matlfTransform.lM33  = 1;
239    GpiSetDefaultViewMatrix( hpsBuffer, 9, &matlfTransform, TRANSFORM_REPLACE );
240
241    LONG xPos = startPos - ( SHAPE_LEN * 2 );
242    for ( ; xPos < rcl.xRight; xPos += ( SHAPE_LEN * 2 ) )
243    {
244        RECTL r = { xPos, rcl.yBottom, xPos + SHAPE_LEN, rcl.yTop - 1 };
245        drawShape( hpsBuffer, &r );
246    }
247
248    BlitGraphicsBuffer( hps, hpsBuffer, &rcl );
249    WinEndPaint( hps );
250}
251
Note: See TracBrowser for help on using the repository browser.