lokdocview: Add support for editing documents

Change-Id: I8637d99e6fa59129af207e667bcdf03dc212efeb
This commit is contained in:
Pranav Kant
2015-06-04 03:32:18 +05:30
committed by Miklos Vajna
parent cc78267f27
commit 82a208a08f
3 changed files with 133 additions and 78 deletions

View File

@@ -821,7 +821,7 @@ void LOKDocView_Impl::renderDocument(GdkRectangle* pPartial)
if (bPaint) if (bPaint)
{ {
g_info("gettile: (%d %d)", nRow, nColumn); // g_info("gettile: (%d %d)", nRow, nColumn);
Tile& currentTile = m_pTileBuffer->tile_buffer_get_tile(nRow, nColumn); Tile& currentTile = m_pTileBuffer->tile_buffer_get_tile(nRow, nColumn);
GdkPixbuf* pPixBuf = currentTile.tile_get_buffer(); GdkPixbuf* pPixBuf = currentTile.tile_get_buffer();
@@ -934,17 +934,50 @@ gboolean LOKDocView_Impl::callbackImpl(CallbackData* pCallback)
if (pCallback->m_aPayload != "EMPTY") if (pCallback->m_aPayload != "EMPTY")
{ {
GdkRectangle aRectangle = LOKDocView_Impl::payloadToRectangle(pCallback->m_aPayload.c_str()); GdkRectangle aRectangle = LOKDocView_Impl::payloadToRectangle(pCallback->m_aPayload.c_str());
renderDocument(&aRectangle); GdkRectangle aRectanglePixels;
aRectanglePixels.x = twipToPixel(aRectangle.x);
aRectanglePixels.y = twipToPixel(aRectangle.y);
aRectanglePixels.width = twipToPixel(aRectangle.width);
aRectanglePixels.height = twipToPixel(aRectangle.height);
int rowStart = aRectanglePixels.y / nTileSizePixels;
int colStart = aRectanglePixels.x / nTileSizePixels;
int rowEnd = (aRectanglePixels.y + aRectanglePixels.height + nTileSizePixels) / nTileSizePixels;
int colEnd = (aRectanglePixels.x + aRectanglePixels.width + nTileSizePixels) / nTileSizePixels;
int i,j;
for (i = rowStart; i < rowEnd; i++) {
for (j = colStart; j < colEnd; j++) {
m_pTileBuffer->tile_buffer_set_invalid(i, j);
}
}
renderDocument(0);
} }
else else
{
m_pTileBuffer->tile_buffer_reset_all_tiles();
renderDocument(0); renderDocument(0);
}
} }
break; break;
case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR: case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR:
{ {
m_aVisibleCursor = LOKDocView_Impl::payloadToRectangle(pCallback->m_aPayload.c_str()); m_aVisibleCursor = LOKDocView_Impl::payloadToRectangle(pCallback->m_aPayload.c_str());
m_bCursorOverlayVisible = true; m_bCursorOverlayVisible = true;
gtk_widget_queue_draw(GTK_WIDGET(m_pDrawingArea)); GdkRectangle aRectanglePixels;
aRectanglePixels.x = twipToPixel(m_aVisibleCursor.x);
aRectanglePixels.y = twipToPixel(m_aVisibleCursor.y);
aRectanglePixels.width = twipToPixel(m_aVisibleCursor.width);
aRectanglePixels.height = twipToPixel(m_aVisibleCursor.height);
int rowStart = aRectanglePixels.y / nTileSizePixels;
int colStart = aRectanglePixels.x / nTileSizePixels;
int rowEnd = (aRectanglePixels.y + aRectanglePixels.height + nTileSizePixels) / nTileSizePixels;
int colEnd = (aRectanglePixels.x + aRectanglePixels.width + nTileSizePixels) / nTileSizePixels;
int i,j;
for (i = rowStart; i < rowEnd; i++) {
for (j = colStart; j < colEnd; j++) {
m_pTileBuffer->tile_buffer_set_invalid(i, j);
}
}
renderDocument(0);
} }
break; break;
case LOK_CALLBACK_TEXT_SELECTION: case LOK_CALLBACK_TEXT_SELECTION:
@@ -961,7 +994,6 @@ gboolean LOKDocView_Impl::callbackImpl(CallbackData* pCallback)
} }
else else
memset(&m_aHandleMiddleRect, 0, sizeof(m_aHandleMiddleRect)); memset(&m_aHandleMiddleRect, 0, sizeof(m_aHandleMiddleRect));
gtk_widget_queue_draw(GTK_WIDGET(m_pDrawingArea));
} }
break; break;
case LOK_CALLBACK_TEXT_SELECTION_START: case LOK_CALLBACK_TEXT_SELECTION_START:
@@ -1144,6 +1176,16 @@ static void lok_docview_init( GTypeInstance* pInstance, gpointer )
g_signal_connect(GTK_OBJECT(pDocView->m_pImpl->m_pDrawingArea), g_signal_connect(GTK_OBJECT(pDocView->m_pImpl->m_pDrawingArea),
"expose-event", "expose-event",
GTK_SIGNAL_FUNC(LOKDocView_Impl::on_exposed), pDocView); GTK_SIGNAL_FUNC(LOKDocView_Impl::on_exposed), pDocView);
g_signal_connect(GTK_OBJECT(pDocView->m_pImpl->m_pDrawingArea),
"expose-event",
GTK_SIGNAL_FUNC(LOKDocView_Impl::renderOverlay), pDocView);
gtk_widget_add_events(pDocView->m_pImpl->m_pDrawingArea,
GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
| GDK_BUTTON_MOTION_MASK);
g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea), "button-press-event", G_CALLBACK(LOKDocView_Impl::signalButton), pDocView);
g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea), "button-release-event", G_CALLBACK(LOKDocView_Impl::signalButton), pDocView);
g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea), "motion-notify-event", G_CALLBACK(LOKDocView_Impl::signalMotion), pDocView);
gtk_signal_connect(GTK_OBJECT(pDocView), "destroy", GTK_SIGNAL_FUNC(LOKDocView_Impl::destroy), 0); gtk_signal_connect(GTK_OBJECT(pDocView), "destroy", GTK_SIGNAL_FUNC(LOKDocView_Impl::destroy), 0);
} }

