File: edcompo.cc

package info (click to toggle)
vdk2 2.0.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,616 kB
  • ctags: 4,174
  • sloc: cpp: 25,357; sh: 8,955; ansic: 5,275; makefile: 577; perl: 113
file content (286 lines) | stat: -rw-r--r-- 8,168 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * ===========================
 * VDK Visual Develeopment Kit
 * Version 2.0.0
 * February 2001
 * ===========================
 *
 * Copyright (C) 1998, Mario Motta
 * Developed by Mario Motta <mmotta@guest.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "edcompo.h"
#include "testvdk.h"
#include <cstring>
static char buff[512];
extern char* togglech_xpm[] ;
extern char* mini_ofolder_xpm[];
static char* get_extension(char* s);
static char* ToggledSource(const char* file);
///////////////////////////////////////////////////
DEFINE_SIGNAL_LIST(EditorComponent,VDKBox);
DEFINE_SIGNAL_MAP(EditorComponent,VDKBox) 
  ON_SIGNAL(tbar,clicked_signal,OnToolbarClicked)
END_SIGNAL_MAP
///////////////////////////////////////////////////
/*
  response methods
*/
bool
EditorComponent::OnToolbarClicked(VDKObject* sender)
{
  TestvdkForm* ownerform = NULL;
  VDKToolbar* bar = dynamic_cast<VDKToolbar*>(sender);
  int ndx;
  if(bar)
    {
      int btn = bar->ButtonPressed;
      switch(btn)
	{
	case 0: // open test related source
	  if ( (ownerform = dynamic_cast<TestvdkForm*>(Owner())) )
	    ownerform->OnopenMenuActivate(NULL);
	  break;
	case 1: // toggle source<->header
	  ndx = nbook->ActivePage;
	  if( (ndx >= 0) && (ndx < nbook->Pages.size()) )
	    {
	      char* toggledname = ToggledSource(nbook->Pages[ndx]->TabLabel->Caption);
	      if(toggledname)
		{
		  AddPage(toggledname);
		  delete [] toggledname;
		}
	    }
	  break;
	}
    }
  return true;
}

/*
  constructor
*/
EditorComponent::EditorComponent(VDKForm* owner):  VDKBox(owner)
{
  // load tokens for editor word completion
  sprintf(buff,"tokens.db");
  tokenlist = VDKEditor::LoadTokens(buff);
}
/*
  setup interface
 */
void    
EditorComponent::Setup() 
{
  tbar = new VDKToolbar(Owner());
  tbar->AddButton(mini_ofolder_xpm,"Select a test, then open related source"); 
  tbar->AddButton(togglech_xpm,"toggle source <-> header"); 
  tbar->Relief = GTK_RELIEF_NONE;

  nbook = new VDKNotebook(Owner());
  nbook->Scrollable = true;
  Add(tbar,l_justify,false,false,5);
  Add(new VDKSeparator(Owner()),l_justify,false,false,2);
  Add(nbook,5);
}

/*
  add a new page to notebook,
  if a page containing <filename>
  already exists, activate it.
 */
static char file_selection[256] = {'\0'};
void 
EditorComponent::AddPage(const char* filename, const char* member)
{
  if(filename)
    {
      // check if an editor oage on that file is already opened
      int ndx = HasPage(filename);
      // makes a new editor page
      if(ndx < 0)
	{
	  std::strcpy(file_selection, filename);
	  VDKEditor *editor = new VDKEditor(Owner());
	  // editor->Font = new VDKFont(Owner(),"courier Medium 12");
	  // install token list for word completion
	  if(tokenlist)
	    editor->SetTokens(tokenlist);
	  // NOTE:
	  // due to a not yet discovered bug isn't possible
	  // connect here vdkeditor with a realize signal
	  // using dynamic tables (aka SignalConnect())
	  // so we use a lower level gtk+ way.
	  gtk_signal_connect(GTK_OBJECT(editor->WrappedWidget()),"realize",
			     GTK_SIGNAL_FUNC(EditorComponent::OnVDKEditorRealize), 
			     (gpointer) editor); // pass himself here
	  // add a page to notebook
	  nbook->AddPage(editor,filename);
	  // activate this (last) page
	  nbook->ActivePage = (nbook->Pages.size()-1);
	}
      else
	// activates page containing <filename>
	nbook->ActivePage = (ndx);
      if(member)
	ScrollTo(member);
    }
} 
/*
  search into notebook page list
  to see it owns  an editor with <filename>
  Returns ordinal position or -1 if not found.
 */
