summaryrefslogtreecommitdiffstats
path: root/vmchooser/SWindow.cxx
blob: f70e8d22fd9730cb13c09942519127563c24d188 (plain) (blame)
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
287
288

#include "inc/SWindow.h"

#include "inc/functions.h"
#include <iostream>
#include <map>

#include <string.h>

#include <img/gnome_32.xpm>
#include <img/kde_32.xpm>
#include <img/linux_32.xpm>
#include <img/xp_32.xpm>
#include <img/xp_locked_32.xpm>
#include <img/xfce_32.xpm>


using namespace fltk;
using namespace std;


/********************************************************
 * default constructur for the main window
 * ----------------------------------------------------
 * if you want to use default sizes, call first ctor
 ********************************************************/
SWindow::SWindow(int w, int h, char* p)
: fltk::Window(w,h,p),
  go(w/3 + 10, h-40, (2*w)/3 - 20 , 30, "START"),
  exit_btn(10, h-40, w/3 -10, 30, "EXIT"),
  sel(10,10, w-20, h-50)
{
//  sel.indented(1);
  begin();
  add_resizable(sel);
  add(exit_btn);
  add(go);
  width = w;
  height = h;
  
  border(false);
  go.callback(cb_return,this);
  sel.callback(cb_select, this);
  exit_btn.callback(cb_exit, this);
  
  Style* btn_style = new Style(*fltk::ReturnButton::default_style);
  Style* sel_style = new Style(*fltk::Browser::default_style);
  
  
  
  Font* f1 = font("sans");
  //Font* f1bold = f1->bold();
  
  btn_style->textsize(16);
  btn_style->labelsize(16);
  btn_style->labelfont(f1);
  btn_style->textfont(f1);
  
  sel_style->textfont(f1);
  sel_style->textsize(16);
  
  exit_btn.style(btn_style);
  go.style(btn_style);
  sel.style(sel_style);
  
  const int widths[]   = { -1,-1,-1,-1, 0 };
  sel.column_widths(widths);
  
  end();
  sel.take_focus();
};


/********************************************************
 * Callback for ReturnButton at the bottom of the GUI
 * ----------------------------------------------------
 * Should start chosen session entry -> if something is selected
 *********************************************************/
void SWindow::cb_return()
{
  //if(!sel.item()) return;
  //curr = (Item*) sel.item();
  
  if(curr != 0 && curr->user_data()) {
    DataEntry* dat = (DataEntry*) curr->user_data();
    cout << dat->short_description << endl;
    if(dat) {
      runImage(curr, dat);
    }
  }
}


/*******************************************************
 *  Callback for Selection-Browser in the center
 * ----------------------------------------------------
 * Starts the session if required -> Mouse Click
 *******************************************************/
void SWindow::cb_select()
{
  oldcurr = curr;
  curr = (Item*)  sel.item();
  if(!sel.item()) return;
  //cout << "cb_select called with" << sel.item() << endl;
  sel.select_only_this();
  if (sel.item_is_parent() )
    {
      sel.set_item_opened(true);
      return;
    }

  if( curr == oldcurr ) {
    // start image if it has data associated
    // -> double click
    cout << ((DataEntry*)curr->user_data())->short_description << endl;
    if(curr->user_data()) {
      runImage(curr, (DataEntry*) curr->user_data() );
    }
    return;
  }
}


/**********************************************************
 * Put entries in a Linux-Session Group into Browser
 *********************************************************/
void SWindow::set_lin_entries(DataEntry** ent)
{
  this->lin_ent = ent;
  lin_entgroup = (ItemGroup*) sel.add_group("LINUX DESKTOP", &sel);
  map<string, DataEntry*> mapEntry;
  for (int i=0; ent[i] != '\0'; i++)
  {
    mapEntry.insert(make_pair(ent[i]->short_description, ent[i]));
  }
  map<string, DataEntry*>::iterator it= mapEntry.begin();
  for(;it!=mapEntry.end(); it++) {
//    Item* w= (Item*)sel.add_leaf(it->second->short_description.c_str() , lin_entgroup, (void*)it->second );
    Item* w= (Item*)lin_entgroup->add(it->second->short_description.c_str(), (void*)it->second );
    xpmImage* xpm = new xpmImage(get_symbol(it->second));
    ((Widget*) w)->image(xpm);
    w->tooltip(it->second->description.c_str());
    w->callback(&runImage, (void*)it->second);
    
  }
  lin_entgroup->end();
}