View File

@@ -23,69 +23,82 @@ static float twipToPixel(float fInput, float zoom)
GdkPixbuf* Tile::tile_get_buffer() GdkPixbuf* Tile::tile_get_buffer()
{ {
return m_pBuffer; return m_pBuffer;
} }
void Tile::tile_release() void Tile::tile_release()
{ {
gdk_pixbuf_unref(m_pBuffer); g_object_unref (m_pBuffer);
m_pBuffer = NULL; m_pBuffer = NULL;
} }
void TileBuffer::tile_buffer_set_zoom(float newZoomFactor, int rows, int columns) void TileBuffer::tile_buffer_set_zoom(float newZoomFactor, int rows, int columns)
{ {
m_fZoomFactor = newZoomFactor; m_fZoomFactor = newZoomFactor;
tile_buffer_reset_all_tiles(); tile_buffer_reset_all_tiles();
// set new buffer width and height // set new buffer width and height
m_nWidth = columns; m_nWidth = columns;
m_nHeight = rows; m_nHeight = rows;
} }
void TileBuffer::tile_buffer_reset_all_tiles() void TileBuffer::tile_buffer_reset_all_tiles()
{ {
std::map<int, Tile>::iterator it = m_mTiles.begin(); std::map<int, Tile>::iterator it = m_mTiles.begin();
for (; it != m_mTiles.end(); it++) for (; it != m_mTiles.end(); it++)
{ {
it->second.tile_release(); it->second.tile_release();
} }
m_mTiles.clear(); m_mTiles.clear();
}
void TileBuffer::tile_buffer_set_invalid(int x, int y)
{
int index = x * m_nWidth + y;
g_info("setting invalid : %d %d",x, y);
if (m_mTiles.find(index) != m_mTiles.end())
{
m_mTiles[index].valid = 0;
m_mTiles[index].tile_release();
m_mTiles.erase(index);
}
} }
Tile& TileBuffer::tile_buffer_get_tile(int x, int y) Tile& TileBuffer::tile_buffer_get_tile(int x, int y)
{ {
int index = x * m_nWidth + y; int index = x * m_nWidth + y;
if(m_mTiles.find(index) == m_mTiles.end()) if(m_mTiles.find(index) == m_mTiles.end() || !m_mTiles[index].valid)
{ {
GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, m_nTileSize, m_nTileSize);
if (!pPixBuf){
g_info ("error allocating memory to pixbuf");
}
unsigned char* pBuffer = gdk_pixbuf_get_pixels(pPixBuf);
GdkRectangle aTileRectangle;
aTileRectangle.x = pixelToTwip(m_nTileSize, m_fZoomFactor) * y;
aTileRectangle.y = pixelToTwip(m_nTileSize, m_fZoomFactor) * x;
g_info ("rendering (%d %d)", x, y); GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, m_nTileSize, m_nTileSize);
m_pLOKDocument->pClass->paintTile(m_pLOKDocument, if (!pPixBuf){
// Buffer and its size, depends on the position only. g_info ("error allocating memory to pixbuf");
pBuffer, }
m_nTileSize, m_nTileSize, unsigned char* pBuffer = gdk_pixbuf_get_pixels(pPixBuf);
// Position of the tile. GdkRectangle aTileRectangle;
aTileRectangle.x, aTileRectangle.y, aTileRectangle.x = pixelToTwip(m_nTileSize, m_fZoomFactor) * y;
// Size of the tile, depends on the zoom factor and the tile position only. aTileRectangle.y = pixelToTwip(m_nTileSize, m_fZoomFactor) * x;
pixelToTwip(m_nTileSize, m_fZoomFactor), pixelToTwip(m_nTileSize, m_fZoomFactor));
//create a mapping for it g_info ("rendering (%d %d)", x, y);
m_mTiles[index].tile_set_pixbuf(pPixBuf); m_pLOKDocument->pClass->paintTile(m_pLOKDocument,
m_mTiles[index].valid = 1; // Buffer and its size, depends on the position only.
} pBuffer,
m_nTileSize, m_nTileSize,
// Position of the tile.
aTileRectangle.x, aTileRectangle.y,
// Size of the tile, depends on the zoom factor and the tile position only.
pixelToTwip(m_nTileSize, m_fZoomFactor), pixelToTwip(m_nTileSize, m_fZoomFactor));
return m_mTiles[index]; //create a mapping for it
m_mTiles[index].tile_set_pixbuf(pPixBuf);
m_mTiles[index].valid = 1;
}
return m_mTiles[index];
} }
void Tile::tile_set_pixbuf(GdkPixbuf *buffer) void Tile::tile_set_pixbuf(GdkPixbuf *buffer)
{ {
m_pBuffer = buffer; m_pBuffer = buffer;
} }

