Re: [gtk-list] Valient or Valium?



>(Damned my object based thought processes :) I'm used to using object
>based languages (and gui's) like java, object pascal and c++ so it's been
>rather though designing and writing modular/serial code.

perhaps you should be using Gtk-- rather than just GTK+. Its a very
nice layer/wrapper above/around GTK+ that lets you work in a C++
paradigm rather than a C one.

>Despite the valiant efforts of the two people who responded to my earlier
>inquiries I find myself in the same state.  I am not too sure as to what
>these object binding functions actually do in specific widgets (the rdp
>nor the reference doesn't seem to have any nice explanations).  Would
>someone please explain how these functions are supposed to work and/or
>point me to a simple example program that uses them?

well, few of my programs are simple, but here is an code fragment (in C++)
that sets up a Gtk_CList in which each row has an associated "object"
(in this case, just a string). this object can be retrieved whenever
that row is selected, for example (and in my case, it gives the full
pathname of a file whose name is displayed only in part. Sorry if the
C++ makes it difficult for you to read, but it still might give you
the idea.

static void
DeleteOnDestroy (void *ptr)

{
	delete ptr;
}

void
dirpath_lister (Gtk_CList &list, const string &dirpath,
		bool (*filter)(const string &), bool with_suffix)

{
	vector<string *> *matched_files;
	vector<string *>::iterator i;
	PathScanner scanner;
	int row;

	matched_files = scanner (dirpath, filter, false, true);

	sort (matched_files->begin(), matched_files->end(), 
	      less<const string *>());

	if (matched_files == NULL) {
		return;
	}

	if (matched_files->size() == 0) {
		delete matched_files;
		error << "No files found along "
		      << dirpath
		      << endmsg;
		return;
	}

	for (row = 0, i = matched_files->begin(); 
	     i != matched_files->end(); 
	     row++, i++) {

		const gchar *rowdata[1];
		string &fullpath = *(*i);
		int start, end;

		start = fullpath.find_last_of ('/') + 1;
		
		if (with_suffix) {
			end = fullpath.length();
		} else {
			if ((end = fullpath.find_last_of ('.')) < 0) {
				end = fullpath.length();
			}
		}
		
		string foo = fullpath.substr (start, (end-start));
		rowdata[0] = foo.c_str();
		list.insert_row (row, rowdata);
		list.set_row_data_full (row, *i, DeleteOnDestroy);

   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   /* This doesn't modify `list' at all, but associates the object 
      pointed to by `*i' (an STL iterator; in this case a `string *', but thats
      irrelevant) with this row. When this row is deleted from the 
      Gtk_CList, DeleteOnDestroy will be called and passed the object
      pointer. If we don't do this, this particular use of set_row_data_full
      would be connected with a memory leak. Other situations might
      not require this, because the object wasn't supposed to be
      deleted at that time, and there was another handle to it. In those
      cases, you could just use set_row_data().
    */
          }
}

***** then, later, another part of the program can do this (a
      Gtk_Selector is one of my own widgets that HAS-A Gtk_CList, among other
      things)


void
Gtk_Selector::accept ()

{
	_gtk_string buf;
	SelectionResult result;

	if (selected_row >= 0) {
		
		/* XXX if _gtk_string wasn't used here, we could
		   avoid the copy ....
		*/

		if (list.get_text (selected_row, selected_column,&buf)) {
			result.text = new string (buf);
			result.data = list.get_row_data(selected_row);

     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     /* OK, so now result.data points to the object we associated with
        this row.
      */

			result.row = selected_row;
			result.column = selected_column;
			selection_made (this, &result);
		}

	} else {
		cancel ();
	}
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]