int EditorComponent::HasPage(const char* filename)
{
  PageListIterator li(nbook->Pages);
  int ndx = 0;
  for(;li;li++,ndx++)
    {
      const char* fn = li.current()->TabLabel->Caption;
      if(!std::strcmp(fn,filename))
	return ndx;
    }
  return -1;
}

/*
  makes a text widget to load a source file into.
  - we can't trap "realize" signal in a safe way using
    dynamics tables on this widget.
  - a possible work around is to use a lower call to 
    gtk_signal_connect() and connect with a static function
    that uses a static buffer on which file selection is stored
    (inelegant but works).
 */
static char* defaultFont = "courier 11";
static char* defaultFontBold = "courier bold 11";
static char* defaultFontItalic = "courier oblique 11";

void
EditorComponent::OnVDKEditorRealize(GtkWidget* , gpointer gp)
{

  VDKEditor* text = reinterpret_cast<VDKEditor*>(gp);
  g_return_if_fail(text != NULL);
  text->Font = new VDKFont(text->Owner(),defaultFont);
  text->NormalBackground = clIvory;
  // text->Foreground = clNavyBlue;
  gtk_source_view_set_auto_indent(GTK_SOURCE_VIEW(text->WrappedWidget()),true);

  // makes some nice colors and fonts
  VDKColor* firebrick = new VDKColor(text,"firebrick");
  VDKColor *navyblue = new VDKColor(text,"navy blue");
  VDKFont* bold = new VDKFont(text,defaultFontBold);
  VDKFont* italic = new VDKFont(text,defaultFontItalic);
  VDKColor* forest_green = new VDKColor(text,"forest green");
  VDKColor* blue = new VDKColor(text,"blue");
  // install syntax and patterns table
  text->InstallSyntaxTable (
			    firebrick,bold, // keywords 
			    firebrick,NULL, // gtk+ names
			    forest_green,NULL, // macros
			    forest_green,bold, // preprocessor directives
			    blue,NULL, // constants
			    navyblue,italic ); // remarks
  if(text && *file_selection)
      text->LoadFromFile(file_selection);

}
/*
  scrolls to member into notebook
  active page
 */
void 
EditorComponent::ScrollTo(const char* member)
{
  int ndx = nbook->ActivePage;
  if(ndx < 0)
    return;
  // downcasts from tabpage child to editor
  VDKEditor* editor = dynamic_cast<VDKEditor*>(nbook->Pages[ndx]->Child());
  if(editor)
    {
      GtkTextBuffer* buffer = GTK_TEXT_BUFFER(editor->Buffer());
      GtkTextIter start;
      GtkTextIter match_start;
      GtkTextIter match_end;
      bool found = false;
      int start_offset = 0;
      // int end_offset = 0;
      int found_start_offset = -1;
      int found_end_offset = -1;
      // goes to the last match of <member>
      do
	{
	  gtk_text_buffer_get_iter_at_offset (buffer,& start, start_offset);
	  found = gtk_text_iter_forward_search (&start,member,GTK_TEXT_SEARCH_TEXT_ONLY,&match_start,&match_end,NULL);
	  if(found)
	    {
	      found_start_offset = gtk_text_iter_get_offset(&match_start);
	      found_end_offset = gtk_text_iter_get_offset(&match_end);
	      start_offset = found_end_offset;
	    }
	  } while(found);
      
	  if(found_start_offset >= 0)
	    {
	      editor->Scroll(start_offset);
	      editor->SelectText(found_start_offset,found_end_offset);
	    }
    }
}
/*
  support functions
 */

// get file extension
char* get_extension(char* s)
{
  int t = std::strlen(s)-1;
  char* p = &s[t];
  for(; (s != p) && (*p != '.') && (*p != '/'); p--) ;
  return ( s!= p)  && (*p != '/') ? p : static_cast<char*>(0);
}

/*
 answers a toggled .cc <-> .h filename
 user should delete answered buffer
*/
char* ToggledSource(const char* file)
{
  char* localbuff = new char[std::strlen(file)+1];
  char* answer = NULL;
  std::strcpy(localbuff,file);
  char* ext = get_extension(localbuff);
  if(ext)
    {
      *ext = '\0';
      ext++;
    }
  else {
    delete [] localbuff;
    return NULL;
  }
  sprintf(buff,"%s.%s",localbuff,!std::strcmp(ext,"cc") ? "h" : "cc");
  delete [] localbuff;
  answer = new char[std::strlen(buff)+1];
  std::strcpy(answer,buff);
  return answer;
}