Beispiel #1
0
void DocActionPix::Exec(bool bInteractive)
{
	DocumentIterator it(*m_pDoc);

	if(bInteractive)
	{
		FileDialog dlg(true);
		dlg.SetTitle(_("Open picture"));

		//define file filters
		dlg.AddFilter(_("All supported image formats (*.png,*.jpg,*.gif)"), "*.png|*.jpg|*.gif");
		dlg.AddFilter(_("PNG format (*.png)"), "*.png");
		dlg.AddFilter(_("JPG format (*.jpg)"), "*.jpg");
		dlg.AddFilter(_("GIF format (*.gif)"), "*.gif");
		dlg.AddFilter(_("All files (*)"), "*");

		//set initial directory from INI (store last used)
		std::string strDefaultDir;
		std::string strDir;
		g_objIni.GetValue("Cache", "LastPictureDir", strDir, "");
		if(!strDir.empty() && 0 == access(strDir.c_str(), 00))
			strDefaultDir = strDir;
		else
			strDefaultDir = GetHomeDir();

		dlg.SetDirectory(strDefaultDir.c_str());

		if(dlg.DoModal())
		{
			const gchar *filename = dlg.GetFilename();
			strDefaultDir = dlg.GetDirectory();
			dlg.Close();

			//store last open directory
			g_objIni.SetValue("Cache", "LastPictureDir", strDefaultDir.c_str());
			g_objIni.Save();

			GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);

			PixPropertiesDlg dlg1;
			dlg1.m_nOrigWidth  = gdk_pixbuf_get_width(pixbuf);
			dlg1.m_nOrigHeight = gdk_pixbuf_get_height(pixbuf);
			dlg1.m_bUsePNG = false;
			dlg1.Create();
			if(GTK_RESPONSE_OK != dlg1.ShowModal()){
				g_object_unref (G_OBJECT (pixbuf));
				return;
			}

			GdkPixbuf *destpix = gdk_pixbuf_scale_simple(pixbuf, dlg1.m_nNewWidth, dlg1.m_nNewHeight, GDK_INTERP_BILINEAR);
			g_object_unref (G_OBJECT (pixbuf));
			pixbuf = destpix;

			UpdateTextFromScreen();

			//search from last result selection (or start)
			int cursor = -1;
			int selection = -1;
			g_text.GetSelectionBounds(cursor, selection);
			if(selection >= 0)
				cursor = selection;

			//get the iterator
			GtkTextBuffer* buffer1 = gtk_text_view_get_buffer((GtkTextView *)g_text.m_pWidget);
			GtkTextIter cursIter;
			gtk_text_buffer_get_iter_at_offset(buffer1, &cursIter, cursor);

			gtk_text_buffer_insert_pixbuf(buffer1, &cursIter, pixbuf);

			//store base name for the picture (with changed extension if required)
			std::string strName = GetBaseName(filename);
			EnsureExtension(strName, dlg1.m_bUsePNG ? ".png" : ".jpg");

			PixInfo info;
			info.bUsePNG = dlg1.m_bUsePNG;
			info.nOffset = cursor;
			info.pixbuf  = pixbuf;
			info.strName = strName;
			g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures.push_back(info);
			std::sort(g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures.begin(), g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures.end());

			m_nOffset = cursor;
			m_pixbuf  = pixbuf;
			m_bUsePNG = dlg1.m_bUsePNG;

			//increment offsets for all formatting marks
			int nSize = g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt.size();
			int i;
			for(i=0; i<nSize; i++){
				if(g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt[i].nOffset >= m_nOffset)
					g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt[i].nOffset ++;
			}

			//increment all the pictures after this one
			nSize = g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures.size();
			for(i=0; i<nSize; i++){
				if(g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures[i].nOffset > m_nOffset)
					g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures[i].nOffset ++;
			}
		}
	}
	else
	{
		PixInfo info;
		info.bUsePNG = m_bUsePNG;
		info.nOffset = m_nOffset;
		info.pixbuf  = m_pixbuf;
		g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures.push_back(info);

		//increment offsets for all formatting marks
		int nSize = g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt.size();
		int i;
		for(i=0; i<nSize; i++){
			if(g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt[i].nOffset >= m_nOffset)
				g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt[i].nOffset ++;
		}

		//increment all the pictures after this one
		nSize = g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures.size();
		for(i=0; i<nSize; i++){
			if(g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures[i].nOffset > m_nOffset)
				g_doc.GetNodeByIdx(m_nNodeIdx).m_lstPictures[i].nOffset ++;
		}

		if(m_nNodeIdx == GetSelectedNodeIdx()){
			//get the iterator
			GtkTextBuffer* buffer1 = gtk_text_view_get_buffer((GtkTextView *)g_text.m_pWidget);
			GtkTextIter cursIter;
			gtk_text_buffer_get_iter_at_offset(buffer1, &cursIter, m_nOffset);
			gtk_text_buffer_insert_pixbuf(buffer1, &cursIter, m_pixbuf);
		}
	}
}
Beispiel #2
0
void DocActionFmt::Exec(bool bInteractive)
{
	bool bExists = false;

	if(bInteractive)
	{
		m_lstFmt = g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt;

		int nCursor = 0;
		int nSelection = 0;

		g_text.GetSelectionBounds(nCursor, nSelection);
		if(nCursor != nSelection)
		{
			int nStart  = min(nCursor, nSelection);
			int nEnd    = max(nCursor, nSelection);

			m_nStartOffset = nStart;
			m_nEndOffset   = nEnd;

			bExists = g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt.IsRangeFormatted(m_nStartOffset, m_nEndOffset, m_nOperationTag);

			//start color picker if required
			GdkColor color;
			if( (!bExists || m_bCtrlPressed) &&
				(FMT_TXT_COLOR_BEGIN == m_nOperationTag || FMT_TXT_BKG_COLOR_BEGIN == m_nOperationTag))
			{
				if(m_bCtrlPressed)
				{
					if(FMT_TXT_COLOR_BEGIN == m_nOperationTag)
						color = g_rgbTextColor;
					else
						color = g_rgbTextBgColor;
				}
				else
				{
					GtkWidget *picker = gtk_color_selection_dialog_new  (_("Pick text color"));
					gtk_window_set_modal (GTK_WINDOW (picker), TRUE);
					gtk_window_set_destroy_with_parent (GTK_WINDOW (picker), TRUE);
				#ifndef _WIN32  //TOFIX API is buggy on Win32 (kills modal dialog state)
					gtk_window_set_skip_taskbar_hint (GTK_WINDOW (picker), TRUE);
				#endif
					gtk_window_set_skip_pager_hint (GTK_WINDOW (picker), TRUE);
					gtk_window_set_type_hint (GTK_WINDOW (picker), GDK_WINDOW_TYPE_HINT_DIALOG);
					gtk_window_set_transient_for(GTK_WINDOW (picker), GTK_WINDOW(window1));   //set parent
				#if GTK_CHECK_VERSION(2,4,0) //new API TOFIX set proper version
					#ifndef _WIN32  //TOFIX API is buggy on Win32 (kills modal dialog state)
					  //gtk_window_set_keep_above(GTK_WINDOW (picker), TRUE);
					#endif
				#endif
					gtk_widget_realize(picker);
					gdk_window_set_decorations(picker->window, (GdkWMDecoration)(GDK_DECOR_BORDER|GDK_DECOR_TITLE));

					GtkColorSelection *colorsel = GTK_COLOR_SELECTION (GTK_COLOR_SELECTION_DIALOG (picker)->colorsel);
					if(FMT_TXT_COLOR_BEGIN == m_nOperationTag){
						gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &g_rgbTextColor);
						gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &g_rgbTextColor);
					}
					else
					{
						gtk_color_selection_set_previous_color(GTK_COLOR_SELECTION(colorsel), &g_rgbTextBgColor);
						gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &g_rgbTextBgColor);
					}
					gtk_color_selection_set_has_palette (GTK_COLOR_SELECTION(colorsel), TRUE);

					if(GTK_RESPONSE_OK != gtk_dialog_run(GTK_DIALOG(picker))){
						gtk_widget_destroy(picker);
						return;
					}
					gtk_color_selection_get_current_color(colorsel, &color);
					gtk_widget_destroy(picker);
				}

				m_color = color;
				if(FMT_TXT_COLOR_BEGIN == m_nOperationTag)
					g_rgbTextColor = color;		//remember last used color
				else
					g_rgbTextBgColor = color;		//remember last used color
			}
		}
	}

	bExists = g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt.IsRangeFormatted(m_nStartOffset, m_nEndOffset, m_nOperationTag);
	if(bExists){
		g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt.ClearFormatRange(m_nStartOffset, m_nEndOffset, m_nOperationTag);
		RefreshTextFormat(g_doc.GetNodeByIdx(m_nNodeIdx), false, m_nStartOffset, m_nEndOffset);
	}
	else{
		g_doc.GetNodeByIdx(m_nNodeIdx).m_lstTxtFmt.AddFormatRange(m_nStartOffset, m_nEndOffset, m_nOperationTag, m_color);
		if(FMT_NONE == m_nOperationTag)
			RefreshTextFormat(g_doc.GetNodeByIdx(m_nNodeIdx), false, m_nStartOffset, m_nEndOffset);
	}

	textview_selection_changed(NULL, NULL, NULL, NULL);

	TRACE("DocActionFmt::Exec\n");
}