View File

@@ -25,18 +25,17 @@
*/ */
class Tile class Tile
{ {
public: public:
Tile() : valid(0) {} Tile() : valid(0) {}
~Tile() { ~Tile() {
tile_release(); }
}
GdkPixbuf* tile_get_buffer(); GdkPixbuf* tile_get_buffer();
void tile_release(); void tile_release();
void tile_set_pixbuf(GdkPixbuf*); void tile_set_pixbuf(GdkPixbuf*);
bool valid; bool valid;
private: private:
GdkPixbuf *m_pBuffer; GdkPixbuf *m_pBuffer;
}; };
/* /*
@@ -45,32 +44,33 @@ private:
*/ */
class TileBuffer class TileBuffer
{ {
public: public:
TileBuffer(LibreOfficeKitDocument *document, TileBuffer(LibreOfficeKitDocument *document,
int tileSize, int tileSize,
int rows, int rows,
int columns) int columns)
: m_pLOKDocument(document) : m_pLOKDocument(document)
, m_nTileSize(tileSize) , m_nTileSize(tileSize)
, m_fZoomFactor(1) , m_fZoomFactor(1)
, m_nWidth(columns) , m_nWidth(columns)
, m_nHeight(rows) , m_nHeight(rows)
{ } { }
~TileBuffer() {} ~TileBuffer() {}
void tile_buffer_set_zoom(float zoomFactor, int rows, int columns); void tile_buffer_set_zoom(float zoomFactor, int rows, int columns);
Tile& tile_buffer_get_tile(int x, int y); Tile& tile_buffer_get_tile(int x, int y);
void tile_buffer_update(); void tile_buffer_update();
void tile_buffer_reset_all_tiles(); void tile_buffer_reset_all_tiles();
private: void tile_buffer_set_invalid(int x, int y);
LibreOfficeKitDocument *m_pLOKDocument; private:
int m_nTileSize; LibreOfficeKitDocument *m_pLOKDocument;
float m_fZoomFactor; int m_nTileSize;
std::map<int, Tile> m_mTiles; float m_fZoomFactor;
//TODO: Also set width and height when document size changes std::map<int, Tile> m_mTiles;
int m_nWidth; //TODO: Also set width and height when document size changes
int m_nHeight; int m_nWidth;
int m_nHeight;
}; };
#endif // INCLUDED_TILEBUFFER_HXX #endif // INCLUDED_TILEBUFFER_HXX