]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/include/wxImageBrowserWdg.cxx
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / include / wxImageBrowserWdg.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 #include "wxImageBrowserWdg.h"
27
28 #include <math.h>
29 //#include <gtmlib/math/mathdefs.h>
30 #include <mathdefs.h>
31
32 BEGIN_EVENT_TABLE( wxImageBrowserWdg, wxScrolledWindow )
33     EVT_LEFT_UP( wxImageBrowserWdg::OnMouseLeftClick )
34     EVT_RIGHT_UP( wxImageBrowserWdg::OnMouseRightClick )
35     EVT_SIZE( wxImageBrowserWdg::OnSize )
36     END_EVENT_TABLE( );
37
38 wxImageBrowserWdg::wxImageBrowserWdg(
39     wxWindow* parent,
40     wxWindowID id,
41     int sx,
42     int sy,
43     int gap,
44     const wxPoint& pos,
45     const wxSize& size,
46     long style,
47     const wxString& name
48     )
49     : wxScrolledWindow( parent, id, pos, size, style, name )
50 {
51     _sx = sx;
52     _sy = sy;
53     _gap = gap;
54     _first_selected = -1;
55     _last_selected = -1;
56     _ima_list.RemoveAll( );
57     _ima_list.Create( _sx, _sy );
58     _nrs_list.Clear( );
59
60 }
61
62 void wxImageBrowserWdg::SetVolume(
63                                    unsigned short*** volume,
64                                    wxArrayString& names,
65                                    int width,
66                                    int height,
67                                    int depth,
68                                    int min,
69                                    int max
70                                    )
71 {
72     wxImage tmpI;
73     unsigned char value;
74     unsigned char* data;  //pointeur sur mon image
75 //    int i, j, k;
76
77     tmpI.Create( width, height );
78     _nrs_list = names;
79
80     // Canvas creation
81     _ima_list.RemoveAll( );
82     _ima_list.Create( _sx, _sy );
83
84     for( int k = 0; k < depth; k++ )
85     {
86
87       data = tmpI.GetData();
88       data += 3*width*height;
89 //      unsigned short** voltemp = volume[k];
90       for ( int i = 0; i < width; i++ )
91       {
92         for ( int j = 0; j < height; j++ )
93         {
94           value = ( unsigned char )( 0x00ff*( volume[ k ][ i ][ height - j - 1] - min) / (max - min));
95           data -= 3;
96           data[0] = value;
97           data[1] = value;
98           data[2] = value;
99           //memset( data, value, 3);
100         } //rof
101       } //rof
102
103       //image is set, add it to the list
104       //another image is created so allocation is not a problem
105       _ima_list.Add( wxBitmap( tmpI.Scale( _sx, _sy ) ) );
106     } // rof
107
108 //FIXME:
109 #ifdef __WXMSW__
110     this->OnSize( wxSizeEvent( ) );
111 #else
112   wxSizeEvent myevent(wxSize(-1, -1), this->GetId());
113   this->OnSize(myevent);
114   this->Show(true);
115 #endif
116 }
117
118
119
120 void wxImageBrowserWdg::SetScaleX( int sx )
121 {
122     _sx = sx;
123
124 }
125
126 void wxImageBrowserWdg::SetScaleY( int sy )
127 {
128     _sy = sy;
129
130 }
131
132 void wxImageBrowserWdg::SetGap( int gap )
133 {
134     _gap = gap;
135
136 }
137
138 int wxImageBrowserWdg::GetScaleX( )
139 {
140     return( _sx );
141
142 }
143
144 int wxImageBrowserWdg::GetScaleY( )
145 {
146     return( _sy );
147
148 }
149
150 int wxImageBrowserWdg::GetGap( )
151 {
152     return( _gap );
153
154 }
155
156 int wxImageBrowserWdg::GetFirst( )
157 {
158     return( GTM_MIN( _first_selected, _last_selected ) );
159
160 }
161
162 int wxImageBrowserWdg::GetLast( )
163 {
164     return( GTM_MAX( _first_selected, _last_selected ) );
165
166 }
167
168 void wxImageBrowserWdg::GetSizeParameters( int* n, int* c, int* r )
169 {
170     wxSize sz = this->GetClientSize( );
171
172     *n = _ima_list.GetImageCount( );
173     *c = ( int )floor( ( double )( sz.GetWidth( ) ) / ( double )( _gap + _sx ) );
174     *c = ( *c <= 0 )? 1: *c;
175     *r = ( int )ceil( ( double )( *n ) / ( double )( *c ) );
176
177 }
178
179 void wxImageBrowserWdg::OnDraw( wxDC& dc )
180 {
181     int r, c, i, x, y, n, rt;
182     wxSize sz = this->GetClientSize( );
183
184     dc.SetTextForeground( wxColour( 255, 255, 0 ) );
185     dc.SetBrush( *wxTRANSPARENT_BRUSH );
186
187     this->GetSizeParameters( &n, &c, &r );
188     for( i = 0; i < n; i++ )
189     {
190         rt = ( int )floor( ( double )i / ( double )c );
191         x = ( _sx * ( i % c ) ) + ( ( ( i % c ) + 1 ) * _gap );
192         y = ( _sy * ( rt % r ) ) + ( ( ( rt % r ) + 1 ) * _gap );
193         _ima_list.Draw( i, dc, x, y, wxIMAGELIST_DRAW_NORMAL, true );
194         dc.DrawText( _nrs_list[ i ], x, y );
195         if( _first_selected == i && _last_selected != i )
196         {
197             dc.SetPen( *wxRED_PEN );
198             dc.DrawRectangle( x - ( _gap / 2 ), y - ( _gap / 2 ), _sx + _gap, _sy + _gap );
199
200         }
201         else if( _first_selected != i && _last_selected == i )
202         {
203             dc.SetPen( *wxGREEN_PEN );
204             dc.DrawRectangle( x - ( _gap / 2 ), y - ( _gap / 2 ), _sx + _gap, _sy + _gap );
205
206         }
207         else if( _first_selected == i && _last_selected == i )
208         {
209             dc.SetPen( *wxCYAN_PEN );
210             dc.DrawRectangle( x - ( _gap / 2 ), y - ( _gap / 2 ), _sx + _gap, _sy + _gap );
211
212         } // fi
213
214     } // rof
215
216 }
217
218 void wxImageBrowserWdg::OnSize( wxSizeEvent& event )
219 {
220     int r, c, n;
221     wxSize sz = this->GetClientSize( );
222
223     this->GetSizeParameters( &n, &c, &r );
224
225     // Scroll bars
226     this->SetScrollbars( _sx + _gap, _sy + ( _gap * 2 ), c, r );
227
228 }
229
230 int wxImageBrowserWdg::GetIndexClicked( wxMouseEvent& event )
231 {
232     double re;
233     int x, y, n, c, r, i;
234     wxClientDC dc( this );
235     this->PrepareDC( dc );
236     wxPoint pos = event.GetLogicalPosition( dc );
237
238     this->GetSizeParameters( &n, &c, &r );
239
240     // Image index calculation (Help!!, Mr. wizard!)
241     x = -1;
242     re = ( double )( pos.x ) / ( double )( _gap + _sx );
243     if( ( re - floor( re ) ) >= ( ( double )( _gap ) / ( double )( _gap + _sx ) ) ) x = ( int )floor( re );
244     x = ( x >= 0 && x < c )? x: -1;
245     y = -1;
246     re = ( double )( pos.y ) / ( double )( _gap + _sy );
247     if( ( re - floor( re ) ) >= ( ( double )( _gap ) / ( double )( _gap + _sy ) ) ) y = ( int )floor( re );
248     y = ( y >= 0 && y < r )? y: -1;
249     i = ( x != -1 && y != -1 )? x + ( y * c ): -1;
250     return( ( i < n )? i: -1 );
251
252 }
253
254 void wxImageBrowserWdg::OnMouseLeftClick( wxMouseEvent& event )
255 {
256     // Select!
257     _first_selected = this->GetIndexClicked( event );
258
259     // Drawing
260     this->Refresh( );
261
262 }
263
264 void wxImageBrowserWdg::OnMouseRightClick( wxMouseEvent& event )
265 {
266     // Select!
267     _last_selected = this->GetIndexClicked( event );
268
269     // Drawing
270     this->Refresh( );
271
272 }
273
274 /*
275 void wxImageBrowserWdg::Add( const wxImage& ima, const wxString& nrs )
276 {
277 _ima_list.Add( wxBitmap( ima.Scale( _sx, _sy ) ) );
278 _nrs_list.Add( nrs );
279 this->OnSize( wxSizeEvent( ) );
280 }
281 */