Changeset 146
- Timestamp:
- 05/05/06 08:46:28 (2 years ago)
- Files:
-
- trunk/src/IPageView.h (modified) (2 diffs)
- trunk/src/PagePter.cxx (modified) (5 diffs)
- trunk/src/PagePter.h (modified) (3 diffs)
- trunk/src/gtk/PageView.cxx (modified) (10 diffs)
- trunk/src/gtk/PageView.h (modified) (1 diff)
- trunk/tests/DumbPageView.cxx (modified) (2 diffs)
- trunk/tests/DumbPageView.h (modified) (1 diff)
- trunk/tests/PagePterTest.cxx (modified) (1 diff)
- trunk/tests/PagePterTest.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/IPageView.h
r139 r146 44 44 PagePter *getPresenter (void) { return m_Pter; } 45 45 46 /// @brief Gets the horizontal scroll position. 47 /// 48 /// @return The position of the horizontal scroll bar. 49 virtual gdouble getHorizontalScroll (void) = 0; 50 46 51 /// 47 52 /// @brief Gets the size of the document's view. … … 58 63 /// @param height The location to save the page view's height. 59 64 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; 60 86 61 87 /// trunk/src/PagePter.cxx
r145 r146 20 20 using namespace ePDFView; 21 21 22 // Types 22 23 typedef struct 23 24 { … … 27 28 PageNotAvailableData; 28 29 30 struct _DragInfo 31 { 32 gint x; 33 gint y; 34 gdouble scrollX; 35 gdouble scrollY; 36 }; 37 29 38 // Global variables. 30 39 static gboolean g_WaitingForPage = FALSE; … … 37 46 m_Document = document; 38 47 m_Document->attach (this); 48 m_DragInfo = NULL; 39 49 m_NextPageScroll = PAGE_SCROLL_START; 40 50 m_PageView = NULL; … … 46 56 PagePter::~PagePter () 47 57 { 58 delete m_DragInfo; 48 59 } 49 60 … … 65 76 66 77 return (*m_PageView); 78 } 79 80 void 81 PagePter::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 93 void 94 PagePter::mouseButtonReleased (gint button) 95 { 96 if ( 1 == button ) 97 { 98 delete m_DragInfo; 99 m_DragInfo = NULL; 100 } 101 } 102 103 void 104 PagePter::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 } 67 111 } 68 112 trunk/src/PagePter.h
r140 r146 19 19 #define __PAGE_PTER_H__ 20 20 21 // Forward declarations. 22 typedef struct _DragInfo DragInfo; 23 21 24 namespace ePDFView 22 25 { … … 29 32 void getSize (gint *width, gint *height); 30 33 IPageView &getView (void); 34 void mouseButtonPressed (gint mouseButton, gint x, gint y); 35 void mouseButtonReleased (gint mouseButton); 36 void mouseMoved (gint x, gint y); 31 37 void notifyLoad (void); 32 38 void notifyPageChanged (gint pageNum); … … 41 47 protected: 42 48 IDocument *m_Document; 49 DragInfo *m_DragInfo; 43 50 PageScroll m_NextPageScroll; 44 51 IPageView *m_PageView; trunk/src/gtk/PageView.cxx
r144 r146 27 27 static gint PAGE_VIEW_PADDING = 12; 28 28 29 // Types.30 typedef struct31 {32 gboolean inDrag;33 gdouble startX;34 gdouble startY;35 gdouble hAdjustment;36 gdouble vAdjustment;37 GtkScrolledWindow *pageScroll;38 } DragInfo;39 40 41 29 // Forwards declarations. 42 30 static gboolean page_view_button_press_cb (GtkWidget *, GdkEventButton *, … … 44 32 static gboolean page_view_button_release_cb (GtkWidget *, GdkEventButton *, 45 33 gpointer); 46 static gboolean page_view_drag_cb (GtkWidget *, GdkEventMotion *, gpointer); 34 static gboolean page_view_mouse_motion_cb (GtkWidget *, GdkEventMotion *, 35 gpointer); 47 36 static void page_view_get_scrollbars_size (GtkWidget *widget, 48 37 gint *width, gint *height); … … 84 73 } 85 74 75 gdouble 76 PageView::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 86 83 void 87 84 PageView::getSize (gint *width, gint *height) … … 97 94 } 98 95 96 gdouble 97 PageView::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 99 104 void 100 105 PageView::setPresenter (PagePter *pter) … … 109 114 G_CALLBACK (page_view_scrolled_cb), pter); 110 115 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 | 114 120 GDK_BUTTON_RELEASE_MASK); 115 121 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 129 void 130 PageView::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)); 118 149 } 119 150 … … 151 182 } 152 183 184 void 185 PageView::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 153 194 /// 154 195 /// @brief Creates a new GdkPixbuf from a given DocumentPage. … … 181 222 { 182 223 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; 214 235 } 215 236 … … 223 244 g_assert ( NULL != data && "The data is NULL."); 224 245 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; 242 250 } 243 251 … … 246 254 /// 247 255 gboolean 248 page_view_drag_cb (GtkWidget *widget, GdkEventMotion *event, gpointer data) 256 page_view_mouse_motion_cb (GtkWidget *widget, GdkEventMotion *event, 257 gpointer data) 249 258 { 250 259 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; 294 271 } 295 272 … … 347 324 /// @brief The page view has been scrolled. 348 325 /// 349 /// This only happen ds when the user uses the mouse wheel.326 /// This only happens when the user uses the mouse wheel. 350 327 /// 351 328 static gboolean trunk/src/gtk/PageView.h
r139 r146 26 26 PageView (void); 27 27 ~PageView (void); 28 28 29 gdouble getHorizontalScroll (void); 29 30 GtkWidget *getTopWidget (void); 31 void getPagePosition (gint widgetX, gint widgetY, 32 gint *pageX, gint *pageY); 30 33 void getSize (gint *width, gint *height); 34 gdouble getVerticalScroll (void); 35 void scrollPage (gdouble scrollX, gdouble scrollY, 36 gint dx, gint dy); 31 37 void setPresenter (PagePter *pter); 32 38 void showPage (DocumentPage *page, PageScroll scroll); trunk/tests/DumbPageView.cxx
r141 r146 24 24 IPageView () 25 25 { 26 m_HorizontalScroll = 0.0; 27 m_VerticalScroll = 0.0; 26 28 } 27 29 28 30 DumbPageView::~DumbPageView () 29 31 { 32 } 33 34 gdouble 35 DumbPageView::getHorizontalScroll () 36 { 37 return m_HorizontalScroll; 30 38 } 31 39 … … 37 45 } 38 46 47 gdouble 48 DumbPageView::getVerticalScroll () 49 { 50 return m_VerticalScroll; 51 } 52 53 void 54 DumbPageView::scrollPage (gdouble scrollX, gdouble scrollY, gint dx, gint dy) 55 { 56 m_HorizontalScroll = (gdouble)dx; 57 m_VerticalScroll = (gdouble)dy; 58 } 59 39 60 void 40 61 DumbPageView::showPage (DocumentPage *page, PageScroll scroll) trunk/tests/DumbPageView.h
r141 r146 27 27 ~DumbPageView (void); 28 28 29 gdouble getHorizontalScroll (void); 29 30 void getSize (gint *width, gint *height); 31 gdouble getVerticalScroll (void); 32 void scrollPage (gdouble scrollX, gdouble scrollY, 33 gint dx, gint dy); 30 34 void showPage (DocumentPage *page, PageScroll scroll); 35 36 37 private: 38 gdouble m_HorizontalScroll; 39 gdouble m_VerticalScroll; 31 40 }; 32 41 } trunk/tests/PagePterTest.cxx
r141 r146 152 152 CPPUNIT_ASSERT_DOUBLES_EQUAL (0.4, m_Document->getZoom (), 0.0001); 153 153 } 154 155 /// 156 /// @brief Test dragging the page with the left mouse button. 157 /// 158 void 159 PagePterTest::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 28 28 CPPUNIT_TEST (pageZoomWidth); 29 29 CPPUNIT_TEST (pageZoomFit); 30 CPPUNIT_TEST (pageDrag); 30 31 CPPUNIT_TEST_SUITE_END (); 31 32 … … 36 37 void pageZoomWidth (void); 37 38 void pageZoomFit (void); 39 void pageDrag (void); 38 40 39 41 protected:
