File: browser.ec

package info (click to toggle)
ecere-sdk 0.44.15-1
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 97,712 kB
  • ctags: 54,695
  • sloc: ansic: 593,042; makefile: 12,250; yacc: 4,955; lex: 707; objc: 259; python: 252; xml: 102
file content (322 lines) | stat: -rw-r--r-- 8,797 bytes parent folder | download | duplicates (2)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import "HTMLView"

//define homeLocation = "localhost:8080";
//define homeLocation = "www.ecere.com";
define homeLocation = "google.com";

class AddressBar : Window
{
   background = activeBorder;
   tabCycle = true;
   Button back
   {
      this, bevelOver = true, inactive = true, anchor = Anchor { left = 0, top = 0, bottom = 0 }, size = Size { 24 }, hotKey = altLeft, bitmap = { "<:ecere>actions/goPrevious.png" };
      disabled = true;

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         ((Explorer)parent).Back();
         return true;
      }
   };
   Button forward
   {
      this, bevelOver = true, inactive = true, anchor = Anchor { left = 24, top = 0, bottom = 0 }, size = Size { 24 }, hotKey = altRight, bitmap = { "<:ecere>actions/goNext.png" };
      disabled = true;

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         ((Explorer)parent).Forward();
         return true;
      }
   };
   Button home
   {
      this, bevelOver = true, inactive = true, anchor = Anchor { left = 52, top = 0, bottom = 0 }, size = Size { 24 }, hotKey = ctrlH, bitmap = { "<:ecere>actions/goHome.png" };

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         ((Explorer)parent).Go(homeLocation);
         return true;
      }
   };
   Button refresh
   {
      this, bevelOver = true, inactive = true, anchor = Anchor { left = 76, top = 0, bottom = 0 }, size = Size { 24 }, hotKey = f5, bitmap = { "<:ecere>actions/viewRefresh.png" };

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         ((Explorer)parent).Refresh();
         return true;
      }
   };
   Label { this, anchor = Anchor { left = (96+12) }, labeledWindow = address };
   EditBox address
   {
      this, text = "Address:", anchor = Anchor { left = (16+48+96), right = 60, top = 0, bottom = 0 }, hotKey = altD;

      bool NotifyKeyDown(EditBox editBox, Key key, unichar ch)
      {
         if(!go.disabled && (SmartKey)key == enter)
            ((Explorer)parent).Go(editBox.contents);
         return true;
      }

      void NotifyUpdate(EditBox editBox)
      {
         String location = ((Explorer)parent).htmlView.location;
         go.disabled = !strcmp(location ? location : "", editBox.contents);
      }
   };
   Button go
   {
      this, bevelOver = true, inactive = true, text = "Go!", anchor = Anchor { top = 0, right = 0, bottom = 0 }, size = Size { 60 }, hotKey = altG;

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         ((Explorer)parent).Go(address.contents);
         return true;
      }
   };

   bool OnKeyHit(Key key, unichar ch)
   {
      if(key == escape)
         ((Explorer)parent).htmlView.MakeActive();
      return true;
   }
}

class Explorer : Window
{
   FileDialog openFileDialog { caption = "Select a file or enter a URL... " };
   FileDialog saveFileDialog { type = save, caption = "Save to file... " };

   icon = { ":newb.png" };

   tabCycle = true;
   background = activeBorder;
   hasMenuBar = true;
   hasStatusBar = true;
   borderStyle = sizable;
   hasClose = true;
   hasMaximize = true;
   hasMinimize = true;
   text = "Ecere Web Browser";
   size = Size { 800, 600 };
   state = maximized;

   AddressBar addressBar { this, borderStyle = bevel, anchor = Anchor { top = 0, left = 0, right = 0 }, size.h = 26, hotKey = altD };
   HTMLView htmlView { this, borderStyle = deep, hasVertScroll = true, hasHorzScroll = true, anchor = Anchor { left = 0, right = 0, top = 26, bottom = 0 }, NotifyPageOpened = PageOpened, OnOpen = OnOpen };

   // File Menu
   menu = Menu {};
   Menu fileMenu { menu, "File", f };
   MenuItem newWindowItem
   {
      fileMenu, "New Window\tCtrl+N", n, ctrlN;

      bool NotifySelect(MenuItem selection, Modifiers mods)
      {
         Explorer { state = normal }.Create();
         return true;
      }
   };
   MenuItem openItem
   {
      fileMenu, "Open...\tCtrl+O", o, ctrlO;

      bool NotifySelect(MenuItem selection, Modifiers mods)
      {
         if(openFileDialog.Modal() == ok)
            Go(openFileDialog.filePath);
         return true;
      }
   };
   // MenuItem closeItem { fileMenu, "Close\tCtrl-F4", c, NotifySelect = MenuFileClose };
   MenuDivider { fileMenu };
   MenuItem saveAsItem
   {
      fileMenu, "Save As...";

      bool NotifySelect(MenuItem selection, Modifiers mods)
      {
         char fileName[MAX_LOCATION];
         GetLastDirectory(htmlView.location, fileName);
         strcpy(fileName, saveFileDialog.currentDirectory);
         PathCat(fileName, fileName);
         saveFileDialog.filePath = fileName;

         if(saveFileDialog.Modal() == ok)
         {
            File f = FileOpen(htmlView.location, read);
            if(f)
            {
               if(!f.CopyTo(saveFileDialog.filePath))
               {
                  String e = PrintString("Error saving to ", saveFileDialog.filePath);
                  MessageBox { contents = e }.Modal();
                  delete e;
               }
            }
         }
         return true;
      }
   };
   MenuDivider { fileMenu };
   MenuItem exitItem { fileMenu, "Exit\tAlt+F4", x, NotifySelect = MenuFileExit };

   bool PageOpened()
   {
      char caption[MAX_LOCATION];

      strcpy(caption, "Ecere Web Browser - ");
      strcat(caption, htmlView.title);

      text = caption;
      addressBar.address.Clear();
      addressBar.address.PutS(htmlView.fileName);
      return true;
   }

   bool OnPostCreate()
   {
      addressBar.MakeActive();
      return true;
   }

   Array<String> history { };
   int historyPos;

   ~Explorer()
   {
      history.Free();
   }

   bool HTMLView::OnOpen(char * href)
   {
      bool result = true;
      char newLocation[MAX_LOCATION];
      Explorer explorer = (Explorer)parent;

      strcpy(newLocation, location ? location : "");
      if(newLocation[strlen(newLocation)-1] != '/')
         PathCat(newLocation, "..");
      if(href[0] == '/' && href[1] == '/')
      {
         strcpy(newLocation, "http:");
         strcat(newLocation, href);
      }
      else
         PathCat(newLocation, href);

      if(explorer.history.count > explorer.historyPos+1)
      {
         int i;
         for(i = explorer.historyPos+1; i < explorer.history.count; i++)
            delete explorer.history[i];
         explorer.history.count = explorer.historyPos+1;
      }
      explorer.historyPos = explorer.history.count-1;
      explorer.addressBar.back.disabled = (explorer.historyPos == 0);
      explorer.addressBar.forward.disabled = (explorer.historyPos >= explorer.history.count-1);

      Open(newLocation, null);

      explorer.history.Add(CopyString(location));
      explorer.historyPos = explorer.history.count-1;

      explorer.addressBar.back.disabled = (explorer.historyPos == 0);
      explorer.addressBar.forward.disabled = (explorer.historyPos >= explorer.history.count-1);
      return result;
   }

   void Go(const String location)
   {
      if(history.count > historyPos+1)
      {
         int i;
         for(i = historyPos+1; i < history.count; i++)
            delete history[i];
         history.count = historyPos+1;
      }
      history.Add(CopyString(location));
      historyPos = history.count-1;
      addressBar.back.disabled = (historyPos == 0);
      addressBar.forward.disabled = (historyPos >= history.count-1);
      htmlView.MakeActive();
      htmlView.location = location;
   }

   void Refresh()
   {
      htmlView.MakeActive();
      if(history.count)
         htmlView.location = history[historyPos];
   }

   bool Forward()
   {
      if(historyPos < history.count-1)
      {
         historyPos++;
         addressBar.back.disabled = (historyPos == 0);
         addressBar.forward.disabled = (historyPos >= history.count-1);
         htmlView.location = history[historyPos];
         return true;
      }
      return false;
   }

   bool Back()
   {
      if(historyPos > 0)
      {
         historyPos--;
         addressBar.back.disabled = (historyPos == 0);
         addressBar.forward.disabled = (historyPos >= history.count-1);
         htmlView.location = history[historyPos];
         return true;
      }
      return false;
   }

   bool OnKeyHit(Key key, unichar ch)
   {
      if(key == ctrlR)
      {
         Refresh();
         return false;
      }
      return true;
   }
}

#ifndef ECERE_MODULE

class BrowserApp : GuiApplication
{
   //driver = "OpenGL";
   Explorer explorerWindow {};

   bool Init()
   {
      app = this;

   #ifndef ECERE_MODULE
      if(argc > 1)
         explorerWindow.Go(argv[1]);
      else
   #endif
         explorerWindow.Go(homeLocation);
      return true;
   }
}

BrowserApp app;

#else

extern GuiApplication app;

#endif