LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   gtk - How to pass in a parameter (https://www.linuxquestions.org/questions/programming-9/gtk-how-to-pass-in-a-parameter-377206/)

Ian D 10-26-2005 04:50 PM

gtk - How to pass in a parameter
 
I have written in C in the past, but I find C++ a teensy little bit different.

I have taken the gtk "Hello World" example and modified it a bit. instead of outputting to stdout like that example, i want to output to a file. (I can manage that).

However, I want to be able to pass in the filename as a parameter of the program. So I would run
Code:

MyProg SomeFile
and the result would be a file called SomeFile with my output in it.

How do I pick SomeFile from argv[1][0] and pass it through to the routine that picks up the button press, so that it knows what file to open.

(Yes, I intend to Open-write-close on each button press to ensure that my data is writen safely - it won't be a performance problem.

TIA

Ian

naf 10-26-2005 04:58 PM

It would help if you provide what you have gotten so far. Fortunately for needs, GTK is in C.

You should already have char **argv in your [i]main[i] function. Setup a signal with that argv[1] or whatever one it is. [i]char *[i] or const char * can be converted to gpointer (which is void * typedefed). You handler (which is a C function pointer) can dereference the pointer to the pointer of the filename.

Ian D 10-27-2005 02:22 PM

OK. Here is what I have. 3 files plus Compiler output.
BTW A smiley creeps in to the code snippets where I have a : followed by an o.

Code:

//$Id: ElTimeBody.h,

/*
 *
 * This is the .h file for the Elapsed Time module
 *
 */

#ifndef GTKMM_ELAPSEDTIME_H
#define GTKMM_ELAPSEDTIME_H

#include <gtkmm/button.h>
#include <gtkmm/window.h>

class ElapsedTime : public Gtk::Window
{

public:
  ElapsedTime();
  virtual ~ElapsedTime();
  void setFileName(char FileName) {strcpy(_FileName, FileName);}

private:
  char _FileName[300];
  time_t mtimeStartTime;

protected:
  //Signal handlers:
  virtual void on_button_clicked();

  //Member widgets:
  Gtk::Button m_button;
};


#endif // GTKMM_ELAPSEDTIME_H

Code:

//$Id: ElTimeBody.cc

/*
 *
 * This program is the Elapsed Time module.
 * When instantiated, it creates a button and places it in the Window.
 *
 * When the button is pressed, it will calculate the elapsed time since the
 * program started (in HH:MM:SS) and output that time.
 * It is supposed to output to a file, whosw name is passed in as a parameter
 * to the main program.
 *
 */

#include "ElTimeBody.h"
#include <iostream>
#include <time.h>

  time_t mtimeStartTime;

ElapsedTime::ElapsedTime()
: m_button("Press me at Significant Times")  // creates a new button with the label "Press me at Significant Times".
{
  // Sets the border width of the window.
  set_border_width(30);
  time( &mtimeStartTime );
//  strcpy(szFileName, cFilename);

  // When the button receives the "clicked" signal, it will call the
  // on_button_clicked() method. The on_button_clicked() method is defined below.
  m_button.signal_clicked().connect(sigc::mem_fun(*this, &ElapsedTime::on_button_clicked));

  // This packs the button into the Window (a container).
  add(m_button);

  // The final step is to display this newly created widget...
  m_button.show();
}

ElapsedTime::~ElapsedTime()
{
}



void ElapsedTime::on_button_clicked()
{
  time_t ltimeTimeNow;
  double ldETime;
  long llETime;
  int liSeconds, liMinutes, liMinsLeft, liHours;
  char szBuffer[20];
  FILE *list;

  time ( &ltimeTimeNow );
  ldETime = difftime(ltimeTimeNow, mtimeStartTime);
  llETime = ldETime;        // convert time to integer format (long)
  liMinsLeft = llETime / 60;        // Calculate complete minutes
  liSeconds = llETime - (liMinsLeft * 60); // Calculate seconds
  liHours = liMinsLeft / 60;        //Calculate Hours
  liMinutes = liMinsLeft - (liHours * 60);

  list = fopen(_FileName, "a");
  fprintf(list, "%02d:%02d:%02d\n", liHours, liMinutes, liSeconds);
  fclose(list);
 
}

Code:

//$Id: ElTimemain.cc
/*
 *
 * This program is the main program
 * It is supposed to accept one parameter and pass it on to the
 * Elapsed Time module, where it will use it as the File Name
 * to which to output timings.
 *
 */

#include <gtkmm/main.h>
#include "ElTimeBody.h"
#include <time.h>


int main (int argc, char *argv[])
{
  char szTemp[300];
  Gtk::Main kit(argc, argv);

//  strcpy(szTemp, &argv[1][0]);

  ElapsedTime ElapsedTime;
//  ElapsedTime.setFileName(szTemp);
  Gtk::Main::run(ElapsedTime); //Shows the window and returns when it is closed.

  return 0;
}

Code:

ElTimeBody.h: In member function �void ElapsedTime::setFileName(char)�:
ElTimeBody.h:21: error: invalid conversion from �char� to �const char*�
ElTimeBody.h:21: error:  initializing argument 2 of �char* strcpy(char*, const char*)�
ElTimeBody.cc: In member function �virtual void ElapsedTime::on_button_clicked()�:
ElTimeBody.cc:57: warning: converting to �long int� from �double�
ElTimeBody.h: In member function �void ElapsedTime::setFileName(char)�:
ElTimeBody.h:21: error: invalid conversion from �char� to �const char*�
ElTimeBody.h:21: error:  initializing argument 2 of �char* strcpy(char*, const char*)�
[


naf 10-28-2005 10:16 AM

Your error seems to be referring to:
ElTimeBody.h:21: error: invalid conversion from �char� to �const char*�
ElTimeBody.h:21: error: initializing argument 2 of �char* strcpy(char*, const char*)�

so change:
Code:

    void setFileName(char FileName) {strcpy(_FileName, FileName);}
to
Code:

    void setFileName(const char *FileName) {strcpy(_FileName, FileName);}

Ian D 10-28-2005 11:23 AM

Thanks,

I knew it had to be soemthing like that - but there seemed to be about a million combinations, only one right, and I would try the failed ones over again.

If my C wasn't so rusty, I guess I should have been able to work it out,

Anyway, my program just sings now.

Ian


All times are GMT -5. The time now is 08:10 AM.