Changeset 146

Show
Ignore:
Timestamp:
05/05/06 08:46:28 (2 years ago)
Author:
jordi
Message:

Now is the PagePter? how is doing the page scroll. This way I can add other actions when the mouse moves (i.e., hyperlinks) or when the mouse button is pressed (i.e., selecting instead of dragging.)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/IPageView.h

    r139 r146  
    4444            PagePter *getPresenter (void) { return m_Pter; } 
    4545 
     46            /// @brief Gets the horizontal scroll position. 
     47            /// 
     48            /// @return The position of the horizontal scroll bar. 
     49            virtual gdouble getHorizontalScroll (void) = 0; 
     50 
    4651            /// 
    4752            /// @brief Gets the size of the document's view. 
     
    5863            /// @param height The location to save the page view's height. 
    5964            virtual void getSize (gint *width, gint *height) = 0; 
     65 
     66            /// @brief Gets the vertical scroll position. 
     67            /// 
     68            /// @return The position of the vertical scroll bar. 
     69            virtual gdouble getVerticalScroll (void) = 0; 
     70             
     71            /// 
     72            /// @brief Sets the page scroll. 
     73            /// 
     74            /// The view should scroll the page when the presenter calls 
     75            /// this, because it's dragging the page with the mouse. 
     76            /// 
     77            /// @param scrollX The drag X start position of the scroll bar. 
     78            /// @param scrollY The drag Y start position of the scroll bar. 
     79            /// @param dx The difference in the x-axis between the drag 
     80            ///           start position and the current mouse position. 
     81            /// @param dy The difference in the y-axis between the drag 
     82            ///           start position and the current mouse position. 
     83            /// 
     84            virtual void scrollPage (gdouble scrollX, gdouble scrollY, 
     85                                     gint dx, gint dy) = 0; 
    6086 
    6187            /// 
  • trunk/src/PagePter.cxx

    r145 r146  
    2020using namespace ePDFView; 
    2121 
     22// Types 
    2223typedef struct 
    2324{ 
     
    2728PageNotAvailableData; 
    2829 
     30struct _DragInfo 
     31{ 
     32    gint x; 
     33    gint y; 
     34    gdouble scrollX; 
     35    gdouble scrollY; 
     36}; 
     37 
    2938// Global variables. 
    3039static gboolean g_WaitingForPage = FALSE; 
     
    3746    m_Document = document; 
    3847    m_Document->attach (this); 
     48    m_DragInfo = NULL; 
    3949    m_NextPageScroll = PAGE_SCROLL_START; 
    4050    m_PageView = NULL; 
     
    4656PagePter::~PagePter () 
    4757{ 
     58    delete m_DragInfo; 
    4859} 
    4960 
     
    6576 
    6677    return (*m_PageView); 
     78} 
     79 
     80void 
     81PagePter::mouseButtonPressed (gint button, gint x, gint y) 
     82{ 
     83    if ( 1 == button ) 
     84    { 
     85        m_DragInfo = new DragInfo; 
     86        m_DragInfo->x = x; 
     87        m_DragInfo->y = y; 
     88        m_DragInfo->scrollX = getView ().getHorizontalScroll (); 
     89        m_DragInfo->scrollY = getView ().getVerticalScroll (); 
     90    } 
     91} 
     92 
     93void 
     94PagePter::mouseButtonReleased (gint button) 
     95{ 
     96    if ( 1 == button ) 
     97    { 
     98        delete m_DragInfo; 
     99        m_DragInfo = NULL; 
     100    } 
     101} 
     102 
     103void 
     104PagePter::mouseMoved (gint x, gint y) 
     105{ 
     106    if ( NULL != m_DragInfo ) 
     107    { 
     108        getView ().scrollPage (m_DragInfo->scrollX, m_DragInfo->scrollY, 
     109                               x - m_DragInfo->x, y - m_DragInfo->y); 
     110    } 
    67111} 
    68112 
  • trunk/src/PagePter.h

    r140 r146  
    1919#define __PAGE_PTER_H__ 
    2020 
     21// Forward declarations. 
     22typedef struct _DragInfo DragInfo; 
     23 
    2124namespace ePDFView 
    2225{ 
     
    2932            void getSize (gint *width, gint *height); 
    3033            IPageView &getView (void); 
     34            void mouseButtonPressed (gint mouseButton, gint x, gint y); 
     35            void mouseButtonReleased (gint mouseButton); 
     36            void mouseMoved (gint x, gint y); 
    3137            void notifyLoad (void); 
    3238            void notifyPageChanged (gint pageNum); 
     
    4147        protected: 
    4248            IDocument *m_Document; 
     49            DragInfo *m_DragInfo; 
    4350            PageScroll m_NextPageScroll; 
    4451            IPageView *m_PageView; 
  • trunk/src/gtk/PageView.cxx

    r144 r146  
    2727static gint PAGE_VIEW_PADDING = 12; 
    2828 
    29 // Types. 
    30 typedef struct 
    31 { 
    32     gboolean inDrag; 
    33     gdouble startX; 
    34     gdouble startY; 
    35     gdouble hAdjustment; 
    36     gdouble vAdjustment; 
    37     GtkScrolledWindow *pageScroll; 
    38 } DragInfo; 
    39  
    40  
    4129// Forwards declarations. 
    4230static gboolean page_view_button_press_cb (GtkWidget *, GdkEventButton *, 
     
    4432static gboolean page_view_button_release_cb (GtkWidget *, GdkEventButton *,  
    4533                                             gpointer); 
    46 static gboolean page_view_drag_cb (GtkWidget *, GdkEventMotion *, gpointer); 
     34static gboolean page_view_mouse_motion_cb (GtkWidget *, GdkEventMotion *, 
     35                                           gpointer); 
    4736static void page_view_get_scrollbars_size (GtkWidget *widget,  
    4837                                           gint *width, gint *height); 
     
    8473} 
    8574 
     75gdouble 
     76PageView::getHorizontalScroll () 
     77{ 
     78    GtkAdjustment *hAdjustment = gtk_scrolled_window_get_hadjustment ( 
     79            GTK_SCROLLED_WINDOW (m_PageScroll)); 
     80    return gtk_adjustment_get_value (hAdjustment); 
     81} 
     82 
    8683void  
    8784PageView::getSize (gint *width, gint *height) 
     
    9794} 
    9895 
     96gdouble 
     97PageView::getVerticalScroll () 
     98{ 
     99    GtkAdjustment *vAdjustment = gtk_scrolled_window_get_vadjustment ( 
     100            GTK_SCROLLED_WINDOW (m_PageScroll)); 
     101    return gtk_adjustment_get_value (vAdjustment); 
     102} 
     103 
    99104void 
    100105PageView::setPresenter (PagePter *pter) 
     
    109114                      G_CALLBACK (page_view_scrolled_cb), pter); 
    110115 
    111     // And connect the motion event while 1 button  is pressed. 
    112     gtk_widget_add_events (m_EventBox, GDK_BUTTON1_MOTION_MASK |  
    113                                        GDK_BUTTON_PRESS_MASK |  
     116    // And connect the motion, button press and release events. 
     117    gtk_widget_add_events (m_EventBox, GDK_POINTER_MOTION_MASK | 
     118                                       GDK_POINTER_MOTION_HINT_MASK | 
     119                                       GDK_BUTTON_PRESS_MASK | 
    114120                                       GDK_BUTTON_RELEASE_MASK); 
    115121    g_signal_connect (G_OBJECT (m_EventBox), "button-press-event", 
    116                       G_CALLBACK (page_view_button_press_cb),  
    117                       m_PageScroll); 
     122                      G_CALLBACK (page_view_button_press_cb), this); 
     123    g_signal_connect (G_OBJECT (m_EventBox), "motion-notify-event", 
     124                      G_CALLBACK (page_view_mouse_motion_cb), this); 
     125    g_signal_connect (G_OBJECT (m_EventBox), "button-release-event", 
     126                      G_CALLBACK (page_view_button_release_cb), this); 
     127
     128 
     129void 
     130PageView::scrollPage (gdouble scrollX, gdouble scrollY, gint dx, gint dy) 
     131
     132    GtkAdjustment *hAdjustment = gtk_scrolled_window_get_hadjustment ( 
     133            GTK_SCROLLED_WINDOW (m_PageScroll)); 
     134    gdouble hAdjValue = hAdjustment->page_size * 
     135        (gdouble)dx / m_PageImage->allocation.width; 
     136    gtk_adjustment_set_value (hAdjustment, 
     137            CLAMP (scrollX - hAdjValue, 
     138                   hAdjustment->lower, 
     139                   hAdjustment->upper - hAdjustment->page_size)); 
     140     
     141    GtkAdjustment *vAdjustment = gtk_scrolled_window_get_vadjustment ( 
     142            GTK_SCROLLED_WINDOW (m_PageScroll)); 
     143    gdouble vAdjValue = vAdjustment->page_size * 
     144        (gdouble)dy / m_PageImage->allocation.height; 
     145    gtk_adjustment_set_value (vAdjustment, 
     146            CLAMP (scrollY - vAdjValue, 
     147                   vAdjustment->lower, 
     148                   vAdjustment->upper - vAdjustment->page_size)); 
    118149} 
    119150 
     
    151182} 
    152183 
     184void 
     185PageView::getPagePosition (gint widgetX, gint widgetY, gint *pageX, gint *pageY) 
     186{ 
     187    g_assert ( NULL != pageX && "Tried to save the page's X to NULL."); 
     188    g_assert ( NULL != pageY && "Tried to save the page's Y to NULL.");     
     189 
     190    *pageX = widgetX - PAGE_VIEW_PADDING + (gint)getHorizontalScroll (); 
     191    *pageY = widgetY - PAGE_VIEW_PADDING + (gint)getVerticalScroll (); 
     192} 
     193 
    153194/// 
    154195/// @brief Creates a new GdkPixbuf from a given DocumentPage. 
     
    181222{ 
    182223    g_assert ( NULL != data && "The data is NULL."); 
    183  
    184     // I'm only interested on the left mouse button. 
    185     if ( 1 == event->button ) 
    186     { 
    187         // This button causes the page drag to start. 
    188         DragInfo *info = new DragInfo; 
    189         info->inDrag = FALSE; 
    190         info->startX = event->x_root; 
    191         info->startY = event->y_root; 
    192         info->pageScroll = GTK_SCROLLED_WINDOW (data); 
    193          
    194         // The current value for the vertical and horizontal adjustments 
    195         // are required to know the next value when dragging. 
    196         GtkAdjustment *hAdjustment =  
    197             gtk_scrolled_window_get_hadjustment (info->pageScroll); 
    198         info->hAdjustment = gtk_adjustment_get_value (hAdjustment);         
    199         GtkAdjustment *vAdjustment =  
    200             gtk_scrolled_window_get_vadjustment (info->pageScroll); 
    201         info->vAdjustment = gtk_adjustment_get_value (vAdjustment); 
    202  
    203         // Connect the motion and button release callbacks. 
    204         g_signal_connect (G_OBJECT (widget), "motion-notify-event", 
    205                           G_CALLBACK (page_view_drag_cb), 
    206                           info); 
    207         g_signal_connect (G_OBJECT (widget), "button-release-event", 
    208                           G_CALLBACK (page_view_button_release_cb), 
    209                           info); 
    210         return TRUE; 
    211     } 
    212      
    213     return FALSE; 
     224    PageView *view = (PageView *)data; 
     225 
     226    gint event_x; 
     227    gint event_y; 
     228    gtk_widget_get_pointer (view->getTopWidget (), &event_x, &event_y); 
     229    gint x; 
     230    gint y; 
     231    view->getPagePosition (event_x, event_y, &x, &y); 
     232    view->getPresenter ()->mouseButtonPressed (event->button, x, y); 
     233 
     234    return TRUE; 
    214235} 
    215236 
     
    223244    g_assert ( NULL != data && "The data is NULL."); 
    224245 
    225     // Only interested on the left button. 
    226     if ( 1 == event->button ) 
    227     { 
    228         // On this event I remove the event callbacks and remove the drag 
    229         // data. 
    230         g_signal_handlers_disconnect_by_func (G_OBJECT (widget),  
    231                 (gpointer)page_view_button_release_cb, data); 
    232         g_signal_handlers_disconnect_by_func (G_OBJECT (widget),  
    233                 (gpointer)page_view_drag_cb, data); 
    234          
    235         DragInfo *info = (DragInfo *)data; 
    236         gtk_widget_grab_focus (GTK_WIDGET (info->pageScroll)); 
    237         delete info; 
    238         return TRUE; 
    239     } 
    240      
    241     return FALSE; 
     246    PageView *view = (PageView *)data; 
     247    view->getPresenter ()->mouseButtonReleased (event->button); 
     248     
     249    return TRUE; 
    242250} 
    243251 
     
    246254/// 
    247255gboolean 
    248 page_view_drag_cb (GtkWidget *widget, GdkEventMotion *event, gpointer data) 
     256page_view_mouse_motion_cb (GtkWidget *widget, GdkEventMotion *event, 
     257                           gpointer data) 
    249258{ 
    250259    g_assert ( NULL != data && "The data is NULL."); 
    251  
    252     DragInfo *info = (DragInfo *)data; 
    253     if ( !info->inDrag ) 
    254     { 
    255         // Check if we should start the drag. 
    256         info->inDrag =  
    257             gtk_drag_check_threshold (widget, 
    258                                       (int)info->startX, (int)info->startY, 
    259                                       (int)event->x_root, (int)event->y_root); 
    260     } 
    261  
    262     if ( info->inDrag) 
    263     { 
    264         // Once the drag is started, check the difference of 
    265         // position. 
    266         gdouble dx = event->x_root - info->startX; 
    267         gdouble dy = event->y_root - info->startY; 
    268          
    269         GtkAdjustment *hadjustment =  
    270             gtk_scrolled_window_get_hadjustment (info->pageScroll); 
    271         GtkAdjustment *vadjustment =  
    272             gtk_scrolled_window_get_vadjustment (info->pageScroll); 
    273  
    274         // Calculate the difference for the adjustments. 
    275         gdouble hadjValue = hadjustment->page_size *  
    276             (gdouble)dx / widget->allocation.width; 
    277         gdouble vadjValue = vadjustment->page_size * 
    278             (gdouble)dy / widget->allocation.height; 
    279  
    280         // And set them, taking into account not to go beyound the 
    281         // limits. 
    282         gtk_adjustment_set_value (hadjustment,  
    283                 CLAMP (info->hAdjustment - hadjValue, 
    284                        hadjustment->lower, 
    285                        hadjustment->upper - hadjustment->page_size)); 
    286         gtk_adjustment_set_value (vadjustment,  
    287                 CLAMP (info->vAdjustment - vadjValue, 
    288                        vadjustment->lower, 
    289                        vadjustment->upper - vadjustment->page_size)); 
    290         return TRUE; 
    291     } 
    292      
    293     return FALSE; 
     260    PageView *view = (PageView *)data; 
     261     
     262    gint event_x; 
     263    gint event_y; 
     264    gtk_widget_get_pointer (view->getTopWidget (), &event_x, &event_y); 
     265    gint x; 
     266    gint y; 
     267    view->getPagePosition (event_x, event_y, &x, &y); 
     268    view->getPresenter ()->mouseMoved (x, y); 
     269 
     270    return TRUE; 
    294271} 
    295272 
     
    347324/// @brief The page view has been scrolled. 
    348325/// 
    349 /// This only happends when the user uses the mouse wheel. 
     326/// This only happens when the user uses the mouse wheel. 
    350327/// 
    351328static gboolean 
  • trunk/src/gtk/PageView.h

    r139 r146  
    2626            PageView (void); 
    2727            ~PageView (void); 
    28              
     28            
     29            gdouble getHorizontalScroll (void); 
    2930            GtkWidget *getTopWidget (void); 
     31            void getPagePosition (gint widgetX, gint widgetY, 
     32                                  gint *pageX, gint *pageY); 
    3033            void getSize (gint *width, gint *height); 
     34            gdouble getVerticalScroll (void); 
     35            void scrollPage (gdouble scrollX, gdouble scrollY, 
     36                             gint dx, gint dy); 
    3137            void setPresenter (PagePter *pter); 
    3238            void showPage (DocumentPage *page, PageScroll scroll); 
  • trunk/tests/DumbPageView.cxx

    r141 r146  
    2424    IPageView () 
    2525{ 
     26    m_HorizontalScroll = 0.0; 
     27    m_VerticalScroll = 0.0; 
    2628} 
    2729 
    2830DumbPageView::~DumbPageView () 
    2931{ 
     32} 
     33 
     34gdouble 
     35DumbPageView::getHorizontalScroll () 
     36{ 
     37    return m_HorizontalScroll; 
    3038} 
    3139 
     
    3745} 
    3846 
     47gdouble 
     48DumbPageView::getVerticalScroll () 
     49{ 
     50    return m_VerticalScroll; 
     51} 
     52 
     53void 
     54DumbPageView::scrollPage (gdouble scrollX, gdouble scrollY, gint dx, gint dy) 
     55{ 
     56    m_HorizontalScroll = (gdouble)dx; 
     57    m_VerticalScroll = (gdouble)dy; 
     58} 
     59 
    3960void 
    4061DumbPageView::showPage (DocumentPage *page, PageScroll scroll) 
  • trunk/tests/DumbPageView.h

    r141 r146  
    2727            ~DumbPageView (void); 
    2828 
     29            gdouble getHorizontalScroll (void); 
    2930            void getSize (gint *width, gint *height); 
     31            gdouble getVerticalScroll (void); 
     32            void scrollPage (gdouble scrollX, gdouble scrollY, 
     33                             gint dx, gint dy); 
    3034            void showPage (DocumentPage *page, PageScroll scroll); 
     35 
     36             
     37        private: 
     38            gdouble m_HorizontalScroll; 
     39            gdouble m_VerticalScroll; 
    3140    }; 
    3241} 
  • trunk/tests/PagePterTest.cxx

    r141 r146  
    152152    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.4, m_Document->getZoom (), 0.0001); 
    153153} 
     154 
     155/// 
     156/// @brief Test dragging the page with the left mouse button. 
     157/// 
     158void 
     159PagePterTest::pageDrag () 
     160{ 
     161    // Moving the mouse without pressing the first mouse button, doesn't 
     162    // change the current scrolling. 
     163    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getHorizontalScroll (), 
     164                                  0.0001); 
     165    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getVerticalScroll (), 
     166                                  0.0001); 
     167    m_PagePter->mouseMoved (10, 10); 
     168    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getHorizontalScroll (), 
     169                                  0.0001); 
     170    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getVerticalScroll (), 
     171                                  0.0001); 
     172    m_PagePter->mouseMoved (15, 20); 
     173    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getHorizontalScroll (), 
     174                                  0.0001); 
     175    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getVerticalScroll (), 
     176                                  0.0001); 
     177 
     178    // Now press the first mouse button and move a little the pointer. 
     179    // The presenter should tell the view the difference between the position 
     180    // where the drag started and the current position when the mouse moves. 
     181    m_PagePter->mouseButtonPressed (1, 15, 20); 
     182    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getHorizontalScroll (), 
     183                                  0.0001); 
     184    CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getVerticalScroll (), 
     185                                  0.0001); 
     186    m_PagePter->mouseMoved (20, 21); 
     187    CPPUNIT_ASSERT_DOUBLES_EQUAL (5.0, m_PageView->getHorizontalScroll (), 
     188                                  0.0001); 
     189    CPPUNIT_ASSERT_DOUBLES_EQUAL (1.0, m_PageView->getVerticalScroll (), 
     190                                  0.0001); 
     191    m_PagePter->mouseMoved (16, 26); 
     192    CPPUNIT_ASSERT_DOUBLES_EQUAL (1.0, m_PageView->getHorizontalScroll (), 
     193                                  0.0001); 
     194    CPPUNIT_ASSERT_DOUBLES_EQUAL (6.0, m_PageView->getVerticalScroll (), 
     195                                  0.0001); 
     196    m_PagePter->mouseMoved (10, 13); 
     197    CPPUNIT_ASSERT_DOUBLES_EQUAL (-5.0, m_PageView->getHorizontalScroll (), 
     198                                  0.0001); 
     199    CPPUNIT_ASSERT_DOUBLES_EQUAL (-7.0, m_PageView->getVerticalScroll (), 
     200                                  0.0001); 
     201 
     202    // Releasing the first mouse button stops the drag and doesn't change 
     203    // the scroll. 
     204    m_PagePter->mouseButtonReleased (1); 
     205    CPPUNIT_ASSERT_DOUBLES_EQUAL (-5.0, m_PageView->getHorizontalScroll (),  
     206                                  0.0001); 
     207    CPPUNIT_ASSERT_DOUBLES_EQUAL (-7.0, m_PageView->getVerticalScroll (), 
     208                                  0.0001); 
     209    m_PagePter->mouseMoved (20, 21); 
     210    CPPUNIT_ASSERT_DOUBLES_EQUAL (-5.0, m_PageView->getHorizontalScroll (), 
     211                                  0.0001); 
     212    CPPUNIT_ASSERT_DOUBLES_EQUAL (-7.0, m_PageView->getVerticalScroll (), 
     213                                  0.0001); 
     214} 
  • trunk/tests/PagePterTest.h

    r141 r146  
    2828        CPPUNIT_TEST (pageZoomWidth); 
    2929        CPPUNIT_TEST (pageZoomFit); 
     30        CPPUNIT_TEST (pageDrag); 
    3031        CPPUNIT_TEST_SUITE_END (); 
    3132 
     
    3637            void pageZoomWidth (void); 
    3738            void pageZoomFit (void); 
     39            void pageDrag (void); 
    3840 
    3941        protected: