Inside3D!
     

Tutorial: Copying the entire console buffer to clipboard

 
Post new topic   Reply to topic    Inside3d Forums Forum Index -> Programming Tutorials
View previous topic :: View next topic  
Author Message
Baker



Joined: 14 Mar 2006
Posts: 1538

PostPosted: Sun Nov 23, 2008 5:12 am    Post subject: Tutorial: Copying the entire console buffer to clipboard Reply with quote

I think Inside3d should serve as the central depot for engine code now that Quakesrc.org is dead and it is clear it won't ever be back. Here is a tutorial I made a while back @ QuakeOne.com (original thread) and I think this is needs to be here:

Adding "copy" command to Quake

Type "copy" in the console and all that text is now on your clipboard and you can paste it somewhere, instead of the various hassle methods of using condump (if you use an engine with that) or using -condebug + qconsole.log.

In console.c (adapted from FitzQuake's condump command)

Code:
/*
================
Con_Copy_f -- Baker -- adapted from Con_Dump
================
*/
void Con_Copy_f (void)
{
   char   outstring[CON_TEXTSIZE]="";
   int      l, x;
   char   *line;
   char   buffer[1024];

   // skip initial empty lines
   for (l = con_current - con_totallines + 1 ; l <= con_current ; l++)
   {
      line = con_text + (l%con_totallines)*con_linewidth;
      for (x=0 ; x<con_linewidth ; x++)
         if (line[x] != ' ')
            break;
      if (x != con_linewidth)
         break;
   }

   // write the remaining lines
   buffer[con_linewidth] = 0;
   for ( ; l <= con_current ; l++)
   {
      line = con_text + (l%con_totallines)*con_linewidth;
      strncpy (buffer, line, con_linewidth);
      for (x=con_linewidth-1 ; x>=0 ; x--)
      {
         if (buffer[x] == ' ')
            buffer[x] = 0;
         else
            break;
      }
      for (x=0; buffer[x]; x++)
         buffer[x] &= 0x7f;

      strcat(outstring, va("%s\r\n", buffer));

   }

   Sys_CopyToClipboard(outstring);
   Con_Printf ("Copied console to clipboard\n");
}


In Con_Init in console.c

Code:
Cmd_AddCommand ("copy", Con_Copy_f); // Baker - copy console to clipboard


In sys_win.c (straight out of FuhQuake)

Code:
// copies given text to clipboard
void Sys_CopyToClipboard(char *text) {
   char *clipText;
   HGLOBAL hglbCopy;

   if (!OpenClipboard(NULL))
      return;

   if (!EmptyClipboard()) {
      CloseClipboard();
      return;
   }

   if (!(hglbCopy = GlobalAlloc(GMEM_DDESHARE, strlen(text) + 1))) {
      CloseClipboard();
      return;
   }

   if (!(clipText = GlobalLock(hglbCopy))) {
      CloseClipboard();
      return;
   }

   strcpy((char *) clipText, text);
   GlobalUnlock(hglbCopy);
   SetClipboardData(CF_TEXT, hglbCopy);

   CloseClipboard();
}


In sys.h

Code:
void Sys_CopyToClipboard(char *);


The End. Compile and startup Quake and type "copy" in the console and the entire console will be copied to the clipboard.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Inside3d Forums Forum Index -> Programming Tutorials All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2004 phpBB Group