/**********************************************************
 * Put entries in a VMWARE-Session Group into Browser
 *********************************************************/
void SWindow::set_entries(DataEntry** ent)
{
  this->ent = ent;
  sort_entries();
  
  entgroup =  (ItemGroup*)sel.add_group("VMWARE SESSIONS", &sel);
  for (int i=0; ent[i] != '\0'; i++)
  {
    Item* w= (Item*)sel.add_leaf(ent[i]->short_description.c_str(), entgroup, (void*)ent[i] );
        
    xpmImage* xpm = new xpmImage(get_symbol(ent[i]));
    ((Widget*) w)->image(xpm);
    w->tooltip(ent[i]->description.c_str());
    w->callback(&runImage, (void*)ent[i]);
     
  }
  entgroup->end();
  
}

/**************************************************************
 * free arrays (which are dynamically allocated)
 **************************************************************/
void SWindow::free_entries()
{
  for (int i=0; ent[i] != '\0'; i++)
    {
      free(ent[i]);
    }
  free(ent);
}



/******************************************************
 * Small helper function to unfold the 2 parent groups
 *
 * ADDED: Now reads session from ~/.vmchooser via helper
 ******************************************************/
void SWindow::unfold_entries(bool lin_entries, bool vm_entries) {
  int ind = 0;
  if(lin_entries) {
    sel.goto_index(ind);
    if(sel.item_is_parent() ) {
      sel.set_item_opened(true);
    }
    ind++;
  }
  if(vm_entries) {
    sel.goto_index(ind);
    if(sel.item_is_parent() ) {
      sel.set_item_opened(true);
    }
  }

  if(! (lin_entries || vm_entries) ) {
    return;
  }
  sel.next_visible();
  sel.select_only_this(ind);
  curr = (Item*) sel.item();
  //sel.set_focus();
  //sel.set_item_selected(true,1);
  //sel.indented(false);

  char* prename = readSession();
  if ( prename == '\0' ) {
    return;
  } else {
    //cout << "readSession returned:" << prename << endl;
    sel.goto_index(0);
    Item* it = (Item*) sel.next();
    
    while( it ) {
      if(! strcmp(prename,it->label()) ) {
          sel.select_only_this(0);
          curr = it;
          return;
      }
      it = (Item*) sel.next();
    }
  }
}


/******************************************************
 * Helper function to get symbols for entries
 ******************************************************/
const char** SWindow::get_symbol(DataEntry* dat) {
  if(dat->imgtype == VMWARE) {
    if(dat->locked) {
    	return xp_locked_32_xpm;
    }
    else {
    	return xp_32_xpm;
    }
  }
  if(dat->imgtype == LINUX) {
    if(dat->short_description.find("KDE")!= string::npos) {
    	return kde_32_xpm;
    }
    if(dat->short_description.find("GNOME")!= string::npos) {
      return gnome_32_xpm;
    }
    if(dat->short_description.find("Xfce")!= string::npos) {
      return xfce_32_xpm;
    }
    return linux_32_xpm;
  }
  return xp_32_xpm;
}


/******************************************************
 * Sort entries to consider priorities
 *
 * -> puts smallest priority number on top
 ******************************************************/
void SWindow::sort_entries() {
  if(ent == '\0' ) {
    return;
  }
  DataEntry* ptr;
  
  // worst case sort - but it is enough for this few entries
  for(int i=0; ent[i] != '\0'; i++) {
    for(int j=0; ent[j] != '\0'; j++) {
      if(ent[j]->priority < ent[i]->priority && j > i) {
        // swap element i with j (as i is alway larger j)
        ptr = ent[i];
        ent[i] = ent[j];
        ent[j] = ptr;
      }
    }
  }
}