]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_tree.cpp
4858a28b267c47adfc074fd702344d0770708868
[vlc] / modules / gui / skins2 / controls / ctrl_tree.cpp
1 /*****************************************************************************
2  * ctrl_tree.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <math.h>
25 #include "ctrl_tree.hpp"
26 #include "../src/os_factory.hpp"
27 #include "../src/os_graphics.hpp"
28 #include "../src/generic_bitmap.hpp"
29 #include "../src/generic_font.hpp"
30 #include "../src/scaled_bitmap.hpp"
31 #include "../utils/position.hpp"
32 #include "../utils/ustring.hpp"
33 #include "../events/evt_key.hpp"
34 #include "../events/evt_mouse.hpp"
35 #include "../events/evt_scroll.hpp"
36 #include "vlc_keys.h"
37 #ifdef sun
38 #   include "solaris_specific.h" // for lrint
39 #endif
40
41 #define SCROLL_STEP 0.05
42 #define LINE_INTERVAL 1  // Number of pixels inserted between 2 lines
43
44
45 CtrlTree::CtrlTree( intf_thread_t *pIntf,
46                     VarTree &rTree,
47                     const GenericFont &rFont,
48                     const GenericBitmap *pBgBitmap,
49                     const GenericBitmap *pItemBitmap,
50                     const GenericBitmap *pOpenBitmap,
51                     const GenericBitmap *pClosedBitmap,
52                     uint32_t fgColor,
53                     uint32_t playColor,
54                     uint32_t bgColor1,
55                     uint32_t bgColor2,
56                     uint32_t selColor,
57                     const UString &rHelp,
58                     VarBool *pVisible ):
59     CtrlGeneric( pIntf,rHelp, pVisible), m_rTree( rTree), m_rFont( rFont ),
60     m_pBgBitmap( pBgBitmap ), m_pItemBitmap( pItemBitmap ),
61     m_pOpenBitmap( pOpenBitmap ), m_pClosedBitmap( pClosedBitmap ),
62     m_fgColor( fgColor ), m_playColor( playColor ), m_bgColor1( bgColor1 ),
63     m_bgColor2( bgColor2 ), m_selColor( selColor ),
64     m_pLastSelected( NULL ), m_pImage( NULL )
65 {
66     // Observe the tree and position variables
67     m_rTree.addObserver( this );
68     m_rTree.getPositionVar().addObserver( this );
69
70     m_lastPos = m_rTree.begin();
71
72     makeImage();
73 }
74
75 CtrlTree::~CtrlTree()
76 {
77     m_rTree.getPositionVar().delObserver( this );
78     m_rTree.delObserver( this );
79     if( m_pImage )
80     {
81         delete m_pImage;
82     }
83 }
84
85 int CtrlTree::itemHeight()
86 {
87     int itemHeight = m_rFont.getSize();
88     if( m_pClosedBitmap )
89     {
90         itemHeight = __MAX( m_pClosedBitmap->getHeight(), itemHeight );
91     }
92     if( m_pOpenBitmap )
93     {
94         itemHeight = __MAX( m_pOpenBitmap->getHeight(), itemHeight );
95     }
96     if( m_pItemBitmap )
97     {
98         itemHeight = __MAX( m_pItemBitmap->getHeight(), itemHeight );
99     }
100     itemHeight += LINE_INTERVAL;
101     return itemHeight;
102 }
103
104 int CtrlTree::itemImageWidth()
105 {
106     int bitmapWidth = 5;
107     if( m_pClosedBitmap )
108     {
109         bitmapWidth = __MAX( m_pClosedBitmap->getWidth(), bitmapWidth );
110     }
111     if( m_pOpenBitmap )
112     {
113         bitmapWidth = __MAX( m_pOpenBitmap->getWidth(), bitmapWidth );
114     }
115     if( m_pItemBitmap )
116     {
117         bitmapWidth = __MAX( m_pItemBitmap->getWidth(), bitmapWidth );
118     }
119     return bitmapWidth + 2;
120 }
121
122 int CtrlTree::maxItems()
123 {
124     const Position *pPos = getPosition();
125     if( !pPos )
126     {
127         return -1;
128     }
129     return pPos->getHeight() / itemHeight();
130 }
131
132
133 void CtrlTree::onUpdate( Subject<VarTree> &rTree )
134 {
135     // Invalidate the position when the tree is updated
136     m_lastPos = m_rTree.begin();
137
138     autoScroll();
139     m_pLastSelected = NULL;
140 }
141
142 void CtrlTree::onUpdate( Subject<VarPercent> &rPercent )
143 {
144     // Determine what is the first item to display
145     VarTree::Iterator it = m_rTree.begin();
146
147     int excessItems = m_rTree.visibleItems() - maxItems();
148
149     fprintf( stderr, "Hullo\n");
150
151     if( excessItems > 0)
152     {
153         VarPercent &rVarPos = m_rTree.getPositionVar();
154         // a simple (int)(...) causes rounding errors !
155 #ifdef _MSC_VER
156 #   define lrint (int)
157 #endif
158         it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
159     }
160     if( m_lastPos != it )
161     {
162         fprintf( stderr, "updating\n" );
163         // Redraw the control if the position has changed
164         m_lastPos = it;
165         makeImage();
166         notifyLayout();
167     }
168     else
169         fprintf( stderr, "not updating\n" );
170 }
171
172 void CtrlTree::onResize()
173 {
174 // FIXME : shouldn't be the same as the onUpdate function ... but i'm lazy
175     // Determine what is the first item to display
176     VarTree::Iterator it = m_rTree.begin();
177
178     int excessItems = m_rTree.visibleItems() - maxItems();
179
180     if( excessItems > 0)
181     {
182         VarPercent &rVarPos = m_rTree.getPositionVar();
183         // a simple (int)(...) causes rounding errors !
184 #ifdef _MSC_VER
185 #   define lrint (int)
186 #endif
187         it = m_rTree.getVisibleItem(lrint( (1.0 - rVarPos.get()) * (double)excessItems ) + 1);
188     }
189     // Redraw the control if the position has changed
190     m_lastPos = it;
191     makeImage();
192     notifyLayout();
193 #if 0
194     // Determine what is the first item to display
195     VarTree::Iterator it = m_rTree.begin();
196
197     int excessItems = m_rTree.visibleItems() - maxItems();
198
199     if( excessItems > 0)
200     {
201         /* FIXME VarPercent &rVarPos = m_rTree.getPositionVar();
202         double newVal = 1.0 - (double)m_lastPos / excessItems;
203         if( newVal >= 0 )
204         {
205             // Change the position to keep the same first displayed item
206             rVarPos.set( 1.0 - (double)m_lastPos / excessItems );
207         }
208         else
209         {
210             // We cannot keep the current first item
211             m_lastPos = excessItems;
212         }*/
213         it = m_rTree.getVisibleItem( excessItems );
214     }
215     makeImage();
216     notifyLayout();
217 #endif
218 }
219
220 void CtrlTree::onPositionChange()
221 {
222     makeImage();
223     notifyLayout();
224 }
225
226 void CtrlTree::handleEvent( EvtGeneric &rEvent )
227 {
228     if( rEvent.getAsString().find( "key:down" ) != string::npos )
229     {
230         int key = ((EvtKey&)rEvent).getKey();
231         VarTree::Iterator it;
232         bool previousWasSelected = false;
233         for( it = m_rTree.begin(); it != m_rTree.end();
234              it = m_rTree.getNextVisibleItem( it ) )
235         {
236             VarTree::Iterator next = m_rTree.getNextVisibleItem( it );
237             if( key == KEY_UP )
238             {
239                 // Scroll up one item
240                 if( ( it->parent()
241                       && it != it->parent()->begin() )
242                     || &*it != m_pLastSelected )
243                 {
244                     bool nextWasSelected = ( &*next == m_pLastSelected );
245                     it->m_selected = nextWasSelected;
246                     if( nextWasSelected )
247                     {
248                         m_pLastSelected = &*it;
249                     }
250                 }
251             }
252             else if( key == KEY_DOWN )
253             {
254                 // Scroll down one item
255                 if( ( it->parent()
256                       && next != it->parent()->end() )
257                     || &*it != m_pLastSelected )
258                 {
259                     (*it).m_selected = previousWasSelected;
260                 }
261                 if( previousWasSelected )
262                 {
263                     m_pLastSelected = &*it;
264                     previousWasSelected = false;
265                 }
266                 else
267                 {
268                     previousWasSelected = ( &*it == m_pLastSelected );
269                 }
270             }
271             else if( key == KEY_RIGHT )
272             {
273                 // Go down one level (and expand node)
274                 if( &*it == m_pLastSelected )
275                 {
276                     if( it->m_expanded )
277                     {
278                         if( it->size() )
279                         {
280                             it->m_selected = false;
281                             it->begin()->m_selected = true;
282                             m_pLastSelected = &*(it->begin());
283                         }
284                         else
285                         {
286                             m_rTree.action( &*it );
287                         }
288                     }
289                     else
290                     {
291                         it->m_expanded = true;
292                     }
293                 }
294             }
295             else if( key == KEY_LEFT )
296             {
297                 // Go up one level (and close node)
298                 if( &*it == m_pLastSelected )
299                 {
300                     if( it->m_expanded && it->size() )
301                     {
302                         it->m_expanded = false;
303                     }
304                     else
305                     {
306                         if( it->parent() && it->parent() != &m_rTree)
307                         {
308                             it->m_selected = false;
309                             m_pLastSelected = it->parent();
310                             m_pLastSelected->m_selected = true;
311                         }
312                     }
313                 }
314             }
315             else if( key == KEY_ENTER || key == KEY_SPACE )
316             {
317                 // Go up one level (and close node)
318                 if( &*it == m_pLastSelected )
319                 {
320                     m_rTree.action( &*it );
321                 }
322             }
323         }
324
325         // Redraw the control
326         makeImage();
327         notifyLayout();
328     }
329
330     else if( rEvent.getAsString().find( "mouse:left" ) != string::npos )
331     {
332         EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
333         const Position *pos = getPosition();
334         int yPos = ( rEvtMouse.getYPos() - pos->getTop() ) / itemHeight();
335         int xPos = rEvtMouse.getXPos() - pos->getLeft();
336         VarTree::Iterator it;
337
338         if( rEvent.getAsString().find( "mouse:left:down:ctrl,shift" ) !=
339             string::npos )
340         {
341             VarTree::Iterator itClicked = findItemAtPos( yPos );
342             // Flag to know if the current item must be selected
343             bool select = false;
344             for( it = m_rTree.begin(); it != m_rTree.end();
345                  it = m_rTree.getNextVisibleItem( it ) )
346             {
347                 bool nextSelect = select;
348                 if( it == itClicked || &*it == m_pLastSelected )
349                 {
350                     if( select )
351                     {
352                         nextSelect = false;
353                     }
354                     else
355                     {
356                         select = true;
357                         nextSelect = true;
358                     }
359                 }
360                 it->m_selected = (*it).m_selected || select;
361                 select = nextSelect;
362             }
363         }
364         else if( rEvent.getAsString().find( "mouse:left:down:ctrl" ) !=
365                  string::npos )
366         {
367             // Invert the selection of the item
368             it = findItemAtPos( yPos );
369             if( it != m_rTree.end() )
370             {
371                 it->m_selected = !it->m_selected;
372                 m_pLastSelected = &*it;
373             }
374         }
375         else if( rEvent.getAsString().find( "mouse:left:down:shift" ) !=
376                  string::npos )
377         {
378             VarTree::Iterator itClicked = findItemAtPos( yPos );
379             // Flag to know if the current item must be selected
380             bool select = false;
381             for( it = m_rTree.begin(); it != m_rTree.end();
382                  it = m_rTree.getNextVisibleItem( it ) )
383             {
384                 bool nextSelect = select;
385                 if( it == itClicked || &*it == m_pLastSelected )
386                 {
387                     if( select )
388                     {
389                         nextSelect = false;
390                     }
391                     else
392                     {
393                         select = true;
394                         nextSelect = true;
395                     }
396                 }
397                 it->m_selected = select;
398                 select = nextSelect;
399             }
400         }
401         else if( rEvent.getAsString().find( "mouse:left:down" ) !=
402                  string::npos )
403         {
404             // Unselect any previously selected item
405             for( it = m_rTree.begin(); it != m_rTree.end();
406                  it = m_rTree.getNextVisibleItem( it ) )
407             {
408                 it->m_selected = false;
409             }
410             // Select the new item
411             it = findItemAtPos(yPos);
412             if( it != m_rTree.end() )
413             {
414                 it->m_selected = true;
415                 m_pLastSelected = &*it;
416             }
417         }
418
419         else if( rEvent.getAsString().find( "mouse:left:dblclick" ) !=
420                  string::npos )
421         {
422             it = findItemAtPos(yPos);
423             if( it != m_rTree.end() )
424             {
425                 if( it->size() && xPos < it->depth() * itemImageWidth() )
426                 {
427                     it->m_expanded = !it->m_expanded;
428                 }
429                 else
430                 {
431                     // Execute the action associated to this item
432                     m_rTree.action( &*it );
433                 }
434             }
435         }
436
437         // Redraw the control
438         makeImage();
439         notifyLayout();
440     }
441
442     else if( rEvent.getAsString().find( "scroll" ) != string::npos )
443     {
444         int direction = ((EvtScroll&)rEvent).getDirection();
445
446         double percentage = m_rTree.getPositionVar().get();
447         double step = 2.0 / (double)m_rTree.visibleItems();
448         if( direction == EvtScroll::kUp )
449         {
450             percentage += step;
451         }
452         else
453         {
454             percentage -= step;
455         }
456         m_rTree.getPositionVar().set( percentage );
457     }
458 }
459
460 bool CtrlTree::mouseOver( int x, int y ) const
461 {
462     const Position *pPos = getPosition();
463     return ( pPos
464        ? x >= 0 && x <= pPos->getWidth() && y >= 0 && y <= pPos->getHeight()
465        : false);
466 }
467
468 void CtrlTree::draw( OSGraphics &rImage, int xDest, int yDest )
469 {
470     if( m_pImage )
471     {
472         rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
473     }
474 }
475
476 void CtrlTree::autoScroll()
477 {
478     // Find the current playing stream
479     int playIndex = 0;
480     VarTree::Iterator it;
481     for( it = m_rTree.begin(); it != m_rTree.end();
482          it = m_rTree.getNextVisibleItem( it ) )
483     {
484         if( it->m_playing ) break;
485         playIndex++;
486     }
487
488     if( it == m_rTree.end() ) return;
489
490     // Find  m_lastPos
491     int lastPosIndex = 0;
492     for( it = m_rTree.begin(); it != m_rTree.end();
493          it = m_rTree.getNextVisibleItem( it ) )
494     {
495         if( it == m_lastPos ) break;
496         lastPosIndex++;
497     }
498
499     if( it == m_rTree.end() ) return;
500
501
502     if( it != m_rTree.end()
503         && ( playIndex < lastPosIndex
504            || playIndex > lastPosIndex + maxItems() ) )
505     {
506         // Scroll to have the playing stream visible
507         VarPercent &rVarPos = m_rTree.getPositionVar();
508         rVarPos.set( 1.0 - (double)playIndex / (double)m_rTree.visibleItems() );
509     }
510     else
511     {
512         makeImage();
513         notifyLayout();
514     }
515 }
516
517 void CtrlTree::makeImage()
518 {
519     if( m_pImage )
520     {
521         delete m_pImage;
522     }
523
524     // Get the size of the control
525     const Position *pPos = getPosition();
526     if( !pPos )
527     {
528         return;
529     }
530     int width = pPos->getWidth();
531     int height = pPos->getHeight();
532
533     int i_itemHeight = itemHeight();
534
535     // Create an image
536     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
537     m_pImage = pOsFactory->createOSGraphics( width, height );
538
539     VarTree::Iterator it = m_lastPos;
540
541     if( m_pBgBitmap )
542     {
543         // Draw the background bitmap
544         ScaledBitmap bmp( getIntf(), *m_pBgBitmap, width, height );
545         m_pImage->drawBitmap( bmp, 0, 0 );
546
547         for( int yPos = 0; yPos < height; yPos += i_itemHeight )
548         {
549             if( it != m_rTree.end() )
550             {
551                 if( (*it).m_selected )
552                 {
553                     int rectHeight = __MIN( i_itemHeight, height - yPos );
554                     m_pImage->fillRect( 0, yPos, width, rectHeight,
555                                         m_selColor );
556                 }
557                 it = m_rTree.getNextVisibleItem( it );
558             }
559         }
560     }
561     else
562     {
563         // FIXME (TRYME)
564         // Fill background with background color
565         uint32_t bgColor = m_bgColor1;
566         m_pImage->fillRect( 0, 0, width, height, bgColor );
567         for( int yPos = 0; yPos < height; yPos += i_itemHeight )
568         {
569             int rectHeight = __MIN( i_itemHeight, height - yPos );
570             if( it != m_rTree.end() )
571             {
572                 uint32_t color = ( it->m_selected ? m_selColor : bgColor );
573                 m_pImage->fillRect( 0, yPos, width, rectHeight, color );
574                 it = m_rTree.getNextVisibleItem( it );
575             }
576             else
577             {
578                 m_pImage->fillRect( 0, yPos, width, rectHeight, bgColor );
579             }
580             bgColor = ( bgColor == m_bgColor1 ? m_bgColor2 : m_bgColor1 );
581         }
582     }
583 //    fprintf( stderr, "done\n");
584
585     int bitmapWidth = itemImageWidth();
586
587     int yPos = 0;
588     it = m_lastPos;
589     while( it != m_rTree.end() && yPos < height )
590     {
591         const GenericBitmap *m_pCurBitmap;
592         UString *pStr = (UString*)(it->m_cString.get());
593         uint32_t color = ( it->m_playing ? m_playColor : m_fgColor );
594         // Draw the text
595         if( pStr != NULL )
596         {
597             int depth = it->depth();
598             GenericBitmap *pText = m_rFont.drawString( *pStr, color, width - bitmapWidth * depth );
599             if( !pText )
600             {
601                 return;
602             }
603             if( it->size() )
604                 m_pCurBitmap = it->m_expanded ? m_pOpenBitmap : m_pClosedBitmap;
605             else
606                 m_pCurBitmap = m_pItemBitmap;
607
608             if( m_pCurBitmap )
609             {
610                 int yPos2 = yPos+(i_itemHeight-m_pCurBitmap->getHeight()+1)/2;
611                 m_pImage->drawBitmap( *m_pCurBitmap, 0, 0,
612                                       bitmapWidth * (depth - 1 ), yPos2,
613                                       m_pCurBitmap->getWidth(),
614                                       __MIN( m_pCurBitmap->getHeight(),
615                                              height -  yPos2), true );
616             }
617             else
618             {
619                 /* it would be nice to draw something */
620             }
621             yPos += i_itemHeight - pText->getHeight();
622             int ySrc = 0;
623             if( yPos < 0 )
624             {
625                 ySrc = - yPos;
626                 yPos = 0;
627             }
628             int lineHeight = __MIN( pText->getHeight() - ySrc, height - yPos );
629             m_pImage->drawBitmap( *pText, 0, ySrc, bitmapWidth * depth, yPos,
630                                   pText->getWidth(),
631                                   lineHeight, true );
632             yPos += (pText->getHeight() - ySrc );
633             delete pText;
634         }
635         it = m_rTree.getNextVisibleItem( it );
636     }
637 }
638
639 VarTree::Iterator CtrlTree::findItemAtPos( int pos )
640 {
641     // The first item is m_lastPos.
642     // We decrement pos as we try the other items, until pos == 0.
643     VarTree::Iterator it;
644     for( it = m_lastPos; it != m_rTree.end() && pos != 0;
645          it = m_rTree.getNextVisibleItem( it ) )
646     {
647         pos--;
648     }
649
650     return it;
651 }
652